Viewport

Introduction

vtkViewport represents part or all of a RenderWindow. It holds a
collection of props that will be rendered into the area it represents.
This class also contains methods to convert between coordinate systems
commonly used in rendering.

See Also

vtkActor

vtkCoordinate

vtkProp

vtkRender

vtkRenderWindow

vtkVolume

Methods

PickPropFrom

Not Implemented yet

addActor2D

Not Implemented yet

addViewProp

Add a prop to the list of props.

Argument Type Required Description
prop Yes

displayToView

Convert display coordinates to view coordinates.

extend

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

getActors2D

getBackground

getBackground2

getBackground2ByReference

getBackgroundByReference

getSize

getViewProps

getViewPropsWithNestedProps

getViewport

getViewportByReference

hasViewProp

Argument Type Required Description
prop Yes

newInstance

Method use to create a new instance of vtkViewport

normalizedDisplayToNormalizedViewport

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

normalizedDisplayToProjection

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

normalizedViewportToNormalizedDisplay

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

normalizedViewportToProjection

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

projectionToNormalizedDisplay

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

projectionToNormalizedViewport

Argument Type Required Description
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

removeActor2D

Argument Type Required Description
prop Yes

removeAllViewProps

removeViewProp

Argument Type Required Description
prop Yes

setBackground

Set the viewport background.

Argument Type Required Description
background Array. Yes The RGB color array.

setBackground

Set the viewport background.

Argument Type Required Description
r Number Yes Defines the red component (between 0 and 1).
g Number Yes Defines the green component (between 0 and 1).
b Number Yes Defines the blue component (between 0 and 1).

setBackground2

Argument Type Required Description
background Array. Yes

setBackground2

Argument Type Required Description
r Number Yes Defines the red component (between 0 and 1).
g Number Yes Defines the green component (between 0 and 1).
b Number Yes Defines the blue component (between 0 and 1).

setBackground2From

Argument Type Required Description
background Array. Yes

setBackgroundFrom

Argument Type Required Description
background Array. Yes

setViewport

Specify the viewport for the Viewport to draw in the rendering window.
Each coordinate is 0 <= coordinate <= 1.0.

Argument Type Required Description
xmin Number Yes The xmin coordinate.
ymin Number Yes The ymin coordinate.
xmax Number Yes The xmax coordinate.
ymax Number Yes The ymax coordinate.

setViewportFrom

Specify the viewport for the Viewport to draw in the rendering window.
Coordinates are expressed as [xmin, ymin, xmax, ymax], where each coordinate is 0 <= coordinate <= 1.0.

Argument Type Required Description
viewport Array. Yes

viewToDisplay

Source

index.d.ts

import { vtkObject } from "../../../interfaces";
import { RGBColor, Size } from "../../../types";
import vtkActor2D from '../Actor2D';
import vtkProp from '../Prop';

export interface IViewportInitialValues {
background?: RGBColor;
background2?: RGBColor;
gradientBackground?: boolean;
viewport?: number[];
aspect?: number[];
pixelAspect?: number[];
props?: vtkProp[];
actors2D?: vtkActor2D[];
}

export interface vtkViewport extends vtkObject {

/**
* Not Implemented yet
*/
addActor2D(): any;

/**
* Add a prop to the list of props.
* @param prop
*/
addViewProp(prop: vtkProp): void;

/**
* Convert display coordinates to view coordinates.
*/
displayToView(): any;

/**
*
*/
getActors2D(): vtkActor2D[];

/**
*
*/
getBackground2(): number[];

/**
*
*/
getBackground2ByReference(): number[];

/**
*
*/
getBackground(): number[];

/**
*
*/
getBackgroundByReference(): number[];

/**
*
*/
getSize(): Size;

/**
*
*/
getViewport(): vtkViewport;

/**
*
*/
getViewportByReference(): vtkViewport;

/**
*
*/
getViewPropsWithNestedProps(): any;

/**
*
*/
getViewProps(): vtkProp[];

/**
*
* @param prop
*/
hasViewProp(prop: vtkProp): boolean;

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
normalizedDisplayToProjection(x: number, y: number, z: number): number[];

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
normalizedDisplayToNormalizedViewport(x: number, y: number, z: number): any;

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
normalizedViewportToProjection(x: number, y: number, z: any): number[];

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
projectionToNormalizedDisplay(x: number, y: number, z: number): number[];

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
normalizedViewportToNormalizedDisplay(x: number, y: number, z: number): number[];

/**
* Set the viewport background.
* @param {Number} r Defines the red component (between 0 and 1).
* @param {Number} g Defines the green component (between 0 and 1).
* @param {Number} b Defines the blue component (between 0 and 1).
*/
setBackground(r: number, g: number, b: number): boolean;

/**
* Set the viewport background.
* @param {Number[]} background The RGB color array.
*/
setBackground(background: number[]): boolean;

/**
*
* @param {Number} r Defines the red component (between 0 and 1).
* @param {Number} g Defines the green component (between 0 and 1).
* @param {Number} b Defines the blue component (between 0 and 1).
*/
setBackground2(r: number, g: number, b: number): boolean;

/**
*
* @param {Number[]} background
*/
setBackground2(background: number[]): boolean;

/**
*
* @param {Number[]} background
*/
setBackground2From(background: number[]): boolean;

/**
*
* @param {Number[]} background
*/
setBackgroundFrom(background: number[]): boolean;

/**
* Specify the viewport for the Viewport to draw in the rendering window.
* Each coordinate is 0 <= coordinate <= 1.0.
* @param {Number} xmin The xmin coordinate.
* @param {Number} ymin The ymin coordinate.
* @param {Number} xmax The xmax coordinate.
* @param {Number} ymax The ymax coordinate.
*/
setViewport(xmin: number, ymin: number, xmax: number, ymax: number): boolean;

/**
* Specify the viewport for the Viewport to draw in the rendering window.
* Coordinates are expressed as [xmin, ymin, xmax, ymax], where each coordinate is 0 <= coordinate <= 1.0.
* @param {Number[]} viewport
*/
setViewportFrom(viewport: number[]): boolean;

/**
*
* @param prop
*/
removeViewProp(prop: vtkProp): void;

/**
*
*/
removeAllViewProps(): void;

/**
*
* @param prop
*/
removeActor2D(prop: vtkProp): void;

/**
*
*/
viewToDisplay(): any;

/**
*
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
projectionToNormalizedViewport(x: number, y: number, z: number): number[];


/**
* Not Implemented yet
*/
PickPropFrom(): any;
}

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

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

/**
* vtkViewport represents part or all of a RenderWindow. It holds a
* collection of props that will be rendered into the area it represents.
* This class also contains methods to convert between coordinate systems
* commonly used in rendering.
*
* @see [vtkActor](./Rendering_Core_Actor.html)
* @see [vtkCoordinate](./Rendering_Core_Coordinate.html)
* @see [vtkProp](./Rendering_Core_Prop.html)
* @see [vtkRender](./Rendering_Core_Renderer.html)
* @see [vtkRenderWindow](./Rendering_Core_RenderWindow.html)
* @see [vtkVolume](./Rendering_Core_Volume.html)
*/
export declare const vtkViewport: {
newInstance: typeof newInstance,
extend: typeof extend,
};
export default vtkViewport;
index.js
import macro from 'vtk.js/Sources/macros';

const { vtkErrorMacro } = macro;

function notImplemented(method) {
return () => vtkErrorMacro(`vtkViewport::${method} - NOT IMPLEMENTED`);
}

// ----------------------------------------------------------------------------
// vtkViewport methods
// ----------------------------------------------------------------------------

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

// Public API methods
publicAPI.getViewProps = () => model.props;
publicAPI.hasViewProp = (prop) =>
!!model.props.filter((item) => item === prop).length;
publicAPI.addViewProp = (prop) => {
if (prop && !publicAPI.hasViewProp(prop)) {
model.props = model.props.concat(prop);
}
};

publicAPI.removeViewProp = (prop) => {
const newPropList = model.props.filter((item) => item !== prop);
if (model.props.length !== newPropList.length) {
model.props = newPropList;
}
};

publicAPI.removeAllViewProps = () => {
model.props = [];
};

// this method get all the props including any nested props
function gatherProps(prop, allProps = []) {
allProps.push(prop);
const children = prop.getNestedProps();
if (children && children.length) {
for (let i = 0; i < children.length; i++) {
gatherProps(children[i], allProps);
}
}
return allProps;
}

publicAPI.getViewPropsWithNestedProps = () => {
const allPropsArray = [];
for (let i = 0; i < model.props.length; i++) {
gatherProps(model.props[i], allPropsArray);
}
return allPropsArray;
};

publicAPI.addActor2D = publicAPI.addViewProp;
publicAPI.removeActor2D = (prop) => {
// VTK way: model.actors2D.RemoveItem(prop);
publicAPI.removeViewProp(prop);
};

publicAPI.getActors2D = () => {
model.actors2D = [];
model.props.forEach((prop) => {
model.actors2D = model.actors2D.concat(prop.getActors2D());
});
return model.actors2D;
};

publicAPI.displayToView = () =>
vtkErrorMacro('call displayToView on your view instead');
publicAPI.viewToDisplay = () =>
vtkErrorMacro('callviewtodisplay on your view instead');
publicAPI.getSize = () => vtkErrorMacro('call getSize on your View instead');

publicAPI.normalizedDisplayToProjection = (x, y, z) => {
// first to normalized viewport
const nvp = publicAPI.normalizedDisplayToNormalizedViewport(x, y, z);

// then to view
return publicAPI.normalizedViewportToProjection(nvp[0], nvp[1], nvp[2]);
};

publicAPI.normalizedDisplayToNormalizedViewport = (x, y, z) => {
const scale = [
model.viewport[2] - model.viewport[0],
model.viewport[3] - model.viewport[1],
];
return [
(x - model.viewport[0]) / scale[0],
(y - model.viewport[1]) / scale[1],
z,
];
};

publicAPI.normalizedViewportToProjection = (x, y, z) => [
x * 2.0 - 1.0,
y * 2.0 - 1.0,
z * 2.0 - 1.0,
];

publicAPI.projectionToNormalizedDisplay = (x, y, z) => {
// first to nvp
const nvp = publicAPI.projectionToNormalizedViewport(x, y, z);

// then to ndp
return publicAPI.normalizedViewportToNormalizedDisplay(
nvp[0],
nvp[1],
nvp[2]
);
};

publicAPI.normalizedViewportToNormalizedDisplay = (x, y, z) => {
const scale = [
model.viewport[2] - model.viewport[0],
model.viewport[3] - model.viewport[1],
];
return [
x * scale[0] + model.viewport[0],
y * scale[1] + model.viewport[1],
z,
];
};

publicAPI.projectionToNormalizedViewport = (x, y, z) => [
(x + 1.0) * 0.5,
(y + 1.0) * 0.5,
(z + 1.0) * 0.5,
];

publicAPI.PickPropFrom = notImplemented('PickPropFrom');
}

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

const DEFAULT_VALUES = {
// _vtkWindow: null,
background: [0, 0, 0],
background2: [0.2, 0.2, 0.2],
gradientBackground: false,
viewport: [0, 0, 1, 1],
aspect: [1, 1],
pixelAspect: [1, 1],
props: [],
actors2D: [],
};

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

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

// Build VTK API
macro.obj(publicAPI, model);
macro.event(publicAPI, model, 'event');

macro.setGetArray(publicAPI, model, ['viewport'], 4);

macro.setGetArray(publicAPI, model, ['background', 'background2'], 3);

vtkViewport(publicAPI, model);
}

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

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

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

export default { newInstance, extend };