All files / Sources/Common/DataModel/Plane index.js

79.67% Statements 98/123
70% Branches 21/30
78.94% Functions 15/19
79.16% Lines 95/120

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 1x             103x                 2x       2x                             4x   4x 4x       4x 4x 4x 4x       16x 16x   16x 16x   16x 16x 16x 16x                 459x             459x 459x   459x 459x       459x 459x               459x 143x   316x   459x 71x   388x   459x 140x         319x   319x 319x 319x   319x 319x 319x                 3x             3x 3x 9x     3x   2x 2x 2x 1x   1x   2x           1x   1x 1x           1x   1x 1x     1x             1x 1x 1x 1x               1x 1x 1x   1x             1x                                   115x   115x 2x   115x       115x   115x 1x     1x 3x       115x       115x 1947x 1946x           1x             115x         115x 3x   115x 3x             1x               115x     115x   115x   115x         1x          
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import macro from 'vtk.js/Sources/macros';
import vtkImplicitFunction from 'vtk.js/Sources/Common/DataModel/ImplicitFunction';
 
const PLANE_TOLERANCE = 1.0e-6;
const COINCIDE = 'coincide';
const DISJOINT = 'disjoint';
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
function evaluate(normal, origin, x) {
  return (
    normal[0] * (x[0] - origin[0]) +
    normal[1] * (x[1] - origin[1]) +
    normal[2] * (x[2] - origin[2])
  );
}
 
function distanceToPlane(x, origin, normal) {
  const distance =
    normal[0] * (x[0] - origin[0]) +
    normal[1] * (x[1] - origin[1]) +
    normal[2] * (x[2] - origin[2]);
 
  return Math.abs(distance);
}
 
function projectPoint(x, origin, normal, xproj) {
  const xo = [];
  vtkMath.subtract(x, origin, xo);
 
  const t = vtkMath.dot(normal, xo);
 
  xproj[0] = x[0] - t * normal[0];
  xproj[1] = x[1] - t * normal[1];
  xproj[2] = x[2] - t * normal[2];
}
 
function projectVector(v, normal, vproj) {
  const t = vtkMath.dot(v, normal);
 
  let n2 = vtkMath.dot(normal, normal);
  Iif (n2 === 0) {
    n2 = 1.0;
  }
 
  vproj[0] = v[0] - (t * normal[0]) / n2;
  vproj[1] = v[1] - (t * normal[1]) / n2;
  vproj[2] = v[2] - (t * normal[2]) / n2;
  return vproj;
}
 
function generalizedProjectPoint(x, origin, normal, xproj) {
  const xo = [];
  vtkMath.subtract(x, origin, xo);
 
  const t = vtkMath.dot(normal, xo);
  const n2 = vtkMath.dot(normal, normal);
 
  if (n2 !== 0) {
    xproj[0] = x[0] - (t * normal[0]) / n2;
    xproj[1] = x[1] - (t * normal[1]) / n2;
    xproj[2] = x[2] - (t * normal[2]) / n2;
  } else E{
    xproj[0] = x[0];
    xproj[1] = x[1];
    xproj[2] = x[2];
  }
}
 
function intersectWithLine(p1, p2, origin, normal) {
  const outObj = {
    intersection: false,
    betweenPoints: false,
    t: Number.MAX_VALUE,
    x: [],
  };
 
  const p21 = [];
  const p1Origin = [];
  // Compute line vector
  vtkMath.subtract(p2, p1, p21);
  vtkMath.subtract(origin, p1, p1Origin);
 
  // Compute denominator.  If ~0, line and plane are parallel.
  // const num = vtkMath.dot(normal, origin) - vtkMath.dot(normal, p1);
  const num = vtkMath.dot(normal, p1Origin);
  const den = vtkMath.dot(normal, p21);
 
  // If denominator with respect to numerator is "zero", then the line and
  // plane are considered parallel.
  let fabsden;
  let fabstolerance;
 
  // Trying to avoid an expensive call to fabs()
  if (den < 0.0) {
    fabsden = -den;
  } else {
    fabsden = den;
  }
  if (num < 0.0) {
    fabstolerance = -num * PLANE_TOLERANCE;
  } else {
    fabstolerance = num * PLANE_TOLERANCE;
  }
  if (fabsden <= fabstolerance) {
    return outObj;
  }
 
  // Where on the line between p1 and p2 is the intersection
  // If between 0 and 1, it is between the two points. If < 0 it's before p1, if > 1 it's after p2
  outObj.t = num / den;
 
  outObj.x[0] = p1[0] + outObj.t * p21[0];
  outObj.x[1] = p1[1] + outObj.t * p21[1];
  outObj.x[2] = p1[2] + outObj.t * p21[2];
 
  outObj.intersection = true;
  outObj.betweenPoints = outObj.t >= 0.0 && outObj.t <= 1.0;
  return outObj;
}
 
