-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathoperationsUtils.js
More file actions
233 lines (148 loc) · 5.12 KB
/
Copy pathoperationsUtils.js
File metadata and controls
233 lines (148 loc) · 5.12 KB
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
import { Ray, Matrix4, DoubleSide, Line3 } from 'three';
import { IntersectionMap } from '../IntersectionMap.js';
import {
ADDITION,
SUBTRACTION,
REVERSE_SUBTRACTION,
INTERSECTION,
DIFFERENCE,
HOLLOW_SUBTRACTION,
HOLLOW_INTERSECTION,
} from '../constants.js';
import { isTriDegenerate } from '../utils/triangleUtils.js';
import { getCoplanarIntersectionEdges, isTriangleCoplanar } from '../utils/intersectionUtils.js';
import { Pool } from '../utils/Pool.js';
const _ray = new Ray();
const _matrix = new Matrix4();
const _edge = new Line3();
const _coplanarEdges = [];
const _edgePool = new Pool( () => new Line3() );
export const BACK_SIDE = - 1;
export const FRONT_SIDE = 1;
export const COPLANAR_OPPOSITE = - 2;
export const COPLANAR_ALIGNED = 2;
export const INVERT_TRI = 0;
export const ADD_TRI = 1;
export const SKIP_TRI = 2;
let _debugContext = null;
export function setDebugContext( debugData ) {
_debugContext = debugData;
}
export function getHitSide( tri, bvh, matrix = null ) {
tri.getMidpoint( _ray.origin );
tri.getNormal( _ray.direction );
if ( matrix ) {
_ray.origin.applyMatrix4( matrix );
_ray.direction.transformDirection( matrix );
}
const hit = bvh.raycastFirst( _ray, DoubleSide );
const hitBackSide = Boolean( hit && _ray.direction.dot( hit.face.normal ) > 0 );
return hitBackSide ? BACK_SIDE : FRONT_SIDE;
}
// returns the intersected triangles and returns objects mapping triangle indices to
// the other triangles intersected
export function collectIntersectingTriangles( a, b ) {
const aIntersections = new IntersectionMap();
const bIntersections = new IntersectionMap();
_edgePool.clear();
_matrix
.copy( a.matrixWorld )
.invert()
.multiply( b.matrixWorld );
a.geometry.boundsTree.bvhcast( b.geometry.boundsTree, _matrix, {
intersectsTriangles( triangleA, triangleB, ia, ib ) {
if ( ! isTriDegenerate( triangleA ) && ! isTriDegenerate( triangleB ) ) {
// due to floating point error it's possible that we can have two overlapping, coplanar triangles
// that are a _tiny_ fraction of a value away from each other. If we find that case then check the
// distance between triangles and if it's small enough consider them intersecting.
const coplanarCount = isTriangleCoplanar( triangleA, triangleB ) ? getCoplanarIntersectionEdges( triangleA, triangleB, _coplanarEdges ) : 0;
const isCoplanarIntersection = coplanarCount > 2;
const intersected = isCoplanarIntersection || triangleA.intersectsTriangle( triangleB, _edge, true );
if ( intersected ) {
const va = a.geometry.boundsTree.resolveTriangleIndex( ia );
const vb = b.geometry.boundsTree.resolveTriangleIndex( ib );
aIntersections.add( va, vb, isCoplanarIntersection );
bIntersections.add( vb, va, isCoplanarIntersection );
// cache intersection edges in geometry A's local frame
if ( isCoplanarIntersection ) {
// coplanar
const count = getCoplanarIntersectionEdges( triangleA, triangleB, _coplanarEdges );
for ( let i = 0; i < count; i ++ ) {
const e = _edgePool.getInstance().copy( _coplanarEdges[ i ] );
aIntersections.addIntersectionEdge( va, e );
bIntersections.addIntersectionEdge( vb, e );
}
} else {
// non-coplanar — share the same edge instance for symmetric splitting
const e = _edgePool.getInstance().copy( _edge );
aIntersections.addIntersectionEdge( va, e );
bIntersections.addIntersectionEdge( vb, e );
}
if ( _debugContext ) {
_debugContext.addEdge( _edge );
_debugContext.addIntersectingTriangles( ia, triangleA, ib, triangleB );
}
}
}
return false;
}
} );
return { aIntersections, bIntersections };
}
// Returns the triangle to add when performing an operation
export function getOperationAction( operation, hitSide, invert = false ) {
switch ( operation ) {
case ADDITION:
if ( hitSide === FRONT_SIDE || ( hitSide === COPLANAR_ALIGNED && ! invert ) ) {
return ADD_TRI;
}
break;
case SUBTRACTION:
if ( invert ) {
if ( hitSide === BACK_SIDE ) {
return INVERT_TRI;
}
} else {
if ( hitSide === FRONT_SIDE || hitSide === COPLANAR_OPPOSITE ) {
return ADD_TRI;
}
}
break;
case REVERSE_SUBTRACTION:
if ( invert ) {
if ( hitSide === FRONT_SIDE || hitSide === COPLANAR_OPPOSITE ) {
return ADD_TRI;
}
} else {
if ( hitSide === BACK_SIDE ) {
return INVERT_TRI;
}
}
break;
case DIFFERENCE:
if ( hitSide === BACK_SIDE ) {
return INVERT_TRI;
} else if ( hitSide === FRONT_SIDE ) {
return ADD_TRI;
}
break;
case INTERSECTION:
if ( hitSide === BACK_SIDE || ( hitSide === COPLANAR_ALIGNED && ! invert ) ) {
return ADD_TRI;
}
break;
case HOLLOW_SUBTRACTION:
if ( ! invert && ( hitSide === FRONT_SIDE || hitSide === COPLANAR_OPPOSITE ) ) {
return ADD_TRI;
}
break;
case HOLLOW_INTERSECTION:
if ( ! invert && ( hitSide === BACK_SIDE || hitSide === COPLANAR_ALIGNED ) ) {
return ADD_TRI;
}
break;
default:
throw new Error( `Unrecognized CSG operation enum "${ operation }".` );
}
return SKIP_TRI;
}