All files / Sources/Rendering/OpenGL/CellArrayBufferObject index.js

87.17% Statements 204/234
81.65% Branches 89/109
80.95% Functions 17/21
87.78% Lines 194/221

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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496              1x             84x 84x   84x 84x             84x       84x       84x                       5082x   5082x   5082x             3632x 2600x 2600x       1032x 1032x 1032x 1032x 1032x 1032x 1032x 1032x   1032x 1032x 1032x 1032x   1032x     1032x           1032x 562x 562x 562x     1032x 958x                         1032x 184x 184x 184x 184x     1032x 7x 7x 7x 7x 7x   7x   1025x   1032x   1032x 1032x 1032x 1032x 1032x 1032x     1032x     649x 773x         418x 1911x 1911x         981x 981x 3084x 3084x                                   93118x 94921x 94921x 94921x       120x 568x 568x 568x         1032x     649x     418x 418x         981x 981x                     93118x 92923x   195x     120x 120x           1032x 1032x 1032x 4x 4x 1028x 145x 145x   883x 883x     1032x 1032x 1032x 1032x 95286x 95286x     1032x 1032x 1032x 7x   1032x 1032x       1032x 1032x 1032x 3096x   3096x 3096x   3096x 3096x       1032x         1032x   84x 84x 84x 252x 252x   252x 252x   84x 948x           1032x 821x 817x 817x   4x     4x 4x 4x 4x 4x       1032x 1032x   297230x 262664x 262664x   297230x     297230x   297230x 186384x 186384x 186384x     110846x   110846x   110846x       297230x 240066x     240066x   240066x 240066x 240066x     297230x             297230x 3252x 3252x 6576x       297230x 8946x 1440x   7506x   8946x 8946x 8946x 8946x         1032x 95286x 95286x 95286x   1032x 1032x 1032x 7x 7x   1032x     5082x 84x               84x               84x         71x     84x         71x     84x         84x 84x                           1x                                         5082x     5082x   5082x                           5082x               5082x         1x          
import { mat4, quat, vec3 } from 'gl-matrix';
 
import macro from 'vtk.js/Sources/macros';
import vtkBufferObject from 'vtk.js/Sources/Rendering/OpenGL/BufferObject';
import { ObjectType } from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
import { Representation } from 'vtk.js/Sources/Rendering/Core/Property/Constants';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// Static functions
// ----------------------------------------------------------------------------
 
function computeInverseShiftAndScaleMatrix(coordShift, coordScale) {
  const inverseScale = new Float64Array(3);
  vec3.inverse(inverseScale, coordScale);
 
  const matrix = new Float64Array(16);
  mat4.fromRotationTranslationScale(
    matrix,
    quat.create(),
    coordShift,
    inverseScale
  );
 
  return matrix;
}
 
function shouldApplyCoordShiftAndScale(coordShift, coordScale) {
  Iif (coordShift === null || coordScale === null) {
    return false;
  }
 
  return !(
    vec3.exactEquals(coordShift, [0, 0, 0]) &&
    vec3.exactEquals(coordScale, [1, 1, 1])
  );
}
 
