What is the proper way to update an existing contour segmentation
I’m using VTK to load an STL file and geometryLoader to cache the geometry data for use in surface or contour segmentation. I apply initial position and rotation using vtkMatrixBuilder and then create the surface geometry with geometryLoader.createAndCacheGeometry as you can see in below code. After that, I convert the surface to a contour segmentation, showing the surface in the 3D volume viewport and the contour in the orthographic viewport using the segmentation API.
Now, I’m managing the STL model’s position and rotation using React state, controlled through input sliders. When the state changes, I reapply the transformation to the points using vtkMatrixBuilder and try to update the geometry data.
However, once the contour segmentation is loaded, I’m not sure how to reflect the updated position and rotation in the orthographic viewport. What’s the proper way to update the segmentation when the underlying geometry changes?
This is how i’m loading the segmentation
const setSegmentation = async (lengthToolData: LengthToolTypes) => {
const { position, rotation, url: stlFileUrl } = lengthToolData.screw;
const geometryDetails = await createAndCacheGeometriesFromSurfaces(
position,
{ x: 1, y: 1, z: 1 },
rotation,
stlFileUrl,
"axial"
);
const segmentationId = lengthToolData.name;
const segmentationConfig = {
segmentationId,
representation: {
type: csToolsEnums.SegmentationRepresentations.Surface,
data: { geometryIds: geometryDetails.geometryIds }
}
};
segmentation.addSegmentations([segmentationConfig]);
const segmentRepConfig = {
segmentationId,
type: csToolsEnums.SegmentationRepresentations.Contour,
};
segmentation.addSegmentationRepresentations(viewportIds[0], [segmentRepConfig]);
};
export async function createAndCacheGeometriesFromSurfaces(
position = { x: 0, y: 0, z: 0 },
scale = { x: 1, y: 1, z: 1 },
rotation = { x: 0, y: 0, z: 0 },
stlFileUrl: string | undefined,
type: string
): Promise<any> {
const stlReader = vtkSTLReader.newInstance();
await stlReader.setUrl(stlFileUrl as string);
const polyData = stlReader.getOutputData();
const pointsArray = polyData.getPoints().getData();
const polysArray = polyData.getPolys().getData();
const transform = vtkMatrixBuilder
.buildFromDegree()
.identity()
.translate(position.x, position.y, position.z)
.rotateX(rotation.x)
.rotateY(rotation.y)
.rotateZ(rotation.z);
const transformedPoints = new Float32Array(pointsArray.length);
for (let i = 0; i < pointsArray.length; i += 3) {
const point = [pointsArray[i], pointsArray[i + 1], pointsArray[i + 2]];
transform.apply(point);
transformedPoints[i] = point[0];
transformedPoints[i + 1] = point[1];
transformedPoints[i + 2] = point[2];
}
const geometryId = `surface-geometry-${Date.now()}`;
geometryLoader.createAndCacheGeometry(geometryId, {
type: GeometryType.SURFACE,
geometryData: {
points: transformedPoints,
polys: polysArray,
id: geometryId,
segmentIndex: 1,
frameOfReferenceUID: "some-unique-id-surface",
}
});
const geometryIds = new Map<number, string>();
geometryIds.set(1, geometryId);
return {
points: [...pointsArray],
polys: [...polysArray],
geometryId,
geometryIds,
transformedPoints
};
}
Thank you in advance for your help!
Best regards,
Mannan Qureshi