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

70.73% Statements 87/123
47.5% Branches 19/40
60% Functions 6/10
72.41% Lines 84/116

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                14x                     6x   6x 6x 6x 6x   6x 7x             7x 7x 7x 7x 7x 7x                 7x     7x 7x 7x 28x 28x 21x 21x     7x     7x             7x 7x 2x 2x 2x 2x   5x       7x     7x         7x                                                                                                                         7x 7x 7x 7x               7x 7x 7x 7x               7x         7x         7x 2x 2x 2x 2x   2x 2x 2x 5x 1x   1x 1x 1x 1x 1x 1x   7x       7x     6x 1x 1x   1x 1x 1x 1x     6x 1x     1x   1x 1x 1x   1x 4x 4x 12x                   1x         6x   6x   6x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCell from 'vtk.js/Sources/Common/DataModel/Cell';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import { CellType } from 'vtk.js/Sources/Common/DataModel/CellTypes/Constants';
import vtkTriangle from 'vtk.js/Sources/Common/DataModel/Triangle';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
 
function intersectionStruct() {
  return {
    intersected: false,
    subId: -1,
    x: [0.0, 0.0, 0.0],
    pCoords: [0.0, 0.0, 0.0],
    t: -1,
  };
}
 
function vtkQuad(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkQuad');
 
  publicAPI.getCellDimension = () => 2;
  publicAPI.getCellType = () => CellType.VTK_QUAD;
  publicAPI.getNumberOfEdges = () => 4;
  publicAPI.getNumberOfFaces = () => 0;
 
  publicAPI.intersectWithLine = (p1, p2, tol, x, pcoords) => {
    let outObj = {
      subId: 0,
      t: Number.MAX_VALUE,
      intersect: 0,
      betweenPoints: false,
    };
    let diagonalCase;
    const point0 = model.points.getPoint(0, []);
    const point1 = model.points.getPoint(1, []);
    const point2 = model.points.getPoint(2, []);
    const point3 = model.points.getPoint(3, []);
    const d1 = vtkMath.distance2BetweenPoints(point0, point2);
    const d2 = vtkMath.distance2BetweenPoints(point1, point3);
 
    /* Figure out how to uniquely tessellate the quad. Watch out for
     * equivalent triangulations (i.e., the triangulation is equivalent
     * no matter where the diagonal). In this case use the point ids as
     * a tie breaker to ensure unique triangulation across the quad.
     */
 
    // rare case; discriminate based on point id
    if (d1 === d2) {
      // find the maximum id
      let id;
      let maxId = 0;
      let maxIdx = 0;
      for (let i = 0; i < 4; i++) {
        id = model.pointsIds[i];
        if (id > maxId) {
          maxId = id;
          maxIdx = i;
        }
      }
      Iif (maxIdx === 0 || maxIdx === 2) {
        diagonalCase = 0;
      } else {
        diagonalCase = 1;
      }
    } else Eif (d1 < d2) {
      diagonalCase = 0;
    } else {
      diagonalCase = 1;
    }
    let points = null;
    if (!model.triangle) {
      model.triangle = vtkTriangle.newInstance();
      points = vtkPoints.newInstance();
      points.setNumberOfPoints(3);
      model.triangle.initialize(points);
    } else {
      points = model.triangle.getPoints();
    }
 
    let firstIntersect;
    const firstIntersectTmpObj = intersectionStruct();
 
    let secondIntersect;
    const secondIntersectTmpObj = intersectionStruct();
 
    let useFirstIntersection;
    let useSecondIntersection;
 
    switch (diagonalCase) {
      case 0:
        points.setPoint(0, ...point0);
        points.setPoint(1, ...point1);
        points.setPoint(2, ...point2);
        firstIntersect = model.triangle.intersectWithLine(
          p1,
          p2,
          tol,
          firstIntersectTmpObj.x,
          firstIntersectTmpObj.pCoords
        );
 
        points.setPoint(0, ...point2);
        points.setPoint(1, ...point3);
        points.setPoint(2, ...point0);
        secondIntersect = model.triangle.intersectWithLine(
          p1,
          p2,
          tol,
          secondIntersectTmpObj.x,
          secondIntersectTmpObj.pCoords
        );
 
        useFirstIntersection =
          firstIntersect.intersect && secondIntersect.intersect
            ? firstIntersect.t <= secondIntersect.t
            : firstIntersect.intersect;
 
        useSecondIntersection =
          firstIntersect.intersect && secondIntersect.intersect
            ? secondIntersect.t < firstIntersect.t
            : secondIntersect.intersect;
 
        if (useFirstIntersection) {
          outObj = firstIntersect;
 
          x[0] = firstIntersectTmpObj.x[0];
          x[1] = firstIntersectTmpObj.x[1];
          x[2] = firstIntersectTmpObj.x[2];
 
          pcoords[0] =
            firstIntersectTmpObj.pCoords[0] + firstIntersectTmpObj.pCoords[1];
          pcoords[1] = firstIntersectTmpObj.pCoords[1];
          pcoords[2] = firstIntersectTmpObj.pCoords[2];
        } else if (useSecondIntersection) {
          outObj = secondIntersect;
          x[0] = secondIntersectTmpObj.x[0];
          x[1] = secondIntersectTmpObj.x[1];
          x[2] = secondIntersectTmpObj.x[2];
 
          pcoords[0] =
            1.0 -
            (secondIntersectTmpObj.pCoords[0] +
              secondIntersectTmpObj.pCoords[1]);
          pcoords[1] = 1 - secondIntersectTmpObj.pCoords[1];
          pcoords[2] = secondIntersectTmpObj.pCoords[2];
        }
 
        break;
      case 1:
        points.setPoint(0, ...point0);
        points.setPoint(1, ...point1);
        points.setPoint(2, ...point3);
        firstIntersect = model.triangle.intersectWithLine(
          p1,
          p2,
          tol,
          firstIntersectTmpObj.x,
          firstIntersectTmpObj.pCoords
        );
 
        points.setPoint(0, ...point2);
        points.setPoint(1, ...point3);
        points.setPoint(2, ...point1);
        secondIntersect = model.triangle.intersectWithLine(
          p1,
          p2,
          tol,
          secondIntersectTmpObj.x,
          secondIntersectTmpObj.pCoords
        );
 
        useFirstIntersection =
          firstIntersect.intersect && secondIntersect.intersect
            ? firstIntersect.t <= secondIntersect.t
            : firstIntersect.intersect;
 
        useSecondIntersection =
          firstIntersect.intersect && secondIntersect.intersect
            ? secondIntersect.t < firstIntersect.t
            : secondIntersect.intersect;
 
        if (useFirstIntersection) {
          outObj = firstIntersect;
          x[0] = firstIntersectTmpObj.x[0];
          x[1] = firstIntersectTmpObj.x[1];
          x[2] = firstIntersectTmpObj.x[2];
 
          pcoords[0] = firstIntersectTmpObj.pCoords[0];
          pcoords[1] = firstIntersectTmpObj.pCoords[1];
          pcoords[2] = firstIntersectTmpObj.pCoords[2];
        } else if (useSecondIntersection) {
          outObj = secondIntersect;
 
          x[0] = secondIntersectTmpObj.x[0];
          x[1] = secondIntersectTmpObj.x[1];
          x[2] = secondIntersectTmpObj.x[2];
          pcoords[0] = 1 - secondIntersectTmpObj.pCoords[0];
          pcoords[1] = 1 - secondIntersectTmpObj.pCoords[1];
          pcoords[2] = secondIntersectTmpObj.pCoords[2];
        }
        break;
      default:
        break;
    }
    return outObj;
  };
 
  publicAPI.interpolationFunctions = (pcoords, weights) => {
    const rm = 1 - pcoords[0];
    const sm = 1 - pcoords[1];
 
    weights[0] = rm * sm;
    weights[1] = pcoords[0] * sm;
    weights[2] = pcoords[0] * pcoords[1];
    weights[3] = rm * pcoords[1];
  };
 
  publicAPI.evaluateLocation = (pcoords, x, weights) => {
    const point = [];
 
    // Calculate the weights
    publicAPI.interpolationFunctions(pcoords, weights);
 
    x[0] = 0.0;
    x[1] = 0.0;
    x[2] = 0.0;
 
    for (let i = 0; i < 4; i++) {
      model.points.getPoint(i, point);
      for (let j = 0; j < 3; j++) {
        x[j] += point[j] * weights[i];
      }
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkCell.extend(publicAPI, model, initialValues);
 
  vtkQuad(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkQuad');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };