@thewtex/vtk.js-esm
Version:
Visualization Toolkit for the Web
166 lines (134 loc) • 4.56 kB
JavaScript
import macro from '../../macro.js';
import vtkDataArray from '../../Common/Core/DataArray.js';
import { f as distance2BetweenPoints } from '../../Common/Core/Math/index.js';
import vtkPolyData from '../../Common/DataModel/PolyData.js';
var vtkErrorMacro = macro.vtkErrorMacro; // ----------------------------------------------------------------------------
// vtkTextureMapToSphere methods
// ----------------------------------------------------------------------------
function vtkTextureMapToSphere(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkTextureMapToSphere');
publicAPI.requestData = function (inData, outData) {
if (model.deleted) {
return;
}
var input = inData[0];
var nbPoints = input.getPoints().getNumberOfPoints();
if (nbPoints <= 1) {
vtkErrorMacro("Can't generate texture coordinates without points");
return;
}
var piOverTwo = Math.PI / 2;
var x = [];
var points = input.getPoints();
if (model.automaticSphereGeneration) {
model.center = [0, 0, 0];
for (var 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;
}
var rho = 0;
var diff = 0;
var phi = 0;
var tc = [0, 0];
var r = 0;
var thetaX = 0;
var thetaY = 0;
var tcoordsData = [];
for (var _i = 0; _i < nbPoints; _i++) {
points.getPoint(_i, x);
rho = Math.sqrt(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.apply(tcoordsData, tc);
}
var tCoords = vtkDataArray.newInstance({
name: 'Texture Coordinates',
numberOfComponents: 2,
size: nbPoints,
values: tcoordsData
});
var 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
// ----------------------------------------------------------------------------
var DEFAULT_VALUES = {
center: [0, 0, 0],
automaticSphereGeneration: 1,
preventSeam: 1
}; // ----------------------------------------------------------------------------
function extend(publicAPI, model) {
var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
Object.assign(model, DEFAULT_VALUES, initialValues); // Build VTK API
macro.obj(publicAPI, model);
macro.setGetArray(publicAPI, model, ['center']);
macro.setGet(publicAPI, model, ['automaticSphereGeneration', 'preventSeam']);
macro.algo(publicAPI, model, 1, 1);
vtkTextureMapToSphere(publicAPI, model);
} // ----------------------------------------------------------------------------
var newInstance = macro.newInstance(extend, 'vtkTextureMapToSphere'); // ----------------------------------------------------------------------------
var vtkTextureMapToSphere$1 = {
newInstance: newInstance,
extend: extend
};
export default vtkTextureMapToSphere$1;
export { extend, newInstance };