Skip to content

Commit

Permalink
fix: Start moving adapters to shared base file to allow for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wayfarer3130 committed Jan 8, 2025
1 parent 5ec16c8 commit 281c4dc
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 129 deletions.
86 changes: 86 additions & 0 deletions packages/adapters/src/adapters/Cornerstone3D/BaseAdapter3D.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import CORNERSTONE_3D_TAG from "./cornerstone3DTag";
import MeasurementReport from "./MeasurementReport";

/**
* This is a basic definition of adapters to be inherited for other adapters.
*/
export default class BaseAdapter3D {
public static toolType: string;
public static utilityToolType: string;
public static TID300Representation;
public static trackingIdentifierTextValue: string;

/**
* @returns true if the tool is of the given tool type based on the tracking identifier
*/
public static isValidCornerstoneTrackingIdentifier = trackingIdentifier => {
if (!trackingIdentifier.includes(":")) {
return false;
}

const [cornerstone3DTag, toolType] = trackingIdentifier.split(":");

if (cornerstone3DTag !== CORNERSTONE_3D_TAG) {
return false;
}

return toolType === this.toolType;
};

/**
* Returns annotation data for CS3D to use based on the underlying
* DICOM SR annotation data.
*/
public static getMeasurementData(
MeasurementGroup,
sopInstanceUIDToImageIdMap,
_imageToWorldCoords,
metadata
) {
const { defaultState: state, ReferencedFrameNumber } =
MeasurementReport.getSetupMeasurementData(
MeasurementGroup,
sopInstanceUIDToImageIdMap,
metadata,
this.toolType
);

state.annotation.data = {
cachedStats: {},
frameNumber: ReferencedFrameNumber
};

return state;
}

public static getTID300RepresentationArguments(tool, worldToImageCoords) {
const { data, metadata } = tool;
const { finding, findingSites } = tool;
const { referencedImageId } = metadata;

if (!referencedImageId) {
throw new Error(
"Probe.getTID300RepresentationArguments: referencedImageId is not defined"
);
}

const { points } = data.handles;

const pointsImage = points.map(point => {
const pointImage = worldToImageCoords(referencedImageId, point);
return {
x: pointImage[0],
y: pointImage[1]
};
});

const TID300RepresentationArguments = {
points: pointsImage,
trackingIdentifierTextValue: this.trackingIdentifierTextValue,
findingSites: findingSites || [],
finding
};

return TID300RepresentationArguments;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { utilities } from "dcmjs";
import CORNERSTONE_3D_TAG from "./cornerstone3DTag";
import MeasurementReport from "./MeasurementReport";
import BaseAdapter3D from "./BaseAdapter3D";

const { Length: TID300Length } = utilities.TID300;

const LENGTH = "Length";
const trackingIdentifierTextValue = `${CORNERSTONE_3D_TAG}:${LENGTH}`;

class Length {
export default class Length extends BaseAdapter3D {
public static toolType = LENGTH;
public static utilityToolType = LENGTH;
public static TID300Representation = TID300Length;
public static trackingIdentifierTextValue = `${CORNERSTONE_3D_TAG}:${LENGTH}`;

// TODO: this function is required for all Cornerstone Tool Adapters, since it is called by MeasurementReport.
static getMeasurementData(
MeasurementGroup,
Expand All @@ -20,7 +25,7 @@ class Length {
MeasurementGroup,
sopInstanceUIDToImageIdMap,
metadata,
Length.toolType
this.toolType
);

const referencedImageId =
Expand Down Expand Up @@ -84,30 +89,11 @@ class Length {
point1,
point2,
distance,
trackingIdentifierTextValue,
trackingIdentifierTextValue: this.trackingIdentifierTextValue,
finding,
findingSites: findingSites || []
};
}
}

Length.toolType = LENGTH;
Length.utilityToolType = LENGTH;
Length.TID300Representation = TID300Length;
Length.isValidCornerstoneTrackingIdentifier = TrackingIdentifier => {
if (!TrackingIdentifier.includes(":")) {
return false;
}

const [cornerstone3DTag, toolType] = TrackingIdentifier.split(":");

if (cornerstone3DTag !== CORNERSTONE_3D_TAG) {
return false;
}

return toolType === LENGTH;
};

MeasurementReport.registerTool(Length);

export default Length;
106 changes: 0 additions & 106 deletions packages/adapters/src/adapters/Cornerstone3D/Probe.js

This file was deleted.

68 changes: 68 additions & 0 deletions packages/adapters/src/adapters/Cornerstone3D/Probe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { utilities } from "dcmjs";
import CORNERSTONE_3D_TAG from "./cornerstone3DTag";
import MeasurementReport from "./MeasurementReport";
import BaseAdapter3D from "./BaseAdapter3D";

const { Point: TID300Point } = utilities.TID300;

const PROBE = "Probe";
const trackingIdentifierTextValue = `${CORNERSTONE_3D_TAG}:${PROBE}`;

class Probe extends BaseAdapter3D {
public static toolType = PROBE;
public static utilityToolType = PROBE;
public static TID300Representation = TID300Point;

static getMeasurementData(
MeasurementGroup,
sopInstanceUIDToImageIdMap,
imageToWorldCoords,
metadata
) {
const state = super.getMeasurementData(
MeasurementGroup,
sopInstanceUIDToImageIdMap,
imageToWorldCoords,
metadata
);

const { defaultState, SCOORDGroup } =
MeasurementReport.getSetupMeasurementData(
MeasurementGroup,
sopInstanceUIDToImageIdMap,
metadata,
Probe.toolType
);

const referencedImageId =
defaultState.annotation.metadata.referencedImageId;

const { GraphicData } = SCOORDGroup;

const worldCoords = [];
for (let i = 0; i < GraphicData.length; i += 2) {
const point = imageToWorldCoords(referencedImageId, [
GraphicData[i],
GraphicData[i + 1]
]);
worldCoords.push(point);
}

state.annotation.data = {
...state.annotation.data,
handles: {
points: worldCoords,
activeHandleIndex: null,
textBox: {
hasMoved: false
}
}
};

return state;
}
}

MeasurementReport.registerTool(Probe);

export default Probe;

0 comments on commit 281c4dc

Please sign in to comment.