Skip to content

Commit

Permalink
Merge pull request #3165 from AnalyticalGraphicsInc/sphere-from-orien…
Browse files Browse the repository at this point in the history
…ted-box

Added BoundingSphere.fromOrientedBoundingBox function
  • Loading branch information
pjcozzi committed Nov 5, 2015
2 parents ee68e6b + e084434 commit 00f8d98
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Change Log
* Fixed an issue where the sun texture is not generated correctly on some mobile devices. [#3141](https://github.com/AnalyticalGraphicsInc/cesium/issues/3141)
* Fixed a bug in the deprecated `jsonp` that prevented it from returning a promise. Its replacement, `loadJsonp`, was unaffected.
* Fixed glTF implementation to read the version as a string as per the specification and to correctly handle backwards compatibility for axis-angle rotations in glTF 0.8 models.
* Added `BoundingSphere.fromOrientedBoundingBox` function.
* Fixed a bug that caused setting `Entity.parent` to `undefined` to throw an exception. [#3169](https://github.com/AnalyticalGraphicsInc/cesium/issues/3169)
* Added `Model.maximumScale` and `ModelGraphics.maximumScale` properties, giving an upper limit for minimumPixelSize.
* Entities have a reference to their entity collection.
Expand Down
33 changes: 33 additions & 0 deletions Source/Core/BoundingSphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
'./GeographicProjection',
'./Intersect',
'./Interval',
'./Matrix3',
'./Matrix4',
'./Plane',
'./Rectangle'
Expand All @@ -24,6 +25,7 @@ define([
GeographicProjection,
Intersect,
Interval,
Matrix3,
Matrix4,
Plane,
Rectangle) {
Expand Down Expand Up @@ -590,6 +592,37 @@ define([
return result;
};

var fromOrientedBoundingBoxScratchU = new Cartesian3();
var fromOrientedBoundingBoxScratchV = new Cartesian3();
var fromOrientedBoundingBoxScratchW = new Cartesian3();

/**
* Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
*
* @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
* @param {BoundingSphere} [result] The object onto which to store the result.
* @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
*/
BoundingSphere.fromOrientedBoundingBox = function(orientedBoundingBox, result) {
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);

var uHalf = Cartesian3.magnitude(u);
var vHalf = Cartesian3.magnitude(v);
var wHalf = Cartesian3.magnitude(w);

result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
result.radius = Math.max(uHalf, vHalf, wHalf);

return result;
};

/**
* Duplicates a BoundingSphere instance.
*
Expand Down
17 changes: 3 additions & 14 deletions Source/Core/OrientedBoundingBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ define([
/**
* Determines whether or not a bounding box is hidden from view by the occluder.
*
* @param {OrientedBoundingBox} sphere The bounding box surrounding the occludee object.
* @param {OrientedBoundingBox} box The bounding box surrounding the occludee object.
* @param {Occluder} occluder The occluder.
* @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
* @returns {Boolean} <code>true</code> if the box is not visible; otherwise <code>false</code>.
*/
OrientedBoundingBox.isOccluded = function(box, occluder) {
//>>includeStart('debug', pragmas.debug);
Expand All @@ -625,18 +625,7 @@ define([
}
//>>includeEnd('debug');

var halfAxes = box.halfAxes;
var u = Matrix3.getColumn(halfAxes, 0, scratchCartesianU);
var v = Matrix3.getColumn(halfAxes, 1, scratchCartesianV);
var w = Matrix3.getColumn(halfAxes, 2, scratchCartesianW);

var uHalf = Cartesian3.magnitude(u);
var vHalf = Cartesian3.magnitude(v);
var wHalf = Cartesian3.magnitude(w);

var sphere = scratchBoundingSphere;
sphere.center = Cartesian3.clone(box.center, sphere.center);
sphere.radius = Math.max(uHalf, vHalf, wHalf);
var sphere = BoundingSphere.fromOrientedBoundingBox(box, scratchBoundingSphere);

return !occluder.isBoundingSphereVisible(sphere);
};
Expand Down
17 changes: 17 additions & 0 deletions Specs/Core/BoundingSphereSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defineSuite([
'Core/Interval',
'Core/Math',
'Core/Matrix4',
'Core/OrientedBoundingBox',
'Core/Plane',
'Core/Rectangle',
'Specs/createPackableSpecs'
Expand All @@ -24,6 +25,7 @@ defineSuite([
Interval,
CesiumMath,
Matrix4,
OrientedBoundingBox,
Plane,
Rectangle,
createPackableSpecs) {
Expand Down Expand Up @@ -386,6 +388,21 @@ defineSuite([
expect(sphere).toEqual(expected);
});

it('fromOrientedBoundingBox works with a result', function() {
var box = OrientedBoundingBox.fromPoints(getPositions());
var expected = new BoundingSphere(positionsCenter, positionsRadius);
var sphere = new BoundingSphere();
BoundingSphere.fromOrientedBoundingBox(box, sphere);
expect(sphere).toEqual(expected);
});

it('fromOrientedBoundingBox works without a result parameter', function() {
var box = OrientedBoundingBox.fromPoints(getPositions());
var expected = new BoundingSphere(positionsCenter, positionsRadius);
var sphere = BoundingSphere.fromOrientedBoundingBox(box);
expect(sphere).toEqual(expected);
});

it('intersectPlane with sphere on the positive side of a plane', function() {
var sphere = new BoundingSphere(Cartesian3.ZERO, 0.5);
var normal = Cartesian3.negate(Cartesian3.UNIT_X, new Cartesian3());
Expand Down

0 comments on commit 00f8d98

Please sign in to comment.