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 | 178x 178x 178x 178x 178x 178x 24028x 12x 12x 178x | import { PubSubService, Types as OhifTypes } from '@ohif/core';
import { RENDERING_ENGINE_ID } from '../ViewportService/constants';
import { getRenderingEngine } from '@cornerstonejs/core';
import { getViewportAdapter } from '../ViewportService/adapter';
import { ColorbarOptions, ChangeTypes } from '../../types/Colorbar';
export default class ColorbarService extends PubSubService {
static EVENTS = {
STATE_CHANGED: 'event::ColorbarService:stateChanged',
};
public static REGISTRATION = {
name: 'colorbarService',
create: ({ servicesManager }: OhifTypes.Extensions.ExtensionParams) => {
return new ColorbarService(servicesManager);
},
};
/**
* Structure of colorbars state:
* {
* [viewportId]: [
* {
* displaySetInstanceUID: string,
* colorbar: {
* activeColormapName: string,
* colormaps: array,
* volumeId: string (optional),
* }
* }
* ]
* }
*/
colorbars = {};
servicesManager: AppTypes.ServicesManager;
constructor(servicesManager: AppTypes.ServicesManager) {
super(ColorbarService.EVENTS);
this.servicesManager = servicesManager;
}
/**
* Adds a colorbar to a specific viewport identified by `viewportId`, using the provided `displaySetInstanceUIDs` and `options`.
* This method prepares the colorbar state that will be used by the ViewportColorbarsContainer component.
*
* @param viewportId The identifier for the viewport where the colorbar will be added.
* @param displaySetInstanceUIDs An array of display set instance UIDs to associate with the colorbar.
* @param options Configuration options for the colorbar, including position, colormaps, active colormap name, ticks, and width.
*/
public addColorbar(
viewportId: string,
displaySetInstanceUIDs: string[],
options = {} as ColorbarOptions
) {
const { displaySetService } = this.servicesManager.services;
const renderingEngine = getRenderingEngine(RENDERING_ENGINE_ID);
const viewport = renderingEngine.getViewport(viewportId);
Iif (!viewport) {
return;
}
const adapter = getViewportAdapter(viewport);
Iif (!adapter.hasContent()) {
return;
}
const { activeColormapName, colormaps } = options;
displaySetInstanceUIDs.forEach(displaySetInstanceUID => {
// don't show colorbar for overlay display sets (e.g. segmentation)
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
Iif (displaySet.isOverlayDisplaySet) {
return;
}
const dataId = adapter.getDataIdForDisplaySet(displaySetInstanceUID);
const properties = adapter.getPresentation(dataId);
const colormap = properties?.colormap;
Iif (activeColormapName && !colormap) {
this.setViewportColormap(
viewportId,
displaySetInstanceUID,
colormaps[activeColormapName],
true
);
}
// Prepare colorbar data for the React component
const colorbarData = {
activeColormapName: colormap?.name || options?.activeColormapName || 'Grayscale',
colormaps: options.colormaps ? Object.values(options.colormaps) : [],
volumeId: dataId,
dataId,
};
// Store the colorbar data in the service state
if (this.colorbars[viewportId]) {
// Check if there's already an entry for this displaySetInstanceUID
const existingIndex = this.colorbars[viewportId].findIndex(
item => item.displaySetInstanceUID === displaySetInstanceUID
);
if (existingIndex !== -1) {
// Update existing colorbar
this.colorbars[viewportId][existingIndex].colorbar = colorbarData;
} else {
// Add new colorbar
this.colorbars[viewportId].push({
colorbar: colorbarData,
displaySetInstanceUID,
});
}
} else {
// Create new colorbar array for this viewport
this.colorbars[viewportId] = [
{
colorbar: colorbarData,
displaySetInstanceUID,
},
];
}
});
// Notify listeners about the state change
this._broadcastEvent(ColorbarService.EVENTS.STATE_CHANGED, {
viewportId,
changeType: ChangeTypes.Added,
});
}
/**
* Removes a colorbar from a specific viewport. If displaySetInstanceUID is provided,
* only the colorbar associated with that specific displaySetInstanceUID will be removed.
* Otherwise, all colorbars for the given viewport will be removed.
*
* @param viewportId The identifier for the viewport from which the colorbar will be removed.
* @param displaySetInstanceUID Optional. The specific display set instance UID associated with the colorbar to remove.
*/
public removeColorbar(viewportId, displaySetInstanceUID?: string) {
const colorbarInfo = this.colorbars[viewportId];
Iif (!colorbarInfo) {
return;
}
if (displaySetInstanceUID) {
// Find the index of the colorbar with the matching displaySetInstanceUID
const index = colorbarInfo.findIndex(
info => info.displaySetInstanceUID === displaySetInstanceUID
);
Iif (index !== -1) {
// Remove the colorbar from the array
colorbarInfo.splice(index, 1);
// If there are no more colorbars for this viewport, remove the entry
Iif (colorbarInfo.length === 0) {
delete this.colorbars[viewportId];
}
}
} else {
delete this.colorbars[viewportId];
}
this._broadcastEvent(ColorbarService.EVENTS.STATE_CHANGED, {
viewportId,
displaySetInstanceUID,
changeType: ChangeTypes.Removed,
});
}
/**
* Checks whether a colorbar is associated with a given viewport ID.
*
* @param viewportId The identifier for the viewport to check.
* @returns `true` if a colorbar exists for the specified viewport, otherwise `false`.
*/
public hasColorbar(viewportId) {
return this.colorbars[viewportId] ? true : false;
}
/**
* Retrieves the current state of colorbars, including all active colorbars and their configurations.
*
* @returns An object representing the current state of all colorbars managed by this service.
*/
public getState() {
return this.colorbars;
}
/**
* Retrieves colorbar information for a specific viewport ID.
*
* @param viewportId The identifier for the viewport to retrieve colorbar information for.
* @returns The colorbar information associated with the specified viewport, if available.
*/
public getViewportColorbar(viewportId) {
return this.colorbars[viewportId];
}
/**
* Handles the cleanup and removal of all colorbars from the viewports. This is typically called
* when exiting the mode or context in which the colorbars are used, ensuring that no DOM
* elements or references are left behind.
*/
public onModeExit() {
const viewportIds = Object.keys(this.colorbars);
viewportIds.forEach(viewportId => {
this.removeColorbar(viewportId);
});
}
/**
* Sets the colormap for a viewport. This function is used internally to update the colormap the viewport
*
* @param viewportId The identifier of the viewport to update.
* @param displaySetInstanceUID The display set instance UID associated with the viewport.
* @param colormap The colormap object to set on the viewport.
* @param immediate A boolean indicating whether the viewport should be re-rendered immediately after setting the colormap.
*/
private setViewportColormap(viewportId, displaySetInstanceUID, colormap, immediate = false) {
const renderingEngine = getRenderingEngine(RENDERING_ENGINE_ID);
const viewport = renderingEngine.getViewport(viewportId);
Iif (!viewport) {
return;
}
const adapter = getViewportAdapter(viewport);
Iif (!adapter.hasContent()) {
return;
}
// Address the display set's binding (volumeId on legacy multi-volume, bare
// UID on native, active binding otherwise)
const dataId = adapter.getDataIdForDisplaySet(displaySetInstanceUID);
adapter.setPresentation({ colormap }, dataId);
Iif (immediate) {
viewport.render();
}
}
}
|