All files / Sources/Filters/Texture/TextureMapToSphere index.js

76.82% Statements 63/82
33.33% Branches 9/27
100% Functions 3/3
76.25% Lines 61/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162          1x               3x   3x 1x     1x   1x 1x         1x 1x 1x 1x 1x 1x 24x 24x 24x 24x   1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x               24x 24x           24x 24x 24x 24x             24x     24x 24x             24x             24x 24x             24x     1x             1x 1x     1x 1x     1x               1x                 3x     3x   3x 3x   3x 3x         1x          
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) => {
    Iif (model.deleted) {
      return;
    }
    const input = inData[0];
 
    const nbPoints = input.getPoints().getNumberOfPoints();
    Iif (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];
        Iif (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 E{
        tc[1] = 0;
      }
 
      r = rho * Math.sin(phi);
      if (r !== 0) {
        diff = x[0] - model.center[0];
        Iif (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];
        Iif (Math.abs(diff) > r) {
          if (diff > 0) {
            thetaY = piOverTwo;
          } else {
            thetaY = -piOverTwo;
          }
        } else {
          thetaY = Math.asin(diff / r);
        }
      } else E{
        thetaX = 0;
        thetaY = 0;
      }
 
      if (model.preventSeam) {
        tc[0] = thetaX / Math.PI;
      } else E{
        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 };