-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.cpp
99 lines (91 loc) · 4.03 KB
/
Mesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// --------------------------------------------------------------------------
// gMini,
// a minimal Glut/OpenGL app to extend
//
// Copyright(C) 2007-2009
// Tamy Boubekeur
//
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
// for more details.
//
// --------------------------------------------------------------------------
#include "Mesh.h"
#include <algorithm>
using namespace std;
void Mesh::clear () {
clearTopology ();
clearGeometry ();
}
void Mesh::clearGeometry () {
vertices.clear ();
}
void Mesh::clearTopology () {
triangles.clear ();
}
void Mesh::computeTriangleNormals (vector<Vec3Df> & triangleNormals) {
for (vector<Triangle>::const_iterator it = triangles.begin ();
it != triangles.end ();
it++) {
Vec3Df e01 (vertices[it->getVertex (1)].getPos ()
- vertices[it->getVertex (0)].getPos ());
Vec3Df e02 (vertices[it->getVertex (2)].getPos ()
- vertices[it->getVertex (0)].getPos ());
Vec3Df n (Vec3Df::crossProduct (e01, e02));
n.normalize ();
triangleNormals.push_back (n);
}
}
void Mesh::recomputeSmoothVertexNormals (unsigned int normWeight) {
vector<Vec3Df> triangleNormals;
computeTriangleNormals (triangleNormals);
for (vector<Vertex>::iterator it = vertices.begin ();
it != vertices.end ();
it++)
it->setNormal (Vec3Df (0.0, 0.0, 0.0));
vector<Vec3Df>::iterator itNormal = triangleNormals.begin ();
vector<Triangle>::iterator it = triangles.begin ();
for ( ; it != triangles.end (); it++, itNormal++)
for (unsigned int j = 0; j < 3; j++) {
Vertex & vj = vertices[it->getVertex (j)];
float w = 1.0; // uniform weights
Vec3Df e0 = vertices[it->getVertex ((j+1)%3)].getPos ()
- vj.getPos ();
Vec3Df e1 = vertices[it->getVertex ((j+2)%3)].getPos ()
- vj.getPos ();
if (normWeight == 1) { // area weight
w = Vec3Df::crossProduct (e0, e1).getLength () / 2.0;
} else if (normWeight == 2) { // angle weight
e0.normalize ();
e1.normalize ();
w = (2.0 - (Vec3Df::dotProduct (e0, e1) + 1.0)) / 2.0;
}
if (w <= 0.0)
continue;
vj.setNormal (vj.getNormal () + (*itNormal) * w);
}
Vertex::normalizeNormals (vertices);
}
void Mesh::collectOneRing (vector<vector<unsigned int> > & oneRing) const {
oneRing.resize (vertices.size ());
for (unsigned int i = 0; i < triangles.size (); i++) {
const Triangle & ti = triangles[i];
for (unsigned int j = 0; j < 3; j++) {
unsigned int vj = ti.getVertex (j);
for (unsigned int k = 1; k < 3; k++) {
unsigned int vk = ti.getVertex ((j+k)%3);
if (find (oneRing[vj].begin (), oneRing[vj].end (), vk) == oneRing[vj].end ())
oneRing[vj].push_back (vk);
}
}
}
}