Repository navigation
fix(tools): label the scale overlay in px without pixel spacing and honour a user calibration #2943
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
Open
namespaceMarcello
wants to merge
2
commits into
cornerstonejs:main
Choose a base branch
from
namespaceMarcello:fix/scale-overlay-units
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, it, expect, jest, beforeEach } from '@jest/globals'; | ||
| import { drawLine, drawTextBox } from '../src/drawingSvg'; | ||
| import { getAnnotations } from '../src/stateManagement/annotation/annotationState'; | ||
| import ScaleOverlayTool from '../src/tools/ScaleOverlayTool'; | ||
|
|
||
| jest.mock('../src/drawingSvg', () => ({ | ||
| drawLine: jest.fn(), | ||
| drawTextBox: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../src/stateManagement/annotation/annotationState', () => ({ | ||
| addAnnotation: jest.fn(), | ||
| getAnnotations: jest.fn(), | ||
| })); | ||
|
|
||
| /** | ||
| * Renders the scale over a 600 x 600 view whose world units map one to one | ||
| * to canvas pixels, and returns the label and the length of the scale line | ||
| * in canvas pixels. | ||
| */ | ||
| function renderScale(imageData) { | ||
| const tool = new ScaleOverlayTool(); | ||
| const viewport = { | ||
| id: 'viewport', | ||
| element: document.createElement('div'), | ||
| canvas: { width: 600, height: 600 }, | ||
| worldToCanvas: ([x, y]) => [x, y], | ||
| ...(imageData && { getImageData: () => imageData }), | ||
| }; | ||
| getAnnotations.mockReturnValue([ | ||
| { | ||
| annotationUID: 'scale', | ||
| data: { | ||
| viewportId: 'viewport', | ||
| handles: { | ||
| points: [ | ||
| [0, 0, 0], | ||
| [600, 0, 0], | ||
| [0, 600, 0], | ||
| [600, 600, 0], | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
| tool.editData = { viewport }; | ||
|
|
||
| tool.renderAnnotation({ viewport }, {}); | ||
|
|
||
| if (!drawLine.mock.calls.length) { | ||
| return { drawn: drawTextBox.mock.calls.length > 0 }; | ||
| } | ||
| const [, , , start, end] = drawLine.mock.calls[0]; | ||
| const [, , , lines] = drawTextBox.mock.calls[0]; | ||
| return { lines, length: Math.hypot(end[0] - start[0], end[1] - start[1]) }; | ||
| } | ||
|
|
||
| describe('ScaleOverlayTool units', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| Object.defineProperty(window, 'devicePixelRatio', { | ||
| configurable: true, | ||
| value: 1, | ||
| }); | ||
| }); | ||
|
|
||
| it('labels and sizes the scale in mm on an image with pixel spacing', () => { | ||
| const { lines, length } = renderScale({ | ||
| hasPixelSpacing: true, | ||
| spacing: [1, 1, 1], | ||
| }); | ||
|
|
||
| // 600 mm across: 250 mm is the size between 20% and 60% of the width | ||
| expect(lines).toEqual(['25 cm']); | ||
| expect(length).toBeCloseTo(250); | ||
| }); | ||
|
|
||
| it('labels the scale in pixels on an image without pixel spacing', () => { | ||
| // An ultrasound frame without pixel spacing: world units are pixels | ||
| const { lines, length } = renderScale({ | ||
| hasPixelSpacing: false, | ||
| spacing: [1, 1, 1], | ||
| }); | ||
|
|
||
| expect(lines).toEqual(['250 px']); | ||
| expect(length).toBeCloseTo(250); | ||
| }); | ||
|
|
||
| it('applies a user calibration to the length the scale covers', () => { | ||
| // A user calibration of 2: 2 world units measure 1 mm, so the view is | ||
| // 300 mm across, and 100 mm covers 200 world units | ||
| const { lines, length } = renderScale({ | ||
| hasPixelSpacing: true, | ||
| spacing: [1, 1, 1], | ||
| calibration: { type: 'User', scale: 2 }, | ||
| }); | ||
|
|
||
| expect(lines).toEqual(['10 cm']); | ||
| expect(length).toBeCloseTo(200); | ||
| }); | ||
|
|
||
| it('keeps mm on a viewport without image data', () => { | ||
| const { lines, length } = renderScale(undefined); | ||
|
|
||
| expect(lines).toEqual(['25 cm']); | ||
| expect(length).toBeCloseTo(250); | ||
| }); | ||
|
|
||
| it('draws no scale when none of the sizes fits the view', () => { | ||
| // A user calibration of 1000: the view is 0.6 mm across, below the | ||
| // smallest scale size | ||
| const result = renderScale({ | ||
| hasPixelSpacing: true, | ||
| spacing: [1, 1, 1], | ||
| calibration: { type: 'User', scale: 1000 }, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ drawn: false }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.