Skip to content

Commit

Permalink
Merge pull request #535 from Kitware/cleanup-view-proxy
Browse files Browse the repository at this point in the history
fix(ViewProxy): Refactor to enable more flexibility
  • Loading branch information
jourdain authored Jan 25, 2018
2 parents 810e921 + d47776e commit 3ba579e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 39 deletions.
19 changes: 19 additions & 0 deletions Sources/Proxy/Core/ProxyManager/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ export default function addRegistrationAPI(publicAPI, model) {
proxyManager: publicAPI,
})
);

// Handle property setting
if (definition.props) {
proxy.set(definition.props);
}

// Handle proxy property settings
if (definition.proxyProps) {
const proxyMap = {};
Object.keys(definition.proxyProps).forEach((key) => {
const newProxyDef = definition.proxyProps[key];
proxyMap[key] = publicAPI.createProxy(
newProxyDef.group,
newProxyDef.name,
newProxyDef.options
);
});
proxy.set(proxyMap);
}
registerProxy(proxy);

// Automatically make it active if possible
Expand Down
35 changes: 2 additions & 33 deletions Sources/Proxy/Core/View2DProxy/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import macro from 'vtk.js/Sources/macro';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';

import vtkViewProxy from 'vtk.js/Sources/Proxy/Core/ViewProxy';

