All files / engine/Source/Core FrustumOutlineGeometry.js

87.37% Statements 90/103
61.53% Branches 16/26
100% Functions 4/4
87.37% Lines 90/103

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                            1x 1x                             21x 20x 19x 18x     17x 17x 17x         17x       17x 11x 11x 6x         17x 17x 17x 17x 17x 17x           17x                         1x   5x 4x     3x   3x 3x   3x   3x 3x 3x           3x 3x 3x 3x 3x   3x     1x 1x 1x 1x                 1x   4x     3x   3x     3x 3x         3x                   3x 3x 3x         3x 3x   3x 3x                                                   1x 13x 13x 13x 13x 13x   13x 13x               13x                     13x 13x     13x 13x 26x 26x   26x 26x 26x 26x 26x 26x 26x 26x       13x 26x 26x   26x 26x 26x 26x 26x 26x 26x 26x     13x                
import BoundingSphere from "./BoundingSphere.js";
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import ComponentDatatype from "./ComponentDatatype.js";
import defined from "./defined.js";
import FrustumGeometry from "./FrustumGeometry.js";
import Geometry from "./Geometry.js";
import GeometryAttribute from "./GeometryAttribute.js";
import GeometryAttributes from "./GeometryAttributes.js";
import OrthographicFrustum from "./OrthographicFrustum.js";
import PerspectiveFrustum from "./PerspectiveFrustum.js";
import PrimitiveType from "./PrimitiveType.js";
import Quaternion from "./Quaternion.js";
 
const PERSPECTIVE = 0;
const ORTHOGRAPHIC = 1;
 
/**
 * A description of the outline of a frustum with the given the origin and orientation.
 *
 * @alias FrustumOutlineGeometry
 * @constructor
 *
 * @param {object} options Object with the following properties:
 * @param {PerspectiveFrustum|OrthographicFrustum} options.frustum The frustum.
 * @param {Cartesian3} options.origin The origin of the frustum.
 * @param {Quaternion} options.orientation The orientation of the frustum.
 */
function FrustumOutlineGeometry(options) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("options", options);
  Check.typeOf.object("options.frustum", options.frustum);
  Check.typeOf.object("options.origin", options.origin);
  Check.typeOf.object("options.orientation", options.orientation);
  //>>includeEnd('debug');
 
  const frustum = options.frustum;
  const orientation = options.orientation;
  const origin = options.origin;
 
  // This is private because it is used by DebugCameraPrimitive to draw a multi-frustum by
  // creating multiple FrustumOutlineGeometrys. This way the near plane of one frustum doesn't overlap
  // the far plane of another.
  const drawNearPlane = options._drawNearPlane ?? true;
 
  let frustumType;
  let frustumPackedLength;
  if (frustum instanceof PerspectiveFrustum) {
    frustumType = PERSPECTIVE;
    frustumPackedLength = PerspectiveFrustum.packedLength;
  } else Iif (frustum instanceof OrthographicFrustum) {
    frustumType = ORTHOGRAPHIC;
    frustumPackedLength = OrthographicFrustum.packedLength;
  }
 
  this._frustumType = frustumType;
  this._frustum = frustum.clone();
  this._origin = Cartesian3.clone(origin);
  this._orientation = Quaternion.clone(orientation);
  this._drawNearPlane = drawNearPlane;
  this._workerName = "createFrustumOutlineGeometry";
 
  /**
   * The number of elements used to pack the object into an array.
   * @type {number}
   */
  this.packedLength =
    2 + frustumPackedLength + Cartesian3.packedLength + Quaternion.packedLength;
}
 
/**
 * Stores the provided instance into the provided array.
 *
 * @param {FrustumOutlineGeometry} value The value to pack.
 * @param {number[]} array The array to pack into.
 * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
 *
 * @returns {number[]} The array that was packed into
 */
FrustumOutlineGeometry.pack = function (value, array, startingIndex) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("value", value);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  startingIndex = startingIndex ?? 0;
 
  const frustumType = value._frustumType;
  const frustum = value._frustum;
 
  array[startingIndex++] = frustumType;
 
  if (frustumType === PERSPECTIVE) {
    PerspectiveFrustum.pack(frustum, array, startingIndex);
    startingIndex += PerspectiveFrustum.packedLength;
  } else E{
    OrthographicFrustum.pack(frustum, array, startingIndex);
    startingIndex += OrthographicFrustum.packedLength;
  }
 
  Cartesian3.pack(value._origin, array, startingIndex);
  startingIndex += Cartesian3.packedLength;
  Quaternion.pack(value._orientation, array, startingIndex);
  startingIndex += Quaternion.packedLength;
  array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
 
  return array;
};
 
