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

62.88% Statements 144/229
45.52% Branches 56/123
38.46% Functions 5/13
63.67% Lines 135/212

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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450                      2x       1x         2x           2x                 2x 2x 2x       2x         2x         2x 1x   1x 1x 1x     1x 1x 1x   1x     1x 1x       2x       1x         1x   1x   1x 1x     1x 1x     1x 1x   1x 1x 1x 1x 1x   1x             1x   1x     1x                       1x                                       1x                               1x 2x 2x     2x 2x     2x 1x 1x       2x         1x 1x             1x 1x     1x 1x     1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 3x   3x 120x 120x 120x 120x 120x       3x 120x 120x 120x 120x 120x         1x     1x                                                                                                             1x   2x             2x 2x 2x 2x     2x 80x 80x 80x 80x 80x   80x       2x 80x 80x   80x 80x 80x     80x       2x 1x 40x 40x 40x 40x   40x   40x         2x         1x 40x 40x 40x 40x 40x     40x       2x                                       1x           1x 1x 1x 1x         1x       2x             1x                                 2x     2x 2x             2x 2x 2x 2x         1x                
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
 
// ----------------------------------------------------------------------------
// vtkConcentricCylinderSource methods
// ----------------------------------------------------------------------------
 
function vtkConcentricCylinderSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkConcentricCylinderSource');
 
  // Internal private function
  function validateCellFields() {
    while (model.cellFields.length < model.radius.length) {
      model.cellFields.push(model.cellFields.length);
    }
  }
 
  publicAPI.clearRadius = () => {
    model.radius = [];
    model.cellFields = [];
    publicAPI.modified();
  };
 
  publicAPI.addRadius = (radius, cellField) => {
    model.radius.push(radius);
    if (cellField !== undefined) {
      model.cellFields.push(cellField);
    }
    validateCellFields();
    publicAPI.modified();
  };
 
  publicAPI.getNumberOfRadius = () => model.radius.length;
  publicAPI.getRadius = (index = 0) => model.radius[index];
  publicAPI.setRadius = (index, radius) => {
    model.radius[index] = radius;
    publicAPI.modified();
  };
  publicAPI.setCellField = (index, field) => {
    model.cellFields[index] = field;
    publicAPI.modified();
  };
 
  publicAPI.removeMask = () => {
    model.mask = null;
    publicAPI.modified();
  };
 
  publicAPI.setMaskLayer = (index, hidden) => {
    let changeDetected = false;
 
    if (!model.mask && hidden) {
      changeDetected = true;
      model.mask = [];
    }
 
    if (model.mask) {
      if (!model.mask[index] !== !hidden) {
        changeDetected = true;
      }
      model.mask[index] = hidden;
    }
 
    if (changeDetected) {
      publicAPI.modified();
    }
  };
 
  publicAPI.getMaskLayer = (index) =>
    index === undefined ? model.mask : model.mask[index];
 
  function requestData(inData, outData) {
    Iif (model.deleted || !model.radius.length) {
      return;
    }
 
    // Make sure we have consistency
    validateCellFields();
 
    let dataset = outData[0];
 
    const numLayers = model.radius.length;
    const zRef = model.height / 2.0;
 
    // Compute cell count
    let cellArraySize = 0;
    let numCells = 0;
 
    let startTheta =
      model.startTheta < model.endTheta ? model.startTheta : model.endTheta;
    startTheta *= Math.PI / 180.0;
    let endTheta =
      model.endTheta > model.startTheta ? model.endTheta : model.startTheta;
    endTheta *= Math.PI / 180.0;
    let thetaResolution = model.resolution;
    let partialDisk = false;
    if (endTheta >= startTheta + 2 * Math.PI) {
      // complete, closed cylinder
      endTheta = startTheta + 2 * Math.PI;
    } else E{
      // We add an extra point at endTheta, since the cylinder isn't closed.
      // We cap the sides of the partial cylinder, so set the partialDisk flag.
      ++thetaResolution;
      partialDisk = true;
    }
    const deltaTheta = (endTheta - startTheta) / model.resolution;
 
    const numberOfPoints = thetaResolution * numLayers * 2 + 2;
 
    // 5 entries per poly, 4 polys (top, bottom, in, out) per resolution
    Iif (!model.skipInnerFaces && !model.mask) {
      // We keep everything
      cellArraySize =
        2 * (thetaResolution + 1) +
        5 * thetaResolution +
        (numLayers - 1) * thetaResolution * 20 +
        (partialDisk ? 10 * numLayers : 0);
      numCells =
        2 +
        thetaResolution +
        (numLayers - 1) * 4 * thetaResolution +
        (partialDisk ? 2 * numLayers : 0);
    } else Iif (!model.skipInnerFaces && model.mask) {
      // We skip some cylinders
      // Handle core
      if (!model.mask[0]) {
        cellArraySize +=
          2 * (thetaResolution + 1) +
          5 * thetaResolution +
          (partialDisk ? 10 : 0);
        numCells += 2 + thetaResolution + (partialDisk ? 2 : 0);
      }
      // Handle inside cylinders
      for (let layer = 1; layer < numLayers; layer++) {
        if (!model.mask[layer]) {
          // Add inside cylinder count
          cellArraySize += thetaResolution * 20 + (partialDisk ? 10 : 0);
          numCells += 4 * thetaResolution + (partialDisk ? 2 : 0);
        }
      }
    } else {
      // We skip cylinders and internal faces
      Iif (!model.skipInnerFaces || !model.mask || !model.mask[0]) {
        // core handling
        cellArraySize += 2 * (thetaResolution + 1) + (partialDisk ? 10 : 0);
        numCells += 2 + (partialDisk ? 2 : 0);
        if (
          model.radius.length === 1 ||
          !model.skipInnerFaces ||
          (model.mask && model.mask[1])
        ) {
          // add side faces
          cellArraySize += 5 * thetaResolution;
          numCells += thetaResolution;
        }
      }
 
      // Handle inside cylinders
      for (let layer = 1; layer < numLayers; layer++) {
        if (!model.skipInnerFaces || !model.mask || !model.mask[layer]) {
          const lastLayer = numLayers - 1 === layer;
 
          // Add inside cylinder
          cellArraySize += thetaResolution * 10 + (partialDisk ? 10 : 0);
          numCells += thetaResolution * 2 + (partialDisk ? 2 : 0); // top + bottom + side caps
 
          // Do we add innerFaces
          if (!model.skipInnerFaces || (model.mask && model.mask[layer - 1])) {
            cellArraySize += thetaResolution * 5;
            numCells += thetaResolution;
          }
 
          // Do we add outerFaces
          if (
            lastLayer ||
            !model.skipInnerFaces ||
            (model.mask && model.mask[layer + 1])
          ) {
            cellArraySize += thetaResolution * 5;
            numCells += thetaResolution;
          }
        }
      }
    }
 
    // Points
    let pointIdx = 0;
    const points = macro.newTypedArray(model.pointType, numberOfPoints * 3);
 
    // Cells
    let cellLocation = 0;
    const polys = new Uint32Array(cellArraySize);
 
    // CellFields
    let fieldLocation = 0;
    const field = new Float32Array(numCells);
 
    // Create points
    // First two are centered, top and bottom. Used only if partialDisk is true.
    points[pointIdx * 3 + 0] = 0;
    points[pointIdx * 3 + 1] = 0;
    points[pointIdx * 3 + 2] = zRef;
    pointIdx++;
    points[pointIdx * 3 + 0] = 0;
    points[pointIdx * 3 + 1] = 0;
    points[pointIdx * 3 + 2] = -zRef;
    pointIdx++;
    for (let layer = 0; layer < numLayers; layer++) {
      const radius = model.radius[layer];
      // Create top
      for (let i = 0; i < thetaResolution; i++) {
        const theta = startTheta + i * deltaTheta;
        points[pointIdx * 3 + 0] = radius * Math.cos(theta);
        points[pointIdx * 3 + 1] = radius * Math.sin(theta);
        points[pointIdx * 3 + 2] = zRef;
        pointIdx++;
      }
 
      // Create bottom
      for (let i = 0; i < thetaResolution; i++) {
        const theta = startTheta + i * deltaTheta;
        points[pointIdx * 3 + 0] = radius * Math.cos(theta);
        points[pointIdx * 3 + 1] = radius * Math.sin(theta);
        points[pointIdx * 3 + 2] = -zRef;
        pointIdx++;
      }
    }
 
    // Create cells for the core
    let currentField = model.cellFields[0];
 
    // Core: filtering
    Iif (!model.mask || !model.mask[0]) {
      // Core: Top disk
      field[fieldLocation++] = currentField;
      // partial adds the center point and the last point.
      polys[cellLocation++] = thetaResolution + (partialDisk ? 1 : 0);
      if (partialDisk) polys[cellLocation++] = 0;
      for (let i = 0; i < thetaResolution; i++) {
        polys[cellLocation++] = i + 2;
      }
 
      // Core: Bottom disk
      field[fieldLocation++] = currentField;
      polys[cellLocation++] = thetaResolution + (partialDisk ? 1 : 0);
      if (partialDisk) polys[cellLocation++] = 1;
      for (let i = 0; i < thetaResolution; i++) {
        polys[cellLocation++] = 2 * thetaResolution - i - 1 + 2;
      }
 
      // Core: sides
      if (
        !model.skipInnerFaces ||
        (model.mask && model.mask[1]) ||
        numLayers === 1
      ) {
        for (let i = 0; i < model.resolution; i++) {
          polys[cellLocation++] = 4;
          polys[cellLocation++] = ((i + 1) % thetaResolution) + 2;
          polys[cellLocation++] = i + 2;
          polys[cellLocation++] = i + thetaResolution + 2;
          polys[cellLocation++] =
            ((i + 1) % thetaResolution) + thetaResolution + 2;
 
          field[fieldLocation++] = currentField;
        }
      }
      if (partialDisk) {
        polys[cellLocation++] = 4;
        polys[cellLocation++] = 2;
        polys[cellLocation++] = 0;
        polys[cellLocation++] = 1;
        polys[cellLocation++] = thetaResolution + 2;
 
        field[fieldLocation++] = currentField;
 
        polys[cellLocation++] = 4;
        polys[cellLocation++] = 0;
        polys[cellLocation++] = thetaResolution + 1;
        polys[cellLocation++] = 2 * thetaResolution + 1;
        polys[cellLocation++] = 1;
 
        field[fieldLocation++] = currentField;
      }
    }
 
    // Create cells for the layers
    for (let layer = 1; layer < numLayers; layer++) {
      // Skip layer if masked
      Iif (model.mask && model.mask[layer]) {
        /* eslint-disable no-continue */
        continue;
        /* eslint-enable no-continue */
      }
 
      // two for center points, then skip previous layer's points
      const offset = thetaResolution * 2 * (layer - 1) + 2;
      const a = offset + 2 * thetaResolution; // next layer offset
      const lastLayer = numLayers - 1 === layer;
      currentField = model.cellFields[layer];
 
      // Create top
      for (let i = 0; i < model.resolution; i++) {
        polys[cellLocation++] = 4;
        polys[cellLocation++] = i + offset;
        polys[cellLocation++] = ((i + 1) % thetaResolution) + offset;
        polys[cellLocation++] = ((i + 1) % thetaResolution) + a;
        polys[cellLocation++] = i + a;
 
        field[fieldLocation++] = currentField;
      }
 
      // Create bottom
      for (let i = 0; i < model.resolution; i++) {
        polys[cellLocation++] = 4;
        polys[cellLocation++] =
          ((i + 1) % thetaResolution) + offset + thetaResolution;
        polys[cellLocation++] = i + offset + thetaResolution;
        polys[cellLocation++] = i + a + thetaResolution;
        polys[cellLocation++] =
          ((i + 1) % thetaResolution) + a + thetaResolution;
 
        field[fieldLocation++] = currentField;
      }
 
      // Create inner
      if (!model.skipInnerFaces || (model.mask && model.mask[layer - 1])) {
        for (let i = 0; i < model.resolution; i++) {
          polys[cellLocation++] = 4;
          polys[cellLocation++] = i + offset;
          polys[cellLocation++] = ((i + 1) % thetaResolution) + offset;
          polys[cellLocation++] =
            ((i + 1) % thetaResolution) + thetaResolution + offset;
          polys[cellLocation++] = i + thetaResolution + offset;
 
          field[fieldLocation++] = currentField;
        }
      }
 
      // Create outer
      if (
        !model.skipInnerFaces ||
        lastLayer ||
        (model.mask && (model.mask[layer + 1] || lastLayer))
      ) {
        for (let i = 0; i < model.resolution; i++) {
          polys[cellLocation++] = 4;
          polys[cellLocation++] = ((i + 1) % thetaResolution) + a;
          polys[cellLocation++] = i + a;
          polys[cellLocation++] = i + thetaResolution + a;
          polys[cellLocation++] =
            ((i + 1) % thetaResolution) + thetaResolution + a;
 
          field[fieldLocation++] = currentField;
        }
      }
      // create caps
      Iif (partialDisk) {
        polys[cellLocation++] = 4;
        polys[cellLocation++] = a; // from first outer
        polys[cellLocation++] = offset; // first inner
        polys[cellLocation++] = thetaResolution + offset; // first inner
        polys[cellLocation++] = thetaResolution + a; // first outer
 
        field[fieldLocation++] = currentField;
 
        polys[cellLocation++] = 4;
        polys[cellLocation++] = model.resolution + a; // last outer
        polys[cellLocation++] = model.resolution + offset; // last inner
        polys[cellLocation++] = model.resolution + thetaResolution + offset; // last inner
        polys[cellLocation++] = model.resolution + thetaResolution + a; // last outer
 
        field[fieldLocation++] = currentField;
      }
    }
 
    // Apply transformation to the point coordinates
    vtkMatrixBuilder
      .buildFromRadian()
      .translate(...model.center)
      .rotateFromDirections([0, 0, 1], model.direction)
      .apply(points);
 
    dataset = vtkPolyData.newInstance();
    dataset.getPoints().setData(points, 3);
    dataset.getPolys().setData(polys, 1);
    dataset
      .getCellData()
      .setScalars(vtkDataArray.newInstance({ name: 'layer', values: field }));
 
    // Update output
    outData[0] = dataset;
  }
 
  // Expose methods
  publicAPI.requestData = requestData;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  height: 1.0,
  radius: [0.5],
  cellFields: [1],
  resolution: 6,
  startTheta: 0.0,
  endTheta: 360.0,
  center: [0, 0, 0],
  direction: [0.0, 0.0, 1.0],
  skipInnerFaces: true,
  mask: null, // If present, array to know if a layer should be skipped(=true)
  pointType: 'Float64Array',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, [
    'height',
    'resolution',
    'startTheta',
    'endTheta',
    'skipInnerFaces',
  ]);
  macro.setGetArray(publicAPI, model, ['center', 'direction'], 3);
  macro.getArray(publicAPI, model, ['cellFields']);
  macro.algo(publicAPI, model, 0, 1);
  vtkConcentricCylinderSource(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkConcentricCylinderSource'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };