All files / Sources/Filters/General/ClosedPolyLineToSurfaceFilter index.js

93.57% Statements 102/109
80% Branches 20/25
100% Functions 5/5
93.93% Lines 93/99

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        1x       4x 4x 4x       165x 165x 165x     165x 165x 165x 23x   4x 4x   4x 4x                 4x 4x 4x 4x         19x 19x 19x 19x 19x 19x 19x 19x 19x 19x     19x 19x 19x   142x 81x   42x 42x 372x     42x     39x   39x   39x 105x       81x 61x 19x   7x 7x 72x     7x     12x     12x   12x 44x       19x     42x 42x 42x 42x                     16x       16x   4x 4x         4x 4x     4x 4x 4x 4x 108x 108x 108x 216x   108x 108x       4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 108x     4x       4x               1x         16x     16x     16x     16x         1x                
import macro from 'vtk.js/Sources/macros';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
const { vtkErrorMacro } = macro;
 
class SegmentAgregator {
  constructor() {
    this.segmentMapping = {};
    this.segments = [null]; // to force first id to be 1
    this.faces = [];
  }
 
  addSegment(segment) {
    const first = segment[0];
    const last = segment[segment.length - 1];
    Iif (first === last || segment.length < 2) {
      return;
    }
    const mappingFirst = this.segmentMapping[first];
    const mappingLast = this.segmentMapping[last];
    if (mappingFirst !== undefined && mappingLast !== undefined) {
      if (Math.abs(mappingFirst) === Math.abs(mappingLast)) {
        // This make a closing loop
        const idx = mappingFirst < mappingLast ? mappingLast : mappingFirst;
        const seg = this.segments[idx];
 
        if (mappingFirst > 0) {
          for (let i = 1; i < segment.length - 1; i++) {
            seg.push(segment[i]);
          }
        } else E{
          for (let i = 1; i < segment.length - 1; i++) {
            seg.unshift(segment[segment.length - 1 - i]);
          }
        }
 
        this.faces.push(seg);
        this.segments[idx] = null;
        this.segmentMapping[first] = undefined;
        this.segmentMapping[last] = undefined;
      } else {
        // we need to merge segments
        // strategie:
        // => remove and add them again in special order to induce merge
        const idxHead = Math.abs(mappingFirst);
        const idxTail = Math.abs(mappingLast);
        const segHead = this.segments[idxHead];
        const segTail = this.segments[idxTail];
        this.segments[idxHead] = null;
        this.segments[idxTail] = null;
        this.segmentMapping[segHead[0]] = undefined;
        this.segmentMapping[segTail[0]] = undefined;
        this.segmentMapping[segHead[segHead.length - 1]] = undefined;
        this.segmentMapping[segTail[segTail.length - 1]] = undefined;
 
        // This will lead to a single segment
        this.addSegment(segment);
        this.addSegment(segHead);
        this.addSegment(segTail);
      }
    } else if (mappingFirst !== undefined) {
      if (mappingFirst > 0) {
        // The head of our segment match the tail of the existing one
        const seg = this.segments[mappingFirst];
        for (let i = 1; i < segment.length; i++) {
          seg.push(segment[i]);
        }
        // record new tail
        this.segmentMapping[last] = mappingFirst;
      } else {
        // our segment should be reverted and put on the front of the existing one
        const seg = this.segments[-mappingFirst];
        // record new head
        this.segmentMapping[last] = mappingFirst;
 
        for (let i = 1; i < segment.length; i++) {
          seg.unshift(segment[i]);
        }
      }
      // Erase used connection
      this.segmentMapping[first] = undefined;
    } else if (mappingLast !== undefined) {
      if (mappingLast > 0) {
        // The tail of our segment match the tail of the existing one
        const seg = this.segments[mappingLast];
        for (let i = 1; i < segment.length; i++) {
          seg.push(segment[segment.length - 1 - i]);
        }
        // record new tail
        this.segmentMapping[first] = mappingLast;
      } else {
        // our segment should be reverted and put on the front of the existing one
        const seg = this.segments[-mappingLast];
 
        // record new head
        this.segmentMapping[first] = mappingLast;
 
        for (let i = 1; i < segment.length; i++) {
          seg.unshift(segment[segment.length - i - 1]);
        }
      }
      // Erase used connection
      this.segmentMapping[last] = undefined;
    } else {
      // store segment for now
      const id = this.segments.length;
      this.segments.push(segment);
      this.segmentMapping[first] = -id;
      this.segmentMapping[last] = id;
    }
  }
}
 
// ----------------------------------------------------------------------------
// vtkClosedPolyLineToSurfaceFilter methods
// ----------------------------------------------------------------------------
 
function vtkClosedPolyLineToSurfaceFilter(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkClosedPolyLineToSurfaceFilter');
 
  // --------------------------------------------------------------------------
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
    Iif (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    const output = vtkPolyData.newInstance();
    output.shallowCopy(input);
 
    // Extract faces
    const agregator = new SegmentAgregator();
    const lines = input.getLines().getData();
    let offset = 0;
    while (offset < lines.length) {
      const lineSize = lines[offset++];
      const lineSegment = [];
      for (let i = 0; i < lineSize; i++) {
        lineSegment.push(lines[offset + i]);
      }
      agregator.addSegment(lineSegment);
      offset += lineSize;
    }
 
    // Create CellArray for polys
    const { faces } = agregator;
    let cellArraySize = faces.length;
    for (let i = 0; i < faces.length; i++) {
      cellArraySize += faces[i].length;
    }
    const cellArray = new Uint16Array(cellArraySize);
    offset = 0;
    for (let i = 0; i < faces.length; i++) {
      const face = faces[i];
      cellArray[offset++] = face.length;
      for (let j = 0; j < face.length; j++) {
        cellArray[offset++] = face[j];
      }
    }
    output.setPolys(
      vtkCellArray.newInstance({ values: cellArray, name: 'faces' })
    );
 
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  // Object specific methods
  vtkClosedPolyLineToSurfaceFilter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkClosedPolyLineToSurfaceFilter'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };