Skip to content

Commit

Permalink
fix(Plane and Box): avoid slowing down evaluateFunction
Browse files Browse the repository at this point in the history
evaluateFunction can be called in an interactive fashion, requiring it to
stay fast. However, spreading the input then creating a empty array and
concatenating the spread inputs is very slow.
  • Loading branch information
Alexis Girault committed Mar 14, 2018
1 parent 2853cf9 commit 16e6c71
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
11 changes: 2 additions & 9 deletions Sources/Common/DataModel/Box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,8 @@ function vtkBox(publicAPI, model) {

publicAPI.getBounds = () => model.bbox.getBounds();

publicAPI.evaluateFunction = (...x) => {
let point = [].concat(x);
if (Array.isArray(x[0])) {
point = x[0];
}

if (point.length !== 3) {
return Number.MAX_VALUE;
}
publicAPI.evaluateFunction = (x, y, z) => {
const point = Array.isArray(x) ? x : [x, y, z];

let diff;
let dist;
Expand Down
21 changes: 12 additions & 9 deletions Sources/Common/DataModel/Box/test/testBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ test('Test vtkBox evaluateFunction', (t) => {
const box = vtkBox.newInstance();
box.setBounds(bounds);

const point1 = [0.0, 0.0, 0.0];
const evaluation1 = box.evaluateFunction(point1);
t.equal(evaluation1, -50);
let point = [0.0, 0.0, 0.0];
let res = box.evaluateFunction(point);
t.equal(res, -50);

const point2 = [100.0, 0.0, 0.0];
const evaluation2 = box.evaluateFunction(point2);
t.equal(evaluation2, 50);
point = [100.0, 0.0, 0.0];
res = box.evaluateFunction(point);
t.equal(res, 50);

const point3 = [50.0, 0.0, 0.0];
const evaluation3 = box.evaluateFunction(point3);
t.equal(evaluation3, 0);
point = [50.0, 0.0, 0.0];
res = box.evaluateFunction(point);
t.equal(res, 0);

res = box.evaluateFunction(...point);
t.equal(res, 0);

t.end();
});
22 changes: 10 additions & 12 deletions Sources/Common/DataModel/Plane/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,18 @@ export function vtkPlane(publicAPI, model) {
generalizedProjectPoint(x, model.origin, model.normal, xproj);
};

publicAPI.evaluateFunction = (...x) => {
let point = [].concat(x);
if (Array.isArray(x[0])) {
point = x[0];
publicAPI.evaluateFunction = (x, y, z) => {
if (!Array.isArray(x)) {
return (
model.normal[0] * (x - model.origin[0]) +
model.normal[1] * (y - model.origin[1]) +
model.normal[2] * (z - model.origin[2])
);
}

if (point.length !== 3) {
return Number.MAX_VALUE;
}

return (
model.normal[0] * (point[0] - model.origin[0]) +
model.normal[1] * (point[1] - model.origin[1]) +
model.normal[2] * (point[2] - model.origin[2])
model.normal[0] * (x[0] - model.origin[0]) +
model.normal[1] * (x[1] - model.origin[1]) +
model.normal[2] * (x[2] - model.origin[2])
);
};

Expand Down
9 changes: 5 additions & 4 deletions Sources/Common/DataModel/Plane/test/testPlane.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ test('Test vtkPlane evaluateFunction', (t) => {
plane.setOrigin(0.0, 0.0, 0.0);
plane.setNormal(1.0, 1.0, 1.0);

let res = plane.evaluateFunction([1.0, 1.0, 1.0]);
const point = [1.0, 1.0, 1.0];
let res = plane.evaluateFunction(point);
t.equal(res, 3);

res = plane.evaluateFunction(1.0, 1.0, 1.0);
res = plane.evaluateFunction(...point);
t.equal(res, 3);

res = plane.evaluateFunction(1.0, 1.0, 1.0, 0.0);
t.equal(res, Number.MAX_VALUE);
res = plane.evaluateFunction(...point, 1.0); // ignore last value
t.equal(res, 3);

t.end();
});

0 comments on commit 16e6c71

Please sign in to comment.