CubeSource

Introduction

vtkCubeSource creates a cube centered at origin. The cube is represented with four-sided polygons.
It is possible to specify the length, width, and height of the cube independently.

Usage

import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource';

const cubeSource = vtkCubeSource.newInstance({ xLength: 5, yLength: 5, zLength: 5 });
const polydata = cubeSource.getOutputData();

Methods

extend

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

getCenter

Get the center of the cube.

getCenterByReference

Get the center of the cube.

getGenerate3DTextureCoordinates

getGenerateFaces

Flag that indicates whether the output will generate faces of the outline.

Returns

Type Description
boolean

getGenerateLines

Flag that indicates whether the output will generate wireframe lines of the outline.

Returns

Type Description
boolean

getMatrix

Get the 4x4 transformation set to apply as a final trasformation to the output.

Argument Type Required Description
matrix Yes

getRotations

getRotationsByReference

getXLength

Get the length of the cube in the x-direction.

getYLength

Get the length of the cube in the y-direction.

getZLength

Get the length of the cube in the z-direction.

newInstance

Method used to create a new instance of vtkCubeSource.

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

requestData

Expose methods

Argument Type Required Description
inData Yes
outData Yes

setBounds

Convenience methods allows creation of cube by specifying bounding box.

Argument Type Required Description
bounds Bounds Yes The bounds for the cube.

setBounds

Convenience methods allows creation of cube by specifying bounding box.

Argument Type Required Description
xMin Number Yes
xMax Number Yes
yMin Number Yes
yMax Number Yes
zMin Number Yes
zMax Number Yes

setCenter

Set the center of the cube.

Argument Type Required Description
x Yes
y Yes
z Yes

setCenterFrom

Set the center of the cube.

Argument Type Required Description
center Yes

setGenerate3DTextureCoordinates

Argument Type Required Description
generate3DTextureCoordinates Yes

setGenerateFaces

Flag to indicate that the output should generate triangulated faces of the outline.

Argument Type Required Description
generateFaces boolean Yes

setGenerateLines

Flag to indicate that the output should generate wireframe of the outline.

Argument Type Required Description
generateLines boolean Yes

setMatrix

Set a 4x4 transformation that will be applied as a final trasformation to the output.

Argument Type Required Description
matrix Yes

setRotations

Float array of size 3 representing the angles, in degrees, of rotation for the cube.

Argument Type Required Description
xAngle Yes
yAngle Yes
zAngle Yes

setRotationsFrom

Argument Type Required Description
rotations Vector3 Yes

setXLength

Set the length of the cube in the x-direction.

Argument Type Required Description
xLength Yes

setYLength

Set the length of the cube in the y-direction.

Argument Type Required Description
yLength Yes

setZLength

Set the length of the cube in the z-direction.

Argument Type Required Description
zLength Yes

Source

index.d.ts
import { mat4 } from 'gl-matrix';
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import { Bounds, Vector3 } from '../../../types';

/**
*
*/
export interface ICircleSourceInitialValues {
xLength?: number;
yLength?: number;
zLength?: number;
center?: Vector3;
rotations?: Vector3;
pointType?: string;
generate3DTextureCoordinates?: boolean;
}

type vtkCubeSourceBase = vtkObject &
Omit<
vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'
>;

export interface vtkCubeSource extends vtkCubeSourceBase {
/**
* Get the center of the cube.
* @default [0.0, 0.0, 0.0]
*/
getCenter(): Vector3;

/**
* Get the center of the cube.
*/
getCenterByReference(): Vector3;

/**
*
* @default false
*/
getGenerate3DTextureCoordinates(): boolean;

/**
* Flag that indicates whether the output will generate faces of the outline.
* @returns {boolean}
*/
getGenerateFaces(): boolean;

/**
* Flag that indicates whether the output will generate wireframe lines of the outline.
* @returns {boolean}
*/
getGenerateLines(): boolean;

/**
* Get the 4x4 transformation set to apply as a final trasformation to the output.
* @param matrix
*/
getMatrix(): mat4;

/**
*
* @default [0.0, 0.0, 0.0]
*/
getRotations(): Vector3;

/**
*
* @default [0.0, 0.0, 0.0]
*/
getRotationsByReference(): Vector3;

/**
* Get the length of the cube in the x-direction.
* @default 1.0
*/
getXLength(): number;

/**
* Get the length of the cube in the y-direction.
* @default 1.0
*/
getYLength(): number;

/**
* Get the length of the cube in the z-direction.
* @default 1.0
*/
getZLength(): number;

/**
* Expose methods
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Convenience methods allows creation of cube by specifying bounding box.
* @param {Number} xMin
* @param {Number} xMax
* @param {Number} yMin
* @param {Number} yMax
* @param {Number} zMin
* @param {Number} zMax
*/
setBounds(
xMin: number,
xMax: number,
yMin: number,
yMax: number,
zMin: number,
zMax: number
): boolean;

/**
* Convenience methods allows creation of cube by specifying bounding box.
* @param {Bounds} bounds The bounds for the cube.
*/
setBounds(bounds: Bounds): boolean;

/**
* Set the center of the cube.
* @param x
* @param y
* @param z
* @default [0, 0, 0]
*/
setCenter(x: number, y: number, z: number): boolean;

/**
* Set the center of the cube.
* @param center
* @default [0, 0, 0]
*/
setCenterFrom(center: Vector3): boolean;

/**
*
* @param generate3DTextureCoordinates
*/
setGenerate3DTextureCoordinates(
generate3DTextureCoordinates: boolean
): boolean;

/**
* Flag to indicate that the output should generate wireframe of the outline.
* @param {boolean} generateLines
*/
setGenerateLines(generateLines: boolean): boolean;

/**
* Flag to indicate that the output should generate triangulated faces of the outline.
* @param {boolean} generateFaces
*/
setGenerateFaces(generateFaces: boolean): boolean;

/**
* Set a 4x4 transformation that will be applied as a final trasformation to the output.
* @param matrix
*/
setMatrix(matrix: mat4): boolean;

/**
* Float array of size 3 representing the angles, in degrees, of rotation for the cube.
* @param xAngle
* @param yAngle
* @param zAngle
*/
setRotations(xAngle: number, yAngle: number, zAngle: number): boolean;

/**
*
* @param {Vector3} rotations
*/
setRotationsFrom(rotations: Vector3): boolean;

/**
* Set the length of the cube in the x-direction.
* @param xLength
*/
setXLength(xLength: number): boolean;

/**
* Set the length of the cube in the y-direction.
* @param yLength
*/
setYLength(yLength: number): boolean;

/**
* Set the length of the cube in the z-direction.
* @param zLength
*/
setZLength(zLength: number): boolean;
}

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

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

/**
* vtkCubeSource creates a cube centered at origin. The cube is represented with four-sided polygons.
* It is possible to specify the length, width, and height of the cube independently.
*
* @example
* ```js
* import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource';
*
* const cubeSource = vtkCubeSource.newInstance({ xLength: 5, yLength: 5, zLength: 5 });
* const polydata = cubeSource.getOutputData();
* ```
*/
export declare const vtkCubeSource: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkCubeSource;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';

// prettier-ignore
const LINE_ARRAY = [
2, 0, 1,
2, 2, 3,
2, 4, 5,
2, 6, 7,
2, 0, 2,
2, 1, 3,
2, 4, 6,
2, 5, 7,
2, 0, 4,
2, 1, 5,
2, 2, 6,
2, 3, 7,
];

// prettier-ignore
const POLY_ARRAY = [
4, 0, 1, 3, 2,
4, 4, 6, 7, 5,
4, 8, 10, 11, 9,
4, 12, 13, 15, 14,
4, 16, 18, 19, 17,
4, 20, 21, 23, 22,
];

// ----------------------------------------------------------------------------
// vtkCubeSource methods
// ----------------------------------------------------------------------------

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

function requestData(inData, outData) {
if (model.deleted) {
return;
}

const polyData = vtkPolyData.newInstance();
outData[0] = polyData;

const numberOfPoints = 24;

// Define points
const points = macro.newTypedArray(model.pointType, numberOfPoints * 3);
polyData.getPoints().setData(points, 3);

const normals = macro.newTypedArray(model.pointType, numberOfPoints * 3);
const normalArray = vtkDataArray.newInstance({
name: 'Normals',
values: normals,
numberOfComponents: 3,
});
polyData.getPointData().setNormals(normalArray);

let tcdim = 2;
if (model.generate3DTextureCoordinates === true) {
tcdim = 3;
}

const textureCoords = macro.newTypedArray(
model.pointType,
numberOfPoints * tcdim
);
const tcoords = vtkDataArray.newInstance({
name: 'TextureCoordinates',
values: textureCoords,
numberOfComponents: tcdim,
});
polyData.getPointData().setTCoords(tcoords);

const x = [0.0, 0.0, 0.0];
const n = [0.0, 0.0, 0.0];
const tc = [0.0, 0.0];

let pointIndex = 0;

x[0] = -model.xLength / 2.0;
n[0] = -1.0;
n[1] = 0.0;
n[2] = 0.0;
for (let i = 0; i < 2; i++) {
x[1] = -model.yLength / 2.0;

for (let j = 0; j < 2; j++) {
tc[1] = x[1] + 0.5;
x[2] = -model.zLength / 2.0;

for (let k = 0; k < 2; k++) {
tc[0] = (x[2] + 0.5) * (1 - 2 * i);
points[pointIndex * 3] = x[0];
points[pointIndex * 3 + 1] = x[1];
points[pointIndex * 3 + 2] = x[2];

normals[pointIndex * 3] = n[0];
normals[pointIndex * 3 + 1] = n[1];
normals[pointIndex * 3 + 2] = n[2];

if (tcdim === 2) {
textureCoords[pointIndex * tcdim] = tc[0];
textureCoords[pointIndex * tcdim + 1] = tc[1];
} else {
textureCoords[pointIndex * tcdim] = 2 * i - 1;
textureCoords[pointIndex * tcdim + 1] = 2 * j - 1;
textureCoords[pointIndex * tcdim + 2] = 2 * k - 1;
}

pointIndex++;

x[2] += model.zLength;
}
x[1] += model.yLength;
}
x[0] += model.xLength;
n[0] += 2.0;
}

x[1] = -model.yLength / 2.0;
n[1] = -1.0;
n[0] = 0.0;
n[2] = 0.0;
for (let i = 0; i < 2; i++) {
x[0] = -model.xLength / 2.0;

for (let j = 0; j < 2; j++) {
tc[0] = (x[0] + 0.5) * (2 * i - 1);
x[2] = -model.zLength / 2.0;

for (let k = 0; k < 2; k++) {
tc[1] = (x[2] + 0.5) * -1;

points[pointIndex * 3] = x[0];
points[pointIndex * 3 + 1] = x[1];
points[pointIndex * 3 + 2] = x[2];

normals[pointIndex * 3] = n[0];
normals[pointIndex * 3 + 1] = n[1];
normals[pointIndex * 3 + 2] = n[2];

if (tcdim === 2) {
textureCoords[pointIndex * tcdim] = tc[0];
textureCoords[pointIndex * tcdim + 1] = tc[1];
} else {
textureCoords[pointIndex * tcdim] = 2 * j - 1;
textureCoords[pointIndex * tcdim + 1] = 2 * i - 1;
textureCoords[pointIndex * tcdim + 2] = 2 * k - 1;
}

pointIndex++;
x[2] += model.zLength;
}
x[0] += model.xLength;
}
x[1] += model.yLength;
n[1] += 2.0;
}

x[2] = -model.zLength / 2.0;
n[2] = -1.0;
n[0] = 0.0;
n[1] = 0.0;
for (let i = 0; i < 2; i++) {
x[1] = -model.yLength / 2.0;

for (let j = 0; j < 2; j++) {
tc[1] = x[1] + 0.5;
x[0] = -model.xLength / 2.0;

for (let k = 0; k < 2; k++) {
tc[0] = (x[0] + 0.5) * (2 * i - 1);

points[pointIndex * 3] = x[0];
points[pointIndex * 3 + 1] = x[1];
points[pointIndex * 3 + 2] = x[2];

normals[pointIndex * 3] = n[0];
normals[pointIndex * 3 + 1] = n[1];
normals[pointIndex * 3 + 2] = n[2];

if (tcdim === 2) {
textureCoords[pointIndex * tcdim] = tc[0];
textureCoords[pointIndex * tcdim + 1] = tc[1];
} else {
textureCoords[pointIndex * tcdim] = 2 * k - 1;
textureCoords[pointIndex * tcdim + 1] = 2 * j - 1;
textureCoords[pointIndex * tcdim + 2] = 2 * i - 1;
}

pointIndex++;
x[0] += model.xLength;
}
x[1] += model.yLength;
}
x[2] += model.zLength;
n[2] += 2.0;
}

// Apply rotation to the points coordinates and normals
if (model.rotations) {
vtkMatrixBuilder
.buildFromDegree()
.rotateX(model.rotations[0])
.rotateY(model.rotations[1])
.rotateZ(model.rotations[2])
.apply(points)
.apply(normals);
}

// Apply transformation to the points coordinates
if (model.center) {
vtkMatrixBuilder
.buildFromRadian()
.translate(...model.center)
.apply(points);
}

// Apply optional additionally specified matrix transformation
if (model.matrix) {
vtkMatrixBuilder.buildFromRadian().setMatrix(model.matrix).apply(points);

// prettier-ignore
const rotMatrix = [
model.matrix[0], model.matrix[1], model.matrix[2], 0,
model.matrix[4], model.matrix[5], model.matrix[6], 0,
model.matrix[8], model.matrix[9], model.matrix[10], 0,
0, 0, 0, 1
];
vtkMatrixBuilder.buildFromRadian().setMatrix(rotMatrix).apply(normals);
}

// Lastly, generate the necessary cell arrays.
if (model.generateFaces) {
polyData.getPolys().deepCopy(model._polys);
} else {
polyData.getPolys().initialize();
}
if (model.generateLines) {
polyData.getLines().deepCopy(model._lineCells);
// only set normals for faces, not for lines.
polyData.getPointData().setNormals(null);
} else {
polyData.getLines().initialize();
}
polyData.modified();
}

publicAPI.setBounds = (...bounds) => {
let boundsArray = [];

if (Array.isArray(bounds[0])) {
boundsArray = bounds[0];
} else {
for (let i = 0; i < bounds.length; i++) {
boundsArray.push(bounds[i]);
}
}

if (boundsArray.length !== 6) {
return;
}

publicAPI.setXLength(boundsArray[1] - boundsArray[0]);
publicAPI.setYLength(boundsArray[3] - boundsArray[2]);
publicAPI.setZLength(boundsArray[5] - boundsArray[4]);
publicAPI.setCenter([
(boundsArray[0] + boundsArray[1]) / 2.0,
(boundsArray[2] + boundsArray[3]) / 2.0,
(boundsArray[4] + boundsArray[5]) / 2.0,
]);
};

// Expose methods
publicAPI.requestData = requestData;
}

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

const DEFAULT_VALUES = {
xLength: 1.0,
yLength: 1.0,
zLength: 1.0,
pointType: 'Float64Array',
generate3DTextureCoordinates: false,
generateFaces: true,
generateLines: false,
};

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

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

// Build VTK API
macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, [
'xLength',
'yLength',
'zLength',
'generate3DTextureCoordinates',
'generateFaces',
'generateLines',
]);
macro.setGetArray(publicAPI, model, ['center', 'rotations'], 3);
macro.setGetArray(publicAPI, model, ['matrix'], 16);

// Internal persistent/static objects
model._polys = vtkCellArray.newInstance({
values: Uint16Array.from(POLY_ARRAY),
});
model._lineCells = vtkCellArray.newInstance({
values: Uint16Array.from(LINE_ARRAY),
});
macro.moveToProtected(publicAPI, model, ['polys', 'lineCells']);

macro.algo(publicAPI, model, 0, 1);
vtkCubeSource(publicAPI, model);
}

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

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

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

export default { newInstance, extend };