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

40.54% Statements 45/111
28.07% Branches 16/57
50% Functions 5/10
42.15% Lines 43/102

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                                                                                                                                                                                          1x             4x     4x 2x   2x 2x             2x         2x     4x   4x 4x         4x 4x 4x 4x 4x 4x 12x 12x 12x 12x     12x 1x 1x     11x 9x   2x   11x   4x                 12x 1x     4x 4x 3x   1x     4x                                     4x 4x               1x             4x     4x   4x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import vtkImplicitFunction from 'vtk.js/Sources/Common/DataModel/ImplicitFunction';
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
//------------------------------------------------------------------------------
// Bounding box intersection code from David Gobbi.  Go through the
// bounding planes one at a time and compute the parametric coordinate
// of each intersection and return the parametric values and the calculated points
export function intersectWithLine(bounds, p1, p2) {
  let plane1 = -1;
  let plane2 = -1;
  let t1 = 0.0;
  let t2 = 1.0;
 
  for (let j = 0; j < 3; j++) {
    for (let k = 0; k < 2; k++) {
      // Compute distances of p1 and p2 from the plane along the plane normal
      const i = 2 * j + k;
      const d1 = (bounds[i] - p1[j]) * (1 - 2 * k);
      const d2 = (bounds[i] - p2[j]) * (1 - 2 * k);
 
      // If both distances are positive, both points are outside
      if (d1 > 0 && d2 > 0) {
        return;
      }
      // If one of the distances is positive, the line crosses the plane
      if (d1 > 0 || d2 > 0) {
        // Compute fractional distance "t" of the crossing between p1 & p2
        let t = 0.0;
        if (d1 !== 0) {
          t = d1 / (d1 - d2);
        }
 
        // If point p1 was clipped, adjust t1
        if (d1 > 0) {
          if (t >= t1) {
            t1 = t;
            plane1 = i;
          }
        }
        // else point p2 was clipped, so adjust t2
        else if (t <= t2) {
          t2 = t;
          plane2 = i;
        }
        // If this happens, there's no line left
        if (t1 > t2) {
          // Allow for planes that are coincident or slightly inverted
          if (plane1 < 0 || plane2 < 0) {
            return;
          }
        }
      }
    }
  }
 
  function getValues(plane, t) {
    const x = [0, 0, 0];
    for (let count = 0; count < 2; count++) {
      for (let i = 0; i < 3; i++) {
        if (plane === 2 * i || plane === 2 * i + 1) {
          x[i] = bounds[plane];
        } else {
          x[i] = p1[i] * (1.0 - t) + p2[i] * t;
          if (x[i] < bounds[2 * i]) {
            x[i] = bounds[2 * i];
          }
          if (x[i] > bounds[2 * i + 1]) {
            x[i] = bounds[2 * i + 1];
          }
        }
      }
    }
    return x;
  }
 
  const x1 = getValues(plane1, t1);
  const x2 = getValues(plane2, t2);
 
  const outObject = { t1, t2, x1, x2 };
 
  // eslint-disable-next-line consistent-return
  return outObject;
}
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
export const STATIC = {};
 
// ----------------------------------------------------------------------------
// vtkBox methods
// ----------------------------------------------------------------------------
function vtkBox(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkBox');
 
  // TODO: replace with macro.setArray ?
  publicAPI.setBounds = (...bounds) => {
    let boundsArray = [];
 
    if (Array.isArray(bounds[0])) {
      boundsArray = bounds[0];
    } else E{
      for (let i = 0; i < bounds.length; i++) {
        boundsArray.push(bounds[i]);
      }
    }
 
    Iif (boundsArray.length !== 6) {
      console.log('vtkBox.setBounds', boundsArray, bounds);
      return;
    }
 
    vtkBoundingBox.setBounds(model.bbox, boundsArray);
  };
 
  publicAPI.getBounds = () => model.bbox;
 
  publicAPI.evaluateFunction = (x, y, z) => {
    const point = Array.isArray(x) ? x : [x, y, z];
 
    let diff;
    let dist;
    let t;
    let minDistance = -Number.MAX_VALUE;
    let distance = 0;
    const minPoint = vtkBoundingBox.getMinPoint(model.bbox);
    const maxPoint = vtkBoundingBox.getMaxPoint(model.bbox);
    let inside = 1;
    for (let i = 0; i < 3; i++) {
      diff = vtkBoundingBox.getLength(model.bbox, i);
      if (diff !== 0.0) {
        t = (point[i] - minPoint[i]) / diff;
        Iif (t < 0.0) {
          inside = 0;
          dist = minPoint[i] - point[i];
        } else if (t > 1.0) {
          inside = 0;
          dist = point[i] - maxPoint[i];
        } else {
          // want negative distance, we are inside
          if (t <= 0.5) {
            dist = minPoint[i] - point[i];
          } else {
            dist = point[i] - maxPoint[i];
          }
          if (dist > minDistance) {
            // remember, it's negative
            minDistance = dist;
          }
        } // end if inside
      } else E{
        dist = Math.abs(point[i] - minPoint[i]);
        if (dist > 0.0) {
          inside = 0;
        }
      }
      if (dist > 0.0) {
        distance += dist * dist;
      }
    } // end for i
    distance = Math.sqrt(distance);
    if (inside) {
      return minDistance;
    }
    return distance;
  };
 
  publicAPI.addBounds = (...bounds) => {
    let boundsArray = [];
 
    if (Array.isArray(bounds[0])) {
      boundsArray = bounds[0];
    } else {
      for (let i = 0; i < bounds.length; i++) {
        boundsArray.push(bounds[i]);
      }
    }
 
    if (boundsArray.length !== 6) {
      return;
    }
 
    vtkBoundingBox.addBounds(model.bbox, ...boundsArray);
    publicAPI.modified();
  };
 
  publicAPI.addBox = (other) => publicAPI.addBounds(other.getBounds());
  publicAPI.intersectWithLine = (p1, p2) =>
    intersectWithLine(model.bbox, p1, p2);
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  bbox: [...vtkBoundingBox.INIT_BOUNDS],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkImplicitFunction.extend(publicAPI, model, initialValues);
 
  vtkBox(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkBox');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, intersectWithLine, ...STATIC };