123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- import BoundingSphere from "../Core/BoundingSphere.js";
- import Cartesian3 from "../Core/Cartesian3.js";
- import Cartographic from "../Core/Cartographic.js";
- import Check from "../Core/Check.js";
- import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
- import defaultValue from "../Core/defaultValue.js";
- import Ellipsoid from "../Core/Ellipsoid.js";
- import GeometryInstance from "../Core/GeometryInstance.js";
- import IntersectionTests from "../Core/IntersectionTests.js";
- import Matrix4 from "../Core/Matrix4.js";
- import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
- import Plane from "../Core/Plane.js";
- import Ray from "../Core/Ray.js";
- import Rectangle from "../Core/Rectangle.js";
- import RectangleOutlineGeometry from "../Core/RectangleOutlineGeometry.js";
- import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
- import Primitive from "./Primitive.js";
- import SceneMode from "./SceneMode.js";
- function TileBoundingRegion(options) {
-
- Check.typeOf.object("options", options);
- Check.typeOf.object("options.rectangle", options.rectangle);
-
- this.rectangle = Rectangle.clone(options.rectangle);
- this.minimumHeight = defaultValue(options.minimumHeight, 0.0);
- this.maximumHeight = defaultValue(options.maximumHeight, 0.0);
-
- this.southwestCornerCartesian = new Cartesian3();
-
- this.northeastCornerCartesian = new Cartesian3();
-
- this.westNormal = new Cartesian3();
-
- this.southNormal = new Cartesian3();
-
- this.eastNormal = new Cartesian3();
-
- this.northNormal = new Cartesian3();
- var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
- computeBox(this, options.rectangle, ellipsoid);
- if (defaultValue(options.computeBoundingVolumes, true)) {
-
- this._orientedBoundingBox = OrientedBoundingBox.fromRectangle(
- this.rectangle,
- this.minimumHeight,
- this.maximumHeight,
- ellipsoid
- );
- this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
- this._orientedBoundingBox
- );
- }
- }
- Object.defineProperties(TileBoundingRegion.prototype, {
-
- boundingVolume: {
- get: function () {
- return this._orientedBoundingBox;
- },
- },
-
- boundingSphere: {
- get: function () {
- return this._boundingSphere;
- },
- },
- });
- var cartesian3Scratch = new Cartesian3();
- var cartesian3Scratch2 = new Cartesian3();
- var cartesian3Scratch3 = new Cartesian3();
- var eastWestNormalScratch = new Cartesian3();
- var westernMidpointScratch = new Cartesian3();
- var easternMidpointScratch = new Cartesian3();
- var cartographicScratch = new Cartographic();
- var planeScratch = new Plane(Cartesian3.UNIT_X, 0.0);
- var rayScratch = new Ray();
- function computeBox(tileBB, rectangle, ellipsoid) {
- ellipsoid.cartographicToCartesian(
- Rectangle.southwest(rectangle),
- tileBB.southwestCornerCartesian
- );
- ellipsoid.cartographicToCartesian(
- Rectangle.northeast(rectangle),
- tileBB.northeastCornerCartesian
- );
-
- cartographicScratch.longitude = rectangle.west;
- cartographicScratch.latitude = (rectangle.south + rectangle.north) * 0.5;
- cartographicScratch.height = 0.0;
- var westernMidpointCartesian = ellipsoid.cartographicToCartesian(
- cartographicScratch,
- westernMidpointScratch
- );
-
- var westNormal = Cartesian3.cross(
- westernMidpointCartesian,
- Cartesian3.UNIT_Z,
- cartesian3Scratch
- );
- Cartesian3.normalize(westNormal, tileBB.westNormal);
-
- cartographicScratch.longitude = rectangle.east;
- var easternMidpointCartesian = ellipsoid.cartographicToCartesian(
- cartographicScratch,
- easternMidpointScratch
- );
-
- var eastNormal = Cartesian3.cross(
- Cartesian3.UNIT_Z,
- easternMidpointCartesian,
- cartesian3Scratch
- );
- Cartesian3.normalize(eastNormal, tileBB.eastNormal);
-
- var westVector = Cartesian3.subtract(
- westernMidpointCartesian,
- easternMidpointCartesian,
- cartesian3Scratch
- );
- var eastWestNormal = Cartesian3.normalize(westVector, eastWestNormalScratch);
- var south = rectangle.south;
- var southSurfaceNormal;
- if (south > 0.0) {
-
- cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
- cartographicScratch.latitude = south;
- var southCenterCartesian = ellipsoid.cartographicToCartesian(
- cartographicScratch,
- rayScratch.origin
- );
- Cartesian3.clone(eastWestNormal, rayScratch.direction);
- var westPlane = Plane.fromPointNormal(
- tileBB.southwestCornerCartesian,
- tileBB.westNormal,
- planeScratch
- );
-
- IntersectionTests.rayPlane(
- rayScratch,
- westPlane,
- tileBB.southwestCornerCartesian
- );
- southSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
- southCenterCartesian,
- cartesian3Scratch2
- );
- } else {
- southSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
- Rectangle.southeast(rectangle),
- cartesian3Scratch2
- );
- }
- var southNormal = Cartesian3.cross(
- southSurfaceNormal,
- westVector,
- cartesian3Scratch3
- );
- Cartesian3.normalize(southNormal, tileBB.southNormal);
-
- var north = rectangle.north;
- var northSurfaceNormal;
- if (north < 0.0) {
-
- cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
- cartographicScratch.latitude = north;
- var northCenterCartesian = ellipsoid.cartographicToCartesian(
- cartographicScratch,
- rayScratch.origin
- );
- Cartesian3.negate(eastWestNormal, rayScratch.direction);
- var eastPlane = Plane.fromPointNormal(
- tileBB.northeastCornerCartesian,
- tileBB.eastNormal,
- planeScratch
- );
-
- IntersectionTests.rayPlane(
- rayScratch,
- eastPlane,
- tileBB.northeastCornerCartesian
- );
- northSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
- northCenterCartesian,
- cartesian3Scratch2
- );
- } else {
- northSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
- Rectangle.northwest(rectangle),
- cartesian3Scratch2
- );
- }
- var northNormal = Cartesian3.cross(
- westVector,
- northSurfaceNormal,
- cartesian3Scratch3
- );
- Cartesian3.normalize(northNormal, tileBB.northNormal);
- }
- var southwestCornerScratch = new Cartesian3();
- var northeastCornerScratch = new Cartesian3();
- var negativeUnitY = new Cartesian3(0.0, -1.0, 0.0);
- var negativeUnitZ = new Cartesian3(0.0, 0.0, -1.0);
- var vectorScratch = new Cartesian3();
- TileBoundingRegion.prototype.distanceToCamera = function (frameState) {
-
- Check.defined("frameState", frameState);
-
- var camera = frameState.camera;
- var cameraCartesianPosition = camera.positionWC;
- var cameraCartographicPosition = camera.positionCartographic;
- var result = 0.0;
- if (!Rectangle.contains(this.rectangle, cameraCartographicPosition)) {
- var southwestCornerCartesian = this.southwestCornerCartesian;
- var northeastCornerCartesian = this.northeastCornerCartesian;
- var westNormal = this.westNormal;
- var southNormal = this.southNormal;
- var eastNormal = this.eastNormal;
- var northNormal = this.northNormal;
- if (frameState.mode !== SceneMode.SCENE3D) {
- southwestCornerCartesian = frameState.mapProjection.project(
- Rectangle.southwest(this.rectangle),
- southwestCornerScratch
- );
- southwestCornerCartesian.z = southwestCornerCartesian.y;
- southwestCornerCartesian.y = southwestCornerCartesian.x;
- southwestCornerCartesian.x = 0.0;
- northeastCornerCartesian = frameState.mapProjection.project(
- Rectangle.northeast(this.rectangle),
- northeastCornerScratch
- );
- northeastCornerCartesian.z = northeastCornerCartesian.y;
- northeastCornerCartesian.y = northeastCornerCartesian.x;
- northeastCornerCartesian.x = 0.0;
- westNormal = negativeUnitY;
- eastNormal = Cartesian3.UNIT_Y;
- southNormal = negativeUnitZ;
- northNormal = Cartesian3.UNIT_Z;
- }
- var vectorFromSouthwestCorner = Cartesian3.subtract(
- cameraCartesianPosition,
- southwestCornerCartesian,
- vectorScratch
- );
- var distanceToWestPlane = Cartesian3.dot(
- vectorFromSouthwestCorner,
- westNormal
- );
- var distanceToSouthPlane = Cartesian3.dot(
- vectorFromSouthwestCorner,
- southNormal
- );
- var vectorFromNortheastCorner = Cartesian3.subtract(
- cameraCartesianPosition,
- northeastCornerCartesian,
- vectorScratch
- );
- var distanceToEastPlane = Cartesian3.dot(
- vectorFromNortheastCorner,
- eastNormal
- );
- var distanceToNorthPlane = Cartesian3.dot(
- vectorFromNortheastCorner,
- northNormal
- );
- if (distanceToWestPlane > 0.0) {
- result += distanceToWestPlane * distanceToWestPlane;
- } else if (distanceToEastPlane > 0.0) {
- result += distanceToEastPlane * distanceToEastPlane;
- }
- if (distanceToSouthPlane > 0.0) {
- result += distanceToSouthPlane * distanceToSouthPlane;
- } else if (distanceToNorthPlane > 0.0) {
- result += distanceToNorthPlane * distanceToNorthPlane;
- }
- }
- var cameraHeight;
- var minimumHeight;
- var maximumHeight;
- if (frameState.mode === SceneMode.SCENE3D) {
- cameraHeight = cameraCartographicPosition.height;
- minimumHeight = this.minimumHeight;
- maximumHeight = this.maximumHeight;
- } else {
- cameraHeight = cameraCartesianPosition.x;
- minimumHeight = 0.0;
- maximumHeight = 0.0;
- }
- if (cameraHeight > maximumHeight) {
- var distanceAboveTop = cameraHeight - maximumHeight;
- result += distanceAboveTop * distanceAboveTop;
- } else if (cameraHeight < minimumHeight) {
- var distanceBelowBottom = minimumHeight - cameraHeight;
- result += distanceBelowBottom * distanceBelowBottom;
- }
- return Math.sqrt(result);
- };
- TileBoundingRegion.prototype.intersectPlane = function (plane) {
-
- Check.defined("plane", plane);
-
- return this._orientedBoundingBox.intersectPlane(plane);
- };
- TileBoundingRegion.prototype.createDebugVolume = function (color) {
-
- Check.defined("color", color);
-
- var modelMatrix = new Matrix4.clone(Matrix4.IDENTITY);
- var geometry = new RectangleOutlineGeometry({
- rectangle: this.rectangle,
- height: this.minimumHeight,
- extrudedHeight: this.maximumHeight,
- });
- 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 TileBoundingRegion;
|