// ----------------------------------------------------------------------------
// vtkOpenGLCellArrayBufferObject methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLCellArrayBufferObject(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLCellArrayBufferObject');
 
  publicAPI.setType(ObjectType.ARRAY_BUFFER);
 
  publicAPI.createVBO = (
    cellArray,
    inRep,
    outRep,
    options,
    selectionMaps = null
  ) => {
    if (!cellArray.getData() || !cellArray.getData().length) {
      model.elementCount = 0;
      return 0;
    }
 
    // Figure out how big each block will be, currently 6 or 7 floats.
    model.blockSize = 3;
    model.vertexOffset = 0;
    model.normalOffset = 0;
    model.tCoordOffset = 0;
    model.tCoordComponents = 0;
    model.colorComponents = 0;
    model.colorOffset = 0;
    model.customData = [];
 
    const pointData = options.points.getData();
    let normalData = null;
    let tcoordData = null;
    let colorData = null;
 
    const colorComponents = options.colors
      ? options.colors.getNumberOfComponents()
      : 0;
    const textureComponents = options.tcoords
      ? options.tcoords.getNumberOfComponents()
      : 0;
 
    // the values of 4 below are because floats are 4 bytes
 
    if (options.normals) {
      model.normalOffset = 4 * model.blockSize;
      model.blockSize += 3;
      normalData = options.normals.getData();
    }
 
    if (options.customAttributes) {
      options.customAttributes.forEach((a) => {
        if (a) {
          model.customData.push({
            data: a.getData(),
            offset: 4 * model.blockSize,
            components: a.getNumberOfComponents(),
            name: a.getName(),
          });
          model.blockSize += a.getNumberOfComponents();
        }
      });
    }
 
    if (options.tcoords) {
      model.tCoordOffset = 4 * model.blockSize;
      model.tCoordComponents = textureComponents;
      model.blockSize += textureComponents;
      tcoordData = options.tcoords.getData();
    }
 
    if (options.colors) {
      model.colorComponents = options.colors.getNumberOfComponents();
      model.colorOffset = 0;
      colorData = options.colors.getData();
      if (!model.colorBO) {
        model.colorBO = vtkBufferObject.newInstance();
      }
      model.colorBO.setOpenGLRenderWindow(model._openGLRenderWindow);
    } else {
      model.colorBO = null;
    }
    model.stride = 4 * model.blockSize;
 
    let pointIdx = 0;
    let normalIdx = 0;
    let tcoordIdx = 0;
    let colorIdx = 0;
    let custIdx = 0;
    let cellCount = 0;
    let addAPoint;
 
    const cellBuilders = {
      // easy, every input point becomes an output point
      anythingToPoints(numPoints, cellPts, offset) {
        for (let i = 0; i < numPoints; ++i) {
          addAPoint(cellPts[offset + i]);
        }
      },
      linesToWireframe(numPoints, cellPts, offset) {
        // for lines we add a bunch of segments
        for (let i = 0; i < numPoints - 1; ++i) {
          addAPoint(cellPts[offset + i]);
          addAPoint(cellPts[offset + i + 1]);
        }
      },
      polysToWireframe(numPoints, cellPts, offset) {
        // for polys we add a bunch of segments and close it
        if (numPoints > 2) {
          for (let i = 0; i < numPoints; ++i) {
            addAPoint(cellPts[offset + i]);
            addAPoint(cellPts[offset + ((i + 1) % numPoints)]);
          }
        }
      },
      stripsToWireframe(numPoints, cellPts, offset) {
        if (numPoints > 2) {
          // for strips we add a bunch of segments and close it
          for (let i = 0; i < numPoints - 1; ++i) {
            addAPoint(cellPts[offset + i]);
            addAPoint(cellPts[offset + i + 1]);
          }
          for (let i = 0; i < numPoints - 2; i++) {
            addAPoint(cellPts[offset + i]);
            addAPoint(cellPts[offset + i + 2]);
          }
        }
      },
      polysToSurface(npts, cellPts, offset) {
        for (let i = 0; i < npts - 2; i++) {
          addAPoint(cellPts[offset + 0]);
          addAPoint(cellPts[offset + i + 1]);
          addAPoint(cellPts[offset + i + 2]);
        }
      },
      stripsToSurface(npts, cellPts, offset) {
        for (let i = 0; i < npts - 2; i++) {
          addAPoint(cellPts[offset + i]);
          addAPoint(cellPts[offset + i + 1 + (i % 2)]);
          addAPoint(cellPts[offset + i + 1 + ((i + 1) % 2)]);
        }
      },
    };
 
    const cellCounters = {
      // easy, every input point becomes an output point
      anythingToPoints(numPoints, cellPts) {
        return numPoints;
      },
      linesToWireframe(numPoints, cellPts) {
        if (numPoints > 1) {
          return (numPoints - 1) * 2;
        }
        return 0;
      },
      polysToWireframe(numPoints, cellPts) {
        if (numPoints > 2) {
          return numPoints * 2;
        }
        return 0;
      },
      stripsToWireframe(numPoints, cellPts) {
        if (numPoints > 2) {
          return numPoints * 4 - 6;
        }
        return 0;
      },
      polysToSurface(npts, cellPts) {
        if (npts > 2) {
          return (npts - 2) * 3;
        }
        return 0;
      },
      stripsToSurface(npts, cellPts, offset) {
        if (npts > 2) {
          return (npts - 2) * 3;
        }
        return 0;
      },
    };
 
    let func = null;
    let countFunc = null;
    if (outRep === Representation.POINTS || inRep === 'verts') {
      func = cellBuilders.anythingToPoints;
      countFunc = cellCounters.anythingToPoints;
    } else if (outRep === Representation.WIREFRAME || inRep === 'lines') {
      func = cellBuilders[`${inRep}ToWireframe`];
      countFunc = cellCounters[`${inRep}ToWireframe`];
    } else {
      func = cellBuilders[`${inRep}ToSurface`];
      countFunc = cellCounters[`${inRep}ToSurface`];
    }
 
    const array = cellArray.getData();
    const size = array.length;
    let caboCount = 0;
    for (let index = 0; index < size; ) {
      caboCount += countFunc(array[index], array);
      index += array[index] + 1;
    }
 
    let packedUCVBO = null;
    const packedVBO = new Float32Array(caboCount * model.blockSize);
    if (colorData) {
      packedUCVBO = new Uint8Array(caboCount * 4);
    }
    let vboidx = 0;
    let ucidx = 0;
 
    // Find out if shift scale should be used
    // Compute squares of diagonal size and distance from the origin
    let diagSq = 0.0;
    let distSq = 0.0;
    for (let i = 0; i < 3; ++i) {
      const range = options.points.getRange(i);
 
      const delta = range[1] - range[0];
      diagSq += delta * delta;
 
      const distShift = 0.5 * (range[1] + range[0]);
      distSq += distShift * distShift;
    }
 
    const useShiftAndScale =
      diagSq > 0 &&
      (Math.abs(distSq) / diagSq > 1.0e6 || // If data is far from the origin relative to its size
        Math.abs(Math.log10(diagSq)) > 3.0 || // If the size is huge when not far from the origin
        (diagSq === 0 && distSq > 1.0e6)); // If data is a point, but far from the origin
 
    if (useShiftAndScale) {
      // Compute shift and scale vectors
      const coordShift = new Float64Array(3);
      const coordScale = new Float64Array(3);
      for (let i = 0; i < 3; ++i) {
        const range = options.points.getRange(i);
        const delta = range[1] - range[0];
 
        coordShift[i] = 0.5 * (range[1] + range[0]);
        coordScale[i] = delta > 0 ? 1.0 / delta : 1.0;
      }
      publicAPI.setCoordShiftAndScale(coordShift, coordScale);
    } else Iif (model.coordShiftAndScaleEnabled === true) {
      // Make sure to reset
      publicAPI.setCoordShiftAndScale(null, null);
    }
 
    // Initialize the structures used to keep track of point ids and cell ids for selectors
    if (selectionMaps) {
      if (!selectionMaps.points && !selectionMaps.cells) {
        selectionMaps.points = new Int32Array(caboCount);
        selectionMaps.cells = new Int32Array(caboCount);
      } else {
        const newPoints = new Int32Array(
          caboCount + selectionMaps.points.length
        );
        newPoints.set(selectionMaps.points);
        selectionMaps.points = newPoints;
        const newCells = new Int32Array(caboCount + selectionMaps.cells.length);
        newCells.set(selectionMaps.cells);
        selectionMaps.cells = newCells;
      }
    }
 
    let pointCount = options.vertexOffset;
    addAPoint = function addAPointFunc(i) {
      // Keep track of original point and cell ids, for selection
      if (selectionMaps) {
        selectionMaps.points[pointCount] = i;
        selectionMaps.cells[pointCount] = cellCount + options.cellOffset;
      }
      ++pointCount;
 
      // Vertices
      pointIdx = i * 3;
 
      if (!model.coordShiftAndScaleEnabled) {
        packedVBO[vboidx++] = pointData[pointIdx++];
        packedVBO[vboidx++] = pointData[pointIdx++];
        packedVBO[vboidx++] = pointData[pointIdx++];
      } else {
        // Apply shift and scale
        packedVBO[vboidx++] =
          (pointData[pointIdx++] - model.coordShift[0]) * model.coordScale[0];
        packedVBO[vboidx++] =
          (pointData[pointIdx++] - model.coordShift[1]) * model.coordScale[1];
        packedVBO[vboidx++] =
          (pointData[pointIdx++] - model.coordShift[2]) * model.coordScale[2];
      }
 
      if (normalData !== null) {
        Iif (options.haveCellNormals) {
          normalIdx = (cellCount + options.cellOffset) * 3;
        } else {
          normalIdx = i * 3;
        }
        packedVBO[vboidx++] = normalData[normalIdx++];
        packedVBO[vboidx++] = normalData[normalIdx++];
        packedVBO[vboidx++] = normalData[normalIdx++];
      }
 
      model.customData.forEach((attr) => {
        custIdx = i * attr.components;
        for (let j = 0; j < attr.components; ++j) {
          packedVBO[vboidx++] = attr.data[custIdx++];
        }
      });
 
      if (tcoordData !== null) {
        tcoordIdx = i * textureComponents;
        for (let j = 0; j < textureComponents; ++j) {
          packedVBO[vboidx++] = tcoordData[tcoordIdx++];
        }
      }
 
      if (colorData !== null) {
        if (options.haveCellScalars) {
          colorIdx = (cellCount + options.cellOffset) * colorComponents;
        } else {
          colorIdx = i * colorComponents;
        }
        packedUCVBO[ucidx++] = colorData[colorIdx++];
        packedUCVBO[ucidx++] = colorData[colorIdx++];
        packedUCVBO[ucidx++] = colorData[colorIdx++];
        packedUCVBO[ucidx++] =
          colorComponents === 4 ? colorData[colorIdx++] : 255;
      }
    };
 
    for (let index = 0; index < size; ) {
      func(array[index], array, index + 1);
      index += array[index] + 1;
      cellCount++;
    }
    model.elementCount = caboCount;
    publicAPI.upload(packedVBO, ObjectType.ARRAY_BUFFER);
    if (model.colorBO) {
      model.colorBOStride = 4;
      model.colorBO.upload(packedUCVBO, ObjectType.ARRAY_BUFFER);
    }
    return cellCount;
  };
 
  publicAPI.setCoordShiftAndScale = (coordShift, coordScale) => {
    Iif (
      coordShift !== null &&
      (coordShift.constructor !== Float64Array || coordShift.length !== 3)
    ) {
      vtkErrorMacro('Wrong type for coordShift, expected vec3 or null');
      return;
    }
 
    Iif (
      coordScale !== null &&
      (coordScale.constructor !== Float64Array || coordScale.length !== 3)
    ) {
      vtkErrorMacro('Wrong type for coordScale, expected vec3 or null');
      return;
    }
 
    if (
      model.coordShift === null ||
      coordShift === null ||
      !vec3.equals(coordShift, model.coordShift)
    ) {
      model.coordShift = coordShift;
    }
 
    if (
      model.coordScale === null ||
      coordScale === null ||
      !vec3.equals(coordScale, model.coordScale)
    ) {
      model.coordScale = coordScale;
    }
 
    model.coordShiftAndScaleEnabled = shouldApplyCoordShiftAndScale(
      model.coordShift,
      model.coordScale
    );
 
    if (model.coordShiftAndScaleEnabled) {
      model.inverseShiftAndScaleMatrix = computeInverseShiftAndScaleMatrix(
        model.coordShift,
        model.coordScale
      );
    } else E{
      model.inverseShiftAndScaleMatrix = null;
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  elementCount: 0,
  stride: 0,
  colorBOStride: 0,
  vertexOffset: 0,
  normalOffset: 0,
  tCoordOffset: 0,
  tCoordComponents: 0,
  colorOffset: 0,
  colorComponents: 0,
  tcoordBO: null,
  customData: [],
  coordShift: null,
  coordScale: null,
  coordShiftAndScaleEnabled: false,
  inverseShiftAndScaleMatrix: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkBufferObject.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, [
    'colorBO',
    'elementCount',
    'stride',
    'colorBOStride',
    'vertexOffset',
    'normalOffset',
    'tCoordOffset',
    'tCoordComponents',
    'colorOffset',
    'colorComponents',
    'customData',
  ]);
 
  macro.get(publicAPI, model, [
    'coordShift',
    'coordScale',
    'coordShiftAndScaleEnabled',
    'inverseShiftAndScaleMatrix',
  ]);
 
  // Object specific methods
  vtkOpenGLCellArrayBufferObject(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };