AxesActor

Introduction

vtkAxesActor is a hybrid 2D/3D actor used to represent 3D axes in a scene.
The user can define the geometry to use for the shaft or the tip,
and the user can set the text for the three axes. The text will appear
to follow the camera since it is implemented by means of vtkCaptionActor2D.
All of the functionality of the underlying vtkCaptionActor2D objects are accessible so that,
for instance, the font attributes of the axes text can be manipulated through vtkTextProperty.
Since this class inherits from vtkProp3D, one can apply a user transform to the underlying
geometry and the positioning of the labels. For example, a rotation transform could be used to
generate a left-handed axes representation.

See Also

vtkAnnotatedCubeActor

vtkOrientationMarkerWidget

Methods

extend

Method use to decorate a given object (publicAPI+model) with vtkAxesActor 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 IAxesActorInitialValues No (default: {})

getConfig

Get config object of the actor.

getXAxisColor

Retrieves the color of the X-axis.

getXConfig

Get config object of the X axis.

getYAxisColor

Retrieves the color of the Y-axis.

getYConfig

Get config object of the Y axis.

getZAxisColor

Retrieves the color of the Z-axis.

getZConfig

Get config object of the Z axis.

newInstance

Method use to create a new instance of vtkAxesActor.

Argument Type Required Description
initialValues IAxesActorInitialValues No for pre-setting some of its content

setConfig

Set config object of the actor.

Argument Type Required Description
config Yes

setXAxisColor

Set X axis color.

Argument Type Required Description
rgb RGBColor Yes An Array of the RGB color.

setXConfig

Set config object of the X axis.

Argument Type Required Description
config Yes

setYAxisColor

Set Y axis color.

Argument Type Required Description
rgb RGBColor Yes An Array of the RGB color.

setYConfig

Set config object of the Y axis.

Argument Type Required Description
config Yes

setZAxisColor

Set Z axis color.

Argument Type Required Description
rgb RGBColor Yes An Array of the RGB color.

setZConfig

Set config object of the Z axis.

Argument Type Required Description
config Yes

update

Update the actor.

Source

index.d.ts
import vtkActor, { IActorInitialValues } from '../Actor';
import { RGBColor } from '../../../types';

/**
* Represents the initial values for the AxesActor.
*/
export interface IAxesActorInitialValues extends IActorInitialValues {}

/**
* Represents an actor that displays axes in a 3D scene.
*/
export interface vtkAxesActor extends vtkActor {
/**
* Get config object of the actor.
*/
getConfig(): object;

/**
* Get config object of the X axis.
*/
getXConfig(): object;

/**
* Get config object of the Y axis.
*/
getYConfig(): object;

/**
* Get config object of the Z axis.
*/
getZConfig(): object;

/**
* Retrieves the color of the X-axis.
*
* @return {RGBColor} The color of the X-axis.
*/
getXAxisColor(): RGBColor;

/**
* Retrieves the color of the Y-axis.
*
* @return {RGBColor} The color of the Y-axis.
*/
getYAxisColor(): RGBColor;

/**
* Retrieves the color of the Z-axis.
*
* @return {RGBColor} The color of the Z-axis.
*/
getZAxisColor(): RGBColor;

/**
* Set config object of the actor.
* @param config
*/
setConfig(config: object): boolean;

/**
* Set config object of the X axis.
* @param config
*/
setXConfig(config: object): boolean;

/**
* Set config object of the Y axis.
* @param config
*/
setYConfig(config: object): boolean;

/**
* Set config object of the Z axis.
* @param config
*/
setZConfig(config: object): boolean;

/**
* Set X axis color.
* @param {RGBColor} rgb An Array of the RGB color.
*/
setXAxisColor(rgb: RGBColor): boolean;

/**
* Set Y axis color.
* @param {RGBColor} rgb An Array of the RGB color.
*/
setYAxisColor(rgb: RGBColor): boolean;

/**
* Set Z axis color.
* @param {RGBColor} rgb An Array of the RGB color.
*/
setZAxisColor(rgb: RGBColor): boolean;

/**
* Update the actor.
*/
update(): void;
}

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

/**
* Method use to create a new instance of vtkAxesActor.
* @param {IAxesActorInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: IAxesActorInitialValues
): vtkAxesActor;

/**
* vtkAxesActor is a hybrid 2D/3D actor used to represent 3D axes in a scene.
* The user can define the geometry to use for the shaft or the tip,
* and the user can set the text for the three axes. The text will appear
* to follow the camera since it is implemented by means of vtkCaptionActor2D.
* All of the functionality of the underlying vtkCaptionActor2D objects are accessible so that,
* for instance, the font attributes of the axes text can be manipulated through vtkTextProperty.
* Since this class inherits from vtkProp3D, one can apply a user transform to the underlying
* geometry and the positioning of the labels. For example, a rotation transform could be used to
* generate a left-handed axes representation.
* @see [vtkAnnotatedCubeActor](./Rendering_Core_AnnotatedCubeActor.html)
* @see [vtkOrientationMarkerWidget](./Interaction_Widgets_OrientationMarkerWidget.html)
*/
export declare const vtkAxesActor: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkAxesActor;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkArrowSource from 'vtk.js/Sources/Filters/Sources/ArrowSource';
import vtkAppendPolyData from 'vtk.js/Sources/Filters/General/AppendPolyData';

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

function centerDataSet(ds) {
const bounds = ds.getPoints().getBounds();
const center = [
-(bounds[0] + bounds[1]) * 0.5,
-(bounds[2] + bounds[3]) * 0.5,
-(bounds[4] + bounds[5]) * 0.5,
];
vtkMatrixBuilder
.buildFromDegree()
.translate(...center)
.apply(ds.getPoints().getData());
}

function shiftDataset(ds, axis, invert = false) {
const bounds = ds.getPoints().getBounds();
const center = [0, 0, 0];
if (invert) {
center[axis] = -bounds[axis * 2 + 1];
} else {
center[axis] = -bounds[axis * 2];
}
vtkMatrixBuilder
.buildFromDegree()
.translate(...center)
.apply(ds.getPoints().getData());
}

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

function addColor(ds, r, g, b) {
const size = ds.getPoints().getData().length;
const rgbArray = new Uint8ClampedArray(size);
let offset = 0;

while (offset < size) {
rgbArray[offset++] = r;
rgbArray[offset++] = g;
rgbArray[offset++] = b;
}

ds.getPointData().setScalars(
vtkDataArray.newInstance({
name: 'color',
numberOfComponents: 3,
values: rgbArray,
})
);
}

// ----------------------------------------------------------------------------
// vtkAxesActor
// ----------------------------------------------------------------------------

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

const _mapper = vtkMapper.newInstance();
publicAPI.setMapper(_mapper);

publicAPI.update = () => {
let currentConfig = {
...model.config,
...model.xConfig,
};

const xAxis = vtkArrowSource
.newInstance({ direction: [1, 0, 0], ...currentConfig })
.getOutputData();
if (model.config.recenter) {
centerDataSet(xAxis);
} else {
shiftDataset(xAxis, 0, currentConfig.invert);
}
addColor(xAxis, ...currentConfig.color);

currentConfig = {
...model.config,
...model.yConfig,
};
const yAxis = vtkArrowSource
.newInstance({ direction: [0, 1, 0], ...currentConfig })
.getOutputData();
if (model.config.recenter) {
centerDataSet(yAxis);
} else {
shiftDataset(yAxis, 1, currentConfig.invert);
}
addColor(yAxis, ...currentConfig.color);

currentConfig = {
...model.config,
...model.zConfig,
};
const zAxis = vtkArrowSource
.newInstance({ direction: [0, 0, 1], ...currentConfig })
.getOutputData();
if (model.config.recenter) {
centerDataSet(zAxis);
} else {
shiftDataset(zAxis, 2, currentConfig.invert);
}
addColor(zAxis, ...currentConfig.color);

const source = vtkAppendPolyData.newInstance();
source.setInputData(xAxis);
source.addInputData(yAxis);
source.addInputData(zAxis);

_mapper.setInputConnection(source.getOutputPort());
};

publicAPI.update();
const _debouncedUpdate = macro.debounce(publicAPI.update, 0);

publicAPI.setXAxisColor = (color) =>
publicAPI.setXConfig({ ...publicAPI.getXConfig(), color });

publicAPI.setYAxisColor = (color) =>
publicAPI.setYConfig({ ...publicAPI.getYConfig(), color });

publicAPI.setZAxisColor = (color) =>
publicAPI.setZConfig({ ...publicAPI.getZConfig(), color });

publicAPI.getXAxisColor = () => model.getXConfig().color;

publicAPI.getYAxisColor = () => model.getYConfig().color;

publicAPI.getZAxisColor = () => model.getZConfig().color;

model._onConfigChanged = _debouncedUpdate;
model._onXConfigChanged = _debouncedUpdate;
model._onYConfigChanged = _debouncedUpdate;
model._onZConfigChanged = _debouncedUpdate;
}

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

function defaultValues(initialValues) {
return {
config: {
recenter: true,
tipResolution: 60,
tipRadius: 0.1,
tipLength: 0.2,
shaftResolution: 60,
shaftRadius: 0.03,
invert: false,
...initialValues?.config,
},
xConfig: {
color: [255, 0, 0],
invert: false,
...initialValues?.xConfig,
},
yConfig: {
color: [255, 255, 0],
invert: false,
...initialValues?.yConfig,
},
zConfig: {
color: [0, 128, 0],
invert: false,
...initialValues?.zConfig,
},
};
}

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

export function extend(publicAPI, model, initialValues = {}) {
// Inheritance
vtkActor.extend(publicAPI, model, defaultValues(initialValues));

macro.setGet(publicAPI, model, ['config', 'xConfig', 'yConfig', 'zConfig']);

// Object methods
vtkAxesActor(publicAPI, model);
}

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

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

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

export default { newInstance, extend };