Skip to content

Commit

Permalink
fix: use one actor for a contourset rendering (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigobasilio2022 authored Feb 10, 2023
1 parent c25199e commit c92f8be
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
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

0 comments on commit c92f8be

Please sign in to comment.