SectionContour.js
14.1 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
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
import {
Line,
Line3,
Vector3,
BufferGeometry,
BufferAttribute,
DynamicDrawUsage,
LineSegments,
LineBasicMaterial,
Matrix4,
Plane
} from 'three';
import { buildEdgeMap, posKey2Edge, posVertexToCreateKey , posTri2Indices } from '@/utility/jsm/function/topology';
const drawLine = (points, color = 0x000000) => {
const geometry = new BufferGeometry().setFromPoints(points);
const material = new LineBasicMaterial({
color,
linewidth: 20
});
const line = new Line(geometry, material);
return line;
};
const getPointsCenter = (points) => {
const center = new Vector3();
points.forEach((el) => {
center.add(el);
});
center.multiplyScalar(1 / points.length);
return center;
};
const getPointsBoundary = (points) => {
let maxx = -10000;
let maxy = -10000;
let maxz = -10000;
let minx = 10000;
let miny = 10000;
let minz = 10000;
for (let i = 0; i < points.length; i++) {
const point = points[i];
if (point.x > maxx) {
maxx = point.x;
}
if (point.y > maxy) {
maxy = point.y;
}
if (point.z > maxz) {
maxz = point.z;
}
if (point.x < minx) {
minx = point.x;
}
if (point.y < miny) {
miny = point.y;
}
if (point.z < minz) {
minz = point.z;
}
}
const box = {
max: new Vector3(maxx, maxy, maxz),
min: new Vector3(minx, miny, minz),
center: new Vector3((minx + maxx) * 0.5, (miny + maxy) * 0.5, (minz + maxz) * 0.5)
};
return box;
};
const findNeibor = (triangleIndex, maps) => {
for (let i = 0; i < maps.length; i++) {
if (!maps[i].check) {
const neiborTriangleIndics = maps[i].triIndices;
const neiborIndex = neiborTriangleIndics.indexOf(triangleIndex);
if (neiborIndex !== -1) {
const index = neiborIndex === 0 ? 1 : 0;
maps[i].check = true;
return {
nextIndex: neiborTriangleIndics[index],
point: maps[i].sectionPoint
};
}
}
}
return null;
};
const splitProfilePoints = (maps) => {
const pointsSplit = {};
let targetNeiborIndex = maps[0].triIndices;
let neibor0 = targetNeiborIndex[0];
let rest = [...maps];
let neiborIndex = findNeibor(neibor0, rest);
maps[0].check = true;
let count = 0;
pointsSplit[count] = [];
pointsSplit[count].push(rest[0].sectionPoint);
while (rest.length > 0) {
if (count > 0) {
pointsSplit[count] = [];
targetNeiborIndex = rest[0].triIndices;
neibor0 = targetNeiborIndex[0];
neiborIndex = findNeibor(neibor0, rest);
rest[0].check = true;
}
while (neiborIndex) {
if (neiborIndex) {
pointsSplit[count].push(neiborIndex.point);
}
neiborIndex = findNeibor(neiborIndex.nextIndex, rest);
}
count += 1;
rest = rest.filter((el) => el.check === false);
}
return Object.values(pointsSplit);
// console.log(pointsSplit);
/*
for (let i = 0; i < count; i++) {
let color = '#'+Math.floor(Math.random()*16777215).toString(16);
const c = drawPoints(pointsSplit[i], color);
this._scene.add(c);
}
const b = drawPoints(pointsSplit, 0x0000ff);
this._scene.add(b);
*/
};
class SectionContour {
constructor({ scene, sectionviewScene }) {
this._scene = scene;
this._sectionviewScene = sectionviewScene;
this.buildEdgeMaps = [];
this.profileName = 'profilelines';
this.outlineName = 'outlines';
this.outlineCenter = null;
this.outlineMax = null;
this.plane = new Plane(new Vector3(0, 0, -1), 0);
}
setBuildEdgeMaps = (geometryMaps) => {
this.buildEdgeMaps = geometryMaps;
};
computeProfiles = (mesh, tangent, passPoint) => {
const tempLine = new Line3();
const tempVector = new Vector3();
const tempVector1 = new Vector3();
const tempVector2 = new Vector3();
const tempVector3 = new Vector3();
const lineGeometry = new BufferGeometry();
const linePosAttr = new BufferAttribute(new Float32Array(300000), 3, false);
linePosAttr.setUsage(DynamicDrawUsage);
lineGeometry.setAttribute('position', linePosAttr);
const outlineLines = new LineSegments(lineGeometry, new LineBasicMaterial());
outlineLines.name = this.outlineName;
outlineLines.material.color.set(0x000).convertSRGBToLinear();
outlineLines.frustumCulled = false;
const posAttr = outlineLines.geometry.attributes.position;
const inverseMatrix = new Matrix4();
inverseMatrix.copy(mesh.matrix).invert();
const computePlane = this.plane.clone();
computePlane.applyMatrix4(inverseMatrix);
let index = 0;
const points = [];
const maps = []; // create topology to get the closed profile part
const mapsTemp = [];
mesh.geometry.boundsTree.shapecast({
intersectsBounds: (box) => {
// if we're not using the BVH then skip straight to iterating over all triangles
return computePlane.intersectsBox(box);
},
intersectsTriangle: (tri) => {
// check each triangle edge to see if it intersects with the plane. If so then
// add it to the list of segments.
let count = 0;
tempLine.start.copy(tri.a);
tempLine.end.copy(tri.b);
if (computePlane.intersectLine(tempLine, tempVector)) {
posAttr.setXYZ(index, tempVector.x, tempVector.y, tempVector.z);
points.push(tempVector.clone());
const key = posVertexToCreateKey(tri.a, tri.b);
const neiborTriangleIndics = posKey2Edge(this.buildEdgeMaps, key);
if (neiborTriangleIndics) {
if (mapsTemp[key] === undefined) {
mapsTemp[key] = true;
maps.push({
key,
sectionPoint: tempVector.clone(),
triIndices: [...neiborTriangleIndics],
check: false
});
}
}
index += 1;
count += 1;
}
tempLine.start.copy(tri.b);
tempLine.end.copy(tri.c);
if (computePlane.intersectLine(tempLine, tempVector)) {
posAttr.setXYZ(index, tempVector.x, tempVector.y, tempVector.z);
points.push(tempVector.clone());
const key = posVertexToCreateKey(tri.b, tri.c);
const neiborTriangleIndics = posKey2Edge(this.buildEdgeMaps, key);
if (neiborTriangleIndics) {
if (mapsTemp[key] === undefined) {
mapsTemp[key] = true;
maps.push({
key,
sectionPoint: tempVector.clone(),
triIndices: [...neiborTriangleIndics],
check: false
});
}
}
index += 1;
count += 1;
}
tempLine.start.copy(tri.c);
tempLine.end.copy(tri.a);
if (computePlane.intersectLine(tempLine, tempVector)) {
posAttr.setXYZ(index, tempVector.x, tempVector.y, tempVector.z);
points.push(tempVector.clone());
const key = posVertexToCreateKey(tri.c, tri.a);
const neiborTriangleIndics = posKey2Edge(this.buildEdgeMaps, key);
if (neiborTriangleIndics) {
if (mapsTemp[key] === undefined) {
mapsTemp[key] = true;
maps.push({
key,
sectionPoint: tempVector.clone(),
triIndices: [...neiborTriangleIndics],
check: false
});
}
}
index += 1;
count += 1;
}
// When the plane passes through a vertex and one of the edges of the triangle, there will be three intersections, two of which must be repeated
if (count === 3) {
tempVector1.fromBufferAttribute(posAttr, index - 3);
tempVector2.fromBufferAttribute(posAttr, index - 2);
tempVector3.fromBufferAttribute(posAttr, index - 1);
// If the last point is a duplicate intersection
if (tempVector3.equals(tempVector1) || tempVector3.equals(tempVector2)) {
index -= 1;
count -= 1;
} else if (tempVector1.equals(tempVector2)) {
// If the last point is not a duplicate intersection
// Set the penultimate point as a distinct point and delete the last point
posAttr.setXYZ(index - 2, tempVector3);
points.push(tempVector3.clone());
const key = posVertexToCreateKey(tempVector1, tempVector2);
const neiborTriangleIndics = posKey2Edge(this.buildEdgeMaps, key);
if (neiborTriangleIndics) {
if (mapsTemp[key] === undefined) {
mapsTemp[key] = true;
maps.push({
key,
sectionPoint: tempVector3.clone(),
triIndices: [...neiborTriangleIndics],
check: false
});
}
}
index -= 1;
count -= 1;
}
}
// If we only intersected with one or three sides then just remove it. This could be handled
// more gracefully.
if (count !== 2) {
index -= count;
}
}
});
posAttr.needsUpdate = true;
const isProfile = this.updateProfileLines(maps, tangent, passPoint);
return isProfile;
};
updateProfileLines = (maps, tangent, passPoint) => {
const { _scene } = this;
if (_scene) {
const objects = _scene.children.filter((el) => el.name === this.profileName);
objects.forEach((el) => {
_scene.remove(el);
});
}
if (maps.length === 0) return false;
const splits = splitProfilePoints(maps);
const planeTangent = tangent.clone();
planeTangent.multiplyScalar(-1);
planeTangent.z = 0;
const planePassPoint = passPoint.clone();
planePassPoint.multiplyScalar(-1);
let lineSegments = null;
let box = null;
let _linesCenter = new Vector3();
if (splits.length > 1) {
const temps = [];
let tempTangent = new Vector3();
let nearestIndex = 0;
for (let i = 0; i < splits.length; i++) {
const center = getPointsCenter(splits[i]);
tempTangent = center.clone().sub(planePassPoint).normalize();
tempTangent.z = 0;
if (Math.sign(planeTangent.dot(tempTangent)) > 0) {
nearestIndex = i;
temps.push({
key: i,
center: center.clone()
});
}
}
if (temps.length > 1) {
let distance = 0;
let maxDistance = Infinity;
for (let i = 0; i < temps.length; i++) {
const temp = temps[i];
distance = temp.center.distanceTo(planePassPoint);
if (distance < maxDistance) {
nearestIndex = temp.key;
maxDistance = distance;
}
}
}
splits[nearestIndex].push(splits[nearestIndex][0]);
box = getPointsBoundary(splits[nearestIndex]);
_linesCenter = getPointsCenter(splits[nearestIndex]);
const lines = drawLine(splits[nearestIndex]);
lines.name = this.profileName;
//this._scene.add(lines);
lineSegments = drawLine(splits[nearestIndex]);
} else if (splits.length === 1) {
splits[0].push(splits[0][0]);
box = getPointsBoundary(splits[0]);
_linesCenter = getPointsCenter(splits[0]);
const lines = drawLine(splits[0]);
lines.name = this.profileName;
//this._scene.add(lines);
lineSegments = drawLine(splits[0]);
}
if (box) {
const center = new Vector3().copy(box.center);
this.outlineCenter = _linesCenter.clone();
const invertCenter = center.clone().multiplyScalar(-1);
lineSegments.position.copy(invertCenter);
this.outlineMax = box.max.clone();
}
const sectionViewScene = this._sectionviewScene;
if (sectionViewScene) {
const lines = sectionViewScene.children.filter((el) => el.name === this.outlineName);
lines.forEach((element) => {
sectionViewScene.remove(element);
});
lineSegments.name = this.outlineName;
sectionViewScene.add(lineSegments);
}
return splits.length > 0;
};
}
export default SectionContour;