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

91.9% Statements 159/173
59.45% Branches 22/37
100% Functions 11/11
91.51% Lines 151/165

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      1x               3x   3x 3x 3x 3x 3x 3x 3x   3x 6996x 6996x 6996x   6996x 6996x 6996x   6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x     3x                   7012x       7012x 7012x 21036x 21036x   21036x 21036x   21036x 21036x 21036x     21036x             21036x                 21036x 304x         288x 288x 288x     20732x       21036x     7012x     3x 6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x     3x 7012x                     16x     6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x 6996x 55968x 55968x 167904x       6996x     3x       2340x       2340x 7020x     2340x 8x   2332x 6996x     2332x 8x     2324x     3x 16x   16x 16x 16x 16x 16x 16x   16x   16x 16x 2340x 16x   2324x 6972x       16x   16x 16x   16x   16x 16x 16x 16x 2324x   16x   16x   16x     3x   1x 1x   1x         1x         1x 1x   1x 1x 1x 1x 1x 16x 16x 16x 16x     1x 1x 1x 16x 16x   1x 1x 1x 1x 1x 16x 16x 16x 16x     1x 1x 1x   1x               1x               3x     3x     3x     3x     3x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkImageStreamline methods
// ----------------------------------------------------------------------------
 
function vtkImageStreamline(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageStreamline');
 
  const indices = new Int32Array(3);
  const paramCoords = new Float32Array(3);
  const weights = new Float32Array(8);
  const voxelIndices = new Uint32Array(8);
  const dimensions = new Uint32Array(3);
  const velAt = new Float32Array(3);
  const xtmp = new Float32Array(3);
 
  publicAPI.interpolationFunctions = (pcoords, sf) => {
    const r = pcoords[0];
    const s = pcoords[1];
    const t = pcoords[2];
 
    const rm = 1.0 - r;
    const sm = 1.0 - s;
    const tm = 1.0 - t;
 
    sf[0] = rm * sm * tm;
    sf[1] = r * sm * tm;
    sf[2] = rm * s * tm;
    sf[3] = r * s * tm;
    sf[4] = rm * sm * t;
    sf[5] = r * sm * t;
    sf[6] = rm * s * t;
    sf[7] = r * s * t;
  };
 
  publicAPI.computeStructuredCoordinates = (
    x,
    ijk,
    pcoords,
    extent,
    spacing,
    origin,
    bounds
  ) => {
    // tolerance is needed for 2D data (this is squared tolerance)
    const tol2 = 1e-12;
    //
    //  Compute the ijk location
    //
    let isInBounds = true;
    for (let i = 0; i < 3; i++) {
      const d = x[i] - origin[i];
      const doubleLoc = d / spacing[i];
      // Floor for negative indexes.
      ijk[i] = Math.floor(doubleLoc);
      pcoords[i] = doubleLoc - ijk[i];
 
      let tmpInBounds = false;
      const minExt = extent[i * 2];
      const maxExt = extent[i * 2 + 1];
 
      // check if data is one pixel thick
      Iif (minExt === maxExt) {
        const dist = x[i] - bounds[2 * i];
        if (dist * dist <= spacing[i] * spacing[i] * tol2) {
          pcoords[i] = 0.0;
          ijk[i] = minExt;
          tmpInBounds = true;
        }
      } else Iif (ijk[i] < minExt) {
        if (
          (spacing[i] >= 0 && x[i] >= bounds[i * 2]) ||
          (spacing[i] < 0 && x[i] <= bounds[i * 2 + 1])
        ) {
          pcoords[i] = 0.0;
          ijk[i] = minExt;
          tmpInBounds = true;
        }
      } else if (ijk[i] >= maxExt) {
        if (
          (spacing[i] >= 0 && x[i] <= bounds[i * 2 + 1]) ||
          (spacing[i] < 0 && x[i] >= bounds[i * 2])
        ) {
          // make sure index is within the allowed cell index range
          pcoords[i] = 1.0;
          ijk[i] = maxExt - 1;
          tmpInBounds = true;
        }
      } else {
        tmpInBounds = true;
      }
 
      // clear isInBounds if out of bounds for this dimension
      isInBounds = isInBounds && tmpInBounds;
    }
 
    return isInBounds;
  };
 
  publicAPI.getVoxelIndices = (ijk, dims, ids) => {
    ids[0] = ijk[2] * dims[0] * dims[1] + ijk[1] * dims[0] + ijk[0];
    ids[1] = ids[0] + 1; // i+1, j, k
    ids[2] = ids[0] + dims[0]; // i, j+1, k
    ids[3] = ids[2] + 1; // i+1, j+1, k
    ids[4] = ids[0] + dims[0] * dims[1]; // i, j, k+1
    ids[5] = ids[4] + 1; // i+1, j, k+1
    ids[6] = ids[4] + dims[0]; // i, j+1, k+1
    ids[7] = ids[6] + 1; // i+1, j+1, k+1
  };
 
  publicAPI.vectorAt = (xyz, velArray, image, velAtArg) => {
    if (
      !publicAPI.computeStructuredCoordinates(
        xyz,
        indices,
        paramCoords,
        image.getExtent(),
        image.getSpacing(),
        image.getOrigin(),
        image.getBounds()
      )
    ) {
      return false;
    }
 
    publicAPI.interpolationFunctions(paramCoords, weights);
    const extent = image.getExtent();
    dimensions[0] = extent[1] - extent[0] + 1;
    dimensions[1] = extent[3] - extent[2] + 1;
    dimensions[2] = extent[5] - extent[4] + 1;
    publicAPI.getVoxelIndices(indices, dimensions, voxelIndices);
    velAtArg[0] = 0.0;
    velAtArg[1] = 0.0;
    velAtArg[2] = 0.0;
    const vel = new Array(3);
    for (let i = 0; i < 8; i++) {
      velArray.getTuple(voxelIndices[i], vel);
      for (let j = 0; j < 3; j++) {
        velAtArg[j] += weights[i] * vel[j];
      }
    }
 
    return true;
  };
 
  publicAPI.computeNextStep = (velArray, image, delT, xyz) => {
    // This does Runge-Kutta 2
 
    // Start with evaluating velocity @ initial point
    Iif (!publicAPI.vectorAt(xyz, velArray, image, velAt)) {
      return false;
    }
    // Now find the mid point
    for (let i = 0; i < 3; i++) {
      xtmp[i] = xyz[i] + (delT / 2.0) * velAt[i];
    }
    // Use the velocity @ that point to project
    if (!publicAPI.vectorAt(xtmp, velArray, image, velAt)) {
      return false;
    }
    for (let i = 0; i < 3; i++) {
      xyz[i] += delT * velAt[i];
    }
 
    if (!publicAPI.vectorAt(xyz, velArray, image, velAt)) {
      return false;
    }
 
    return true;
  };
 
  publicAPI.streamIntegrate = (velArray, image, seed, offset) => {
    const retVal = [];
 
    const maxSteps = model.maximumNumberOfSteps;
    const delT = model.integrationStep;
    const xyz = new Float32Array(3);
    xyz[0] = seed[0];
    xyz[1] = seed[1];
    xyz[2] = seed[2];
 
    const pointsBuffer = [];
 
    let step = 0;
    for (step = 0; step < maxSteps; step++) {
      if (!publicAPI.computeNextStep(velArray, image, delT, xyz)) {
        break;
      }
      for (let i = 0; i < 3; i++) {
        pointsBuffer[3 * step + i] = xyz[i];
      }
    }
 
    const pd = vtkPolyData.newInstance();
 
    const points = new Float32Array(pointsBuffer);
    retVal[0] = points;
 
    pd.getPoints().setData(points, 3);
 
    const npts = points.length / 3;
    const line = new Uint32Array(npts + 1);
    line[0] = npts;
    for (let i = 0; i < npts; i++) {
      line[i + 1] = i + offset;
    }
    retVal[1] = line;
 
    pd.getLines().setData(line);
 
    return retVal;
  };
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
    const seeds = inData[1];
 
    Iif (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    Iif (!seeds) {
      vtkErrorMacro('Invalid or missing seeds');
      return;
    }
 
    const seedPts = seeds.getPoints();
    const nSeeds = seedPts.getNumberOfPoints();
 
    let offset = 0;
    const datas = [];
    const vectors = input.getPointData().getVectors();
    const point = [];
    for (let i = 0; i < nSeeds; i++) {
      seedPts.getTuple(i, point);
      const retVal = publicAPI.streamIntegrate(vectors, input, point, offset);
      offset += retVal[0].length / 3;
      datas.push(retVal);
    }
 
    let cellArrayLength = 0;
    let pointArrayLength = 0;
    datas.forEach((data) => {
      cellArrayLength += data[1].length;
      pointArrayLength += data[0].length;
    });
    offset = 0;
    let offset2 = 0;
    const cellArray = new Uint32Array(cellArrayLength);
    const pointArray = new Float32Array(pointArrayLength);
    datas.forEach((data) => {
      cellArray.set(data[1], offset);
      offset += data[1].length;
      pointArray.set(data[0], offset2);
      offset2 += data[0].length;
    });
 
    const output = vtkPolyData.newInstance();
    output.getPoints().setData(pointArray, 3);
    output.getLines().setData(cellArray);
 
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  integrationStep: 1,
  maximumNumberOfSteps: 1000,
};
 
// ----------------------------------------------------------------------------
 
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, 2, 1);
 
  // Generate macros for properties
  macro.setGet(publicAPI, model, ['integrationStep', 'maximumNumberOfSteps']);
 
  // Object specific methods
  vtkImageStreamline(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageStreamline');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };