All files / Sources/Widgets/Widgets3D/ResliceCursorWidget helpers.js

71.87% Statements 92/128
22.58% Branches 7/31
76% Functions 19/25
73.38% Lines 91/124

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361                              1x                       77x 77x 77x   77x 77x 77x   77x 77x 77x     77x 77x 77x 77x                 77x                   77x 77x 77x   77x 77x   77x 77x 77x   77x 77x 1x   76x           76x 228x   228x   228x     76x                                                                                                                                   40x 40x 40x                   15x 45x                 99x             84x                 10x 10x 30x 90x 60x       10x                             15x 15x 15x 23x   15x                     120x                       10x 30x       10x 30x 90x   60x         60x 60x   30x     10x 10x     10x   10x   10x 60x 60x 60x 60x 60x     60x         60x               60x 60x     60x         60x               60x 60x 60x 60x         60x   60x 60x 60x 60x 60x                                       72x 72x         72x         72x 72x    
import vtkBoundingBox, {
  STATIC,
} from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource';
import vtkCutter from 'vtk.js/Sources/Filters/Core/Cutter';
import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
 
import {
  planeNames,
  planeNameToViewType,
  viewTypeToPlaneName,
} from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/Constants';
 
const EPSILON = 10e-7;
 
/**
 * Fit the plane defined by origin, p1, p2 onto the bounds.
 * Plane is untouched if does not intersect bounds.
 * @param {Array} bounds
 * @param {Array} origin
 * @param {Array} p1
 * @param {Array} p2
 * @return {Boolean} false if no bounds have been found, else true
 */
export function boundPlane(bounds, origin, p1, p2) {
  const v1 = [];
  vtkMath.subtract(p1, origin, v1);
  vtkMath.normalize(v1);
 
  const v2 = [];
  vtkMath.subtract(p2, origin, v2);
  vtkMath.normalize(v2);
 
  const n = [0, 0, 1];
  vtkMath.cross(v1, v2, n);
  vtkMath.normalize(n);
 
  // Inflate bounds in order to avoid precision error when cutting cubesource
  const inflatedBounds = [...bounds];
  const eps = [...n];
  vtkMath.multiplyScalar(eps, EPSILON);
  vtkBoundingBox.addBounds(
    inflatedBounds,
    bounds[0] + eps[0],
    bounds[1] + eps[0],
    bounds[2] + eps[1],
    bounds[3] + eps[1],
    bounds[4] + eps[2],
    bounds[5] + eps[2]
  );
  vtkBoundingBox.addBounds(
    inflatedBounds,
    bounds[0] - eps[0],
    bounds[1] - eps[0],
    bounds[2] - eps[1],
    bounds[3] - eps[1],
    bounds[4] - eps[2],
    bounds[5] - eps[2]
  );
 
  const plane = vtkPlane.newInstance();
  plane.setOrigin(...origin);
  plane.setNormal(...n);
 
  const cubeSource = vtkCubeSource.newInstance();
  cubeSource.setBounds(inflatedBounds);
 
  const cutter = vtkCutter.newInstance();
  cutter.setCutFunction(plane);
  cutter.setInputConnection(cubeSource.getOutputPort());
 
  const cutBounds = cutter.getOutputData();
  if (cutBounds.getNumberOfPoints() === 0) {
    return false;
  }
  const localBounds = STATIC.computeLocalBounds(
    cutBounds.getPoints(),
    v1,
    v2,
    n
  );
  for (let i = 0; i < 3; i += 1) {
    origin[i] =
      localBounds[0] * v1[i] + localBounds[2] * v2[i] + localBounds[4] * n[i];
    p1[i] =
      localBounds[1] * v1[i] + localBounds[2] * v2[i] + localBounds[4] * n[i];
    p2[i] =
      localBounds[0] * v1[i] + localBounds[3] * v2[i] + localBounds[4] * n[i];
  }
  return true;
}
// Project point (inPoint) to the bounds of the image according to a plane
// defined by two vectors (v1, v2)
export function boundPoint(inPoint, v1, v2, bounds) {
  const absT1 = v1.map((val) => Math.abs(val));
  const absT2 = v2.map((val) => Math.abs(val));
 
  let o1 = 0.0;
  let o2 = 0.0;
 
  for (let i = 0; i < 3; i++) {
    let axisOffset = 0;
 
    const useT1 = absT1[i] > absT2[i];
    const t = useT1 ? v1 : v2;
    const absT = useT1 ? absT1 : absT2;
 
    if (inPoint[i] < bounds[i * 2]) {
      axisOffset = absT[i] > EPSILON ? (bounds[2 * i] - inPoint[i]) / t[i] : 0;
    } else if (inPoint[i] > bounds[2 * i + 1]) {
      axisOffset =
        absT[i] > EPSILON ? (bounds[2 * i + 1] - inPoint[i]) / t[i] : 0;
    }
 
    if (useT1) {
      if (Math.abs(axisOffset) > Math.abs(o1)) {
        o1 = axisOffset;
      }
    } else if (Math.abs(axisOffset) > Math.abs(o2)) {
      o2 = axisOffset;
    }
  }
 
  const outPoint = [inPoint[0], inPoint[1], inPoint[2]];
 
  if (o1 !== 0.0) {
    vtkMath.multiplyAccumulate(outPoint, v1, o1, outPoint);
  }
  if (o2 !== 0.0) {
    vtkMath.multiplyAccumulate(outPoint, v2, o2, outPoint);
  }
 
  return outPoint;
}
 
