All files / engine/Source/Scene TileOrientedBoundingBox.js

100% Statements 65/65
96.96% Branches 32/33
100% Functions 10/10
100% Lines 65/65

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                          1x 1x 1x 1x     28x 28x 28x               6x 6x             6x       1795x 1795x 1795x   1795x 1795x 1795x   1795x 1769x   26x 4x 4x 4x 4x   22x 2x 20x 12x 8x 2x 6x 2x 2x 4x 2x 2x 2x 2x 2x     22x 22x 22x   22x                               1776x 1776x 1776x         1x                     5096x                         4017x                     1x   3360x   3359x                           1x   7332x   7331x                     1x 19x 19x 19x 19x                       1x   5x     4x         4x       4x                 4x                    
import BoundingSphere from "../Core/BoundingSphere.js";
import BoxOutlineGeometry from "../Core/BoxOutlineGeometry.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Check from "../Core/Check.js";
import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
import GeometryInstance from "../Core/GeometryInstance.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import CesiumMath from "../Core/Math.js";
import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
import Primitive from "./Primitive.js";
 
const scratchU = new Cartesian3();
const scratchV = new Cartesian3();
const scratchW = new Cartesian3();
const scratchCartesian = new Cartesian3();
 
function computeMissingVector(a, b, result) {
  result = Cartesian3.cross(a, b, result);
  const magnitude = Cartesian3.magnitude(result);
  return Cartesian3.multiplyByScalar(
    result,
    CesiumMath.EPSILON7 / magnitude,
    result,
  );
}
 
function findOrthogonalVector(a, result) {
  const temp = Cartesian3.normalize(a, scratchCartesian);
  const b = Cartesian3.equalsEpsilon(
    temp,
    Cartesian3.UNIT_X,
    CesiumMath.EPSILON6,
  )
    ? Cartesian3.UNIT_Y
    : Cartesian3.UNIT_X;
  return computeMissingVector(a, b, result);
}
 
function checkHalfAxes(halfAxes) {
  let u = Matrix3.getColumn(halfAxes, 0, scratchU);
  let v = Matrix3.getColumn(halfAxes, 1, scratchV);
  let w = Matrix3.getColumn(halfAxes, 2, scratchW);
 
  const uZero = Cartesian3.equals(u, Cartesian3.ZERO);
  const vZero = Cartesian3.equals(v, Cartesian3.ZERO);
  const wZero = Cartesian3.equals(w, Cartesian3.ZERO);
 
  if (!uZero && !vZero && !wZero) {
    return halfAxes;
  }
  if (uZero && vZero && wZero) {
    halfAxes[0] = CesiumMath.EPSILON7;
    halfAxes[4] = CesiumMath.EPSILON7;
    halfAxes[8] = CesiumMath.EPSILON7;
    return halfAxes;
  }
  if (uZero && !vZero && !wZero) {
    u = computeMissingVector(v, w, u);
  } else if (!uZero && vZero && !wZero) {
    v = computeMissingVector(u, w, v);
  } else if (!uZero && !vZero && wZero) {
    w = computeMissingVector(v, u, w);
  } else if (!uZero) {
    v = findOrthogonalVector(u, v);
    w = computeMissingVector(v, u, w);
  } else if (!vZero) {
    u = findOrthogonalVector(v, u);
    w = computeMissingVector(v, u, w);
  } else Eif (!wZero) {
    u = findOrthogonalVector(w, u);
    v = computeMissingVector(w, u, v);
  }
 
  Matrix3.setColumn(halfAxes, 0, u, halfAxes);
  Matrix3.setColumn(halfAxes, 1, v, halfAxes);
  Matrix3.setColumn(halfAxes, 2, w, halfAxes);
 
  return halfAxes;
}
 
/**
 * A tile bounding volume specified as an oriented bounding box.
 * @alias TileOrientedBoundingBox
 * @constructor
 *
 * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the box.
 * @param {Matrix3} [halfAxes=Matrix3.ZERO] The three orthogonal half-axes of the bounding box.
 *                                          Equivalently, the transformation matrix, to rotate and scale a 2x2x2
 *                                          cube centered at the origin.
 *
 * @private
 */
