-
Notifications
You must be signed in to change notification settings - Fork 2
/
hull.cpp
1597 lines (1419 loc) · 43.1 KB
/
hull.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright John E. Lloyd, 2004. All rights reserved. Permission to use,
* copy, modify and redistribute is granted, provided that this copyright
* notice is retained and the author is given credit whenever appropriate.
*
* This software is distributed "as is", without any warranty, including
* any implied warranty of merchantability or fitness for a particular
* use. The author assumes no responsibility for, and shall not be liable
* for, any special, indirect, or consequential damages, or any damages
* whatsoever, arising out of or in connection with the use of this
* software.
*/
#include "hull.h"
template <class TV>
class HalfEdge;
template <class TV>
class Vertex;
template <class TV>
class FaceList;
static const double DOUBLE_PREC = 2.2204460492503131e-16;
static const double INF = 1e99;
typedef Vector<double,3> V3d;
typedef Vector<double,2> V2d;
typedef Vector<int,3> V3i;
static const int VISIBLE = 1;
static const int NON_CONVEX = 2;
static const int DELETED = 3;
inline V3d v3d (double x, double y, double z) {
V3d res(x, y, z);
return res;
}
inline V2d v2d (double x, double y) {
V2d res(x, y);
return res;
}
inline V3i v3i (int x, int y, int z) {
V3i res(x, y, z);
return res;
}
template<class TV>
class Face {
private:
TV normal;
TV centroid;
void computeNormalAndCentroid(void);
void computeNormalAndCentroid(double minArea);
double areaSquared (HalfEdge<TV>* hedge0, HalfEdge<TV>* hedge1);
public:
HalfEdge<TV>* he0;
double area;
double planeOffset;
int mark = VISIBLE;
int index;
int numVerts;
Face<TV>* next;
Vertex<TV>* outside;
TV computeCentroid (void);
TV computeNormal (double minArea);
TV computeNormal (void);
static Face<TV>* createTriangle (Vertex<TV>* v0, Vertex<TV>* v1, Vertex<TV>* v2);
static Face<TV>* createTriangle (Vertex<TV>* v0, Vertex<TV>* v1, Vertex<TV>* v2, double minArea);
Face (void) : he0(NULL), mark(VISIBLE), next(NULL), outside(NULL) { }
HalfEdge<TV>* getEdge(int i);
HalfEdge<TV>* getFirstEdge(void) { return he0; }
HalfEdge<TV>* findEdge (Vertex<TV>* vt, Vertex<TV>* vh);
double distanceToPlane (TV p);
TV getNormal (void) { return normal; }
TV getCentroid (void) { return centroid; }
int numVertices(void) { return numVerts; }
std::string getVertexString (void);
std::vector< int > getVertexIndices (void);
Face<TV>* connectHalfEdges (HalfEdge<TV>* hedgePrev, HalfEdge<TV>* hedge);
void checkConsistency(void);
std::vector<Face<TV>*> mergeAdjacentFace (HalfEdge<TV>* hedgeAdj);
void triangulate (FaceList<TV>* newFaces, double minArea);
};
template<class TV>
class FaceList {
private:
Face<TV>* head;
Face<TV>* tail;
public:
void clear(void) { head = tail = NULL; }
void add (Face<TV>* vtx);
inline Face<TV>* first(void) { return head; }
inline bool isEmpty () { return head == NULL; }
FaceList (void) : head(NULL), tail(NULL) { }
};
template<class TV>
class HalfEdge {
public:
Vertex<TV>* vertex;
Face<TV>* face;
HalfEdge<TV>* next;
HalfEdge<TV>* prev;
// Half-edge associated with the opposite triangle adjacent to this edge.
HalfEdge<TV>* opposite;
// Constructs a HalfEdge with head vertex v and left-hand triangular face f.
HalfEdge (Vertex<TV>* v, Face<TV>* f) : vertex(v), face(f), next(NULL), prev(NULL) { }
HalfEdge (void) : vertex(NULL), face(NULL), next(NULL), prev(NULL) { }
// Sets the value of the next edge adjacent (counter-clockwise) to
// this one within the triangle.
void setNext (HalfEdge<TV>* edge) { next = edge; }
// Gets the value of the next edge adjacent ccw to this one within the triangle.
HalfEdge<TV>* getNext(void) { return next; }
// Sets the value of the previous edge adjacent cw to this one within the triangle.
void setPrev (HalfEdge<TV>* edge) { prev = edge; }
// Gets the value of the previous edge adjacent cw to this one within the triangle.
HalfEdge<TV>* getPrev(void) { return prev; }
// Returns the triangular face located to the left of this half-edge
Face<TV>* getFace(void) { return face; }
// Returns the half-edge opposite to this half-edge.
HalfEdge<TV>* getOpposite(void) { return opposite; }
void setOpposite (HalfEdge<TV>* edge) {
opposite = edge;
edge->opposite = this;
}
// Returns the head vertex associated with this half-edge.
Vertex<TV>* head(void) { return vertex; }
// Returns the tail vertex associated with this half-edge.
Vertex<TV>* tail(void) { return prev != NULL ? prev->vertex : NULL; }
// Returns the opposite triangular face associated with this half-edge.
Face<TV>* oppositeFace(void) { return opposite != NULL ? opposite->face : NULL; }
// Produces a string identifying this half-edge by the point
// index values of its tail and head vertices.
std::string getVertexString(void);
// Returns the length of this half-edge.
double length(void);
double lengthSquared(void);
};
template<class TV>
class Vertex {
public:
TV pnt;
// back index into an array
int index;
Vertex<TV>* prev;
Vertex<TV>* next;
Face<TV>* face;
Vertex(TV pt, int idx) : pnt(pt), index(idx) { }
Vertex (void) : prev(NULL), next(NULL), face(NULL) { }
};
template<class TV>
class VertexList {
private:
Vertex<TV>* head;
Vertex<TV>* tail;
public:
VertexList (void) : head(NULL), tail(NULL) { }
void clear(void) { head = tail = NULL; }
void add (Vertex<TV>* vtx);
// Adds a chain of vertices to the end of this list.
void addAll (Vertex<TV>* vtx);
// Deletes a vertex from this list.
void del (Vertex<TV>* vtx);
// Deletes a chain of vertices from this list.
void del (Vertex<TV>* vtx1, Vertex<TV>* vtx2);
// Inserts a vertex into this list before another specificed vertex.
void insertBefore (Vertex<TV>* vtx, Vertex<TV>* next);
// Returns the first element in this list.
Vertex<TV>* first(void) { return head; }
bool isEmpty(void) { return head == NULL; }
};
static const int CLOCKWISE = 0x1;
static const int POINT_RELATIVE = 0x8;
static const double AUTOMATIC_TOLERANCE = -1;
static const int NONCONVEX_WRT_LARGER_FACE = 1;
static const int NONCONVEX = 2;
template<class TV>
class QuickHull {
private:
std::vector<Face<TV>*> discardedFaces;
std::vector<Vertex<TV>*> maxVtxs;
std::vector<Vertex<TV>*> minVtxs;
FaceList<TV>* newFaces;
VertexList<TV>* unclaimed;
VertexList<TV>* claimed;
void addPointToFace (Vertex<TV>* vtx, Face<TV>* face);
void removePointFromFace (Vertex<TV>* vtx, Face<TV>* face);
Vertex<TV>* removeAllPointsFromFace (Face<TV>* face);
HalfEdge<TV>* findHalfEdge (Vertex<TV>* tail, Vertex<TV>* head);
std::vector< int > getFaceIndices (Face<TV>* face, int flags);
bool doAdjacentMerge (Face<TV>* face, int mergeType);
HalfEdge<TV>* addAdjoiningFace (Vertex<TV>* eyeVtx, HalfEdge<TV>* he);
void markFaceVertices (Face<TV>* face, int mark);
protected:
int findIndex = -1;
bool debug = false;
double charLength;
std::vector<Vertex<TV>*> pointBuffer;
std::vector<int> vertexPointIndices;
std::vector<Face<TV>*> faces;
std::vector<HalfEdge<TV>*> horizon;
int numVertices;
int numFaces;
int numPoints;
double explicitTolerance = AUTOMATIC_TOLERANCE;
double tolerance;
void initBuffers (int nump);
void setPoints (std::vector<double> coords);
void setPoints (std::vector<TV> pnts);
void computeMaxAndMin (void);
// Creates the initial simplex from which the hull will be built.
void createInitialSimplex ( void );
void resolveUnclaimedPoints (FaceList<TV>* newFaces);
void deleteFacePoints (Face<TV>* face, Face<TV>* absorbingFace);
double oppFaceDistance (HalfEdge<TV>* he);
void calculateHorizon (TV eyePnt, HalfEdge<TV>* edge0, Face<TV>* face, std::vector<HalfEdge<TV>*>& horizon);
void addNewFaces (FaceList<TV>* newFaces, Vertex<TV>* eyeVtx, std::vector<HalfEdge<TV>*>& horizon);
void addPointToHull(Vertex<TV>* eyeVtx);
void buildHull (void);
void reindexFacesAndVertices(void);
bool checkFaceConvexity (Face<TV>* face, double tol, std::ostream& ps);
bool checkFaces(double tol, std::ostream& ps);
Vertex<TV>* nextPointToAdd(void);
public:
bool getDebug() { return debug; }
void setDebug (bool enable) { debug = enable; }
double getDistanceTolerance ( void ) { return tolerance; }
void setExplicitDistanceTolerance(double tol) { explicitTolerance = tol; }
double getExplicitDistanceTolerance() { return explicitTolerance; }
void build (std::vector<TV> points);
QuickHull (void) { }
QuickHull (std::vector<TV> points) { build(points); }
// Triangulates any non-triangular hull faces. In some cases, due to
// precision issues, the resulting triangles may be very thin or small,
// and hence appear to be non-convex (this same limitation is present
// in <a href=http://www.qhull.org>qhull</a>).
void triangulate (void);
int getNumVertices() { return numVertices; }
std::vector<TV> getVertices(void);
// Returns an array specifing the index of each hull vertex
// with respect to the original input points.
std::vector<int> getVertexPointIndices(void);
int getNumFaces(void) { return faces.size(); }
// Returns the faces associated with this hull, where
// Each face is represented by an integer array which gives the
// indices of the vertices. These indices are numbered
// relative to the hull vertices, are zero-based,
// and are arranged counter-clockwise. More control
// over the index format can be obtained using
std::vector< std::vector< int > > getFaces () { return getFaces(0); }
// Returns the faces associated with this hull.
//
// <p>Each face is represented by an integer array which gives the
// indices of the vertices. By default, these indices are numbered with
// respect to the hull vertices (as opposed to the input points), are
// zero-based, and are arranged counter-clockwise. However, this
// can be changed by setting {@link #POINT_RELATIVE
// POINT_RELATIVE}, or // {@link #CLOCKWISE CLOCKWISE}
// in the indexFlags parameter.
//
// @param indexFlags specifies index characteristics (0 results
// in the default)
// @return array of integer arrays, giving the vertex
// indices for each face.
// @see QuickHull#getVertices()
std::vector< std::vector< int > > getFaces (int indexFlags);
// Checks the correctness of the hull. This is done by making sure that
// no faces are non-convex and that no points are outside any face.
// These tests are performed using the distance tolerance <i>tol</i>.
// Faces are considered non-convex if any edge is non-convex, and an
// edge is non-convex if the centroid of either adjoining face is more
// than <i>tol</i> above the plane of the other face. Similarly,
// a point is considered outside a face if its distance to that face's
// plane is more than 10 times <i>tol</i>.
//
// <p>If the hull has been {@link #triangulate triangulated},
// then this routine may fail if some of the resulting
// triangles are very small or thin.
bool check (std::ostream& ps);
bool check (std::ostream& ps, double tol);
Mesh to_mesh (void);
Nested<TV2> to_poly (void);
};
extern void error(std::string);
template<class TV>
void Face<TV>::computeNormalAndCentroid(void) {
normal = computeNormal ();
centroid = computeCentroid ();
planeOffset = dot(normal, centroid);
int numv = 0;
auto he = he0;
do {
numv++;
he = he->next;
} while (he != he0);
if (numv != numVerts) {
std::stringstream ss;
ss << "face " + getVertexString() << " numVerts=" << numVerts << " should be " << numv;
error(ss.str());
}
}
template<class TV>
void Face<TV>::computeNormalAndCentroid(double minArea) {
normal = computeNormal (minArea);
centroid = computeCentroid ();
planeOffset = dot(normal, centroid);
}
template<class TV>
double Face<TV>::areaSquared (HalfEdge<TV>* hedge0, HalfEdge<TV>* hedge1) {
// return the squared area of the triangle defined
// by the half edge hedge0 and the point at the
// head of hedge1.
auto p0 = hedge0->tail()->pnt;
auto p1 = hedge0->head()->pnt;
auto p2 = hedge1->head()->pnt;
auto d10 = p1 - p0;
auto d20 = p2 - p0;
auto c = cross(d10, d20);
return dot(c, c);
}
template<class TV>
TV Face<TV>::computeCentroid (void) {
TV centroid;
auto he = he0;
do {
centroid += he->head()->pnt;
he = he->next;
} while (he != he0);
return 1/(double)numVerts * centroid;
}
template<class TV>
TV Face<TV>::computeNormal (double minArea) {
auto normal = computeNormal();
if (area < minArea) {
// make the normal more robust by removing
// components parallel to the longest edge
HalfEdge<TV>* hedgeMax = NULL;
double lenSqrMax = 0;
HalfEdge<TV>* hedge = he0;
do {
double lenSqr = hedge->lengthSquared();
if (lenSqr > lenSqrMax) {
hedgeMax = hedge;
lenSqrMax = lenSqr;
}
hedge = hedge->next;
} while (hedge != he0);
TV p2 = hedgeMax->head()->pnt;
TV p1 = hedgeMax->tail()->pnt;
double lenMax = sqrt(lenSqrMax);
auto u = (p2 - p1) / lenMax;
auto dotty = dot(normal, u);
normal -= dotty * u;
normal.normalize();
}
return normal;
}
template<class TV>
TV Face<TV>::computeNormal (void) {
auto he1 = he0->next;
auto he2 = he1->next;
auto p0 = he0->head()->pnt;
auto p2 = he1->head()->pnt;
auto d2 = p2 - p0;
TV normal;
numVerts = 2;
while (he2 != he0) {
auto d1 = d2;
p2 = he2->head()->pnt;
d2 = p2 - p0;
normal += cross(d1, d2);
he1 = he2;
he2 = he2->next;
numVerts++;
}
area = normal.magnitude();
return (1/area) * normal;
}
template<class TV>
Face<TV>* Face<TV>::createTriangle (Vertex<TV>* v0, Vertex<TV>* v1, Vertex<TV>* v2) {
return createTriangle (v0, v1, v2, 0);
}
template<class TV>
Face<TV>* Face<TV>::createTriangle (Vertex<TV>* v0, Vertex<TV>* v1, Vertex<TV>* v2, double minArea) {
auto face = new Face<TV>();
auto he0 = new HalfEdge<TV> (v0, face);
auto he1 = new HalfEdge<TV> (v1, face);
auto he2 = new HalfEdge<TV> (v2, face);
he0->prev = he2;
he0->next = he1;
he1->prev = he0;
he1->next = he2;
he2->prev = he1;
he2->next = he0;
face->he0 = he0;
// compute the normal and offset
face->computeNormalAndCentroid(minArea);
return face;
}
template<class TV>
HalfEdge<TV>* Face<TV>::getEdge (int i) {
auto he = he0;
while (i > 0) {
he = he->next;
i--;
}
while (i < 0) {
he = he->prev;
i++;
}
return he;
}
template<class TV>
// finds the half-edge within this face which has tail vt and head vh
HalfEdge<TV>* Face<TV>::findEdge (Vertex<TV>* vt, Vertex<TV>* vh) {
auto he = he0;
do {
if (he->head() == vh && he->tail() == vt) {
return he;
}
he = he->next;
} while (he != he0);
return NULL;
}
template<class TV>
double Face<TV>::distanceToPlane (TV p) {
return dot(normal, p) - planeOffset;
}
template<class TV>
std::string Face<TV>::getVertexString (void) {
std::stringstream ss;
auto he = he0;
bool is_first = true;
do {
if (is_first) {
ss << he->head()->index;
is_first = false;
} else {
ss << " " << he->head()->index;
}
he = he->next;
} while (he != he0);
return ss.str();
}
template<class TV>
std::vector<int> Face<TV>::getVertexIndices (void) {
auto he = he0;
std::vector<int> indices;
do {
indices.push_back(he->head()->index);
he = he->next;
} while (he != he0);
return indices;
}
template<class TV>
Face<TV>* Face<TV>::connectHalfEdges (HalfEdge<TV>* hedgePrev, HalfEdge<TV>* hedge) {
Face<TV>* discardedFace = NULL;
if (hedgePrev->oppositeFace() == hedge->oppositeFace()) {
// then there is a redundant edge that we can get rid off
auto oppFace = hedge->oppositeFace();
HalfEdge<TV>* hedgeOpp;
if (hedgePrev == he0) {
he0 = hedge;
}
if (oppFace->numVertices() == 3) {
// then we can get rid of the opposite face altogether
hedgeOpp = hedge->getOpposite()->prev->getOpposite();
oppFace->mark = DELETED;
discardedFace = oppFace;
} else {
hedgeOpp = hedge->getOpposite()->next;
if (oppFace->he0 == hedgeOpp->prev) {
oppFace->he0 = hedgeOpp;
}
hedgeOpp->prev = hedgeOpp->prev->prev;
hedgeOpp->prev->next = hedgeOpp;
}
hedge->prev = hedgePrev->prev;
hedge->prev->next = hedge;
hedge->opposite = hedgeOpp;
hedgeOpp->opposite = hedge;
// oppFace was modified, so need to recompute
oppFace->computeNormalAndCentroid();
} else {
hedgePrev->next = hedge;
hedge->prev = hedgePrev;
}
return discardedFace;
}
template<class TV>
void Face<TV>::checkConsistency(void) {
// do a sanity check on the face
auto hedge = he0;
double maxd = 0;
int numv = 0;
if (numVerts < 3) {
std::stringstream ss;
ss << "degenerate face: " << getVertexString();
error(ss.str());
}
do {
auto hedgeOpp = hedge->getOpposite();
if (hedgeOpp == NULL) {
std::stringstream ss;
ss << "face " << getVertexString() << ": " << "unreflected half edge " << hedge->getVertexString();
error(ss.str());
} else if (hedgeOpp->getOpposite() != hedge) {
std::stringstream ss;
ss << "face " << getVertexString() << ": " <<
"opposite half edge " << hedgeOpp->getVertexString() <<
" has opposite " << hedgeOpp->getOpposite()->getVertexString();
error(ss.str());
}
if (hedgeOpp->head() != hedge->tail() || hedge->head() != hedgeOpp->tail()) {
std::stringstream ss;
ss << "face " << getVertexString() << ": " <<
"half edge " << hedge->getVertexString() <<
" reflected by " << hedgeOpp->getVertexString();
error(ss.str());
}
auto oppFace = hedgeOpp->face;
if (oppFace == NULL) {
std::stringstream ss;
ss << "face " << getVertexString() << ": " << "no face on half edge " << hedgeOpp->getVertexString();
error (ss.str());
} else if (oppFace->mark == DELETED) {
std::stringstream ss;
ss << "face " << getVertexString() << ": " << "opposite face " << oppFace->getVertexString() << " not on hull";
error (ss.str());
}
double d = fabs(distanceToPlane(hedge->head()->pnt));
if (d > maxd) {
maxd = d;
}
numv++;
hedge = hedge->next;
} while (hedge != he0);
if (numv != numVerts) {
std::stringstream ss;
ss << "face " << getVertexString() << " numVerts=" << numVerts << " should be " << numv;
error(ss.str());
}
}
template<class TV>
std::vector<Face<TV>*> Face<TV>::mergeAdjacentFace (HalfEdge<TV>* hedgeAdj) {
auto oppFace = hedgeAdj->oppositeFace();
std::vector<Face*> discarded;
discarded.push_back(oppFace);
oppFace->mark = DELETED;
auto hedgeOpp = hedgeAdj->getOpposite();
auto hedgeAdjPrev = hedgeAdj->prev;
auto hedgeAdjNext = hedgeAdj->next;
auto hedgeOppPrev = hedgeOpp->prev;
auto hedgeOppNext = hedgeOpp->next;
while (hedgeAdjPrev->oppositeFace() == oppFace) {
hedgeAdjPrev = hedgeAdjPrev->prev;
hedgeOppNext = hedgeOppNext->next;
}
while (hedgeAdjNext->oppositeFace() == oppFace) {
hedgeOppPrev = hedgeOppPrev->prev;
hedgeAdjNext = hedgeAdjNext->next;
}
HalfEdge<TV>* hedge;
for (hedge=hedgeOppNext; hedge!=hedgeOppPrev->next; hedge=hedge->next) {
hedge->face = this;
}
if (hedgeAdj == he0) {
he0 = hedgeAdjNext;
}
// handle the half edges at the head
Face<TV>* discardedFace;
discardedFace = connectHalfEdges (hedgeOppPrev, hedgeAdjNext);
if (discardedFace != NULL) {
discarded.push_back(discardedFace);
}
// handle the half edges at the tail
discardedFace = connectHalfEdges (hedgeAdjPrev, hedgeOppNext);
if (discardedFace != NULL) {
discarded.push_back(discardedFace);
}
computeNormalAndCentroid ();
checkConsistency();
return discarded;
}
template<class TV>
void Face<TV>::triangulate (FaceList<TV>* newFaces, double minArea) {
HalfEdge<TV>* hedge;
if (numVertices() < 4) {
return;
}
auto v0 = he0->head();
hedge = he0->next;
auto oppPrev = hedge->opposite;
Face* face0 = NULL;
for (hedge=hedge->next; hedge!=he0->prev; hedge=hedge->next) {
auto face = createTriangle (v0, hedge->prev->head(), hedge->head(), minArea);
face->he0->next->setOpposite (oppPrev);
face->he0->prev->setOpposite (hedge->opposite);
oppPrev = face->he0;
newFaces->add (face);
if (face0 == NULL) {
face0 = face;
}
}
hedge = new HalfEdge<TV> (he0->prev->prev->head(), this);
hedge->setOpposite (oppPrev);
hedge->prev = he0;
hedge->prev->next = hedge;
hedge->next = he0->prev;
hedge->next->prev = hedge;
computeNormalAndCentroid (minArea);
checkConsistency();
for (auto face=face0; face!=NULL; face=face->next) {
face->checkConsistency();
}
}
template<class TV>
void FaceList<TV>::add (Face<TV>* vtx) {
if (head == NULL) {
head = vtx;
} else {
tail->next = vtx;
}
vtx->next = NULL;
tail = vtx;
}
template<class TV>
std::string HalfEdge<TV>::getVertexString(void) {
std::stringstream ss;
if (tail() != NULL) {
ss << tail()->index << "-" << head()->index;
} else {
ss << "?-" << head()->index;
}
return ss.str();
}
// Returns the length of this half-edge.
template<class TV>
double HalfEdge<TV>::length(void) {
if (tail() != NULL) {
return (head()->pnt - tail()->pnt).magnitude();
} else {
return -1;
}
}
template<class TV>
double HalfEdge<TV>::lengthSquared(void) {
if (tail() != NULL) {
return (head()->pnt - tail()->pnt).sqr_magnitude();
} else {
return -1;
}
}
template<class TV>
void VertexList<TV>::add (Vertex<TV>* vtx) {
if (head == NULL) {
head = vtx;
} else {
tail->next = vtx;
}
vtx->prev = tail;
vtx->next = NULL;
tail = vtx;
}
// Adds a chain of vertices to the end of this list.
template<class TV>
void VertexList<TV>::addAll (Vertex<TV>* vtx) {
if (head == NULL) {
head = vtx;
} else {
tail->next = vtx;
}
vtx->prev = tail;
while (vtx->next != NULL) {
vtx = vtx->next;
}
tail = vtx;
}
// Deletes a vertex from this list.
template<class TV>
void VertexList<TV>::del (Vertex<TV>* vtx) {
if (vtx->prev == NULL) {
head = vtx->next;
} else {
vtx->prev->next = vtx->next;
}
if (vtx->next == NULL) {
tail = vtx->prev;
} else {
vtx->next->prev = vtx->prev;
}
}
// Deletes a chain of vertices from this list.
template<class TV>
void VertexList<TV>::del (Vertex<TV>* vtx1, Vertex<TV>* vtx2) {
if (vtx1->prev == NULL) {
head = vtx2->next;
} else {
vtx1->prev->next = vtx2->next;
}
if (vtx2->next == NULL) {
tail = vtx1->prev;
} else {
vtx2->next->prev = vtx1->prev;
}
}
// Inserts a vertex into this list before another specificed vertex.
template<class TV>
void VertexList<TV>::insertBefore (Vertex<TV>* vtx, Vertex<TV>* next) {
vtx->prev = next->prev;
if (next->prev == NULL) {
head = vtx;
} else {
next->prev->next = vtx;
}
vtx->next = next;
next->prev = vtx;
}
template<class TV>
void QuickHull<TV>::addPointToFace (Vertex<TV>* vtx, Face<TV>* face) {
vtx->face = face;
if (face->outside == NULL) {
claimed->add (vtx);
} else {
claimed->insertBefore (vtx, face->outside);
}
face->outside = vtx;
}
template<class TV>
void QuickHull<TV>::removePointFromFace (Vertex<TV>* vtx, Face<TV>* face) {
if (vtx == face->outside) {
if (vtx->next != NULL && vtx->next->face == face) {
face->outside = vtx->next;
} else {
face->outside = NULL;
}
}
claimed->del (vtx);
}
template<class TV>
Vertex<TV>* QuickHull<TV>::removeAllPointsFromFace (Face<TV>* face) {
if (face->outside != NULL) {
auto end = face->outside;
while (end->next != NULL && end->next->face == face) {
end = end->next;
}
claimed->del (face->outside, end);
end->next = NULL;
return face->outside;
} else {
return NULL;
}
}
template<class TV>
HalfEdge<TV>* QuickHull<TV>::findHalfEdge (Vertex<TV>* tail, Vertex<TV>* head) {
for (auto face : faces) {
auto he = face->findEdge (tail, head);
if (he != NULL) {
return he;
}
}
return NULL;
}
template<class TV>
std::vector<int> QuickHull<TV>::getFaceIndices (Face<TV>* face, int flags) {
std::vector<int> indices;
bool ccw = ((flags & CLOCKWISE) == 0);
bool pointRelative = ((flags & POINT_RELATIVE) != 0);
auto hedge = face->he0;
do {
int idx = hedge->head()->index;
if (pointRelative) {
idx = vertexPointIndices[idx];
}
indices.push_back(idx);
hedge = (ccw ? hedge->next : hedge->prev);
} while (hedge != face->he0);
return indices;
}
template<class TV>
bool QuickHull<TV>::doAdjacentMerge (Face<TV>* face, int mergeType) {
auto hedge = face->he0;
bool convex = true;
do {
auto oppFace = hedge->oppositeFace();
bool merge = false;
double dist1;
if (mergeType == NONCONVEX) {
// then merge faces if they are definitively non-convex
if (oppFaceDistance (hedge) > -tolerance ||
oppFaceDistance (hedge->opposite) > -tolerance) {
merge = true;
}
} else { // mergeType == NONCONVEX_WRT_LARGER_FACE
// merge faces if they are parallel or non-convex
// wrt to the larger face; otherwise, just mark
// the face non-convex for the second pass.
if (face->area > oppFace->area) {
if ((dist1 = oppFaceDistance (hedge)) > -tolerance) {
merge = true;
} else if (oppFaceDistance (hedge->opposite) > -tolerance) {
convex = false;
}
} else {
if (oppFaceDistance (hedge->opposite) > -tolerance) {
merge = true;
} else if (oppFaceDistance (hedge) > -tolerance) {
convex = false;
}
}
}
if (merge) {
if (debug) {
std::cout << " merging " << face->getVertexString() << " and " <<
oppFace->getVertexString() << "\n";
}
auto discardedFaces = face->mergeAdjacentFace (hedge);
for (auto discard : discardedFaces) {
deleteFacePoints (discard, face);
}
if (debug) {
std::cout << " result: " << face->getVertexString() << "\n";
}
return true;
}
hedge = hedge->next;
}
while (hedge != face->he0);
if (!convex) {
face->mark = NON_CONVEX;
}
return false;
}
template<class TV>
HalfEdge<TV>* QuickHull<TV>::addAdjoiningFace (Vertex<TV>* eyeVtx, HalfEdge<TV>* he) {
auto face = Face<TV>::createTriangle (eyeVtx, he->tail(), he->head());
faces.push_back (face);
face->getEdge(-1)->setOpposite(he->getOpposite());
return face->getEdge(0);
}
template<class TV>
void QuickHull<TV>::markFaceVertices (Face<TV>* face, int mark) {
auto he0 = face->getFirstEdge();
auto he = he0;
do {
he->head()->index = mark;
he = he->next;
}
while (he != he0);
}
template<class TV>
void QuickHull<TV>::initBuffers (int nump) {
for (int i = 0; i < 4; i++) {
maxVtxs.push_back(NULL);
minVtxs.push_back(NULL);
discardedFaces.push_back(NULL);
}
newFaces = new FaceList<TV>();
claimed = new VertexList<TV>();
unclaimed = new VertexList<TV>();
if ((int)pointBuffer.size() < nump) {
std::vector<Vertex<TV>*> newBuffer(nump);
for (size_t i=0; i<pointBuffer.size(); i++) {
newBuffer[i] = pointBuffer[i];
vertexPointIndices.push_back(-1);
}
for (int i=pointBuffer.size(); i<nump; i++) {
newBuffer[i] = new Vertex<TV>();
}
pointBuffer = newBuffer;
}
faces.clear();
claimed->clear();
numFaces = 0;
numPoints = nump;
}
template<class TV>
void QuickHull<TV>::setPoints (std::vector<double> coords) {
int nump = coords.size() / 3;
for (int i=0; i<nump; i++) {
auto vtx = pointBuffer[i];
vtx->pnt = v3d(coords[i*3+0], coords[i*3+1], coords[i*3+2]);
vtx->index = i;
}
}
template<class TV>