@buerli.io/core 0.10.0 → 0.12.0

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 (81) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +12 -12
  3. package/build/cjs/geometry/ThreeGeometryCreator.js +320 -320
  4. package/build/cjs/geometry/index.js +17 -17
  5. package/build/cjs/graphic/filters.js +25 -25
  6. package/build/cjs/index.js +25 -25
  7. package/build/cjs/store/index.js +37 -37
  8. package/build/cjs/store/states/buerli.js +98 -98
  9. package/build/cjs/store/states/cad.js +47 -35
  10. package/build/cjs/store/states/drawing.js +49 -49
  11. package/build/cjs/store/states/geometry.js +137 -137
  12. package/build/cjs/store/states/graphic.js +112 -112
  13. package/build/cjs/store/states/interaction.js +55 -55
  14. package/build/cjs/store/states/message.js +25 -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/IGraphicPackage.js +46 -46
  22. package/build/cjs/types/IStructurePatch.js +2 -2
  23. package/build/cjs/types/IStructureTree.js +28 -28
  24. package/build/cjs/types/InteractionInfo.js +11 -11
  25. package/build/cjs/types/index.js +20 -20
  26. package/build/cjs/utils/Math.js +91 -91
  27. package/build/cjs/utils/Measure.js +576 -576
  28. package/build/cjs/utils/Timing.js +34 -34
  29. package/build/esm/geometry/ThreeGeometryCreator.js +291 -291
  30. package/build/esm/geometry/index.js +1 -1
  31. package/build/esm/graphic/filters.js +12 -12
  32. package/build/esm/index.js +9 -9
  33. package/build/esm/store/index.js +26 -26
  34. package/build/esm/store/states/buerli.js +92 -92
  35. package/build/esm/store/states/cad.js +42 -30
  36. package/build/esm/store/states/drawing.js +45 -45
  37. package/build/esm/store/states/geometry.js +109 -109
  38. package/build/esm/store/states/graphic.js +107 -107
  39. package/build/esm/store/states/interaction.js +50 -50
  40. package/build/esm/store/states/message.js +21 -21
  41. package/build/esm/store/states/plugin.js +109 -109
  42. package/build/esm/store/states/selection.js +261 -261
  43. package/build/esm/store/states/structure.js +219 -219
  44. package/build/esm/store/store.js +3 -3
  45. package/build/esm/store/subscribtion/selection.js +44 -44
  46. package/build/esm/store/subscribtion/subscribeToDrawing.js +9 -9
  47. package/build/esm/types/IGraphicPackage.js +43 -43
  48. package/build/esm/types/IStructurePatch.js +1 -1
  49. package/build/esm/types/IStructureTree.js +24 -24
  50. package/build/esm/types/InteractionInfo.js +6 -6
  51. package/build/esm/types/index.js +4 -4
  52. package/build/esm/utils/Math.js +65 -65
  53. package/build/esm/utils/Measure.js +550 -550
  54. package/build/esm/utils/Timing.js +31 -31
  55. package/build/types/geometry/ThreeGeometryCreator.d.ts +256 -256
  56. package/build/types/geometry/index.d.ts +1 -1
  57. package/build/types/graphic/filters.d.ts +11 -11
  58. package/build/types/index.d.ts +37 -37
  59. package/build/types/store/index.d.ts +16 -16
  60. package/build/types/store/states/buerli.d.ts +41 -41
  61. package/build/types/store/states/cad.d.ts +53 -42
  62. package/build/types/store/states/drawing.d.ts +45 -45
  63. package/build/types/store/states/geometry.d.ts +63 -63
  64. package/build/types/store/states/graphic.d.ts +22 -22
  65. package/build/types/store/states/interaction.d.ts +34 -34
  66. package/build/types/store/states/message.d.ts +30 -30
  67. package/build/types/store/states/plugin.d.ts +53 -53
  68. package/build/types/store/states/selection.d.ts +62 -62
  69. package/build/types/store/states/structure.d.ts +47 -47
  70. package/build/types/store/store.d.ts +2 -2
  71. package/build/types/store/subscribtion/selection.d.ts +7 -7
  72. package/build/types/store/subscribtion/subscribeToDrawing.d.ts +4 -4
  73. package/build/types/types/IGraphicPackage.d.ts +157 -155
  74. package/build/types/types/IStructurePatch.d.ts +6 -6
  75. package/build/types/types/IStructureTree.d.ts +80 -80
  76. package/build/types/types/InteractionInfo.d.ts +43 -43
  77. package/build/types/types/index.d.ts +4 -4
  78. package/build/types/utils/Math.d.ts +21 -21
  79. package/build/types/utils/Measure.d.ts +64 -64
  80. package/build/types/utils/Timing.d.ts +8 -8
  81. package/package.json +44 -44
