Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect distanceSquaredTo calculation in BoundingSphere #9686

Merged
merged 5 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions Source/Core/BoundingSphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/OrientedBoundingBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions Specs/Core/BoundingSphereSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down