diff --git a/CHANGES.md b/CHANGES.md index 4ea469cd70f0..08544ab89201 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ - Fixed render crash when creating a `polylineVolume` with very close points. [#9669](https://github.com/CesiumGS/cesium/pull/9669) - Fixed a bug in `PolylineGeometry` that incorrectly shifted colors when duplicate positions were removed. [#9676](https://github.com/CesiumGS/cesium/pull/9676) - Fixed a crash that would hang the browser if a `Label` was created with a soft hyphen in its text. [#9682](https://github.com/CesiumGS/cesium/pull/9682) +- Fixed the incorrect calculation of `distanceSquaredTo` in `BoundingSphere`. [#9686](https://github.com/CesiumGS/cesium/pull/9686) ### 1.83 - 2021-07-01 diff --git a/Source/Core/BoundingSphere.js b/Source/Core/BoundingSphere.js index 98610e009512..3d95c017ec15 100644 --- a/Source/Core/BoundingSphere.js +++ b/Source/Core/BoundingSphere.js @@ -1113,7 +1113,7 @@ var distanceSquaredToScratch = new Cartesian3(); * * @param {BoundingSphere} sphere The sphere. * @param {Cartesian3} cartesian The point - * @returns {Number} The estimated distance squared from the bounding sphere to the point. + * @returns {Number} The distance squared from the bounding sphere to the point. Returns 0 if the point is inside the sphere. * * @example * // Sort bounding spheres from back to front @@ -1132,7 +1132,13 @@ BoundingSphere.distanceSquaredTo = function (sphere, cartesian) { cartesian, distanceSquaredToScratch ); - return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius; + + var distance = Cartesian3.magnitude(diff) - sphere.radius; + if (distance <= 0.0) { + return 0.0; + } + + return distance * distance; }; /** diff --git a/Source/Core/OrientedBoundingBox.js b/Source/Core/OrientedBoundingBox.js index f9e1927afe26..85825c1d1c15 100644 --- a/Source/Core/OrientedBoundingBox.js +++ b/Source/Core/OrientedBoundingBox.js @@ -693,7 +693,7 @@ var scratchPPrime = new Cartesian3(); * * @param {OrientedBoundingBox} box The box. * @param {Cartesian3} cartesian The point - * @returns {Number} The estimated distance squared from the bounding sphere to the point. + * @returns {Number} The distance squared from the oriented bounding box to the point. Returns 0 if the point is inside the box. * * @example * // Sort bounding boxes from back to front diff --git a/Specs/Core/BoundingSphereSpec.js b/Specs/Core/BoundingSphereSpec.js index 3c0909dc09df..8a8ce0428061 100644 --- a/Specs/Core/BoundingSphereSpec.js +++ b/Specs/Core/BoundingSphereSpec.js @@ -703,11 +703,20 @@ describe("Core/BoundingSphere", function () { ).toEqual(expected); }); - it("estimated distance squared to point", function () { + it("distance squared to point outside of sphere", function () { var bs = new BoundingSphere(Cartesian3.ZERO, 1.0); var position = new Cartesian3(-2.0, 1.0, 0.0); - var expected = Cartesian3.magnitudeSquared(position) - 1.0; - expect(BoundingSphere.distanceSquaredTo(bs, position)).toEqual(expected); + var expected = 1.52786405; + expect(BoundingSphere.distanceSquaredTo(bs, position)).toEqualEpsilon( + expected, + CesiumMath.EPSILON6 + ); + }); + + it("distance squared to point inside sphere", function () { + var bs = new BoundingSphere(Cartesian3.ZERO, 1.0); + var position = new Cartesian3(-0.5, 0.5, 0.0); + expect(BoundingSphere.distanceSquaredTo(bs, position)).toEqual(0.0); }); it("projectTo2D", function () {