-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.cpp
212 lines (193 loc) · 7.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// ---------------------------------------------------------
// Mesh Class
// Author : Tamy Boubekeur ([email protected]).
// Copyright (C) 2008 Tamy Boubekeur.
// All rights reserved.
// ---------------------------------------------------------
#include "Mesh.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <GL/glew.h>
using namespace std;
void Mesh::clear () {
clearTopology ();
clearGeometry ();
}
void Mesh::clearGeometry () {
vertices.clear ();
}
void Mesh::clearTopology () {
triangles.clear ();
}
void Mesh::unmarkAllVertices () {
for (unsigned int i = 0; i < vertices.size (); i++)
vertices[i].unmark ();
}
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 (std::vector<Vertex>::iterator it = vertices.begin (); it != vertices.end (); it++)
it->setNormal (Vec3Df (0.0, 0.0, 0.0));
vector<Vec3Df>::const_iterator itNormal = triangleNormals.begin ();
vector<Triangle>::const_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);
}
}
}
}
void Mesh::collectOrderedOneRing (vector<vector<unsigned int> > & oneRing) const {
oneRing.resize (vertices.size ());
for (unsigned int t = 0; t < triangles.size (); t++) {
const Triangle & ti = triangles[t];
for (unsigned int i = 0; i < 3; i++) {
unsigned int vi = ti.getVertex (i);
unsigned int vj = ti.getVertex ((i+1)%3);
unsigned int vk = ti.getVertex ((i+2)%3);
vector<unsigned int> & oneRingVi = oneRing[vi];
vector<unsigned int>::iterator begin = oneRingVi.begin ();
vector<unsigned int>::iterator end = oneRingVi.end ();
vector<unsigned int>::iterator nj = find (begin, end, vj);
vector<unsigned int>::iterator nk = find (begin, end, vk);
if (nj != end && nk == end) {
if (nj == begin)
nj = end;
nj--;
oneRingVi.insert (nj, vk);
} else if (nj == end && nk != end)
oneRingVi.insert (nk, vj);
else if (nj == end && nk == end) {
oneRingVi.push_back (vk);
oneRingVi.push_back (vj);
}
}
}
}
void Mesh::computeDualEdgeMap (EdgeMapIndex & dualVMap1, EdgeMapIndex & dualVMap2) {
for (vector<Triangle>::iterator it = triangles.begin ();
it != triangles.end (); it++) {
for (unsigned int i = 0; i < 3; i++) {
Edge eij (it->getVertex (i), it->getVertex ((i+1)%3));
if (dualVMap1.find (eij) == dualVMap1.end ())
dualVMap1[eij] = it->getVertex ((i+2)%3);
else
dualVMap2[eij] = it->getVertex ((i+2)%3);
}
}
}
void Mesh::markBorderEdges (EdgeMapIndex & edgeMap) {
for (vector<Triangle>::iterator it = triangles.begin ();
it != triangles.end (); it++) {
for (unsigned int i = 0; i < 3; i++) {
unsigned int j = (i+1)%3;
Edge eij (it->getVertex (i), it->getVertex (j));
if (edgeMap.find (eij) == edgeMap.end ())
edgeMap[eij] = 0;
else
edgeMap[eij] += 1;
}
}
}
inline void glVertexVec3Df (const Vec3Df & v) {
glVertex3f (v[0], v[1], v[2]);
}
inline void glNormalVec3Df (const Vec3Df & n) {
glNormal3f (n[0], n[1], n[2]);
}
inline void glDrawPoint (const Vec3Df & pos, const Vec3Df & normal) {
glNormalVec3Df (normal);
glVertexVec3Df (pos);
}
inline void glDrawPoint (const Vertex & v) {
glDrawPoint (v.getPos (), v.getNormal ());
}
void Mesh::renderGL (bool flat) const {
glBegin (GL_TRIANGLES);
for (unsigned int i = 0; i < triangles.size (); i++) {
const Triangle & t = triangles[i];
Vertex v[3];
for (unsigned int j = 0; j < 3; j++)
v[j] = vertices[t.getVertex(j)];
if (flat) {
Vec3Df normal = Vec3Df::crossProduct (v[1].getPos () - v[0].getPos (),
v[2].getPos () - v[0].getPos ());
normal.normalize ();
glNormalVec3Df (normal);
}
for (unsigned int j = 0; j < 3; j++)
if (!flat)
glDrawPoint (v[j]);
else
glVertexVec3Df (v[j].getPos ());
}
glEnd ();
}
void Mesh::loadOFF (const std::string & filename) {
clear ();
ifstream input (filename.c_str ());
if (!input)
throw Exception ("Failing opening the file.");
string magic_word;
input >> magic_word;
if (magic_word != "OFF")
throw Exception ("Not an OFF file.");
unsigned int numOfVertices, numOfTriangles, numOfWhat;
input >> numOfVertices >> numOfTriangles >> numOfWhat;
for (unsigned int i = 0; i < numOfVertices; i++) {
Vec3Df pos;
Vec3Df col;
input >> pos;
vertices.push_back (Vertex (pos, Vec3Df (1.0, 0.0, 0.0)));
}
for (unsigned int i = 0; i < numOfTriangles; i++) {
unsigned int polygonSize;
input >> polygonSize;
vector<unsigned int> index (polygonSize);
for (unsigned int j = 0; j < polygonSize; j++)
input >> index[j];
for (unsigned int j = 1; j < (polygonSize - 1); j++)
triangles.push_back (Triangle (index[0], index[j], index[j+1]));
}
input.close ();
recomputeSmoothVertexNormals (0);
}