All files / Sources/Filters/Sources/SphereSource index.js

93.49% Statements 115/123
48.48% Branches 16/33
100% Functions 3/3
93.16% Lines 109/117

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246                    534x   534x 524x       524x 524x     524x     524x     524x   524x 524x   524x 524x     524x 524x 524x 524x   524x     524x   524x 524x   524x 524x     524x 524x     524x     524x 524x     524x 524x 524x 524x   524x 524x 524x   524x 524x       524x 524x 524x 524x   524x 524x 524x   524x 524x     524x 524x     524x 4386x 4386x 31216x 31216x   31216x 31216x 31216x   31216x 31216x 31216x   31216x           31216x 31216x 31216x 31216x   31216x         524x   524x         524x 524x 4386x 4386x 4386x 4386x         524x 524x   524x 4386x 4386x 4386x 4386x         524x 4386x 26830x 26830x 26830x   26830x 26830x 26830x 26830x 26830x 26830x 26830x 26830x 26830x                       524x 524x   524x 524x         524x   524x 524x     524x               1x                               534x     534x 534x                   534x 534x 534x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
 
// ----------------------------------------------------------------------------
// vtkSphereSource methods
// ----------------------------------------------------------------------------
 
function vtkSphereSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSphereSource');
 
  publicAPI.requestData = (inData, outData) => {
    Iif (model.deleted) {
      return;
    }
 
    let dataset = outData[0];
    const pointDataType = dataset
      ? dataset.getPoints().getDataType()
      : model.pointType;
    dataset = vtkPolyData.newInstance();
 
    // ----------------------------------------------------------------------
    let numPoles = 0;
 
    // Check data, determine increments, and convert to radians
    let { thetaResolution } = model;
    let startTheta =
      model.startTheta < model.endTheta ? model.startTheta : model.endTheta;
    startTheta *= Math.PI / 180.0;
    let endTheta =
      model.endTheta > model.startTheta ? model.endTheta : model.startTheta;
    endTheta *= Math.PI / 180.0;
 
    let startPhi =
      model.startPhi < model.endPhi ? model.startPhi : model.endPhi;
    startPhi *= Math.PI / 180.0;
    let endPhi = model.endPhi > model.startPhi ? model.endPhi : model.startPhi;
    endPhi *= Math.PI / 180.0;
 
    Iif (Math.abs(startTheta - endTheta) < 2.0 * Math.PI) {
      ++thetaResolution;
    }
    const deltaTheta = (endTheta - startTheta) / model.thetaResolution;
 
    const jStart = model.startPhi <= 0.0 ? 1 : 0;
    const jEnd = model.phiResolution + (model.endPhi >= 180.0 ? -1 : 0);
 
    const numPts = model.phiResolution * thetaResolution + 2;
    const numPolys = model.phiResolution * 2 * model.thetaResolution;
 
    // Points
    let pointIdx = 0;
    let points = macro.newTypedArray(pointDataType, numPts * 3);
 
    // Normals
    let normals = new Float32Array(numPts * 3);
 
    // Cells
    let cellLocation = 0;
    let polys = new Uint32Array(numPolys * 5);
 
    // Create north pole if needed
    if (model.startPhi <= 0.0) {
      points[pointIdx * 3 + 0] = model.center[0];
      points[pointIdx * 3 + 1] = model.center[1];
      points[pointIdx * 3 + 2] = model.center[2] + model.radius;
 
      normals[pointIdx * 3 + 0] = 0;
      normals[pointIdx * 3 + 1] = 0;
      normals[pointIdx * 3 + 2] = 1;
 
      pointIdx++;
      numPoles++;
    }
 
    // Create south pole if needed
    if (model.endPhi >= 180.0) {
      points[pointIdx * 3 + 0] = model.center[0];
      points[pointIdx * 3 + 1] = model.center[1];
      points[pointIdx * 3 + 2] = model.center[2] - model.radius;
 
      normals[pointIdx * 3 + 0] = 0;
      normals[pointIdx * 3 + 1] = 0;
      normals[pointIdx * 3 + 2] = -1;
 
      pointIdx++;
      numPoles++;
    }
 
    const phiResolution = model.phiResolution - numPoles;
    const deltaPhi = (endPhi - startPhi) / (model.phiResolution - 1);
 
    // Create intermediate points
    for (let i = 0; i < thetaResolution; i++) {
      const theta = startTheta + i * deltaTheta;
      for (let j = jStart; j < jEnd; j++) {
        const phi = startPhi + j * deltaPhi;
        const radius = model.radius * Math.sin(phi);
 
        normals[pointIdx * 3 + 0] = radius * Math.cos(theta);
        normals[pointIdx * 3 + 1] = radius * Math.sin(theta);
        normals[pointIdx * 3 + 2] = model.radius * Math.cos(phi);
 
        points[pointIdx * 3 + 0] = normals[pointIdx * 3 + 0] + model.center[0];
        points[pointIdx * 3 + 1] = normals[pointIdx * 3 + 1] + model.center[1];
        points[pointIdx * 3 + 2] = normals[pointIdx * 3 + 2] + model.center[2];
 
        let norm = Math.sqrt(
          normals[pointIdx * 3 + 0] * normals[pointIdx * 3 + 0] +
            normals[pointIdx * 3 + 1] * normals[pointIdx * 3 + 1] +
            normals[pointIdx * 3 + 2] * normals[pointIdx * 3 + 2]
        );
 
        norm = norm === 0 ? 1 : norm;
        normals[pointIdx * 3 + 0] /= norm;
        normals[pointIdx * 3 + 1] /= norm;
        normals[pointIdx * 3 + 2] /= norm;
 
        pointIdx++;
      }
    }
 
    // Generate mesh connectivity
    const base = phiResolution * thetaResolution;
 
    Iif (Math.abs(startTheta - endTheta) < 2.0 * Math.PI) {
      --thetaResolution;
    }
 
    // around north pole
    if (model.startPhi <= 0.0) {
      for (let i = 0; i < thetaResolution; i++) {
        polys[cellLocation++] = 3;
        polys[cellLocation++] = phiResolution * i + numPoles;
        polys[cellLocation++] = ((phiResolution * (i + 1)) % base) + numPoles;
        polys[cellLocation++] = 0;
      }
    }
 
    // around south pole
    if (model.endPhi >= 180.0) {
      const numOffset = phiResolution - 1 + numPoles;
 
      for (let i = 0; i < thetaResolution; i++) {
        polys[cellLocation++] = 3;
        polys[cellLocation++] = phiResolution * i + numOffset;
        polys[cellLocation++] = numPoles - 1;
        polys[cellLocation++] = ((phiResolution * (i + 1)) % base) + numOffset;
      }
    }
 
    // bands in-between poles
    for (let i = 0; i < thetaResolution; i++) {
      for (let j = 0; j < phiResolution - 1; j++) {
        const a = phiResolution * i + j + numPoles;
        const b = a + 1;
        const c = ((phiResolution * (i + 1) + j) % base) + numPoles + 1;
 
        if (!model.latLongTessellation) {
          polys[cellLocation++] = 3;
          polys[cellLocation++] = a;
          polys[cellLocation++] = b;
          polys[cellLocation++] = c;
          polys[cellLocation++] = 3;
          polys[cellLocation++] = a;
          polys[cellLocation++] = c;
          polys[cellLocation++] = c - 1;
        } else E{
          polys[cellLocation++] = 4;
          polys[cellLocation++] = a;
          polys[cellLocation++] = b;
          polys[cellLocation++] = c;
          polys[cellLocation++] = c - 1;
        }
      }
    }
 
    // Squeeze
    points = points.subarray(0, pointIdx * 3);
    dataset.getPoints().setData(points, 3);
 
    normals = normals.subarray(0, pointIdx * 3);
    const normalArray = vtkDataArray.newInstance({
      name: 'Normals',
      values: normals,
      numberOfComponents: 3,
    });
    dataset.getPointData().setNormals(normalArray);
 
    polys = polys.subarray(0, cellLocation);
    dataset.getPolys().setData(polys, 1);
 
    // Update output
    outData[0] = dataset;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  radius: 0.5,
  latLongTessellation: false,
  thetaResolution: 8,
  startTheta: 0.0,
  endTheta: 360.0,
  phiResolution: 8,
  startPhi: 0.0,
  endPhi: 180.0,
  center: [0, 0, 0],
  pointType: 'Float64Array',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, [
    'radius',
    'latLongTessellation',
    'thetaResolution',
    'startTheta',
    'endTheta',
    'phiResolution',
    'startPhi',
    'endPhi',
  ]);
  macro.setGetArray(publicAPI, model, ['center'], 3);
  macro.algo(publicAPI, model, 0, 1);
  vtkSphereSource(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkSphereSource');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };