three-cad-viewer 2.2.4 → 2.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-cad-viewer",
3
- "version": "2.2.4",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
package/src/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = "2.2.4";
1
+ export const version = "2.3.0";
package/src/bbox.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import * as THREE from "three";
2
2
 
3
3
  class BoundingBox extends THREE.Box3 {
4
- constructor() {
5
- super();
6
- }
7
4
 
8
5
  expandByObject(object, precise = false) {
9
6
  object.updateWorldMatrix(false, false);
@@ -5,6 +5,7 @@ import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";
5
5
  import { VertexNormalsHelper } from "three/examples/jsm/helpers/VertexNormalsHelper.js";
6
6
  import { BoundingBox } from "./bbox.js";
7
7
  import { ObjectGroup } from "./objectgroup.js";
8
+ import { flatten } from "./utils.js";
8
9
 
9
10
  class NestedGroup {
10
11
  constructor(
@@ -55,7 +56,7 @@ class NestedGroup {
55
56
  var positions =
56
57
  edgeList instanceof Float32Array
57
58
  ? edgeList
58
- : new Float32Array(edgeList.flat(2));
59
+ : new Float32Array(flatten(edgeList, 2));
59
60
 
60
61
  const lineGeometry = new LineSegmentsGeometry();
61
62
  lineGeometry.setPositions(positions);
@@ -103,7 +104,7 @@ class NestedGroup {
103
104
  var edges = this._renderEdges(
104
105
  edgeList.edges
105
106
  ? edgeList.edges // protocol version 2
106
- : edgeList, // protocol version 1
107
+ : flatten(edgeList), // protocol version 1
107
108
  lineWidth,
108
109
  color,
109
110
  state,
@@ -142,8 +143,8 @@ class NestedGroup {
142
143
  // protocol version 1
143
144
  positions =
144
145
  vertexList instanceof Float32Array
145
- ? vertexList.flat()
146
- : new Float32Array(vertexList.flat());
146
+ ? vertexList
147
+ : new Float32Array(flatten(vertexList));
147
148
  }
148
149
  const geometry = new THREE.BufferGeometry();
149
150
  geometry.setAttribute(
@@ -190,15 +191,15 @@ class NestedGroup {
190
191
  const positions =
191
192
  shape.vertices instanceof Float32Array
192
193
  ? shape.vertices
193
- : new Float32Array(shape.vertices.flat());
194
+ : new Float32Array(flatten(shape.vertices));
194
195
  const normals =
195
196
  shape.normals instanceof Float32Array
196
197
  ? shape.normals
197
- : new Float32Array(shape.normals.flat());
198
+ : new Float32Array(flatten(shape.normals));
198
199
  const triangles =
199
200
  shape.triangles instanceof Uint32Array
200
201
  ? shape.triangles
201
- : new Uint32Array(shape.triangles.flat());
202
+ : new Uint32Array(flatten(shape.triangles));
202
203
 
203
204
  var group = new ObjectGroup(
204
205
  this.defaultOpacity,
@@ -273,7 +274,7 @@ class NestedGroup {
273
274
  side: THREE.FrontSide,
274
275
  visible: states[0] == 1,
275
276
  map: texture,
276
- name:"frontMaterial"
277
+ name: "frontMaterial"
277
278
  });
278
279
  }
279
280
 
package/src/timer.js CHANGED
@@ -4,7 +4,7 @@ class Timer {
4
4
  this.timeit = timeit;
5
5
  this.start = performance.now();
6
6
  if (timeit) {
7
- console.info(`three-cad-viewer: Timer ${prefix}:start`);
7
+ console.info(`three-cad-viewer: ${prefix}:timer start`);
8
8
  }
9
9
  }
10
10
 
@@ -12,7 +12,7 @@ class Timer {
12
12
  if (this.timeit) {
13
13
  const t = performance.now();
14
14
  console.info(
15
- `three-cad-viewer: Timer ${this.prefix}:${msg} ${(
15
+ `three-cad-viewer: ${this.prefix}:${msg}:timer split ${(
16
16
  t - this.start
17
17
  ).toFixed(1)} ms`,
18
18
  );
@@ -23,9 +23,9 @@ class Timer {
23
23
  if (this.timeit) {
24
24
  const t = performance.now();
25
25
  console.info(
26
- `three-cad-viewer: Timer ${this.prefix}:stop ${(t - this.start).toFixed(
26
+ `three-cad-viewer: ${this.prefix}:timer stop ${(t - this.start).toFixed(
27
27
  1,
28
- )} ms`,
28
+ )} ms:`,
29
29
  );
30
30
  }
31
31
  }
package/src/utils.js CHANGED
@@ -11,6 +11,9 @@ function clone(obj) {
11
11
  return obj;
12
12
  }
13
13
  }
14
+ function flatten(arr, depth = 1) {
15
+ return (Array.isArray(arr)) ? arr.flat(depth) : arr;
16
+ }
14
17
 
15
18
  function isEqual(obj1, obj2, tol = 1e-9) {
16
19
  if (Array.isArray(obj1) && Array.isArray(obj2)) {
@@ -96,4 +99,4 @@ function scaleLight(intensity) {
96
99
 
97
100
  const KeyMapper = new _KeyMapper();
98
101
 
99
- export { clone, isEqual, sceneTraverse, prettyPrintVector, KeyMapper, scaleLight };
102
+ export { clone, flatten, isEqual, sceneTraverse, prettyPrintVector, KeyMapper, scaleLight };
package/src/viewer.js CHANGED
@@ -316,6 +316,12 @@ class Viewer {
316
316
  this.roughness,
317
317
  this.normalLen,
318
318
  );
319
+ if (shapes.bb) {
320
+ this.bbox = new BoundingBox(
321
+ new THREE.Vector3(shapes.bb.xmin, shapes.bb.ymin, shapes.bb.zmin),
322
+ new THREE.Vector3(shapes.bb.xmax, shapes.bb.ymax, shapes.bb.zmax)
323
+ );
324
+ }
319
325
  nestedGroup.render(states);
320
326
  return nestedGroup;
321
327
  }
@@ -374,10 +380,30 @@ class Viewer {
374
380
  name: "faces",
375
381
  id: `${part.id}/faces`,
376
382
  };
383
+ var triangles;
377
384
  const vertices = shape.vertices;
378
385
  const normals = shape.normals;
379
- for (j = 0; j < shape.triangles.length; j++) {
380
- var triangles = shape.triangles[j];
386
+ const num = (shape.triangles_per_face) ? shape.triangles_per_face.length : shape.triangles.length;
387
+ var current = 0;
388
+ for (j = 0; j < num; j++) {
389
+ if (shape.triangles_per_face) {
390
+ triangles = shape.triangles.subarray(current, current + 3 * shape.triangles_per_face[j]);
391
+ current += 3 * shape.triangles_per_face[j];
392
+ } else {
393
+ triangles = shape.triangles[j];
394
+ }
395
+
396
+ var vecs = new Float32Array(triangles.length * 3);
397
+ var norms = new Float32Array(triangles.length * 3);
398
+ for (var i = 0; i < triangles.length; i++) {
399
+ var s = triangles[i];
400
+ vecs[3 * i] = vertices[3 * s];
401
+ vecs[3 * i + 1] = vertices[3 * s + 1];
402
+ vecs[3 * i + 2] = vertices[3 * s + 2];
403
+ norms[3 * i] = normals[3 * s];
404
+ norms[3 * i + 1] = normals[3 * s + 1];
405
+ norms[3 * i + 2] = normals[3 * s + 2];
406
+ }
381
407
  var new_shape = {
382
408
  loc: [
383
409
  [0, 0, 0],
@@ -395,20 +421,8 @@ class Viewer {
395
421
  subtype: part.subtype,
396
422
  shape: {
397
423
  triangles: [...Array(triangles.length).keys()],
398
- vertices: triangles
399
- .map((s) => [
400
- vertices[3 * s],
401
- vertices[3 * s + 1],
402
- vertices[3 * s + 2],
403
- ])
404
- .flat(),
405
- normals: triangles
406
- .map((s) => [
407
- normals[3 * s],
408
- normals[3 * s + 1],
409
- normals[3 * s + 2],
410
- ])
411
- .flat(),
424
+ vertices: vecs,
425
+ normals: norms,
412
426
  edges: [],
413
427
  },
414
428
  };
@@ -433,8 +447,17 @@ class Viewer {
433
447
  const multiColor =
434
448
  Array.isArray(part.color) && part.color.length == shape.edges.length;
435
449
  var color;
436
- for (j = 0; j < shape.edges.length; j++) {
437
- const edge = shape.edges[j];
450
+
451
+ const num = (shape.segments_per_edge) ? shape.segments_per_edge.length : shape.triangles.length;
452
+ current = 0;
453
+ var edge;
454
+ for (j = 0; j < num; j++) {
455
+ if (shape.segments_per_edge) {
456
+ edge = shape.edges.subarray(current, current + 6 * shape.segments_per_edge[j]);
457
+ current += 6 * shape.segments_per_edge[j];
458
+ } else {
459
+ edge = shape.edges[j];
460
+ }
438
461
  color = multiColor ? part.color[j] : part.color;
439
462
  new_shape = {
440
463
  loc: [
@@ -517,8 +540,8 @@ class Viewer {
517
540
  renderTessellatedShapes(shapes, states, options) {
518
541
  this.setRenderDefaults(options);
519
542
  const _render = (shapes, states, measureTools) => {
520
- var part, shape;
521
- if (shapes.version == 2) {
543
+ var part;
544
+ if (shapes.version == 2 || shapes.version == 3) {
522
545
  if (measureTools) {
523
546
  var i, tmp;
524
547
  let parts = [];
@@ -532,17 +555,6 @@ class Viewer {
532
555
  }
533
556
  }
534
557
  shapes.parts = parts;
535
- } else {
536
- for (i = 0; i < shapes.parts.length; i++) {
537
- part = shapes.parts[i];
538
- shape = part.shape;
539
- if (part.type == "shapes") {
540
- shape.triangles = shape.triangles.flat();
541
- shape.edges = shape.edges.flat();
542
- } else if (part.type == "edges" || part.type == "shapes") {
543
- shape.edges = shape.edges.flat();
544
- }
545
- }
546
558
  }
547
559
  }
548
560
  return shapes;
@@ -871,7 +883,9 @@ class Viewer {
871
883
 
872
884
  timer.split("rendered nested group");
873
885
 
874
- this.bbox = this.nestedGroup.boundingBox();
886
+ if (!this.bbox) {
887
+ this.bbox = this.nestedGroup.boundingBox();
888
+ }
875
889
  const center = new THREE.Vector3();
876
890
  this.bbox.getCenter(center);
877
891
  this.bb_max = this.bbox.max_dist_from_center();
@@ -1124,12 +1138,12 @@ class Viewer {
1124
1138
  this.tools,
1125
1139
  this.glass,
1126
1140
  );
1127
-
1141
+ timer.split("ui updated");
1128
1142
  this.display.autoCollapse();
1129
1143
 
1130
1144
  // ensure all for all deselected objects the stencil planes are invisible
1131
1145
  this.setObjects(this.states, true, true);
1132
-
1146
+ timer.split("stencil done");
1133
1147
  //
1134
1148
  // show the rendering
1135
1149
  //
@@ -1144,7 +1158,7 @@ class Viewer {
1144
1158
  //
1145
1159
  // notify calculated results
1146
1160
  //
1147
-
1161
+ timer.split("show done");
1148
1162
  if (this.notifyCallback) {
1149
1163
  this.notifyCallback({
1150
1164
  tab: { old: null, new: this.display.activeTab },
@@ -1155,8 +1169,10 @@ class Viewer {
1155
1169
  clip_normal_2: { old: null, new: this.clipNormal2 },
1156
1170
  });
1157
1171
  }
1158
- this.update(true, false);
1172
+ timer.split("notification done");
1159
1173
 
1174
+ this.update(true, false);
1175
+ timer.split("update done");
1160
1176
  timer.stop();
1161
1177
  }
1162
1178