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