All files / Sources/Rendering/Core/ImageSlice index.js

65.38% Statements 68/104
50.98% Branches 26/51
52.17% Functions 12/23
69.23% Lines 63/91

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            1x               49x   49x 49x   49x 184x     184x       184x   12x     184x     184x   184x               49x   49x   49x 965x 41x   965x     49x 51x         51x 51x         51x 7x 7x 7x 7x               204x 44x     102x         34x 204x   34x 34x 34x   34x 34x   44x     49x                                 49x     49x     49x     49x     49x     49x   49x 355x 355x 308x 308x     355x     49x                                             49x               1x                   49x     49x     49x 49x     49x 49x 49x     49x         1x          
import { mat4 } from 'gl-matrix';
import macro from 'vtk.js/Sources/macros';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import vtkProp3D from 'vtk.js/Sources/Rendering/Core/Prop3D';
import vtkImageProperty from 'vtk.js/Sources/Rendering/Core/ImageProperty';
 
const { vtkDebugMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkImageSlice methods
// ----------------------------------------------------------------------------
 
function vtkImageSlice(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageSlice');
 
  publicAPI.getActors = () => publicAPI;
  publicAPI.getImages = () => publicAPI;
 
  publicAPI.getIsOpaque = () => {
    Iif (model.forceOpaque) {
      return true;
    }
    Iif (model.forceTranslucent) {
      return false;
    }
    // make sure we have a property
    if (!model.property) {
      // force creation of a property
      publicAPI.getProperty();
    }
 
    let isOpaque = model.property.getOpacity() >= 1.0;
 
    // are we using an opaque scalar array, if any?
    isOpaque = isOpaque && (!model.mapper || model.mapper.getIsOpaque());
 
    return isOpaque;
  };
 
  // Always render during opaque pass, to keep the behavior
  // predictable and because depth-peeling kills alpha-blending.
  // In the future, the Renderer should render images in layers,
  // i.e. where each image will have a layer number assigned to it,
  // and the Renderer will do the images in their own pass.
  publicAPI.hasTranslucentPolygonalGeometry = () => false;
 
  publicAPI.makeProperty = vtkImageProperty.newInstance;
 
  publicAPI.getProperty = () => {
    if (model.property === null) {
      model.property = publicAPI.makeProperty();
    }
    return model.property;
  };
 
  publicAPI.getBounds = () => {
    Iif (model.mapper === null) {
      return model.bounds;
    }
 
    // Check for the special case when the mapper's bounds are unknown
    const bds = model.mapper.getBounds();
    Iif (!bds || bds.length !== 6) {
      return bds;
    }
 
    // Check for the special case when the actor is empty.
    if (bds[0] > bds[1]) {
      model.mapperBounds = bds.concat(); // copy the mapper's bounds
      model.bounds = [1, -1, 1, -1, 1, -1];
      model.boundsMTime.modified();
      return bds;
    }
 
    // Check if we have cached values for these bounds - we cache the
    // values returned by model.mapper.getBounds() and we store the time
    // of caching. If the values returned this time are different, or
    // the modified time of this class is newer than the cached time,
    // then we need to rebuild.
    const zip = (rows) => rows[0].map((_, c) => rows.map((row) => row[c]));
    if (
      !model.mapperBounds ||
      !zip([bds, model.mapperBounds]).reduce(
        (a, b) => a && b[0] === b[1],
        true
      ) ||
      publicAPI.getMTime() > model.boundsMTime.getMTime()
    ) {
      vtkDebugMacro('Recomputing bounds...');
      model.mapperBounds = bds.map((x) => x);
 
      publicAPI.computeMatrix();
      const tmp4 = new Float64Array(16);
      mat4.transpose(tmp4, model.matrix);
 
      vtkBoundingBox.transformBounds(bds, tmp4, model.bounds);
      model.boundsMTime.modified();
    }
    return model.bounds;
  };
 
  publicAPI.getBoundsForSlice = (slice, thickness) => {
    // Check for the special case when the mapper's bounds are unknown
    const bds = model.mapper.getBoundsForSlice(slice, thickness);
    // Check for the special case when the actor is empty.
    if (!vtkBoundingBox.isValid(bds)) {
      return bds;
    }
 
    publicAPI.computeMatrix();
    const tmp4 = new Float64Array(16);
    mat4.transpose(tmp4, model.matrix);
    const newBounds = vtkBoundingBox.transformBounds(bds, tmp4);
    return newBounds;
  };
 
  //----------------------------------------------------------------------------
  // Get the minimum X bound
  publicAPI.getMinXBound = () => publicAPI.getBounds()[0];
 
  // Get the maximum X bound
  publicAPI.getMaxXBound = () => publicAPI.getBounds()[1];
 
  // Get the minimum Y bound
  publicAPI.getMinYBound = () => publicAPI.getBounds()[2];
 
  // Get the maximum Y bound
  publicAPI.getMaxYBound = () => publicAPI.getBounds()[3];
 
  // Get the minimum Z bound
  publicAPI.getMinZBound = () => publicAPI.getBounds()[4];
 
  // Get the maximum Z bound
  publicAPI.getMaxZBound = () => publicAPI.getBounds()[5];
 
  publicAPI.getMTime = () => {
    let mt = model.mtime;
    if (model.property !== null) {
      const time = model.property.getMTime();
      mt = time > mt ? time : mt;
    }
 
    return mt;
  };
 
  publicAPI.getRedrawMTime = () => {
    let mt = model.mtime;
    if (model.mapper !== null) {
      let time = model.mapper.getMTime();
      mt = time > mt ? time : mt;
      if (model.mapper.getInput() !== null) {
        // FIXME !!! getInputAlgorithm / getInput
        model.mapper.getInputAlgorithm().update();
        time = model.mapper.getInput().getMTime();
        mt = time > mt ? time : mt;
      }
    }
    if (model.property !== null) {
      let time = model.property.getMTime();
      mt = time > mt ? time : mt;
      if (model.property.getRGBTransferFunction() !== null) {
        time = model.property.getRGBTransferFunction().getMTime();
        mt = time > mt ? time : mt;
      }
    }
    return mt;
  };
 
  publicAPI.getSupportsSelection = () =>
    model.mapper ? model.mapper.getSupportsSelection() : false;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  mapper: null,
  property: null,
 
  bounds: [...vtkBoundingBox.INIT_BOUNDS],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkProp3D.extend(publicAPI, model, initialValues);
 
  // vtkTimeStamp
  model.boundsMTime = {};
  macro.obj(model.boundsMTime);
 
  // Build VTK API
  macro.set(publicAPI, model, ['property']);
  macro.setGet(publicAPI, model, ['mapper']);
  macro.getArray(publicAPI, model, ['bounds'], 6);
 
  // Object methods
  vtkImageSlice(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageSlice');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };