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

64.28% Statements 72/112
48.14% Branches 13/27
70.58% Functions 12/17
64.76% Lines 68/105

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                                                        246x   246x 246x   246x   246x                                             246x   246x 232x 232x 232x 232x               246x 5689x   5689x       5689x 5689x 5689x 5689x   5689x         5689x                 246x 246x 106857x 106857x     246x 476x     476x   476x       476x   476x 28788x         4337x 4337x 4337x 515258x   250869x 250869x   219886x     30983x 30983x   30983x       30983x         30983x     30983x       30983x                             30983x   30983x       4337x       4337x 4337x                                                                                                                                                   246x 221380x   246x   246x 246x     246x   246x             232x 232x 232x 232x 232x     5689x                             428x         246x   246x     246x         428x                
import macro from '@kitware/vtk.js/macros';
import vtkOpenGLTexture from '@kitware/vtk.js/Rendering/OpenGL/Texture';
import cache from '../../cache/cache';
import { getConstructorFromType } from '../../utilities/getBufferConfiguration';
 
/**
 * Converts the input data array to the specified data type
 * @param {TypedArray} data - The source data array
 * @param {string} targetDataType - The target data type to convert to
 * @returns {TypedArray} The converted data array
 */
function convertDataType(data, targetDataType) {
  const Constructor = getConstructorFromType(targetDataType);
  const convertedData = new Constructor(data.length);
  convertedData.set(data);
  return convertedData;
}
 
/**
 * vtkStreamingOpenGLTexture - A derived class of the core vtkOpenGLTexture.
 * This class has methods to update the texture memory on the GPU slice by slice
 * in an efficient yet GPU-architecture friendly manner.
 *
 *
 * @param {*} publicAPI The public API to extend
 * @param {*} model The private model to extend.
 */
function vtkStreamingOpenGLTexture(publicAPI, model) {
  model.classHierarchy.push('vtkStreamingOpenGLTexture');
 
  model.updatedFrames = [];
  model.volumeId = null;
 
  const superCreate3DFilterableFromRaw = publicAPI.create3DFilterableFromRaw;
 
  publicAPI.create3DFilterableFromRaw = (
    width,
    height,
    depth,
    numberOfComponents,
    dataType,
    data,
    preferSizeOverAccuracy
  ) => {
    model.inputDataType = dataType;
    model.inputNumComps = numberOfComponents;
 
    superCreate3DFilterableFromRaw(
      width,
      height,
      depth,
      numberOfComponents,
      dataType,
      data,
      preferSizeOverAccuracy
    );
  };
 
  const superUpdate = publicAPI.updateVolumeInfoForGL;
 
  publicAPI.updateVolumeInfoForGL = (dataType, numComps) => {
    const isScalingApplied = superUpdate(dataType, numComps);
    model.volumeInfo.dataComputedScale = [1];
    model.volumeInfo.dataComputedOffset = [0];
    return isScalingApplied;
  };
 
  /**
   * This function updates the GPU texture memory to match the current
   * representation of data held in RAM.
   *
   */
  publicAPI.update3DFromRaw = () => {
    const { volumeId } = model;
 
    Iif (!volumeId) {
      return;
    }
 
    const volume = cache.getVolume(volumeId);
    model._openGLRenderWindow.activateTexture(publicAPI);
    publicAPI.createTexture();
    publicAPI.bind();
 
    Iif (volume.isDynamicVolume()) {
      updateDynamicVolumeTexture();
      return;
    }
 
    return (
      publicAPI.hasUpdatedFrames() && updateTextureImagesUsingVoxelManager()
    );
  };
 
  /**
   * Called when a frame is loaded so that on next render we know which data to load in.
   * @param {number} frameIndex The frame to load in.
   */
  const superModified = publicAPI.modified;
  publicAPI.setUpdatedFrame = (frameIndex) => {
    model.updatedFrames[frameIndex] = true;
    superModified();
  };
 
  publicAPI.modified = () => {
    superModified();
 
    // this is really not efficient, but it works for now
    const volume = cache.getVolume(model.volumeId);
 
    Iif (!volume) {
      return;
    }
 
    const imageIds = volume.imageIds;
 
    for (let i = 0; i < imageIds.length; i++) {
      model.updatedFrames[i] = true;
    }
  };
 
  function updateTextureImagesUsingVoxelManager() {
    const volume = cache.getVolume(model.volumeId);
    const imageIds = volume.imageIds;
    for (let i = 0; i < model.updatedFrames.length; i++) {
      if (model.updatedFrames[i]) {
        // find the updated frames
        const image = cache.getImage(imageIds[i]);
        if (!image) {
          // console.debug('image not found', imageIds[i]);
          continue;
        }
 
        let data = image.voxelManager.getScalarData();
        const gl = model.context;
 
        Iif (volume.dataType !== data.constructor.name) {
          data = convertDataType(data, volume.dataType);
        }
 
        const [pixData] = publicAPI.updateArrayDataTypeForGL(volume.dataType, [
          data,
        ]);
 
        // Bind the texture
        publicAPI.bind();
 
        // Calculate the offset within the 3D texture
        const zOffset = i;
 
        // Update the texture sub-image
        // Todo: need to check other systems if it can handle it
        gl.texSubImage3D(
          model.target, // target
          0, // level
          0, // xoffset
          0, // yoffset
          zOffset, // zoffset
          model.width, // width
          model.height, // height
          1, // depth (1 slice)
          model.format, // format
          model.openGLDataType, // type
          pixData // data
        );
 
        // Unbind the texture
        publicAPI.deactivate();
        // Reset the updated flag
        model.updatedFrames[i] = null;
      }
    }
 
    Iif (model.generateMipmap) {
      model.context.generateMipmap(model.target);
    }
 
    publicAPI.deactivate();
    return true;
  }
 
  function updateDynamicVolumeTexture() {
    const volume = cache.getVolume(model.volumeId);
 
    // loop over imageIds of the current time point and update the texture
    const imageIds = volume.getCurrentDimensionGroupImageIds();
 
    if (!imageIds.length) {
      return false;
    }
 
    let constructor;
 
    for (let i = 0; i < imageIds.length; i++) {
      const imageId = imageIds[i];
      const image = cache.getImage(imageId);
 
      let data;
      if (!image) {
        // if there is no data we should set zero
        constructor = getConstructorFromType(volume.dataType, true);
        data = new constructor(model.width * model.height);
      } else {
        data = image.voxelManager.getScalarData();
        constructor = data.constructor;
      }
 
      const gl = model.context;
 
      if (volume.dataType !== data.constructor.name) {
        data = convertDataType(data, volume.dataType);
      }
 
      const [pixData] = publicAPI.updateArrayDataTypeForGL(volume.dataType, [
        data,
      ]);
 
      // Bind the texture
      publicAPI.bind();
 
      // Calculate the offset within the 3D texture
      let zOffset = i;
 
      // Update the texture sub-image
      // Todo: need to check other systems if it can handle it
      gl.texSubImage3D(
        model.target, // target
        0, // level
        0, // xoffset
        0, // yoffset
        zOffset, // zoffset
        model.width, // width
        model.height, // height
        1, // depth (1 slice)
        model.format, // format
        model.openGLDataType, // type
        pixData // data
      );
 
      // Unbind the texture
      publicAPI.deactivate();
      // Reset the updated flag
    }
 
    if (model.generateMipmap) {
      model.context.generateMipmap(model.target);
    }
 
    publicAPI.deactivate();
    return true;
  }
 
  publicAPI.hasUpdatedFrames = () =>
    !model.updatedFrames.length || model.updatedFrames.some((frame) => frame);
 
  publicAPI.getUpdatedFrames = () => model.updatedFrames;
 
  publicAPI.setVolumeId = (volumeId) => {
    model.volumeId = volumeId;
  };
 
  publicAPI.getVolumeId = () => model.volumeId;
 
  publicAPI.setTextureParameters = ({
    width,
    height,
    depth,
    numberOfComponents,
    dataType,
  }) => {
    model.width ??= width;
    model.height ??= height;
    model.depth ??= depth;
    model.inputNumComps ??= numberOfComponents;
    model.inputDataType ??= dataType;
  };
 
  publicAPI.getTextureParameters = () => ({
    width: model.width,
    height: model.height,
    depth: model.depth,
    numberOfComponents: model.inputNumComps,
    dataType: model.inputDataType,
  });
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  updatedFrames: [],
};
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkOpenGLTexture.extend(publicAPI, model, initialValues);
 
  // Object methods
  vtkStreamingOpenGLTexture(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkStreamingOpenGLTexture'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };