OpenGL : Laplacian Smoothing & Taubin Smoothing

2024. 11. 24. 00:21·C++
반응형

Laplacian Smoothing

void Mesh::Laplacian(int iter)
{
	vector<Vec3<double>> newPos;

	for (int it = 0; it < iter; it++) {
		for (auto v : _vertices) {
			auto pos = v->_pos;
			Vec3<double> p;
			for (auto nv : v->_nbVertices)
			{
				p += (nv->_pos - pos) / 2.0f;
			}
			p /= v->_nbVertices.size();
			pos += p;
			newPos.push_back(pos);
		}

		for (int i = 0; i < _vertices.size(); i++)
		{
			auto v = _vertices[i];
			v->_pos = newPos[i];
		}
		newPos.clear();
	}
	computeNormal();
}

Taubin Smoothing

void Mesh::Taubin(int iter)
{
	vector<Vec3<double>> newPos;

	for (int it = 0; it < iter; it++) {
		for (auto v : _vertices) {
			auto pos = v->_pos;
			Vec3<double> p;
			for (auto nv : v->_nbVertices)
			{
				p += (nv->_pos - pos) / 2.0f;
			}
			p /= v->_nbVertices.size();
			pos += p;
			newPos.push_back(pos);
		}

		for (int i = 0; i < _vertices.size(); i++)
		{
			auto v = _vertices[i];
			v->_pos = newPos[i];
		}
		newPos.clear();
	}
	for (int it = 0; it < iter; it++) {
		for (auto v : _vertices) {
			auto pos = v->_pos;
			Vec3<double> p;
			for (auto nv : v->_nbVertices)
			{
				p += (nv->_pos - pos) / 2.0f;
			}
			p /= v->_nbVertices.size();
			p *= -1;
			pos += p;
			newPos.push_back(pos);
		}

		for (int i = 0; i < _vertices.size(); i++)
		{
			auto v = _vertices[i];
			v->_pos = newPos[i];
		}
		newPos.clear();
	}
	computeNormal();
}

 

void Mesh::withCot(int iter)
{
	vector<Vec3<double>> newPos;
	vector<Vec3<double>> overlapPos;
	
	for (int it = 0; it < iter; it++) 
	{
		for (auto v : _vertices) 
		{
			auto pos = v->_pos;
			Vec3<double> p;
			double total_w = 0;
			for (auto nv : v->_nbVertices)
			{
				for (auto fv : v->_nbFaces)
				{
					if (fv->getIndex(nv) != -1)
					{
						int index_v = fv->getIndex(v);
						int index_nv = fv->getIndex(nv);
						overlapPos.push_back(fv->v(3 - index_v - index_nv)->_pos);
					}
				}
				//a,b 계산
				double a1 = sqrt(pow(v->_pos.x() - overlapPos[0].x(), 2) + pow(v->_pos.y() - overlapPos[0].y(), 2) + pow(v->_pos.z() - overlapPos[0].z(), 2));
				double b1 = sqrt(pow(nv->_pos.x() - overlapPos[0].x(), 2) + pow(nv->_pos.y() - overlapPos[0].y(), 2) + pow(nv->_pos.z() - overlapPos[0].z(), 2));
				//가중치 계산
				double theta1 = (v->_pos - overlapPos[0]).Dot(nv->_pos - overlapPos[0]);
				double w1 = acos(theta1 / (a1 * b1));

				double a2 = sqrt(pow(v->_pos.x() - overlapPos[1].x(), 2) + pow(v->_pos.y() - overlapPos[1].y(), 2) + pow(v->_pos.z() - overlapPos[1].z(), 2));
				double b2 = sqrt(pow(nv->_pos.x() - overlapPos[1].x(), 2) + pow(nv->_pos.y() - overlapPos[1].y(), 2) + pow(nv->_pos.z() - overlapPos[1].z(), 2));

				double theta2 = (v->_pos - overlapPos[1]).Dot(nv->_pos - overlapPos[1]);
				double w2 = acos(theta2 / (a2 * b2));

				//가중치를 nv pos에 곱함
				p += (nv->_pos-v->_pos) * 0.5 * ((1 / tan(w1)) + (1 / tan(w2)));
				total_w += 0.5 * ((1 / tan(w1)) + (1 / tan(w2)));

				//overlap 비움
				overlapPos.clear();
			}
			p *= (1 / total_w);
			pos += p;
			newPos.push_back(pos);
		}

		for (int i = 0; i < _vertices.size(); i++)
		{
			auto v = _vertices[i];
			v->_pos = newPos[i];
		}
		newPos.clear();
	}
	computeNormal();
}
반응형
저작자표시 (새창열림)

'C++' 카테고리의 다른 글

함수 포인터 (typedef, using, std::function)  (1) 2024.12.14
C++ Linked List  (0) 2024.12.07
cout 소수점 고정  (0) 2024.11.24
string::find  (1) 2024.11.24
STL : sort algorithm  (0) 2024.11.24
'C++' 카테고리의 다른 글
  • 함수 포인터 (typedef, using, std::function)
  • C++ Linked List
  • cout 소수점 고정
  • string::find
숯불돼지왕갈비
숯불돼지왕갈비
  • 숯불돼지왕갈비
    게임 개발 공부기
    숯불돼지왕갈비
  • 전체
    오늘
    어제
    • 분류 전체보기 (302)
      • 학교수업 (165)
      • 취업강의 (6)
      • C++ (49)
        • 코딩 테스트 (4)
      • Unreal Engine 5 (25)
        • MMORPG 개발 (25)
      • Unreal Engine 4 (44)
        • Omak Project (3)
        • Unreal Engine 4 개발일지 (9)
        • Unreal Engine 4 (32)
      • Unity (1)
        • 개발 일지 (1)
      • 수학 (3)
        • 소프트웨어 공학용 수학 (3)
      • DirectX 11 (4)
      • 게임 디자인 패턴 (2)
      • 포트폴리오 (1)
      • 자격증 (1)
        • 정보처리기사 (0)
        • SQLD (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
숯불돼지왕갈비
OpenGL : Laplacian Smoothing & Taubin Smoothing
상단으로

티스토리툴바