Skip to content

Commit

Permalink
Merge pull request #485 from bruyeret/refactor-orientation
Browse files Browse the repository at this point in the history
Use the new getSparseOrthogonalMatrix instead of getLPSDirections
  • Loading branch information
floryst authored Oct 2, 2023
2 parents c6cad90 + d0ccd4f commit 1bfde11
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 73 deletions.
6 changes: 1 addition & 5 deletions src/components/core/VtkView/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@ export const DEFAULT_LPS_VIEW_TYPES = {
[VIEW_TYPE_VALUES.z]: 'Axial',
};

export const DEFAULT_VIEW_ORIENTATION = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
];
export const DEFAULT_VIEW_ORIENTATION = [1, 0, 0, 0, 1, 0, 0, 0, 1];
38 changes: 26 additions & 12 deletions src/store/views.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Vue from 'vue';
import vtkWidgetManager from '@kitware/vtk.js/Widgets/Core/WidgetManager';
import WidgetManagerConstants from '@kitware/vtk.js/Widgets/Core/WidgetManager/Constants';
import { getSparseOrthogonalMatrix } from '@kitware/vtk.js/Common/Core/Math';

import { mat3 } from 'gl-matrix';

import {
DEFAULT_VIEW_TYPE,
Expand All @@ -15,7 +18,6 @@ import { DEFAULT_BACKGROUND } from 'paraview-glance/src/components/core/VtkView/
import {
remapIdValues,
wrapMutationAsAction,
getLPSDirections,
updateViewOrientationFromBasisAndAxis,
} from 'paraview-glance/src/utils';

Expand All @@ -40,7 +42,7 @@ export default ({ proxyManager }) => ({
maxTextureLODSize: 50000, // Units are in KiB
viewOrder: Object.values(VIEW_TYPE_VALUES),
visibleCount: 1,
// a basis in column-major order (list of 3 vectors): number[3][3]
// a column-major matrix
viewOrientation: DEFAULT_VIEW_ORIENTATION,
// for each view type, the corresponding text to display { viewType: text }
viewTypes: DEFAULT_VIEW_TYPES,
Expand Down Expand Up @@ -225,20 +227,32 @@ export default ({ proxyManager }) => ({
const masterSource = proxyManager.getProxyById(state.masterSourceId);
if (masterSource?.getDataset().isA('vtkImageData')) {
// lps mode with a master volume
// directionMatrix is a column major index to world matrix
const directionMatrix = masterSource.getDataset().getDirection();
const lpsDirections = getLPSDirections(directionMatrix);
const axisToXYZ = ['x', 'y', 'z'];
const sparseDirectionMatrix =
getSparseOrthogonalMatrix(directionMatrix);

const lpsToXyz = {};
for (let row = 0; row < 3; ++row) {
for (let col = 0; col < 3; ++col) {
const index = row + 3 * col;
if (sparseDirectionMatrix[index] !== 0) {
lpsToXyz['lps'[row]] = 'xyz'[col];
}
}
}
const viewTypes = {
[VIEW_TYPE_VALUES.default]: '3D',
[VIEW_TYPE_VALUES[axisToXYZ[lpsDirections.l.axis]]]: 'Sagittal',
[VIEW_TYPE_VALUES[axisToXYZ[lpsDirections.p.axis]]]: 'Coronal',
[VIEW_TYPE_VALUES[axisToXYZ[lpsDirections.s.axis]]]: 'Axial',
[VIEW_TYPE_VALUES[lpsToXyz.l]]: 'Sagittal',
[VIEW_TYPE_VALUES[lpsToXyz.p]]: 'Coronal',
[VIEW_TYPE_VALUES[lpsToXyz.s]]: 'Axial',
};
const viewOrientation = [
lpsDirections.l.vector,
lpsDirections.p.vector,
lpsDirections.s.vector,
];

// viewOrientation = directionMatrix * transpose(sparseDirectionMatrix)
const viewOrientation = Array(3);
mat3.transpose(viewOrientation, sparseDirectionMatrix);
mat3.mul(viewOrientation, directionMatrix, viewOrientation);

dispatch('setViewTypes', viewTypes);
dispatch('setViewOrientation', {
orientation: viewOrientation,
Expand Down
59 changes: 3 additions & 56 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,58 +115,6 @@ export function getCropFilter(pxm, proxy) {
return volRep.getCropFilter();
}

/**
* Function adapted from getLPSDirections lps.ts from VolView.
* Associates the column vectors of a 3x3 matrix with the LPS axes.
*
* For each of the LPS axes, this function returns the associated column index (0, 1, 2)
* in the provided 3x3 column-major matrix and the corresponding positive vector.
*
* Approach:
* - find the max of the direction matrix, ignoring columns and rows marked as done
* - assign the column vector of that max value to the row axis
* - mark that row and column as done
* - continue until all rows and columns are done
*/
export function getLPSDirections(directionMatrix) {
const axisToLPS = ['l', 'p', 's'];
const lpsDirs = {};
// Track the rows and columns that have yet to be assigned.
const availableCols = [0, 1, 2];
const availableRows = [0, 1, 2];

for (let i = 0; i < 3; i++) {
let bestValue = 0;
let bestValueLoc = [0, 0]; // col, row
let removeIndices = [0, 0]; // indices into availableCols/Rows for deletion

availableCols.forEach((col, colIdx) => {
availableRows.forEach((row, rowIdx) => {
const value = directionMatrix[col * 3 + row];
if (Math.abs(value) > Math.abs(bestValue)) {
bestValue = value;
bestValueLoc = [col, row];
removeIndices = [colIdx, rowIdx];
}
});
});

// the row index corresponds to the index of the LPS axis
const [col, axis] = bestValueLoc;
const axisVector = directionMatrix.slice(col * 3, (col + 1) * 3);
const vecSign = Math.sign(bestValue);
const posVector = vec3.scale(Array(3), axisVector, vecSign);
lpsDirs[axisToLPS[axis]] = {
axis: col,
vector: posVector,
};
availableCols.splice(removeIndices[0], 1);
availableRows.splice(removeIndices[1], 1);
}

return lpsDirs;
}

/**
* Associate a key of VIEW_TYPE_VALUES to axis index and orientation
* for forward and upward vectors.
Expand Down Expand Up @@ -201,7 +149,7 @@ const MODE_TO_AXES = {
/**
* Update the camera view using the given arguments:
* - view which is a vtkViewProxy
* - basis, a list of 3 vec3 defining a basis
* - basis, a column major matrix defining a basis
* - mode, a key of VIEW_TYPE_VALUES
* - an optional number of animateSteps
*
Expand All @@ -221,12 +169,12 @@ export function updateViewOrientationFromBasisAndAxis(
MODE_TO_AXES[mode];
const forwardVector = vec3.scale(
Array(3),
basis[forwardAxis],
basis.slice(forwardAxis * 3, forwardAxis * 3 + 3),
forwardOrientation
);
const upwardVector = vec3.scale(
Array(3),
basis[upwardAxis],
basis.slice(upwardAxis * 3, upwardAxis * 3 + 3),
upwardOrientation
);

Expand Down Expand Up @@ -268,6 +216,5 @@ export default {
remapIdList,
createRepresentationInAllViews,
getCropFilter,
getLPSDirections,
updateViewOrientationFromBasisAndAxis,
};

0 comments on commit 1bfde11

Please sign in to comment.