function TileOrientedBoundingBox(center, halfAxes) {
  halfAxes = checkHalfAxes(halfAxes);
  this._orientedBoundingBox = new OrientedBoundingBox(center, halfAxes);
  this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
    this._orientedBoundingBox,
  );
}
 
Object.defineProperties(TileOrientedBoundingBox.prototype, {
  /**
   * The underlying bounding volume.
   *
   * @memberof TileOrientedBoundingBox.prototype
   *
   * @type {OrientedBoundingBox}
   * @readonly
   */
  boundingVolume: {
    get: function () {
      return this._orientedBoundingBox;
    },
  },
  /**
   * The underlying bounding sphere.
   *
   * @memberof TileOrientedBoundingBox.prototype
   *
   * @type {BoundingSphere}
   * @readonly
   */
  boundingSphere: {
    get: function () {
      return this._boundingSphere;
    },
  },
});
 
/**
 * Computes the distance between this bounding box and the camera attached to frameState.
 *
 * @param {FrameState} frameState The frameState to which the camera is attached.
 * @returns {number} The distance between the camera and the bounding box in meters. Returns 0 if the camera is inside the bounding volume.
 */
TileOrientedBoundingBox.prototype.distanceToCamera = function (frameState) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("frameState", frameState);
  //>>includeEnd('debug');
  return Math.sqrt(
    this._orientedBoundingBox.distanceSquaredTo(frameState.camera.positionWC),
  );
};
 
/**
 * Determines which side of a plane this box is located.
 *
 * @param {Plane} plane The plane to test against.
 * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
 *                      the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
 *                      on the opposite side, and {@link Intersect.INTERSECTING} if the box
 *                      intersects the plane.
 */
TileOrientedBoundingBox.prototype.intersectPlane = function (plane) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("plane", plane);
  //>>includeEnd('debug');
  return this._orientedBoundingBox.intersectPlane(plane);
};
 
/**
 * Update the bounding box after the tile is transformed.
 *
 * @param {Cartesian3} center The center of the box.
 * @param {Matrix3} halfAxes The three orthogonal half-axes of the bounding box.
 *                           Equivalently, the transformation matrix, to rotate and scale a 2x2x2
 *                           cube centered at the origin.
 */
TileOrientedBoundingBox.prototype.update = function (center, halfAxes) {
  Cartesian3.clone(center, this._orientedBoundingBox.center);
  halfAxes = checkHalfAxes(halfAxes);
  Matrix3.clone(halfAxes, this._orientedBoundingBox.halfAxes);
  BoundingSphere.fromOrientedBoundingBox(
    this._orientedBoundingBox,
    this._boundingSphere,
  );
};
 
/**
 * Creates a debug primitive that shows the outline of the box.
 *
 * @param {Color} color The desired color of the primitive's mesh
 * @return {Primitive}
 */
TileOrientedBoundingBox.prototype.createDebugVolume = function (color) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("color", color);
  //>>includeEnd('debug');
 
  const geometry = new BoxOutlineGeometry({
    // Make a 2x2x2 cube
    minimum: new Cartesian3(-1.0, -1.0, -1.0),
    maximum: new Cartesian3(1.0, 1.0, 1.0),
  });
  const modelMatrix = Matrix4.fromRotationTranslation(
    this.boundingVolume.halfAxes,
    this.boundingVolume.center,
  );
  const instance = new GeometryInstance({
    geometry: geometry,
    id: "outline",
    modelMatrix: modelMatrix,
    attributes: {
      color: ColorGeometryInstanceAttribute.fromColor(color),
    },
  });
 
  return new Primitive({
    geometryInstances: instance,
    appearance: new PerInstanceColorAppearance({
      translucent: false,
      flat: true,
    }),
    asynchronous: false,
  });
};
export default TileOrientedBoundingBox;