Skip to content

Commit

Permalink
docs(vtkinteractiveorientationwidget): use new helper functions in th…
Browse files Browse the repository at this point in the history
…e example
  • Loading branch information
bourdaisj committed Jun 18, 2024
1 parent ba37932 commit 30b0526
Showing 1 changed file with 22 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,16 @@ import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import '@kitware/vtk.js/Rendering/Profiles/Glyph';

import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkOrientationMarkerWidget from '@kitware/vtk.js/Interaction/Widgets/OrientationMarkerWidget';
import vtkAxesActor from '@kitware/vtk.js/Rendering/Core/AxesActor';

import vtkInteractiveOrientationWidget from '@kitware/vtk.js/Widgets/Widgets3D/InteractiveOrientationWidget';
import vtkWidgetManager from '@kitware/vtk.js/Widgets/Core/WidgetManager';

import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkWidgetManager from '@kitware/vtk.js/Widgets/Core/WidgetManager';

import * as vtkMath from '@kitware/vtk.js/Common/Core/Math';

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

function majorAxis(vec3, idxA, idxB) {
const axis = [0, 0, 0];
const idx = Math.abs(vec3[idxA]) > Math.abs(vec3[idxB]) ? idxA : idxB;
const value = vec3[idx] > 0 ? 1 : -1;
axis[idx] = value;
return axis;
}
import {
createInteractiveOrientationMarkerWidget,
alignCameraOnViewWidgetOrientationChange,
} from '@kitware/vtk.js/Widgets/Widgets3D/InteractiveOrientationWidget/helpers';

// ----------------------------------------------------------------------------
// Standard rendering code setup
Expand All @@ -37,19 +25,7 @@ const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
const render = renderWindow.render;

const axes = vtkAxesActor.newInstance();
const orientationWidget = vtkOrientationMarkerWidget.newInstance({
actor: axes,
interactor: renderWindow.getInteractor(),
});
orientationWidget.setEnabled(true);
orientationWidget.setViewportCorner(
vtkOrientationMarkerWidget.Corners.BOTTOM_LEFT
);
orientationWidget.setViewportSize(0.1);
orientationWidget.setMinPixelSize(100);
orientationWidget.setMaxPixelSize(300);
const renderWindowInteractor = renderWindow.getInteractor();

// ----------------------------------------------------------------------------
// Add context to 3D scene for orientation
Expand All @@ -68,46 +44,27 @@ const camera = renderer.getActiveCamera();
// ----------------------------------------------------------------------------
// Widget manager
// ----------------------------------------------------------------------------

const widgetManager = vtkWidgetManager.newInstance();
widgetManager.setRenderer(orientationWidget.getRenderer());

const widget = vtkInteractiveOrientationWidget.newInstance();
widget.placeWidget(axes.getBounds());
widget.setBounds(axes.getBounds().map((v) => v * 0.45));

const vw = widgetManager.addWidget(widget);

// Manage user interaction
vw.onOrientationChange(({ up, direction, action, event }) => {
const focalPoint = camera.getFocalPoint();
const position = camera.getPosition();
const viewUp = camera.getViewUp();

const distance = Math.sqrt(
vtkMath.distance2BetweenPoints(position, focalPoint)
);
camera.setPosition(
focalPoint[0] + direction[0] * distance,
focalPoint[1] + direction[1] * distance,
focalPoint[2] + direction[2] * distance
const { interactiveOrientationWidget, orientationMarkerWidget } =
createInteractiveOrientationMarkerWidget(
widgetManager,
renderWindowInteractor,
renderer
);

if (direction[0]) {
camera.setViewUp(majorAxis(viewUp, 1, 2));
}
if (direction[1]) {
camera.setViewUp(majorAxis(viewUp, 0, 2));
}
if (direction[2]) {
camera.setViewUp(majorAxis(viewUp, 0, 1));
}
const vw = widgetManager.addWidget(interactiveOrientationWidget);

orientationWidget.updateMarkerOrientation();
widgetManager.enablePicking();
render();
});
const subscription = alignCameraOnViewWidgetOrientationChange(
vw,
camera,
orientationMarkerWidget,
widgetManager,
render
);

renderer.resetCamera();
widgetManager.enablePicking();
render();

global.subscription = subscription;

0 comments on commit 30b0526

Please sign in to comment.