LineSource

Introduction

vtkLineSource creates a line segment from point1 to point2;
The resolution can be specified, which determines the number of points along the line.
Following a vtkLineSource by a vtkTubeFilter is a convenient way to create a cylinder based on endpoints.

Usage

import vtkLineSource from '@kitware/vtk.js/Filters/Sources/LineSource';

const line = vtkLineSource.newInstance({ resolution: 10 });
const polydata = line.getOutputData();

Methods

extend

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

getPoint1

Get the starting point of the line.

getPoint1ByReference

Get the starting point of the line.

getPoint2

Get the ending point of the line.

getPoint2ByReference

Get the ending point of the line.

getResolution

Get the resolution of the line.

newInstance

Method used to create a new instance of vtkLineSource.

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

requestData

Argument Type Required Description
inData Yes
outData Yes

setPoint1

Set the starting point of the line.

Argument Type Required Description
point1 Vector3 Yes The starting point’s coordinates.

setPoint1

Set the starting point of the line.

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

setPoint1From

Set the starting point of the line.

Argument Type Required Description
point1 Vector3 Yes The starting point’s coordinates.

setPoint2

Set the ending point of the line.

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

setPoint2From

Set the ending point of the line.

Argument Type Required Description
point2 Vector3 Yes The ending point’s coordinates.

setResolution

Set the number of segments used to represent the line.

Argument Type Required Description
resolution Number Yes The number of segments.

Source

index.d.ts
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import { Vector3 } from '../../../types';

/**
*
*/
export interface ILineSourceInitialValues {
resolution?: number;
point1?: Vector3;
point2?: Vector3;
pointType?: string;
}

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

export interface vtkLineSource extends vtkLineSourceBase {
/**
* Get the starting point of the line.
* @default [-1, 0, 0]
*/
getPoint1(): Vector3;

/**
* Get the starting point of the line.
*/
getPoint1ByReference(): Vector3;

/**
* Get the ending point of the line.
* @default [1, 0, 0]
*/
getPoint2(): Vector3;

/**
* Get the ending point of the line.
*/
getPoint2ByReference(): Vector3;

/**
* Get the resolution of the line.
* @default 6
*/
getResolution(): number;

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

/**
* Set the starting point of the line.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
setPoint1(x: number, y: number, z: number): boolean;

/**
* Set the starting point of the line.
* @param {Vector3} point1 The starting point's coordinates.
*/
setPoint1(point1: Vector3): boolean;

/**
* Set the starting point of the line.
* @param {Vector3} point1 The starting point's coordinates.
*/
setPoint1From(point1: Vector3): boolean;

/**
* Set the ending point of the line.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
setPoint2(x: number, y: number, z: number): boolean;

/**
* Set the ending point of the line.
* @param {Vector3} point2 The ending point's coordinates.
*/
setPoint2From(point2: Vector3): boolean;

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

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

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

/**
* vtkLineSource creates a line segment from point1 to point2;
* The resolution can be specified, which determines the number of points along the line.
* Following a vtkLineSource by a vtkTubeFilter is a convenient way to create a cylinder based on endpoints.
*
* @example
* ```js
* import vtkLineSource from '@kitware/vtk.js/Filters/Sources/LineSource';
*
* const line = vtkLineSource.newInstance({ resolution: 10 });
* const polydata = line.getOutputData();
* ```
*/
export declare const vtkLineSource: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkLineSource;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';

const { vtkWarningMacro } = macro;

// ----------------------------------------------------------------------------
// vtkLineSource methods
// ----------------------------------------------------------------------------

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

publicAPI.requestData = (inData, outData) => {
if (model.deleted) {
return;
}

const dataset = outData[0];

// Check input
const pointDataType = dataset
? dataset.getPoints().getDataType()
: model.pointType;
const pd = vtkPolyData.newInstance();
const v21 = [];
vtkMath.subtract(model.point2, model.point1, v21);
if (vtkMath.norm(v21) <= 0.0) {
vtkWarningMacro('Zero-length line definition');
return;
}

// hand create a line with special scalars
const res = model.resolution;
const numPts = res + 1;

// Points
const points = macro.newTypedArray(pointDataType, numPts * 3);
pd.getPoints().setData(points, 3);

// Cells
const lines = new Uint32Array(numPts + 1);
pd.getLines().setData(lines, 1);

let idx = 0;
let t = 0.0;
for (let i = 0; i < res + 1; i++) {
t = i / res;

points[idx * 3] = model.point1[0] + t * v21[0];
points[idx * 3 + 1] = model.point1[1] + t * v21[1];
points[idx * 3 + 2] = model.point1[2] + t * v21[2];

idx++;
}

// Generate line connectivity
//
idx = 0;
lines[0] = numPts;
for (let i = 0; i < numPts; i++) {
lines[i + 1] = i;
}

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

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

const DEFAULT_VALUES = {
resolution: 10,
point1: [-1, 0, 0],
point2: [1, 0, 0],
pointType: 'Float64Array',
};

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

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

// Build VTK API
macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, ['resolution']);
macro.setGetArray(publicAPI, model, ['point1', 'point2'], 3);
macro.algo(publicAPI, model, 0, 1);
vtkLineSource(publicAPI, model);
}

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

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

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

export default { newInstance, extend };