All files / Sources/Imaging/Core/ImagePointDataIterator index.js

46.47% Statements 79/170
17.64% Branches 12/68
81.81% Functions 9/11
45.45% Lines 75/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 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                        22x   22x 21x 21x 21x     21x 21x 63x 63x 63x       21x 21x             21x   21x 21x 21x 21x             21x 21x                                     21x 21x 21x   21x       21x 21x 21x     21x     21x                                                                                                                                                                     21x 21x 21x 21x 21x 21x 21x     21x             21x 21x 21x       22x                                                             22x 7187x 7187x   7187x   7116x 7116x 7116x 7116x 71x   50x 50x 50x 50x 50x 50x 50x     21x 21x       7166x   7166x                             7166x                                                                       7208x 7187x 7187x   22x   22x 21x   22x 21x             1x                                                         22x     22x   22x     22x         1x                
import macro from 'vtk.js/Sources/macros';
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
// vtkImagePointDataIterator methods
// ----------------------------------------------------------------------------
 
function vtkImagePointDataIterator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImagePointDataIterator');
 
  publicAPI.initialize = (image, inExtent, stencil, algorithm) => {
    const dataExtent = image.getExtent();
    let extent = inExtent;
    Iif (extent == null) {
      extent = dataExtent;
    }
    let emptyExtent = false;
    for (let i = 0; i < 6; i += 2) {
      model.extent[i] = Math.max(extent[i], dataExtent[i]);
      model.extent[i + 1] = Math.min(extent[i + 1], dataExtent[i + 1]);
      Iif (model.extent[i] > model.extent[i + 1]) {
        emptyExtent = true;
      }
    }
    model.rowIncrement = dataExtent[1] - dataExtent[0] + 1;
    model.sliceIncrement =
      model.rowIncrement * (dataExtent[3] - dataExtent[2] + 1);
 
    let rowSpan;
    let sliceSpan;
    let volumeSpan;
 
    if (!emptyExtent) {
      // Compute the span of the image region to be covered.
      rowSpan = model.extent[1] - model.extent[0] + 1;
      sliceSpan = model.extent[3] - model.extent[2] + 1;
      volumeSpan = model.extent[5] - model.extent[4] + 1;
      model.id =
        model.extent[0] -
        dataExtent[0] +
        (model.extent[2] - dataExtent[2]) * model.rowIncrement +
        (model.extent[4] - dataExtent[4]) * model.sliceIncrement;
 
      // Compute the end increments (continuous increments).
      model.rowEndIncrement = model.rowIncrement - rowSpan;
      model.sliceEndIncrement =
        model.rowEndIncrement +
        model.sliceIncrement -
        model.rowIncrement * sliceSpan;
    } else E{
      // Extent is empty, isAtEnd() will immediately return "true"
      rowSpan = 0;
      sliceSpan = 0;
      volumeSpan = 0;
      model.id = 0;
      model.rowEndIncrement = 0;
      model.sliceEndIncrement = 0;
      for (let i = 0; i < 6; i += 2) {
        model.extent[i] = dataExtent[i];
        model.extent[i + 1] = dataExtent[i] - 1;
      }
    }
 
    // Get the end pointers for row, slice, and volume.
    model.spanEnd = model.id + rowSpan;
    model.rowEnd = model.id + rowSpan;
    model.sliceEnd =
      model.id + (model.rowIncrement * sliceSpan - model.rowEndIncrement);
    model.end =
      model.id + (model.sliceIncrement * volumeSpan - model.sliceEndIncrement);
 
    // For keeping track of the current x,y,z index.
    model.index[0] = model.extent[0];
    model.index[1] = model.extent[2];
    model.index[2] = model.extent[4];
 
    // For resetting the Y index after each slice.
    model.startY = model.index[1];
 
    // Code for when a stencil is provided.
    Iif (stencil) {
      model.hasStencil = true;
      model.inStencil = false;
 
      model.spanIndex = 0;
      const stencilExtent = stencil.getExtent();
 
      // The stencil has a YZ array of span lists, we need increments
      // to get to the next Z position in the YZ array.
      model.spanSliceIncrement = 0;
      model.spanSliceEndIncrement = 0;
 
      if (
        stencilExtent[3] >= stencilExtent[2] &&
        stencilExtent[5] >= stencilExtent[4]
      ) {
        model.spanSliceIncrement = stencilExtent[3] - stencilExtent[2] + 1;
        const botOffset = model.extent[2] - stencilExtent[2];
        if (botOffset >= 0) {
          model.spanSliceEndIncrement += botOffset;
        }
        const topOffset = stencilExtent[3] - model.extent[3];
        if (topOffset >= 0) {
          model.spanSliceEndIncrement += topOffset;
        }
      }
 
      // Find the offset to the start position within the YZ array.
      let startOffset = 0;
 
      const yOffset = model.extent[2] - stencilExtent[2];
      if (yOffset < 0) {
        model.extent[2] = stencilExtent[2];
        // starting before start of stencil: subtract the increment that
        // will be added in NextSpan() upon entry into stencil extent
        startOffset -= 1;
      } else {
        // starting partway into the stencil, so add an offset
        startOffset += yOffset;
      }
 
      if (stencilExtent[3] <= model.extent[3]) {
        model.extent[3] = stencilExtent[3];
      }
 
      const zOffset = model.extent[4] - stencilExtent[4];
      if (zOffset < 0) {
        model.extent[4] = stencilExtent[4];
        // starting before start of stencil: subtract the increment that
        // will be added in NextSpan() upon entry into stencil extent
        if (yOffset >= 0) {
          startOffset -= 1 + model.spanSliceEndIncrement;
        }
      } else {
        // starting partway into the stencil, so add an offset
        startOffset += zOffset * model.spanSliceIncrement;
      }
 
      if (stencilExtent[5] <= model.extent[5]) {
        model.extent[5] = stencilExtent[5];
      }
 
      if (
        model.extent[2] <= model.extent[3] &&
        model.extent[4] <= model.extent[5]
      ) {
        model.spanCountPointer =
          stencil.extentListLengths.subarray(startOffset);
 
        model.spanListPointer = stencil.extentLists.subarray(startOffset);
 
        // Get the current position within the span list for the current row
        if (yOffset >= 0 && zOffset >= 0) {
          // If starting within stencil extent, check stencil immediately
          model.inStencil = true;
          model.setSpanState(model.extent[0]);
        }
      } else {
        model.spanCountPointer = null;
        model.spanListPointer = null;
        model.inStencil = false;
      }
    } else {
      model.hasStencil = false;
      model.inStencil = true;
      model.spanSliceEndIncrement = 0;
      model.spanSliceIncrement = 0;
      model.spanIndex = 0;
      model.spanCountPointer = null;
      model.spanListPointer = null;
    }
 
    Iif (algorithm) {
      model.algorithm = algorithm;
      const maxCount = sliceSpan * volumeSpan;
      model.target = maxCount / 50 + 1;
      model.count =
        model.target * 50 - (maxCount / model.target) * model.target + 1;
    } else {
      model.algorithm = null;
      model.target = 0;
      model.count = 0;
    }
  };
 
  publicAPI.setSpanState = (idX) => {
    // Find the span that includes idX
    let inStencil = false;
    const spans = model.spanListPointer;
    const n = model.spanCountPointer[0];
    let i;
    for (i = 0; i < n; ++i) {
      if (spans[i] > idX) {
        break;
      }
      inStencil = !inStencil;
    }
 
    // Set the primary span state variables
    model.spanIndex = i;
    model.inStencil = inStencil;
 
    // Clamp the span end to MaxX+1
    let endIdX = model.extent[1] + 1;
    if (i < n && spans[i] <= model.extent[1]) {
      endIdX = spans[i];
    }
 
    // Compute the pointers for idX and endIdX
    const rowStart =
      model.rowEnd - (model.rowIncrement - model.rowEndIncrement);
 
    model.id = rowStart + (idX - model.extent[0]);
    model.spanEnd = rowStart + (endIdX - model.extent[0]);
  };
 
  publicAPI.nextSpan = () => {
    if (model.spanEnd === model.rowEnd) {
      let spanIncr = 1;
 
      if (model.spanEnd !== model.sliceEnd) {
        // Move to the next row
        model.id = model.rowEnd + model.rowEndIncrement;
        model.rowEnd += model.rowIncrement;
        model.spanEnd = model.rowEnd;
        model.index[1]++;
      } else if (model.spanEnd !== model.end) {
        // Move to the next slice
        model.id = model.sliceEnd + model.sliceEndIncrement;
        model.sliceEnd += model.sliceIncrement;
        model.rowEnd = model.id + (model.rowIncrement - model.rowEndIncrement);
        model.spanEnd = model.rowEnd;
        model.index[1] = model.startY;
        model.index[2]++;
        spanIncr += model.spanSliceEndIncrement;
      } else {
        // reached End
        model.id = model.end;
        return;
      }
 
      // Start of next row
      model.index[0] = model.extent[0];
 
      Iif (model.hasStencil) {
        if (
          model.index[1] >= model.extent[2] &&
          model.index[1] <= model.extent[3] &&
          model.index[2] >= model.extent[4] &&
          model.index[2] <= model.extent[5]
        ) {
          model.spanCountPointer = model.spanCountPointer.subarray(spanIncr);
          model.spanListPointer = model.spanListPointer.subarray(spanIncr);
          publicAPI.setSpanState(model.extent[0]);
        } else {
          model.inStencil = false;
        }
      }
 
      Iif (model.algorithm) {
        publicAPI.reportProgress();
      }
    } else E{
      // Move to the next span in the current row
      model.id = model.spanEnd;
      const spanCount = model.spanCountPointer[0];
      let endIdX = model.extent[1] + 1;
      model.index[0] = endIdX;
      if (model.spanIndex < spanCount) {
        const tmpIdX = model.spanListPointer[model.spanIndex];
        if (tmpIdX < endIdX) {
          model.index[0] = tmpIdX;
        }
      }
 
      // Get the index to the start of the span after the next
      model.spanIndex++;
      if (model.spanIndex < spanCount) {
        const tmpIdX = model.spanListPointer[model.spanIndex];
        if (tmpIdX < endIdX) {
          endIdX = tmpIdX;
        }
      }
 
      // Compute the end of the span
      model.spanEnd =
        model.rowEnd -
        (model.rowIncrement - model.rowEndIncrement) +
        (endIdX - model.extent[0]);
 
      // Flip the state
      model.inStencil = !model.inStencil;
    }
  };
 
  publicAPI.isAtEnd = () => model.id === model.end;
  publicAPI.isInStencil = () => model.inStencil;
  publicAPI.spanEndId = () => model.spanEnd;
 
  publicAPI.reportProgress = () => {};
 
  publicAPI.getArray = (array, i) =>
    array.getData().subarray(i * array.getNumberOfComponents());
 
  publicAPI.getScalars = (image, i = 0) =>
    publicAPI.getArray(image.getPointData().getScalars(), i);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  spanState: 0,
  extent: [0, -1, 0, -1, 0, -1],
  end: 0,
  spanEnd: 0,
  rowEnd: 0,
  sliceEnd: 0,
  rowIncrement: 0,
  rowEndIncrement: 0,
  sliceIncrement: 0,
  sliceEndIncrement: 0,
  id: 0,
  index: [0, 0, 0],
  startY: 0,
  hasStencil: false,
  inStencil: true,
  spanIndex: 0,
  spanSliceIncrement: 0,
  spanSliceEndIncrement: 0,
  spanCountPointer: null,
  spanListPointer: null,
  algorithm: null,
  target: 0,
  count: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
 
  macro.get(publicAPI, model, ['id', 'index']);
 
  // Object specific methods
  vtkImagePointDataIterator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkImagePointDataIterator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };