Skip to content

Commit 9f9e467

Browse files
committed
refactor(imagedata): simplify bounds usage
1 parent 4f37aed commit 9f9e467

File tree

3 files changed

+50
-60
lines changed

3 files changed

+50
-60
lines changed

Sources/Common/DataModel/BoundingBox/api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ Note that the bounding box may have 0 volume if its bounds
2323
were just initialized.
2424
Returns the updated bounds
2525

26+
### addPoints(bounds[6], points)
27+
28+
Change bounding box so it includes the points.
29+
points can be a list of lists (e.g. [[0,0,0], [1, 0, 0], ...]) or
30+
a flat list (e.g. [0, 0, 0, 1, 0, 0, ...])
31+
Note that the bounding box may have 0 volume if its bounds
32+
were just initialized.
33+
2634
### addBounds(bounds[6], xMin, xMax, yMin, yMax, zMin, zMax)
2735

2836
Change the bounding box so it includes bounds (defined by vtk standard)

Sources/Common/DataModel/BoundingBox/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ export function addPoint(bounds, ...xyz) {
5656
return bounds;
5757
}
5858

59+
export function addPoints(bounds, points) {
60+
if (points.length === 0) {
61+
return bounds;
62+
}
63+
if (Array.isArray(points[0])) {
64+
for (let i = 0; i < points.length; ++i) {
65+
addPoint(bounds, points[i]);
66+
}
67+
} else {
68+
for (let i = 0; i < points.length; i += 3) {
69+
addPoint(bounds, points.slice(i, i + 3));
70+
}
71+
}
72+
return bounds;
73+
}
74+
5975
export function addBounds(bounds, xMin, xMax, yMin, yMax, zMin, zMax) {
6076
const [_xMin, _xMax, _yMin, _yMax, _zMin, _zMax] = bounds;
6177
if (zMax === undefined) {
@@ -624,6 +640,10 @@ class BoundingBox {
624640
return addPoint(this.bounds, xyz);
625641
}
626642

643+
addPoints(points) {
644+
return addPoints(this.bounds, points);
645+
}
646+
627647
addBounds(xMin, xMax, yMin, yMax, zMin, zMax) {
628648
return addBounds(this.bounds, xMin, xMax, yMin, yMax, zMin, zMax);
629649
}
@@ -744,6 +764,7 @@ export const STATIC = {
744764
setBounds,
745765
reset,
746766
addPoint,
767+
addPoints,
747768
addBounds,
748769
setMinPoint,
749770
setMaxPoint,

Sources/Common/DataModel/ImageData/index.js

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ function vtkImageData(publicAPI, model) {
2828
return false;
2929
}
3030

31-
let changeDetected = false;
32-
model.extent.forEach((item, index) => {
33-
if (item !== extentArray[index]) {
34-
if (changeDetected) {
35-
return;
36-
}
37-
changeDetected = true;
38-
}
39-
});
31+
const changeDetected = model.extent.some(
32+
(item, index) => item !== extentArray[index]
33+
);
4034

4135
if (changeDetected) {
4236
model.extent = extentArray.slice();
@@ -195,51 +189,27 @@ function vtkImageData(publicAPI, model) {
195189
publicAPI.extentToBounds = (ex) => {
196190
// prettier-ignore
197191
const corners = [
198-
ex[0], ex[2], ex[4],
199-
ex[1], ex[2], ex[4],
200-
ex[0], ex[3], ex[4],
201-
ex[1], ex[3], ex[4],
202-
ex[0], ex[2], ex[5],
203-
ex[1], ex[2], ex[5],
204-
ex[0], ex[3], ex[5],
205-
ex[1], ex[3], ex[5]];
206-
207-
const idx = new Float64Array([corners[0], corners[1], corners[2]]);
208-
const vout = new Float64Array(3);
209-
publicAPI.indexToWorld(idx, vout);
210-
const bounds = [vout[0], vout[0], vout[1], vout[1], vout[2], vout[2]];
211-
for (let i = 3; i < 24; i += 3) {
212-
vec3.set(idx, corners[i], corners[i + 1], corners[i + 2]);
213-
publicAPI.indexToWorld(idx, vout);
214-
if (vout[0] < bounds[0]) {
215-
bounds[0] = vout[0];
216-
}
217-
if (vout[1] < bounds[2]) {
218-
bounds[2] = vout[1];
219-
}
220-
if (vout[2] < bounds[4]) {
221-
bounds[4] = vout[2];
222-
}
223-
if (vout[0] > bounds[1]) {
224-
bounds[1] = vout[0];
225-
}
226-
if (vout[1] > bounds[3]) {
227-
bounds[3] = vout[1];
228-
}
229-
if (vout[2] > bounds[5]) {
230-
bounds[5] = vout[2];
231-
}
192+
[ex[0], ex[2], ex[4]],
193+
[ex[1], ex[2], ex[4]],
194+
[ex[0], ex[3], ex[4]],
195+
[ex[1], ex[3], ex[4]],
196+
[ex[0], ex[2], ex[5]],
197+
[ex[1], ex[2], ex[5]],
198+
[ex[0], ex[3], ex[5]],
199+
[ex[1], ex[3], ex[5]]
200+
];
201+
202+
const bounds = [...vtkBoundingBox.INIT_BOUNDS];
203+
const vout = [];
204+
for (let i = 0; i < 8; ++i) {
205+
publicAPI.indexToWorld(corners[i], vout);
206+
vtkBoundingBox.addPoint(bounds, ...vout);
232207
}
233-
234208
return bounds;
235209
};
236210

237-
publicAPI.getSpatialExtent = () => {
238-
const boundingBox = vtkBoundingBox.newInstance();
239-
boundingBox.setBounds(model.extent);
240-
boundingBox.inflate(0.5);
241-
return boundingBox.getBounds();
242-
};
211+
publicAPI.getSpatialExtent = () =>
212+
vtkBoundingBox.inflate([...model.extent], 0.5);
243213

244214
// Internal, shouldn't need to call this manually.
245215
publicAPI.computeTransforms = () => {
@@ -350,16 +320,7 @@ function vtkImageData(publicAPI, model) {
350320
publicAPI.onModified(publicAPI.computeTransforms);
351321
publicAPI.computeTransforms();
352322

353-
publicAPI.getCenter = () => {
354-
const bounds = publicAPI.getBounds();
355-
const center = [];
356-
357-
for (let i = 0; i < 3; i++) {
358-
center[i] = (bounds[2 * i + 1] + bounds[2 * i]) / 2;
359-
}
360-
361-
return center;
362-
};
323+
publicAPI.getCenter = () => vtkBoundingBox.getCenter(publicAPI.getBounds());
363324

364325
publicAPI.computeHistogram = (worldBounds, voxelFunc = null) => {
365326
const bounds = [0, 0, 0, 0, 0, 0];

0 commit comments

Comments
 (0)