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

77.61% Statements 52/67
30% Branches 3/10
46.15% Functions 6/13
81.25% Lines 52/64

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                      12x 12x         12x                                             12x 12x 12x   12x 3612x 3612x 3612x     12x                             4x   4x 4x   4x 4x         4x     4x 4x       4x   4x       4x     4x 4x       4x   4x       4x     4x 4x       4x   4x 4x 4x 4x   4x     4x 4x   4x     4x     4x     4x   4x   4x   4x 4x 4x 4x               4x                                                                 4x   4x     4x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkArrowSource from 'vtk.js/Sources/Filters/Sources/ArrowSource';
import vtkAppendPolyData from 'vtk.js/Sources/Filters/General/AppendPolyData';
 
// ----------------------------------------------------------------------------
 
function centerDataSet(ds) {
  const bounds = ds.getPoints().getBounds();
  const center = [
    -(bounds[0] + bounds[1]) * 0.5,
    -(bounds[2] + bounds[3]) * 0.5,
    -(bounds[4] + bounds[5]) * 0.5,
  ];
  vtkMatrixBuilder
    .buildFromDegree()
    .translate(...center)
    .apply(ds.getPoints().getData());
}
 
function shiftDataset(ds, axis, invert = false) {
  const bounds = ds.getPoints().getBounds();
  const center = [0, 0, 0];
  if (invert) {
    center[axis] = -bounds[axis * 2 + 1];
  } else {
    center[axis] = -bounds[axis * 2];
  }
  vtkMatrixBuilder
    .buildFromDegree()
    .translate(...center)
    .apply(ds.getPoints().getData());
}
 
// ----------------------------------------------------------------------------
 
function addColor(ds, r, g, b) {
  const size = ds.getPoints().getData().length;
  const rgbArray = new Uint8ClampedArray(size);
  let offset = 0;
 
  while (offset < size) {
    rgbArray[offset++] = r;
    rgbArray[offset++] = g;
    rgbArray[offset++] = b;
  }
 
  ds.getPointData().setScalars(
    vtkDataArray.newInstance({
      name: 'color',
      numberOfComponents: 3,
      values: rgbArray,
    })
  );
}
 
// ----------------------------------------------------------------------------
// vtkAxesActor
// ----------------------------------------------------------------------------
 
function vtkAxesActor(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkAxesActor');
 
  const _mapper = vtkMapper.newInstance();
  publicAPI.setMapper(_mapper);
 
  publicAPI.update = () => {
    let currentConfig = {
      ...model.config,
      ...model.xConfig,
    };
 
    const xAxis = vtkArrowSource
      .newInstance({ direction: [1, 0, 0], ...currentConfig })
      .getOutputData();
    if (model.config.recenter) {
      centerDataSet(xAxis);
    } else E{
      shiftDataset(xAxis, 0, currentConfig.invert);
    }
    addColor(xAxis, ...currentConfig.color);
 
    currentConfig = {
      ...model.config,
      ...model.yConfig,
    };
    const yAxis = vtkArrowSource
      .newInstance({ direction: [0, 1, 0], ...currentConfig })
      .getOutputData();
    if (model.config.recenter) {
      centerDataSet(yAxis);
    } else E{
      shiftDataset(yAxis, 1, currentConfig.invert);
    }
    addColor(yAxis, ...currentConfig.color);
 
    currentConfig = {
      ...model.config,
      ...model.zConfig,
    };
    const zAxis = vtkArrowSource
      .newInstance({ direction: [0, 0, 1], ...currentConfig })
      .getOutputData();
    if (model.config.recenter) {
      centerDataSet(zAxis);
    } else E{
      shiftDataset(zAxis, 2, currentConfig.invert);
    }
    addColor(zAxis, ...currentConfig.color);
 
    const source = vtkAppendPolyData.newInstance();
    source.setInputData(xAxis);
    source.addInputData(yAxis);
    source.addInputData(zAxis);
 
    _mapper.setInputConnection(source.getOutputPort());
  };
 
  publicAPI.update();
  const _debouncedUpdate = macro.debounce(publicAPI.update, 0);
 
  publicAPI.setXAxisColor = (color) =>
    publicAPI.setXConfig({ ...publicAPI.getXConfig(), color });
 
  publicAPI.setYAxisColor = (color) =>
    publicAPI.setYConfig({ ...publicAPI.getYConfig(), color });
 
  publicAPI.setZAxisColor = (color) =>
    publicAPI.setZConfig({ ...publicAPI.getZConfig(), color });
 
  publicAPI.getXAxisColor = () => model.getXConfig().color;
 
  publicAPI.getYAxisColor = () => model.getYConfig().color;
 
  publicAPI.getZAxisColor = () => model.getZConfig().color;
 
  model._onConfigChanged = _debouncedUpdate;
  model._onXConfigChanged = _debouncedUpdate;
  model._onYConfigChanged = _debouncedUpdate;
  model._onZConfigChanged = _debouncedUpdate;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
function defaultValues(initialValues) {
  return {
    config: {
      recenter: true,
      tipResolution: 60,
      tipRadius: 0.1,
      tipLength: 0.2,
      shaftResolution: 60,
      shaftRadius: 0.03,
      invert: false,
      ...initialValues?.config,
    },
    xConfig: {
      color: [255, 0, 0],
      invert: false,
      ...initialValues?.xConfig,
    },
    yConfig: {
      color: [255, 255, 0],
      invert: false,
      ...initialValues?.yConfig,
    },
    zConfig: {
      color: [0, 128, 0],
      invert: false,
      ...initialValues?.zConfig,
    },
  };
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  // Inheritance
  vtkActor.extend(publicAPI, model, defaultValues(initialValues));
 
  macro.setGet(publicAPI, model, ['config', 'xConfig', 'yConfig', 'zConfig']);
 
  // Object methods
  vtkAxesActor(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkAxesActor');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };