From 16e6c71cc0106d367cc9ffe53f566f5fd3225c5d Mon Sep 17 00:00:00 2001 From: Alexis Girault Date: Wed, 14 Mar 2018 14:45:02 -0400 Subject: [PATCH] fix(Plane and Box): avoid slowing down evaluateFunction 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. --- Sources/Common/DataModel/Box/index.js | 11 ++-------- Sources/Common/DataModel/Box/test/testBox.js | 21 ++++++++++-------- Sources/Common/DataModel/Plane/index.js | 22 +++++++++---------- .../Common/DataModel/Plane/test/testPlane.js | 9 ++++---- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/Sources/Common/DataModel/Box/index.js b/Sources/Common/DataModel/Box/index.js index dbb6e88e658..a74331ad0dc 100644 --- a/Sources/Common/DataModel/Box/index.js +++ b/Sources/Common/DataModel/Box/index.js @@ -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; diff --git a/Sources/Common/DataModel/Box/test/testBox.js b/Sources/Common/DataModel/Box/test/testBox.js index 87db1d87d57..194c99ca28e 100644 --- a/Sources/Common/DataModel/Box/test/testBox.js +++ b/Sources/Common/DataModel/Box/test/testBox.js @@ -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(); }); diff --git a/Sources/Common/DataModel/Plane/index.js b/Sources/Common/DataModel/Plane/index.js index b848aa544af..1b8e0fed23b 100644 --- a/Sources/Common/DataModel/Plane/index.js +++ b/Sources/Common/DataModel/Plane/index.js @@ -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]) ); }; diff --git a/Sources/Common/DataModel/Plane/test/testPlane.js b/Sources/Common/DataModel/Plane/test/testPlane.js index 8e11dd659d4..355c7162be1 100644 --- a/Sources/Common/DataModel/Plane/test/testPlane.js +++ b/Sources/Common/DataModel/Plane/test/testPlane.js @@ -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(); });