Skip to content

Commit

Permalink
create namespace for imageLoader and volumeLoader
Browse files Browse the repository at this point in the history
remove getVolume from top level export

rename setVolumesOnViewports and addVolumesOnViewports

rename Utilities to utilities

add Enums top level export to cornerstone-render

remaining work for Enums for cornerstone render

rename Cursors to cursors

add annotation name space for annotations

add segmentation name space for all seg methods

rename setUseCPURender

remove error codes and put it in events

renamed enums to PascalCase

rename all cap enums to PascalCase

made orientation as CONSTANT

make cpu colormaps a CONSTANT

wip for creating enums for csTools

rename cornerstoneToolsEvents to Events

move BlendModes to Cornerstone core

move getVolumeViewportsContainingSameVolumes to utilities

change annotation style to annotation config

add documentation to cs-image-volume-streaming-loader

change createNewSegmentationForViewport to for ToolGroup

redo segmentation screenshots with the correct colorLUT

rebuild yarn lock
  • Loading branch information
sedghi authored and swederik committed Mar 22, 2022
1 parent 666596e commit ca7d1a9
Show file tree
Hide file tree
Showing 258 changed files with 4,554 additions and 4,551 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
RenderingEngine,
VIEWPORT_TYPE,
Enums,
init as csRenderInit,
Types,
} from '@precisionmetrics/cornerstone-render'
import * as cs from '@precisionmetrics/cornerstone-render'
import * as csTools3d from '@precisionmetrics/cornerstone-tools'

import { registerWebImageLoader } from '@precisionmetrics/cornerstone-image-loader-streaming-volume'
// import { registerWebImageLoader } from '@precisionmetrics/cornerstone-image-loader-streaming-volume'

const content = document.getElementById('content')

Expand All @@ -18,21 +18,20 @@ element.style.height = '500px'

content.appendChild(element)

console.warn('hello!! - ERIK')
async function setup() {
await csRenderInit()

csTools3d.init()

registerWebImageLoader(cs)
// registerWebImageLoader(cs)

const renderingEngineUID = 'myRenderingEngine'
const renderingEngine = new RenderingEngine(renderingEngineUID)

const viewportInput = [
{
viewportUID: 'CT_STACK',
type: VIEWPORT_TYPE.STACK,
type: Enums.ViewportType.STACK,
element,
defaultOptions: {
background: <Types.Point3>[0.2, 0, 0.2],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import {
EVENTS,
Enums,
eventTarget,
metaData,
imageLoadPoolManager,
triggerEvent,
ImageVolume,
cache,
Types,
loadImage,
Utilities as cornerstoneUtils,
REQUEST_TYPE,
ERROR_CODES,
imageLoader,
utilities as cornerstoneUtils,
} from '@precisionmetrics/cornerstone-render'

import { scaleArray, autoLoad } from './helpers'

const requestType = REQUEST_TYPE.Prefetch
const requestType = Enums.RequestType.Prefetch
const { getMinMax } = cornerstoneUtils

// TODO James wants another layer in between ImageVolume and SliceStreamingImageVolume
// which adds loaded/loading as an interface?

/**
* Streaming Image Volume Class that extends ImageVolume base class.
* It implements load method to load the imageIds and insert them into the volume.
*/
export default class StreamingImageVolume extends ImageVolume {
private _cornerstoneImageMetaData

Expand All @@ -45,8 +44,6 @@ export default class StreamingImageVolume extends ImageVolume {

/**
* Creates the metadata required for converting the volume to an cornerstoneImage
*
* @returns {void}
*/
private _createCornerstoneImageMetaData() {
const numImages = this.imageIds.length
Expand Down Expand Up @@ -99,6 +96,11 @@ export default class StreamingImageVolume extends ImageVolume {
return true
}

/**
* It cancels loading the images of the volume. It sets the loading status to false
* and filters any imageLoad request in the requestPoolManager that has the same
* volumeUID
*/
public cancelLoading(): void {
const { loadStatus } = this

Expand All @@ -124,10 +126,19 @@ export default class StreamingImageVolume extends ImageVolume {
imageLoadPoolManager.filterRequests(filterFunction)
}

/**
* Clear the load callbacks
*/
public clearLoadCallbacks(): void {
this.loadStatus.callbacks = []
}

/**
* It triggers a prefetch for images in the volume.
* @param callback - A callback function to be called when the volume is fully loaded
* @param priority - The priority for loading the volume images, lower number is higher priority
* @returns
*/
public load = (
callback: (...args: unknown[]) => void,
priority = 5
Expand Down Expand Up @@ -162,9 +173,16 @@ export default class StreamingImageVolume extends ImageVolume {
}

/**
* Useful for sorting requests outside of the volume loader itself
* e.g. loading a single slice of CT, followed by a single slice of PET, before
* moving to the next slice
* It returns the imageLoad requests for the streaming image volume instance.
* It involves getting all the imageIds of the volume and creating a success callback
* which would update the texture (when the image has loaded) and the failure callback.
* Note that this method does not run executes the requests but only returns the requests.
* It can be used for sorting requests outside of the volume loader itself
* e.g. loading a single slice of CT, followed by a single slice of PET (interleaved), before
* moving to the next slice.
*
* @returns Array of requests including imageId of the request, its imageIdIndex,
* options (targetBuffer and scaling parameters), and additionalDetails (volumeUID)
*/
public getImageLoadRequests = () => {
const { scalarData, loadStatus } = this
Expand Down Expand Up @@ -294,7 +312,7 @@ export default class StreamingImageVolume extends ImageVolume {
imageVolume: volume,
}

triggerEvent(eventTarget, EVENTS.IMAGE_VOLUME_MODIFIED, eventDetail)
triggerEvent(eventTarget, Enums.Events.IMAGE_VOLUME_MODIFIED, eventDetail)

if (framesProcessed === numFrames) {
loadStatus.loaded = true
Expand Down Expand Up @@ -358,7 +376,7 @@ export default class StreamingImageVolume extends ImageVolume {
imageId,
}

triggerEvent(eventTarget, ERROR_CODES.IMAGE_LOAD_ERROR, eventDetail)
triggerEvent(eventTarget, Enums.Events.IMAGE_LOAD_ERROR, eventDetail)
}

const requests = imageIds.map((imageId, imageIdIndex) => {
Expand Down Expand Up @@ -404,7 +422,7 @@ export default class StreamingImageVolume extends ImageVolume {
// Use loadImage because we are skipping the Cornerstone Image cache
// when we load directly into the Volume cache
function callLoadImage(imageId, imageIdIndex, options) {
return loadImage(imageId, options).then(
return imageLoader.loadImage(imageId, options).then(
() => {
successCallback(imageIdIndex, imageId, scalingParameters)
},
Expand All @@ -427,6 +445,7 @@ export default class StreamingImageVolume extends ImageVolume {

return requests
}

private _prefetchImageIds(priority: number) {
const requests = this.getImageLoadRequests()

Expand Down Expand Up @@ -639,7 +658,6 @@ export default class StreamingImageVolume extends ImageVolume {
* It iterates over all the imageIds and convert them until there is no
* enough space left inside the imageCache. Finally it will decache the Volume.
*
* @returns {void}
*/
private _convertToImages() {
// 1. Try to decache images in the volatile Image Cache to provide
Expand Down Expand Up @@ -683,6 +701,12 @@ export default class StreamingImageVolume extends ImageVolume {
this._removeFromCache()
}

/**
* If completelyRemove is true, remove the volume completely from the cache. Otherwise,
* convert the volume to cornerstone images (stack images) and store it in the cache
* @param completelyRemove - If true, the image will be removed from the
* cache completely.
*/
public decache(completelyRemove = false): void {
if (completelyRemove) {
this._removeFromCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import {
registerVolumeLoader,
registerUnknownVolumeLoader,
volumeLoader,
cache,
Utilities,
ERROR_CODES,
utilities,
Enums,
Types,
} from '@precisionmetrics/cornerstone-render'
import { vec3 } from 'gl-matrix'
import { makeVolumeMetadata, sortImageIdsAndGetSpacing } from './helpers'
import StreamingImageVolume from './StreamingImageVolume'

const { createUint8SharedArray, createFloat32SharedArray } = Utilities
const { createUint8SharedArray, createFloat32SharedArray } = utilities

interface IVolumeLoader {
promise: Promise<StreamingImageVolume>
cancel: () => void
}

/**
* It handles loading of a image by streaming in its imageIds. It will be the
* volume loader if the schema for the volumeID is `cornerstoneStreamingImageVolume`.
* This function returns a promise that resolves to the StreamingImageVolume instance.
*
* In order to use the cornerstoneStreamingImageVolumeLoader you should use
* createAndCacheVolume helper from the cornerstone-core volumeLoader module.
*
* @param volumeId - The ID of the volume
* @param options - options for loading, imageIds
* @returns a promise that resolves to a StreamingImageVolume
*/
function cornerstoneStreamingImageVolumeLoader(
volumeId: string,
options: {
Expand Down Expand Up @@ -92,7 +103,7 @@ function cornerstoneStreamingImageVolumeLoader(
// check if there is enough space in unallocated + image Cache
const isCacheable = cache.isCacheable(sizeInBytes)
if (!isCacheable) {
throw new Error(ERROR_CODES.CACHE_SIZE_EXCEEDED)
throw new Error(Enums.Events.CACHE_SIZE_EXCEEDED)
}

cache.decacheIfNecessaryUntilBytesAvailable(sizeInBytes)
Expand Down Expand Up @@ -162,8 +173,8 @@ function cornerstoneStreamingImageVolumeLoader(
}
}

registerUnknownVolumeLoader(cornerstoneStreamingImageVolumeLoader)
registerVolumeLoader(
volumeLoader.registerUnknownVolumeLoader(cornerstoneStreamingImageVolumeLoader)
volumeLoader.registerVolumeLoader(
'cornerstoneStreamingImageVolume',
cornerstoneStreamingImageVolumeLoader
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {
getRenderingEngines,
RenderingEngine,
getVolumeViewportsContainingVolumeUID,
utilities,
} from '@precisionmetrics/cornerstone-render'

type RenderingEngineAndViewportUIDs = {
renderingEngine: RenderingEngine | undefined
viewportUIDs: Array<string>
}

/**
* Given a volumeUID, it finds the viewports and renderingEngines that
* include that volume, and triggers a render if renderingEngine is available.
*
* @param volumeUID - The UID of the volume
*/
const autoLoad = (volumeUID: string): void => {
const renderingEngineAndViewportUIDs =
getRenderingEngineAndViewportsContainingVolume(volumeUID)
Expand Down Expand Up @@ -38,7 +44,7 @@ function getRenderingEngineAndViewportsContainingVolume(

for (let i = 0; i < renderingEnginesArray.length; i++) {
const renderingEngine = renderingEnginesArray[i]
const viewports = getVolumeViewportsContainingVolumeUID(
const viewports = utilities.getVolumeViewportsContainingVolumeUID(
volumeUID,
renderingEngine.uid
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { metaData, Types } from '@precisionmetrics/cornerstone-render'

/**
* It creates a metadata object for a volume given the imageIds that compose it.
* It uses the first imageId to get the metadata.
*
* @param imageIds - array of imageIds
* @returns The volume metadata
*/
export default function makeVolumeMetadata(
imageIds: Array<string>
): Types.Metadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// array is of type Any number array

type ScalingParameters = {
modality: string
rescaleSlope: number
rescaleIntercept: number
suvbw?: number
}
import type { Types } from '@precisionmetrics/cornerstone-render'

/**
* Given a pixel array, rescale the pixel values using the rescale slope and
* intercept and if modality is PT it uses the suv values to scale the array
* @param array - The array to be scaled.
* @param scalingParameters - The scaling parameters
* @returns The array is being scaled
*/
export default function scaleArray(
array: Float32Array | Uint8Array,
scalingParameters: ScalingParameters
scalingParameters: Types.ScalingParameters
): Float32Array | Uint8Array {
const arrayLength = array.length
const { rescaleSlope, rescaleIntercept, suvbw } = scalingParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type SortedImageIdsItem = {
* @param imageIds - array of imageIds
* @param scanAxisNormal - [x, y, z] array or gl-matrix vec3
*
* @returns The sortedImageIds, zSpacing, and origin of the first imageg in the series.
* @returns The sortedImageIds, zSpacing, and origin of the first image in the series.
*/
export default function sortImageIdsAndGetSpacing(
imageIds: Array<string>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import cornerstoneStreamingImageVolumeLoader from './cornerstoneStreamingImageVolumeLoader'
import sharedArrayBufferImageLoader from './sharedArrayBufferImageLoader'
import StreamingImageVolume from './StreamingImageVolume'
import { registerWebImageLoader } from './registerWebImageLoader'

export {
cornerstoneStreamingImageVolumeLoader,
sharedArrayBufferImageLoader,
StreamingImageVolume,
registerWebImageLoader,
}
Loading

0 comments on commit ca7d1a9

Please sign in to comment.