function intersectWithPlane(
  plane1Origin,
  plane1Normal,
  plane2Origin,
  plane2Normal
) {
  const outObj = {
    intersection: false,
    l0: [],
    l1: [],
    error: null,
  };
 
  const cross = [];
  vtkMath.cross(plane1Normal, plane2Normal, cross);
  const absCross = cross.map((n) => Math.abs(n));
 
  // test if the two planes are parallel
  if (absCross[0] + absCross[1] + absCross[2] < PLANE_TOLERANCE) {
    // test if disjoint or coincide
    const v = [];
    vtkMath.subtract(plane1Origin, plane2Origin, v);
    if (vtkMath.dot(plane1Normal, v) === 0) {
      outObj.error = COINCIDE;
    } else {
      outObj.error = DISJOINT;
    }
    return outObj;
  }
 
  // Plane1 and Plane2 intersect in a line
  // first determine max abs coordinate of the cross product
  let maxc;
  Iif (absCross[0] > absCross[1] && absCross[0] > absCross[2]) {
    maxc = 'x';
  } else if (absCross[1] > absCross[2]) {
    maxc = 'y';
  } else E{
    maxc = 'z';
  }
 
  // To get a point on the intersect line, zero the max coord, and solve for the other two
  const iP = []; // intersectionPoint
  // the constants in the 2 plane equations
  const d1 = -vtkMath.dot(plane1Normal, plane1Origin);
  const d2 = -vtkMath.dot(plane2Normal, plane2Origin);
 
  // eslint-disable-next-line default-case
  switch (maxc) {
    case 'x': // intersect with x=0
      iP[0] = 0;
      iP[1] = (d2 * plane1Normal[2] - d1 * plane2Normal[2]) / cross[0];
      iP[2] = (d1 * plane2Normal[1] - d2 * plane1Normal[1]) / cross[0];
      break;
    case 'y': // intersect with y=0
      iP[0] = (d1 * plane2Normal[2] - d2 * plane1Normal[2]) / cross[1];
      iP[1] = 0;
      iP[2] = (d2 * plane1Normal[0] - d1 * plane2Normal[0]) / cross[1];
      break;
    case 'z': // intersect with z=0
      iP[0] = (d2 * plane1Normal[1] - d1 * plane2Normal[1]) / cross[2];
      iP[1] = (d1 * plane2Normal[0] - d2 * plane1Normal[0]) / cross[2];
      iP[2] = 0;
      break;
  }
 
  outObj.l0 = iP;
  vtkMath.add(iP, cross, outObj.l1);
  outObj.intersection = true;
 
  return outObj;
}
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
export const STATIC = {
  evaluate,
  distanceToPlane,
  projectPoint,
  projectVector,
  generalizedProjectPoint,
  intersectWithLine,
  intersectWithPlane,
  DISJOINT,
  COINCIDE,
};
 
// ----------------------------------------------------------------------------
// vtkPlane methods
// ----------------------------------------------------------------------------
 
export function vtkPlane(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPlane');
 
  publicAPI.distanceToPlane = (x) =>
    distanceToPlane(x, model.origin, model.normal);
 
  publicAPI.projectPoint = (x, xproj) => {
    projectPoint(x, model.origin, model.normal, xproj);
  };
 
  publicAPI.projectVector = (v, vproj) => projectVector(v, model.normal, vproj);
 
  publicAPI.push = (distance) => {
    Iif (distance === 0.0) {
      return;
    }
    for (let i = 0; i < 3; i++) {
      model.origin[i] += distance * model.normal[i];
    }
  };
 
  publicAPI.generalizedProjectPoint = (x, xproj) => {
    generalizedProjectPoint(x, model.origin, model.normal, xproj);
  };
 
  publicAPI.evaluateFunction = (x, y, z) => {
    if (!Array.isArray(x)) {
      return (
        model.normal[0] * (x - model.origin[0]) +
        model.normal[1] * (y - model.origin[1]) +
        model.normal[2] * (z - model.origin[2])
      );
    }
    return (
      model.normal[0] * (x[0] - model.origin[0]) +
      model.normal[1] * (x[1] - model.origin[1]) +
      model.normal[2] * (x[2] - model.origin[2])
    );
  };
 
  publicAPI.evaluateGradient = (xyz) => {
    const retVal = [model.normal[0], model.normal[1], model.normal[2]];
    return retVal;
  };
 
  publicAPI.intersectWithLine = (p1, p2) =>
    intersectWithLine(p1, p2, model.origin, model.normal);
 
  publicAPI.intersectWithPlane = (planeOrigin, planeNormal) =>
    intersectWithPlane(planeOrigin, planeNormal, model.origin, model.normal);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  normal: [0.0, 0.0, 1.0],
  origin: [0.0, 0.0, 0.0],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkImplicitFunction.extend(publicAPI, model, initialValues);
 
  macro.setGetArray(publicAPI, model, ['normal', 'origin'], 3);
 
  vtkPlane(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPlane');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC };