1
1
import {
2
- BufferGeometry ,
3
2
Line3 ,
4
3
Plane ,
5
4
Triangle ,
@@ -42,6 +41,41 @@ var ConvexHull = ( function () {
42
41
43
42
Object . assign ( ConvexHull . prototype , {
44
43
44
+ collectFacesAndVertices : function ( ) {
45
+ // Get face vertex indices.
46
+ // These are the indices of all input vertices, including vertices inside the convex hull.
47
+ const faceIndices = this . faces . map ( ( f ) => f . collectIndices ( ) ) ;
48
+
49
+ // Get distinct vertex indices referenced by convex hull faces.
50
+ const referencedFaceIndices = Array . from (
51
+ new Set ( faceIndices . flatMap ( ( f ) => f ) ) . values ( )
52
+ ) . sort ( ( a , b ) => a - b ) ;
53
+
54
+ // Create a mapping between original vertex indices to new vertex indices.
55
+ const originalToNewIndex = new Map ( ) ;
56
+ for ( let i = 0 ; i < referencedFaceIndices . length ; i ++ ) {
57
+ originalToNewIndex . set ( referencedFaceIndices [ i ] , i ) ;
58
+ }
59
+
60
+ // Get faces with remapped vertex indices.
61
+ const faces = [ ] ;
62
+ for ( let i = 0 ; i < faceIndices . length ; i ++ ) {
63
+ const indices = faceIndices [ i ] ;
64
+ const a = originalToNewIndex . get ( indices [ 0 ] ) ;
65
+ const b = originalToNewIndex . get ( indices [ 1 ] ) ;
66
+ const c = originalToNewIndex . get ( indices [ 2 ] ) ;
67
+ faces . push ( [ a , b , c ] ) ;
68
+ }
69
+
70
+ // Get vertices referenced by faces.
71
+ const vertices = [ ] ;
72
+ for ( let i = 0 ; i < referencedFaceIndices . length ; i ++ ) {
73
+ vertices . push ( this . vertices [ referencedFaceIndices [ i ] ] . point ) ;
74
+ }
75
+
76
+ return { faces, vertices } ;
77
+ } ,
78
+
45
79
setFromPoints : function ( points ) {
46
80
47
81
if ( Array . isArray ( points ) !== true ) {
@@ -60,7 +94,7 @@ var ConvexHull = ( function () {
60
94
61
95
for ( var i = 0 , l = points . length ; i < l ; i ++ ) {
62
96
63
- this . vertices . push ( new VertexNode ( points [ i ] ) ) ;
97
+ this . vertices . push ( new VertexNode ( points [ i ] , i ) ) ;
64
98
65
99
}
66
100
@@ -980,6 +1014,16 @@ var ConvexHull = ( function () {
980
1014
981
1015
Object . assign ( Face . prototype , {
982
1016
1017
+ collectIndices : function ( ) {
1018
+ const indices = [ ] ;
1019
+ let edge = this . edge ;
1020
+ do {
1021
+ indices . push ( edge . head ( ) . index ) ;
1022
+ edge = edge . next ;
1023
+ } while ( edge !== this . edge ) ;
1024
+ return indices ;
1025
+ } ,
1026
+
983
1027
getEdge : function ( i ) {
984
1028
985
1029
var edge = this . edge ;
@@ -1105,12 +1149,15 @@ var ConvexHull = ( function () {
1105
1149
1106
1150
// A vertex as a double linked list node.
1107
1151
1108
- function VertexNode ( point ) {
1152
+ function VertexNode ( point , index ) {
1109
1153
1110
1154
this . point = point ;
1155
+ // index in the input array
1156
+ this . index = index ;
1111
1157
this . prev = null ;
1112
1158
this . next = null ;
1113
- this . face = null ; // the face that is able to see this vertex
1159
+ // the face that is able to see this vertex
1160
+ this . face = null ;
1114
1161
1115
1162
}
1116
1163
0 commit comments