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

70.22% Statements 92/131
38.96% Branches 30/77
80% Functions 16/20
69.53% Lines 89/128

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          1x 1x   1x               17x   17x 77x     77x   308x 217x         91x 91x   91x 91x         308x   90x 90x     308x                 77x     17x                   17x                                     17x                               17x 16x 16x 16x 16x     16x 16x 16x     16x 16x   16x       17x 55x                                         55x       17x 15x 15x 15x 15x           17x 55x 1x   1x 1x 1x     55x     17x 6x         6x 6x 2x 2x 2x   4x     17x 8x         8x     17x 1x   17x 4x   17x 7x   17x     17x                   17x 136x 136x 27x 22x 22x 22x   5x       17x                   17x 136x 165x               1x                                 17x     17x   17x 17x 17x 68x                                       17x                         17x     17x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction';
import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction';
import Constants from 'vtk.js/Sources/Rendering/Core/VolumeProperty/Constants';
 
const { InterpolationType, OpacityMode } = Constants;
const { vtkErrorMacro } = macro;
 
const VTK_MAX_VRCOMP = 4;
 
// ----------------------------------------------------------------------------
// vtkVolumeProperty methods
// ----------------------------------------------------------------------------
 
function vtkVolumeProperty(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkVolumeProperty');
 
  publicAPI.getMTime = () => {
    let mTime = model.mtime;
    let time;
 
    for (let index = 0; index < VTK_MAX_VRCOMP; index++) {
      // Color MTimes
      if (model.componentData[index].colorChannels === 1) {
        Iif (model.componentData[index].grayTransferFunction) {
          // time that Gray transfer function was last modified
          time = model.componentData[index].grayTransferFunction.getMTime();
          mTime = mTime > time ? mTime : time;
        }
      } else if (model.componentData[index].colorChannels === 3) {
        if (model.componentData[index].rGBTransferFunction) {
          // time that RGB transfer function was last modified
          time = model.componentData[index].rGBTransferFunction.getMTime();
          mTime = mTime > time ? mTime : time;
        }
      }
 
      // Opacity MTimes
      if (model.componentData[index].scalarOpacity) {
        // time that Scalar opacity transfer function was last modified
        time = model.componentData[index].scalarOpacity.getMTime();
        mTime = mTime > time ? mTime : time;
      }
 
      Iif (model.componentData[index].gradientOpacity) {
        if (!model.componentData[index].disableGradientOpacity) {
          // time that Gradient opacity transfer function was last modified
          time = model.componentData[index].gradientOpacity.getMTime();
          mTime = mTime > time ? mTime : time;
        }
      }
    }
 
    return mTime;
  };
 
  publicAPI.getColorChannels = (index) => {
    if (index < 0 || index > 3) {
      vtkErrorMacro('Bad index - must be between 0 and 3');
      return 0;
    }
 
    return model.componentData[index].colorChannels;
  };
 
  // Set the color of a volume to a gray transfer function
  publicAPI.setGrayTransferFunction = (index = 0, func = null) => {
    let modified = false;
    if (model.componentData[index].grayTransferFunction !== func) {
      model.componentData[index].grayTransferFunction = func;
      modified = true;
    }
 
    if (model.componentData[index].colorChannels !== 1) {
      model.componentData[index].colorChannels = 1;
      modified = true;
    }
 
    if (modified) {
      publicAPI.modified();
    }
    return modified;
  };
 
  // Get the currently set gray transfer function. Create one if none set.
  publicAPI.getGrayTransferFunction = (index = 0) => {
    if (model.componentData[index].grayTransferFunction === null) {
      model.componentData[index].grayTransferFunction =
        vtkPiecewiseFunction.newInstance();
      model.componentData[index].grayTransferFunction.addPoint(0, 0.0);
      model.componentData[index].grayTransferFunction.addPoint(1024, 1.0);
      if (model.componentData[index].colorChannels !== 1) {
        model.componentData[index].colorChannels = 1;
      }
      publicAPI.modified();
    }
 
    return model.componentData[index].grayTransferFunction;
  };
 
  // Set the color of a volume to an RGB transfer function
  publicAPI.setRGBTransferFunction = (index = 0, func = null) => {
    let modified = false;
    if (model.componentData[index].rGBTransferFunction !== func) {
      model.componentData[index].rGBTransferFunction = func;
      modified = true;
    }
 
    if (model.componentData[index].colorChannels !== 3) {
      model.componentData[index].colorChannels = 3;
      modified = true;
    }
 
    if (modified) {
      publicAPI.modified();
    }
    return modified;
  };
 
  // Get the currently set RGB transfer function. Create one if none set.
  publicAPI.getRGBTransferFunction = (index = 0) => {
    Iif (model.componentData[index].rGBTransferFunction === null) {
      model.componentData[index].rGBTransferFunction =
        vtkColorTransferFunction.newInstance();
      model.componentData[index].rGBTransferFunction.addRGBPoint(
        0,
        0.0,
        0.0,
        0.0
      );
      model.componentData[index].rGBTransferFunction.addRGBPoint(
        1024,
        1.0,
        1.0,
        1.0
      );
      if (model.componentData[index].colorChannels !== 3) {
        model.componentData[index].colorChannels = 3;
      }
      publicAPI.modified();
    }
 
    return model.componentData[index].rGBTransferFunction;
  };
 
  // Set the scalar opacity of a volume to a transfer function
  publicAPI.setScalarOpacity = (index = 0, func = null) => {
    if (model.componentData[index].scalarOpacity !== func) {
      model.componentData[index].scalarOpacity = func;
      publicAPI.modified();
      return true;
    }
    return false;
  };
 
  // Get the scalar opacity transfer function. Create one if none set.
  publicAPI.getScalarOpacity = (index = 0) => {
    if (model.componentData[index].scalarOpacity === null) {
      model.componentData[index].scalarOpacity =
        vtkPiecewiseFunction.newInstance();
      model.componentData[index].scalarOpacity.addPoint(0, 1.0);
      model.componentData[index].scalarOpacity.addPoint(1024, 1.0);
      publicAPI.modified();
    }
 
    return model.componentData[index].scalarOpacity;
  };
 
  publicAPI.setComponentWeight = (index = 0, value = 1) => {
    Iif (index < 0 || index >= VTK_MAX_VRCOMP) {
      vtkErrorMacro('Invalid index');
      return false;
    }
 
    const val = Math.min(1, Math.max(0, value));
    if (model.componentData[index].componentWeight !== val) {
      model.componentData[index].componentWeight = val;
      publicAPI.modified();
      return true;
    }
    return false;
  };
 
  publicAPI.getComponentWeight = (index = 0) => {
    Iif (index < 0 || index >= VTK_MAX_VRCOMP) {
      vtkErrorMacro('Invalid index');
      return 0.0;
    }
 
    return model.componentData[index].componentWeight;
  };
 
  publicAPI.setInterpolationTypeToNearest = () =>
    publicAPI.setInterpolationType(InterpolationType.NEAREST);
 
  publicAPI.setInterpolationTypeToLinear = () =>
    publicAPI.setInterpolationType(InterpolationType.LINEAR);
 
  publicAPI.setInterpolationTypeToFastLinear = () =>
    publicAPI.setInterpolationType(InterpolationType.FAST_LINEAR);
 
  publicAPI.getInterpolationTypeAsString = () =>
    macro.enumToString(InterpolationType, model.interpolationType);
 
  const sets = [
    'useGradientOpacity',
    'scalarOpacityUnitDistance',
    'gradientOpacityMinimumValue',
    'gradientOpacityMinimumOpacity',
    'gradientOpacityMaximumValue',
    'gradientOpacityMaximumOpacity',
    'opacityMode',
    'forceNearestInterpolation',
  ];
  sets.forEach((val) => {
    const cap = macro.capitalize(val);
    publicAPI[`set${cap}`] = (index, value) => {
      if (model.componentData[index][`${val}`] !== value) {
        model.componentData[index][`${val}`] = value;
        publicAPI.modified();
        return true;
      }
      return false;
    };
  });
 
  const gets = [
    'useGradientOpacity',
    'scalarOpacityUnitDistance',
    'gradientOpacityMinimumValue',
    'gradientOpacityMinimumOpacity',
    'gradientOpacityMaximumValue',
    'gradientOpacityMaximumOpacity',
    'opacityMode',
    'forceNearestInterpolation',
  ];
  gets.forEach((val) => {
    const cap = macro.capitalize(val);
    publicAPI[`get${cap}`] = (index) => model.componentData[index][`${val}`];
  });
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  colorMixPreset: null,
  independentComponents: true,
  interpolationType: InterpolationType.FAST_LINEAR,
  shade: false,
  ambient: 0.1,
  diffuse: 0.7,
  specular: 0.2,
  specularPower: 10.0,
  useLabelOutline: false,
  labelOutlineThickness: [1],
  labelOutlineOpacity: 1.0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
 
  if (!model.componentData) {
    model.componentData = [];
    for (let i = 0; i < VTK_MAX_VRCOMP; ++i) {
      model.componentData.push({
        colorChannels: 1,
        grayTransferFunction: null,
        rGBTransferFunction: null,
        scalarOpacity: null,
        scalarOpacityUnitDistance: 1.0,
        opacityMode: OpacityMode.FRACTIONAL,
 
        gradientOpacityMinimumValue: 0,
        gradientOpacityMinimumOpacity: 0.0,
        gradientOpacityMaximumValue: 1.0,
        gradientOpacityMaximumOpacity: 1.0,
        useGradientOpacity: false,
 
        componentWeight: 1.0,
        forceNearestInterpolation: false,
      });
    }
  }
 
  macro.setGet(publicAPI, model, [
    'colorMixPreset',
    'independentComponents',
    'interpolationType',
    'shade',
    'ambient',
    'diffuse',
    'specular',
    'specularPower',
    'useLabelOutline',
    'labelOutlineOpacity',
  ]);
 
  macro.setGetArray(publicAPI, model, ['labelOutlineThickness']);
 
  // Object methods
  vtkVolumeProperty(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkVolumeProperty');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };