-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickhull3d.jscad
1321 lines (1215 loc) · 37.9 KB
/
quickhull3d.jscad
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
/*
* Copied from https://github.com/mauriciopoppe/quickhull3d.
* Copyright (c) 2015 Mauricio Poppe
* Licensed under the MIT license.
*/
var debug = function() {
}
debug.enabled = false;
class Vertex {
constructor (point, index) {
this.point = point
// index in the input array
this.index = index
// vertex is a double linked list node
this.next = null
this.prev = null
// the face that is able to see this point
this.face = null
}
}
class VertexList {
constructor () {
this.head = null
this.tail = null
}
clear () {
this.head = this.tail = null
}
/**
* Inserts a `node` before `target`, it's assumed that
* `target` belongs to this doubly linked list
*
* @param {*} target
* @param {*} node
*/
insertBefore (target, node) {
node.prev = target.prev
node.next = target
if (!node.prev) {
this.head = node
} else {
node.prev.next = node
}
target.prev = node
}
/**
* Inserts a `node` after `target`, it's assumed that
* `target` belongs to this doubly linked list
*
* @param {Vertex} target
* @param {Vertex} node
*/
insertAfter (target, node) {
node.prev = target
node.next = target.next
if (!node.next) {
this.tail = node
} else {
node.next.prev = node
}
target.next = node
}
/**
* Appends a `node` to the end of this doubly linked list
* Note: `node.next` will be unlinked from `node`
* Note: if `node` is part of another linked list call `addAll` instead
*
* @param {*} node
*/
add (node) {
if (!this.head) {
this.head = node
} else {
this.tail.next = node
}
node.prev = this.tail
// since node is the new end it doesn't have a next node
node.next = null
this.tail = node
}
/**
* Appends a chain of nodes where `node` is the head,
* the difference with `add` is that it correctly sets the position
* of the node list `tail` property
*
* @param {*} node
*/
addAll (node) {
if (!this.head) {
this.head = node
} else {
this.tail.next = node
}
node.prev = this.tail
// find the end of the list
while (node.next) {
node = node.next
}
this.tail = node
}
/**
* Deletes a `node` from this linked list, it's assumed that `node` is a
* member of this linked list
*
* @param {*} node
*/
remove (node) {
if (!node.prev) {
this.head = node.next
} else {
node.prev.next = node.next
}
if (!node.next) {
this.tail = node.prev
} else {
node.next.prev = node.prev
}
}
/**
* Removes a chain of nodes whose head is `a` and whose tail is `b`,
* it's assumed that `a` and `b` belong to this list and also that `a`
* comes before `b` in the linked list
*
* @param {*} a
* @param {*} b
*/
removeChain (a, b) {
if (!a.prev) {
this.head = b.next
} else {
a.prev.next = b.next
}
if (!b.next) {
this.tail = a.prev
} else {
b.next.prev = a.prev
}
}
first () {
return this.head
}
isEmpty () {
return !this.head
}
}
class HalfEdge {
constructor (vertex, face) {
this.vertex = vertex
this.face = face
this.next = null
this.prev = null
this.opposite = null
}
head () {
return this.vertex
}
tail () {
return this.prev
? this.prev.vertex
: null
}
length () {
if (this.tail()) {
return distance(
this.tail().point,
this.head().point
)
}
return -1
}
lengthSquared () {
if (this.tail()) {
return squaredDistance(
this.tail().point,
this.head().point
)
}
return -1
}
setOpposite (edge) {
var me = this
if (debug.enabled) {
debug(`opposite ${me.tail().index} <--> ${me.head().index} between ${me.face.collectIndices()}, ${edge.face.collectIndices()}`)
}
this.opposite = edge
edge.opposite = this
}
}
const VISIBLE = 0
const NON_CONVEX = 1
const DELETED = 2
class Face {
constructor () {
this.normal = []
this.centroid = []
// signed distance from face to the origin
this.offset = 0
// pointer to the a vertex in a double linked list this face can see
this.outside = null
this.mark = VISIBLE
this.edge = null
this.nVertices = 0
}
getEdge (i) {
if (typeof i !== 'number') {
throw Error('requires a number')
}
let it = this.edge
while (i > 0) {
it = it.next
i -= 1
}
while (i < 0) {
it = it.prev
i += 1
}
return it
}
computeNormal () {
let e0 = this.edge
let e1 = e0.next
let e2 = e1.next
let v2 = gl_vec3.subtract([], e1.head().point, e0.head().point)
let t = []
let v1 = []
this.nVertices = 2
this.normal = [0, 0, 0]
while (e2 !== e0) {
gl_vec3.copy(v1, v2)
gl_vec3.subtract(v2, e2.head().point, e0.head().point)
gl_vec3.add(this.normal, this.normal, gl_vec3.cross(t, v1, v2))
e2 = e2.next
this.nVertices += 1
}
this.area = gl_vec3.vec3_length(this.normal)
// normalize the vector, since we've already calculated the area
// it's cheaper to scale the vector using this quantity instead of
// doing the same operation again
this.normal = gl_vec3.scale(this.normal, this.normal, 1 / this.area)
}
computeNormalMinArea (minArea) {
this.computeNormal()
if (this.area < minArea) {
// compute the normal without the longest edge
let maxEdge
let maxSquaredLength = 0
let edge = this.edge
// find the longest edge (in length) in the chain of edges
do {
let lengthSquared = edge.lengthSquared()
if (lengthSquared > maxSquaredLength) {
maxEdge = edge
maxSquaredLength = lengthSquared
}
edge = edge.next
} while (edge !== this.edge)
let p1 = maxEdge.tail().point
let p2 = maxEdge.head().point
let maxVector = gl_vec3.subtract([], p2, p1)
let maxLength = Math.sqrt(maxSquaredLength)
// maxVector is normalized after this operation
gl_vec3.scale(maxVector, maxVector, 1 / maxLength)
// compute the projection of maxVector over this face normal
let maxProjection = gl_vec3.dot(this.normal, maxVector)
// subtract the quantity maxEdge adds on the normal
scaleAndAdd(this.normal, this.normal, maxVector, -maxProjection)
// renormalize `this.normal`
gl_vec3.normalize(this.normal, this.normal)
}
}
computeCentroid () {
this.centroid = [0, 0, 0]
let edge = this.edge
do {
gl_vec3.add(this.centroid, this.centroid, edge.head().point)
edge = edge.next
} while (edge !== this.edge)
gl_vec3.scale(this.centroid, this.centroid, 1 / this.nVertices)
}
computeNormalAndCentroid (minArea) {
if (typeof minArea !== 'undefined') {
this.computeNormalMinArea(minArea)
} else {
this.computeNormal()
}
this.computeCentroid()
this.offset = gl_vec3.dot(this.normal, this.centroid)
}
distanceToPlane (point) {
return gl_vec3.dot(this.normal, point) - this.offset
}
/**
* @private
*
* Connects two edges assuming that prev.head().point === next.tail().point
*
* @param {HalfEdge} prev
* @param {HalfEdge} next
*/
connectHalfEdges (prev, next) {
let discardedFace
if (prev.opposite.face === next.opposite.face) {
// `prev` is remove a redundant edge
let oppositeFace = next.opposite.face
let oppositeEdge
if (prev === this.edge) {
this.edge = next
}
if (oppositeFace.nVertices === 3) {
// case:
// remove the face on the right
//
// /|\
// / | \ the face on the right
// / | \ --> opposite edge
// / a | \
// *----*----*
// / b | \
// ▾
// redundant edge
//
// Note: the opposite edge is actually in the face to the right
// of the face to be destroyed
oppositeEdge = next.opposite.prev.opposite
oppositeFace.mark = DELETED
discardedFace = oppositeFace
} else {
// case:
// t
// *----
// /| <- right face's redundant edge
// / | opposite edge
// / | ▴ /
// / a | | /
// *----*----*
// / b | \
// ▾
// redundant edge
oppositeEdge = next.opposite.next
// make sure that the link `oppositeFace.edge` points correctly even
// after the right face redundant edge is removed
if (oppositeFace.edge === oppositeEdge.prev) {
oppositeFace.edge = oppositeEdge
}
// /| /
// / | t/opposite edge
// / | / ▴ /
// / a |/ | /
// *----*----*
// / b \
oppositeEdge.prev = oppositeEdge.prev.prev
oppositeEdge.prev.next = oppositeEdge
}
// /|
// / |
// / |
// / a |
// *----*----*
// / b ▴ \
// |
// redundant edge
next.prev = prev.prev
next.prev.next = next
// / \ \
// / \->\
// / \<-\ opposite edge
// / a \ \
// *----*----*
// / b ^ \
next.setOpposite(oppositeEdge)
oppositeFace.computeNormalAndCentroid()
} else {
// trivial case
// *
// /|\
// / | \
// / |--> next
// / a | \
// *----*----*
// \ b | /
// \ |--> prev
// \ | /
// \|/
// *
prev.next = next
next.prev = prev
}
return discardedFace
}
mergeAdjacentFaces (adjacentEdge, discardedFaces) {
const oppositeEdge = adjacentEdge.opposite
const oppositeFace = oppositeEdge.face
discardedFaces.push(oppositeFace)
oppositeFace.mark = DELETED
// find the chain of edges whose opposite face is `oppositeFace`
//
// ===>
// \ face /
// * ---- * ---- * ---- *
// / opposite face \
// <===
//
let adjacentEdgePrev = adjacentEdge.prev
let adjacentEdgeNext = adjacentEdge.next
let oppositeEdgePrev = oppositeEdge.prev
let oppositeEdgeNext = oppositeEdge.next
// left edge
while (adjacentEdgePrev.opposite.face === oppositeFace) {
adjacentEdgePrev = adjacentEdgePrev.prev
oppositeEdgeNext = oppositeEdgeNext.next
}
// right edge
while (adjacentEdgeNext.opposite.face === oppositeFace) {
adjacentEdgeNext = adjacentEdgeNext.next
oppositeEdgePrev = oppositeEdgePrev.prev
}
// adjacentEdgePrev \ face / adjacentEdgeNext
// * ---- * ---- * ---- *
// oppositeEdgeNext / opposite face \ oppositeEdgePrev
// fix the face reference of all the opposite edges that are not part of
// the edges whose opposite face is not `face` i.e. all the edges that
// `face` and `oppositeFace` do not have in common
let edge
for (edge = oppositeEdgeNext; edge !== oppositeEdgePrev.next; edge = edge.next) {
edge.face = this
}
// make sure that `face.edge` is not one of the edges to be destroyed
// Note: it's important for it to be a `next` edge since `prev` edges
// might be destroyed on `connectHalfEdges`
this.edge = adjacentEdgeNext
// connect the extremes
// Note: it might be possible that after connecting the edges a triangular
// face might be redundant
let discardedFace
discardedFace = this.connectHalfEdges(oppositeEdgePrev, adjacentEdgeNext)
if (discardedFace) {
discardedFaces.push(discardedFace)
}
discardedFace = this.connectHalfEdges(adjacentEdgePrev, oppositeEdgeNext)
if (discardedFace) {
discardedFaces.push(discardedFace)
}
this.computeNormalAndCentroid()
// TODO: additional consistency checks
return discardedFaces
}
collectIndices () {
let indices = []
let edge = this.edge
do {
indices.push(edge.head().index)
edge = edge.next
} while (edge !== this.edge)
return indices
}
static createTriangle (v0, v1, v2, minArea = 0) {
const face = new Face()
const e0 = new HalfEdge(v0, face)
const e1 = new HalfEdge(v1, face)
const e2 = new HalfEdge(v2, face)
// join edges
e0.next = e2.prev = e1
e1.next = e0.prev = e2
e2.next = e1.prev = e0
// main half edge reference
face.edge = e0
face.computeNormalAndCentroid(minArea)
if (debug.enabled) {
debug('face created %j', face.collectIndices())
}
return face
}
}
// merge types
// non convex with respect to the large face
const MERGE_NON_CONVEX_WRT_LARGER_FACE = 1;
const MERGE_NON_CONVEX = 2;
class QuickHull {
constructor (points) {
if (!Array.isArray(points)) {
throw TypeError('input is not a valid array')
}
if (points.length < 4) {
throw Error('cannot build a simplex out of <4 points')
}
this.tolerance = -1
// buffers
this.nFaces = 0
this.nPoints = points.length
this.faces = []
this.newFaces = []
// helpers
//
// let `a`, `b` be `Face` instances
// let `v` be points wrapped as instance of `Vertex`
//
// [v, v, ..., v, v, v, ...]
// ^ ^
// | |
// a.outside b.outside
//
this.claimed = new VertexList()
this.unclaimed = new VertexList()
// vertices of the hull(internal representation of points)
this.vertices = []
for (let i = 0; i < points.length; i += 1) {
this.vertices.push(new Vertex(points[i], i))
}
this.discardedFaces = []
this.vertexPointIndices = []
}
addVertexToFace (vertex, face) {
vertex.face = face
if (!face.outside) {
this.claimed.add(vertex)
} else {
this.claimed.insertBefore(face.outside, vertex)
}
face.outside = vertex
}
/**
* Removes `vertex` for the `claimed` list of vertices, it also makes sure
* that the link from `face` to the first vertex it sees in `claimed` is
* linked correctly after the removal
*
* @param {Vertex} vertex
* @param {Face} face
*/
removeVertexFromFace (vertex, face) {
if (vertex === face.outside) {
// fix face.outside link
if (vertex.next && vertex.next.face === face) {
// face has at least 2 outside vertices, move the `outside` reference
face.outside = vertex.next
} else {
// vertex was the only outside vertex that face had
face.outside = null
}
}
this.claimed.remove(vertex)
}
/**
* Removes all the visible vertices that `face` is able to see which are
* stored in the `claimed` vertext list
*
* @param {Face} face
* @return {Vertex|undefined} If face had visible vertices returns
* `face.outside`, otherwise undefined
*/
removeAllVerticesFromFace (face) {
if (face.outside) {
// pointer to the last vertex of this face
// [..., outside, ..., end, outside, ...]
// | | |
// a a b
let end = face.outside
while (end.next && end.next.face === face) {
end = end.next
}
this.claimed.removeChain(face.outside, end)
// b
// [ outside, ...]
// | removes this link
// [ outside, ..., end ] -┘
// | |
// a a
end.next = null
return face.outside
}
}
/**
* Removes all the visible vertices that `face` is able to see, additionally
* checking the following:
*
* If `absorbingFace` doesn't exist then all the removed vertices will be
* added to the `unclaimed` vertex list
*
* If `absorbingFace` exists then this method will assign all the vertices of
* `face` that can see `absorbingFace`, if a vertex cannot see `absorbingFace`
* it's added to the `unclaimed` vertex list
*
* @param {Face} face
* @param {Face} [absorbingFace]
*/
deleteFaceVertices (face, absorbingFace) {
const faceVertices = this.removeAllVerticesFromFace(face)
if (faceVertices) {
if (!absorbingFace) {
// mark the vertices to be reassigned to some other face
this.unclaimed.addAll(faceVertices)
} else {
// if there's an absorbing face try to assign as many vertices
// as possible to it
// the reference `vertex.next` might be destroyed on
// `this.addVertexToFace` (see VertexList#add), nextVertex is a
// reference to it
let nextVertex
for (let vertex = faceVertices; vertex; vertex = nextVertex) {
nextVertex = vertex.next
const distance = absorbingFace.distanceToPlane(vertex.point)
// check if `vertex` is able to see `absorbingFace`
if (distance > this.tolerance) {
this.addVertexToFace(vertex, absorbingFace)
} else {
this.unclaimed.add(vertex)
}
}
}
}
}
/**
* Reassigns as many vertices as possible from the unclaimed list to the new
* faces
*
* @param {Faces[]} newFaces
*/
resolveUnclaimedPoints (newFaces) {
// cache next vertex so that if `vertex.next` is destroyed it's still
// recoverable
let vertexNext = this.unclaimed.first()
for (let vertex = vertexNext; vertex; vertex = vertexNext) {
vertexNext = vertex.next
let maxDistance = this.tolerance
let maxFace
for (let i = 0; i < newFaces.length; i += 1) {
let face = newFaces[i]
if (face.mark === VISIBLE) {
const dist = face.distanceToPlane(vertex.point)
if (dist > maxDistance) {
maxDistance = dist
maxFace = face
}
if (maxDistance > 1000 * this.tolerance) {
break
}
}
}
if (maxFace) {
this.addVertexToFace(vertex, maxFace)
}
}
}
/**
* Computes the extremes of a tetrahedron which will be the initial hull
*
* @return {number[]} The min/max vertices in the x,y,z directions
*/
computeExtremes () {
const me = this
const min = []
const max = []
// min vertex on the x,y,z directions
const minVertices = []
// max vertex on the x,y,z directions
const maxVertices = []
let i, j
// initially assume that the first vertex is the min/max
for (i = 0; i < 3; i += 1) {
minVertices[i] = maxVertices[i] = this.vertices[0]
}
// copy the coordinates of the first vertex to min/max
for (i = 0; i < 3; i += 1) {
min[i] = max[i] = this.vertices[0].point[i]
}
// compute the min/max vertex on all 6 directions
for (i = 1; i < this.vertices.length; i += 1) {
const vertex = this.vertices[i]
const point = vertex.point
// update the min coordinates
for (j = 0; j < 3; j += 1) {
if (point[j] < min[j]) {
min[j] = point[j]
minVertices[j] = vertex
}
}
// update the max coordinates
for (j = 0; j < 3; j += 1) {
if (point[j] > max[j]) {
max[j] = point[j]
maxVertices[j] = vertex
}
}
}
// compute epsilon
this.tolerance = 3 * Number.EPSILON * (
Math.max(Math.abs(min[0]), Math.abs(max[0])) +
Math.max(Math.abs(min[1]), Math.abs(max[1])) +
Math.max(Math.abs(min[2]), Math.abs(max[2]))
)
if (debug.enabled) {
debug('tolerance %d', me.tolerance)
}
return [minVertices, maxVertices]
}
/**
* Compues the initial tetrahedron assigning to its faces all the points that
* are candidates to form part of the hull
*/
createInitialSimplex () {
const vertices = this.vertices
const [min, max] = this.computeExtremes()
let v0, v1, v2, v3
let i, j
// Find the two vertices with the greatest 1d separation
// (max.x - min.x)
// (max.y - min.y)
// (max.z - min.z)
let maxDistance = 0
let indexMax = 0
for (i = 0; i < 3; i += 1) {
const distance = max[i].point[i] - min[i].point[i]
if (distance > maxDistance) {
maxDistance = distance
indexMax = i
}
}
v0 = min[indexMax]
v1 = max[indexMax]
// the next vertex is the one farthest to the line formed by `v0` and `v1`
maxDistance = 0
for (i = 0; i < this.vertices.length; i += 1) {
const vertex = this.vertices[i]
if (vertex !== v0 && vertex !== v1) {
const distance = pointLineDistance(
vertex.point, v0.point, v1.point
)
if (distance > maxDistance) {
maxDistance = distance
v2 = vertex
}
}
}
// the next vertes is the one farthest to the plane `v0`, `v1`, `v2`
// normalize((v2 - v1) x (v0 - v1))
const normal = planeNormal([], v0.point, v1.point, v2.point)
// distance from the origin to the plane
const distPO = gl_vec3.dot(v0.point, normal)
maxDistance = -1
for (i = 0; i < this.vertices.length; i += 1) {
const vertex = this.vertices[i]
if (vertex !== v0 && vertex !== v1 && vertex !== v2) {
const distance = Math.abs(gl_vec3.dot(normal, vertex.point) - distPO)
if (distance > maxDistance) {
maxDistance = distance
v3 = vertex
}
}
}
// initial simplex
// Taken from http://everything2.com/title/How+to+paint+a+tetrahedron
//
// v2
// ,|,
// ,7``\'VA,
// ,7` |, `'VA,
// ,7` `\ `'VA,
// ,7` |, `'VA,
// ,7` `\ `'VA,
// ,7` |, `'VA,
// ,7` `\ ,..ooOOTK` v3
// ,7` |,.ooOOT''` AV
// ,7` ,..ooOOT`\` /7
// ,7` ,..ooOOT''` |, AV
// ,T,..ooOOT''` `\ /7
// v0 `'TTs., |, AV
// `'TTs., `\ /7
// `'TTs., |, AV
// `'TTs., `\ /7
// `'TTs., |, AV
// `'TTs.,\/7
// `'T`
// v1
//
const faces = []
if (gl_vec3.dot(v3.point, normal) - distPO < 0) {
// the face is not able to see the point so `planeNormal`
// is pointing outside the tetrahedron
faces.push(
Face.createTriangle(v0, v1, v2),
Face.createTriangle(v3, v1, v0),
Face.createTriangle(v3, v2, v1),
Face.createTriangle(v3, v0, v2)
)
// set the opposite edge
for (i = 0; i < 3; i += 1) {
let j = (i + 1) % 3
// join face[i] i > 0, with the first face
faces[i + 1].getEdge(2).setOpposite(faces[0].getEdge(j))
// join face[i] with face[i + 1], 1 <= i <= 3
faces[i + 1].getEdge(1).setOpposite(faces[j + 1].getEdge(0))
}
} else {
// the face is able to see the point so `planeNormal`
// is pointing inside the tetrahedron
faces.push(
Face.createTriangle(v0, v2, v1),
Face.createTriangle(v3, v0, v1),
Face.createTriangle(v3, v1, v2),
Face.createTriangle(v3, v2, v0)
)
// set the opposite edge
for (i = 0; i < 3; i += 1) {
let j = (i + 1) % 3
// join face[i] i > 0, with the first face
faces[i + 1].getEdge(2).setOpposite(faces[0].getEdge((3 - i) % 3))
// join face[i] with face[i + 1]
faces[i + 1].getEdge(0).setOpposite(faces[j + 1].getEdge(1))
}
}
// the initial hull is the tetrahedron
for (i = 0; i < 4; i += 1) {
this.faces.push(faces[i])
}
// initial assignment of vertices to the faces of the tetrahedron
for (i = 0; i < vertices.length; i += 1) {
const vertex = vertices[i]
if (vertex !== v0 && vertex !== v1 && vertex !== v2 && vertex !== v3) {
maxDistance = this.tolerance
let maxFace
for (j = 0; j < 4; j += 1) {
const distance = faces[j].distanceToPlane(vertex.point)
if (distance > maxDistance) {
maxDistance = distance
maxFace = faces[j]
}
}
if (maxFace) {
this.addVertexToFace(vertex, maxFace)
}
}
}
}
reindexFaceAndVertices () {
// remove inactive faces
let activeFaces = []
for (let i = 0; i < this.faces.length; i += 1) {
let face = this.faces[i]
if (face.mark === VISIBLE) {
activeFaces.push(face)
}
}
this.faces = activeFaces
}
collectFaces (skipTriangulation) {
let faceIndices = []
for (let i = 0; i < this.faces.length; i += 1) {
if (this.faces[i].mark !== VISIBLE) {
throw Error('attempt to include a destroyed face in the hull')
}
let indices = this.faces[i].collectIndices()
if (skipTriangulation) {
faceIndices.push(indices)
} else {
for (let j = 0; j < indices.length - 2; j += 1) {
faceIndices.push(
[indices[0], indices[j + 1], indices[j + 2]]
)
}
}
}
return faceIndices
}
/**
* Finds the next vertex to make faces with the current hull
*
* - let `face` be the first face existing in the `claimed` vertex list
* - if `face` doesn't exist then return since there're no vertices left
* - otherwise for each `vertex` that face sees find the one furthest away
* from `face`
*
* @return {Vertex|undefined} Returns undefined when there're no more
* visible vertices
*/
nextVertexToAdd () {
if (!this.claimed.isEmpty()) {
let eyeVertex, vertex
let maxDistance = 0
const eyeFace = this.claimed.first().face
for (vertex = eyeFace.outside; vertex && vertex.face === eyeFace; vertex = vertex.next) {
let distance = eyeFace.distanceToPlane(vertex.point)
if (distance > maxDistance) {
maxDistance = distance
eyeVertex = vertex
}
}
return eyeVertex
}
}
/**
* Computes a chain of half edges in ccw order called the `horizon`, for an
* edge to be part of the horizon it must join a face that can see
* `eyePoint` and a face that cannot see `eyePoint`
*
* @param {number[]} eyePoint - The coordinates of a point
* @param {HalfEdge} crossEdge - The edge used to jump to the current `face`
* @param {Face} face - The current face being tested
* @param {HalfEdge[]} horizon - The edges that form part of the horizon in
* ccw order
*/
computeHorizon (eyePoint, crossEdge, face, horizon) {
// moves face's vertices to the `unclaimed` vertex list
this.deleteFaceVertices(face)
face.mark = DELETED
let edge
if (!crossEdge) {
edge = crossEdge = face.getEdge(0)
} else {
// start from the next edge since `crossEdge` was already analyzed
// (actually `crossEdge.opposite` was the face who called this method
// recursively)
edge = crossEdge.next
}
// All the faces that are able to see `eyeVertex` are defined as follows