Skip to content

Fix: FreeHandRoiTool Statistics Text Not Displayed#2723

Merged
wayfarer3130 merged 3 commits into
cornerstonejs:mainfrom
nithin-trenser:fix-freehand-roi-stats-not-displayed
Jun 19, 2026
Merged

Fix: FreeHandRoiTool Statistics Text Not Displayed#2723
wayfarer3130 merged 3 commits into
cornerstonejs:mainfrom
nithin-trenser:fix-freehand-roi-stats-not-displayed

Conversation

@nithin-trenser

@nithin-trenser nithin-trenser commented May 6, 2026

Copy link
Copy Markdown
Contributor

Context

Fix issue: #2645

After drawing an annotation using the PlanarFreeHandRoiTool, the statistics text (such as area, mean, etc.) is not displayed next to the annotation when switching from 2D MPR view to Frame view. The ROI is created and rendered correctly on the viewport, but the associated statistics label is missing.

Changes & Results

Added an additional condition to ensure statistics are recalculated when cached stats for the current targetId are missing. The check now triggers when the annotation is invalidated or when the cached statistics for the specified target are not available, preventing missing measurement values.

fix-freehand-roi-stat-display.mp4

Testing

  • Draw a measurement using the PlanarFreeHandRoiTool in the 2D MPR view.
  • Switch the layout to Frame view.
  • In the right-side panel, locate and click the created measurement.
  • Observe the annotation displayed on the viewport.

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.

Tested Environment

  • OS: Ubuntu 24.04
  • Node version: 22
  • Browser: Chrome 147.0.7727.138

Summary by CodeRabbit

  • Bug Fixes
    • Improved freehand ROI contour statistics accuracy by clamping computed bounds to valid image extents to prevent out-of-range coordinate effects.
  • Performance / Reliability
    • Refined when ROI contour statistics are recalculated, using cached-stat availability and invalidation signals to avoid unnecessary recomputation while keeping results up to date.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@sen-trenser

Copy link
Copy Markdown

@sedghi Could you please take a look at this PR and provide your feedback?
Thanks!

Comment thread packages/tools/src/tools/annotation/PlanarFreehandROITool.ts Outdated

