Skip to content

Commit aeee51b

Browse files
authored
fix(null-safety): Add null checks in various segmentation related code. (#2550)
1 parent 744949c commit aeee51b

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

packages/tools/src/stateManagement/segmentation/segmentIndex.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ function setActiveSegmentIndex(
6969

7070
viewportIds.forEach((viewportId) => {
7171
const toolGroup = getToolGroupForViewport(viewportId);
72+
if (!toolGroup) {
73+
return;
74+
}
7275
invalidateBrushCursor(toolGroup.id);
7376
});
7477
}

packages/tools/src/tools/base/ContourSegmentationBaseTool.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ abstract class ContourSegmentationBaseTool extends ContourBaseTool {
190190

191191
const viewportIds = getViewportIdsWithSegmentation(segmentationId);
192192

193-
const toolGroupIds = viewportIds.map((viewportId) => {
194-
const toolGroup = getToolGroupForViewport(viewportId);
195-
return toolGroup.id;
196-
});
193+
const toolGroupIds = viewportIds
194+
.map((viewportId) => {
195+
const toolGroup = getToolGroupForViewport(viewportId);
196+
return toolGroup?.id;
197+
})
198+
.filter((toolGroupId): toolGroupId is string => toolGroupId != null);
197199

198200
triggerAnnotationRenderForToolGroupIds(toolGroupIds);
199201
}

packages/tools/src/tools/segmentation/strategies/BrushStrategy.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export type InitializedOperationData = LabelmapToolOperationDataAny & {
6060
viewportIdsToRender: string[];
6161
centerCanvas?: Array<number>;
6262
viewport: Types.IViewport;
63-
}
63+
};
6464
memo?: LabelmapMemo;
6565
modified?: boolean;
6666
};
@@ -183,18 +183,24 @@ export default class BrushStrategy {
183183
this.configurationName = name;
184184

185185
// Ensuring backwards compatibility - always have a circular cursor if none is defined
186-
const cursorGeometryInitializer = initializers.find(init => init.hasOwnProperty(StrategyCallbacks.CalculateCursorGeometry))
187-
const renderCursorInitializer = initializers.find(init => init.hasOwnProperty(StrategyCallbacks.RenderCursor));
186+
const cursorGeometryInitializer = initializers.find((init) =>
187+
init.hasOwnProperty(StrategyCallbacks.CalculateCursorGeometry)
188+
);
189+
const renderCursorInitializer = initializers.find((init) =>
190+
init.hasOwnProperty(StrategyCallbacks.RenderCursor)
191+
);
188192

189-
if(!cursorGeometryInitializer) {
193+
if (!cursorGeometryInitializer) {
190194
initializers.push({
191-
[StrategyCallbacks.CalculateCursorGeometry]: compositions.circularCursor.calculateCursorGeometry
195+
[StrategyCallbacks.CalculateCursorGeometry]:
196+
compositions.circularCursor.calculateCursorGeometry,
192197
});
193198
}
194199

195-
if(!renderCursorInitializer) {
200+
if (!renderCursorInitializer) {
196201
initializers.push({
197-
[StrategyCallbacks.RenderCursor]: compositions.circularCursor.renderCursor
202+
[StrategyCallbacks.RenderCursor]:
203+
compositions.circularCursor.renderCursor,
198204
});
199205
}
200206

@@ -263,7 +269,12 @@ export default class BrushStrategy {
263269

264270
const data = getStrategyData({ operationData, viewport, strategy: this });
265271

266-
if (!data) {
272+
if (
273+
!data ||
274+
!data.imageVoxelManager ||
275+
!data.segmentationVoxelManager ||
276+
!data.segmentationImageData
277+
) {
267278
return null;
268279
}
269280

@@ -411,7 +422,6 @@ export default class BrushStrategy {
411422
enabledElement: Types.IEnabledElement,
412423
operationData: InitializedOperationData
413424
) => void;
414-
415425
}
416426
/**
417427
* Adds a list method to the set of defined methods.

packages/tools/src/utilities/segmentation/getOrCreateSegmentationVolume.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function getOrCreateSegmentationVolume(
1515
segmentationId: string
1616
): Types.IImageVolume | undefined {
1717
const { representationData } = getSegmentation(segmentationId);
18+
if (!representationData.Labelmap) {
19+
return;
20+
}
21+
1822
let { volumeId } =
1923
representationData.Labelmap as LabelmapSegmentationDataVolume;
2024

0 commit comments

Comments
 (0)