@buerli.io/core 1.0.1 → 1.1.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +19 -19
  2. package/build/cjs/geometry/ThreeGeometryCreator.js +320 -320
  3. package/build/cjs/geometry/index.js +17 -17
  4. package/build/cjs/graphic/filters.js +25 -25
  5. package/build/cjs/index.js +26 -26
  6. package/build/cjs/re-export.js +29 -29
  7. package/build/cjs/store/index.js +41 -37
  8. package/build/cjs/store/states/buerli.js +98 -98
  9. package/build/cjs/store/states/cad.js +47 -47
  10. package/build/cjs/store/states/drawing.js +49 -49
  11. package/build/cjs/store/states/geometry.js +157 -157
  12. package/build/cjs/store/states/graphic.js +115 -115
  13. package/build/cjs/store/states/interaction.js +55 -55
  14. package/build/cjs/store/states/message.js +32 -25
  15. package/build/cjs/store/states/plugin.js +115 -115
  16. package/build/cjs/store/states/selection.js +267 -267
  17. package/build/cjs/store/states/structure.js +250 -250
  18. package/build/cjs/store/store.js +9 -9
  19. package/build/cjs/store/subscribtion/selection.js +49 -49
  20. package/build/cjs/store/subscribtion/subscribeToDrawing.js +13 -13
  21. package/build/cjs/types/InteractionInfo.js +11 -11
  22. package/build/cjs/types/index.js +34 -34
  23. package/build/cjs/utils/Math.js +91 -91
  24. package/build/cjs/utils/Measure.js +576 -576
  25. package/build/cjs/utils/Timing.js +34 -34
  26. package/build/esm/geometry/ThreeGeometryCreator.js +291 -291
  27. package/build/esm/geometry/index.js +1 -1
  28. package/build/esm/graphic/filters.js +12 -12
  29. package/build/esm/index.js +10 -10
  30. package/build/esm/re-export.js +23 -23
  31. package/build/esm/store/index.js +29 -26
  32. package/build/esm/store/states/buerli.js +92 -92
  33. package/build/esm/store/states/cad.js +42 -42
  34. package/build/esm/store/states/drawing.js +45 -45
  35. package/build/esm/store/states/geometry.js +129 -129
  36. package/build/esm/store/states/graphic.js +110 -110
  37. package/build/esm/store/states/interaction.js +50 -50
  38. package/build/esm/store/states/message.js +28 -21
  39. package/build/esm/store/states/plugin.js +109 -109
  40. package/build/esm/store/states/selection.js +261 -261
  41. package/build/esm/store/states/structure.js +219 -219
  42. package/build/esm/store/store.js +3 -3
  43. package/build/esm/store/subscribtion/selection.js +44 -44
  44. package/build/esm/store/subscribtion/subscribeToDrawing.js +9 -9
  45. package/build/esm/types/InteractionInfo.js +6 -6
  46. package/build/esm/types/index.js +16 -16
  47. package/build/esm/utils/Math.js +65 -65
  48. package/build/esm/utils/Measure.js +550 -550
  49. package/build/esm/utils/Timing.js +31 -31
  50. package/build/types/geometry/ThreeGeometryCreator.d.ts +256 -256
  51. package/build/types/geometry/index.d.ts +1 -1
  52. package/build/types/graphic/filters.d.ts +11 -11
  53. package/build/types/index.d.ts +38 -38
  54. package/build/types/re-export.d.ts +70 -70
  55. package/build/types/store/index.d.ts +17 -16
  56. package/build/types/store/states/buerli.d.ts +41 -41
  57. package/build/types/store/states/cad.d.ts +56 -56
  58. package/build/types/store/states/drawing.d.ts +45 -45
  59. package/build/types/store/states/geometry.d.ts +64 -64
  60. package/build/types/store/states/graphic.d.ts +21 -21
  61. package/build/types/store/states/interaction.d.ts +34 -34
  62. package/build/types/store/states/message.d.ts +32 -30
  63. package/build/types/store/states/plugin.d.ts +53 -53
  64. package/build/types/store/states/selection.d.ts +69 -69
  65. package/build/types/store/states/structure.d.ts +47 -47
  66. package/build/types/store/store.d.ts +2 -2
  67. package/build/types/store/subscribtion/selection.d.ts +7 -7
  68. package/build/types/store/subscribtion/subscribeToDrawing.d.ts +4 -4
  69. package/build/types/types/InteractionInfo.d.ts +42 -42
  70. package/build/types/types/index.d.ts +7 -7
  71. package/build/types/utils/Math.d.ts +21 -21
  72. package/build/types/utils/Measure.d.ts +64 -64
  73. package/build/types/utils/Timing.d.ts +8 -8
  74. package/package.json +2 -2
@@ -1,550 +1,550 @@
1
- /* eslint-disable max-lines */
2
- import * as THREE from 'three';
3
- import { GraphicType } from '../re-export';
4
- import { getDrawing } from './../store';
5
- export var MeasureResultType;
6
- (function (MeasureResultType) {
7
- MeasureResultType["NONE"] = "none";
8
- MeasureResultType["VALUE"] = "value";
9
- MeasureResultType["DISTANCE"] = "linear";
10
- MeasureResultType["ANGLE"] = "angular";
11
- MeasureResultType["VECTOR"] = "vector";
12
- MeasureResultType["POSITION"] = "position";
13
- })(MeasureResultType || (MeasureResultType = {}));
14
- const tolerance = 1e-3;
15
- export const Measure = {
16
- transform(items, drawingId) {
17
- const drawing = getDrawing(drawingId);
18
- const transformed = [];
19
- for (const item of items) {
20
- const matrix = drawing.api.structure.calculateGlobalTransformation(item.productId);
21
- const matrix3 = new THREE.Matrix3().setFromMatrix4(matrix);
22
- const newItem = Object.assign({}, item);
23
- switch (newItem.type) {
24
- case GraphicType.POINT:
25
- case GraphicType.CURVEPOINT:
26
- const point = newItem;
27
- point.position = point.position.clone().applyMatrix4(matrix);
28
- break;
29
- case GraphicType.LINE:
30
- case GraphicType.CURVELINE:
31
- const line = newItem;
32
- line.start = line.start.clone().applyMatrix4(matrix);
33
- line.end = line.end.clone().applyMatrix4(matrix);
34
- break;
35
- case GraphicType.PLANE:
36
- const plane = newItem;
37
- plane.normal = plane.normal.clone().applyMatrix3(matrix3);
38
- plane.normal.normalize();
39
- plane.pointOnPlane = plane.pointOnPlane.clone().applyMatrix4(matrix);
40
- break;
41
- case GraphicType.CYLINDER:
42
- const cylinder = newItem;
43
- cylinder.axis = cylinder.axis.clone().applyMatrix3(matrix3);
44
- cylinder.axis.normalize();
45
- cylinder.origin = cylinder.origin.clone().applyMatrix4(matrix);
46
- break;
47
- case GraphicType.CONE:
48
- const cone = newItem;
49
- cone.axis = cone.axis.clone().applyMatrix3(matrix3);
50
- cone.axis.normalize();
51
- cone.origin = cone.origin.clone().applyMatrix4(matrix);
52
- break;
53
- case GraphicType.ARC:
54
- case GraphicType.CURVEARC:
55
- const arc = newItem;
56
- arc.normal = arc.normal.clone().applyMatrix3(matrix3);
57
- arc.normal.normalize();
58
- arc.center = arc.center.clone().applyMatrix4(matrix);
59
- arc.start = arc.start.clone().applyMatrix4(matrix);
60
- arc.end = arc.end.clone().applyMatrix4(matrix);
61
- break;
62
- case GraphicType.CIRCLE:
63
- case GraphicType.CURVECIRCLE:
64
- const circle = newItem;
65
- circle.normal = circle.normal.clone().applyMatrix3(matrix3);
66
- circle.normal.normalize();
67
- circle.center = circle.center.clone().applyMatrix4(matrix);
68
- circle.start = circle.start.clone().applyMatrix4(matrix);
69
- circle.end = circle.end.clone().applyMatrix4(matrix);
70
- break;
71
- default:
72
- break;
73
- }
74
- transformed.push(newItem);
75
- }
76
- return transformed;
77
- },
78
- getInfo(objects, drawingId) {
79
- const transformed = Measure.transform(objects, drawingId);
80
- const results = [];
81
- const dir = (start, end) => end.clone().sub(start).normalize();
82
- const getPointOnCircumference = (center, axis, radius) => {
83
- const axisNormalized = axis.clone().normalize();
84
- const helperVec = axisNormalized.x !== 1 ? new THREE.Vector3(1, 0, 0) : new THREE.Vector3(0, 1, 0);
85
- const tangent = axisNormalized.clone().cross(helperVec).normalize();
86
- return center.clone().addScaledVector(tangent, radius);
87
- };
88
- for (const element of transformed) {
89
- switch (element.type) {
90
- case GraphicType.POINT:
91
- case GraphicType.CURVEPOINT:
92
- const point = element;
93
- results.push({
94
- description: 'Position',
95
- type: MeasureResultType.POSITION,
96
- measureData: { value: point.position.clone() },
97
- });
98
- break;
99
- case GraphicType.LINE:
100
- case GraphicType.CURVELINE:
101
- const line = element;
102
- const absoluteDistance = line.start.distanceTo(line.end);
103
- results.push({
104
- description: 'Line length',
105
- type: MeasureResultType.DISTANCE,
106
- measureData: { value: absoluteDistance, start: line.start.clone(), end: line.end.clone() },
107
- });
108
- break;
109
- case GraphicType.NURBSCURVE:
110
- case GraphicType.CURVESPLINE:
111
- const edge = element;
112
- results.push({
113
- description: 'Edge start',
114
- type: MeasureResultType.POSITION,
115
- measureData: { value: edge.start.clone() },
116
- });
117
- results.push({
118
- description: 'Edge end',
119
- type: MeasureResultType.POSITION,
120
- measureData: { value: edge.end.clone() },
121
- });
122
- break;
123
- case GraphicType.ARC:
124
- case GraphicType.CURVEARC:
125
- const arc = element;
126
- const arcNEnd = arc.center.clone().addScaledVector(arc.normal.clone().normalize(), 40);
127
- const pointOnArc = getPointOnCircumference(arc.center, arc.normal, arc.radius);
128
- results.push({
129
- description: 'Arc start',
130
- type: MeasureResultType.POSITION,
131
- measureData: { value: arc.start.clone() },
132
- });
133
- results.push({
134
- description: 'Arc end',
135
- type: MeasureResultType.POSITION,
136
- measureData: { value: arc.end.clone() },
137
- });
138
- results.push({
139
- description: 'Arc center',
140
- type: MeasureResultType.POSITION,
141
- measureData: { value: arc.center.clone() },
142
- });
143
- results.push({
144
- description: 'Arc normal',
145
- type: MeasureResultType.VECTOR,
146
- measureData: { value: arc.normal.clone(), start: arc.center.clone(), end: arcNEnd },
147
- });
148
- results.push({
149
- description: 'Arc radius',
150
- type: MeasureResultType.DISTANCE,
151
- measureData: { value: arc.radius, start: arc.center.clone(), end: pointOnArc },
152
- });
153
- break;
154
- case GraphicType.CIRCLE:
155
- case GraphicType.CURVECIRCLE:
156
- const circle = element;
157
- const circleNEnd = circle.center.clone().addScaledVector(circle.normal.clone().normalize(), 40);
158
- const pointOnCircle = getPointOnCircumference(circle.center, circle.normal, circle.radius);
159
- results.push({
160
- description: 'Circle center',
161
- type: MeasureResultType.POSITION,
162
- measureData: { value: circle.center.clone() },
163
- });
164
- results.push({
165
- description: 'Circle normal',
166
- type: MeasureResultType.VECTOR,
167
- measureData: { value: circle.normal.clone(), start: circle.center.clone(), end: circleNEnd },
168
- });
169
- results.push({
170
- description: 'Circle radius',
171
- type: MeasureResultType.DISTANCE,
172
- measureData: { value: circle.radius, start: circle.center.clone(), end: pointOnCircle },
173
- });
174
- results.push({
175
- description: 'Circle start/end',
176
- type: MeasureResultType.POSITION,
177
- measureData: { value: circle.start.clone() },
178
- });
179
- break;
180
- case GraphicType.PLANE:
181
- const plane = element;
182
- const planeNEnd = plane.pointOnPlane.clone().addScaledVector(plane.normal.clone().normalize(), 40);
183
- results.push({
184
- description: 'Plane normal',
185
- type: MeasureResultType.VECTOR,
186
- measureData: { value: plane.normal, start: plane.pointOnPlane.clone(), end: planeNEnd },
187
- });
188
- break;
189
- case GraphicType.CYLINDER:
190
- const cylinder = element;
191
- const cylAxisNormalized = cylinder.axis.clone().normalize();
192
- const cylNEnd = cylinder.origin.clone().addScaledVector(cylAxisNormalized, cylinder.height + 40);
193
- const pointOnCyl = getPointOnCircumference(cylinder.origin, cylinder.axis, cylinder.radius);
194
- const cylHStart = cylinder.origin
195
- .clone()
196
- .addScaledVector(dir(cylinder.origin, pointOnCyl), cylinder.radius + 3);
197
- const cylHEnd = cylHStart.clone().addScaledVector(cylAxisNormalized, cylinder.height);
198
- results.push({
199
- description: 'Cylinder origin',
200
- type: MeasureResultType.POSITION,
201
- measureData: { value: cylinder.origin.clone() },
202
- });
203
- results.push({
204
- description: 'Cylinder axis',
205
- type: MeasureResultType.VECTOR,
206
- measureData: { value: cylinder.axis, start: cylinder.origin.clone(), end: cylNEnd },
207
- });
208
- results.push({
209
- description: 'Cylinder radius',
210
- type: MeasureResultType.DISTANCE,
211
- measureData: { value: cylinder.radius, start: cylinder.origin.clone(), end: pointOnCyl },
212
- });
213
- results.push({
214
- description: 'Cylinder height',
215
- type: MeasureResultType.DISTANCE,
216
- measureData: { value: cylinder.height, start: cylHStart, end: cylHEnd },
217
- });
218
- break;
219
- case GraphicType.CONE:
220
- const cone = element;
221
- const isExpanding = cone.radiusBottom < cone.radiusTop;
222
- const R = Math.max(cone.radiusBottom, cone.radiusTop);
223
- const r = Math.min(cone.radiusBottom, cone.radiusTop);
224
- const coneAxisNormalized = cone.axis.clone().normalize();
225
- const coneCenterTop = cone.origin.clone().addScaledVector(coneAxisNormalized, cone.height);
226
- const coneEnd = coneCenterTop.clone().addScaledVector(coneAxisNormalized, 40);
227
- const coneCenterMinR = isExpanding ? cone.origin.clone() : coneCenterTop.clone();
228
- const coneCenterMaxR = isExpanding ? coneCenterTop.clone() : cone.origin.clone();
229
- const pointOnConeBottom = getPointOnCircumference(cone.origin, cone.axis, cone.radiusBottom);
230
- const pointOnConeTop = getPointOnCircumference(coneCenterTop, cone.axis, cone.radiusTop);
231
- const pointOnConeMinR = isExpanding ? pointOnConeBottom : pointOnConeTop;
232
- const pointOnConeMaxR = isExpanding ? pointOnConeTop : pointOnConeBottom;
233
- const coneHStart = coneCenterMaxR.clone().addScaledVector(dir(coneCenterMaxR, pointOnConeMaxR), R + 3);
234
- const coneHEnd = coneHStart
235
- .clone()
236
- .addScaledVector(coneAxisNormalized, isExpanding ? -cone.height : cone.height);
237
- results.push({
238
- description: 'Cone origin',
239
- type: MeasureResultType.POSITION,
240
- measureData: { value: cone.origin.clone() },
241
- });
242
- results.push({
243
- description: 'Cone axis',
244
- type: MeasureResultType.VECTOR,
245
- measureData: { value: cone.axis, start: cone.origin.clone(), end: coneEnd },
246
- });
247
- results.push({
248
- description: 'Cone radius (min)',
249
- type: MeasureResultType.DISTANCE,
250
- measureData: { value: r, start: coneCenterMinR, end: pointOnConeMinR },
251
- });
252
- results.push({
253
- description: 'Cone radius (max)',
254
- type: MeasureResultType.DISTANCE,
255
- measureData: { value: R, start: coneCenterMaxR, end: pointOnConeMaxR },
256
- });
257
- results.push({
258
- description: 'Cone height',
259
- type: MeasureResultType.DISTANCE,
260
- measureData: { value: cone.height, start: coneHStart, end: coneHEnd },
261
- });
262
- break;
263
- case GraphicType.SPHERE:
264
- const sphere = element;
265
- results.push({
266
- description: 'Sphere radius',
267
- type: MeasureResultType.VALUE,
268
- measureData: { value: sphere.radius },
269
- });
270
- break;
271
- case GraphicType.NURBSSURFACE:
272
- results.push({ description: 'NURBS Surface', type: MeasureResultType.NONE });
273
- break;
274
- default:
275
- results.push({ description: 'Undefined', type: MeasureResultType.NONE });
276
- break;
277
- }
278
- }
279
- return results;
280
- },
281
- calculateMeasures(objects, drawingId) {
282
- const transformed = Measure.transform(objects, drawingId);
283
- const first = transformed[0];
284
- const second = transformed[1];
285
- let results = [];
286
- switch (first.type) {
287
- case GraphicType.POINT:
288
- case GraphicType.CURVEPOINT:
289
- switch (second.type) {
290
- case GraphicType.POINT:
291
- case GraphicType.CURVEPOINT:
292
- results = Measure.calculatePointPoint(first.position, second.position);
293
- break;
294
- case GraphicType.LINE:
295
- case GraphicType.CURVELINE:
296
- results = Measure.calculatePointLine(first, second);
297
- break;
298
- case GraphicType.PLANE:
299
- results = Measure.calculatePointPlane(first, second);
300
- break;
301
- case GraphicType.ARC:
302
- case GraphicType.CIRCLE:
303
- results = Measure.calculatePointPoint(first.position.clone(), second.center.clone());
304
- break;
305
- default:
306
- results = Measure.getInfo(objects, drawingId);
307
- break;
308
- }
309
- break;
310
- case GraphicType.LINE:
311
- case GraphicType.CURVELINE:
312
- switch (second.type) {
313
- case GraphicType.POINT:
314
- case GraphicType.CURVEPOINT:
315
- results = Measure.calculatePointLine(second, first);
316
- break;
317
- case GraphicType.LINE:
318
- case GraphicType.CURVELINE:
319
- results = Measure.calculateLineLine(first, second);
320
- break;
321
- case GraphicType.PLANE:
322
- results = Measure.calculateLinePlane(first, second);
323
- break;
324
- default:
325
- results = Measure.getInfo(objects, drawingId);
326
- break;
327
- }
328
- break;
329
- case GraphicType.CIRCLE:
330
- case GraphicType.ARC:
331
- switch (second.type) {
332
- case GraphicType.ARC:
333
- case GraphicType.CIRCLE:
334
- results = Measure.calculatePointPoint(first.center, second.center);
335
- break;
336
- case GraphicType.POINT:
337
- results = Measure.calculatePointPoint(first.center, second.position);
338
- break;
339
- default:
340
- results = Measure.getInfo(objects, drawingId);
341
- break;
342
- }
343
- break;
344
- case GraphicType.PLANE:
345
- switch (second.type) {
346
- case GraphicType.POINT:
347
- case GraphicType.CURVEPOINT:
348
- results = Measure.calculatePointPlane(second, first);
349
- break;
350
- case GraphicType.LINE:
351
- case GraphicType.CURVELINE:
352
- results = Measure.calculateLinePlane(second, first);
353
- break;
354
- case GraphicType.ARC:
355
- case GraphicType.CURVEARC:
356
- results = Measure.calculateArcPlane(second, first);
357
- break;
358
- case GraphicType.PLANE:
359
- results = Measure.calculatePlanePlane(first, second);
360
- break;
361
- default:
362
- results = Measure.getInfo(objects, drawingId);
363
- break;
364
- }
365
- break;
366
- default:
367
- results = Measure.getInfo(objects, drawingId);
368
- break;
369
- }
370
- return results;
371
- },
372
- calculatePointPoint(point1, point2) {
373
- const p1 = point1.clone();
374
- const p2 = point2.clone();
375
- const distance = p1.distanceTo(p2);
376
- const dx = Math.abs(p1.x - p2.x);
377
- const dy = Math.abs(p1.y - p2.y);
378
- const dz = Math.abs(p1.z - p2.z);
379
- const px = p1.clone().setX(p2.x);
380
- const pxy = px.clone().setY(p2.y);
381
- return [
382
- {
383
- description: 'Distance',
384
- type: MeasureResultType.DISTANCE,
385
- measureData: { value: distance, start: p1, end: p2 },
386
- },
387
- { description: 'dx', type: MeasureResultType.DISTANCE, measureData: { value: dx, start: p1, end: px } },
388
- { description: 'dy', type: MeasureResultType.DISTANCE, measureData: { value: dy, start: px, end: pxy } },
389
- { description: 'dz', type: MeasureResultType.DISTANCE, measureData: { value: dz, start: pxy, end: p2 } },
390
- ];
391
- },
392
- calculatePointLine(point, line) {
393
- const [start, end] = Measure.getPointLineMPoints(point.position, line.start, line.end);
394
- const distance = start.distanceTo(end);
395
- return [
396
- {
397
- description: 'Distance',
398
- type: MeasureResultType.DISTANCE,
399
- measureData: { value: distance, start, end },
400
- },
401
- ];
402
- },
403
- calculatePointPlane(point, plane) {
404
- const [start, end] = Measure.getPointPlaneMPoints(point.position, plane.pointOnPlane, plane.normal);
405
- const distance = start.distanceTo(end);
406
- return [
407
- {
408
- description: 'Distance',
409
- type: MeasureResultType.DISTANCE,
410
- measureData: { value: distance, start, end },
411
- },
412
- ];
413
- },
414
- calculateLineLine(first, second) {
415
- const [start, end] = Measure.getLineLineMPoints(first.start, first.end, second.start, second.end);
416
- const distance = start && end ? start.distanceTo(end) : NaN;
417
- const [dir1, dir2] = Measure.getLineLineMDirs(first.start, first.end, second.start, second.end);
418
- const angle = Measure.degrees(dir1.angleTo(dir2));
419
- const angleS = 180 - angle;
420
- return [
421
- {
422
- description: 'Distance',
423
- type: MeasureResultType.DISTANCE,
424
- measureData: start && end ? { value: distance, start, end } : undefined,
425
- },
426
- {
427
- description: 'Angle1',
428
- type: MeasureResultType.ANGLE,
429
- measureData: { value: angle, center: first.start, dir1, dir2 },
430
- },
431
- {
432
- description: 'Angle2',
433
- type: MeasureResultType.ANGLE,
434
- measureData: { value: angleS, center: first.start, dir1, dir2: dir2.clone().negate() },
435
- },
436
- ];
437
- },
438
- calculateLinePlane(line, plane) {
439
- const [start, end] = Measure.getLinePlaneMPoints(line.start, line.end, plane.pointOnPlane, plane.normal);
440
- const distance = start && end ? start.distanceTo(end) : NaN;
441
- return [
442
- {
443
- description: 'Distance',
444
- type: MeasureResultType.DISTANCE,
445
- measureData: start && end ? { value: distance, start, end } : undefined,
446
- },
447
- ];
448
- },
449
- calculateArcPlane(arc, plane) {
450
- const [start, end] = Measure.getArcPlaneMPoints(arc.center, arc.normal, plane.pointOnPlane, plane.normal);
451
- const distance = start && end ? start.distanceTo(end) : NaN;
452
- return [
453
- {
454
- description: 'Distance',
455
- type: MeasureResultType.DISTANCE,
456
- measureData: start && end ? { value: distance, start, end } : undefined,
457
- },
458
- ];
459
- },
460
- calculatePlanePlane(plane1, plane2) {
461
- const [start, end] = Measure.getPlanePlaneMPoints(plane1.pointOnPlane, plane1.normal, plane2.pointOnPlane, plane2.normal);
462
- const distance = start && end ? start.distanceTo(end) : NaN;
463
- const [dir1, dir2] = Measure.getPlanePlaneMDirs(plane1.normal, plane2.normal);
464
- const angle = dir1 && dir2 ? Measure.degrees(dir1.angleTo(dir2)) : Measure.degrees(plane1.normal.angleTo(plane2.normal));
465
- return [
466
- {
467
- description: 'Distance',
468
- type: MeasureResultType.DISTANCE,
469
- measureData: start && end ? { value: distance, start, end } : undefined,
470
- },
471
- {
472
- description: 'Angle',
473
- type: MeasureResultType.ANGLE,
474
- measureData: dir1 && dir2 ? { value: angle, center: plane1.pointOnPlane.clone(), dir1, dir2 } : undefined,
475
- },
476
- ];
477
- },
478
- degrees(radians) {
479
- return (radians / Math.PI) * 180;
480
- },
481
- areParallel(vec1, vec2) {
482
- const val = Math.abs(vec1.clone().cross(vec2).length());
483
- return val < tolerance;
484
- },
485
- arePerpendicular(vec1, vec2) {
486
- const val = Math.abs(vec1.clone().normalize().dot(vec2.clone().normalize()));
487
- return val < tolerance;
488
- },
489
- getPointLineMPoints(pointsPos, startPos, endPos) {
490
- const p1 = pointsPos.clone();
491
- const lineDir = endPos.clone().sub(startPos);
492
- const p2 = pointsPos.clone().sub(startPos).projectOnVector(lineDir).add(startPos);
493
- return [p1, p2];
494
- },
495
- getPointPlaneMPoints(pointPos, pointOnPlane, normal) {
496
- const p1 = pointPos.clone();
497
- const offsetV = pointPos.clone().sub(pointOnPlane).projectOnVector(normal);
498
- const p2 = pointPos.clone().sub(offsetV);
499
- return [p1, p2];
500
- },
501
- getLineLineMPoints(start1Pos, end1Pos, start2Pos, end2Pos) {
502
- const vec1 = start1Pos.clone().sub(end1Pos);
503
- const vec2 = start2Pos.clone().sub(end2Pos);
504
- if (Measure.areParallel(vec1, vec2)) {
505
- // lines are parallel
506
- const vecAB = start1Pos.clone().sub(start2Pos);
507
- if (!Measure.areParallel(vecAB, vec1)) {
508
- // lines are not conincident
509
- return Measure.getPointLineMPoints(start1Pos, start2Pos, end2Pos);
510
- }
511
- }
512
- return [undefined, undefined];
513
- },
514
- getLinePlaneMPoints(startPos, endPos, pointOnPlane, normal) {
515
- const vec = startPos.clone().sub(endPos);
516
- if (Measure.arePerpendicular(vec, normal)) {
517
- // line is perpendicular to plane normal
518
- return Measure.getPointPlaneMPoints(startPos, pointOnPlane, normal);
519
- }
520
- return [undefined, undefined];
521
- },
522
- getArcPlaneMPoints(arcCenter, arcNormal, pointOnPlane, planeNormal) {
523
- if (Measure.areParallel(arcNormal, planeNormal)) {
524
- // parallel
525
- return Measure.getPointPlaneMPoints(arcCenter, pointOnPlane, planeNormal);
526
- }
527
- return [undefined, undefined];
528
- },
529
- getPlanePlaneMPoints(pointOnPlane1, normal1, pointOnPlane2, normal2) {
530
- if (Measure.areParallel(normal1, normal2)) {
531
- // parallel
532
- return Measure.getPointPlaneMPoints(pointOnPlane1, pointOnPlane2, normal2);
533
- }
534
- return [undefined, undefined];
535
- },
536
- getLineLineMDirs(start1Pos, end1Pos, start2Pos, end2Pos) {
537
- const vec1 = end1Pos.clone().sub(start1Pos);
538
- const vec2 = end2Pos.clone().sub(start2Pos);
539
- return [vec1, vec2];
540
- },
541
- getPlanePlaneMDirs(normal1, normal2) {
542
- if (!Measure.areParallel(normal1, normal2)) {
543
- const tangent1 = normal1.clone().projectOnPlane(normal2).normalize();
544
- const tangent2 = normal2.clone().projectOnPlane(normal1).normalize();
545
- const angle = tangent1.angleTo(tangent2);
546
- return angle <= Math.PI / 2 ? [tangent1, tangent2] : [tangent1, tangent2.negate()];
547
- }
548
- return [undefined, undefined];
549
- },
550
- };
1
+ /* eslint-disable max-lines */
2
+ import * as THREE from 'three';
3
+ import { GraphicType } from '../re-export';
4
+ import { getDrawing } from './../store';
5
+ export var MeasureResultType;
6
+ (function (MeasureResultType) {
7
+ MeasureResultType["NONE"] = "none";
8
+ MeasureResultType["VALUE"] = "value";
9
+ MeasureResultType["DISTANCE"] = "linear";
10
+ MeasureResultType["ANGLE"] = "angular";
11
+ MeasureResultType["VECTOR"] = "vector";
12
+ MeasureResultType["POSITION"] = "position";
13
+ })(MeasureResultType || (MeasureResultType = {}));
14
+ const tolerance = 1e-3;
15
+ export const Measure = {
16
+ transform(items, drawingId) {
17
+ const drawing = getDrawing(drawingId);
18
+ const transformed = [];
19
+ for (const item of items) {
20
+ const matrix = drawing.api.structure.calculateGlobalTransformation(item.productId);
21
+ const matrix3 = new THREE.Matrix3().setFromMatrix4(matrix);
22
+ const newItem = Object.assign({}, item);
23
+ switch (newItem.type) {
24
+ case GraphicType.POINT:
25
+ case GraphicType.CURVEPOINT:
26
+ const point = newItem;
27
+ point.position = point.position.clone().applyMatrix4(matrix);
28
+ break;
29
+ case GraphicType.LINE:
30
+ case GraphicType.CURVELINE:
31
+ const line = newItem;
32
+ line.start = line.start.clone().applyMatrix4(matrix);
33
+ line.end = line.end.clone().applyMatrix4(matrix);
34
+ break;
35
+ case GraphicType.PLANE:
36
+ const plane = newItem;
37
+ plane.normal = plane.normal.clone().applyMatrix3(matrix3);
38
+ plane.normal.normalize();
39
+ plane.pointOnPlane = plane.pointOnPlane.clone().applyMatrix4(matrix);
40
+ break;
41
+ case GraphicType.CYLINDER:
42
+ const cylinder = newItem;
43
+ cylinder.axis = cylinder.axis.clone().applyMatrix3(matrix3);
44
+ cylinder.axis.normalize();
45
+ cylinder.origin = cylinder.origin.clone().applyMatrix4(matrix);
46
+ break;
47
+ case GraphicType.CONE:
48
+ const cone = newItem;
49
+ cone.axis = cone.axis.clone().applyMatrix3(matrix3);
50
+ cone.axis.normalize();
51
+ cone.origin = cone.origin.clone().applyMatrix4(matrix);
52
+ break;
53
+ case GraphicType.ARC:
54
+ case GraphicType.CURVEARC:
55
+ const arc = newItem;
56
+ arc.normal = arc.normal.clone().applyMatrix3(matrix3);
57
+ arc.normal.normalize();
58
+ arc.center = arc.center.clone().applyMatrix4(matrix);
59
+ arc.start = arc.start.clone().applyMatrix4(matrix);
60
+ arc.end = arc.end.clone().applyMatrix4(matrix);
61
+ break;
62
+ case GraphicType.CIRCLE:
63
+ case GraphicType.CURVECIRCLE:
64
+ const circle = newItem;
65
+ circle.normal = circle.normal.clone().applyMatrix3(matrix3);
66
+ circle.normal.normalize();
67
+ circle.center = circle.center.clone().applyMatrix4(matrix);
68
+ circle.start = circle.start.clone().applyMatrix4(matrix);
69
+ circle.end = circle.end.clone().applyMatrix4(matrix);
70
+ break;
71
+ default:
72
+ break;
73
+ }
74
+ transformed.push(newItem);
75
+ }
76
+ return transformed;
77
+ },
78
+ getInfo(objects, drawingId) {
79
+ const transformed = Measure.transform(objects, drawingId);
80
+ const results = [];
81
+ const dir = (start, end) => end.clone().sub(start).normalize();
82
+ const getPointOnCircumference = (center, axis, radius) => {
83
+ const axisNormalized = axis.clone().normalize();
84
+ const helperVec = axisNormalized.x !== 1 ? new THREE.Vector3(1, 0, 0) : new THREE.Vector3(0, 1, 0);
85
+ const tangent = axisNormalized.clone().cross(helperVec).normalize();
86
+ return center.clone().addScaledVector(tangent, radius);
87
+ };
88
+ for (const element of transformed) {
89
+ switch (element.type) {
90
+ case GraphicType.POINT:
91
+ case GraphicType.CURVEPOINT:
92
+ const point = element;
93
+ results.push({
94
+ description: 'Position',
95
+ type: MeasureResultType.POSITION,
96
+ measureData: { value: point.position.clone() },
97
+ });
98
+ break;
99
+ case GraphicType.LINE:
100
+ case GraphicType.CURVELINE:
101
+ const line = element;
102
+ const absoluteDistance = line.start.distanceTo(line.end);
103
+ results.push({
104
+ description: 'Line length',
105
+ type: MeasureResultType.DISTANCE,
106
+ measureData: { value: absoluteDistance, start: line.start.clone(), end: line.end.clone() },
107
+ });
108
+ break;
109
+ case GraphicType.NURBSCURVE:
110
+ case GraphicType.CURVESPLINE:
111
+ const edge = element;
112
+ results.push({
113
+ description: 'Edge start',
114
+ type: MeasureResultType.POSITION,
115
+ measureData: { value: edge.start.clone() },
116
+ });
117
+ results.push({
118
+ description: 'Edge end',
119
+ type: MeasureResultType.POSITION,
120
+ measureData: { value: edge.end.clone() },
121
+ });
122
+ break;
123
+ case GraphicType.ARC:
124
+ case GraphicType.CURVEARC:
125
+ const arc = element;
126
+ const arcNEnd = arc.center.clone().addScaledVector(arc.normal.clone().normalize(), 40);
127
+ const pointOnArc = getPointOnCircumference(arc.center, arc.normal, arc.radius);
128
+ results.push({
129
+ description: 'Arc start',
130
+ type: MeasureResultType.POSITION,
131
+ measureData: { value: arc.start.clone() },
132
+ });
133
+ results.push({
134
+ description: 'Arc end',
135
+ type: MeasureResultType.POSITION,
136
+ measureData: { value: arc.end.clone() },
137
+ });
138
+ results.push({
139
+ description: 'Arc center',
140
+ type: MeasureResultType.POSITION,
141
+ measureData: { value: arc.center.clone() },
142
+ });
143
+ results.push({
144
+ description: 'Arc normal',
145
+ type: MeasureResultType.VECTOR,
146
+ measureData: { value: arc.normal.clone(), start: arc.center.clone(), end: arcNEnd },
147
+ });
148
+ results.push({
149
+ description: 'Arc radius',
150
+ type: MeasureResultType.DISTANCE,
151
+ measureData: { value: arc.radius, start: arc.center.clone(), end: pointOnArc },
152
+ });
153
+ break;
154
+ case GraphicType.CIRCLE:
155
+ case GraphicType.CURVECIRCLE:
156
+ const circle = element;
157
+ const circleNEnd = circle.center.clone().addScaledVector(circle.normal.clone().normalize(), 40);
158
+ const pointOnCircle = getPointOnCircumference(circle.center, circle.normal, circle.radius);
159
+ results.push({
160
+ description: 'Circle center',
161
+ type: MeasureResultType.POSITION,
162
+ measureData: { value: circle.center.clone() },
163
+ });
164
+ results.push({
165
+ description: 'Circle normal',
166
+ type: MeasureResultType.VECTOR,
167
+ measureData: { value: circle.normal.clone(), start: circle.center.clone(), end: circleNEnd },
168
+ });
169
+ results.push({
170
+ description: 'Circle radius',
171
+ type: MeasureResultType.DISTANCE,
172
+ measureData: { value: circle.radius, start: circle.center.clone(), end: pointOnCircle },
173
+ });
174
+ results.push({
175
+ description: 'Circle start/end',
176
+ type: MeasureResultType.POSITION,
177
+ measureData: { value: circle.start.clone() },
178
+ });
179
+ break;
180
+ case GraphicType.PLANE:
181
+ const plane = element;
182
+ const planeNEnd = plane.pointOnPlane.clone().addScaledVector(plane.normal.clone().normalize(), 40);
183
+ results.push({
184
+ description: 'Plane normal',
185
+ type: MeasureResultType.VECTOR,
186
+ measureData: { value: plane.normal, start: plane.pointOnPlane.clone(), end: planeNEnd },
187
+ });
188
+ break;
189
+ case GraphicType.CYLINDER:
190
+ const cylinder = element;
191
+ const cylAxisNormalized = cylinder.axis.clone().normalize();
192
+ const cylNEnd = cylinder.origin.clone().addScaledVector(cylAxisNormalized, cylinder.height + 40);
193
+ const pointOnCyl = getPointOnCircumference(cylinder.origin, cylinder.axis, cylinder.radius);
194
+ const cylHStart = cylinder.origin
195
+ .clone()
196
+ .addScaledVector(dir(cylinder.origin, pointOnCyl), cylinder.radius + 3);
197
+ const cylHEnd = cylHStart.clone().addScaledVector(cylAxisNormalized, cylinder.height);
198
+ results.push({
199
+ description: 'Cylinder origin',
200
+ type: MeasureResultType.POSITION,
201
+ measureData: { value: cylinder.origin.clone() },
202
+ });
203
+ results.push({
204
+ description: 'Cylinder axis',
205
+ type: MeasureResultType.VECTOR,
206
+ measureData: { value: cylinder.axis, start: cylinder.origin.clone(), end: cylNEnd },
207
+ });
208
+ results.push({
209
+ description: 'Cylinder radius',
210
+ type: MeasureResultType.DISTANCE,
211
+ measureData: { value: cylinder.radius, start: cylinder.origin.clone(), end: pointOnCyl },
212
+ });
213
+ results.push({
214
+ description: 'Cylinder height',
215
+ type: MeasureResultType.DISTANCE,
216
+ measureData: { value: cylinder.height, start: cylHStart, end: cylHEnd },
217
+ });
218
+ break;
219
+ case GraphicType.CONE:
220
+ const cone = element;
221
+ const isExpanding = cone.radiusBottom < cone.radiusTop;
222
+ const R = Math.max(cone.radiusBottom, cone.radiusTop);
223
+ const r = Math.min(cone.radiusBottom, cone.radiusTop);
224
+ const coneAxisNormalized = cone.axis.clone().normalize();
225
+ const coneCenterTop = cone.origin.clone().addScaledVector(coneAxisNormalized, cone.height);
226
+ const coneEnd = coneCenterTop.clone().addScaledVector(coneAxisNormalized, 40);
227
+ const coneCenterMinR = isExpanding ? cone.origin.clone() : coneCenterTop.clone();
228
+ const coneCenterMaxR = isExpanding ? coneCenterTop.clone() : cone.origin.clone();
229
+ const pointOnConeBottom = getPointOnCircumference(cone.origin, cone.axis, cone.radiusBottom);
230
+ const pointOnConeTop = getPointOnCircumference(coneCenterTop, cone.axis, cone.radiusTop);
231
+ const pointOnConeMinR = isExpanding ? pointOnConeBottom : pointOnConeTop;
232
+ const pointOnConeMaxR = isExpanding ? pointOnConeTop : pointOnConeBottom;
233
+ const coneHStart = coneCenterMaxR.clone().addScaledVector(dir(coneCenterMaxR, pointOnConeMaxR), R + 3);
234
+ const coneHEnd = coneHStart
235
+ .clone()
236
+ .addScaledVector(coneAxisNormalized, isExpanding ? -cone.height : cone.height);
237
+ results.push({
238
+ description: 'Cone origin',
239
+ type: MeasureResultType.POSITION,
240
+ measureData: { value: cone.origin.clone() },
241
+ });
242
+ results.push({
243
+ description: 'Cone axis',
244
+ type: MeasureResultType.VECTOR,
245
+ measureData: { value: cone.axis, start: cone.origin.clone(), end: coneEnd },
246
+ });
247
+ results.push({
248
+ description: 'Cone radius (min)',
249
+ type: MeasureResultType.DISTANCE,
250
+ measureData: { value: r, start: coneCenterMinR, end: pointOnConeMinR },
251
+ });
252
+ results.push({
253
+ description: 'Cone radius (max)',
254
+ type: MeasureResultType.DISTANCE,
255
+ measureData: { value: R, start: coneCenterMaxR, end: pointOnConeMaxR },
256
+ });
257
+ results.push({
258
+ description: 'Cone height',
259
+ type: MeasureResultType.DISTANCE,
260
+ measureData: { value: cone.height, start: coneHStart, end: coneHEnd },
261
+ });
262
+ break;
263
+ case GraphicType.SPHERE:
264
+ const sphere = element;
265
+ results.push({
266
+ description: 'Sphere radius',
267
+ type: MeasureResultType.VALUE,
268
+ measureData: { value: sphere.radius },
269
+ });
270
+ break;
271
+ case GraphicType.NURBSSURFACE:
272
+ results.push({ description: 'NURBS Surface', type: MeasureResultType.NONE });
273
+ break;
274
+ default:
275
+ results.push({ description: 'Undefined', type: MeasureResultType.NONE });
276
+ break;
277
+ }
278
+ }
279
+ return results;
280
+ },
281
+ calculateMeasures(objects, drawingId) {
282
+ const transformed = Measure.transform(objects, drawingId);
283
+ const first = transformed[0];
284
+ const second = transformed[1];
285
+ let results = [];
286
+ switch (first.type) {
287
+ case GraphicType.POINT:
288
+ case GraphicType.CURVEPOINT:
289
+ switch (second.type) {
290
+ case GraphicType.POINT:
291
+ case GraphicType.CURVEPOINT:
292
+ results = Measure.calculatePointPoint(first.position, second.position);
293
+ break;
294
+ case GraphicType.LINE:
295
+ case GraphicType.CURVELINE:
296
+ results = Measure.calculatePointLine(first, second);
297
+ break;
298
+ case GraphicType.PLANE:
299
+ results = Measure.calculatePointPlane(first, second);
300
+ break;
301
+ case GraphicType.ARC:
302
+ case GraphicType.CIRCLE:
303
+ results = Measure.calculatePointPoint(first.position.clone(), second.center.clone());
304
+ break;
305
+ default:
306
+ results = Measure.getInfo(objects, drawingId);
307
+ break;
308
+ }
309
+ break;
310
+ case GraphicType.LINE:
311
+ case GraphicType.CURVELINE:
312
+ switch (second.type) {
313
+ case GraphicType.POINT:
314
+ case GraphicType.CURVEPOINT:
315
+ results = Measure.calculatePointLine(second, first);
316
+ break;
317
+ case GraphicType.LINE:
318
+ case GraphicType.CURVELINE:
319
+ results = Measure.calculateLineLine(first, second);
320
+ break;
321
+ case GraphicType.PLANE:
322
+ results = Measure.calculateLinePlane(first, second);
323
+ break;
324
+ default:
325
+ results = Measure.getInfo(objects, drawingId);
326
+ break;
327
+ }
328
+ break;
329
+ case GraphicType.CIRCLE:
330
+ case GraphicType.ARC:
331
+ switch (second.type) {
332
+ case GraphicType.ARC:
333
+ case GraphicType.CIRCLE:
334
+ results = Measure.calculatePointPoint(first.center, second.center);
335
+ break;
336
+ case GraphicType.POINT:
337
+ results = Measure.calculatePointPoint(first.center, second.position);
338
+ break;
339
+ default:
340
+ results = Measure.getInfo(objects, drawingId);
341
+ break;
342
+ }
343
+ break;
344
+ case GraphicType.PLANE:
345
+ switch (second.type) {
346
+ case GraphicType.POINT:
347
+ case GraphicType.CURVEPOINT:
348
+ results = Measure.calculatePointPlane(second, first);
349
+ break;
350
+ case GraphicType.LINE:
351
+ case GraphicType.CURVELINE:
352
+ results = Measure.calculateLinePlane(second, first);
353
+ break;
354
+ case GraphicType.ARC:
355
+ case GraphicType.CURVEARC:
356
+ results = Measure.calculateArcPlane(second, first);
357
+ break;
358
+ case GraphicType.PLANE:
359
+ results = Measure.calculatePlanePlane(first, second);
360
+ break;
361
+ default:
362
+ results = Measure.getInfo(objects, drawingId);
363
+ break;
364
+ }
365
+ break;
366
+ default:
367
+ results = Measure.getInfo(objects, drawingId);
368
+ break;
369
+ }
370
+ return results;
371
+ },
372
+ calculatePointPoint(point1, point2) {
373
+ const p1 = point1.clone();
374
+ const p2 = point2.clone();
375
+ const distance = p1.distanceTo(p2);
376
+ const dx = Math.abs(p1.x - p2.x);
377
+ const dy = Math.abs(p1.y - p2.y);
378
+ const dz = Math.abs(p1.z - p2.z);
379
+ const px = p1.clone().setX(p2.x);
380
+ const pxy = px.clone().setY(p2.y);
381
+ return [
382
+ {
383
+ description: 'Distance',
384
+ type: MeasureResultType.DISTANCE,
385
+ measureData: { value: distance, start: p1, end: p2 },
386
+ },
387
+ { description: 'dx', type: MeasureResultType.DISTANCE, measureData: { value: dx, start: p1, end: px } },
388
+ { description: 'dy', type: MeasureResultType.DISTANCE, measureData: { value: dy, start: px, end: pxy } },
389
+ { description: 'dz', type: MeasureResultType.DISTANCE, measureData: { value: dz, start: pxy, end: p2 } },
390
+ ];
391
+ },
392
+ calculatePointLine(point, line) {
393
+ const [start, end] = Measure.getPointLineMPoints(point.position, line.start, line.end);
394
+ const distance = start.distanceTo(end);
395
+ return [
396
+ {
397
+ description: 'Distance',
398
+ type: MeasureResultType.DISTANCE,
399
+ measureData: { value: distance, start, end },
400
+ },
401
+ ];
402
+ },
403
+ calculatePointPlane(point, plane) {
404
+ const [start, end] = Measure.getPointPlaneMPoints(point.position, plane.pointOnPlane, plane.normal);
405
+ const distance = start.distanceTo(end);
406
+ return [
407
+ {
408
+ description: 'Distance',
409
+ type: MeasureResultType.DISTANCE,
410
+ measureData: { value: distance, start, end },
411
+ },
412
+ ];
413
+ },
414
+ calculateLineLine(first, second) {
415
+ const [start, end] = Measure.getLineLineMPoints(first.start, first.end, second.start, second.end);
416
+ const distance = start && end ? start.distanceTo(end) : NaN;
417
+ const [dir1, dir2] = Measure.getLineLineMDirs(first.start, first.end, second.start, second.end);
418
+ const angle = Measure.degrees(dir1.angleTo(dir2));
419
+ const angleS = 180 - angle;
420
+ return [
421
+ {
422
+ description: 'Distance',
423
+ type: MeasureResultType.DISTANCE,
424
+ measureData: start && end ? { value: distance, start, end } : undefined,
425
+ },
426
+ {
427
+ description: 'Angle1',
428
+ type: MeasureResultType.ANGLE,
429
+ measureData: { value: angle, center: first.start, dir1, dir2 },
430
+ },
431
+ {
432
+ description: 'Angle2',
433
+ type: MeasureResultType.ANGLE,
434
+ measureData: { value: angleS, center: first.start, dir1, dir2: dir2.clone().negate() },
435
+ },
436
+ ];
437
+ },
438
+ calculateLinePlane(line, plane) {
439
+ const [start, end] = Measure.getLinePlaneMPoints(line.start, line.end, plane.pointOnPlane, plane.normal);
440
+ const distance = start && end ? start.distanceTo(end) : NaN;
441
+ return [
442
+ {
443
+ description: 'Distance',
444
+ type: MeasureResultType.DISTANCE,
445
+ measureData: start && end ? { value: distance, start, end } : undefined,
446
+ },
447
+ ];
448
+ },
449
+ calculateArcPlane(arc, plane) {
450
+ const [start, end] = Measure.getArcPlaneMPoints(arc.center, arc.normal, plane.pointOnPlane, plane.normal);
451
+ const distance = start && end ? start.distanceTo(end) : NaN;
452
+ return [
453
+ {
454
+ description: 'Distance',
455
+ type: MeasureResultType.DISTANCE,
456
+ measureData: start && end ? { value: distance, start, end } : undefined,
457
+ },
458
+ ];
459
+ },
460
+ calculatePlanePlane(plane1, plane2) {
461
+ const [start, end] = Measure.getPlanePlaneMPoints(plane1.pointOnPlane, plane1.normal, plane2.pointOnPlane, plane2.normal);
462
+ const distance = start && end ? start.distanceTo(end) : NaN;
463
+ const [dir1, dir2] = Measure.getPlanePlaneMDirs(plane1.normal, plane2.normal);
464
+ const angle = dir1 && dir2 ? Measure.degrees(dir1.angleTo(dir2)) : Measure.degrees(plane1.normal.angleTo(plane2.normal));
465
+ return [
466
+ {
467
+ description: 'Distance',
468
+ type: MeasureResultType.DISTANCE,
469
+ measureData: start && end ? { value: distance, start, end } : undefined,
470
+ },
471
+ {
472
+ description: 'Angle',
473
+ type: MeasureResultType.ANGLE,
474
+ measureData: dir1 && dir2 ? { value: angle, center: plane1.pointOnPlane.clone(), dir1, dir2 } : undefined,
475
+ },
476
+ ];
477
+ },
478
+ degrees(radians) {
479
+ return (radians / Math.PI) * 180;
480
+ },
481
+ areParallel(vec1, vec2) {
482
+ const val = Math.abs(vec1.clone().cross(vec2).length());
483
+ return val < tolerance;
484
+ },
485
+ arePerpendicular(vec1, vec2) {
486
+ const val = Math.abs(vec1.clone().normalize().dot(vec2.clone().normalize()));
487
+ return val < tolerance;
488
+ },
489
+ getPointLineMPoints(pointsPos, startPos, endPos) {
490
+ const p1 = pointsPos.clone();
491
+ const lineDir = endPos.clone().sub(startPos);
492
+ const p2 = pointsPos.clone().sub(startPos).projectOnVector(lineDir).add(startPos);
493
+ return [p1, p2];
494
+ },
495
+ getPointPlaneMPoints(pointPos, pointOnPlane, normal) {
496
+ const p1 = pointPos.clone();
497
+ const offsetV = pointPos.clone().sub(pointOnPlane).projectOnVector(normal);
498
+ const p2 = pointPos.clone().sub(offsetV);
499
+ return [p1, p2];
500
+ },
501
+ getLineLineMPoints(start1Pos, end1Pos, start2Pos, end2Pos) {
502
+ const vec1 = start1Pos.clone().sub(end1Pos);
503
+ const vec2 = start2Pos.clone().sub(end2Pos);
504
+ if (Measure.areParallel(vec1, vec2)) {
505
+ // lines are parallel
506
+ const vecAB = start1Pos.clone().sub(start2Pos);
507
+ if (!Measure.areParallel(vecAB, vec1)) {
508
+ // lines are not conincident
509
+ return Measure.getPointLineMPoints(start1Pos, start2Pos, end2Pos);
510
+ }
511
+ }
512
+ return [undefined, undefined];
513
+ },
514
+ getLinePlaneMPoints(startPos, endPos, pointOnPlane, normal) {
515
+ const vec = startPos.clone().sub(endPos);
516
+ if (Measure.arePerpendicular(vec, normal)) {
517
+ // line is perpendicular to plane normal
518
+ return Measure.getPointPlaneMPoints(startPos, pointOnPlane, normal);
519
+ }
520
+ return [undefined, undefined];
521
+ },
522
+ getArcPlaneMPoints(arcCenter, arcNormal, pointOnPlane, planeNormal) {
523
+ if (Measure.areParallel(arcNormal, planeNormal)) {
524
+ // parallel
525
+ return Measure.getPointPlaneMPoints(arcCenter, pointOnPlane, planeNormal);
526
+ }
527
+ return [undefined, undefined];
528
+ },
529
+ getPlanePlaneMPoints(pointOnPlane1, normal1, pointOnPlane2, normal2) {
530
+ if (Measure.areParallel(normal1, normal2)) {
531
+ // parallel
532
+ return Measure.getPointPlaneMPoints(pointOnPlane1, pointOnPlane2, normal2);
533
+ }
534
+ return [undefined, undefined];
535
+ },
536
+ getLineLineMDirs(start1Pos, end1Pos, start2Pos, end2Pos) {
537
+ const vec1 = end1Pos.clone().sub(start1Pos);
538
+ const vec2 = end2Pos.clone().sub(start2Pos);
539
+ return [vec1, vec2];
540
+ },
541
+ getPlanePlaneMDirs(normal1, normal2) {
542
+ if (!Measure.areParallel(normal1, normal2)) {
543
+ const tangent1 = normal1.clone().projectOnPlane(normal2).normalize();
544
+ const tangent2 = normal2.clone().projectOnPlane(normal1).normalize();
545
+ const angle = tangent1.angleTo(tangent2);
546
+ return angle <= Math.PI / 2 ? [tangent1, tangent2] : [tangent1, tangent2.negate()];
547
+ }
548
+ return [undefined, undefined];
549
+ },
550
+ };