for (let j = 0; j < points.length; j++) {
const worldPosIndex = indexPoints[j].map(Math.floor);
const worldPosIndex = indexPoints[j].map((v) => Math.floor(v + 1e-3));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This isn't clear why you are doing this, nor that it is valid for things like WSI images.

@wayfarer3130 wayfarer3130 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you strip out the changes for worldPosIndex, or if they are really needed, then give a good explanation and make sure it really works reliably for data like WSI.
If you need ceiling/floor differences, then calculating both ceiling and floor rather than trying to do some not understandable logic that might not work reliably.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b092cba4-a5b1-4d4a-ac8e-d07ae0d8daa4

📥 Commits

Reviewing files that changed from the base of the PR and between 566b38b and 601cca3.

📒 Files selected for processing (1)
  • packages/tools/src/tools/annotation/PlanarFreehandROITool.ts

📝 Walkthrough

Walkthrough

Two targeted fixes in PlanarFreehandROITool.ts: the stats recalculation trigger in renderAnnotationInstance now fires when cached stats are missing for the current targetId in addition to when annotation.invalidated is set; and updateClosedCachedStats now clamps accumulated IJK bounding-box bounds to valid image dimensions before extrema computation.

Changes

PlanarFreehandROI Stats Fixes

Layer / File(s) Summary
Stats recalculation trigger condition
packages/tools/src/tools/annotation/PlanarFreehandROITool.ts
renderAnnotationInstance derives invalidated and cachedStats from annotation.data and calls _calculateStatsIfActive when invalidated is true OR when cachedStats has no entry for the current targetId.
Closed-contour IJK bounds clamping
packages/tools/src/tools/annotation/PlanarFreehandROITool.ts
updateClosedCachedStats retrieves image dimensions via imageData.getDimensions() and clamps each accumulated min/max index bound (iMin/iMax, jMin/jMax, kMin/kMax) to [0, dims[d]-1] before snapIndexBounds and bounding-box expansion; original indexPoints remain unmodified for perimeter use.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

  • cornerstonejs/cornerstone3D#2717: Modifies the same closed-contour cached stats computation path in PlanarFreehandROITool, adjusting how IJK bounds are constrained to prevent slice mismatch — closely related to this PR's per-point clamping approach.

Suggested reviewers

  • wayfarer3130

Poem

🐰 Hop! The stats were lost in the void,
No cached entry — the render was annoyed.
Now clamp those points within the image's rim,
No bounding box shall overflow the brim!
The freehand contour computes with glee,
Every target's stats returned correctly! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately describes the main fix: ensuring statistics text displays for the FreeHandRoiTool when switching views, directly addressing issue #2645.
Description check ✅ Passed The PR description includes all required sections with context, detailed changes, testing steps, and a completed checklist with environment specifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
packages/tools/src/tools/annotation/PlanarFreehandROITool.ts (1)

704-707: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Initialize data.cachedStats before the missing-cache recalculation path.

At Line 707, missing cachedStats now correctly triggers recalculation, but _calculateStatsIfActive later reads data.cachedStats[targetId] directly, which can throw when cachedStats is absent (allowed by the annotation type). Please initialize the cache map before dereferencing.

Suggested fix
  _calculateStatsIfActive(
    annotation: PlanarFreehandROIAnnotation,
    targetId: string,
    viewport,
    renderingEngine,
    enabledElement
  ) {
    const activeAnnotationUID = this.commonData?.annotation.annotationUID;

    if (
      annotation.annotationUID === activeAnnotationUID &&
      !this.commonData?.movingTextBox
    ) {
      return;
    }

    if (!this.commonData?.movingTextBox) {
      const { data } = annotation;
+     data.cachedStats ||= {};
      if (!data.cachedStats[targetId]?.unit) {
        data.cachedStats[targetId] = {
          Modality: null,
          area: null,
          max: null,
          mean: null,
          stdDev: null,
          areaUnit: null,
          unit: null,
        };
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tools/src/tools/annotation/PlanarFreehandROITool.ts` around lines
704 - 707, The issue is that when cachedStats is missing from the annotation
data, the subsequent call to _calculateStatsIfActive will attempt to read
data.cachedStats[targetId] directly, which will throw an error. To fix this,
initialize data.cachedStats as an empty object before the missing-cache
recalculation path executes. Specifically, when the condition at line 707
(invalidated || !cachedStats?.[targetId]) is true, ensure that data.cachedStats
is initialized to an empty object if it does not already exist, so that
_calculateStatsIfActive can safely write to and read from the cache map without
errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@packages/tools/src/tools/annotation/PlanarFreehandROITool.ts`:
- Around line 704-707: The issue is that when cachedStats is missing from the
annotation data, the subsequent call to _calculateStatsIfActive will attempt to
read data.cachedStats[targetId] directly, which will throw an error. To fix
this, initialize data.cachedStats as an empty object before the missing-cache
recalculation path executes. Specifically, when the condition at line 707
(invalidated || !cachedStats?.[targetId]) is true, ensure that data.cachedStats
is initialized to an empty object if it does not already exist, so that
_calculateStatsIfActive can safely write to and read from the cache map without
errors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 21961d64-b7c7-4bdd-8e38-350bbc9cfd40

📥 Commits

Reviewing files that changed from the base of the PR and between 7a0e2fd and 566b38b.

📒 Files selected for processing (1)
  • packages/tools/src/tools/annotation/PlanarFreehandROITool.ts

@wayfarer3130 wayfarer3130 merged commit 015c455 into cornerstonejs:main Jun 19, 2026
14 checks passed
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.

3 participants