TextureMapToSphere

Introduction

vtkTextureMapToSphere generate texture coordinates by mapping points to
sphere The TCoords DataArray is name ‘Texture Coordinate’

Methods

extend

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

getAutomaticSphereGeneration

Get whether the automatic sphere generation is set.

getCenter

Get the point defining the center of the sphere.

getCenterByReference

Get the normal object.

getPreventSeam

Get whether the prevent seam is set.

newInstance

Method used to create a new instance of vtkTextureMapToSphere

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

requestData

Argument Type Required Description
inData Yes
outData Yes

setAutomaticSphereGeneration

Turn on/off the automatic sphere generation.

Argument Type Required Description
automaticSphereGeneration Yes

setCenter

Set the point defining the center of the sphere.

Argument Type Required Description
x Yes
y Yes
z Yes

setCenter

Set the point defining the center of the sphere.

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

setCenterFrom

Set the point defining the center of the sphere.

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

setPreventSeam

Control how the texture coordinates are generated.

If PreventSeam is set, the s-coordinate ranges :

  • from 0->1 and 1->0 corresponding to the theta angle variation between 0->180 and 180->0 degrees
  • Otherwise, the s-coordinate ranges from 0->1 between 0->360 degrees.
Argument Type Required Description
preventSeam Yes

Source

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

/**
*
*/
interface ITextureMapToSphere {
center?: number[];
automaticSphereGeneration?: number;
preventSeam?: number;
}

type vtkTextureMapToSphereBase = vtkObject & vtkAlgorithm;

export interface vtkTextureMapToSphere extends vtkTextureMapToSphereBase {
/**
* Get whether the automatic sphere generation is set.
*/
getAutomaticSphereGeneration(): number;

/**
* Get the point defining the center of the sphere.
*/
getCenter(): number[];

/**
* Get the normal object.
*/
getCenterByReference(): number[];

/**
* Get whether the prevent seam is set.
*/
getPreventSeam(): number;

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

/**
* Turn on/off the automatic sphere generation.
* @param automaticSphereGeneration
*/
setAutomaticSphereGeneration(automaticSphereGeneration: number): boolean;

/**
* Control how the texture coordinates are generated.
*
* If PreventSeam is set, the s-coordinate ranges :
*
* - from 0->1 and 1->0 corresponding to the theta angle variation between 0->180 and 180->0 degrees
* - Otherwise, the s-coordinate ranges from 0->1 between 0->360 degrees.
* @param preventSeam
*/
setPreventSeam(preventSeam: number): boolean;

/**
* Set the point defining the center of the sphere.
* @param {Number[]} center The center point coordinates.
*/
setCenter(center: number[]): boolean;

/**
* Set the point defining the center of the sphere.
* @param x
* @param y
* @param z
*/
setCenter(x: number, y: number, z: number): boolean;

/**
* Set the point defining the center of the sphere.
* @param {Number[]} center The center point coordinates.
*/
setCenterFrom(center: number[]): boolean;
}

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

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

/**
* vtkTextureMapToSphere generate texture coordinates by mapping points to
* sphere The TCoords DataArray is name 'Texture Coordinate'
*/
export declare const vtkTextureMapToSphere: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkTextureMapToSphere;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';

const { vtkErrorMacro } = macro;

// ----------------------------------------------------------------------------
// vtkTextureMapToSphere methods
// ----------------------------------------------------------------------------

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

publicAPI.requestData = (inData, outData) => {
if (model.deleted) {
return;
}
const input = inData[0];

const nbPoints = input.getPoints().getNumberOfPoints();
if (nbPoints <= 1) {
vtkErrorMacro("Can't generate texture coordinates without points");
return;
}

const piOverTwo = Math.PI / 2;
const x = [];
const points = input.getPoints();
if (model.automaticSphereGeneration) {
model.center = [0, 0, 0];
for (let i = 0; i < nbPoints; i++) {
points.getPoint(i, x);
model.center[0] += x[0];
model.center[1] += x[1];
model.center[2] += x[2];
}
model.center[0] /= nbPoints;
model.center[1] /= nbPoints;
model.center[2] /= nbPoints;
}

let rho = 0;
let diff = 0;
let phi = 0;
const tc = [0, 0];
let r = 0;
let thetaX = 0;
let thetaY = 0;
const tcoordsData = [];
for (let i = 0; i < nbPoints; i++) {
points.getPoint(i, x);
rho = Math.sqrt(vtkMath.distance2BetweenPoints(x, model.center));
if (rho !== 0) {
diff = x[2] - model.center[2];
if (Math.abs(diff) > rho) {
phi = 0;
if (diff > 0) {
tc[1] = 0;
} else {
tc[1] = 1;
}
} else {
phi = Math.acos(diff / rho);
tc[1] = phi / Math.PI;
}
} else {
tc[1] = 0;
}

r = rho * Math.sin(phi);
if (r !== 0) {
diff = x[0] - model.center[0];
if (Math.abs(diff) > r) {
if (diff > 0) {
thetaX = 0;
} else {
thetaX = Math.PI;
}
} else {
thetaX = Math.acos(diff / r);
}

diff = x[1] - model.center[1];
if (Math.abs(diff) > r) {
if (diff > 0) {
thetaY = piOverTwo;
} else {
thetaY = -piOverTwo;
}
} else {
thetaY = Math.asin(diff / r);
}
} else {
thetaX = 0;
thetaY = 0;
}

if (model.preventSeam) {
tc[0] = thetaX / Math.PI;
} else {
tc[0] = thetaX / (2 * Math.PI);
if (thetaY < 0) {
tc[0] = 1 - tc[0];
}
}
tcoordsData.push(...tc);
}

const tCoords = vtkDataArray.newInstance({
name: 'Texture Coordinates',
numberOfComponents: 2,
size: nbPoints,
values: tcoordsData,
});

const output = vtkPolyData.newInstance();
output
.getPoints()
.setData(new Float32Array(input.getPoints().getData()), 3);
output.getPolys().setData(new Uint32Array(input.getPolys().getData()));
output.getPointData().setTCoords(tCoords);

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

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

const DEFAULT_VALUES = {
center: [0, 0, 0],
automaticSphereGeneration: 1,
preventSeam: 1,
};

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

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

// Build VTK API
macro.obj(publicAPI, model);

macro.setGetArray(publicAPI, model, ['center'], 3);
macro.setGet(publicAPI, model, ['automaticSphereGeneration', 'preventSeam']);

macro.algo(publicAPI, model, 1, 1);
vtkTextureMapToSphere(publicAPI, model);
}

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

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

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

export default { newInstance, extend };