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

100% Statements 13/13
100% Branches 1/1
100% Functions 5/5
100% Lines 12/12

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              1x                                   1x             3x               9x                                                   3x   9x 27x     18x                                 18x 36x                                 18x       3x    
import vtkStateBuilder from 'vtk.js/Sources/Widgets/Core/StateBuilder';
import {
  ScrollingMethods,
  planeNames,
  planeNameToViewType,
} from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/Constants';
 
const defaultPlanes = {
  X: {
    normal: [1, 0, 0],
    viewUp: [0, 0, 1],
    color3: [255, 0, 0],
  },
  Y: {
    normal: [0, -1, 0],
    viewUp: [0, 0, 1],
    color3: [0, 255, 0],
  },
  Z: {
    normal: [0, 0, -1],
    viewUp: [0, -1, 0],
    color3: [0, 0, 255],
  },
};
 
const viewsColor3 = {
  X: [255, 0, 0], // red
  Y: [0, 255, 0], // green
  Z: [0, 0, 255], // blue
};
 
export default function generateState(planes = planeNames) {
  const state = vtkStateBuilder
    .createBuilder()
    .addField({ name: 'center', initialValue: [0, 0, 0] })
    .addField({ name: 'image', initialValue: null })
    .addField({ name: 'activeViewType', initialValue: null })
    .addField({
      name: 'planes',
      initialValue: planes.reduce(
        (res, planeName) => ({
          ...res,
          [planeNameToViewType[planeName]]: {
            normal: defaultPlanes[planeName].normal,
            viewUp: defaultPlanes[planeName].viewUp,
          },
        }),
        {}
      ),
    })
    .addField({
      name: 'scrollingMethod',
      initialValue: ScrollingMethods.MIDDLE_MOUSE_BUTTON,
    })
    .addField({ name: 'cameraOffsets', initialValue: {} })
    .addField({ name: 'viewUpFromViewType', initialValue: {} })
    .addStateFromMixin({
      labels: ['handles', 'sphere', 'center'],
      mixins: ['origin', 'color3', 'scale1', 'visible', 'manipulator'],
      name: 'centerHandle',
      initialValues: {
        scale1: 30,
        color3: [255, 255, 255],
      },
    });
 
  planes.reduce(
    (viewState, view) =>
      planes
        .filter((v) => v !== view)
        .reduce((axisState, axis) => {
          // Line handle
          axisState.addStateFromMixin({
            labels: ['handles', 'line', `lineIn${view}`, `${axis}in${view}`],
            mixins: [
              'origin',
              'color3',
              'scale3', // scale3[2] will be automatically overwritten
              'orientation',
              'visible',
              'manipulator',
            ],
            name: `axis${axis}in${view}`,
            initialValues: {
              scale3: [4, 4, 4],
              color3: viewsColor3[axis],
            },
          });
          // Rotation handle
          for (let rotationHandle = 0; rotationHandle < 2; ++rotationHandle) {
            axisState.addStateFromMixin({
              labels: [
                'handles',
                'sphere',
                'rotation',
                `rotationIn${view}`,
                `${axis}in${view}`,
                `point${rotationHandle}`,
              ],
              mixins: ['origin', 'color3', 'scale1', 'visible', 'manipulator'],
              name: `rotationHandle${axis}in${view}${rotationHandle}`,
              initialValues: {
                scale1: 30,
                color3: viewsColor3[axis],
              },
            });
          }
          return axisState;
        }, viewState),
    state
  );
  return state.build();
}