-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalfEdge.cpp
495 lines (402 loc) · 12.6 KB
/
HalfEdge.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//
// Created by rkindela on 12-04-2018.
//
#include "HalfEdge.h"
#include "Vector3D.h"
std::ostream& operator<<(std::ostream& os, const HalfEdge& e) {
os << "HALF_EDGE address: " << long(&e) << endl;
if (e.vertex != nullptr)
os << "HalfEdge: { " << e.vertex << endl;
else
os << "HalfEdge:{ vertex: nullptr" << endl;
/**
* Vertex* vertex;
HalfEdge* next;
HalfEdge* pair;
Face* face;
*/
if (e.next != nullptr)
os << ", NEXT: " << (*e.next) << endl;
else
os << ", NEXT: nullptr" << endl;
if (e.pair != nullptr)
os << ", PAIR: " << (*e.pair) << endl;
else
os << ", PAIR: nullptr" << endl;
if (e.face != nullptr)
os << ", " << (*e.face) << endl;
else
os << ", FACE: nullptr" << endl;
os << "}\n";
return os;
}
std::ostream& operator<<(std::ostream& os, const Vertex& e)
{
POINT* point = PointRepository::instance()->getPoint(e.pointIndex);
if (point != nullptr)
os<<" VERTEX: { "<<point;
else
os << "VERTEX: POINT: nullptr";
if (e.edge != nullptr)
os<<", EDGE: "<<(*e.edge);
else
os<<", EDGE: nullptr";
os <<"}\n";
return os;
}
std::ostream& operator<<(std::ostream& os, const Face& e)
{
if (e.edge != nullptr)
os<<"FACE: { EDGE: "<<(*e.edge);
else
os<<"FACE: { EDGE: nullptr";
os <<"}\n";
return os;
}
HalfEdge::HalfEdge(): vertex(nullptr), next(nullptr), pair(nullptr), face(nullptr)
{
}
HalfEdge::HalfEdge(const HalfEdge& half_edge): vertex(half_edge.vertex), next(half_edge.next), pair(half_edge.pair), face(half_edge.face)
{
}
int HalfEdge::getDistance(HalfEdge* otherEdge)
{
HalfEdge* cursor = this;
int count = 0;
if (otherEdge == nullptr)
return -1;
if(otherEdge == cursor)
return 0;
do{
++count;
cursor = cursor->next;
}while(cursor != this && cursor != otherEdge);
if(cursor == otherEdge)
return count;
return -1;
}
int HalfEdge::getEdgeSize()
{
int pa = -1;
int pb = -1;
if (next != nullptr && next->vertex != nullptr)
pb = next->vertex->pointIndex;
if (vertex != nullptr)
pa = vertex->pointIndex;
return PointRepository::instance()->squareMagnitude(pa, pb);
}
Vector3D* HalfEdge::getOrientedVector3D()
{
int pa = -1;
int pb = -1;
if (next != nullptr && next->vertex != nullptr)
pb = next->vertex->pointIndex;
if (vertex != nullptr)
pa = vertex->pointIndex;
return PointRepository::instance()->getVector3dFromPoints(pa, pb);
}
bool HalfEdge::areIntersected(HalfEdge* otherEdge)
{
if(otherEdge == nullptr ||
otherEdge->next == nullptr ||
this->next == nullptr)
return false;
if(otherEdge->vertex == nullptr ||
otherEdge->next->vertex == nullptr ||
this->vertex == nullptr ||
this->next->vertex == nullptr)
return false;
int p1 = this->vertex->pointIndex;
int q1 = this->next->vertex->pointIndex;
int p2 = otherEdge->vertex->pointIndex;
int q2 = otherEdge->next->vertex->pointIndex;
return PointRepository::instance()->areIntersected(p1, q1, p2, q2);
}
bool Vertex::operator ==(const Vertex& e)
{
return (pointIndex == e.pointIndex);
}
vector<Face*>* Vertex::getAdjacentFaces()
{
vector<Face*>* adjacentFaces = new vector<Face*>();
if(edge == nullptr)
return adjacentFaces;
HalfEdge* cursor = edge;
do
{
adjacentFaces->push_back(cursor->face);
if (cursor->pair != nullptr)
cursor = cursor->pair->next;
}while(cursor != edge && cursor != nullptr);
return adjacentFaces;
}
vector<Vertex*>* Vertex::getLink()
{
vector<Vertex*>* oppositeVertexes = new vector<Vertex*>();
if(edge == nullptr)
return oppositeVertexes;
HalfEdge* cursor = edge;
do
{
HalfEdge* temp = cursor;
do{
if(temp->vertex->pointIndex != pointIndex)
{
oppositeVertexes->push_back(temp->vertex);
}
temp = temp->next;
}
while(temp != cursor);
if (cursor->pair != nullptr)
cursor = cursor->pair->next;
}while(cursor != edge && cursor != nullptr);
return oppositeVertexes;
}
HalfEdge* Face::findVertex(Vertex* v) const
{
HalfEdge* tfirst = this->edge;
HalfEdge* tcursor = this->edge;
bool found = false;
do{
if (tcursor != nullptr)
{
if (tcursor->vertex != nullptr)
{
if (tcursor->vertex->pointIndex == v->pointIndex)
found = true;
}
tcursor = tcursor->next;
}
}while(tcursor != tfirst && !found);
if (found)
return tcursor;
return nullptr;
}
bool Face::inside(int pointIndex) const
{
HalfEdge* tfirst = this->edge;
HalfEdge* tcursor = this->edge;
bool found = true;
do{
if (tcursor != nullptr && tcursor->next != nullptr)
{
if (tcursor->vertex != nullptr && tcursor->next->vertex != nullptr)
{
int a = tcursor->vertex->pointIndex;
int b = tcursor->next->vertex->pointIndex;
if (tcursor->vertex->pointIndex != pointIndex && PointRepository::instance()->area2(a, b, pointIndex) > 0)
found &= true;
else
found &= false;
}
tcursor = tcursor->next;
}
}while(tcursor != tfirst && !found);
return found;
}
HalfEdge* Face::getEdgeHoldingPoint(int pointIndex) const
{
HalfEdge* tfirst = this->edge;
HalfEdge* tcursor = this->edge;
do{
if (tcursor != nullptr && tcursor->next != nullptr)
{
if (tcursor->vertex != nullptr && tcursor->next->vertex != nullptr)
{
int a = tcursor->vertex->pointIndex;
int b = tcursor->next->vertex->pointIndex;
if (tcursor->vertex->pointIndex != pointIndex && PointRepository::instance()->area2(a, b, pointIndex) == 0)
return tcursor;
}
tcursor = tcursor->next;
}
}while(tcursor != tfirst);
return nullptr;
}
HalfEdge* Face::buildFromPoints(const std::vector<int>& pointcloud)
{
int count = pointcloud.size(); // obtengo la cantidad de puntos
edge = new HalfEdge(); // se crea el half edge
edge->face = this; // se modifica la cara del edge
edge->face->edge = edge; // se modifica el edge de la cara
edge->pair = nullptr; //se inicializa el pair a nullptr
HalfEdge * cursor = edge; // se crea un cursor para iterar por el halfedge
for (int i = 0; i < count; i++) // por cada punto se hace lo siguiente
{
Vertex* vertex = new Vertex(); // se crea el vertice
vertex->pointIndex = pointcloud[i]; // se cambia el punto
cursor->vertex = vertex; // se modifica el vertice del halfedge actual
vertex->edge = cursor; // se activa el half edge del vertice
if (i < count-1) // se genera half edge seguiente solo hasta el penultimo half edge
{
cursor = new HalfEdge();
cursor->face = this;
cursor->pair = nullptr;
vertex->edge->next = cursor;
}
}
cursor->next = edge; // y se agrega el siguiente del ultimo como el primero y se termina.
return edge;
}
bool Face::operator ==(const Face& face)
{
int count = face.getVertexesCount();
if (this->getVertexesCount() != count)
return false;
HalfEdge* ffirst = face.findVertex(edge->vertex);
HalfEdge* fcursor = ffirst;
HalfEdge* tfirst = this->edge;
HalfEdge* tcursor = this->edge;
do{
if (tcursor != nullptr && fcursor != nullptr)
{
if (tcursor->vertex != nullptr && fcursor->vertex != nullptr)
{
if (!((*tcursor->vertex) == (*fcursor->vertex)))
return false;
}
else
return false;
fcursor = fcursor->next;
tcursor = tcursor->next;
}
}while(tcursor != tfirst && fcursor != ffirst);
if (!(tcursor != tfirst && fcursor != ffirst))
return false;
return true;
}
bool Face::intersectWithSegment(int p0, int p1, int SMALL_NUM)
{
/**
* Vector u = S.P1 - S.P0;
Vector w = S.P0 - Pn.V0;
float D = dot(Pn.n, u);
float N = -dot(Pn.n, w);
if (fabs(D) < SMALL_NUM) { // segment is parallel to plane
if (N == 0) // segment lies in plane
return 2;
else
return 0; // no intersection
}
// they are not parallel
// compute intersect param
float sI = N / D;
if (sI < 0 || sI > 1)
return 0; // no intersection
*I = S.P0 + sI * u; // compute segment intersect point
return 1;
* */
Vector3D* u = PointRepository::instance()->getVector3dFromPoints(p0, p1);
Vector3D* w = PointRepository::instance()->getVector3dFromPoints(edge->vertex->pointIndex, p0);
Vector3D* n = this->getNormal();
int D = n->dot(*u);
int N = n->dot(*w);
delete n;
delete w;
delete u;
if (SMALL_NUM == -1) {
vector<int> *pointList = this->getVertexesIndices();
SMALL_NUM = PointRepository::instance()->generalArea2(*pointList);
}
if (abs(D) < SMALL_NUM)
{
if (N == 0)
return true;
else
return false;
}
float sI = N / D;
if (sI < 0 || sI > 1)
return 0; // no intersection
return 1;
}
int Face::getVertexesCount() const
{
HalfEdge* first = this->edge;
HalfEdge* cursor = first;
int count = 0;
if (first == nullptr)
return 0;
do{
if (cursor != nullptr)
{
if (cursor->vertex != nullptr)
{
++count;
}
cursor = cursor->next;
}
}while(cursor != first);
return count;
}
vector<int>* Face::getVertexesIndices()
{
vector<int>* indexes = new vector<int>();
HalfEdge* first = this->edge;
HalfEdge* cursor = first;
if (first == nullptr)
return indexes;
do{
if (cursor != nullptr)
{
if (cursor->vertex != nullptr)
{
indexes->push_back(cursor->vertex->pointIndex);
}
cursor = cursor->next;
}
}while(cursor != first);
return indexes;
}
bool Face::inLeft(Face* nFace)
{
vector<int>* listV = getVertexesIndices();
if (listV->size() > 3)
return false;
HalfEdge* cursor = nFace->edge;
int vertexCount = 0, inleftCount = 0, n = listV->size();
do{
int a = (*listV)[0];
int b = (*listV)[1];
int c = (*listV)[2];
++vertexCount;
Vertex* v = cursor->vertex;
if (v != nullptr)
{
if (PointRepository::instance()->volume6(a, b, c, v->pointIndex) >= 0)
++inleftCount;
}
}while(cursor != nFace->edge);
delete listV;
return vertexCount == inleftCount;
}
bool Face::areIntersected(Face* nFace)
{
vector<int>* listV = getVertexesIndices();
if (listV->size() > 3)
return false;
HalfEdge* cursor = nFace->edge;
int vertexCount = 0, inleftCount = 0, n = listV->size();
do{
int a = (*listV)[0];
int b = (*listV)[1];
int c = (*listV)[2];
++vertexCount;
Vertex* v = cursor->vertex;
if (v != nullptr)
{
if (PointRepository::instance()->volume6(a, b, c, v->pointIndex) >= 0)
++inleftCount;
}
}while(cursor != nFace->edge);
delete listV;
return (inleftCount > 0 && vertexCount != inleftCount);
}
Vector3D* Face::getNormal()
{
if(edge == nullptr || edge->next == nullptr)
return nullptr;
Vector3D* A = edge->getOrientedVector3D();
Vector3D* B = edge->next->getOrientedVector3D();
return &(A->cross(*B));
}