All files / core/src/loaders geometryLoader.ts

25% Statements 13/52
19.23% Branches 5/26
16.66% Functions 2/12
25% Lines 13/52

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                                        428x                                           428x                                                                                                                                                                                                                                                               12x           12x   12x       12x 8x       4x 4x                           12x   12x                         428x                                       428x  
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import cache from '../cache/cache';
import { GeometryType } from '../enums';
import type {
  IGeometry,
  PublicContourSetData,
  PublicSurfaceData,
  PublicMeshData,
  IGeometryLoadObject,
  GeometryLoaderFn,
} from '../types';
import { createContourSet } from './utils/contourSet/createContourSet';
import { createSurface } from './utils/surface/createSurface';
import { createMesh } from './utils/mesh/createMesh';
import Events from '../enums/Events';
import eventTarget from '../eventTarget';
import triggerEvent from '../utilities/triggerEvent';
import type { GeometryLoaderOptions } from '../types/GeometryLoaderFn';
import { cornerstoneMeshLoader } from './cornerstoneMeshLoader';
 
let loaderOptions: GeometryLoaderOptions = {
  // callback allowing customization of the xhr (e.g. adding custom auth headers, cors, etc)
  beforeSend(xhr) {
    // before send code
  },
};
 
export function setOptions(newOptions: GeometryLoaderOptions): void {
  loaderOptions = Object.assign(loaderOptions, newOptions);
}
 
export function getOptions(): GeometryLoaderOptions {
  return loaderOptions;
}
 
export interface GeometryOptions {
  type: GeometryType;
  geometryData: PublicContourSetData | PublicSurfaceData | PublicMeshData;
  sizeInBytes?: number;
  segmentIndex?: number;
}
 
const geometryLoaders = {};
let unknownGeometryLoader;
 
/**
 * Load a geometry using a registered Cornerstone Geometry Loader.
 *
 * The geometry loader that is used will be
 * determined by the geometry loader scheme matching against the geometryId.
 *
 * @param geometryId - A Cornerstone Geometry Object's geometryId
 * @param options - Options to be passed to the Geometry Loader.
 *
 * @returns An Object which can be used to act after a geometry is loaded or loading fails
 *
 */
function loadGeometryFromGeometryLoader(
  geometryId: string,
  options?: GeometryOptions
): IGeometryLoadObject {
  const colonIndex = geometryId.indexOf(':');
  const scheme = geometryId.substring(0, colonIndex);
  let loader = geometryLoaders[scheme];
 
  if (loader === undefined || loader === null) {
    if (
      unknownGeometryLoader == null ||
      typeof unknownGeometryLoader !== 'function'
    ) {
      throw new Error(
        `No geometry loader for scheme ${scheme} has been registered`
      );
    }
 
    loader = unknownGeometryLoader;
  }
 
  const geometryLoadObject = loader(geometryId, options, loaderOptions);
 
  // Broadcast a geometry loaded event once the geometry is loaded
  geometryLoadObject.promise.then(
    function (geometry) {
      triggerEvent(eventTarget, Events.GEOMETRY_LOADED, { geometry });
    },
    function (error) {
      const errorObject = {
        geometryId,
        error,
      };
 
      triggerEvent(eventTarget, Events.GEOMETRY_LOADED_FAILED, errorObject);
    }
  );
 
  return geometryLoadObject;
}
 
/**
 * Loads a geometry given a geometryId and optional options and returns a promise which will resolve to
 * the loaded geometry object or fail if an error occurred. The loaded geometry is not stored in the cache.
 *
 * @param geometryId - A Cornerstone Geometry Object's geometryId
 * @param options - Options to be passed to the Geometry Loader
 *
 * @returns An Object which can be used to act after a geometry is loaded or loading fails
 */
export function loadGeometry(
  geometryId: string,
  options?: GeometryOptions
): Promise<IGeometry> {
  if (geometryId === undefined) {
    throw new Error('loadGeometry: parameter geometryId must not be undefined');
  }
 
  let geometryLoadObject = cache.getGeometryLoadObject(geometryId);
 
  if (geometryLoadObject !== undefined) {
    return geometryLoadObject.promise;
  }
 
  geometryLoadObject = loadGeometryFromGeometryLoader(geometryId, options);
 
  return geometryLoadObject.promise;
}
 
/**
 * Loads a geometry given a geometryId and optional options and returns a promise which will resolve to
 * the loaded geometry object or fail if an error occurred. The geometry is stored in the cache.
 *
 * @param geometryId - A Cornerstone Geometry Object's geometryId
 * @param options - Options to be passed to the Geometry Loader
 *
 * @returns Geometry Loader Object
 */
export async function loadAndCacheGeometry(
  geometryId: string,
  options?: GeometryOptions
): Promise<IGeometry> {
  if (geometryId === undefined) {
    throw new Error(
      'createAndCacheGeometry: parameter geometryId must not be undefined'
    );
  }
 
  let geometryLoadObject = cache.getGeometryLoadObject(geometryId);
 
  if (geometryLoadObject !== undefined) {
    return geometryLoadObject.promise;
  }
 
  geometryLoadObject = loadGeometryFromGeometryLoader(geometryId, options);
 
  await cache.putGeometryLoadObject(geometryId, geometryLoadObject);
 
  return geometryLoadObject.promise;
}
 
/**
 * Creates and caches a geometry synchronously
 *
 * @param geometryId - A Cornerstone Geometry Object's geometryId
 * @param options - Options to be passed to the Geometry Loader
 *
 * @returns IGeometry
 */
export function createAndCacheGeometry(
  geometryId: string,
  options: GeometryOptions
): IGeometry {
  Iif (geometryId === undefined) {
    throw new Error(
      'createAndCacheGeometry: parameter geometryId must not be undefined'
    );
  }
 
  let geometry = cache.getGeometry(geometryId);
 
  Iif (geometry) {
    return geometry;
  }
 
  if (options.type === GeometryType.CONTOUR) {
    geometry = createContourSet(
      geometryId,
      options.geometryData as PublicContourSetData
    );
  } else if (options.type === GeometryType.SURFACE) {
    geometry = createSurface(
      geometryId,
      options.geometryData as PublicSurfaceData
    );
  } else Eif (options.type === GeometryType.MESH) {
    createMesh(geometryId, options.geometryData as PublicMeshData).then(
      (mesh) => {
        geometry = mesh;
      }
    );
  } else {
    throw new Error(`Unknown geometry type: ${options.type}`);
  }
 
  cache.putGeometrySync(geometryId, geometry);
 
  return geometry;
}
 
/**
 * Registers a geometryLoader plugin with cornerstone for the specified scheme
 *
 * @param scheme - The scheme to use for this geometry loader (e.g. 'dicomweb', 'wadouri', 'http')
 * @param geometryLoader - A Cornerstone Geometry Loader function
 */
export function registerGeometryLoader(
  scheme: string,
  geometryLoader: GeometryLoaderFn
): void {
  geometryLoaders[scheme] = geometryLoader;
}
 
/**
 * Registers a new unknownGeometryLoader and returns the previous one
 *
 * @param geometryLoader - A Cornerstone Geometry Loader
 *
 * @returns The previous Unknown Geometry Loader
 */
export function registerUnknownGeometryLoader(
  geometryLoader: GeometryLoaderFn
): GeometryLoaderFn | undefined {
  const oldGeometryLoader = unknownGeometryLoader;
 
  unknownGeometryLoader = geometryLoader;
 
  return oldGeometryLoader;
}
 
registerGeometryLoader('mesh', cornerstoneMeshLoader);