Points

Introduction

vtkPoints represents 3D points. The data model for vtkPoints is an array
of vx-vy-vz triplets accessible by (point or cell) id.

Methods

computeBounds

Trigger the computation of bounds

extend

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

findPoint

Convenient method to search a point in the array.
This is a naïve search. Consider using a “locator” instead.

Argument Type Required Description
pointToSearch Array or TypedArray Yes
precision Number Yes (1e-6 by default)

Returns

Type Description
Number the index of the point if found, -1 otherwise.

getBounds

Get a copy of the bounds of the array.
Bounds are [xmin, xmax, ymin, ymax,zmin, zmax].
Will recompute the bounds if necessary.

getBoundsByReference

Get a reference to the model bounds of the array.
Bounds are [xmin, xmax, ymin, ymax,zmin, zmax].
Will recompute the bounds if necessary.

getNumberOfPoints

Get the number of points for this object can hold.

getPoint

Get the coordinate of a point.

Argument Type Required Description
idx Number Yes The index of point.
tupleToFill Array[Number] or TypedArray No (default [])

Returns

Type Description
Array[Number] or TypedArray

insertNextPoint

Insert the (x,y,z) coordinates of a point at the next available slot.

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

Returns

Type Description
Number Index of the inserted point.

insertPoint

Insert the [x,y,z] coordinates of a point at the given index.

Argument Type Required Description
ptId Number Yes The index of point.
point Array[Number] Yes The [x, y, z] coordinates of the point.

Returns

Type Description
Number The index of the inserted point.

newInstance

Method used to create a new instance of vtkPoints

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

setNumberOfPoints

Set the number of points for this object to hold.

points.getData()[0] = x;
points.getData()[1] = y;
points.getData()[2] = z;
Argument Type Required Description
nbPoints Number Yes
dimension Number No

setPoint

Set the (x,y,z) coordinates of a point based on its index.

Argument Type Required Description
idx Number Yes The index of point.
x Number Yes The x coordinate.
y Number Yes The y coordinate.
z Number Yes The z coordinate.

Source

index.d.ts
import vtkDataArray from '../../../Common/Core/DataArray';
import { Bounds, TypedArray } from '../../../types';

/**
*
*/
export interface IPointsInitialValues {
empty?: boolean;
numberOfComponents?: number;
bounds?: Bounds;
}

export interface vtkPoints extends vtkDataArray {
/**
* Trigger the computation of bounds
*/
computeBounds(): Bounds;

/**
* Get a copy of the bounds of the array.
* Bounds are [xmin, xmax, ymin, ymax,zmin, zmax].
* Will recompute the bounds if necessary.
* @return {Bounds} The bounds for the mapper.
*/
getBounds(): Bounds;

/**
* Get a reference to the model bounds of the array.
* Bounds are [xmin, xmax, ymin, ymax,zmin, zmax].
* Will recompute the bounds if necessary.
* @return {Bounds} The bounds for the mapper.
*/
getBoundsByReference(): Bounds;

/**
* Get the coordinate of a point.
* @param {Number} idx The index of point.
* @param {Number[]|TypedArray} [tupleToFill] (default [])
* @returns {Number[]|TypedArray}
*/
getPoint(
idx: number,
tupleToFill?: number[] | TypedArray
): number[] | TypedArray;

/**
* Convenient method to search a point in the array.
* This is a naïve search. Consider using a "locator" instead.
* @param {Array<Number>|TypedArray} pointToSearch
* @param {Number} precision (1e-6 by default)
* @returns {Number} the index of the point if found, -1 otherwise.
*/
findPoint(
pointToSearch: Array<number> | TypedArray,
precision?: number
): number;

/**
* Get the number of points for this object can hold.
*/
getNumberOfPoints(): number;

/**
* Set the number of points for this object to hold.
*
* ```js
* points.getData()[0] = x;
* points.getData()[1] = y;
* points.getData()[2] = z;
* ```
*
* @param {Number} nbPoints
* @param {Number} [dimension]
*/
setNumberOfPoints(nbPoints: number, dimension?: number): void;

/**
* Set the (x,y,z) coordinates of a point based on its index.
* @param {Number} idx The index of point.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
*/
setPoint(idx: number, x: number, y: number, z: number): void;

/**
* Insert the (x,y,z) coordinates of a point at the next available slot.
* @param {Number} x The x coordinate.
* @param {Number} y The y coordinate.
* @param {Number} z The z coordinate.
* @returns {Number} Index of the inserted point.
*/
insertNextPoint(x: number, y: number, z: number): number;

/**
* Insert the [x,y,z] coordinates of a point at the given index.
* @param {Number} ptId The index of point.
* @param {Number[]} point The [x, y, z] coordinates of the point.
* @returns {Number} The index of the inserted point.
*/
insertPoint(ptId: number, point: number[]): number;
}

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

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

/**
* vtkPoints represents 3D points. The data model for vtkPoints is an array
* of vx-vy-vz triplets accessible by (point or cell) id.
*/

export declare const vtkPoints: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkPoints;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';

const { vtkErrorMacro } = macro;
// ----------------------------------------------------------------------------
// vtkPoints methods
// ----------------------------------------------------------------------------

function vtkPoints(publicAPI, model) {
// Keep track of modified time for bounds computation
let boundMTime = 0;

// Set our className
model.classHierarchy.push('vtkPoints');

// Forwarding methods
publicAPI.getNumberOfPoints = publicAPI.getNumberOfTuples;

publicAPI.setNumberOfPoints = (nbPoints, dimension = 3) => {
if (publicAPI.getNumberOfPoints() !== nbPoints) {
model.size = nbPoints * dimension;
model.values = macro.newTypedArray(model.dataType, model.size);
publicAPI.setNumberOfComponents(dimension);
publicAPI.modified();
}
};

publicAPI.setPoint = (idx, ...xyz) => {
publicAPI.setTuple(idx, xyz);
};

publicAPI.getPoint = publicAPI.getTuple;
publicAPI.findPoint = publicAPI.findTuple;

publicAPI.insertNextPoint = (x, y, z) => publicAPI.insertNextTuple([x, y, z]);

publicAPI.insertPoint = (ptId, point) => publicAPI.insertTuple(ptId, point);

const superGetBounds = publicAPI.getBounds;
publicAPI.getBounds = () => {
if (boundMTime < model.mtime) {
publicAPI.computeBounds();
}
return superGetBounds();
};

const superGetBoundsByReference = publicAPI.getBoundsByReference;
publicAPI.getBoundsByReference = () => {
if (boundMTime < model.mtime) {
publicAPI.computeBounds();
}
return superGetBoundsByReference();
};

// Trigger the computation of bounds
publicAPI.computeBounds = () => {
if (publicAPI.getNumberOfComponents() === 3) {
const xRange = publicAPI.getRange(0);
model.bounds[0] = xRange[0];
model.bounds[1] = xRange[1];
const yRange = publicAPI.getRange(1);
model.bounds[2] = yRange[0];
model.bounds[3] = yRange[1];
const zRange = publicAPI.getRange(2);
model.bounds[4] = zRange[0];
model.bounds[5] = zRange[1];
} else if (publicAPI.getNumberOfComponents() === 2) {
const xRange = publicAPI.getRange(0);
model.bounds[0] = xRange[0];
model.bounds[1] = xRange[1];
const yRange = publicAPI.getRange(1);
model.bounds[2] = yRange[0];
model.bounds[3] = yRange[1];
model.bounds[4] = 0;
model.bounds[5] = 0;
} else {
vtkErrorMacro(
`getBounds called on an array with components of ${publicAPI.getNumberOfComponents()}`
);
vtkMath.uninitializeBounds(model.bounds);
}
boundMTime = macro.getCurrentGlobalMTime();
};

// Initialize
publicAPI.setNumberOfComponents(
model.numberOfComponents < 2 ? 3 : model.numberOfComponents
);
}

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

const DEFAULT_VALUES = {
empty: true,
numberOfComponents: 3,
dataType: VtkDataTypes.FLOAT,
bounds: [1, -1, 1, -1, 1, -1],
};

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

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

vtkDataArray.extend(publicAPI, model, initialValues);

macro.getArray(publicAPI, model, ['bounds'], 6);
vtkPoints(publicAPI, model);
}

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

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

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

export default { newInstance, extend };