Box

Introduction

vtkBox provides methods for creating a 1D cubic spline object from given
parameters, and allows for the calculation of the spline value and derivative
at any given point inside the spline intervals.

Methods

addBounds

Add the bounds for the box.

Argument Type Required Description
bounds Bounds Yes

addBox

Argument Type Required Description
other Yes

evaluateFunction

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

evaluateFunction

Argument Type Required Description
x Point Yes The point coordinate.

extend

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

getBounds

Get the bounds for the box.

newInstance

Method used to create a new instance of vtkBox.

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

setBounds

Set the bounds for the box.

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

Source

index.d.ts
import { vtkObject } from "../../../interfaces" ;
import { Bounds, Point } from "../../../types";


export interface IBoxInitialValues {
bbox?: Bounds;
}

export interface vtkBox extends vtkObject {

/**
* Add the bounds for the box.
* @param {Bounds} bounds
*/
addBounds(bounds: Bounds): void;

/**
*
* @param other
*/
addBox(other: any): void;

/**
*
* @param {Point} x The point coordinate.
*/
evaluateFunction(x: Point): number;

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

/**
* Get the bounds for the box.
*/
getBounds(): Bounds;

/**
* Set the bounds for the box.
* @param {Bounds} bounds The bounds for the box.
*/
setBounds(bounds: Bounds): void;
}

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

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

/**
* vtkBox provides methods for creating a 1D cubic spline object from given
* parameters, and allows for the calculation of the spline value and derivative
* at any given point inside the spline intervals.
*/
export declare const vtkBox: {
newInstance: typeof newInstance,
extend: typeof extend
};
export default vtkBox;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';

// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------

export const STATIC = {};

// ----------------------------------------------------------------------------
// vtkBox methods
// ----------------------------------------------------------------------------

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

// TODO: replace with macro.setArray ?
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) {
console.log('vtkBox.setBounds', boundsArray, bounds);
return;
}

vtkBoundingBox.setBounds(model.bbox, boundsArray);
};

publicAPI.getBounds = () => model.bbox;

publicAPI.evaluateFunction = (x, y, z) => {
const point = Array.isArray(x) ? x : [x, y, z];

let diff;
let dist;
let t;
let minDistance = -Number.MAX_VALUE;
let distance = 0;
const minPoint = vtkBoundingBox.getMinPoint(model.bbox);
const maxPoint = vtkBoundingBox.getMaxPoint(model.bbox);
let inside = 1;
for (let i = 0; i < 3; i++) {
diff = vtkBoundingBox.getLength(model.bbox, i);
if (diff !== 0.0) {
t = (point[i] - minPoint[i]) / diff;
if (t < 0.0) {
inside = 0;
dist = minPoint[i] - point[i];
} else if (t > 1.0) {
inside = 0;
dist = point[i] - maxPoint[i];
} else {
// want negative distance, we are inside
if (t <= 0.5) {
dist = minPoint[i] - point[i];
} else {
dist = point[i] - maxPoint[i];
}
if (dist > minDistance) {
// remember, it's engative
minDistance = dist;
}
} // end if inside
} else {
dist = Math.abs(point[i] - minPoint[i]);
if (dist > 0.0) {
inside = 0;
}
}
if (dist > 0.0) {
distance += dist * dist;
}
} // end for i
distance = Math.sqrt(distance);
if (inside) {
return minDistance;
}
return distance;
};

publicAPI.addBounds = (...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;
}

vtkBoundingBox.addBounds(model.bbox, ...boundsArray);
publicAPI.modified();
};

publicAPI.addBox = (other) => publicAPI.addBounds(other.getBounds());
}

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

const DEFAULT_VALUES = {
bbox: [...vtkBoundingBox.INIT_BOUNDS],
};

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

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

// Object methods
macro.obj(publicAPI, model);

vtkBox(publicAPI, model);
}

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

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

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

export default { newInstance, extend, ...STATIC };