diff --git a/CHANGES.md b/CHANGES.md index c5323b39591e..7f88100d142b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Change Log * Added `TimeDynamicPointCloud` for playback of time-dynamic point cloud data, where each frame is a 3D Tiles Point Cloud tile. [#6721](https://github.com/AnalyticalGraphicsInc/cesium/pull/6721) * Added `CoplanarPolygonGeometry` and `CoplanarPolygonGeometryOutline` for drawing polygons composed of coplanar positions that are not necessarily on the ellipsoid surface. [#6769](https://github.com/AnalyticalGraphicsInc/cesium/pull/6769) * Improved support for polygon entities using `perPositionHeight`, including supporting vertical polygons. This also improves KML compatibility. [#6791](https://github.com/AnalyticalGraphicsInc/cesium/pull/6791) +* Added `Cartesian3.midpoint` to compute the midpoint between two `Cartesian3` positions [#6836](https://github.com/AnalyticalGraphicsInc/cesium/pull/6836) ##### Deprecated :hourglass_flowing_sand: * Support for 3D Tiles `content.url` is deprecated to reflect updates to the [3D Tiles spec](https://github.com/AnalyticalGraphicsInc/3d-tiles/pull/301). Use `content.uri instead`. Support for `content.url` will remain for backwards compatibility. [#6744](https://github.com/AnalyticalGraphicsInc/cesium/pull/6744) diff --git a/Source/Core/ApproximateTerrainHeights.js b/Source/Core/ApproximateTerrainHeights.js index 400be8585b14..2f20a806afa3 100644 --- a/Source/Core/ApproximateTerrainHeights.js +++ b/Source/Core/ApproximateTerrainHeights.js @@ -98,9 +98,7 @@ define([ ellipsoid.cartographicToCartesian(Rectangle.southwest(rectangle, scratchDiagonalCartographic), scratchDiagonalCartesianSW); - Cartesian3.subtract(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian); - Cartesian3.add(scratchDiagonalCartesianNE, - Cartesian3.multiplyByScalar(scratchCenterCartesian, 0.5, scratchCenterCartesian), scratchCenterCartesian); + Cartesian3.midpoint(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian); var surfacePosition = ellipsoid.scaleToGeodeticSurface(scratchCenterCartesian, scratchSurfaceCartesian); if (defined(surfacePosition)) { var distance = Cartesian3.distance(scratchCenterCartesian, surfacePosition); diff --git a/Source/Core/AxisAlignedBoundingBox.js b/Source/Core/AxisAlignedBoundingBox.js index 1a6a4082149d..523e1a99686c 100644 --- a/Source/Core/AxisAlignedBoundingBox.js +++ b/Source/Core/AxisAlignedBoundingBox.js @@ -41,8 +41,7 @@ define([ //If center was not defined, compute it. if (!defined(center)) { - center = Cartesian3.add(this.minimum, this.maximum, new Cartesian3()); - Cartesian3.multiplyByScalar(center, 0.5, center); + center = Cartesian3.midpoint(this.minimum, this.maximum, new Cartesian3()); } else { center = Cartesian3.clone(center); } @@ -111,8 +110,7 @@ define([ maximum.y = maximumY; maximum.z = maximumZ; - var center = Cartesian3.add(minimum, maximum, result.center); - Cartesian3.multiplyByScalar(center, 0.5, center); + result.center = Cartesian3.midpoint(minimum, maximum, result.center); return result; }; diff --git a/Source/Core/BoundingSphere.js b/Source/Core/BoundingSphere.js index e799a3166b64..935ebae8a6ac 100644 --- a/Source/Core/BoundingSphere.js +++ b/Source/Core/BoundingSphere.js @@ -178,7 +178,7 @@ define([ maxBoxPt.y = yMax.y; maxBoxPt.z = zMax.z; - var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch); + var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch); // Begin 2nd pass to find naive radius and modify the ritter sphere. var naiveRadius = 0; @@ -451,7 +451,7 @@ define([ maxBoxPt.y = yMax.y; maxBoxPt.z = zMax.z; - var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch); + var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch); // Begin 2nd pass to find naive radius and modify the ritter sphere. var naiveRadius = 0; @@ -609,7 +609,7 @@ define([ maxBoxPt.y = yMax.y; maxBoxPt.z = zMax.z; - var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch); + var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch); // Begin 2nd pass to find naive radius and modify the ritter sphere. var naiveRadius = 0; @@ -673,9 +673,7 @@ define([ result = new BoundingSphere(); } - var center = result.center; - Cartesian3.add(corner, oppositeCorner, center); - Cartesian3.multiplyByScalar(center, 0.5, center); + var center = Cartesian3.midpoint(corner, oppositeCorner, result.center); result.radius = Cartesian3.distance(center, oppositeCorner); return result; }; diff --git a/Source/Core/Cartesian3.js b/Source/Core/Cartesian3.js index 65f8c84bc5c0..d12ddbc50ab9 100644 --- a/Source/Core/Cartesian3.js +++ b/Source/Core/Cartesian3.js @@ -778,6 +778,27 @@ define([ return result; }; + /** + * Computes the midpoint between the right and left Cartesian. + * @param {Cartesian3} left The first Cartesian. + * @param {Cartesian3} right The second Cartesian. + * @param {Cartesian3} result The object onto which to store the result. + * @returns {Cartesian3} The midpoint. + */ + Cartesian3.midpoint = function(left, right, result) { + //>>includeStart('debug', pragmas.debug); + Check.typeOf.object('left', left); + Check.typeOf.object('right', right); + Check.typeOf.object('result', result); + //>>includeEnd('debug'); + + result.x = (left.x + right.x) * 0.5; + result.y = (left.y + right.y) * 0.5; + result.z = (left.z + right.z) * 0.5; + + return result; + }; + /** * Returns a Cartesian3 position from longitude and latitude values given in degrees. * diff --git a/Source/Core/CorridorGeometryLibrary.js b/Source/Core/CorridorGeometryLibrary.js index c00ef5907de8..7e7f8aa9b540 100644 --- a/Source/Core/CorridorGeometryLibrary.js +++ b/Source/Core/CorridorGeometryLibrary.js @@ -81,7 +81,7 @@ define([ var leftEdge = calculatedPositions[1]; startPoint = Cartesian3.fromArray(calculatedPositions[1], leftEdge.length - 3, startPoint); endPoint = Cartesian3.fromArray(calculatedPositions[0], 0, endPoint); - cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint); + cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint); var firstEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false); var length = calculatedPositions.length - 1; @@ -89,7 +89,7 @@ define([ leftEdge = calculatedPositions[length]; startPoint = Cartesian3.fromArray(rightEdge, rightEdge.length - 3, startPoint); endPoint = Cartesian3.fromArray(leftEdge, 0, endPoint); - cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint); + cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint); var lastEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false); return [firstEndCap, lastEndCap]; diff --git a/Specs/Core/Cartesian3Spec.js b/Specs/Core/Cartesian3Spec.js index 65467fb60f36..0238e56a7650 100644 --- a/Specs/Core/Cartesian3Spec.js +++ b/Specs/Core/Cartesian3Spec.js @@ -690,6 +690,34 @@ defineSuite([ expect(left).toEqual(expectedResult); }); + it('midpoint works with a result parameter', function() { + var left = new Cartesian3(0.0, 0.0, 6.0); + var right = new Cartesian3(0.0, 0.0, -6.0); + var result = new Cartesian3(); + var expectedResult = new Cartesian3(0.0, 0.0, 0.0); + var returnedResult = Cartesian3.midpoint(left, right, result); + expect(returnedResult).toBe(result); + expect(result).toEqual(expectedResult); + }); + + it('midpoint throws with no left', function() { + expect(function() { + return Cartesian3.midpoint(undefined, new Cartesian3(), new Cartesian3()); + }).toThrowDeveloperError(); + }); + + it('midpoint throws with no right', function() { + expect(function() { + return Cartesian3.midpoint(new Cartesian3(), undefined, new Cartesian3()); + }).toThrowDeveloperError(); + }); + + it('midpoint throws with no result', function() { + expect(function() { + return Cartesian3.midpoint(new Cartesian3(), new Cartesian3(), undefined); + }).toThrowDeveloperError(); + }); + it('fromSpherical throws with no spherical parameter', function() { expect(function() { Cartesian3.fromSpherical(undefined);