All files / core/src/RenderingEngine/vtkClasses vtkStreamingOpenGLVolumeMapper.js

98.58% Statements 139/141
80.7% Branches 46/57
100% Functions 4/4
98.5% Lines 132/134

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                                        384x                       384x 6349x 6349x       6349x   6349x 384x 384x 393216x   384x 384x 384x                 6349x 6349x   6349x   6349x 6349x         6349x 6349x 1781x 1781x   1781x 1781x 1781x 1781x                   1781x 1781x   1781x     1781x 1781x   1781x 1823744x   1823744x       1781x 1781x 1781x           1781x                         1781x 1781x 3647488x   1781x               1781x 1781x         1781x 384x       384x         1781x     4568x       6349x 6349x           6349x 6349x 6349x 384x 384x 384x 384x 384x 384x                   384x 384x 384x 384x   384x 1179648x 1179648x       384x 384x 384x   384x             384x 384x         384x 384x       384x         384x     5965x     6349x             6349x   6349x   5689x 5689x         5689x     5689x   5689x   5689x   5689x 5457x 5457x 5457x 5457x           5689x 232x 232x     232x   232x                         232x                         232x       5457x 5457x     5689x     6349x   384x 384x 1536x 1536x 1536x     384x 384x 384x 384x 384x 384x 384x 384x 384x   384x       384x 384x       384x           6349x     384x 10814x                           6349x     4465x                   428x     384x   384x   384x 384x     384x         428x                
import macro from '@kitware/vtk.js/macros';
import vtkOpenGLVolumeMapper from '@kitware/vtk.js/Rendering/OpenGL/VolumeMapper';
import { Filter } from '@kitware/vtk.js/Rendering/OpenGL/Texture/Constants';
import { VtkDataTypes } from '@kitware/vtk.js/Common/Core/DataArray/Constants';
import { getTransferFunctionHash } from '@kitware/vtk.js/Rendering/OpenGL/RenderWindow/resourceSharingHelper';
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import { Representation } from '@kitware/vtk.js/Rendering/Core/Property/Constants';
import vtkOpenGLTexture from '@kitware/vtk.js/Rendering/OpenGL/Texture';
import { getConstructorFromType } from '../../utilities/getBufferConfiguration';
import { getCanUseNorm16Texture } from '../../init';
 
/**
 * vtkStreamingOpenGLVolumeMapper - A derived class of the core vtkOpenGLVolumeMapper class.
 * This class  replaces the buildBufferObjects function so that we progressively upload our textures
 * into GPU memory using the new methods on vtkStreamingOpenGLTexture.
 *
 * @param {*} publicAPI The public API to extend
 * @param {*} model The private model to extend.
 */
function vtkStreamingOpenGLVolumeMapper(publicAPI, model) {
  model.classHierarchy.push('vtkStreamingOpenGLVolumeMapper');
 
  /**
   * buildBufferObjects - A fork of vtkOpenGLVolumeMapper's buildBufferObjects method.
   * This fork performs most of the same actions, but builds the textures progressively using
   * vtkStreamingOpenGLTexture's methods, and also prevents recomputation of the texture for each
   * vtkStreamingOpenGLVolumeMapper using the texture.
   *
   *
   * @param {*} ren The renderer.
   * @param {*} actor The actor to build the buffer objects for.
   */
  publicAPI.buildBufferObjects = (ren, actor) => {
    const image = model.currentInput;
    Iif (!image) {
      return;
    }
 
    const vprop = actor.getProperty();
 
    if (!model.jitterTexture.getHandle()) {
      const oTable = new Uint8Array(32 * 32);
      for (let i = 0; i < 32 * 32; ++i) {
        oTable[i] = 255.0 * Math.random();
      }
      model.jitterTexture.setMinificationFilter(Filter.LINEAR);
      model.jitterTexture.setMagnificationFilter(Filter.LINEAR);
      model.jitterTexture.create2DFromRaw(
        32,
        32,
        1,
        VtkDataTypes.UNSIGNED_CHAR,
        oTable
      );
    }
 
    const { numberOfComponents: numIComps } = image.get('numberOfComponents');
    const useIndependentComps = publicAPI.useIndependentComponents(vprop);
 
    const scalarOpacityFunc = vprop.getScalarOpacity();
    const opTex =
      model._openGLRenderWindow.getGraphicsResourceForObject(scalarOpacityFunc);
    let toString = getTransferFunctionHash(
      scalarOpacityFunc,
      useIndependentComps,
      numIComps
    );
    const reBuildOp = !opTex?.oglObject || opTex.hash !== toString;
    if (reBuildOp) {
      model.opacityTexture = vtkOpenGLTexture.newInstance();
      model.opacityTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
      // rebuild opacity tfun?
      const oWidth = 1024;
      const oSize = oWidth * 2 * numIComps;
      const ofTable = new Float32Array(oSize);
      const tmpTable = new Float32Array(oWidth);
 
      // for debugging
      // for (let index = 0; index < 3; index++) {
      //   const ofun = vprop.getScalarOpacity(0);
      //   const nodeValue1 = [];
      //   ofun.getNodeValue(index, nodeValue1);
      //   console.debug(index, nodeValue1);
      // }
 
      for (let c = 0; c < numIComps; ++c) {
        const ofun = vprop.getScalarOpacity(c);
        const opacityFactor =
          publicAPI.getCurrentSampleDistance(ren) /
          vprop.getScalarOpacityUnitDistance(c);
 
        const oRange = ofun.getRange();
        ofun.getTable(oRange[0], oRange[1], oWidth, tmpTable, 1);
        // adjust for sample distance etc
        for (let i = 0; i < oWidth; ++i) {
          ofTable[c * oWidth * 2 + i] =
            1.0 - (1.0 - tmpTable[i]) ** opacityFactor;
          ofTable[c * oWidth * 2 + i + oWidth] = ofTable[c * oWidth * 2 + i];
        }
      }
 
      model.opacityTexture.resetFormatAndType();
      model.opacityTexture.setMinificationFilter(Filter.LINEAR);
      model.opacityTexture.setMagnificationFilter(Filter.LINEAR);
 
      // use float texture where possible because we really need the resolution
      // for this table. Errors in low values of opacity accumulate to
      // visible artifacts. High values of opacity quickly terminate without
      // artifacts.
      Iif (
        model._openGLRenderWindow.getWebgl2() &&
        model.context.getExtension('OES_texture_float') &&
        model.context.getExtension('OES_texture_float_linear')
      ) {
        model.opacityTexture.create2DFromRaw(
          oWidth,
          2 * numIComps,
          1,
          VtkDataTypes.FLOAT,
          ofTable
        );
      } else {
        const oTable = new Uint8ClampedArray(oSize);
        for (let i = 0; i < oSize; ++i) {
          oTable[i] = 255.0 * ofTable[i];
        }
        model.opacityTexture.create2DFromRaw(
          oWidth,
          2 * numIComps,
          1,
          VtkDataTypes.UNSIGNED_CHAR,
          oTable
        );
      }
      Eif (scalarOpacityFunc) {
        model._openGLRenderWindow.setGraphicsResourceForObject(
          scalarOpacityFunc,
          model.opacityTexture,
          toString
        );
        if (scalarOpacityFunc !== model._scalarOpacityFunc) {
          model._openGLRenderWindow.registerGraphicsResourceUser(
            scalarOpacityFunc,
            publicAPI
          );
          model._openGLRenderWindow.unregisterGraphicsResourceUser(
            model._scalarOpacityFunc,
            publicAPI
          );
        }
        model._scalarOpacityFunc = scalarOpacityFunc;
      }
    } else {
      model.opacityTexture = opTex.oglObject;
    }
 
    // rebuild color tfun?
    const colorTransferFunc = vprop.getRGBTransferFunction();
    toString = getTransferFunctionHash(
      colorTransferFunc,
      useIndependentComps,
      numIComps
    );
    const cTex =
      model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
    const reBuildC = !cTex?.oglObject?.getHandle() || cTex?.hash !== toString;
    if (reBuildC) {
      model.colorTexture = vtkOpenGLTexture.newInstance();
      model.colorTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
      const cWidth = 1024;
      const cSize = cWidth * 2 * numIComps * 3;
      const cTable = new Uint8ClampedArray(cSize);
      const tmpTable = new Float32Array(cWidth * 3);
 
      // // for debugging
      // for (let index = 0; index < 3; index++) {
      //   const cfun = vprop.getRGBTransferFunction(0);
      //   const nodeValue1 = [];
      //   cfun.getNodeValue(index, nodeValue1);
      //   console.debug(index, nodeValue1);
      // }
 
      for (let c = 0; c < numIComps; ++c) {
        const cfun = vprop.getRGBTransferFunction(c);
        const cRange = cfun.getRange();
        cfun.getTable(cRange[0], cRange[1], cWidth, tmpTable, 1);
 
        for (let i = 0; i < cWidth * 3; ++i) {
          cTable[c * cWidth * 6 + i] = 255.0 * tmpTable[i];
          cTable[c * cWidth * 6 + i + cWidth * 3] = 255.0 * tmpTable[i];
        }
      }
 
      model.colorTexture.resetFormatAndType();
      model.colorTexture.setMinificationFilter(Filter.LINEAR);
      model.colorTexture.setMagnificationFilter(Filter.LINEAR);
 
      model.colorTexture.create2DFromRaw(
        cWidth,
        2 * numIComps,
        3,
        VtkDataTypes.UNSIGNED_CHAR,
        cTable
      );
      Eif (colorTransferFunc) {
        model._openGLRenderWindow.setGraphicsResourceForObject(
          colorTransferFunc,
          model.colorTexture,
          toString
        );
        Eif (colorTransferFunc !== model._colorTransferFunc) {
          model._openGLRenderWindow.registerGraphicsResourceUser(
            colorTransferFunc,
            publicAPI
          );
          model._openGLRenderWindow.unregisterGraphicsResourceUser(
            model._colorTransferFunc,
            publicAPI
          );
        }
        model._colorTransferFunc = colorTransferFunc;
      }
    } else {
      model.colorTexture = cTex.oglObject;
    }
 
    publicAPI.updateLabelOutlineThicknessTexture(actor);
 
    // rebuild the scalarTexture if the data has changed
    // IMPORTANT: this is the most important part of the streaming process.
    // we need to take into account that sometimes the texture is updated (mtime)
    // but the image is not updated since in the new model the image lives in the cpu
    // while the texture lives in the gpu.
    toString = `${image.getMTime()}-${model.scalarTexture.getMTime()}`;
 
    if (model.scalarTextureString !== toString) {
      // Build the textures
      const dims = image.getDimensions();
      model.scalarTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
 
      // Set not to use half float initially since we don't know if the
      // streamed data is actually half float compatible or not yet, as
      // the data has not arrived due to streaming
      model.scalarTexture.enableUseHalfFloat(false);
 
      const previousTextureParameters =
        model.scalarTexture.getTextureParameters();
 
      const dataType = image.get('dataType').dataType;
 
      let shouldReset = true;
 
      if (previousTextureParameters?.dataType === dataType) {
        Eif (previousTextureParameters?.width === dims[0]) {
          Eif (previousTextureParameters?.height === dims[1]) {
            Eif (previousTextureParameters?.depth === dims[2]) {
              shouldReset = false;
            }
          }
        }
      }
 
      if (shouldReset) {
        const norm16Ext = model.context.getExtension('EXT_texture_norm16');
        model.scalarTexture.setOglNorm16Ext(
          getCanUseNorm16Texture() ? norm16Ext : null
        );
        model.scalarTexture.resetFormatAndType();
 
        model.scalarTexture.setTextureParameters({
          width: dims[0],
          height: dims[1],
          depth: dims[2],
          numberOfComponents: numIComps,
          dataType,
        });
 
        // const emptyDataTypeOfType = getConstructorFromType(dataType);
        // const emptyData = new emptyDataTypeOfType(dims[0] * dims[1] * dims[2]);
 
        // There are some bugs in mac for texStorage3D so basically here
        // we let the vtk.js decide if it wants to use it or not
        model.scalarTexture.create3DFromRaw(
          dims[0],
          dims[1],
          dims[2],
          numIComps,
          dataType,
          null
          // emptyData
        );
 
        // do an initial update since some data may be already
        // available and we can avoid a re-render to trigger
        // the update
        model.scalarTexture.update3DFromRaw();
 
        // since we don't have scalars we don't need to set graphics resource for the scalar texture
      } else {
        model.scalarTexture.deactivate();
        model.scalarTexture.update3DFromRaw();
      }
 
      model.scalarTextureString = toString;
    }
 
    if (!model.tris.getCABO().getElementCount()) {
      // build the CABO
      const ptsArray = new Float32Array(12);
      for (let i = 0; i < 4; i++) {
        ptsArray[i * 3] = (i % 2) * 2 - 1.0;
        ptsArray[i * 3 + 1] = i > 1 ? 1.0 : -1.0;
        ptsArray[i * 3 + 2] = -1.0;
      }
 
      const cellArray = new Uint16Array(8);
      cellArray[0] = 3;
      cellArray[1] = 0;
      cellArray[2] = 1;
      cellArray[3] = 3;
      cellArray[4] = 3;
      cellArray[5] = 0;
      cellArray[6] = 3;
      cellArray[7] = 2;
 
      const points = vtkDataArray.newInstance({
        numberOfComponents: 3,
        values: ptsArray,
      });
      points.setName('points');
      const cells = vtkDataArray.newInstance({
        numberOfComponents: 1,
        values: cellArray,
      });
      model.tris.getCABO().createVBO(cells, 'polys', Representation.SURFACE, {
        points,
        cellOffset: 0,
      });
    }
 
    model.VBOBuildTime.modified();
  };
 
  publicAPI.getNeedToRebuildBufferObjects = (ren, actor) => {
    if (
      model.VBOBuildTime.getMTime() < publicAPI.getMTime() ||
      model.VBOBuildTime.getMTime() < actor.getMTime() ||
      model.VBOBuildTime.getMTime() < model.renderable.getMTime() ||
      model.VBOBuildTime.getMTime() < actor.getProperty().getMTime() ||
      model.VBOBuildTime.getMTime() < model.currentInput.getMTime() ||
      model.VBOBuildTime.getMTime() < model.scalarTexture?.getMTime() ||
      model.VBOBuildTime.getMTime() < model.colorTexture?.getMTime() ||
      model.VBOBuildTime.getMTime() <
        model.labelOutlineThicknessTexture?.getMTime() ||
      !model.scalarTexture?.getHandle() ||
      !model.colorTexture?.getHandle() ||
      !model.labelOutlineThicknessTexture?.getHandle()
    ) {
      return true;
    }
 
    return false;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkOpenGLVolumeMapper.extend(publicAPI, model, initialValues);
 
  model.scalarTexture = initialValues.scalarTexture;
  model.previousState = {};
 
  // Object methods
  vtkStreamingOpenGLVolumeMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkStreamingOpenGLVolumeMapper'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };