CylinderSource

Introduction

vtkCylinderSource creates a polygonal cylinder centered at Center;
The axis of the cylinder is aligned along the global y-axis.
The height and radius of the cylinder can be specified, as well as the number of sides.
It is also possible to control whether the cylinder is open-ended or capped.
If you have the end points of the cylinder, you should use a vtkLineSource followed by a vtkTubeFilter instead of the vtkCylinderSource.

Usage

import vtkCylinderSource from 'vtk.js/Sources/Filters/Sources/CylinderSource';

const cylinder = vtkCylinderSource.newInstance({ height: 2, radius: 1, resolution: 80 });
const polydata = cylinder.getOutputData();

Methods

extend

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

getCapping

Get the cap the base of the cylinder with a polygon.

getCenter

Get the center of the cylinder.

getCenterByReference

Get the center of the cylinder.

getDirection

Get the orientation vector of the cylinder.

getDirectionByReference

Get the orientation vector of the cylinder.

getHeight

Get the height of the cylinder.

getInitAngle

Get the initial angle along direction

getOtherRadius

Get the radius on Z axis. If not null and different from radius,
the cylinder base becomes an ellipse instead of a circle.

getRadius

Get the base radius of the cylinder.

getResolution

Get the number of facets used to represent the cylinder.

newInstance

Method used to create a new instance of vtkCylinderSource.

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

requestData

Argument Type Required Description
inData Yes
outData Yes

setCapping

Turn on/off whether to cap the base of the cone with a polygon.

Argument Type Required Description
capping Boolean Yes The capping value.

setCenter

Set the center of the cylinder.

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

setCenterFrom

Set the center of the cylinder.

Argument Type Required Description
center Array. Yes The center point’s coordinates.

setDirection

Set the direction for the cylinder.

Argument Type Required Description
direction Array. Yes The direction coordinates.

setDirection

Set the direction for the cylinder.

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

setDirectionFrom

Set the direction for the cylinder.

Argument Type Required Description
direction Array. Yes The direction coordinates.

setHeight

Set the height of the cylinder.

Argument Type Required Description
height Number Yes The height along the cylinder in its specified direction.

setInitAngle

Set the initial angle along direction.

Argument Type Required Description
initAngle Number Yes The initial angle in radian.

setOtherRadius

Set the base Z radius of the cylinder.

Argument Type Required Description
radius Number Yes The radius of the cylinder in Z.

setRadius

Set the base radius of the cylinder.

Argument Type Required Description
radius Number Yes The radius of the cylinder.

setResolution

Set the number of facets used to represent the cylinder.

Argument Type Required Description
resolution Number Yes The number of facets used to represent the cylinder.

Source

index.d.ts
import { vtkAlgorithm, vtkObject } from "../../../interfaces";

/**
*
*/
export interface ICylinderSourceInitialValues {
height?: number;
initAngle?: number;
otherRadius?: number;
radius?: number;
resolution?: number;
center?: number[];
direction?: number[];
capping?: boolean;
pointType?: string;
}

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

export interface vtkCylinderSource extends vtkCylinderSourceBase {

/**
* Get the cap the base of the cylinder with a polygon.
* @default true
*/
getCapping(): boolean;

/**
* Get the center of the cylinder.
* @default [0, 0, 0]
*/
getCenter(): number[];

/**
* Get the center of the cylinder.
*/
getCenterByReference(): number[];

/**
* Get the orientation vector of the cylinder.
* @default [1.0, 0.0, 0.0]
*/
getDirection(): number[];

/**
* Get the orientation vector of the cylinder.
*/
getDirectionByReference(): number[];

/**
* Get the height of the cylinder.
* @default 1.0
*/
getHeight(): number;

/**
* Get the initial angle along direction
* @default 0
* @see getDirection
*/
getInitAngle(): number;

/**
* Get the radius on Z axis. If not null and different from radius,
* the cylinder base becomes an ellipse instead of a circle.
* @default null
* @see getRadius()
*/
getOtherRadius(): number;

/**
* Get the base radius of the cylinder.
* @default 0.5
* @see getOtherRadius()
*/
getRadius(): number;

/**
* Get the number of facets used to represent the cylinder.
* @default 6
*/
getResolution(): number;

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

/**
* Turn on/off whether to cap the base of the cone with a polygon.
* @param {Boolean} capping The capping value.
*/
setCapping(capping: boolean): boolean;

/**
* Set the center of the cylinder.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
* @default [0, 0, 0]
*/
setCenter(x: number, y: number, z: number): boolean;

/**
* Set the center of the cylinder.
* @param {Number[]} center The center point's coordinates.
* @default [0, 0, 0]
*/
setCenterFrom(center: number[]): boolean;

/**
* Set the direction for the cylinder.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
setDirection(x: number, y: number, z: number): boolean;

/**
* Set the direction for the cylinder.
* @param {Number[]} direction The direction coordinates.
*/
setDirection(direction: number[]): boolean;

/**
* Set the direction for the cylinder.
* @param {Number[]} direction The direction coordinates.
*/
setDirectionFrom(direction: number[]): boolean;

/**
* Set the height of the cylinder.
* @param {Number} height The height along the cylinder in its specified direction.
*/
setHeight(height: number): boolean;

/**
* Set the initial angle along direction.
* @param {Number} initAngle The initial angle in radian.
*/
setInitAngle(radianAngle: number): boolean;

/**
* Set the base Z radius of the cylinder.
* @param {Number} radius The radius of the cylinder in Z.
* @see setRadius()
*/
setOtherRadius(radius: number): boolean;

/**
* Set the base radius of the cylinder.
* @param {Number} radius The radius of the cylinder.
* @see setOtherRadius()
*/
setRadius(radius: number): boolean;

/**
* Set the number of facets used to represent the cylinder.
* @param {Number} resolution The number of facets used to represent the cylinder.
*/
setResolution(resolution: number): boolean;

}

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

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

/**
* vtkCylinderSource creates a polygonal cylinder centered at Center;
* The axis of the cylinder is aligned along the global y-axis.
* The height and radius of the cylinder can be specified, as well as the number of sides.
* It is also possible to control whether the cylinder is open-ended or capped.
* If you have the end points of the cylinder, you should use a vtkLineSource followed by a vtkTubeFilter instead of the vtkCylinderSource.
*
* @example
* ```js
* import vtkCylinderSource from 'vtk.js/Sources/Filters/Sources/CylinderSource';
*
* const cylinder = vtkCylinderSource.newInstance({ height: 2, radius: 1, resolution: 80 });
* const polydata = cylinder.getOutputData();
* ```
*/
export declare const vtkCylinderSource: {
newInstance: typeof newInstance,
extend: typeof extend,
};
export default vtkCylinderSource;
index.js
import macro from 'vtk.js/Sources/macros';
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';

// ----------------------------------------------------------------------------
// vtkCylinderSource methods
// ----------------------------------------------------------------------------

function vtkCylinderSource(publicAPI, model) {
// Set our classname
model.classHierarchy.push('vtkCylinderSource');

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

let dataset = outData[0];

const angle = (2.0 * Math.PI) / model.resolution;
let numberOfPoints = 2 * model.resolution;
let numberOfPolys = 5 * model.resolution;

if (model.capping) {
numberOfPoints = 4 * model.resolution;
numberOfPolys = 7 * model.resolution + 2;
}

// Points
const points = macro.newTypedArray(model.pointType, numberOfPoints * 3);

// Cells
let cellLocation = 0;
const polys = new Uint32Array(numberOfPolys);

// Normals
const normalsData = new Float32Array(numberOfPoints * 3);
const normals = vtkDataArray.newInstance({
numberOfComponents: 3,
values: normalsData,
name: 'Normals',
});

// Texture coords
const tcData = new Float32Array(numberOfPoints * 2);
const tcoords = vtkDataArray.newInstance({
numberOfComponents: 2,
values: tcData,
name: 'TCoords',
});

// Generate points for all sides
const nbot = [0.0, 0.0, 0.0];
const ntop = [0.0, 0.0, 0.0];
const xbot = [0.0, 0.0, 0.0];
const xtop = [0.0, 0.0, 0.0];
const tcbot = [0.0, 0.0];
const tctop = [0.0, 0.0];
const otherRadius =
model.otherRadius == null ? model.radius : model.otherRadius;
for (let i = 0; i < model.resolution; i++) {
// x coordinate
nbot[0] = Math.cos(i * angle + model.initAngle);
ntop[0] = nbot[0];
xbot[0] = model.radius * nbot[0] + model.center[0];
xtop[0] = xbot[0];
tcbot[0] = Math.abs((2.0 * i) / model.resolution - 1.0);
tctop[0] = tcbot[0];

// y coordinate
xbot[1] = 0.5 * model.height + model.center[1];
xtop[1] = -0.5 * model.height + model.center[1];
tcbot[1] = 0.0;
tctop[1] = 1.0;

// z coordinate
nbot[2] = -Math.sin(i * angle + model.initAngle);
ntop[2] = nbot[2];
xbot[2] = otherRadius * nbot[2] + model.center[2];
xtop[2] = xbot[2];

const pointIdx = 2 * i;
for (let j = 0; j < 3; j++) {
normalsData[pointIdx * 3 + j] = nbot[j];
normalsData[(pointIdx + 1) * 3 + j] = ntop[j];
points[pointIdx * 3 + j] = xbot[j];
points[(pointIdx + 1) * 3 + j] = xtop[j];
if (j < 2) {
tcData[pointIdx * 2 + j] = tcbot[j];
tcData[(pointIdx + 1) * 2 + j] = tctop[j];
}
}
}

// Generate polygons for sides
for (let i = 0; i < model.resolution; i++) {
polys[cellLocation++] = 4;
polys[cellLocation++] = 2 * i;
polys[cellLocation++] = 2 * i + 1;
const pt = (2 * i + 3) % (2 * model.resolution);
polys[cellLocation++] = pt;
polys[cellLocation++] = pt - 1;
}

if (model.capping) {
// Generate points for top/bottom polygons
for (let i = 0; i < model.resolution; i++) {
// x coordinate
xbot[0] = model.radius * Math.cos(i * angle + model.initAngle);
xtop[0] = xbot[0];
tcbot[0] = xbot[0];
tctop[0] = xbot[0];
xbot[0] += model.center[0];
xtop[0] += model.center[0];

// y coordinate
nbot[1] = 1.0;
ntop[1] = -1.0;
xbot[1] = 0.5 * model.height + model.center[1];
xtop[1] = -0.5 * model.height + model.center[1];

// z coordinate
xbot[2] = -otherRadius * Math.sin(i * angle + model.initAngle);
xtop[2] = xbot[2];
tcbot[1] = xbot[2];
tctop[1] = xbot[2];
xbot[2] += model.center[2];
xtop[2] += model.center[2];
const botIdx = 2 * model.resolution + i;
const topIdx = 3 * model.resolution + model.resolution - i - 1;
for (let j = 0; j < 3; j++) {
normalsData[3 * botIdx + j] = nbot[j];
normalsData[3 * topIdx + j] = ntop[j];
points[3 * botIdx + j] = xbot[j];
points[3 * topIdx + j] = xtop[j];
if (j < 2) {
tcData[2 * botIdx + j] = tcbot[j];
tcData[2 * topIdx + j] = tctop[j];
}
}
}

// Generate polygons for top/bottom
polys[cellLocation++] = model.resolution;
for (let i = 0; i < model.resolution; i++) {
polys[cellLocation++] = 2 * model.resolution + i;
}
polys[cellLocation++] = model.resolution;
for (let i = 0; i < model.resolution; i++) {
polys[cellLocation++] = 3 * model.resolution + i;
}
}

// Apply transformation to the points coordinates
vtkMatrixBuilder
.buildFromRadian()
.translate(...model.center)
.rotateFromDirections([0, 1, 0], model.direction)
.translate(...model.center.map((c) => c * -1))
.apply(points);

dataset = vtkPolyData.newInstance();
dataset.getPoints().setData(points, 3);
dataset.getPolys().setData(polys, 1);
dataset.getPointData().setNormals(normals);
dataset.getPointData().setTCoords(tcoords);

// Update output
outData[0] = dataset;
}

// Expose methods
publicAPI.requestData = requestData;
}

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

const DEFAULT_VALUES = {
height: 1.0,
initAngle: 0,
radius: 1.0,
resolution: 6,
center: [0, 0, 0],
direction: [0.0, 1.0, 0.0],
capping: true,
pointType: 'Float32Array',
};

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

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

// Build VTK API
macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, [
'height',
'initAngle',
'otherRadius',
'radius',
'resolution',
'capping',
]);
macro.setGetArray(publicAPI, model, ['center', 'direction'], 3);
macro.algo(publicAPI, model, 0, 1);
vtkCylinderSource(publicAPI, model);
}

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

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

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

export default { newInstance, extend };