InteractorObserver

Methods

computeDisplayToWorld

Transform from display to world coordinates.

Argument Type Required Description
renderer vtkRenderer Yes The vtkRenderer instance.
x Number Yes
y Number Yes
z Number Yes

computeWorldToDisplay

Transform from world to display coordinates.

Argument Type Required Description
renderer vtkRenderer Yes The vtkRenderer instance.
x Number Yes
y Number Yes
z Number Yes

getEnabled

Get wether or not this InteractorObserver instance is enabled.

getInteractor

Retrieve the render window interactor instance.

getPriority

Get the priority.

getProcessEvents

Get whether or not the interactor observer instance should process events.

invokeEndInteractionEvent

Invoke an end interaction event.

Argument Type Required Description
args Yes Event payload.

invokeInteractionEvent

Invoke an interaction event.

Argument Type Required Description
args Yes Event payload.

invokeStartInteractionEvent

Invoke a start interaction event.

Argument Type Required Description
args Yes Event payload.

onEndInteractionEvent

Registers a callback to be invoked when an end interaction event occurs.

Argument Type Required Description
cb EventHandler Yes The callback to be called.
priority Number No The callback to be called

onInteractionEvent

Registers a callback to be invoked when an interaction event occurs.

Argument Type Required Description
cb EventHandler Yes The callback to be called.
priority Number No The priority of the event.

onStartInteractionEvent

Registers a callback to be invoked when a start interaction event occurs.

Argument Type Required Description
cb EventHandler Yes The callback to be called.
priority Number No The callback to be called

setEnabled

Enable/disable the interactor observer. Note that if you are enabling the interactor observer, an interactor instance must exists on the model.
Typically you can call setInteractor

Argument Type Required Description
enable Boolean Yes

setInteractor

Set the interactor instance.

Argument Type Required Description
interactor vtkRenderWindowInteractor Yes

setPriority

Set the priority.

Argument Type Required Description
priority Number Yes The priority level.

setProcessEvents

Set whether or not the interactor observer instance should process events.

Argument Type Required Description
processEvents Boolean Yes

Source

index.d.ts
import vtkRenderer from '../Renderer';
import vtkRenderWindowInteractor from '../RenderWindowInteractor';
import { vtkObject, EventHandler, vtkSubscription } from '../../../interfaces';
import { Vector3 } from '../../../types';

export interface vtkInteractorObserver extends vtkObject {
/**
* Invoke an interaction event.
*
* @param args Event payload.
*/
invokeInteractionEvent(...args: unknown[]): void;

/**
* Registers a callback to be invoked when an interaction event occurs.
*
* @param {EventHandler} cb The callback to be called.
* @param {Number} [priority] The priority of the event.
*/
onInteractionEvent(
cb: EventHandler,
priority?: number
): Readonly<vtkSubscription>;

/**
* Invoke a start interaction event.
*
* @param args Event payload.
*/
invokeStartInteractionEvent(...args: unknown[]): void;

/**
* Registers a callback to be invoked when a start interaction event occurs.
*
* @param {EventHandler} cb The callback to be called.
* @param {Number} [priority] The callback to be called
*/
onStartInteractionEvent(
cb: EventHandler,
priority?: number
): Readonly<vtkSubscription>;

/**
* Invoke an end interaction event.
*
* @param args Event payload.
*/
invokeEndInteractionEvent(...args: unknown[]): void;

/**
* Registers a callback to be invoked when an end interaction event occurs.
*
* @param {EventHandler} cb The callback to be called.
* @param {Number?} [priority] The callback to be called
*/
onEndInteractionEvent(
cb: EventHandler,
priority?: number
): Readonly<vtkSubscription>;

/**
* Retrieve the render window interactor instance.
*/
getInteractor(): vtkRenderWindowInteractor;

/**
* Get wether or not this InteractorObserver instance is enabled.
*/
getEnabled(): boolean;

/**
* Enable/disable the interactor observer. Note that if you are enabling the interactor observer, an interactor instance must exists on the model.
* Typically you can call `setInteractor`
*
* @param {Boolean} enable
*/
setEnabled(enable: boolean): void;

/**
* Set the priority.
*
* @param {Number} priority The priority level.
*/
setPriority(priority: number): void;

/**
* Get the priority.
*/
getPriority(): number;

/**
* Set whether or not the interactor observer instance should process events.
*
* @param {Boolean} processEvents
*/
setProcessEvents(processEvents: boolean): boolean;

/**
* Get whether or not the interactor observer instance should process events.
*/
getProcessEvents(): boolean;

/**
* Set the interactor instance.
*
* @param {vtkRenderWindowInteractor} interactor
*/
setInteractor(interactor: vtkRenderWindowInteractor): void;

/**
* Transform from world to display coordinates.
*
* @param {vtkRenderer} renderer The vtkRenderer instance.
* @param {Number} x
* @param {Number} y
* @param {Number} z
*/
computeWorldToDisplay(
renderer: vtkRenderer,
x: number,
y: number,
z: number
): Vector3;

/**
* Transform from display to world coordinates.
*
* @param {vtkRenderer} renderer The vtkRenderer instance.
* @param {Number} x
* @param {Number} y
* @param {Number} z
*/
computeDisplayToWorld(
renderer: vtkRenderer,
x: number,
y: number,
z: number
): Vector3;
}

export default vtkInteractorObserver;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkRenderWindowInteractor from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor';

const { vtkErrorMacro, VOID } = macro;

// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Description:
// Transform from world to display coordinates.
function computeWorldToDisplay(renderer, x, y, z) {
const view = renderer.getRenderWindow().getViews()[0];
return view.worldToDisplay(x, y, z, renderer);
}

//----------------------------------------------------------------------------
// Description:
// Transform from display to world coordinates.
function computeDisplayToWorld(renderer, x, y, z) {
const view = renderer.getRenderWindow().getViews()[0];
return view.displayToWorld(x, y, z, renderer);
}

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
export const STATIC = {
computeWorldToDisplay,
computeDisplayToWorld,
};

// ----------------------------------------------------------------------------
// vtkInteractorObserver methods
// ----------------------------------------------------------------------------

function vtkInteractorObserver(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkInteractorObserver');

const superClass = { ...publicAPI };

//----------------------------------------------------------------------------
function unsubscribeFromEvents() {
while (model.subscribedEvents.length) {
model.subscribedEvents.pop().unsubscribe();
}
}

//----------------------------------------------------------------------------
// Check what events we can handle and register callbacks
function subscribeToEvents() {
vtkRenderWindowInteractor.handledEvents.forEach((eventName) => {
if (publicAPI[`handle${eventName}`]) {
model.subscribedEvents.push(
model._interactor[`on${eventName}`]((callData) => {
if (model.processEvents) {
return publicAPI[`handle${eventName}`](callData);
}
return VOID;
}, model.priority)
);
}
});
}

//----------------------------------------------------------------------------
// Public API methods
//----------------------------------------------------------------------------
publicAPI.setInteractor = (i) => {
if (i === model._interactor) {
return;
}

unsubscribeFromEvents();

model._interactor = i;

if (i && model.enabled) {
subscribeToEvents();
}

publicAPI.modified();
};

//----------------------------------------------------------------------------
publicAPI.setEnabled = (enable) => {
if (enable === model.enabled) {
return;
}

unsubscribeFromEvents();

if (enable) {
if (model._interactor) {
subscribeToEvents();
} else {
vtkErrorMacro(`
The interactor must be set before subscribing to events
`);
}
}

model.enabled = enable;
publicAPI.modified();
};

//----------------------------------------------------------------------------
// Description:
// Transform from display to world coordinates.
publicAPI.computeDisplayToWorld = (renderer, x, y, z) => {
if (!renderer) {
return null;
}

return model._interactor.getView().displayToWorld(x, y, z, renderer);
};

//----------------------------------------------------------------------------
// Description:
// Transform from world to display coordinates.
publicAPI.computeWorldToDisplay = (renderer, x, y, z) => {
if (!renderer) {
return null;
}

return model._interactor.getView().worldToDisplay(x, y, z, renderer);
};

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

publicAPI.setPriority = (priority) => {
const modified = superClass.setPriority(priority);

if (modified && model._interactor) {
unsubscribeFromEvents();
subscribeToEvents();
}
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
enabled: true,
// _interactor: null,
priority: 0.0,
processEvents: true,
subscribedEvents: [],
};

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

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Object methods
macro.obj(publicAPI, model);

macro.event(publicAPI, model, 'InteractionEvent');
macro.event(publicAPI, model, 'StartInteractionEvent');
macro.event(publicAPI, model, 'EndInteractionEvent');

// Create get-only macros
macro.get(publicAPI, model, ['_interactor', 'enabled']);

// Create get-set macros
macro.setGet(publicAPI, model, ['priority', 'processEvents']);

macro.moveToProtected(publicAPI, model, ['interactor']);

// For more macro methods, see "Sources/macros.js"

// Object specific methods
vtkInteractorObserver(publicAPI, model);
}

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

export const newInstance = macro.newInstance(extend, 'vtkInteractorObserver');

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

export default { newInstance, extend, ...STATIC };