All files / Sources/Widgets/Core/StateBuilder boundsMixin.js

33.33% Statements 12/36
20% Branches 1/5
40% Functions 2/5
35.29% Lines 12/34

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        24x 24x   24x             24x                                   24x                                             1x               24x 24x 24x 24x   24x 24x            
import macro from 'vtk.js/Sources/macros';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
 
function vtkBoundsMixin(publicAPI, model) {
  const sourceBounds = [];
  const bbox = [...vtkBoundingBox.INIT_BOUNDS];
 
  publicAPI.containsPoint = (x, y, z) => {
    if (Array.isArray(x)) {
      return vtkBoundingBox.containsPoint(bbox, x[0], x[1], x[2]);
    }
    return vtkBoundingBox.containsPoint(bbox, x, y, z);
  };
 
  publicAPI.placeWidget = (bounds) => {
    model.bounds = [];
    const center = [
      (bounds[0] + bounds[1]) / 2.0,
      (bounds[2] + bounds[3]) / 2.0,
      (bounds[4] + bounds[5]) / 2.0,
    ];
    for (let i = 0; i < 6; i++) {
      const axisCenter = center[Math.floor(i / 2)];
      sourceBounds[i] = bounds[i];
      model.bounds[i] =
        (bounds[i] - axisCenter) * model.placeFactor + axisCenter;
    }
    vtkBoundingBox.setBounds(bbox, model.bounds);
    publicAPI.invokeBoundsChange(model.bounds);
    publicAPI.modified();
  };
 
  publicAPI.setPlaceFactor = (factor) => {
    if (model.placeFactor !== factor) {
      model.placeFactor = factor;
      model.bounds = [];
      const center = [
        (sourceBounds[0] + sourceBounds[1]) / 2.0,
        (sourceBounds[2] + sourceBounds[3]) / 2.0,
        (sourceBounds[4] + sourceBounds[5]) / 2.0,
      ];
      for (let i = 0; i < 6; i++) {
        const axisCenter = center[Math.floor(i / 2)];
        model.bounds[i] =
          (sourceBounds[i] - axisCenter) * model.placeFactor + axisCenter;
      }
      vtkBoundingBox.setBounds(bbox, model.bounds);
      publicAPI.invokeBoundsChange(model.bounds);
      publicAPI.modified();
    }
  };
}
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  bounds: [-1, 1, -1, 1, -1, 1],
  placeFactor: 1,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
  macro.setGetArray(publicAPI, model, ['bounds'], 6);
  macro.get(publicAPI, model, ['placeFactor']);
  macro.event(publicAPI, model, 'BoundsChange');
 
  model.bounds = model.bounds.slice();
  vtkBoundsMixin(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export default { extend };