@@ -1,320 +1,320 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.isEqualGeometry = exports.EMPTY_CONTAINER_GEOMETRY = exports.ThreeGeometryCreator = void 0;
27
- const THREE = __importStar(require("three"));
28
- const types_1 = require("./../types");
29
- const convertType = (contType) => {
30
- let type = types_1.GraphicType.NONE;
31
- switch (contType) {
32
- case types_1.ContainerType.BREP:
33
- type = types_1.GraphicType.BREP;
34
- break;
35
- case types_1.ContainerType.CURVEARRAY:
36
- type = types_1.GraphicType.CURVEARRAY;
37
- break;
38
- case types_1.ContainerType.POLYBREP:
39
- type = types_1.GraphicType.POLYBREP;
40
- break;
41
- }
42
- return type;
43
- };
44
- /**
45
- * Creates THREE geometry based on the data within a @see IGraphicContainer.
46
- */
47
- class ThreeGeometryCreator {
48
- constructor() {
49
- this.coordHeightTolerance = 0.01;
50
- }
51
- getMetadata(geometry, container, color, opacity = 0) {
52
- var _a;
53
- const geomProps = geometry ? geometry.properties : {};
54
- if (geomProps && geomProps.material) {
55
- color = geomProps.material.color || color;
56
- opacity = geomProps.material.opacity || opacity;
57
- }
58
- // Provide values as a CSS color string, which will be gamma corrected by three.js
59
- const rgbColor = color ? new THREE.Color(`rgb(${color[0]}, ${color[1]}, ${color[2]})`) : undefined;
60
- const layer = geomProps && geomProps.layer ? geomProps.layer : container.properties.layer;
61
- return {
62
- color: rgbColor,
63
- opacity,
64
- layer,
65
- container: { id: container.id, ownerId: container.owner, type: convertType(container.type) },
66
- operationId: (_a = geometry === null || geometry === void 0 ? void 0 : geometry.properties) === null || _a === void 0 ? void 0 : _a.operationId,
67
- rawGraphic: geometry,
68
- };
69
- }
70
- createGeometry(container, color) {
71
- var _a, _b, _c, _d;
72
- const { id: graphicId, properties, type: contType } = container;
73
- const type = convertType(contType);
74
- const box = { min: new THREE.Vector3(...properties.min), max: new THREE.Vector3(...properties.max) };
75
- const meshes = [];
76
- const lines = [];
77
- const edges = [];
78
- const arcs = [];
79
- const pointMap = {};
80
- const map = {};
81
- if (container.meshes) {
82
- container.meshes.forEach(mesh => {
83
- const elem = this.createSurface(mesh, container, color);
84
- if (elem.isValid) {
85
- meshes.push(elem);
86
- map[mesh.id] = elem;
87
- }
88
- else {
89
- // TODO: The server should not send invalid meshes.
90
- console.warn(`Invalid mesh detected`);
91
- }
92
- });
93
- }
94
- if (container.lines) {
95
- container.lines.forEach(line => {
96
- const elem = this.createLine(line, container, pointMap, color);
97
- lines.push(elem);
98
- map[line.id] = elem;
99
- });
100
- }
101
- if (container.edges) {
102
- container.edges.forEach(edge => {
103
- const elem = this.createEdge(edge, container, pointMap, color);
104
- edges.push(elem);
105
- map[edge.id] = elem;
106
- });
107
- }
108
- if (container.arcs) {
109
- container.arcs.forEach(arc => {
110
- const elem = this.createArc(arc, container, pointMap, color);
111
- arcs.push(elem);
112
- map[arc.id] = elem;
113
- });
114
- }
115
- const points = Object.values(pointMap);
116
- return Object.assign({ graphicId,
117
- type,
118
- map,
119
- meshes,
120
- lines,
121
- edges,
122
- arcs,
123
- points,
124
- box }, this.getMetadata(null, container, (_b = (_a = container.properties) === null || _a === void 0 ? void 0 : _a.material) === null || _b === void 0 ? void 0 : _b.color, (_d = (_c = container.properties) === null || _c === void 0 ? void 0 : _c.material) === null || _d === void 0 ? void 0 : _d.opacity));
125
- }
126
- createSurface(mesh, container, color) {
127
- let meshGeom = this.createMesh(mesh, container, color);
128
- switch (meshGeom.type) {
129
- case types_1.GraphicType.PLANE:
130
- meshGeom = this.createPlane(mesh, meshGeom);
131
- break;
132
- case types_1.GraphicType.CYLINDER:
133
- meshGeom = this.createCylinder(mesh, meshGeom);
134
- break;
135
- case types_1.GraphicType.CONE:
136
- meshGeom = this.createCone(mesh, meshGeom);
137
- break;
138
- case types_1.GraphicType.SPHERE:
139
- meshGeom = this.createSphere(mesh, meshGeom);
140
- break;
141
- }
142
- return meshGeom;
143
- }
144
- createMesh(mesh, container, color) {
145
- const graphicId = mesh.id;
146
- const loops = mesh.loops;
147
- const type = mesh.properties.surface.type;
148
- const geometry = new THREE.BufferGeometry();
149
- geometry.setIndex(mesh.indices);
150
- geometry.setAttribute('position', new THREE.Float32BufferAttribute(mesh.vertices, 3));
151
- mesh.normals && geometry.setAttribute('normal', new THREE.Float32BufferAttribute(mesh.normals, 3));
152
- return Object.assign({
153
- // TODO: The server should not send invalid meshes.
154
- isValid: mesh.vertices && mesh.vertices.length > 0, geometry,
155
- graphicId,
156
- type,
157
- loops }, this.getMetadata(mesh, container, color));
158
- }
159
- createPlane(mesh, meshGeom) {
160
- const type = types_1.GraphicType.PLANE;
161
- const { normal: n, pointOnPlane: p } = mesh.properties.surface;
162
- const pointOnPlane = new THREE.Vector3(p[0], p[1], p[2]);
163
- const normal = new THREE.Vector3(n[0], n[1], n[2]).normalize();
164
- return Object.assign(Object.assign({}, meshGeom), { type, pointOnPlane, normal });
165
- }
166
- createCylinder(mesh, meshGeom) {
167
- const type = types_1.GraphicType.CYLINDER;
168
- const { origin: o, axis: a, height, radius } = mesh.properties.surface;
169
- const origin = new THREE.Vector3(o[0], o[1], o[2]);
170
- const axis = new THREE.Vector3(a[0], a[1], a[2]).normalize();
171
- return Object.assign(Object.assign({}, meshGeom), { type, origin, axis, height, radius });
172
- }
173
- createCone(mesh, meshGeom) {
174
- const type = types_1.GraphicType.CONE;
175
- const { origin: o, axis: a, height, radiusBottom, radiusTop } = mesh.properties.surface;
176
- const origin = new THREE.Vector3(o[0], o[1], o[2]);
177
- const axis = new THREE.Vector3(a[0], a[1], a[2]).normalize();
178
- return Object.assign(Object.assign({}, meshGeom), { type, origin, axis, height, radiusBottom, radiusTop });
179
- }
180
- createSphere(mesh, meshGeom) {
181
- const type = types_1.GraphicType.SPHERE;
182
- const { radius } = mesh.properties.surface;
183
- return Object.assign(Object.assign({}, meshGeom), { type, radius });
184
- }
185
- createLine(line, container, points, color) {
186
- const graphicId = line.id;
187
- let type = types_1.GraphicType.LINE;
188
- if (container.type === types_1.ContainerType.CURVEARRAY) {
189
- type = types_1.GraphicType.CURVELINE;
190
- }
191
- const pts = line.points;
192
- const [startId, endId] = line.pointIds;
193
- const start = new THREE.Vector3(pts[0], pts[1], pts[2]);
194
- const end = new THREE.Vector3(pts[3], pts[4], pts[5]);
195
- this.insertPoint(container, `${startId}|point`, startId, start.clone(), line.id, 'start', points);
196
- this.insertPoint(container, `${endId}|point`, endId, end.clone(), line.id, 'end', points);
197
- const geometry = new THREE.BufferGeometry().setFromPoints([start.clone(), end.clone()]);
198
- return Object.assign(Object.assign({ geometry,
199
- graphicId, type: type }, this.getMetadata(line, container, color)), { start: start.clone(), end: end.clone() });
200
- }
201
- createEdge(edge, container, points, color) {
202
- const threePts = [];
203
- const graphicId = edge.id;
204
- let type = types_1.GraphicType.NURBSCURVE;
205
- if (container.type === types_1.ContainerType.CURVEARRAY) {
206
- type = types_1.GraphicType.CURVESPLINE;
207
- }
208
- const pts = edge.points;
209
- for (let i = 0, length = pts.length / 3; i < length; i++) {
210
- threePts.push(new THREE.Vector3(pts[i * 3], pts[i * 3 + 1], pts[i * 3 + 2]));
211
- // Each segment has to be defined with start and end to use it for BufferGeometry
212
- if (i > 0) {
213
- threePts.push(new THREE.Vector3(pts[i * 3], pts[i * 3 + 1], pts[i * 3 + 2]));
214
- }
215
- }
216
- // Seems like the three BufferGeometryUtils expect an edge to have an even number of points.
217
- // Just add the end point twice if this is not the case.
218
- if (threePts.length % 2 !== 0) {
219
- threePts.push(threePts[threePts.length - 1].clone());
220
- }
221
- const start = threePts[0].clone();
222
- const end = threePts[threePts.length - 1].clone();
223
- const startId = edge.pointIds[0];
224
- const endId = edge.pointIds[edge.pointIds.length - 1];
225
- this.insertPoint(container, `${startId}|point`, startId, start, edge.id, 'start', points);
226
- this.insertPoint(container, `${endId}|point`, endId, end, edge.id, 'end', points);
227
- const geometry = new THREE.BufferGeometry().setFromPoints(threePts);
228
- return Object.assign(Object.assign({ geometry,
229
- graphicId, type: type }, this.getMetadata(edge, container, color)), { start: start.clone(), end: end.clone() });
230
- }
231
- createArc(arc, container, points, color) {
232
- const graphicId = arc.id;
233
- let type = arc.isCircle ? types_1.GraphicType.CIRCLE : types_1.GraphicType.ARC;
234
- if (container.type === types_1.ContainerType.CURVEARRAY) {
235
- type = arc.isCircle ? types_1.GraphicType.CURVECIRCLE : types_1.GraphicType.CURVEARC;
236
- }
237
- // take half of coordHeightTolerance used for faces, as classcad does it in the tessellation process
238
- const coordHeight = this.coordHeightTolerance / 2;
239
- const radius = arc.radius;
240
- const center = new THREE.Vector3(arc.center[0], arc.center[1], arc.center[2]);
241
- const normal = new THREE.Vector3(arc.zAxis[0], arc.zAxis[1], arc.zAxis[2]).normalize();
242
- const startVector = new THREE.Vector3(arc.xAxis[0], arc.xAxis[1], arc.xAxis[2]).normalize().multiplyScalar(radius);
243
- const theta = arc.isCircle ? Math.PI * 2 : arc.angle;
244
- // calculate angle for one segment to fulfill coordHeight tolerance
245
- let alpha = 2 * Math.acos(1 - coordHeight / radius);
246
- // calculate number of segments
247
- let segments = Math.ceil(Math.abs(arc.angle) / alpha);
248
- // choose at minimum three segments
249
- segments = Math.max(3, segments);
250
- // adjust angle to match with number of segments, still fulfilling coordHeigth tolerance
251
- alpha = theta / segments;
252
- const threePts = [];
253
- const matrix = new THREE.Matrix4().makeRotationAxis(normal, alpha);
254
- const startPoint = center.clone();
255
- startPoint.add(startVector);
256
- threePts.push(startPoint);
257
- for (let i = 0; i < segments; i++) {
258
- startVector.applyMatrix4(matrix);
259
- const nextPoint = center.clone();
260
- nextPoint.add(startVector);
261
- threePts.push(nextPoint.clone());
262
- // Each segment has to be defined with start and end to use it for BufferGeometry
263
- threePts.push(nextPoint.clone());
264
- }
265
- // Seems like the three BufferGeometryUtils expect an edge to have an even number of points.
266
- // Just add the end point twice if this is not the case.
267
- if (threePts.length % 2 !== 0) {
268
- threePts.push(threePts[threePts.length - 1].clone());
269
- }
270
- const [startId, endId] = arc.pointIds;
271
- const start = threePts[0].clone();
272
- const end = threePts[threePts.length - 1].clone();
273
- this.insertPoint(container, `${startId}|point`, startId, start, arc.id, 'start', points);
274
- this.insertPoint(container, `${endId}|point`, endId, end, arc.id, 'end', points);
275
- const geometry = new THREE.BufferGeometry().setFromPoints(threePts);
276
- return Object.assign(Object.assign({ geometry,
277
- graphicId, type: type }, this.getMetadata(arc, container, color)), { normal: normal.clone(), center: center.clone(), start: start.clone(), end: end.clone(), radius: arc.radius, angle: arc.angle });
278
- }
279
- createPoint(container, key, graphicId, position, ownerId, type) {
280
- return Object.assign(Object.assign({ key,
281
- graphicId, type: (container.type === types_1.ContainerType.CURVEARRAY ? types_1.GraphicType.CURVEPOINT : types_1.GraphicType.POINT) }, this.getMetadata(null, container)), { position: position.clone(), geometry: position.clone(), owners: [{ ownerId, type }] });
282
- }
283
- insertPoint(container, key, graphicId, position, ownerId, type, points) {
284
- const existingPoint = points[key];
285
- if (existingPoint) {
286
- const filteredOwners = existingPoint.owners.filter(gr => gr.ownerId === ownerId && gr.type === type);
287
- if (filteredOwners.length <= 0) {
288
- existingPoint.owners.push({ ownerId, type });
289
- }
290
- }
291
- else {
292
- points[key] = this.createPoint(container, key, graphicId, position, ownerId, type);
293
- }
294
- }
295
- }
296
- exports.ThreeGeometryCreator = ThreeGeometryCreator;
297
- const EMPTY_CONTAINER_GEOMETRY = () => {
298
- return {
299
- graphicId: Number.NaN,
300
- type: types_1.GraphicType.NONE,
301
- map: {},
302
- meshes: [],
303
- lines: [],
304
- edges: [],
305
- arcs: [],
306
- points: [],
307
- box: { min: new THREE.Vector3(), max: new THREE.Vector3() },
308
- container: { id: Number.NaN, ownerId: Number.NaN, type: types_1.GraphicType.NONE },
309
- color: new THREE.Color(),
310
- opacity: 1.0,
311
- layer: null,
312
- operationId: Number.NaN,
313
- rawGraphic: null,
314
- };
315
- };
316
- exports.EMPTY_CONTAINER_GEOMETRY = EMPTY_CONTAINER_GEOMETRY;
317
- const isEqualGeometry = (a, b) => {
318
- return a.graphicId === b.graphicId && a.type === b.type;
319
- };
320
- exports.isEqualGeometry = isEqualGeometry;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.isEqualGeometry = exports.EMPTY_CONTAINER_GEOMETRY = exports.ThreeGeometryCreator = void 0;
27
+ const THREE = __importStar(require("three"));
28
+ const types_1 = require("./../types");
29
+ const convertType = (contType) => {
30
+ let type = types_1.GraphicType.NONE;
31
+ switch (contType) {
32
+ case types_1.ContainerType.BREP:
33
+ type = types_1.GraphicType.BREP;
34
+ break;
35
+ case types_1.ContainerType.CURVEARRAY:
36
+ type = types_1.GraphicType.CURVEARRAY;
37
+ break;
38
+ case types_1.ContainerType.POLYBREP:
39
+ type = types_1.GraphicType.POLYBREP;
40
+ break;
41
+ }
42
+ return type;
43
+ };
44
+ /**
45
+ * Creates THREE geometry based on the data within a @see IGraphicContainer.
46
+ */
47
+ class ThreeGeometryCreator {
48
+ constructor() {
49
+ this.coordHeightTolerance = 0.01;
50
+ }
51
+ getMetadata(geometry, container, color, opacity = 0) {
52
+ var _a;
53
+ const geomProps = geometry ? geometry.properties : {};
54
+ if (geomProps && geomProps.material) {
55
+ color = geomProps.material.color || color;
56
+ opacity = geomProps.material.opacity || opacity;
57
+ }
58
+ // Provide values as a CSS color string, which will be gamma corrected by three.js
59
+ const rgbColor = color ? new THREE.Color(`rgb(${color[0]}, ${color[1]}, ${color[2]})`) : undefined;
60
+ const layer = geomProps && geomProps.layer ? geomProps.layer : container.properties.layer;
61
+ return {
62
+ color: rgbColor,
63
+ opacity,
64
+ layer,
65
+ container: { id: container.id, ownerId: container.owner, type: convertType(container.type) },
66
+ operationId: (_a = geometry === null || geometry === void 0 ? void 0 : geometry.properties) === null || _a === void 0 ? void 0 : _a.operationId,
67
+ rawGraphic: geometry,
68
+ };
69
+ }
70
+ createGeometry(container, color) {
71
+ var _a, _b, _c, _d;
72
+ const { id: graphicId, properties, type: contType } = container;
73
+ const type = convertType(contType);
74
+ const box = { min: new THREE.Vector3(...properties.min), max: new THREE.Vector3(...properties.max) };
75
+ const meshes = [];
76
+ const lines = [];
77
+ const edges = [];
78
+ const arcs = [];
79
+ const pointMap = {};
80
+ const map = {};
81
+ if (container.meshes) {
82
+ container.meshes.forEach(mesh => {
83
+ const elem = this.createSurface(mesh, container, color);
84
+ if (elem.isValid) {
85
+ meshes.push(elem);
86
+ map[mesh.id] = elem;
87
+ }
88
+ else {
89
+ // TODO: The server should not send invalid meshes.
90
+ console.warn(`Invalid mesh detected`);
91
+ }
92
+ });
93
+ }
94
+ if (container.lines) {
95
+ container.lines.forEach(line => {
96
+ const elem = this.createLine(line, container, pointMap, color);
97
+ lines.push(elem);
98
+ map[line.id] = elem;
99
+ });
100
+ }
101
+ if (container.edges) {
102
+ container.edges.forEach(edge => {
103
+ const elem = this.createEdge(edge, container, pointMap, color);
104
+ edges.push(elem);
105
+ map[edge.id] = elem;
106
+ });
107
+ }
108
+ if (container.arcs) {
109
+ container.arcs.forEach(arc => {
110
+ const elem = this.createArc(arc, container, pointMap, color);
111
+ arcs.push(elem);
112
+ map[arc.id] = elem;
113
+ });
114
+ }
115
+ const points = Object.values(pointMap);
116
+ return Object.assign({ graphicId,
117
+ type,
118
+ map,
119
+ meshes,
120
+ lines,
121
+ edges,
122
+ arcs,
123
+ points,
124
+ box }, this.getMetadata(null, container, (_b = (_a = container.properties) === null || _a === void 0 ? void 0 : _a.material) === null || _b === void 0 ? void 0 : _b.color, (_d = (_c = container.properties) === null || _c === void 0 ? void 0 : _c.material) === null || _d === void 0 ? void 0 : _d.opacity));
125
+ }
126
+ createSurface(mesh, container, color) {
127
+ let meshGeom = this.createMesh(mesh, container, color);
128
+ switch (meshGeom.type) {
129
+ case types_1.GraphicType.PLANE:
130
+ meshGeom = this.createPlane(mesh, meshGeom);
131
+ break;
132
+ case types_1.GraphicType.CYLINDER:
133
+ meshGeom = this.createCylinder(mesh, meshGeom);
134
+ break;
135
+ case types_1.GraphicType.CONE:
136
+ meshGeom = this.createCone(mesh, meshGeom);
137
+ break;
138
+ case types_1.GraphicType.SPHERE:
139
+ meshGeom = this.createSphere(mesh, meshGeom);
140
+ break;
141
+ }
142
+ return meshGeom;
143
+ }
144
+ createMesh(mesh, container, color) {
145
+ const graphicId = mesh.id;
146
+ const loops = mesh.loops;
147
+ const type = mesh.properties.surface.type;
148
+ const geometry = new THREE.BufferGeometry();
149
+ geometry.setIndex(mesh.indices);
150
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(mesh.vertices, 3));
151
+ mesh.normals && geometry.setAttribute('normal', new THREE.Float32BufferAttribute(mesh.normals, 3));
152
+ return Object.assign({
153
+ // TODO: The server should not send invalid meshes.
154
+ isValid: mesh.vertices && mesh.vertices.length > 0, geometry,
155
+ graphicId,
156
+ type,
157
+ loops }, this.getMetadata(mesh, container, color));
158
+ }
159
+ createPlane(mesh, meshGeom) {
160
+ const type = types_1.GraphicType.PLANE;
161
+ const { normal: n, pointOnPlane: p } = mesh.properties.surface;
162
+ const pointOnPlane = new THREE.Vector3(p[0], p[1], p[2]);
163
+ const normal = new THREE.Vector3(n[0], n[1], n[2]).normalize();
164
+ return Object.assign(Object.assign({}, meshGeom), { type, pointOnPlane, normal });
165
+ }
166
+ createCylinder(mesh, meshGeom) {
167
+ const type = types_1.GraphicType.CYLINDER;
168
+ const { origin: o, axis: a, height, radius } = mesh.properties.surface;
169
+ const origin = new THREE.Vector3(o[0], o[1], o[2]);
170
+ const axis = new THREE.Vector3(a[0], a[1], a[2]).normalize();
171
+ return Object.assign(Object.assign({}, meshGeom), { type, origin, axis, height, radius });
172
+ }
173
+ createCone(mesh, meshGeom) {
174
+ const type = types_1.GraphicType.CONE;
175
+ const { origin: o, axis: a, height, radiusBottom, radiusTop } = mesh.properties.surface;
176
+ const origin = new THREE.Vector3(o[0], o[1], o[2]);
177
+ const axis = new THREE.Vector3(a[0], a[1], a[2]).normalize();
178
+ return Object.assign(Object.assign({}, meshGeom), { type, origin, axis, height, radiusBottom, radiusTop });
179
+ }
180
+ createSphere(mesh, meshGeom) {
181
+ const type = types_1.GraphicType.SPHERE;
182
+ const { radius } = mesh.properties.surface;
183
+ return Object.assign(Object.assign({}, meshGeom), { type, radius });
184
+ }
185
+ createLine(line, container, points, color) {
186
+ const graphicId = line.id;
187
+ let type = types_1.GraphicType.LINE;
188
+ if (container.type === types_1.ContainerType.CURVEARRAY) {
189
+ type = types_1.GraphicType.CURVELINE;
190
+ }
191
+ const pts = line.points;
192
+ const [startId, endId] = line.pointIds;
193
+ const start = new THREE.Vector3(pts[0], pts[1], pts[2]);
194
+ const end = new THREE.Vector3(pts[3], pts[4], pts[5]);
195
+ this.insertPoint(container, `${startId}|point`, startId, start.clone(), line.id, 'start', points);
196
+ this.insertPoint(container, `${endId}|point`, endId, end.clone(), line.id, 'end', points);
197
+ const geometry = new THREE.BufferGeometry().setFromPoints([start.clone(), end.clone()]);
198
+ return Object.assign(Object.assign({ geometry,
199
+ graphicId, type: type }, this.getMetadata(line, container, color)), { start: start.clone(), end: end.clone() });
200
+ }
201
+ createEdge(edge, container, points, color) {
202
+ const threePts = [];
203
+ const graphicId = edge.id;
204
+ let type = types_1.GraphicType.NURBSCURVE;
205
+ if (container.type === types_1.ContainerType.CURVEARRAY) {
206
+ type = types_1.GraphicType.CURVESPLINE;
207
+ }
208
+ const pts = edge.points;
209
+ for (let i = 0, length = pts.length / 3; i < length; i++) {
210
+ threePts.push(new THREE.Vector3(pts[i * 3], pts[i * 3 + 1], pts[i * 3 + 2]));
211
+ // Each segment has to be defined with start and end to use it for BufferGeometry
212
+ if (i > 0) {
213
+ threePts.push(new THREE.Vector3(pts[i * 3], pts[i * 3 + 1], pts[i * 3 + 2]));
214
+ }
215
+ }
216
+ // Seems like the three BufferGeometryUtils expect an edge to have an even number of points.
217
+ // Just add the end point twice if this is not the case.
218
+ if (threePts.length % 2 !== 0) {
219
+ threePts.push(threePts[threePts.length - 1].clone());
220
+ }
221
+ const start = threePts[0].clone();
222
+ const end = threePts[threePts.length - 1].clone();
223
+ const startId = edge.pointIds[0];
224
+ const endId = edge.pointIds[edge.pointIds.length - 1];
225
+ this.insertPoint(container, `${startId}|point`, startId, start, edge.id, 'start', points);
226
+ this.insertPoint(container, `${endId}|point`, endId, end, edge.id, 'end', points);
227
+ const geometry = new THREE.BufferGeometry().setFromPoints(threePts);
228
+ return Object.assign(Object.assign({ geometry,
229
+ graphicId, type: type }, this.getMetadata(edge, container, color)), { start: start.clone(), end: end.clone() });
230
+ }
231
+ createArc(arc, container, points, color) {
232
+ const graphicId = arc.id;
233
+ let type = arc.isCircle ? types_1.GraphicType.CIRCLE : types_1.GraphicType.ARC;
234
+ if (container.type === types_1.ContainerType.CURVEARRAY) {
235
+ type = arc.isCircle ? types_1.GraphicType.CURVECIRCLE : types_1.GraphicType.CURVEARC;
236
+ }
237
+ // take half of coordHeightTolerance used for faces, as classcad does it in the tessellation process
238
+ const coordHeight = this.coordHeightTolerance / 2;
239
+ const radius = arc.radius;
240
+ const center = new THREE.Vector3(arc.center[0], arc.center[1], arc.center[2]);
241
+ const normal = new THREE.Vector3(arc.zAxis[0], arc.zAxis[1], arc.zAxis[2]).normalize();
242
+ const startVector = new THREE.Vector3(arc.xAxis[0], arc.xAxis[1], arc.xAxis[2]).normalize().multiplyScalar(radius);
243
+ const theta = arc.isCircle ? Math.PI * 2 : arc.angle;
244
+ // calculate angle for one segment to fulfill coordHeight tolerance
245
+ let alpha = 2 * Math.acos(1 - coordHeight / radius);
246
+ // calculate number of segments
247
+ let segments = Math.ceil(Math.abs(arc.angle) / alpha);
248
+ // choose at minimum three segments
249
+ segments = Math.max(3, segments);
250
+ // adjust angle to match with number of segments, still fulfilling coordHeigth tolerance
251
+ alpha = theta / segments;
252
+ const threePts = [];
253
+ const matrix = new THREE.Matrix4().makeRotationAxis(normal, alpha);
254
+ const startPoint = center.clone();
255
+ startPoint.add(startVector);
256
+ threePts.push(startPoint);
257
+ for (let i = 0; i < segments; i++) {
258
+ startVector.applyMatrix4(matrix);
259
+ const nextPoint = center.clone();
260
+ nextPoint.add(startVector);
261
+ threePts.push(nextPoint.clone());
262
+ // Each segment has to be defined with start and end to use it for BufferGeometry
263
+ threePts.push(nextPoint.clone());
264
+ }
265
+ // Seems like the three BufferGeometryUtils expect an edge to have an even number of points.
266
+ // Just add the end point twice if this is not the case.
267
+ if (threePts.length % 2 !== 0) {
268
+ threePts.push(threePts[threePts.length - 1].clone());
269
+ }
270
+ const [startId, endId] = arc.pointIds;
271
+ const start = threePts[0].clone();
272
+ const end = threePts[threePts.length - 1].clone();
273
+ this.insertPoint(container, `${startId}|point`, startId, start, arc.id, 'start', points);
274
+ this.insertPoint(container, `${endId}|point`, endId, end, arc.id, 'end', points);
275
+ const geometry = new THREE.BufferGeometry().setFromPoints(threePts);
276
+ return Object.assign(Object.assign({ geometry,
277
+ graphicId, type: type }, this.getMetadata(arc, container, color)), { normal: normal.clone(), center: center.clone(), start: start.clone(), end: end.clone(), radius: arc.radius, angle: arc.angle });
278
+ }
279
+ createPoint(container, key, graphicId, position, ownerId, type) {
280
+ return Object.assign(Object.assign({ key,
281
+ graphicId, type: (container.type === types_1.ContainerType.CURVEARRAY ? types_1.GraphicType.CURVEPOINT : types_1.GraphicType.POINT) }, this.getMetadata(null, container)), { position: position.clone(), geometry: position.clone(), owners: [{ ownerId, type }] });
282
+ }
283
+ insertPoint(container, key, graphicId, position, ownerId, type, points) {
284
+ const existingPoint = points[key];
285
+ if (existingPoint) {
286
+ const filteredOwners = existingPoint.owners.filter(gr => gr.ownerId === ownerId && gr.type === type);
287
+ if (filteredOwners.length <= 0) {
288
+ existingPoint.owners.push({ ownerId, type });
289
+ }
290
+ }
291
+ else {
292
+ points[key] = this.createPoint(container, key, graphicId, position, ownerId, type);
293
+ }
294
+ }
295
+ }
296
+ exports.ThreeGeometryCreator = ThreeGeometryCreator;
297
+ const EMPTY_CONTAINER_GEOMETRY = () => {
298
+ return {
299
+ graphicId: Number.NaN,
300
+ type: types_1.GraphicType.NONE,
301
+ map: {},
302
+ meshes: [],
303
+ lines: [],
304
+ edges: [],
305
+ arcs: [],
306
+ points: [],
307
+ box: { min: new THREE.Vector3(), max: new THREE.Vector3() },
308
+ container: { id: Number.NaN, ownerId: Number.NaN, type: types_1.GraphicType.NONE },
309
+ color: new THREE.Color(),
310
+ opacity: 1.0,
311
+ layer: null,
312
+ operationId: Number.NaN,
313
+ rawGraphic: null,
314
+ };
315
+ };
316
+ exports.EMPTY_CONTAINER_GEOMETRY = EMPTY_CONTAINER_GEOMETRY;
317
+ const isEqualGeometry = (a, b) => {
318
+ return a.graphicId === b.graphicId && a.type === b.type;
319
+ };
320
+ exports.isEqualGeometry = isEqualGeometry;