123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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";
- var scratchU = new Cartesian3();
- var scratchV = new Cartesian3();
- var scratchW = new Cartesian3();
- var scratchCartesian = new Cartesian3();
- function computeMissingVector(a, b, result) {
- result = Cartesian3.cross(a, b, result);
- var magnitude = Cartesian3.magnitude(result);
- return Cartesian3.multiplyByScalar(
- result,
- CesiumMath.EPSILON7 / magnitude,
- result
- );
- }
- function findOrthogonalVector(a, result) {
- var temp = Cartesian3.normalize(a, scratchCartesian);
- var b = Cartesian3.equalsEpsilon(temp, Cartesian3.UNIT_X, CesiumMath.EPSILON6)
- ? Cartesian3.UNIT_Y
- : Cartesian3.UNIT_X;
- return computeMissingVector(a, b, result);
- }
- function checkHalfAxes(halfAxes) {
- var u = Matrix3.getColumn(halfAxes, 0, scratchU);
- var v = Matrix3.getColumn(halfAxes, 1, scratchV);
- var w = Matrix3.getColumn(halfAxes, 2, scratchW);
- var uZero = Cartesian3.equals(u, Cartesian3.ZERO);
- var vZero = Cartesian3.equals(v, Cartesian3.ZERO);
- var 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 if (!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;
- }
- function TileOrientedBoundingBox(center, halfAxes) {
- halfAxes = checkHalfAxes(halfAxes);
- this._orientedBoundingBox = new OrientedBoundingBox(center, halfAxes);
- this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
- this._orientedBoundingBox
- );
- }
- Object.defineProperties(TileOrientedBoundingBox.prototype, {
-
- boundingVolume: {
- get: function () {
- return this._orientedBoundingBox;
- },
- },
-
- boundingSphere: {
- get: function () {
- return this._boundingSphere;
- },
- },
- });
- TileOrientedBoundingBox.prototype.distanceToCamera = function (frameState) {
-
- Check.defined("frameState", frameState);
-
- return Math.sqrt(
- this._orientedBoundingBox.distanceSquaredTo(frameState.camera.positionWC)
- );
- };
- TileOrientedBoundingBox.prototype.intersectPlane = function (plane) {
-
- Check.defined("plane", plane);
-
- return this._orientedBoundingBox.intersectPlane(plane);
- };
- 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
- );
- };
- TileOrientedBoundingBox.prototype.createDebugVolume = function (color) {
-
- Check.defined("color", color);
-
- var geometry = new BoxOutlineGeometry({
-
- minimum: new Cartesian3(-1.0, -1.0, -1.0),
- maximum: new Cartesian3(1.0, 1.0, 1.0),
- });
- var modelMatrix = Matrix4.fromRotationTranslation(
- this.boundingVolume.halfAxes,
- this.boundingVolume.center
- );
- var 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;
|