const scratchPackPerspective = new PerspectiveFrustum();
const scratchPackOrthographic = new OrthographicFrustum();
const scratchPackQuaternion = new Quaternion();
const scratchPackorigin = new Cartesian3();
 
/**
 * Retrieves an instance from a packed array.
 *
 * @param {number[]} array The packed array.
 * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
 * @param {FrustumOutlineGeometry} [result] The object into which to store the result.
 */
FrustumOutlineGeometry.unpack = function (array, startingIndex, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  startingIndex = startingIndex ?? 0;
 
  const frustumType = array[startingIndex++];
 
  let frustum;
  if (frustumType === PERSPECTIVE) {
    frustum = PerspectiveFrustum.unpack(
      array,
      startingIndex,
      scratchPackPerspective,
    );
    startingIndex += PerspectiveFrustum.packedLength;
  } else E{
    frustum = OrthographicFrustum.unpack(
      array,
      startingIndex,
      scratchPackOrthographic,
    );
    startingIndex += OrthographicFrustum.packedLength;
  }
 
  const origin = Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  startingIndex += Cartesian3.packedLength;
  const orientation = Quaternion.unpack(
    array,
    startingIndex,
    scratchPackQuaternion,
  );
  startingIndex += Quaternion.packedLength;
  const drawNearPlane = array[startingIndex] === 1.0;
 
  Eif (!defined(result)) {
    return new FrustumOutlineGeometry({
      frustum: frustum,
      origin: origin,
      orientation: orientation,
      _drawNearPlane: drawNearPlane,
    });
  }
 
  const frustumResult =
    frustumType === result._frustumType ? result._frustum : undefined;
  result._frustum = frustum.clone(frustumResult);
 
  result._frustumType = frustumType;
  result._origin = Cartesian3.clone(origin, result._origin);
  result._orientation = Quaternion.clone(orientation, result._orientation);
  result._drawNearPlane = drawNearPlane;
 
  return result;
};
 
/**
 * Computes the geometric representation of a frustum outline, including its vertices, indices, and a bounding sphere.
 *
 * @param {FrustumOutlineGeometry} frustumGeometry A description of the frustum.
 * @returns {Geometry|undefined} The computed vertices and indices.
 */
FrustumOutlineGeometry.createGeometry = function (frustumGeometry) {
  const frustumType = frustumGeometry._frustumType;
  const frustum = frustumGeometry._frustum;
  const origin = frustumGeometry._origin;
  const orientation = frustumGeometry._orientation;
  const drawNearPlane = frustumGeometry._drawNearPlane;
 
  const positions = new Float64Array(3 * 4 * 2);
  FrustumGeometry._computeNearFarPlanes(
    origin,
    orientation,
    frustumType,
    frustum,
    positions,
  );
 
  const attributes = new GeometryAttributes({
    position: new GeometryAttribute({
      componentDatatype: ComponentDatatype.DOUBLE,
      componentsPerAttribute: 3,
      values: positions,
    }),
  });
 
  let offset;
  let index;
 
  const numberOfPlanes = drawNearPlane ? 2 : 1;
  const indices = new Uint16Array(8 * (numberOfPlanes + 1));
 
  // Build the near/far planes
  let i = drawNearPlane ? 0 : 1;
  for (; i < 2; ++i) {
    offset = drawNearPlane ? i * 8 : 0;
    index = i * 4;
 
    indices[offset] = index;
    indices[offset + 1] = index + 1;
    indices[offset + 2] = index + 1;
    indices[offset + 3] = index + 2;
    indices[offset + 4] = index + 2;
    indices[offset + 5] = index + 3;
    indices[offset + 6] = index + 3;
    indices[offset + 7] = index;
  }
 
  // Build the sides of the frustums
  for (i = 0; i < 2; ++i) {
    offset = (numberOfPlanes + i) * 8;
    index = i * 4;
 
    indices[offset] = index;
    indices[offset + 1] = index + 4;
    indices[offset + 2] = index + 1;
    indices[offset + 3] = index + 5;
    indices[offset + 4] = index + 2;
    indices[offset + 5] = index + 6;
    indices[offset + 6] = index + 3;
    indices[offset + 7] = index + 7;
  }
 
  return new Geometry({
    attributes: attributes,
    indices: indices,
    primitiveType: PrimitiveType.LINES,
    boundingSphere: BoundingSphere.fromVertices(positions),
  });
};
export default FrustumOutlineGeometry;