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 1 commit
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
32 changes: 25 additions & 7 deletions packages/core/src/RenderingEngine/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,38 @@ class Viewport implements IViewport {
vec3.scaleAndAdd(center, center, jVector, (size[1] / 2.0) * spacing[1]);
vec3.scaleAndAdd(center, center, kVector, (size[2] / 2.0) * spacing[2]);

const imageToWorld: mat4 = [
Copy link
Member

Choose a reason for hiding this comment

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

This is not imageToWorld (since it does not have Origin in the 4th column). Maybe call it directionMatrix?

direction[0],
direction[1],
direction[2],
0, //
direction[3],
direction[4],
direction[5],
0, //
direction[6],
direction[7],
direction[8],
0, //
0,
0,
0,
1, //
];
const worldToImage: mat4 = mat4.transpose(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6],
imageToWorld
);

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(imageToWorld);
const transformBackFromOriginTx = vtkMatrixBuilder
.buildFromRadian()
.identity()
.rotateFromDirections([1, 0, 0], iVector)
.rotateFromDirections([0, 1, 0], jVector)
.multiply(worldToImage)
.translate(-center[0], -center[1], -center[2]);

if (flipH) {
Expand Down