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 logic for flip, remove useless member variable ( panCache ) #258

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 18 additions & 13 deletions packages/core/src/RenderingEngine/StackViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class StackViewport extends Viewport implements IStackViewport {
private _imageData: vtkImageDataType;
private cameraPosOnRender: Point3;
private stackInvalidated = false; // if true -> new actor is forced to be created for the stack
private panCache: Point3;
private voiApplied = false;
private rotationCache = 0;
private _publishCalibratedEvent = false;
Expand Down Expand Up @@ -196,7 +195,6 @@ class StackViewport extends Viewport implements IStackViewport {
this.imageIds = [];
this.currentImageIdIndex = 0;
this.targetImageIdIndex = 0;
this.panCache = [0, 0, 0];
this.cameraPosOnRender = [0, 0, 0];
this.resetCamera();

Expand Down Expand Up @@ -1667,9 +1665,11 @@ class StackViewport extends Viewport implements IStackViewport {
// it in the space 3) restore the pan, zoom props.
const cameraProps = this.getCamera();

this.panCache[0] = this.cameraPosOnRender[0] - cameraProps.position[0];
this.panCache[1] = this.cameraPosOnRender[1] - cameraProps.position[1];
this.panCache[2] = this.cameraPosOnRender[2] - cameraProps.position[2];
const panCache: [number, number, number] = [
this.cameraPosOnRender[0] - cameraProps.position[0],
this.cameraPosOnRender[1] - cameraProps.position[1],
this.cameraPosOnRender[2] - cameraProps.position[2],
];

// store rotation cache since reset camera will reset it
const rotationCache = this.rotationCache;
Expand All @@ -1691,7 +1691,7 @@ class StackViewport extends Viewport implements IStackViewport {

// We shouldn't restore the focalPoint, position and parallelScale after reset
// if it is the first render or we have completely re-created the vtkImageData
this._restoreCameraProps(cameraProps, previousCameraProps);
this._restoreCameraProps(cameraProps, previousCameraProps, panCache);

// Restore rotation for the new slice of the image
this.rotationCache = 0;
Expand Down Expand Up @@ -1976,23 +1976,24 @@ class StackViewport extends Viewport implements IStackViewport {
*/
private _restoreCameraProps(
{ parallelScale: prevScale }: ICamera,
previousCamera: ICamera
previousCamera: ICamera,
panCache: [number, number, number]
): void {
const renderer = this.getRenderer();

// get the focalPoint and position after the reset
const { position, focalPoint } = this.getCamera();

const newPosition = <Point3>[
position[0] - this.panCache[0],
position[1] - this.panCache[1],
position[2] - this.panCache[2],
position[0] - panCache[0],
position[1] - panCache[1],
position[2] - panCache[2],
];

const newFocal = <Point3>[
focalPoint[0] - this.panCache[0],
focalPoint[1] - this.panCache[1],
focalPoint[2] - this.panCache[2],
focalPoint[0] - panCache[0],
focalPoint[1] - panCache[1],
focalPoint[2] - panCache[2],
];

// Restoring previous state x,y and scale, keeping the new z
Expand All @@ -2001,6 +2002,10 @@ class StackViewport extends Viewport implements IStackViewport {
position: newPosition,
focalPoint: newFocal,
});
this.flip({
flipHorizontal: previousCamera.flipHorizontal,
flipVertical: previousCamera.flipVertical,
});

const camera = this.getCamera();

Expand Down
25 changes: 4 additions & 21 deletions packages/core/src/RenderingEngine/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,36 +237,19 @@ class Viewport implements IViewport {
// proper scaling (flipping), they should be transformed to the origin and
// then flipped. The following code does this transformation.

const origin = imageData.getOrigin();
const direction = imageData.getDirection();
const spacing = imageData.getSpacing();
const size = imageData.getDimensions();

const iVector = direction.slice(0, 3);
const jVector = direction.slice(3, 6);
const kVector = direction.slice(6, 9);

// finding the center of the image
const center = vec3.create();
vec3.scaleAndAdd(center, origin, iVector, (size[0] / 2.0) * spacing[0]);
vec3.scaleAndAdd(center, center, jVector, (size[1] / 2.0) * spacing[1]);
vec3.scaleAndAdd(center, center, kVector, (size[2] / 2.0) * spacing[2]);

let flipHTx, flipVTx;

const transformToOriginTx = vtkMatrixBuilder
.buildFromRadian()
.identity()
.translate(center[0], center[1], center[2])
.rotateFromDirections(jVector, [0, 1, 0])
.rotateFromDirections(iVector, [1, 0, 0]);
.multiply(imageData.getIndexToWorld())
.translate(size[0] / 2.0, size[1] / 2.0, 0);

const transformBackFromOriginTx = vtkMatrixBuilder
.buildFromRadian()
.identity()
.rotateFromDirections([1, 0, 0], iVector)
.rotateFromDirections([0, 1, 0], jVector)
.translate(-center[0], -center[1], -center[2]);
.translate(-size[0] / 2.0, -size[1] / 2.0, 0)
.multiply(imageData.getWorldToIndex());

if (flipH) {
this.flipHorizontal = flipHorizontal;
Expand Down