Skip to content

Commit

Permalink
Merge pull request Kitware#2003 from yarous224/fix_mvp_coordinate
Browse files Browse the repository at this point in the history
fix(Coordinate): Incorrect coordinate calculation for multiple view p…
  • Loading branch information
finetjul authored Jul 20, 2021
2 parents 3b816b9 + 6483b77 commit 4e02527
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 6 deletions.
28 changes: 24 additions & 4 deletions Sources/Rendering/Core/Coordinate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ function vtkCoordinate(publicAPI, model) {
val = renderer.viewToProjection(val[0], val[1], val[2], aspect);

val = renderer.projectionToNormalizedViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(
val[0],
val[1],
val[2],
renderer
);
val = view.viewportToNormalizedDisplay(
val[0],
val[1],
Expand All @@ -441,7 +446,12 @@ function vtkCoordinate(publicAPI, model) {
case Coordinate.VIEW: {
val = renderer.viewToProjection(val[0], val[1], val[2], aspect);
val = renderer.projectionToNormalizedViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(
val[0],
val[1],
val[2],
renderer
);
val = view.viewportToNormalizedDisplay(
val[0],
val[1],
Expand All @@ -453,7 +463,12 @@ function vtkCoordinate(publicAPI, model) {
}
case Coordinate.PROJECTION: {
val = renderer.projectionToNormalizedViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(
val[0],
val[1],
val[2],
renderer
);
val = view.viewportToNormalizedDisplay(
val[0],
val[1],
Expand All @@ -464,7 +479,12 @@ function vtkCoordinate(publicAPI, model) {
break;
}
case Coordinate.NORMALIZED_VIEWPORT: {
val = view.normalizedViewportToViewport(val[0], val[1], val[2]);
val = view.normalizedViewportToViewport(
val[0],
val[1],
val[2],
renderer
);

if (model.referenceCoordinate) {
const refValue = model.referenceCoordinate.getComputedDoubleViewportValue(
Expand Down
163 changes: 163 additions & 0 deletions Sources/Rendering/Core/Coordinate/test/testCoordinate.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,169 @@ test('Test vtkCoordinate publicAPI', (t) => {
viewPort = [50.0, 50.0];
testGetters(coord, renderer, testVal, world, display, localDisplay, viewPort);

// --------------------- Multi Renderers
// In my previous test, some view ports were normal and some not. so create a 2x2 grid view to test.
const renderers = new Array(4);
for (let i = 0; i < renderers.length; i++) {
const ren = gc.registerResource(vtkRenderer.newInstance());
/* index order
0 1
2 3
*/
const x = (i % 2) * 0.5;
const y = 1 - Math.floor(i / 2) * 0.5;
ren.setViewport(x, y - 0.5, x + 0.5, y);
ren.setActiveCamera(vtkCamera.newInstance());
renderWindow.addRenderer(ren);
renderers[i] = ren;
}

coord.setCoordinateSystemToWorld();
testVal = [0.0, 0.0, 0.0];
world = [0.0, 0.0, 0.0];
let displays = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
let localDisplays = [
[25.0, 24.0],
[75.0, 24.0],
[25.0, 74.0],
[75.0, 74.0],
];
viewPort = [25.0, 25.0];

for (let i = 0; i < renderers.length; i++) {
testGetters(
coord,
renderers[i],
testVal,
world,
displays[i],
localDisplays[i],
viewPort
);
}

coord.setCoordinateSystemToDisplay();
testVal = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
world = [0.0, 0.0, 0.99];
displays = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
localDisplays = [
[25.0, 24.0],
[75.0, 24.0],
[25.0, 74.0],
[75.0, 74.0],
];
viewPort = [25.0, 25.0];
for (let i = 0; i < renderers.length; i++) {
testGetters(
coord,
renderers[i],
testVal[i],
world,
displays[i],
localDisplays[i],
viewPort
);
}

coord.setCoordinateSystemToViewport();
testVal = [25.0, 25.0, 0.0];
world = [0.0, 0.0, 0.99];
displays = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
localDisplays = [
[25.0, 24.0],
[75.0, 24.0],
[25.0, 74.0],
[75.0, 74.0],
];
viewPort = [25.0, 25.0];
for (let i = 0; i < renderers.length; i++) {
testGetters(
coord,
renderers[i],
testVal,
world,
displays[i],
localDisplays[i],
viewPort
);
}

coord.setCoordinateSystemToNormalizedViewport();
testVal = [0.5, 0.5, 0.0];
world = [0.0, 0.0, 0.99];
displays = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
localDisplays = [
[25.0, 24.0],
[75.0, 24.0],
[25.0, 74.0],
[75.0, 74.0],
];
viewPort = [25.0, 25.0];
for (let i = 0; i < renderers.length; i++) {
testGetters(
coord,
renderers[i],
testVal,
world,
displays[i],
localDisplays[i],
viewPort
);
}

coord.setCoordinateSystemToView();
testVal = [0.0, 0.0, 0.0];
world = [0.0, 0.0, 1.0];
displays = [
[25.0, 75.0],
[75.0, 75.0],
[25.0, 25.0],
[75.0, 25.0],
];
localDisplays = [
[25.0, 24.0],
[75.0, 24.0],
[25.0, 74.0],
[75.0, 74.0],
];
viewPort = [25.0, 25.0];
for (let i = 0; i < renderers.length; i++) {
testGetters(
coord,
renderers[i],
testVal,
world,
displays[i],
localDisplays[i],
viewPort
);
}

// --------------------- Add a specific renderer
coord.setRenderer(renderer);

Expand Down
4 changes: 2 additions & 2 deletions Sources/Rendering/SceneGraph/RenderWindowViewNode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function vtkRenderWindowViewNode(publicAPI, model) {
return [x, y, z];
};

publicAPI.normalizedViewportToViewport = (x, y, z) => {
const size = publicAPI.getFramebufferSize();
publicAPI.normalizedViewportToViewport = (x, y, z, renderer) => {
const size = publicAPI.getViewportSize(renderer);
return [x * (size[0] - 1.0), y * (size[1] - 1.0), z];
};

Expand Down

0 comments on commit 4e02527

Please sign in to comment.