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

85.39% Statements 152/178
78.37% Branches 58/74
37.5% Functions 6/16
85.05% Lines 148/174

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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367                1x 1x               25x                         25x   25x   25x   25x   25x 85x 85x     85x 59x   26x     25x   25x   25x   25x   25x 85x 85x     85x 6x   79x     25x 2x 2x 2x         2x 2x     25x   103x 103x 103x         85x 85x 85x 85x 85x 81x 81x     85x                         85x     85x 85x 85x 85x 85x 85x 85x 85x   85x   85x   85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 434x 434x 434x 434x 434x   434x 290x 290x     48x           48x 48x                 242x         242x 242x 242x 242x 242x 242x   242x             434x 434x 434x 434x   434x 434x   386x 386x   386x 386x 386x 386x   48x 144x   48x 48x 48x 48x           434x 22x   434x 22x   434x 22x   434x       434x 3472x 3472x 56x   3472x 42x   3472x 116x   3472x 201x   3472x 286x   3472x 155x       434x 434x 434x 434x       85x               85x 85x         85x 85x 85x   81x 81x     85x       25x                                           25x               1x                               25x     25x 25x   25x 25x   25x 25x   25x                   25x               25x         1x          
import { mat3, mat4, vec3 } from 'gl-matrix';
 
import Constants from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper/Constants';
import macro from 'vtk.js/Sources/macros';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
 
const { OrientationModes, ScaleModes } = Constants;
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// class methods
// ----------------------------------------------------------------------------
 
