Skip to content

feat: volume cropping - #2139

Merged
wayfarer3130 merged 134 commits into
cornerstonejs:mainfrom
mbellehumeur:feat-volume-cropping
Jul 28, 2025
Merged

feat: volume cropping #2139
wayfarer3130 merged 134 commits into
cornerstonejs:mainfrom
mbellehumeur:feat-volume-cropping

Conversation

@mbellehumeur

@mbellehumeur mbellehumeur commented Jun 12, 2025

Copy link
Copy Markdown
Contributor

Context

ticket: 9248395925

This pull request adds two tools: VolumeCroppingTool and VolumeCroppingControlTool.

The VolumeCroppingTool acts on a VRT viewport and provides the ability to crop the volume using control spheres/handles along the axial, sagittal and coronal axis and the corners of the cropping volume.
The display of the control spheres can be enabled/disabled through the setHandlesVisible/ getHandlesVisible methods.

The VolumeCroppingControlTool acts on 1, 2 or 3 orthographic viewports and adds reference lines that control the cropping volume and react to changes in the cropping volume.

Messaging between the tools is handled through Cornerstone events that are validated by the series instance UID of the volume. Therefore the tools do not need to be in the same tool group as the volume cropping tool. Multiple cropping & control instances can be used on different series in the same display.

Configurable items of the VolumeCroppingTool are the following:

// Basic setup
const toolGroup = ToolGroupManager.createToolGroup('myToolGroup');
toolGroup.addTool(VolumeCroppingControlTool.toolName);
toolGroup.addTool(VolumeCroppingTool.toolName);

// Configure with custom colors and settings
 toolGroup.setToolConfiguration(VolumeCroppingControlTool.toolName, {
 lineColors: {
   AXIAL: [1.0, 0.0, 0.0],    // Red for axial views
   CORONAL: [0.0, 1.0, 0.0],  // Green for coronal views
   SAGITTAL: [1.0, 1.0, 0.0], // Yellow for sagittal views
  },
  lineWidth: 2.0,
  extendReferenceLines: true,
  viewportIndicators: true
});

toolGroup.setToolConfiguration(VolumeCroppingTool.toolName, {
    showCornerSpheres: true,
    showHandles: true,
    showClippingPlanes: true,
    initialCropFactor: 0.08,
    sphereColors: {
      AXIAL: [1.0, 0.0, 0.0], // Red for axial (Z-axis)
      CORONAL: [0.0, 1.0, 0.0], // Green for coronal (Y-axis)
      SAGITTAL: [1.0, 1.0, 0.0], //  Yellow for sagittal (X-axis)
      CORNERS: [0.0, 0.0, 1.0], // Blue for corners
    },
    sphereRadius: 8,
    grabSpherePixelDistance: 20, //pixels threshold for closeness to the sphere being grabbed
    rotateIncrementDegrees: 2,
    rotateSampleDistanceFactor: 2, // Factor to increase sample distance (lower resolution) when rotating
},


// Activate the tool
toolGroup.setToolActive(VolumeCroppingControlTool.toolName);
toolGroup.setToolActive(VolumeCroppingTool.toolName);


// Programmatically control visibility
const tool = toolGroup.getToolInstance(VolumeCroppingTool.toolName);
tool.setHandlesVisible(true);
tool.setClippingPlanesVisible(true);

// Toggle visibility for interactive UI
function toggleCroppingInterface() {
  const handlesVisible = tool.getHandlesVisible();
  const planesVisible = tool.getClippingPlanesVisible();

  // Toggle handles (spheres and edge lines)
  tool.setHandlesVisible(!handlesVisible);

  // Toggle clipping effect
  tool.setClippingPlanesVisible(!planesVisible);

  console.log(`Handles: ${!handlesVisible ? 'shown' : 'hidden'}`);
  console.log(`Cropping: ${!planesVisible ? 'active' : 'disabled'}`);
}

The volumeCroppingTool example demonstrates the two tools working together.

A dropdown box allows to select the number of orthographic viewports for the VolumeCroppingControl tool: 1, 2 or 3.

There are two toggle buttons to toggle the display of the control spheres and clipping planes:

image

The toggle buttons demonstrate the use of the following public methods:

  • setHandlesVisible(visible: boolean): Show/hide manipulation spheres and edge lines
  • setClippingPlanesVisible(visible: boolean): Enable/disable volume clipping planes
  • getHandlesVisible(): Get current handle visibility state
  • getClippingPlanesVisible(): Get current clipping plane visibility state

Changes & Results

New files are VolumeCropping, VolumeCroppingControl and index.js for the example.

Testing

Run the example and use the spheres to modify the cropping. The volume should change the control lines in the orthogonal views as well.
Use the control lines in the orthogonal views to change the cropping volume.
Use the 'Toggle 3D handles' button to hide/show the control spheres.

In OHIF, do not use the 'run dev:fast' option. It will cause rendering problems when moving the first sphere quickly. Use 'bun run dev'.

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the
    semantic-release format and guidelines.

Code

  • My code has been well-documented (function documentation, inline comments,
    etc.)

Public Documentation Updates

  • The documentation page has been updated as necessary for any public API
    additions or removals.

…n VolumeViewport3D and update ZoomTool to handle clipping planes during zoom
- Imported VolumeCroppingTool in the tools index file.
- Exported VolumeCroppingTool alongside other manipulation tools.
…r VolumeCroppingTool

- Added a check in TrackballRotateTool to allow rotation only for VOLUME_3D viewports.
- Refactored VolumeCroppingTool to remove slab thickness handling and rotation points.
- Introduced reference lines for better annotation management.
- Updated annotation interfaces and removed unused methods related to slab thickness.
- Enhanced handle selection logic and improved code readability.
Comment thread packages/tools/examples/volumeCroppingTool/index.ts Fixed
Comment thread packages/tools/src/tools/VolumeCroppingTool.ts Fixed
…functions and enhancing sphere state management
…trolTool to enhance sphere handling and event management
…able initialCropFactor and clean up reference line handling
Comment thread packages/tools/src/tools/VolumeCroppingTool.ts Outdated
Comment thread packages/tools/src/tools/VolumeCroppingControlTool.ts
@mbellehumeur
mbellehumeur requested a review from sedghi July 22, 2025 12:25
…l and VolumeCroppingControlTool interactions
…bility control methods for cropping handles and planes

@sedghi sedghi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, great addition

@igoroctaviano igoroctaviano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great work @mbellehumeur

Comment thread packages/tools/src/tools/VolumeCroppingControlTool.ts
@wayfarer3130
wayfarer3130 merged commit e8a5a04 into cornerstonejs:main Jul 28, 2025
9 checks passed
@sedghi sedghi mentioned this pull request Aug 25, 2025
@sedghi sedghi mentioned this pull request Jun 4, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants