1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465 |
- import Cartesian3 from "./Cartesian3.js";
- import Cartographic from "./Cartographic.js";
- import Check from "./Check.js";
- import defaultValue from "./defaultValue.js";
- import defined from "./defined.js";
- import Ellipsoid from "./Ellipsoid.js";
- import GeographicProjection from "./GeographicProjection.js";
- import Intersect from "./Intersect.js";
- import Interval from "./Interval.js";
- import CesiumMath from "./Math.js";
- import Matrix3 from "./Matrix3.js";
- import Matrix4 from "./Matrix4.js";
- import Rectangle from "./Rectangle.js";
- function BoundingSphere(center, radius) {
-
- this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
-
- this.radius = defaultValue(radius, 0.0);
- }
- var fromPointsXMin = new Cartesian3();
- var fromPointsYMin = new Cartesian3();
- var fromPointsZMin = new Cartesian3();
- var fromPointsXMax = new Cartesian3();
- var fromPointsYMax = new Cartesian3();
- var fromPointsZMax = new Cartesian3();
- var fromPointsCurrentPos = new Cartesian3();
- var fromPointsScratch = new Cartesian3();
- var fromPointsRitterCenter = new Cartesian3();
- var fromPointsMinBoxPt = new Cartesian3();
- var fromPointsMaxBoxPt = new Cartesian3();
- var fromPointsNaiveCenterScratch = new Cartesian3();
- var volumeConstant = (4.0 / 3.0) * CesiumMath.PI;
- BoundingSphere.fromPoints = function (positions, result) {
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (!defined(positions) || positions.length === 0) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- var currentPos = Cartesian3.clone(positions[0], fromPointsCurrentPos);
- var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
- var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
- var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
- var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
- var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
- var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
- var numPositions = positions.length;
- var i;
- for (i = 1; i < numPositions; i++) {
- Cartesian3.clone(positions[i], currentPos);
- var x = currentPos.x;
- var y = currentPos.y;
- var z = currentPos.z;
-
- if (x < xMin.x) {
- Cartesian3.clone(currentPos, xMin);
- }
- if (x > xMax.x) {
- Cartesian3.clone(currentPos, xMax);
- }
- if (y < yMin.y) {
- Cartesian3.clone(currentPos, yMin);
- }
- if (y > yMax.y) {
- Cartesian3.clone(currentPos, yMax);
- }
- if (z < zMin.z) {
- Cartesian3.clone(currentPos, zMin);
- }
- if (z > zMax.z) {
- Cartesian3.clone(currentPos, zMax);
- }
- }
-
- var xSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(xMax, xMin, fromPointsScratch)
- );
- var ySpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(yMax, yMin, fromPointsScratch)
- );
- var zSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(zMax, zMin, fromPointsScratch)
- );
-
- var diameter1 = xMin;
- var diameter2 = xMax;
- var maxSpan = xSpan;
- if (ySpan > maxSpan) {
- maxSpan = ySpan;
- diameter1 = yMin;
- diameter2 = yMax;
- }
- if (zSpan > maxSpan) {
- maxSpan = zSpan;
- diameter1 = zMin;
- diameter2 = zMax;
- }
-
- var ritterCenter = fromPointsRitterCenter;
- ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
- ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
- ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
-
- var radiusSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
- );
- var ritterRadius = Math.sqrt(radiusSquared);
-
- var minBoxPt = fromPointsMinBoxPt;
- minBoxPt.x = xMin.x;
- minBoxPt.y = yMin.y;
- minBoxPt.z = zMin.z;
- var maxBoxPt = fromPointsMaxBoxPt;
- maxBoxPt.x = xMax.x;
- maxBoxPt.y = yMax.y;
- maxBoxPt.z = zMax.z;
- var naiveCenter = Cartesian3.midpoint(
- minBoxPt,
- maxBoxPt,
- fromPointsNaiveCenterScratch
- );
-
- var naiveRadius = 0;
- for (i = 0; i < numPositions; i++) {
- Cartesian3.clone(positions[i], currentPos);
-
- var r = Cartesian3.magnitude(
- Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
- );
- if (r > naiveRadius) {
- naiveRadius = r;
- }
-
- var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
- );
- if (oldCenterToPointSquared > radiusSquared) {
- var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
-
- ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
- radiusSquared = ritterRadius * ritterRadius;
-
- var oldToNew = oldCenterToPoint - ritterRadius;
- ritterCenter.x =
- (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
- oldCenterToPoint;
- ritterCenter.y =
- (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
- oldCenterToPoint;
- ritterCenter.z =
- (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
- oldCenterToPoint;
- }
- }
- if (ritterRadius < naiveRadius) {
- Cartesian3.clone(ritterCenter, result.center);
- result.radius = ritterRadius;
- } else {
- Cartesian3.clone(naiveCenter, result.center);
- result.radius = naiveRadius;
- }
- return result;
- };
- var defaultProjection = new GeographicProjection();
- var fromRectangle2DLowerLeft = new Cartesian3();
- var fromRectangle2DUpperRight = new Cartesian3();
- var fromRectangle2DSouthwest = new Cartographic();
- var fromRectangle2DNortheast = new Cartographic();
- BoundingSphere.fromRectangle2D = function (rectangle, projection, result) {
- return BoundingSphere.fromRectangleWithHeights2D(
- rectangle,
- projection,
- 0.0,
- 0.0,
- result
- );
- };
- BoundingSphere.fromRectangleWithHeights2D = function (
- rectangle,
- projection,
- minimumHeight,
- maximumHeight,
- result
- ) {
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (!defined(rectangle)) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- projection = defaultValue(projection, defaultProjection);
- Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
- fromRectangle2DSouthwest.height = minimumHeight;
- Rectangle.northeast(rectangle, fromRectangle2DNortheast);
- fromRectangle2DNortheast.height = maximumHeight;
- var lowerLeft = projection.project(
- fromRectangle2DSouthwest,
- fromRectangle2DLowerLeft
- );
- var upperRight = projection.project(
- fromRectangle2DNortheast,
- fromRectangle2DUpperRight
- );
- var width = upperRight.x - lowerLeft.x;
- var height = upperRight.y - lowerLeft.y;
- var elevation = upperRight.z - lowerLeft.z;
- result.radius =
- Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
- var center = result.center;
- center.x = lowerLeft.x + width * 0.5;
- center.y = lowerLeft.y + height * 0.5;
- center.z = lowerLeft.z + elevation * 0.5;
- return result;
- };
- var fromRectangle3DScratch = [];
- BoundingSphere.fromRectangle3D = function (
- rectangle,
- ellipsoid,
- surfaceHeight,
- result
- ) {
- ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
- surfaceHeight = defaultValue(surfaceHeight, 0.0);
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (!defined(rectangle)) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- var positions = Rectangle.subsample(
- rectangle,
- ellipsoid,
- surfaceHeight,
- fromRectangle3DScratch
- );
- return BoundingSphere.fromPoints(positions, result);
- };
- BoundingSphere.fromVertices = function (positions, center, stride, result) {
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (!defined(positions) || positions.length === 0) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- center = defaultValue(center, Cartesian3.ZERO);
- stride = defaultValue(stride, 3);
-
- Check.typeOf.number.greaterThanOrEquals("stride", stride, 3);
-
- var currentPos = fromPointsCurrentPos;
- currentPos.x = positions[0] + center.x;
- currentPos.y = positions[1] + center.y;
- currentPos.z = positions[2] + center.z;
- var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
- var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
- var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
- var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
- var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
- var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
- var numElements = positions.length;
- var i;
- for (i = 0; i < numElements; i += stride) {
- var x = positions[i] + center.x;
- var y = positions[i + 1] + center.y;
- var z = positions[i + 2] + center.z;
- currentPos.x = x;
- currentPos.y = y;
- currentPos.z = z;
-
- if (x < xMin.x) {
- Cartesian3.clone(currentPos, xMin);
- }
- if (x > xMax.x) {
- Cartesian3.clone(currentPos, xMax);
- }
- if (y < yMin.y) {
- Cartesian3.clone(currentPos, yMin);
- }
- if (y > yMax.y) {
- Cartesian3.clone(currentPos, yMax);
- }
- if (z < zMin.z) {
- Cartesian3.clone(currentPos, zMin);
- }
- if (z > zMax.z) {
- Cartesian3.clone(currentPos, zMax);
- }
- }
-
- var xSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(xMax, xMin, fromPointsScratch)
- );
- var ySpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(yMax, yMin, fromPointsScratch)
- );
- var zSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(zMax, zMin, fromPointsScratch)
- );
-
- var diameter1 = xMin;
- var diameter2 = xMax;
- var maxSpan = xSpan;
- if (ySpan > maxSpan) {
- maxSpan = ySpan;
- diameter1 = yMin;
- diameter2 = yMax;
- }
- if (zSpan > maxSpan) {
- maxSpan = zSpan;
- diameter1 = zMin;
- diameter2 = zMax;
- }
-
- var ritterCenter = fromPointsRitterCenter;
- ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
- ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
- ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
-
- var radiusSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
- );
- var ritterRadius = Math.sqrt(radiusSquared);
-
- var minBoxPt = fromPointsMinBoxPt;
- minBoxPt.x = xMin.x;
- minBoxPt.y = yMin.y;
- minBoxPt.z = zMin.z;
- var maxBoxPt = fromPointsMaxBoxPt;
- maxBoxPt.x = xMax.x;
- maxBoxPt.y = yMax.y;
- maxBoxPt.z = zMax.z;
- var naiveCenter = Cartesian3.midpoint(
- minBoxPt,
- maxBoxPt,
- fromPointsNaiveCenterScratch
- );
-
- var naiveRadius = 0;
- for (i = 0; i < numElements; i += stride) {
- currentPos.x = positions[i] + center.x;
- currentPos.y = positions[i + 1] + center.y;
- currentPos.z = positions[i + 2] + center.z;
-
- var r = Cartesian3.magnitude(
- Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
- );
- if (r > naiveRadius) {
- naiveRadius = r;
- }
-
- var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
- );
- if (oldCenterToPointSquared > radiusSquared) {
- var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
-
- ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
- radiusSquared = ritterRadius * ritterRadius;
-
- var oldToNew = oldCenterToPoint - ritterRadius;
- ritterCenter.x =
- (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
- oldCenterToPoint;
- ritterCenter.y =
- (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
- oldCenterToPoint;
- ritterCenter.z =
- (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
- oldCenterToPoint;
- }
- }
- if (ritterRadius < naiveRadius) {
- Cartesian3.clone(ritterCenter, result.center);
- result.radius = ritterRadius;
- } else {
- Cartesian3.clone(naiveCenter, result.center);
- result.radius = naiveRadius;
- }
- return result;
- };
- BoundingSphere.fromEncodedCartesianVertices = function (
- positionsHigh,
- positionsLow,
- result
- ) {
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (
- !defined(positionsHigh) ||
- !defined(positionsLow) ||
- positionsHigh.length !== positionsLow.length ||
- positionsHigh.length === 0
- ) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- var currentPos = fromPointsCurrentPos;
- currentPos.x = positionsHigh[0] + positionsLow[0];
- currentPos.y = positionsHigh[1] + positionsLow[1];
- currentPos.z = positionsHigh[2] + positionsLow[2];
- var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
- var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
- var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
- var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
- var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
- var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
- var numElements = positionsHigh.length;
- var i;
- for (i = 0; i < numElements; i += 3) {
- var x = positionsHigh[i] + positionsLow[i];
- var y = positionsHigh[i + 1] + positionsLow[i + 1];
- var z = positionsHigh[i + 2] + positionsLow[i + 2];
- currentPos.x = x;
- currentPos.y = y;
- currentPos.z = z;
-
- if (x < xMin.x) {
- Cartesian3.clone(currentPos, xMin);
- }
- if (x > xMax.x) {
- Cartesian3.clone(currentPos, xMax);
- }
- if (y < yMin.y) {
- Cartesian3.clone(currentPos, yMin);
- }
- if (y > yMax.y) {
- Cartesian3.clone(currentPos, yMax);
- }
- if (z < zMin.z) {
- Cartesian3.clone(currentPos, zMin);
- }
- if (z > zMax.z) {
- Cartesian3.clone(currentPos, zMax);
- }
- }
-
- var xSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(xMax, xMin, fromPointsScratch)
- );
- var ySpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(yMax, yMin, fromPointsScratch)
- );
- var zSpan = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(zMax, zMin, fromPointsScratch)
- );
-
- var diameter1 = xMin;
- var diameter2 = xMax;
- var maxSpan = xSpan;
- if (ySpan > maxSpan) {
- maxSpan = ySpan;
- diameter1 = yMin;
- diameter2 = yMax;
- }
- if (zSpan > maxSpan) {
- maxSpan = zSpan;
- diameter1 = zMin;
- diameter2 = zMax;
- }
-
- var ritterCenter = fromPointsRitterCenter;
- ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
- ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
- ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
-
- var radiusSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
- );
- var ritterRadius = Math.sqrt(radiusSquared);
-
- var minBoxPt = fromPointsMinBoxPt;
- minBoxPt.x = xMin.x;
- minBoxPt.y = yMin.y;
- minBoxPt.z = zMin.z;
- var maxBoxPt = fromPointsMaxBoxPt;
- maxBoxPt.x = xMax.x;
- maxBoxPt.y = yMax.y;
- maxBoxPt.z = zMax.z;
- var naiveCenter = Cartesian3.midpoint(
- minBoxPt,
- maxBoxPt,
- fromPointsNaiveCenterScratch
- );
-
- var naiveRadius = 0;
- for (i = 0; i < numElements; i += 3) {
- currentPos.x = positionsHigh[i] + positionsLow[i];
- currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
- currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
-
- var r = Cartesian3.magnitude(
- Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
- );
- if (r > naiveRadius) {
- naiveRadius = r;
- }
-
- var oldCenterToPointSquared = Cartesian3.magnitudeSquared(
- Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
- );
- if (oldCenterToPointSquared > radiusSquared) {
- var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
-
- ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
- radiusSquared = ritterRadius * ritterRadius;
-
- var oldToNew = oldCenterToPoint - ritterRadius;
- ritterCenter.x =
- (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
- oldCenterToPoint;
- ritterCenter.y =
- (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
- oldCenterToPoint;
- ritterCenter.z =
- (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
- oldCenterToPoint;
- }
- }
- if (ritterRadius < naiveRadius) {
- Cartesian3.clone(ritterCenter, result.center);
- result.radius = ritterRadius;
- } else {
- Cartesian3.clone(naiveCenter, result.center);
- result.radius = naiveRadius;
- }
- return result;
- };
- BoundingSphere.fromCornerPoints = function (corner, oppositeCorner, result) {
-
- Check.typeOf.object("corner", corner);
- Check.typeOf.object("oppositeCorner", oppositeCorner);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- var center = Cartesian3.midpoint(corner, oppositeCorner, result.center);
- result.radius = Cartesian3.distance(center, oppositeCorner);
- return result;
- };
- BoundingSphere.fromEllipsoid = function (ellipsoid, result) {
-
- Check.typeOf.object("ellipsoid", ellipsoid);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = ellipsoid.maximumRadius;
- return result;
- };
- var fromBoundingSpheresScratch = new Cartesian3();
- BoundingSphere.fromBoundingSpheres = function (boundingSpheres, result) {
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- if (!defined(boundingSpheres) || boundingSpheres.length === 0) {
- result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
- result.radius = 0.0;
- return result;
- }
- var length = boundingSpheres.length;
- if (length === 1) {
- return BoundingSphere.clone(boundingSpheres[0], result);
- }
- if (length === 2) {
- return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
- }
- var positions = [];
- var i;
- for (i = 0; i < length; i++) {
- positions.push(boundingSpheres[i].center);
- }
- result = BoundingSphere.fromPoints(positions, result);
- var center = result.center;
- var radius = result.radius;
- for (i = 0; i < length; i++) {
- var tmp = boundingSpheres[i];
- radius = Math.max(
- radius,
- Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) +
- tmp.radius
- );
- }
- result.radius = radius;
- return result;
- };
- var fromOrientedBoundingBoxScratchU = new Cartesian3();
- var fromOrientedBoundingBoxScratchV = new Cartesian3();
- var fromOrientedBoundingBoxScratchW = new Cartesian3();
- BoundingSphere.fromOrientedBoundingBox = function (
- orientedBoundingBox,
- result
- ) {
-
- Check.defined("orientedBoundingBox", orientedBoundingBox);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- var halfAxes = orientedBoundingBox.halfAxes;
- var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
- var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
- var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
- Cartesian3.add(u, v, u);
- Cartesian3.add(u, w, u);
- result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
- result.radius = Cartesian3.magnitude(u);
- return result;
- };
- BoundingSphere.clone = function (sphere, result) {
- if (!defined(sphere)) {
- return undefined;
- }
- if (!defined(result)) {
- return new BoundingSphere(sphere.center, sphere.radius);
- }
- result.center = Cartesian3.clone(sphere.center, result.center);
- result.radius = sphere.radius;
- return result;
- };
- BoundingSphere.packedLength = 4;
- BoundingSphere.pack = function (value, array, startingIndex) {
-
- Check.typeOf.object("value", value);
- Check.defined("array", array);
-
- startingIndex = defaultValue(startingIndex, 0);
- var center = value.center;
- array[startingIndex++] = center.x;
- array[startingIndex++] = center.y;
- array[startingIndex++] = center.z;
- array[startingIndex] = value.radius;
- return array;
- };
- BoundingSphere.unpack = function (array, startingIndex, result) {
-
- Check.defined("array", array);
-
- startingIndex = defaultValue(startingIndex, 0);
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- var center = result.center;
- center.x = array[startingIndex++];
- center.y = array[startingIndex++];
- center.z = array[startingIndex++];
- result.radius = array[startingIndex];
- return result;
- };
- var unionScratch = new Cartesian3();
- var unionScratchCenter = new Cartesian3();
- BoundingSphere.union = function (left, right, result) {
-
- Check.typeOf.object("left", left);
- Check.typeOf.object("right", right);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- var leftCenter = left.center;
- var leftRadius = left.radius;
- var rightCenter = right.center;
- var rightRadius = right.radius;
- var toRightCenter = Cartesian3.subtract(
- rightCenter,
- leftCenter,
- unionScratch
- );
- var centerSeparation = Cartesian3.magnitude(toRightCenter);
- if (leftRadius >= centerSeparation + rightRadius) {
-
- left.clone(result);
- return result;
- }
- if (rightRadius >= centerSeparation + leftRadius) {
-
- right.clone(result);
- return result;
- }
-
- var halfDistanceBetweenTangentPoints =
- (leftRadius + centerSeparation + rightRadius) * 0.5;
-
- var center = Cartesian3.multiplyByScalar(
- toRightCenter,
- (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation,
- unionScratchCenter
- );
- Cartesian3.add(center, leftCenter, center);
- Cartesian3.clone(center, result.center);
- result.radius = halfDistanceBetweenTangentPoints;
- return result;
- };
- var expandScratch = new Cartesian3();
- BoundingSphere.expand = function (sphere, point, result) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("point", point);
-
- result = BoundingSphere.clone(sphere, result);
- var radius = Cartesian3.magnitude(
- Cartesian3.subtract(point, result.center, expandScratch)
- );
- if (radius > result.radius) {
- result.radius = radius;
- }
- return result;
- };
- BoundingSphere.intersectPlane = function (sphere, plane) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("plane", plane);
-
- var center = sphere.center;
- var radius = sphere.radius;
- var normal = plane.normal;
- var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
- if (distanceToPlane < -radius) {
-
- return Intersect.OUTSIDE;
- } else if (distanceToPlane < radius) {
-
- return Intersect.INTERSECTING;
- }
- return Intersect.INSIDE;
- };
- BoundingSphere.transform = function (sphere, transform, result) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("transform", transform);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- result.center = Matrix4.multiplyByPoint(
- transform,
- sphere.center,
- result.center
- );
- result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
- return result;
- };
- var distanceSquaredToScratch = new Cartesian3();
- BoundingSphere.distanceSquaredTo = function (sphere, cartesian) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("cartesian", cartesian);
-
- var diff = Cartesian3.subtract(
- sphere.center,
- cartesian,
- distanceSquaredToScratch
- );
- return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
- };
- BoundingSphere.transformWithoutScale = function (sphere, transform, result) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("transform", transform);
-
- if (!defined(result)) {
- result = new BoundingSphere();
- }
- result.center = Matrix4.multiplyByPoint(
- transform,
- sphere.center,
- result.center
- );
- result.radius = sphere.radius;
- return result;
- };
- var scratchCartesian3 = new Cartesian3();
- BoundingSphere.computePlaneDistances = function (
- sphere,
- position,
- direction,
- result
- ) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("position", position);
- Check.typeOf.object("direction", direction);
-
- if (!defined(result)) {
- result = new Interval();
- }
- var toCenter = Cartesian3.subtract(
- sphere.center,
- position,
- scratchCartesian3
- );
- var mag = Cartesian3.dot(direction, toCenter);
- result.start = mag - sphere.radius;
- result.stop = mag + sphere.radius;
- return result;
- };
- var projectTo2DNormalScratch = new Cartesian3();
- var projectTo2DEastScratch = new Cartesian3();
- var projectTo2DNorthScratch = new Cartesian3();
- var projectTo2DWestScratch = new Cartesian3();
- var projectTo2DSouthScratch = new Cartesian3();
- var projectTo2DCartographicScratch = new Cartographic();
- var projectTo2DPositionsScratch = new Array(8);
- for (var n = 0; n < 8; ++n) {
- projectTo2DPositionsScratch[n] = new Cartesian3();
- }
- var projectTo2DProjection = new GeographicProjection();
- BoundingSphere.projectTo2D = function (sphere, projection, result) {
-
- Check.typeOf.object("sphere", sphere);
-
- projection = defaultValue(projection, projectTo2DProjection);
- var ellipsoid = projection.ellipsoid;
- var center = sphere.center;
- var radius = sphere.radius;
- var normal;
- if (Cartesian3.equals(center, Cartesian3.ZERO)) {
-
-
- normal = Cartesian3.clone(Cartesian3.UNIT_X, projectTo2DNormalScratch);
- } else {
- normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
- }
- var east = Cartesian3.cross(
- Cartesian3.UNIT_Z,
- normal,
- projectTo2DEastScratch
- );
- Cartesian3.normalize(east, east);
- var north = Cartesian3.cross(normal, east, projectTo2DNorthScratch);
- Cartesian3.normalize(north, north);
- Cartesian3.multiplyByScalar(normal, radius, normal);
- Cartesian3.multiplyByScalar(north, radius, north);
- Cartesian3.multiplyByScalar(east, radius, east);
- var south = Cartesian3.negate(north, projectTo2DSouthScratch);
- var west = Cartesian3.negate(east, projectTo2DWestScratch);
- var positions = projectTo2DPositionsScratch;
-
- var corner = positions[0];
- Cartesian3.add(normal, north, corner);
- Cartesian3.add(corner, east, corner);
-
- corner = positions[1];
- Cartesian3.add(normal, north, corner);
- Cartesian3.add(corner, west, corner);
-
- corner = positions[2];
- Cartesian3.add(normal, south, corner);
- Cartesian3.add(corner, west, corner);
-
- corner = positions[3];
- Cartesian3.add(normal, south, corner);
- Cartesian3.add(corner, east, corner);
- Cartesian3.negate(normal, normal);
-
- corner = positions[4];
- Cartesian3.add(normal, north, corner);
- Cartesian3.add(corner, east, corner);
-
- corner = positions[5];
- Cartesian3.add(normal, north, corner);
- Cartesian3.add(corner, west, corner);
-
- corner = positions[6];
- Cartesian3.add(normal, south, corner);
- Cartesian3.add(corner, west, corner);
-
- corner = positions[7];
- Cartesian3.add(normal, south, corner);
- Cartesian3.add(corner, east, corner);
- var length = positions.length;
- for (var i = 0; i < length; ++i) {
- var position = positions[i];
- Cartesian3.add(center, position, position);
- var cartographic = ellipsoid.cartesianToCartographic(
- position,
- projectTo2DCartographicScratch
- );
- projection.project(cartographic, position);
- }
- result = BoundingSphere.fromPoints(positions, result);
-
- center = result.center;
- var x = center.x;
- var y = center.y;
- var z = center.z;
- center.x = z;
- center.y = x;
- center.z = y;
- return result;
- };
- BoundingSphere.isOccluded = function (sphere, occluder) {
-
- Check.typeOf.object("sphere", sphere);
- Check.typeOf.object("occluder", occluder);
-
- return !occluder.isBoundingSphereVisible(sphere);
- };
- BoundingSphere.equals = function (left, right) {
- return (
- left === right ||
- (defined(left) &&
- defined(right) &&
- Cartesian3.equals(left.center, right.center) &&
- left.radius === right.radius)
- );
- };
- BoundingSphere.prototype.intersectPlane = function (plane) {
- return BoundingSphere.intersectPlane(this, plane);
- };
- BoundingSphere.prototype.distanceSquaredTo = function (cartesian) {
- return BoundingSphere.distanceSquaredTo(this, cartesian);
- };
- BoundingSphere.prototype.computePlaneDistances = function (
- position,
- direction,
- result
- ) {
- return BoundingSphere.computePlaneDistances(
- this,
- position,
- direction,
- result
- );
- };
- BoundingSphere.prototype.isOccluded = function (occluder) {
- return BoundingSphere.isOccluded(this, occluder);
- };
- BoundingSphere.prototype.equals = function (right) {
- return BoundingSphere.equals(this, right);
- };
- BoundingSphere.prototype.clone = function (result) {
- return BoundingSphere.clone(this, result);
- };
- BoundingSphere.prototype.volume = function () {
- var radius = this.radius;
- return volumeConstant * radius * radius * radius;
- };
- export default BoundingSphere;
|