Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use one actor for a contourset rendering #432

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,75 @@ function addContourToElement(
viewport.addActor({ actor: actor1, uid: actorUID });
}

export default addContourToElement;
function addContourSetToElement(
element: HTMLDivElement,
contourSet: Types.IContourSet,
actorUID: string
): void {
const enabledElement = getEnabledElement(element);
const { renderingEngine, viewport } = enabledElement;
const { id: viewportId } = viewport;

// Default to true since we are setting a new segmentation, however,
// in the event listener, we will make other segmentations visible/invisible
// based on the config
const visibility = true;
const immediateRender = false;
const suppressEvents = true;

const actor1 = vtkActor.newInstance();
let color;

const pointArray = [];

const points = vtkPoints.newInstance();
const lines = vtkCellArray.newInstance();

// this variable will indicate the index of the first point in the current line
// so we can correctly generate the point index list to add in the cellArray
let pointIndex = 0;
contourSet.getContours().forEach((contour: Types.IContour) => {
const pointList = contour.getPoints();
const flatPoints = contour.getFlatPointsArray();
color = contour.getColor();
const type = contour.getType();

// creating a point index list that defines a line
const pointIndexes = pointList.map((_, i) => i + pointIndex);

// if close planar, add the first point index to the list
if (type === Enums.ContourType.CLOSED_PLANAR) {
pointIndexes.push(pointIndexes[0]);
}

const linePoints = Float32Array.from(flatPoints);
// add the curent points into the point list
pointArray.push(...linePoints);
// add the point indexes into the cell array
lines.insertNextCell([...pointIndexes]);
// update the first point index
pointIndex = pointIndex + pointList.length;
});

// converts the pointArray into vtkPoints
points.setData(pointArray, 3);

// creates the polydata
const polygon = vtkPolyData.newInstance();
polygon.setPoints(points);
polygon.setLines(lines);

const mapper1 = vtkMapper.newInstance();
mapper1.setInputData(polygon);
actor1.setMapper(mapper1);
actor1.getProperty().setLineWidth(4);

// despite each contour can have its own color, we assign the last color to
// all contours
const colorToUse = color.map((c) => c / 255);
actor1.getProperty().setColor(colorToUse[0], colorToUse[1], colorToUse[2]);

viewport.addActor({ actor: actor1, uid: actorUID });
}

export { addContourToElement, addContourSetToElement };
28 changes: 22 additions & 6 deletions packages/tools/src/tools/displayTools/Contour/contourDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {

import { deepMerge } from '../../../utilities';
import removeContourFromElement from './removeContourFromElement';
import addContourToElement from './addContourToElement';
import {
addContourToElement,
addContourSetToElement,
} from './addContourToElement';

/**
* It adds a new segmentation representation to the segmentation state
Expand Down Expand Up @@ -181,12 +184,25 @@ function _renderContourSets(
function _renderContourSet(
viewport: Types.IVolumeViewport,
contourSet: Types.IContourSet,
segmentationRepresentationUID: string
segmentationRepresentationUID: string,
separated = false
): void {
contourSet.getContours().forEach((contour: Types.IContour, index) => {
const contourUID = `${segmentationRepresentationUID}_${contourSet.id}_${index}}`;
_renderContour(viewport, contour, contourUID);
});
if (separated) {
contourSet.getContours().forEach((contour: Types.IContour, index) => {
const contourUID = `${segmentationRepresentationUID}_${contourSet.id}_${index}}`;
_renderContour(viewport, contour, contourUID);
});
} else {
const contourUID = `${segmentationRepresentationUID}_${contourSet.id}`;
const actorUID = contourUID;
const actorEntry = viewport.getActor(actorUID);

if (!actorEntry) {
addContourSetToElement(viewport.element, contourSet, actorUID);
} else {
throw new Error('Not implemented yet. (Update contour)');
}
}

viewport.resetCamera();
viewport.render();
Expand Down