LineManipulator

Introduction

vtkLineManipulator.

Methods

extend

Method use to decorate a given object (publicAPI+model) with vtkLineManipulator characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues ILineManipulatorInitialValues No (default: {})

newInstance

Method use to create a new instance of vtkLineManipulator

projectDisplayToLine

Argument Type Required Description
x Number Yes
y Number Yes
lineOrigin Vector3 Yes
lineDirection Vector3 Yes
renderer Yes
glRenderWindow Yes

Source

index.d.ts
import {
IAbstractManipulatorInitialValues,
vtkAbstractManipulator,
} from '../AbstractManipulator';
import { Vector3 } from '../../../types';

/**
*
*/
export interface ILineManipulatorInitialValues
extends IAbstractManipulatorInitialValues {}

export interface vtkLineManipulator extends vtkAbstractManipulator {}

/**
* Method use to decorate a given object (publicAPI+model) with vtkLineManipulator characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {ILineManipulatorInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: ILineManipulatorInitialValues
): void;

/**
* Method use to create a new instance of vtkLineManipulator
*/
export function newInstance(
initialValues?: ILineManipulatorInitialValues
): vtkLineManipulator;

/**
*
* @param {Number} x
* @param {Number} y
* @param {Vector3} lineOrigin
* @param {Vector3} lineDirection
* @param renderer
* @param glRenderWindow
*/
export function projectDisplayToLine(
x: number,
y: number,
lineOrigin: Vector3,
lineDirection: Vector3,
renderer: any,
glRenderWindow: any
): Vector3;

/**
* vtkLineManipulator.
*/
export declare const vtkLineManipulator: {
newInstance: typeof newInstance;
extend: typeof extend;
projectDisplayToLine: typeof projectDisplayToLine;
};
export default vtkLineManipulator;
index.js
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';

import vtkAbstractManipulator from 'vtk.js/Sources/Widgets/Manipulators/AbstractManipulator';
import { EPSILON } from 'vtk.js/Sources/Common/Core/Math/Constants';

export function projectDisplayToLine(
x,
y,
lineOrigin,
lineDirection,
renderer,
glRenderWindow
) {
// if the active camera viewPlaneNormal and line direction are parallel, no change is allowed
const dotProduct = Math.abs(
vtkMath.dot(renderer.getActiveCamera().getViewPlaneNormal(), lineDirection)
);

if (1 - dotProduct < EPSILON) {
return [];
}
const near = glRenderWindow.displayToWorld(x, y, 0, renderer);
const far = glRenderWindow.displayToWorld(x, y, 1, renderer);
const viewDir = [0, 0, 0];
vtkMath.subtract(far, near, viewDir);

const normal = [0, 0, 0];
vtkMath.cross(lineDirection, viewDir, normal);
vtkMath.cross(normal, viewDir, normal);

const numerator = vtkMath.dot(
[near[0] - lineOrigin[0], near[1] - lineOrigin[1], near[2] - lineOrigin[2]],
normal
);
const denominator = vtkMath.dot(normal, lineDirection);

const result = lineDirection.slice();
if (denominator === 0) {
// no change is allowed
vtkMath.multiplyScalar(result, 0);
} else {
vtkMath.multiplyScalar(result, numerator / denominator);
}
vtkMath.add(lineOrigin, result, result);

return result;
}

// ----------------------------------------------------------------------------
// vtkLineManipulator methods
// ----------------------------------------------------------------------------

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

publicAPI.handleEvent = (callData, glRenderWindow) =>
model._addWorldDeltas({
worldCoords: projectDisplayToLine(
callData.position.x,
callData.position.y,
publicAPI.getOrigin(callData),
publicAPI.getNormal(callData),
callData.pokedRenderer,
glRenderWindow
),
});
}

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

function defaultValues(initialValues) {
return {
...initialValues,
};
}

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

export function extend(publicAPI, model, initialValues = {}) {
vtkAbstractManipulator.extend(publicAPI, model, defaultValues(initialValues));

vtkLineManipulator(publicAPI, model);
}

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

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

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

export default { projectDisplayToLine, extend, newInstance };