function vtkGlyph3DMapper(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkGlyph3DMapper');
 
  /**
   * An orientation array is a vtkDataArray with 3 components. The first
   * component is the angle of rotation along the X axis. The second
   * component is the angle of rotation along the Y axis. The third
   * component is the angle of rotation along the Z axis. Orientation is
   * specified in X,Y,Z order but the rotations are performed in Z,X an Y.
   * This definition is compliant with SetOrientation method on vtkProp3D.
   * By using vector or normal there is a degree of freedom or rotation
   * left (underconstrained). With the orientation array, there is no degree of
   * freedom left.
   */
  publicAPI.getOrientationModeAsString = () =>
    macro.enumToString(OrientationModes, model.orientationMode);
  publicAPI.setOrientationModeToDirection = () =>
    publicAPI.setOrientationMode(OrientationModes.DIRECTION);
  publicAPI.setOrientationModeToRotation = () =>
    publicAPI.setOrientationMode(OrientationModes.ROTATION);
  publicAPI.setOrientationModeToMatrix = () =>
    publicAPI.setOrientationMode(OrientationModes.MATRIX);
  publicAPI.getOrientationArrayData = () => {
    const idata = publicAPI.getInputData(0);
    Iif (!idata || !idata.getPointData()) {
      return null;
    }
    if (!model.orientationArray) {
      return idata.getPointData().getVectors();
    }
    return idata.getPointData().getArray(model.orientationArray);
  };
 
  publicAPI.getScaleModeAsString = () =>
    macro.enumToString(ScaleModes, model.scaleMode);
  publicAPI.setScaleModeToScaleByMagnitude = () =>
    publicAPI.setScaleMode(ScaleModes.SCALE_BY_MAGNITUDE);
  publicAPI.setScaleModeToScaleByComponents = () =>
    publicAPI.setScaleMode(ScaleModes.SCALE_BY_COMPONENTS);
  publicAPI.setScaleModeToScaleByConstant = () =>
    publicAPI.setScaleMode(ScaleModes.SCALE_BY_CONSTANT);
  publicAPI.getScaleArrayData = () => {
    const idata = publicAPI.getInputData(0);
    Iif (!idata || !idata.getPointData()) {
      return null;
    }
    if (!model.scaleArray) {
      return idata.getPointData().getScalars();
    }
    return idata.getPointData().getArray(model.scaleArray);
  };
 
  publicAPI.getBounds = () => {
    const idata = publicAPI.getInputData(0);
    const gdata = publicAPI.getInputData(1);
    Iif (!idata || !gdata) {
      return vtkMath.createUninitializedBounds();
    }
 
    // first we build the arrays used for the glyphing
    publicAPI.buildArrays();
    return model.bounds;
  };
 
  publicAPI.buildArrays = () => {
    // if the mtgime requires it, rebuild
    const idata = publicAPI.getInputData(0);
    const gdata = publicAPI.getInputData(1);
    if (
      model.buildTime.getMTime() < gdata.getMTime() ||
      model.buildTime.getMTime() < idata.getMTime() ||
      model.buildTime.getMTime() < publicAPI.getMTime()
    ) {
      const pts = idata.getPoints().getData();
      let sArray = publicAPI.getScaleArrayData();
      let sData = null;
      let numSComp = 0;
      if (sArray) {
        sData = sArray.getData();
        numSComp = sArray.getNumberOfComponents();
      }
 
      Iif (
        model.scaling &&
        sArray &&
        model.scaleMode === ScaleModes.SCALE_BY_COMPONENTS &&
        sArray.getNumberOfComponents() !== 3
      ) {
        vtkErrorMacro(
          'Cannot scale by components since scale array does not have 3 components.'
        );
        sArray = null;
      }
 
      // get the glyph bounds
      const gbounds = gdata.getBounds();
      // convert them to 8 points so we can compute the
      // overall bounds while building the arrays
      const corners = [];
      vtkBoundingBox.getCorners(gbounds, corners);
      model.bounds[0] = vtkBoundingBox.INIT_BOUNDS[0];
      model.bounds[1] = vtkBoundingBox.INIT_BOUNDS[1];
      model.bounds[2] = vtkBoundingBox.INIT_BOUNDS[2];
      model.bounds[3] = vtkBoundingBox.INIT_BOUNDS[3];
      model.bounds[4] = vtkBoundingBox.INIT_BOUNDS[4];
      model.bounds[5] = vtkBoundingBox.INIT_BOUNDS[5];
 
      const tcorner = new Float64Array(3);
 
      const oArray = publicAPI.getOrientationArrayData();
 
      const identity = mat4.identity(new Float64Array(16));
      const trans = [];
      const scale = [];
      const numPts = pts.length / 3;
      model.matrixArray = new Float32Array(16 * numPts);
      const mbuff = model.matrixArray.buffer;
      model.normalArray = new Float32Array(9 * numPts);
      const nbuff = model.normalArray.buffer;
      const tuple = [];
      const orientation = [];
      for (let i = 0; i < numPts; ++i) {
        const z = new Float32Array(mbuff, i * 64, 16);
        trans[0] = pts[i * 3];
        trans[1] = pts[i * 3 + 1];
        trans[2] = pts[i * 3 + 2];
        mat4.translate(z, identity, trans);
 
        if (oArray) {
          oArray.getTuple(i, orientation);
          switch (model.orientationMode) {
            case OrientationModes.MATRIX: {
              // prettier-ignore
              const rotMat4 = [
                ...orientation.slice(0, 3), 0,
                ...orientation.slice(3, 6), 0,
                ...orientation.slice(6, 9), 0,
                0, 0, 0, 1,
              ];
              mat4.multiply(z, z, rotMat4);
              break;
            }
            case OrientationModes.ROTATION:
              mat4.rotateZ(z, z, orientation[2]);
              mat4.rotateX(z, z, orientation[0]);
              mat4.rotateY(z, z, orientation[1]);
              break;
 
            case OrientationModes.DIRECTION:
              Iif (orientation[1] === 0.0 && orientation[2] === 0.0) {
                if (orientation[0] < 0) {
                  mat4.rotateY(z, z, 3.1415926);
                }
              } else {
                const vMag = vtkMath.norm(orientation);
                const vNew = [];
                vNew[0] = (orientation[0] + vMag) / 2.0;
                vNew[1] = orientation[1] / 2.0;
                vNew[2] = orientation[2] / 2.0;
                mat4.rotate(z, z, 3.1415926, vNew);
              }
              break;
            default:
              break;
          }
        }
 
        // scale data if appropriate
        if (model.scaling) {
          scale[0] = model.scaleFactor;
          scale[1] = model.scaleFactor;
          scale[2] = model.scaleFactor;
          // Get the scalar and vector data
          if (sArray) {
            switch (model.scaleMode) {
              case ScaleModes.SCALE_BY_MAGNITUDE:
                for (let t = 0; t < numSComp; ++t) {
                  tuple[t] = sData[i * numSComp + t];
                }
                scale[0] *= vtkMath.norm(tuple, numSComp);
                scale[1] = scale[0];
                scale[2] = scale[0];
                break;
              case ScaleModes.SCALE_BY_COMPONENTS:
                for (let t = 0; t < numSComp; ++t) {
                  tuple[t] = sData[i * numSComp + t];
                }
                scale[0] *= tuple[0];
                scale[1] *= tuple[1];
                scale[2] *= tuple[2];
                break;
              case ScaleModes.SCALE_BY_CONSTANT:
              default:
                break;
            }
          }
          if (scale[0] === 0.0) {
            scale[0] = 1.0e-10;
          }
          if (scale[1] === 0.0) {
            scale[1] = 1.0e-10;
          }
          if (scale[2] === 0.0) {
            scale[2] = 1.0e-10;
          }
          mat4.scale(z, z, scale);
        }
 
        // update bounds
        for (let p = 0; p < 8; ++p) {
          vec3.transformMat4(tcorner, corners[p], z);
          if (tcorner[0] < model.bounds[0]) {
            model.bounds[0] = tcorner[0];
          }
          if (tcorner[1] < model.bounds[2]) {
            model.bounds[2] = tcorner[1];
          }
          if (tcorner[2] < model.bounds[4]) {
            model.bounds[4] = tcorner[2];
          }
          if (tcorner[0] > model.bounds[1]) {
            model.bounds[1] = tcorner[0];
          }
          if (tcorner[1] > model.bounds[3]) {
            model.bounds[3] = tcorner[1];
          }
          if (tcorner[2] > model.bounds[5]) {
            model.bounds[5] = tcorner[2];
          }
        }
 
        const n = new Float32Array(nbuff, i * 36, 9);
        mat3.fromMat4(n, z);
        mat3.invert(n, n);
        mat3.transpose(n, n);
      }
 
      // map scalars as well
      const scalars = publicAPI.getAbstractScalars(
        idata,
        model.scalarMode,
        model.arrayAccessMode,
        model.arrayId,
        model.colorByArrayName
      ).scalars;
 
      if (!model.useLookupTableScalarRange) {
        publicAPI
          .getLookupTable()
          .setRange(model.scalarRange[0], model.scalarRange[1]);
      }
 
      model.colorArray = null;
      const lut = publicAPI.getLookupTable();
      if (lut && scalars) {
        // Ensure that the lookup table is built
        lut.build();
        model.colorArray = lut.mapScalars(scalars, model.colorMode, 0);
      }
 
      model.buildTime.modified();
    }
  };
 
  publicAPI.getPrimitiveCount = () => {
    const glyph = publicAPI.getInputData(1);
    const mult = publicAPI.getInputData().getPoints().getNumberOfValues() / 3;
 
    const pcount = {
      points: (mult * glyph.getPoints().getNumberOfValues()) / 3,
      verts:
        mult *
        (glyph.getVerts().getNumberOfValues() -
          glyph.getVerts().getNumberOfCells()),
      lines:
        mult *
        (glyph.getLines().getNumberOfValues() -
          2 * glyph.getLines().getNumberOfCells()),
      triangles:
        mult *
        (glyph.getPolys().getNumberOfValues() -
          3 * glyph.getLines().getNumberOfCells()),
    };
    return pcount;
  };
 
  publicAPI.setSourceConnection = (outputPort) =>
    publicAPI.setInputConnection(outputPort, 1);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  orient: true,
  orientationMode: OrientationModes.DIRECTION,
  orientationArray: null,
  scaling: true,
  scaleFactor: 1.0,
  scaleMode: ScaleModes.SCALE_BY_MAGNITUDE,
  scaleArray: null,
  matrixArray: null,
  normalArray: null,
  colorArray: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkMapper.extend(publicAPI, model, initialValues);
  macro.algo(publicAPI, model, 2, 0);
 
  model.buildTime = {};
  macro.obj(model.buildTime, { mtime: 0 });
 
  model.boundsTime = {};
  macro.obj(model.boundsTime, { mtime: 0 });
 
  macro.setGet(publicAPI, model, [
    'orient',
    'orientationMode',
    'orientationArray',
    'scaleArray',
    'scaleFactor',
    'scaleMode',
    'scaling',
  ]);
 
  macro.get(publicAPI, model, [
    'colorArray',
    'matrixArray',
    'normalArray',
    'buildTime',
  ]);
 
  // Object methods
  vtkGlyph3DMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkGlyph3DMapper');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };