All files / Sources/Widgets/Representations/GlyphRepresentation index.js

98.5% Statements 132/134
80.51% Branches 62/77
100% Functions 30/30
98.43% Lines 126/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 320 321 322 323 324 325 326                                          23x 80x 80x 80x 196x     196x 196x 196x         23x 4x       23x 72x 72x             72x 72x 72x 168x 168x     168x 168x 168x 168x   72x       23x 8x 8x 8x 28x 28x     28x         23x 4x       23x 24x 24x 24x 24x     24x             24x 24x 48x 48x 48x 48x         48x 48x 48x 48x         23x 56x 56x 56x 56x 56x 148x 148x 148x 145x         148x 148x         23x 4x 4x 4x       23x 24x 24x 24x             24x 48x 48x 48x 48x 48x 48x 48x         23x 60x           23x 23x 23x     408x   408x                 23x 84x   204x     23x 84x 84x 80x   4x     84x 72x 12x 8x   4x     84x 24x 60x 56x   4x     84x 24x   60x   84x     23x 84x 84x   84x   84x 336x     84x 84x     23x 23x               23x                                                                                       23x 1x           22x           23x 6x     23x 23x   23x     23x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkGlyph3DMapper from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper';
import vtkHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/HandleRepresentation';
import vtkContextRepresentation from 'vtk.js/Sources/Widgets/Representations/ContextRepresentation';
import vtkSphereSource from 'vtk.js/Sources/Filters/Sources/SphereSource';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
import { ScalarMode } from 'vtk.js/Sources/Rendering/Core/Mapper/Constants';
import { getPixelWorldHeightAtCoord } from 'vtk.js/Sources/Widgets/Core/WidgetManager';
 
import vtkWidgetRepresentation, {
  allocateArray,
} from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation';
import { Behavior } from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation/Constants';
import { OrientationModes } from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper/Constants';
 
// ----------------------------------------------------------------------------
// vtkGlyphRepresentation methods
// ----------------------------------------------------------------------------
export function origin(publicAPI, model) {
  return (polyData, states) => {
    const points = allocateArray(polyData, 'points', states.length).getData();
    let j = 0;
    for (let i = 0; i < states.length; ++i) {
      const coord = states[i].getOrigin(
        model.scaleInPixels && model.displayScaleParams
      );
      points[j++] = coord[0];
      points[j++] = coord[1];
      points[j++] = coord[2];
    }
  };
}
export function noPosition(publicAPI, model) {
  return (polyData, states) => {
    allocateArray(polyData, 'points', 0);
  };
}
export function color3(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setColorByArrayName('color');
    const colorArray = allocateArray(
      polyData,
      'color',
      states.length,
      'Uint8Array', // RGBA
      4
    );
    const colors = colorArray.getData();
    let j = 0;
    for (let i = 0; i < states.length; ++i) {
      let c3 = states[i].getColor3();
      Iif (states[i].getActive() && model.useActiveColor) {
        c3 = model.activeColor;
      }
      colors[j++] = c3[0];
      colors[j++] = c3[1];
      colors[j++] = c3[2];
      colors[j++] = states[i].getOpacity();
    }
    colorArray.dataChange();
  };
}
export function color(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setColorByArrayName('color');
    const colors = allocateArray(polyData, 'color', states.length).getData();
    for (let i = 0; i < states.length; ++i) {
      let c = states[i].getColor();
      Iif (states[i].getActive() && model.useActiveColor) {
        c = model.activeColor;
      }
      colors[i] = c;
    }
  };
}
export function noColor(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setColorByArrayName(null);
  };
}
export function scale3(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setScaleArray('scale');
    model._pipeline.mapper.setScaleFactor(1);
    model._pipeline.mapper.setScaling(true);
    model._pipeline.mapper.setScaleMode(
      vtkGlyph3DMapper.ScaleModes.SCALE_BY_COMPONENTS
    );
    const scales = allocateArray(
      polyData,
      'scale',
      states.length,
      'Float32Array',
      3
    ).getData();
    let j = 0;
    for (let i = 0; i < states.length; ++i) {
      const state = states[i];
      let scaleFactor = state.getActive() ? model.activeScaleFactor : 1;
      if (publicAPI.getScaleInPixels()) {
        scaleFactor *= getPixelWorldHeightAtCoord(
          state.getOrigin(),
          model.displayScaleParams
        );
      }
      const scale = state.getScale3?.() ?? model.defaultScale;
      scales[j++] = scaleFactor * scale[0];
      scales[j++] = scaleFactor * scale[1];
      scales[j++] = scaleFactor * scale[2];
    }
  };
}
export function scale1(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setScaleArray('scale');
    model._pipeline.mapper.setScaleFactor(1);
    model._pipeline.mapper.setScaling(true);
    const scales = allocateArray(polyData, 'scale', states.length).getData();
    for (let i = 0; i < states.length; ++i) {
      const state = states[i];
      let scaleFactor = state.getActive() ? model.activeScaleFactor : 1;
      if (publicAPI.getScaleInPixels()) {
        scaleFactor *= getPixelWorldHeightAtCoord(
          state.getOrigin(),
          model.displayScaleParams
        );
      }
      const scale = state.getScale1?.() ?? model.defaultScale;
      scales[i] = scaleFactor * scale;
    }
  };
}
export function noScale(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setScaleArray(null);
    model._pipeline.mapper.setScaleFactor(model.defaultScale);
    model._pipeline.mapper.setScaling(model.defaultScale !== 1);
  };
}
export function direction(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setOrientationArray('orientation');
    model._pipeline.mapper.setOrientationMode(OrientationModes.MATRIX);
    const orientation = allocateArray(
      polyData,
      'orientation',
      states.length,
      'Float32Array',
      9
    ).getData();
    for (let i = 0; i < states.length; ++i) {
      const state = states[i];
      const right = state.getRight ? state.getRight() : [1, 0, 0];
      const up = state.getUp ? state.getUp() : [0, 1, 0];
      const dir = state.getDirection ? state.getDirection() : [0, 0, 1];
      orientation.set(right, 9 * i);
      orientation.set(up, 9 * i + 3);
      orientation.set(dir, 9 * i + 6);
    }
  };
}
export function noOrientation(publicAPI, model) {
  return (polyData, states) => {
    model._pipeline.mapper.setOrientationArray(null);
  };
}
 
function vtkGlyphRepresentation(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkGlyphRepresentation');
  const superClass = { ...publicAPI };
  const internalPolyData = vtkPolyData.newInstance({ mtime: 0 });
 
  function hasMixin(states, ...requiredMixins) {
    return requiredMixins.every(
      (requiredMixin) =>
        states[0]?.[`get${macro.capitalize(requiredMixin)}`]?.() != null
    );
  }
  // --------------------------------------------------------------------------
  // Generic rendering pipeline
  // --------------------------------------------------------------------------
 
  // --------------------------------------------------------------------------
 
  publicAPI.getRepresentationStates = (input = model.inputData[0]) =>
    superClass
      .getRepresentationStates(input)
      .filter((state) => state.getOrigin?.() && (state.isVisible?.() ?? true));
 
  // --------------------------------------------------------------------------
  publicAPI.getMixins = (states) => {
    const glyphProperties = {};
    if (hasMixin(states, 'origin')) {
      glyphProperties.position = model.applyMixin.origin;
    } else {
      glyphProperties.position = model.applyMixin.noPosition;
    }
 
    if (hasMixin(states, 'color3')) {
      glyphProperties.color = model.applyMixin.color3;
    } else if (hasMixin(states, 'color')) {
      glyphProperties.color = model.applyMixin.color;
    } else {
      glyphProperties.color = model.applyMixin.noColor;
    }
 
    if (hasMixin(states, 'scale3')) {
      glyphProperties.scale = model.applyMixin.scale3;
    } else if (hasMixin(states, 'scale1')) {
      glyphProperties.scale = model.applyMixin.scale1;
    } else {
      glyphProperties.scale = model.applyMixin.noScale;
    }
 
    if (hasMixin(states, 'direction')) {
      glyphProperties.orientation = model.applyMixin.direction;
    } else {
      glyphProperties.orientation = model.applyMixin.noOrientation;
    }
    return glyphProperties;
  };
 
  publicAPI.requestData = (inData, outData) => {
    const states = publicAPI.getRepresentationStates(inData[0]);
    outData[0] = internalPolyData;
 
    const glyphProperties = publicAPI.getMixins(states);
 
    Object.values(glyphProperties).forEach((property) =>
      property(internalPolyData, states)
    );
 
    internalPolyData.getPoints().modified();
    internalPolyData.modified();
  };
 
  vtkWidgetRepresentation.connectPipeline(model._pipeline);
  publicAPI.addActor(model._pipeline.actor);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
function defaultValues(publicAPI, model, initialValues) {
  return {
    defaultScale: 1,
    ...initialValues,
    _pipeline: {
      source: initialValues._pipeline?.source ?? publicAPI,
      glyph:
        initialValues._pipeline?.glyph ?? // in case glyph was provided
        vtkSphereSource.newInstance({
          phiResolution: 8,
          thetaResolution: 8,
        }),
      mapper:
        initialValues._pipeline?.mapper ?? // in case mapper was provided
        vtkGlyph3DMapper.newInstance({
          scalarMode: ScalarMode.USE_POINT_FIELD_DATA,
        }),
      actor:
        initialValues._pipeline?.actor ?? // in case actor was provided
        vtkActor.newInstance({ parentProp: publicAPI }),
      ...initialValues._pipeline, // in case there is something else to add to pipeline
    },
    applyMixin: {
      origin: initialValues.applyMixin?.origin ?? origin(publicAPI, model),
      noPosition:
        initialValues.applyMixin?.noPosition ?? noPosition(publicAPI, model),
      color3: initialValues.applyMixin?.color3 ?? color3(publicAPI, model),
      color: initialValues.applyMixin?.color ?? color(publicAPI, model),
      noColor: initialValues.applyMixin?.noColor ?? noColor(publicAPI, model),
      scale3: initialValues.applyMixin?.scale3 ?? scale3(publicAPI, model),
      scale1: initialValues.applyMixin?.scale1 ?? scale1(publicAPI, model),
      noScale: initialValues.applyMixin?.noScale ?? noScale(publicAPI, model),
      direction:
        initialValues.applyMixin?.direction ?? direction(publicAPI, model),
      noOrientation:
        initialValues.applyMixin?.noOrientation ??
        noOrientation(publicAPI, model),
      ...initialValues.applyMixin,
    },
  };
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  if (initialValues.behavior === Behavior.CONTEXT) {
    vtkContextRepresentation.extend(
      publicAPI,
      model,
      defaultValues(publicAPI, model, initialValues)
    );
  } else {
    vtkHandleRepresentation.extend(
      publicAPI,
      model,
      defaultValues(publicAPI, model, initialValues)
    );
  }
  if ('lighting' in initialValues) {
    model._pipeline.actor.getProperty().setLighting(initialValues.lighting);
  }
 
  macro.setGet(publicAPI, model._pipeline, ['defaultScale']);
  macro.get(publicAPI, model._pipeline, ['glyph', 'mapper', 'actor']);
  // Expose the mixin functions to allow overwriting
  macro.setGet(publicAPI, model.applyMixin, Object.keys(model.applyMixin));
 
  // Object specific methods
  vtkGlyphRepresentation(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkGlyphRepresentation');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };