Skip to content

Commit bea78f5

Browse files
committed
refactor(cell): cleanup vtkCell bounds usage
Expose a convenient vtkBoundingBox::getDiagonalLength2() function
1 parent e6f589b commit bea78f5

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

Sources/Common/DataModel/BoundingBox/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ Return the Max Length of the box
9393

9494
Return the length of the diagonal or null if not valid.
9595

96+
### getDiagonalLength2(bounds[6]) : Number
97+
98+
Return the squared length of the diagonal or null if not valid.
99+
96100
### inflate(bounds[6], delta)
97101

98102
Expand the Box by delta on each side, the box will grow by

Sources/Common/DataModel/BoundingBox/index.d.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mat4 } from 'gl-matrix';
22
import { Bounds, Vector2, Vector3 } from '../../../types';
33
import vtkPoints from '../../Core/Points';
4+
import { Nullable } from '../../../types';
45

56
/**
67
* Tests whether two bounds equal.
@@ -176,10 +177,16 @@ export function getZRange(bounds: Bounds): Vector2;
176177
export function getMaxLength(bounds: Bounds): number;
177178

178179
/**
179-
* Gets the diagonal of the bounding box.
180+
* Gets the diagonal length of the bounding box.
180181
* @param {Bounds} bounds
181182
*/
182-
export function getDiagonalLength(bounds: Bounds): number;
183+
export function getDiagonalLength(bounds: Bounds): Nullable<number>;
184+
185+
/**
186+
* Gets the squared diagonal length of the bounding box.
187+
* @param {Bounds} bounds
188+
*/
189+
export function getDiagonalLength2(bounds: Bounds): Nullable<number>;
183190

184191
/**
185192
* Gets the min point.
@@ -506,10 +513,16 @@ declare class BoundingBox {
506513
getMaxLength(bounds: Bounds): number;
507514

508515
/**
509-
* Gets the diagonal of the bounding box.
516+
* Gets the diagonal length of the bounding box.
517+
* @param {Bounds} bounds
518+
*/
519+
getDiagonalLength(bounds: Bounds): Nullable<number>;
520+
521+
/**
522+
* Gets the squared diagonal length of the bounding box.
510523
* @param {Bounds} bounds
511524
*/
512-
getDiagonalLength(bounds: Bounds): number;
525+
getDiagonalLength2(bounds: Bounds): Nullable<number>;
513526

514527
/**
515528
* Gets the min point.
@@ -674,6 +687,7 @@ declare const vtkBoundingBox: {
674687
getLengths: typeof getLengths;
675688
getMaxLength: typeof getMaxLength;
676689
getDiagonalLength: typeof getDiagonalLength;
690+
getDiagonalLength2: typeof getDiagonalLength2;
677691
getMinPoint: typeof getMinPoint;
678692
getMaxPoint: typeof getMaxPoint;
679693
getXRange: typeof getXRange;

Sources/Common/DataModel/BoundingBox/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,19 @@ export function getMaxLength(bounds) {
259259
return l[2];
260260
}
261261

262-
export function getDiagonalLength(bounds) {
262+
export function getDiagonalLength2(bounds) {
263263
if (isValid(bounds)) {
264264
const l = getLengths(bounds);
265-
return Math.sqrt(l[0] * l[0] + l[1] * l[1] + l[2] * l[2]);
265+
return l[0] * l[0] + l[1] * l[1] + l[2] * l[2];
266266
}
267267
return null;
268268
}
269269

270+
export function getDiagonalLength(bounds) {
271+
const lenght2 = getDiagonalLength2(bounds);
272+
return lenght2 !== null ? Math.sqrt(lenght2) : null;
273+
}
274+
270275
export function getMinPoint(bounds) {
271276
return [bounds[0], bounds[2], bounds[4]];
272277
}
@@ -880,6 +885,10 @@ class BoundingBox {
880885
return getDiagonalLength(this.bounds);
881886
}
882887

888+
getDiagonalLength2() {
889+
return getDiagonalLength2(this.bounds);
890+
}
891+
883892
getMinPoint() {
884893
return getMinPoint(this.bounds);
885894
}

Sources/Common/DataModel/Cell/index.js

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import macro from 'vtk.js/Sources/macros';
2-
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
2+
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
33
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
44

55
// ----------------------------------------------------------------------------
@@ -40,42 +40,15 @@ function vtkCell(publicAPI, model) {
4040
}
4141
};
4242

43-
publicAPI.getBounds = () => {
44-
const nbPoints = model.points.getNumberOfPoints();
45-
const x = [];
46-
if (nbPoints) {
47-
model.points.getPoint(0, x);
48-
model.bounds[0] = x[0];
49-
model.bounds[1] = x[0];
50-
model.bounds[2] = x[1];
51-
model.bounds[3] = x[1];
52-
model.bounds[4] = x[2];
53-
model.bounds[5] = x[2];
54-
55-
for (let i = 1; i < nbPoints; i++) {
56-
model.points.getPoint(i, x);
57-
model.bounds[0] = x[0] < model.bounds[0] ? x[0] : model.bounds[0];
58-
model.bounds[1] = x[0] > model.bounds[1] ? x[0] : model.bounds[1];
59-
model.bounds[2] = x[1] < model.bounds[2] ? x[1] : model.bounds[2];
60-
model.bounds[3] = x[1] > model.bounds[3] ? x[1] : model.bounds[3];
61-
model.bounds[4] = x[2] < model.bounds[4] ? x[2] : model.bounds[4];
62-
model.bounds[5] = x[2] > model.bounds[5] ? x[2] : model.bounds[5];
63-
}
64-
} else {
65-
vtkMath.uninitializeBounds(model.bounds);
66-
}
67-
return model.bounds;
68-
};
43+
publicAPI.getBounds = () => model.points.getBounds();
6944

7045
publicAPI.getLength2 = () => {
71-
publicAPI.getBounds();
72-
let length = 0.0;
73-
let diff = 0;
74-
for (let i = 0; i < 3; i++) {
75-
diff = model.bounds[2 * i + 1] - model.bounds[2 * i];
76-
length += diff * diff;
77-
}
78-
return length;
46+
const lengths = vtkBoundingBox.getLengths(publicAPI.getBounds());
47+
return (
48+
lengths[0] * lengths[0] +
49+
lengths[1] * lengths[1] +
50+
lengths[2] * lengths[2]
51+
);
7952
};
8053

8154
publicAPI.getParametricDistance = (pcoords) => {

0 commit comments

Comments
 (0)