Sphere

Introduction

vtkSphere 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

evaluateFunction

Given the point xyz (three floating value) evaluate the sphere
equation.

Argument Type Required Description
xyz Vector3 Yes The point coordinate.

evaluateGradient

Given the point xyz (three floating values) evaluate the equation for the
sphere gradient.

Argument Type Required Description
xyz Vector3 Yes The point coordinate.

extend

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

getCenter

Get the center of the sphere.

getCenterByReference

Get the center of the sphere.

getRadius

Get the radius of the sphere.

newInstance

Method used to create a new instance of vtkSphere.

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

setCenter

Set the center of the sphere.

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

setCenter

Set the center of the sphere.

Argument Type Required Description
center Vector3 Yes The center coordinate.

setCenterFrom

Set the center of the sphere.

Argument Type Required Description
center Vector3 Yes The center coordinate.

setRadius

Set the radius of the sphere.

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

Source

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


export interface ISphereInitialValues {
radius?: number;
center?: Vector3;
}

export interface vtkSphere extends vtkObject {

/**
* Given the point xyz (three floating value) evaluate the sphere
* equation.
* @param {Vector3} xyz The point coordinate.
*/
evaluateFunction(xyz: Vector3): number[];

/**
* Given the point xyz (three floating values) evaluate the equation for the
* sphere gradient.
* @param {Vector3} xyz The point coordinate.
*/
evaluateGradient(xyz: Vector3): number[];

/**
* Get the center of the sphere.
*/
getCenter(): Vector3;

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

/**
* Get the radius of the sphere.
*/
getRadius(): number;

/**
* Set the center of the sphere.
* @param {Vector3} center The center coordinate.
*/
setCenter(center: Vector3): boolean;

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

/**
* Set the center of the sphere.
* @param {Vector3} center The center coordinate.
*/
setCenterFrom(center: Vector3): boolean;

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

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

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

/**
* @param {Number} radius
* @param {Vector3} center
* @param {Vector3} x
*/
declare function evaluate(radius: number, center: Vector3, x: Vector3): number;

/**
*
* @param {Vector3} point
* @param {Bounds} bounds
*/
declare function isPointIn3DEllipse(point: Vector3, bounds: Bounds): boolean;

/**
* vtkSphere 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 vtkSphere: {
newInstance: typeof newInstance,
extend: typeof extend;
evaluate: typeof evaluate;
isPointIn3DEllipse: typeof isPointIn3DEllipse;
};
export default vtkSphere;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';

const VTK_SMALL_NUMBER = 1e-12;

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

function evaluate(radius, center, x) {
return (
(x[0] - center[0]) * (x[0] - center[0]) +
(x[1] - center[1]) * (x[1] - center[1]) +
(x[2] - center[2]) * (x[2] - center[2]) -
radius * radius
);
}

function isPointIn3DEllipse(point, bounds) {
const center = vtkBoundingBox.getCenter(bounds);
let scale3 = vtkBoundingBox.computeScale3(bounds);
scale3 = scale3.map((x) => Math.max(Math.abs(x), VTK_SMALL_NUMBER));

const radius = [
(point[0] - center[0]) / scale3[0],
(point[1] - center[1]) / scale3[1],
(point[2] - center[2]) / scale3[2],
];

return (
radius[0] * radius[0] + radius[1] * radius[1] + radius[2] * radius[2] <= 1
);
}

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

export const STATIC = {
evaluate,
isPointIn3DEllipse,
};

// ----------------------------------------------------------------------------
// vtkSphere methods
// ----------------------------------------------------------------------------

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

publicAPI.evaluateFunction = (xyz) => {
const retVal =
(xyz[0] - model.center[0]) * (xyz[0] - model.center[0]) +
(xyz[1] - model.center[1]) * (xyz[1] - model.center[1]) +
(xyz[2] - model.center[2]) * (xyz[2] - model.center[2]) -
model.radius * model.radius;

return retVal;
};

publicAPI.evaluateGradient = (xyz) => {
const retVal = [
2.0 - (xyz[0] - model.center[0]),
2.0 - (xyz[1] - model.center[1]),
2.0 - (xyz[2] - model.center[2]),
];
return retVal;
};
}

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

const DEFAULT_VALUES = {
radius: 0.5,
center: [0.0, 0.0, 0.0],
};

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

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

// Object methods
macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, ['radius']);
macro.setGetArray(publicAPI, model, ['center'], 3);

vtkSphere(publicAPI, model);
}

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

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

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

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