Expand All @@ -11,14 +10,9 @@ function vtkView2DProxy(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkView2DProxy');

const superUpdateOrientation = publicAPI.updateOrientation;
publicAPI.updateOrientation = (axisIndex, orientation, viewUp) => {
model.axis = axisIndex;
model.orientation = orientation;
model.viewUp = viewUp;
const position = model.camera.getFocalPoint();
position[model.axis] += model.orientation;
model.camera.setPosition(...position);
model.camera.setViewUp(...viewUp);
superUpdateOrientation(axisIndex, orientation, viewUp);

let count = model.representations.length;
while (count--) {
Expand All @@ -32,30 +26,6 @@ function vtkView2DProxy(publicAPI, model) {
publicAPI.updateCornerAnnotation({ axis: 'XYZ'[axisIndex] });
};

publicAPI.rotate = (angle) => {
const { viewUp, focalPoint, position } = model.camera.get(
'viewUp',
'focalPoint',
'position'
);
const axis = [
focalPoint[0] - position[0],
focalPoint[1] - position[1],
focalPoint[2] - position[2],
];

vtkMatrixBuilder
.buildFromDegree()
.rotate(Number.isNaN(angle) ? 90 : angle, axis)
.apply(viewUp);

model.camera.setViewUp(...viewUp);
model.camera.modified();
model.renderWindow.render();
};

publicAPI.updateOrientation(model.axis, model.orientation, model.viewUp);

// Setup default corner annotation
/* eslint-disable no-template-curly-in-string */
publicAPI.setCornerAnnotation(
Expand Down Expand Up @@ -91,7 +61,6 @@ export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

vtkViewProxy.extend(publicAPI, model, initialValues);
macro.set(publicAPI, model, ['orientation']);
macro.get(publicAPI, model, ['axis']);

// Object specific methods
Expand Down
82 changes: 76 additions & 6 deletions Sources/Proxy/Core/ViewProxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import macro from 'vtk.js/Sources/macro';
import vtkAnnotatedCubeActor from 'vtk.js/Sources/Rendering/Core/AnnotatedCubeActor';
import vtkCornerAnnotation from 'vtk.js/Sources/Interaction/UI/CornerAnnotation';
import vtkInteractorStyleManipulator from 'vtk.js/Sources/Interaction/Style/InteractorStyleManipulator';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import vtkOpenGLRenderWindow from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow';
import vtkOrientationMarkerWidget from 'vtk.js/Sources/Interaction/Widgets/OrientationMarkerWidget';
import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer';
Expand All @@ -21,6 +22,14 @@ function vtkViewProxy(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkViewProxy');

// Private --------------------------------------------------------------------

function updateAnnotationColor() {
const [r, g, b] = model.renderer.getBackground();
model.cornerAnnotation.getAnnotationContainer().style.color =
r + g + b > 1.5 ? 'black' : 'white';
}

// Setup --------------------------------------------------------------------
model.renderWindow = vtkRenderWindow.newInstance();
model.renderer = vtkRenderer.newInstance({ background: [0, 0, 0] });
Expand Down Expand Up @@ -206,6 +215,8 @@ function vtkViewProxy(publicAPI, model) {

publicAPI.captureImage = () => model.renderWindow.captureImages()[0];

// --------------------------------------------------------------------------

publicAPI.openCaptureImage = (target = '_blank') => {
const image = new Image();
image.src = publicAPI.captureImage();
Expand All @@ -224,9 +235,13 @@ function vtkViewProxy(publicAPI, model) {
});
};

// --------------------------------------------------------------------------

publicAPI.updateCornerAnnotation = (meta) =>
model.cornerAnnotation.updateMetadata(meta);

// --------------------------------------------------------------------------

publicAPI.setAnnotationOpacity = (opacity) => {
if (model.annotationOpacity !== Number(opacity)) {
model.annotationOpacity = Number(opacity);
Expand All @@ -235,26 +250,78 @@ function vtkViewProxy(publicAPI, model) {
}
};

function updateAnnotationColor() {
const [r, g, b] = model.renderer.getBackground();
model.cornerAnnotation.getAnnotationContainer().style.color =
r + g + b > 1.5 ? 'black' : 'white';
}
updateAnnotationColor();
// --------------------------------------------------------------------------

publicAPI.setBackground = macro.chain(
model.renderer.setBackground,
updateAnnotationColor
);

// --------------------------------------------------------------------------

publicAPI.getBackground = model.renderer.getBackground;

// --------------------------------------------------------------------------

publicAPI.setAnimation = (enable) => {
if (enable) {
model.renderWindow.getInteractor().requestAnimation('proxy');
} else {
model.renderWindow.getInteractor().cancelAnimation('proxy');
}
};

// --------------------------------------------------------------------------

publicAPI.updateOrientation = (axisIndex, orientation, viewUp) => {
if (axisIndex === undefined) {
return;
}
model.axis = axisIndex;
model.orientation = orientation;
model.viewUp = viewUp;
const position = model.camera.getFocalPoint();
position[model.axis] += model.orientation;
model.camera.setPosition(...position);
model.camera.setViewUp(...viewUp);
};

// --------------------------------------------------------------------------

publicAPI.resetOrientation = () => {
publicAPI.updateOrientation(model.axis, model.orientation, model.viewUp);
};

// --------------------------------------------------------------------------

publicAPI.rotate = (angle) => {
const { viewUp, focalPoint, position } = model.camera.get(
'viewUp',
'focalPoint',
'position'
);
const axis = [
focalPoint[0] - position[0],
focalPoint[1] - position[1],
focalPoint[2] - position[2],
];

vtkMatrixBuilder
.buildFromDegree()
.rotate(Number.isNaN(angle) ? 90 : angle, axis)
.apply(viewUp);

model.camera.setViewUp(...viewUp);
model.camera.modified();
model.renderWindow.render();
};

// --------------------------------------------------------------------------
// Initialization from state or input
// --------------------------------------------------------------------------

publicAPI.updateOrientation(model.axis, model.orientation, model.viewUp);
updateAnnotationColor();
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -296,6 +363,9 @@ function extend(publicAPI, model, initialValues = {}) {
macro.proxy(publicAPI, model);
macro.proxyPropertyMapping(publicAPI, model, {
orientationAxes: { modelKey: 'orientationWidget', property: 'enabled' },
cameraViewUp: { modelKey: 'camera', property: 'viewUp' },
cameraPosition: { modelKey: 'camera', property: 'position' },
cameraFocalPoint: { modelKey: 'camera', property: 'focalPoint' },
});
}

Expand Down

0 comments on commit 3ba579e

Please sign in to comment.