All files / Sources/Filters/Sources/CubeSource index.js

98.1% Statements 155/158
84% Branches 21/25
100% Functions 4/4
97.97% Lines 145/148

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              1x                               1x                             123x     112x       112x 112x   112x     112x 112x   112x 112x         112x   112x 112x 2x     112x       112x         112x   112x 112x 112x   112x   112x 112x 112x 112x 112x 224x   224x 448x 448x   448x 896x 896x 896x 896x   896x 896x 896x   896x 880x 880x   16x 16x 16x     896x   896x   448x   224x 224x     112x 112x 112x 112x 112x 224x   224x 448x 448x   448x 896x   896x 896x 896x   896x 896x 896x   896x 880x 880x   16x 16x 16x     896x 896x   448x   224x 224x     112x 112x 112x 112x 112x 224x   224x 448x 448x   448x 896x   896x 896x 896x   896x 896x 896x   896x 880x 880x   16x 16x 16x     896x 896x   448x   224x 224x       112x                     112x 95x             112x 12x     12x           12x       112x 111x   1x   112x 1x   1x   111x   112x     123x 91x   91x 90x   1x 6x       91x       91x 91x 91x 91x               123x             1x                         123x     123x 123x               123x 123x     123x     123x     123x   123x 123x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
 
// prettier-ignore
const LINE_ARRAY = [
  2, 0, 1,
  2, 2, 3,
  2, 4, 5,
  2, 6, 7,
  2, 0, 2,
  2, 1, 3,
  2, 4, 6,
  2, 5, 7,
  2, 0, 4,
  2, 1, 5,
  2, 2, 6,
  2, 3, 7,
];
 
// prettier-ignore
const POLY_ARRAY = [
  4, 0, 1, 3, 2,
  4, 4, 6, 7, 5,
  4, 8, 10, 11, 9,
  4, 12, 13, 15, 14,
  4, 16, 18, 19, 17,
  4, 20, 21, 23, 22,
];
 
// ----------------------------------------------------------------------------
// vtkCubeSource methods
// ----------------------------------------------------------------------------
 
function vtkCubeSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCubeSource');
 
  function requestData(inData, outData) {
    Iif (model.deleted) {
      return;
    }
 
    const polyData = vtkPolyData.newInstance();
    outData[0] = polyData;
 
    const numberOfPoints = 24;
 
    // Define points
    const points = macro.newTypedArray(model.pointType, numberOfPoints * 3);
    polyData.getPoints().setData(points, 3);
 
    const normals = macro.newTypedArray(model.pointType, numberOfPoints * 3);
    const normalArray = vtkDataArray.newInstance({
      name: 'Normals',
      values: normals,
      numberOfComponents: 3,
    });
    polyData.getPointData().setNormals(normalArray);
 
    let tcdim = 2;
    if (model.generate3DTextureCoordinates === true) {
      tcdim = 3;
    }
 
    const textureCoords = macro.newTypedArray(
      model.pointType,
      numberOfPoints * tcdim
    );
    const tcoords = vtkDataArray.newInstance({
      name: 'TextureCoordinates',
      values: textureCoords,
      numberOfComponents: tcdim,
    });
    polyData.getPointData().setTCoords(tcoords);
 
    const x = [0.0, 0.0, 0.0];
    const n = [0.0, 0.0, 0.0];
    const tc = [0.0, 0.0];
 
    let pointIndex = 0;
 
    x[0] = -model.xLength / 2.0;
    n[0] = -1.0;
    n[1] = 0.0;
    n[2] = 0.0;
    for (let i = 0; i < 2; i++) {
      x[1] = -model.yLength / 2.0;
 
      for (let j = 0; j < 2; j++) {
        tc[1] = x[1] + 0.5;
        x[2] = -model.zLength / 2.0;
 
        for (let k = 0; k < 2; k++) {
          tc[0] = (x[2] + 0.5) * (1 - 2 * i);
          points[pointIndex * 3] = x[0];
          points[pointIndex * 3 + 1] = x[1];
          points[pointIndex * 3 + 2] = x[2];
 
          normals[pointIndex * 3] = n[0];
          normals[pointIndex * 3 + 1] = n[1];
          normals[pointIndex * 3 + 2] = n[2];
 
          if (tcdim === 2) {
            textureCoords[pointIndex * tcdim] = tc[0];
            textureCoords[pointIndex * tcdim + 1] = tc[1];
          } else {
            textureCoords[pointIndex * tcdim] = 2 * i - 1;
            textureCoords[pointIndex * tcdim + 1] = 2 * j - 1;
            textureCoords[pointIndex * tcdim + 2] = 2 * k - 1;
          }
 
          pointIndex++;
 
          x[2] += model.zLength;
        }
        x[1] += model.yLength;
      }
      x[0] += model.xLength;
      n[0] += 2.0;
    }
 
    x[1] = -model.yLength / 2.0;
    n[1] = -1.0;
    n[0] = 0.0;
    n[2] = 0.0;
    for (let i = 0; i < 2; i++) {
      x[0] = -model.xLength / 2.0;
 
      for (let j = 0; j < 2; j++) {
        tc[0] = (x[0] + 0.5) * (2 * i - 1);
        x[2] = -model.zLength / 2.0;
 
        for (let k = 0; k < 2; k++) {
          tc[1] = (x[2] + 0.5) * -1;
 
          points[pointIndex * 3] = x[0];
          points[pointIndex * 3 + 1] = x[1];
          points[pointIndex * 3 + 2] = x[2];
 
          normals[pointIndex * 3] = n[0];
          normals[pointIndex * 3 + 1] = n[1];
          normals[pointIndex * 3 + 2] = n[2];
 
          if (tcdim === 2) {
            textureCoords[pointIndex * tcdim] = tc[0];
            textureCoords[pointIndex * tcdim + 1] = tc[1];
          } else {
            textureCoords[pointIndex * tcdim] = 2 * j - 1;
            textureCoords[pointIndex * tcdim + 1] = 2 * i - 1;
            textureCoords[pointIndex * tcdim + 2] = 2 * k - 1;
          }
 
          pointIndex++;
          x[2] += model.zLength;
        }
        x[0] += model.xLength;
      }
      x[1] += model.yLength;
      n[1] += 2.0;
    }
 
    x[2] = -model.zLength / 2.0;
    n[2] = -1.0;
    n[0] = 0.0;
    n[1] = 0.0;
    for (let i = 0; i < 2; i++) {
      x[1] = -model.yLength / 2.0;
 
      for (let j = 0; j < 2; j++) {
        tc[1] = x[1] + 0.5;
        x[0] = -model.xLength / 2.0;
 
        for (let k = 0; k < 2; k++) {
          tc[0] = (x[0] + 0.5) * (2 * i - 1);
 
          points[pointIndex * 3] = x[0];
          points[pointIndex * 3 + 1] = x[1];
          points[pointIndex * 3 + 2] = x[2];
 
          normals[pointIndex * 3] = n[0];
          normals[pointIndex * 3 + 1] = n[1];
          normals[pointIndex * 3 + 2] = n[2];
 
          if (tcdim === 2) {
            textureCoords[pointIndex * tcdim] = tc[0];
            textureCoords[pointIndex * tcdim + 1] = tc[1];
          } else {
            textureCoords[pointIndex * tcdim] = 2 * k - 1;
            textureCoords[pointIndex * tcdim + 1] = 2 * j - 1;
            textureCoords[pointIndex * tcdim + 2] = 2 * i - 1;
          }
 
          pointIndex++;
          x[0] += model.xLength;
        }
        x[1] += model.yLength;
      }
      x[2] += model.zLength;
      n[2] += 2.0;
    }
 
    // Apply rotation to the points coordinates and normals
    Iif (model.rotations) {
      vtkMatrixBuilder
        .buildFromDegree()
        .rotateX(model.rotations[0])
        .rotateY(model.rotations[1])
        .rotateZ(model.rotations[2])
        .apply(points)
        .apply(normals);
    }
 
    // Apply transformation to the points coordinates
    if (model.center) {
      vtkMatrixBuilder
        .buildFromRadian()
        .translate(...model.center)
        .apply(points);
    }
 
    // Apply optional additionally specified matrix transformation
    if (model.matrix) {
      vtkMatrixBuilder.buildFromRadian().setMatrix(model.matrix).apply(points);
 
      // prettier-ignore
      const rotMatrix = [
        model.matrix[0], model.matrix[1], model.matrix[2], 0,
        model.matrix[4], model.matrix[5], model.matrix[6], 0,
        model.matrix[8], model.matrix[9], model.matrix[10], 0,
        0, 0, 0, 1
      ];
      vtkMatrixBuilder.buildFromRadian().setMatrix(rotMatrix).apply(normals);
    }
 
    // Lastly, generate the necessary cell arrays.
    if (model.generateFaces) {
      polyData.getPolys().deepCopy(model._polys);
    } else {
      polyData.getPolys().initialize();
    }
    if (model.generateLines) {
      polyData.getLines().deepCopy(model._lineCells);
      // only set normals for faces, not for lines.
      polyData.getPointData().setNormals(null);
    } else {
      polyData.getLines().initialize();
    }
    polyData.modified();
  }
 
  publicAPI.setBounds = (...bounds) => {
    let boundsArray = [];
 
    if (Array.isArray(bounds[0])) {
      boundsArray = bounds[0];
    } else {
      for (let i = 0; i < bounds.length; i++) {
        boundsArray.push(bounds[i]);
      }
    }
 
    Iif (boundsArray.length !== 6) {
      return;
    }
 
    publicAPI.setXLength(boundsArray[1] - boundsArray[0]);
    publicAPI.setYLength(boundsArray[3] - boundsArray[2]);
    publicAPI.setZLength(boundsArray[5] - boundsArray[4]);
    publicAPI.setCenter([
      (boundsArray[0] + boundsArray[1]) / 2.0,
      (boundsArray[2] + boundsArray[3]) / 2.0,
      (boundsArray[4] + boundsArray[5]) / 2.0,
    ]);
  };
 
  // Expose methods
  publicAPI.requestData = requestData;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  xLength: 1.0,
  yLength: 1.0,
  zLength: 1.0,
  pointType: 'Float64Array',
  generate3DTextureCoordinates: false,
  generateFaces: true,
  generateLines: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, [
    'xLength',
    'yLength',
    'zLength',
    'generate3DTextureCoordinates',
    'generateFaces',
    'generateLines',
  ]);
  macro.setGetArray(publicAPI, model, ['center', 'rotations'], 3);
  macro.setGetArray(publicAPI, model, ['matrix'], 16);
 
  // Internal persistent/static objects
  model._polys = vtkCellArray.newInstance({
    values: Uint16Array.from(POLY_ARRAY),
  });
  model._lineCells = vtkCellArray.newInstance({
    values: Uint16Array.from(LINE_ARRAY),
  });
  macro.moveToProtected(publicAPI, model, ['polys', 'lineCells']);
 
  macro.algo(publicAPI, model, 0, 1);
  vtkCubeSource(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCubeSource');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };