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

88.38% Statements 137/155
64.44% Branches 29/45
100% Functions 9/9
88.59% Lines 132/149

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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                1x   1x               92x   92x 19x       19x     19x     19x 19x 19x 19x 19x   19x           19x 19x 19x 19x     19x 19x     19x 19x     19x 19x         19x     19x 19x         19x   19x 19x 19x 134x 134x 1045x   1045x 1045x 1045x   1045x 1045x   1045x 1045x 1045x   1045x           19x 19x 115x 816x 816x 816x 816x 816x   816x         19x     92x 72x   72x 72x         72x 72x   72x 72x   72x 60x 4x 4x   56x 56x   60x                   92x 132x 36x     96x 96x 96x   96x 96x 96x   96x 96x 96x   96x         96x     92x 72x   72x 72x         72x                                 92x 238x   238x 92x 146x 146x     238x 130x 130x   130x 130x 130x     130x 130x       92x 238x   238x 92x 146x 146x     238x 130x 130x   130x 130x 130x     130x 130x       92x   279x 837x       279x   279x               1x                       92x   92x 92x     92x 92x 92x 92x   92x 92x   92x 92x         1x          
import { vec3, mat4 } from 'gl-matrix';
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 vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
 
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
const { vtkWarningMacro } = macro;
 
const EPSILON = 1e-6;
 
// ----------------------------------------------------------------------------
// vtkPlaneSource methods
// ----------------------------------------------------------------------------
 
function vtkPlaneSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPlaneSource');
 
  publicAPI.requestData = (inData, outData) => {
    Iif (model.deleted) {
      return;
    }
 
    const dataset = outData[0];
 
    // Check input
    const pointDataType = dataset
      ? dataset.getPoints().getDataType()
      : model.pointType;
    const pd = vtkPolyData.newInstance();
    const v10 = [];
    const v20 = [];
    vtkMath.subtract(model.point1, model.origin, v10);
    vtkMath.subtract(model.point2, model.origin, v20);
 
    Iif (!publicAPI.updatePlane(v10, v20)) {
      vtkWarningMacro('Bad plane definition');
      return;
    }
 
    // hand create a plane with special scalars
    const xres = model.xResolution;
    const yres = model.yResolution;
    const numPts = (xres + 1) * (yres + 1);
    const numPolys = xres * yres;
 
    // Points
    const points = macro.newTypedArray(pointDataType, numPts * 3);
    pd.getPoints().setData(points, 3);
 
    // Cells
    const polys = new Uint32Array(5 * numPolys);
    pd.getPolys().setData(polys, 1);
 
    // Normals
    const normalsData = new Float32Array(numPts * 3);
    const normals = vtkDataArray.newInstance({
      numberOfComponents: 3,
      values: normalsData,
      name: 'Normals',
    });
    pd.getPointData().setNormals(normals);
 
    // Texture coords
    const tcData = new Float32Array(numPts * 2);
    const tcoords = vtkDataArray.newInstance({
      numberOfComponents: 2,
      values: tcData,
      name: 'TextureCoordinates',
    });
    pd.getPointData().setTCoords(tcoords);
 
    const tc = new Float32Array(2);
    let idx = 0;
    for (let j = 0; j < yres + 1; j++) {
      tc[1] = j / yres;
      for (let i = 0; i < xres + 1; i++) {
        tc[0] = i / xres;
 
        points[idx * 3] = model.origin[0] + tc[0] * v10[0] + tc[1] * v20[0];
        points[idx * 3 + 1] = model.origin[1] + tc[0] * v10[1] + tc[1] * v20[1];
        points[idx * 3 + 2] = model.origin[2] + tc[0] * v10[2] + tc[1] * v20[2];
 
        tcData[idx * 2] = tc[0];
        tcData[idx * 2 + 1] = tc[1];
 
        normalsData[idx * 3] = model.normal[0];
        normalsData[idx * 3 + 1] = model.normal[1];
        normalsData[idx * 3 + 2] = model.normal[2];
 
        idx++;
      }
    }
 
    // Generate polygon connectivity
    //
    idx = 0;
    for (let j = 0; j < yres; j++) {
      for (let i = 0; i < xres; i++) {
        polys[idx * 5 + 0] = 4;
        polys[idx * 5 + 1] = i + j * (xres + 1);
        polys[idx * 5 + 2] = polys[idx * 5 + 1] + 1;
        polys[idx * 5 + 3] = polys[idx * 5 + 1] + xres + 2;
        polys[idx * 5 + 4] = polys[idx * 5 + 1] + xres + 1;
 
        idx++;
      }
    }
 
    // Update output
    outData[0] = pd;
  };
 
  publicAPI.setNormal = (...normal) => {
    let n = [];
 
    if (normal.length === 1 || Array.isArray(normal[0])) {
      n = [...normal[0]];
    } else Eif (normal.length === 3) {
      n = [normal[0], normal[1], normal[2]];
    }
 
    if (vtkMath.normalize(n) !== 0) {
      const dp = vtkMath.dot(model.normal, n);
 
      let theta = 0;
      const rotationVector = [];
 
      if (dp < 1.0) {
        if (dp <= -1.0) {
          theta = vtkMath.radiansFromDegrees(180.0);
          vtkMath.subtract(model.point1, model.origin, rotationVector);
        } else {
          vtkMath.cross(model.normal, n, rotationVector);
          theta = Math.acos(dp);
        }
        publicAPI.rotate(theta, rotationVector);
      }
    }
  };
 
  /**
   * Rotate plane around a given axis
   * @param {float} theta Angle (radian) to rotate about
   * @param {vec3} rotationAxis Axis to rotate around
   */
  publicAPI.rotate = (angle, rotationAxis) => {
    if (Math.abs(angle) < EPSILON) {
      return;
    }
    // Create rotation matrix
    const transform = mat4.identity(new Float64Array(16));
    const negCenter = [];
    vec3.negate(negCenter, model.center);
 
    mat4.translate(transform, transform, model.center);
    mat4.rotate(transform, transform, angle, rotationAxis);
    mat4.translate(transform, transform, negCenter);
 
    vec3.transformMat4(model.origin, model.origin, transform);
    vec3.transformMat4(model.point1, model.point1, transform);
    vec3.transformMat4(model.point2, model.point2, transform);
 
    vtkMatrixBuilder
      .buildFromRadian()
      .rotate(angle, rotationAxis)
      .apply(model.normal);
 
    publicAPI.modified();
  };
 
  publicAPI.setCenter = (...center) => {
    let c = [];
 
    if (center.length === 1 || Array.isArray(center[0])) {
      c = [...center[0]];
    } else Eif (center.length === 3) {
      c = [center[0], center[1], center[2]];
    }
 
    Iif (!vec3.exactEquals(c, model.center)) {
      const v1 = [];
      vtkMath.subtract(model.point1, model.origin, v1);
 
      const v2 = [];
      vtkMath.subtract(model.point2, model.origin, v2);
 
      for (let i = 0; i < 3; i++) {
        model.center[i] = c[i];
        model.origin[i] = model.center[i] - 0.5 * (v1[i] + v2[i]);
        model.point1[i] = model.origin[i] + v1[i];
        model.point2[i] = model.origin[i] + v2[i];
      }
      publicAPI.modified();
    }
  };
 
  publicAPI.setPoint1 = (...point) => {
    let point1 = [];
 
    if (point.length === 1 || Array.isArray(point[0])) {
      point1 = [...point[0]];
    } else if (point.length === 3) {
      point1 = [point[0], point[1], point[2]];
    }
 
    if (!vec3.exactEquals(point1, model.point1)) {
      const v1 = [];
      const v2 = [];
 
      model.point1 = [...point1];
      vtkMath.subtract(model.point1, model.origin, v1);
      vtkMath.subtract(model.point2, model.origin, v2);
 
      // set plane normal
      publicAPI.updatePlane(v1, v2);
      publicAPI.modified();
    }
  };
 
  publicAPI.setPoint2 = (...point) => {
    let point2 = [];
 
    if (point.length === 1 || Array.isArray(point[0])) {
      point2 = [...point[0]];
    } else if (point.length === 3) {
      point2 = [point[0], point[1], point[2]];
    }
 
    if (!vec3.exactEquals(point2, model.point2)) {
      const v1 = [];
      const v2 = [];
 
      model.point2 = [...point2];
      vtkMath.subtract(model.point1, model.origin, v1);
      vtkMath.subtract(model.point2, model.origin, v2);
 
      // set plane normal
      publicAPI.updatePlane(v1, v2);
      publicAPI.modified();
    }
  };
 
  publicAPI.updatePlane = (v1, v2) => {
    // set plane center
    for (let i = 0; i < 3; i++) {
      model.center[i] = model.origin[i] + 0.5 * (v1[i] + v2[i]);
    }
 
    // set plane normal
    vtkMath.cross(v1, v2, model.normal);
 
    return vtkMath.normalize(model.normal) !== 0.0;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  xResolution: 10,
  yResolution: 10,
  origin: [0, 0, 0],
  point1: [1, 0, 0],
  point2: [0, 1, 0],
  pointType: 'Float64Array',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  model.normal = [0, 0, 1];
  model.center = [0, 0, 0];
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['xResolution', 'yResolution']);
  macro.setGetArray(publicAPI, model, ['origin'], 3);
  macro.getArray(publicAPI, model, ['point1', 'point2', 'normal', 'center'], 3);
 
  macro.algo(publicAPI, model, 0, 1);
  vtkPlaneSource(publicAPI, model);
 
  publicAPI.setPoint1(model.point1);
  publicAPI.setPoint2(model.point2);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPlaneSource');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };