-
Notifications
You must be signed in to change notification settings - Fork 1
/
vertex.cc
299 lines (278 loc) · 6.65 KB
/
vertex.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "vertex.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "edge.h"
#include "object.h"
using std::cout;
using std::endl;
void Vertex::setVertexNiceness (int val)
{
o->setVertexNiceness(this,val);
}
int Vertex::getVertexNiceness (void)
{
return o->getVertexNiceness(this);
}
bool Vertex::vertexIsNice (void)
{
return o->vertexIsNice(this);
}
Vertex::Vertex (char* triplet,Object *q)
:index(0),cl(NULL),o(q),e(),f(),nf()
{
char val[80];
char *eptr;
int i;
char *cp=triplet;
// get past 'Vertex'
while (strchr("Vertx",*triplet)!=NULL) {triplet++;}
// grab vertex index
while (strchr(" \t,",*triplet)!=NULL) { triplet++; }
i=0;
while (strchr("0123456789+-eE.",*triplet)!=NULL)
{
val[i++] = *triplet++;
}
val[i]=0;
index = atoi(val);
if (val==eptr)
{
index=0;
printf("Error in reading vertex index\n");
return;
}
// grab x coord
while (strchr(" \t,",*triplet)!=NULL) { triplet++; }
i=0;
while (strchr("0123456789+-eE.",*triplet)!=NULL)
{
val[i++] = *triplet++;
}
val[i]=0;
pN[0]=strtod(val,&eptr);
if (val==eptr)
{
printf("Error in reading vertex\n");
printf("Error in reading vertex: string %s\n",cp);
return;
}
// grab y coord
while (strchr(" \t,",*triplet)!=NULL) triplet++;
i=0;
while (strchr("0123456789+-eE.",*triplet))
{
val[i++] = *triplet++;
}
val[i]=0;
pN[1]=strtod(val,&eptr);
if (val==eptr)
{
printf("Error in reading vertex\n");
printf("Error in reading vertex: string %s\n",cp);
return;
}
// grab z coord
while (strchr(" \t,",*triplet)!=NULL) triplet++;
i=0;
while (strchr("0123456789+-eE.",*triplet))
{
val[i++] = *triplet++;
}
val[i]=0;
pN[2]=strtod(val,&eptr);
if (val==eptr)
{
printf("Error in reading vertex\n");
printf("Error in reading vertex: string %s\n",cp);
return;
}
pC[0]=pN[0];
pC[1]=pN[1];
pC[2]=pN[2];
}
void Vertex::getNormal (double *n)
{
f_iterator i;
double t[3],theta,thetaT=0,L;
n[0]=n[1]=n[2]=0;
// for each adjacent face
for (i=f.begin();i!=f.end();i++)
{
// get coordinates of polygon normal
(*i)->getNormal(t);
L=sqrt(t[0]*t[0]+t[1]*t[1]+t[2]*t[2]);
theta=(*i)->getAngle(this);
thetaT+=theta;
// and add to sum
n[0] += t[0]/L*theta;
n[1] += t[1]/L*theta;
n[2] += t[2]/L*theta;
}
n[0] = n[0]/f.size()/thetaT;
n[1] = n[1]/f.size()/thetaT;
n[2] = n[2]/f.size()/thetaT;
}
void Vertex::getAdjacentVertices (std::vector<Vertex*> &a)
{
a.clear();
e_iterator i;
// for each adjacent edge
for (i=e.begin();i!=e.end();i++)
{
Vertex *v1=NULL,*v2=NULL,*o1=NULL,*o2=NULL;
(*i)->getVertices(v1,v2,o1,o2);
// find vertex different from self and add different vertex to vector
if (v1->index!=index){a.push_back(v1);}
else if (v2->index!=index) {a.push_back(v2);}
else { printf("Error. both vertices of edge are equal to current vertex.\n"); exit(1); }
}
}
void Vertex:: getAdjacentFaces (hset_f &fset)
{
e_iterator i;
// for each adjacent edge
for (i=e.begin();i!=e.end();i++)
{
// add edge faces to set
fset.insert((*i)->ptr_f1());
fset.insert((*i)->ptr_f2());
}
}
bool Vertex::scanAdjFaces (Edge *se,Face *sf,bool &nonman)
{
// collect touched edges
vec_e te;
te.push_back(se);
// collect touched faces
vec_f tf;
tf.push_back(sf);
// get new edge
se=sf->getNewEdge(se,this);
if (se->isManifold()==false)
{
// vertex manifoldness cannot be determined
// because it's complicated, so just return flag
nonman=true;
return false;
}
// while new edge has not already been touched
while (find(te.begin(),te.end(),se)==te.end())
{
// keep new edge
te.push_back(se);
// get new face
sf=se->getNewFace(sf);
if (sf==NULL) {break;}
// keep new face
tf.push_back(sf);
// get new edge
se=sf->getNewEdge(se,this);
if (se->isManifold()==false)
{
// vertex manifoldness cannot be determined
// because it's complicated, so just return flag
nonman=true;
return false;
}
}
// if number of touched faces != number of vertex adjacent faces
return tf.size()==f.size();
}
bool Vertex::isManifold (bool flag)
{
// cout << "Vertex::isManifold: @start: flag=" << flag << endl;
// try to "walk" around vertex adjacent faces
// using adjacent face edges
// if an edge is nonmanifold then abort mission
// stop when the walk returns to starting adjacent face
if (e.empty()==true)
{
cout << "\n\nVertex::isManifold: Error."
<< " Vertex was not an 'orphan', but has no edges.\n";
cout << "\n\nVertex::isManifold: Confused vertex:\n";
print(cout);
cout << endl;
exit(1);
}
// grab starting edge
Edge *se=e.front();
// grab starting face
Face *sf=NULL,*cw=NULL,*ccw=NULL;
// if edge is manifold
if (se->isManifold()==true)
{
// grab starting face
if (se->ptr_f1()!=NULL){ cw = se->ptr_f1(); }
if (se->ptr_f2()!=NULL) {
if (cw==NULL) { cw = se->ptr_f2(); }
else { ccw = se->ptr_f2(); }
}
if (cw==NULL)
{
cout << "Error. Both edge faces are NULL!\n"; exit(1);
}
}
else
{
// vertex manifoldness cannot be determined
// because the starting edge is not manifold
// thus determining manifoldness of vertex
// is complicated, so just return flag
return flag;
// TODO: report number of vertices for which manifoldness
// was not determined and alert user
}
// try clockwise
sf=cw;
bool nonman=false;
// if fail i.e. not all adjacent faces touched
if (scanAdjFaces(se,sf,nonman)==false)
{
// if nonmanifold edge found, then bail
if (nonman==true){return flag;}
// if ccw face is not NULL
if (ccw!=NULL)
{
// try counter-clockwise
sf=ccw;
// if fail i.e. all adjacent faces still not touched
if (scanAdjFaces(se,sf,nonman)==false)
{
// record offending edge
//o->nonman_v.push_back(this);
o->addNonmanVertices(this);
return false;
}
else
{
return flag;
}
}
else
{
// record offending edge
//o->nonman_v.push_back(this);
o->addNonmanVertices(this);
return false;
}
}
else
{
// all adjacent faces touched
return flag;
}
/* if (se->getStartingFace(sf)==false)
{
// vertex manifoldness cannot be determined
// because the starting edge is not manifold
// thus determining manifoldness of vertex
// is complicated, so just return flag
return flag;
// TODO: report number of vertices for which manifoldness
// was not determined and alert user
}
*/
}