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: jumpToSlice and scaling of images in renderToCanvas #78

Merged
merged 9 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 8 additions & 8 deletions common/reviews/api/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ interface IRenderingEngine {
// (undocumented)
renderViewports(viewportIds: Array<string>): void;
// (undocumented)
resize(immediate?: boolean, resetPanZoomForViewPlane?: boolean): void;
resize(immediate?: boolean, resetPan?: boolean, resetZoom?: boolean): void;
// (undocumented)
setViewports(viewports: Array<PublicViewportInput>): void;
}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ interface IStackViewport extends IViewport {
// (undocumented)
modality: string;
// (undocumented)
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
// (undocumented)
resetProperties(): void;
// (undocumented)
Expand Down Expand Up @@ -1279,7 +1279,7 @@ interface IVolumeViewport extends IViewport {
// (undocumented)
removeVolumeActors(actorUIDs: Array<string>, immediate?: boolean): void;
// (undocumented)
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
// (undocumented)
setSlabThickness(slabThickness: number): void;
// (undocumented)
Expand Down Expand Up @@ -1450,13 +1450,13 @@ export class RenderingEngine implements IRenderingEngine {
// (undocumented)
renderViewports(viewportIds: Array<string>): void;
// (undocumented)
resize(immediate?: boolean, resetPanZoomForViewPlane?: boolean): void;
resize(immediate?: boolean, resetPan?: boolean, resetZoom?: boolean): void;
// (undocumented)
setViewports(publicViewportInputEntries: Array<PublicViewportInput>): void;
}

// @public (undocumented)
function renderToCanvas(canvas: HTMLCanvasElement, image: IImage): void;
function renderToCanvas(canvas: HTMLCanvasElement, image: IImage, modality?: string): void;

// @public (undocumented)
enum RequestType {
Expand Down Expand Up @@ -1587,7 +1587,7 @@ export class StackViewport extends Viewport implements IStackViewport {
// (undocumented)
removeAllActors(): void;
// (undocumented)
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
// (undocumented)
resetProperties(): void;
// (undocumented)
Expand Down Expand Up @@ -1816,7 +1816,7 @@ export class Viewport implements IViewport {
// (undocumented)
reset(immediate?: boolean): void;
// (undocumented)
protected resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
protected resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
// (undocumented)
protected resetCameraNoEvent(): void;
// (undocumented)
Expand Down Expand Up @@ -1975,7 +1975,7 @@ export class VolumeViewport extends Viewport implements IVolumeViewport {
// (undocumented)
removeVolumeActors(actorUIDs: Array<string>, immediate?: boolean): void;
// (undocumented)
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
// (undocumented)
setSlabThickness(slabThickness: number): void;
// (undocumented)
Expand Down
6 changes: 3 additions & 3 deletions common/reviews/api/streaming-image-volume-loader.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ interface IRenderingEngine {
// (undocumented)
renderViewports(viewportIds: Array<string>): void;
// (undocumented)
resize(immediate?: boolean, resetPanZoomForViewPlane?: boolean): void;
resize(immediate?: boolean, resetPan?: boolean, resetZoom?: boolean): void;
// (undocumented)
setViewports(viewports: Array<PublicViewportInput>): void;
}
Expand All @@ -770,7 +770,7 @@ interface IStackViewport extends IViewport {
hasImageURI: (imageURI: string) => boolean;
// (undocumented)
modality: string;
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
resetProperties(): void;
resize: () => void;
scaling: Scaling;
Expand Down Expand Up @@ -928,7 +928,7 @@ interface IVolumeViewport extends IViewport {
getProperties: () => any;
getSlabThickness(): number;
removeVolumeActors(actorUIDs: Array<string>, immediate?: boolean): void;
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
setSlabThickness(slabThickness: number): void;
setVolumes(
volumeInputArray: Array<IVolumeInput>,
Expand Down
35 changes: 18 additions & 17 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ type AnnotationModifiedEventType = Types_2.CustomEventType<AnnotationModifiedEve

// @public (undocumented)
type AnnotationRemovedEventDetail = {
viewportId: string;
renderingEngineId: string;
annotation: Annotation;
FrameOfReferenceUID: string;
};

// @public (undocumented)
Expand Down Expand Up @@ -890,13 +889,6 @@ const _default: {
getPointInLineOfSightWithCriteria: typeof getPointInLineOfSightWithCriteria;
};

// @public (undocumented)
const _default_2: {
snapFocalPointToSlice: typeof snapFocalPointToSlice;
getSliceRange: typeof getSliceRange;
scrollThroughStack: typeof scrollThroughStack;
};

// @public (undocumented)
function deselectAnnotation(annotation?: Annotation): void;

Expand Down Expand Up @@ -1769,7 +1761,7 @@ interface IRenderingEngine {
// (undocumented)
renderViewports(viewportIds: Array<string>): void;
// (undocumented)
resize(immediate?: boolean, resetPanZoomForViewPlane?: boolean): void;
resize(immediate?: boolean, resetPan?: boolean, resetZoom?: boolean): void;
// (undocumented)
setViewports(viewports: Array<PublicViewportInput>): void;
}
Expand Down Expand Up @@ -1808,7 +1800,7 @@ interface IStackViewport extends IViewport {
hasImageURI: (imageURI: string) => boolean;
// (undocumented)
modality: string;
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
resetProperties(): void;
resize: () => void;
scaling: Scaling;
Expand Down Expand Up @@ -2033,7 +2025,7 @@ interface IVolumeViewport extends IViewport {
getProperties: () => any;
getSlabThickness(): number;
removeVolumeActors(actorUIDs: Array<string>, immediate?: boolean): void;
resetCamera(resetPanZoomForViewPlane?: boolean): boolean;
resetCamera(resetPan?: boolean, resetZoom?: boolean): boolean;
setSlabThickness(slabThickness: number): void;
setVolumes(
volumeInputArray: Array<IVolumeInput>,
Expand Down Expand Up @@ -2852,7 +2844,7 @@ function registerCursor(toolName: string, iconContent: string, viewBox: {
}): void;

// @public (undocumented)
function removeAnnotation(element: HTMLDivElement, annotationUID: string): void;
function removeAnnotation(annotationUID: string, element?: HTMLDivElement): void;

// @public (undocumented)
function removeSegmentationRepresentation(toolGroupId: string, segmentationRepresentationUID: string): void;
Expand Down Expand Up @@ -2893,7 +2885,14 @@ type ScalingParameters = {
};

// @public (undocumented)
function scrollThroughStack(viewport: Types_2.IStackViewport | Types_2.IVolumeViewport, targetId: string, deltaFrames: number, invert?: boolean): void;
type ScrollOptions_2 = {
direction: number;
invert: boolean;
volumeId?: string;
};

// @public (undocumented)
function scrollThroughStack(viewport: Types_2.IStackViewport | Types_2.IVolumeViewport, options: ScrollOptions_2): void;

// @public (undocumented)
type Segmentation = {
Expand Down Expand Up @@ -3185,6 +3184,8 @@ export class StackScrollTool extends BaseTool {
// (undocumented)
mouseDragCallback: () => void;
// (undocumented)
previousDirection: number;
// (undocumented)
static toolName: string;
// (undocumented)
touchDragCallback: () => void;
Expand All @@ -3194,8 +3195,7 @@ declare namespace stackScrollTool {
export {
snapFocalPointToSlice,
getSliceRange,
scrollThroughStack,
_default_2 as default
scrollThroughStack
}
}

Expand Down Expand Up @@ -3479,7 +3479,8 @@ declare namespace Types {
ColorLUT,
LabelmapTypes,
SVGCursorDescriptor,
SVGPoint_2 as SVGPoint
SVGPoint_2 as SVGPoint,
ScrollOptions_2 as ScrollOptions
}
}
export { Types }
Expand Down
28 changes: 16 additions & 12 deletions packages/core/src/RenderingEngine/RenderingEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ class RenderingEngine implements IRenderingEngine {
* This is left as an app level concern as one might want to debounce the changes, or the like.
*
* @param immediate - Whether all of the viewports should be rendered immediately.
* @param resetPanZoomForViewPlane - Whether each viewport gets centered (reset pan) and
* @param resetPan - Whether the viewport should reset its pan.
* @param resetZoom - Whether the viewport should reset its zoom.
* its zoom gets reset upon resize.
*/
public resize(immediate = true, resetPanZoomForViewPlane = true): void {
public resize(immediate = true, resetPan = true, resetZoom = true): void {
this._throwIfDestroyed();

// 1. Get the viewports' canvases
const viewports = this._getViewportsAsArray();

Expand All @@ -320,14 +320,16 @@ class RenderingEngine implements IRenderingEngine {

this._resizeVTKViewports(
vtkDrivenViewports,
immediate,
resetPanZoomForViewPlane
resetPan,
resetZoom,
immediate
);

this._resizeUsingCustomResizeHandler(
customRenderingViewports,
immediate,
resetPanZoomForViewPlane
resetPan,
sedghi marked this conversation as resolved.
Show resolved Hide resolved
resetZoom,
immediate
);
}

Expand Down Expand Up @@ -526,8 +528,9 @@ class RenderingEngine implements IRenderingEngine {

private _resizeUsingCustomResizeHandler(
customRenderingViewports: StackViewport[],
immediate = true,
resetPanZoomForViewPlane = true
resetPan = true,
resetZoom = true,
immediate = true
) {
// 1. If viewport has a custom resize method, call it here.
customRenderingViewports.forEach((vp) => {
Expand All @@ -536,7 +539,7 @@ class RenderingEngine implements IRenderingEngine {

// 3. Reset viewport cameras
customRenderingViewports.forEach((vp) => {
vp.resetCamera(resetPanZoomForViewPlane);
vp.resetCamera(resetPan, resetZoom);
});

// 2. If render is immediate: Render all
Expand All @@ -547,7 +550,8 @@ class RenderingEngine implements IRenderingEngine {

private _resizeVTKViewports(
vtkDrivenViewports: Array<IStackViewport | IVolumeViewport>,
resetPanZoomForViewPlane = true,
resetPan = true,
resetZoom = true,
immediate = true
) {
const canvasesDrivenByVtkJs = vtkDrivenViewports.map((vp) => vp.canvas);
Expand All @@ -567,7 +571,7 @@ class RenderingEngine implements IRenderingEngine {

// 3. Reset viewport cameras
vtkDrivenViewports.forEach((vp: IStackViewport | IVolumeViewport) => {
vp.resetCamera(resetPanZoomForViewPlane);
vp.resetCamera(resetPan, resetZoom);
});

// 4. If render is immediate: Render all
Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/RenderingEngine/StackViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,30 +1602,28 @@ class StackViewport extends Viewport implements IStackViewport {
/**
* Centers Pan and resets the zoom for stack viewport.
*/
public resetCamera(resetPanZoomForViewPlane = true): boolean {
public resetCamera(resetPan = true, resetZoom = true): boolean {
if (this.useCPURendering) {
this.resetCameraCPU(resetPanZoomForViewPlane);
this.resetCameraCPU(resetPan, resetZoom);
} else {
this.resetCameraGPU();
this.resetCameraGPU(resetPan, resetZoom);
}

return true;
}

private resetCameraCPU(resetPanZoomForViewPlane: boolean) {
private resetCameraCPU(resetPan, resetZoom) {
const { image } = this._cpuFallbackEnabledElement;

if (!image) {
return;
}

resetCamera(this._cpuFallbackEnabledElement, resetPanZoomForViewPlane);
resetCamera(this._cpuFallbackEnabledElement, resetPan, resetZoom);
}

private resetCameraGPU() {
// Todo: this doesn't work for resetPanZoomForViewPlane = true
const resetPanZoomForViewPlane = false;
super.resetCamera(resetPanZoomForViewPlane);
private resetCameraGPU(resetPan, resetZoom) {
super.resetCamera(resetPan, resetZoom);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/RenderingEngine/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,14 @@ class Viewport implements IViewport {

/**
* Resets the camera based on the rendering volume(s) bounds. If
* resetPanZoomForViewPlane is false (default behavior), it places
* the focal point at the center of the volume (or slice); otherwise,
* only the camera zoom and camera Pan is reset for the current view.
* @param resetPanZoomForViewPlane - if true, it renders the center of the volume instead
* resetPan and resetZoom are true it places the focal point at the center of
* the volume (or slice); otherwise, only the camera zoom and camera Pan or Zoom
* is reset for the current view.
* @param resetPan - If true, the camera focal point is reset to the center of the volume (slice)
* @param resetZoom - If true, the camera zoom is reset to the default zoom
* @returns boolean
*/
protected resetCamera(resetPanZoomForViewPlane = false): boolean {
protected resetCamera(resetPan = true, resetZoom = true): boolean {
const renderer = this.getRenderer();
const previousCamera = _cloneDeep(this.getCamera());

Expand Down Expand Up @@ -580,7 +581,7 @@ class Viewport implements IViewport {

let focalPointToSet = focalPoint;

if (resetPanZoomForViewPlane && imageData) {
if (!resetPan && imageData) {
focalPointToSet = this._getFocalPointForViewPlaneReset(imageData);
}

Expand All @@ -597,7 +598,9 @@ class Viewport implements IViewport {

renderer.resetCameraClippingRange(bounds);

activeCamera.setParallelScale(parallelScale);
if (resetZoom) {
activeCamera.setParallelScale(parallelScale);
}

// update reasonable world to physical values
activeCamera.setPhysicalScale(radius);
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/RenderingEngine/VolumeViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,9 @@ class VolumeViewport extends Viewport implements IVolumeViewport {

/**
* Reset the camera for the volume viewport
* @param resetPanZoomForViewPlane - only reset Pan and Zoom, if true,
* it renders the center of the volume instead
* viewport to the middle of the volume
*/
public resetCamera(resetPanZoomForViewPlane = false): boolean {
return super.resetCamera(resetPanZoomForViewPlane);
public resetCamera(resetPan = true, resetZoom = true): boolean {
return super.resetCamera(resetPan, resetZoom);
}

public getFrameOfReferenceUID = (): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import { CPUFallbackEnabledElement } from '../../../../types';
*/
export default function (
enabledElement: CPUFallbackEnabledElement,
resetPanZoomForViewPlane: boolean
resetPan = true,
resetZoom = true
): void {
const { canvas, image, viewport } = enabledElement;
const scale = getImageFitScale(canvas, image, 0).scaleFactor;

viewport.vflip = false;
viewport.hflip = false;

if (resetPanZoomForViewPlane) {
if (resetPan) {
viewport.translation.x = 0;
viewport.translation.y = 0;
}

if (resetZoom) {
viewport.displayedArea.tlhc.x = 1;
viewport.displayedArea.tlhc.y = 1;
viewport.displayedArea.brhc.x = image.columns;
Expand Down
Loading