Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to skip transformation calculation when slice normal of the viewport api is in negative x direction #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/VTKViewport/vtkInteractorStyleMPRSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import vtkMouseRangeManipulator from 'vtk.js/Sources/Interaction/Manipulators/Mo
import vtkCoordinate from 'vtk.js/Sources/Rendering/Core/Coordinate';
import Constants from 'vtk.js/Sources/Rendering/Core/InteractorStyle/Constants';
import ViewportData from './ViewportData';
import { helpers } from '../helpers/index';
import EVENTS from '../events';

const { States } = Constants;
const { isAntiParallel } = helpers;

// ----------------------------------------------------------------------------
// Global methods
Expand Down Expand Up @@ -362,13 +364,19 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
const sliceNormal = publicAPI.getSliceNormal();

// Get rotation matrix from normal to +X (since bounds is aligned to XYZ)
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

// Special handling if sliceNormal is [-1,0,0] as rotateFromDirections returns zero matrix.
const fp = camera.getFocalPoint();
transform.apply(fp);
if (isAntiParallel(sliceNormal, [1, 0, 0])) {
fp[0] = fp[0] * -1;
} else {
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

transform.apply(fp);
}

return fp[0];
};

Expand Down Expand Up @@ -534,12 +542,17 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
const points = boundsToCorners(bounds);

// Get rotation matrix from normal to +X (since bounds is aligned to XYZ)
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

points.forEach(pt => transform.apply(pt));
// Special handling if sliceNormal is [-1,0,0] as rotateFromDirections returns zero matrix.
if (isAntiParallel(sliceNormal, [1, 0, 0])) {
points.forEach(pt => (pt[0] = pt[0] * -1));
} else {
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

points.forEach(pt => transform.apply(pt));
}

// range is now maximum X distance
let minX = Infinity;
Expand Down
20 changes: 15 additions & 5 deletions src/VTKViewport/vtkSVGCrosshairsWidget.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import macro from 'vtk.js/Sources/macro';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import vtkCoordinate from 'vtk.js/Sources/Rendering/Core/Coordinate';
import { helpers } from '../helpers/index';

const { isAntiParallel } = helpers;

let instanceId = 1;

Expand Down Expand Up @@ -158,13 +161,20 @@ function vtkSVGCrosshairsWidget(publicAPI, model) {

const istyle = renderWindow.getInteractor().getInteractorStyle();
const sliceNormal = istyle.getSliceNormal();
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

const mutatedWorldPos = worldPos.slice();
transform.apply(mutatedWorldPos);

// Special handling if sliceNormal is [-1,0,0] as rotateFromDirections returns zero matrix.
if (isAntiParallel(sliceNormal, [1, 0, 0])) {
mutatedWorldPos[0] = mutatedWorldPos[0] * -1;
} else {
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);
transform.apply(mutatedWorldPos);
}

const slice = mutatedWorldPos[0];

istyle.setSlice(slice);
Expand Down
20 changes: 15 additions & 5 deletions src/VTKViewport/vtkSVGRotatableCrosshairsWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import vtkCoordinate from 'vtk.js/Sources/Rendering/Core/Coordinate';
import liangBarksyClip from '../helpers/liangBarksyClip';
import { vec2, vec3 } from 'gl-matrix';
import { projectVector2D } from 'vtk.js/Sources/Common/Core/Math';
import { helpers } from '../helpers/index';

const { isAntiParallel } = helpers;

let instanceId = 1;

Expand Down Expand Up @@ -441,13 +444,20 @@ function vtkSVGRotatableCrosshairsWidget(publicAPI, model) {

const istyle = renderWindow.getInteractor().getInteractorStyle();
const sliceNormal = istyle.getSliceNormal();
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);

const mutatedWorldPos = worldPos.slice();
transform.apply(mutatedWorldPos);

// Special handling if sliceNormal is [-1,0,0] as rotateFromDirections returns zero matrix.
if (isAntiParallel(sliceNormal, [1, 0, 0])) {
mutatedWorldPos[0] = mutatedWorldPos[0] * -1;
} else {
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.rotateFromDirections(sliceNormal, [1, 0, 0]);
transform.apply(mutatedWorldPos);
}

const slice = mutatedWorldPos[0];

istyle.setSlice(slice);
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import formatDA from './formatDA';
import formatTM from './formatTM';
import formatNumberPrecision from './formatNumberPrecision';
import isValidNumber from './isValidNumber';
import isAntiParallel from './isAntiParallel';
import uuidv4 from './uuidv4.js';

const helpers = {
Expand All @@ -11,6 +12,7 @@ const helpers = {
formatTM,
formatNumberPrecision,
isValidNumber,
isAntiParallel,
};

export { helpers, uuidv4 };
7 changes: 7 additions & 0 deletions src/helpers/isAntiParallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vec3 } from 'gl-matrix';

export default function isAntiParallel(originalVector, referenceVector) {
vec3.normalize(originalVector, originalVector);
vec3.normalize(referenceVector, referenceVector);
return vec3.dot(originalVector, referenceVector) === -1;
}