// Compute the intersection between p1 and p2 on bounds
export function boundPointOnPlane(p1, p2, bounds) {
  const dir12 = [0, 0, 0];
  vtkMath.subtract(p2, p1, dir12);
 
  const out = [0, 0, 0];
  const tolerance = [0, 0, 0];
  vtkBoundingBox.intersectBox(bounds, p1, dir12, out, tolerance);
 
  return out;
}
 
/**
 * Rotates a vector around another.
 * @param {vec3} vectorToBeRotated Vector to rate
 * @param {vec3} axis Axis to rotate around
 * @param {Number} angle Angle in radian
 * @returns The rotated vector
 */
export function rotateVector(vectorToBeRotated, axis, angle) {
  const rotatedVector = [...vectorToBeRotated];
  vtkMatrixBuilder.buildFromRadian().rotate(angle, axis).apply(rotatedVector);
  return rotatedVector;
}
 
/**
 * Return ['X', 'Y'] if there are only 2 planes defined in the widget state.
 * Return ['X', 'Y', 'Z'] if there are 3 planes defined in the widget state.
 * @param {object} widgetState the state of the widget
 * @returns An array of plane names
 */
export function getPlaneNames(widgetState) {
  return Object.keys(widgetState.getPlanes()).map(
    (viewType) => viewTypeToPlaneName[viewType]
  );
}
 
/**
 * Return X if lineName == XinY|XinZ, Y if lineName == YinX|YinZ and Z otherwise
 * @param {string} lineName name of the line (YinX, ZinX, XinY, ZinY, XinZ, YinZ)
 */
export function getLinePlaneName(lineName) {
  return lineName[0];
}
/**
 * Return X if lineName == YinX|ZinX, Y if lineName == XinY|ZinY and Z otherwise
 * @param {string} lineName name of the line (YinX, ZinX, XinY, ZinY, XinZ, YinZ)
 */
export function getLineInPlaneName(lineName) {
  return lineName[3];
}
 
/**
 * Returns ['XinY', 'YinX'] if planes == ['X', 'Y']
 * ['XinY', 'XinZ', 'YinX', 'YinZ', 'ZinX', 'ZinY'] if planes == ['X', 'Y', 'Z']
 * @param {string} planes name of the planes (e.g. ['X', 'Y'])
 */
export function getPlanesLineNames(planes = planeNames) {
  const lines = [];
  planes.forEach((plane) => {
    planes.forEach((inPlane) => {
      if (plane !== inPlane) {
        lines.push(`${plane}in${inPlane}`);
      }
    });
  });
  return lines;
}
 
export function getLineNames(widgetState) {
  const planes = Object.keys(widgetState.getPlanes()).map(
    (viewType) => viewTypeToPlaneName[viewType]
  );
  return getPlanesLineNames(planes);
}
 
/**
 * Return ZinX if lineName == YinX, YinX if lineName == ZinX, ZinY if lineName == XinY...
 * @param {string} lineName name of the line (YinX, ZinX, XinY, ZinY, XinZ, YinZ)
 */
export function getOtherLineName(widgetState, lineName) {
  const linePlaneName = getLinePlaneName(lineName);
  const lineInPlaneName = getLineInPlaneName(lineName);
  const otherLineName = getPlaneNames(widgetState).find(
    (planeName) => planeName !== linePlaneName && planeName !== lineInPlaneName
  );
  return `${otherLineName}in${lineInPlaneName}`;
}
 
// Compute the offset of the rotation handle origin
function computeRotationHandleOriginOffset(
  axis,
  rotationHandlePosition,
  volumeDiagonalLength,
  scaleInPixels
) {
  // FIXME: p1 and p2 could be placed on the exact boundaries of the volume.
  return vtkMath.multiplyScalar(
    [...axis],
    (rotationHandlePosition * (scaleInPixels ? 1 : volumeDiagonalLength)) / 2
  );
}
 
// Update the reslice cursor state according to the three planes normals and the origin
export function updateState(
  widgetState,
  scaleInPixels,
  rotationHandlePosition
) {
  const planes = Object.keys(widgetState.getPlanes()).map(
    (viewType) => viewTypeToPlaneName[viewType]
  );
  // Generates an object as such:
  // axes = {'XY': cross(X, Y), 'YX': cross(X, Y), 'YZ': cross(Y, Z)...}
  const axes = planes.reduce((res, plane) => {
    planes
      .filter((otherPlane) => plane !== otherPlane)
      .forEach((otherPlane) => {
        const cross = vtkMath.cross(
          widgetState.getPlanes()[planeNameToViewType[plane]].normal,
          widgetState.getPlanes()[planeNameToViewType[otherPlane]].normal,
          []
        );
        res[`${plane}${otherPlane}`] = cross;
        res[`${otherPlane}${plane}`] = cross;
      });
    return res;
  }, {});
 
  const bounds = widgetState.getImage().getBounds();
  const center = widgetState.getCenter();
 
  // Length of the principal diagonal.
  const pdLength = vtkBoundingBox.getDiagonalLength(bounds);
 
  widgetState.getCenterHandle().setOrigin(center);
 
  getPlanesLineNames(planes).forEach((lineName) => {
    const planeName = getLinePlaneName(lineName);
    const inPlaneName = getLineInPlaneName(lineName);
    const direction = axes[`${planeName}${inPlaneName}`];
    widgetState[`getRotationHandle${lineName}0`]().setOrigin(center);
    widgetState[`getRotationHandle${lineName}0`]()
      .getManipulator()
      ?.setHandleOrigin(center);
    widgetState[`getRotationHandle${lineName}0`]()
      .getManipulator()
      ?.setHandleNormal(
        widgetState.getPlanes()[planeNameToViewType[planeName]].normal
      );
    widgetState[`getRotationHandle${lineName}0`]().setOffset(
      computeRotationHandleOriginOffset(
        direction,
        rotationHandlePosition,
        pdLength,
        scaleInPixels
      )
    );
    widgetState[`getRotationHandle${lineName}1`]().setOrigin(center);
    widgetState[`getRotationHandle${lineName}1`]()
      .getManipulator()
      ?.setHandleOrigin(center);
    widgetState[`getRotationHandle${lineName}1`]()
      .getManipulator()
      ?.setHandleNormal(
        widgetState.getPlanes()[planeNameToViewType[planeName]].normal
      );
    widgetState[`getRotationHandle${lineName}1`]().setOffset(
      computeRotationHandleOriginOffset(
        direction,
        -rotationHandlePosition,
        pdLength,
        scaleInPixels
      )
    );
    const lineHandle = widgetState[`getAxis${lineName}`]();
    lineHandle.setOrigin(center);
    lineHandle.getManipulator()?.setHandleOrigin(center);
    lineHandle
      .getManipulator()
      ?.setHandleNormal(
        widgetState.getPlanes()[planeNameToViewType[planeName]].normal
      );
    vtkMath.normalize(direction);
    const right =
      widgetState.getPlanes()[planeNameToViewType[inPlaneName]].normal;
    const up = vtkMath.cross(direction, right, []);
    lineHandle.setRight(right);
    lineHandle.setUp(up);
    lineHandle.setDirection(direction);
  });
}
 
/**
 * First rotate planeToTransform to match targetPlane normal.
 * Then rotate around targetNormal to enforce targetViewUp "up" vector (i.e. Origin->p2 ).
 * There is an infinite number of options to rotate a plane normal to another. Here we attempt to
 * preserve Origin, P1 and P2 when rotating around targetPlane.
 * @param {vtkPlaneSource} planeToTransform
 * @param {vec3} targetOrigin Center of the plane
 * @param {vec3} targetNormal Normal to state to the plane
 * @param {vec3} viewType Vector that enforces view up
 */
export function transformPlane(
  planeToTransform,
  targetCenter,
  targetNormal,
  targetViewUp
) {
  planeToTransform.setNormal(targetNormal);
  const viewUp = vtkMath.subtract(
    planeToTransform.getPoint2(),
    planeToTransform.getOrigin(),
    []
  );
  const angle = vtkMath.signedAngleBetweenVectors(
    viewUp,
    targetViewUp,
    targetNormal
  );
  planeToTransform.rotate(angle, targetNormal);
  planeToTransform.setCenter(targetCenter);
}