Skip to content

Commit

Permalink
Merge pull request #275 from AnalyticalGraphicsInc/sphere-union
Browse files Browse the repository at this point in the history
BoundingSphere union
  • Loading branch information
mramato committed Sep 28, 2012
2 parents 87c624b + 634fd1d commit 28036ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Source/Core/BoundingSphere.js
Expand Up @@ -287,6 +287,7 @@ define([
};

var unionScratch = new Cartesian3();
var unionScratchCenter = new Cartesian3();
/**
* Computes a bounding sphere that contains both the left and right bounding spheres.
* @memberof BoundingSphere
Expand Down Expand Up @@ -315,12 +316,15 @@ define([
var leftCenter = left.center;
var rightCenter = right.center;

var center = Cartesian3.add(leftCenter, rightCenter, result.center);
result.center = Cartesian3.multiplyByScalar(center, 0.5, center);
Cartesian3.add(leftCenter, rightCenter, unionScratchCenter);
var center = Cartesian3.multiplyByScalar(unionScratchCenter, 0.5, unionScratchCenter);

var radius1 = Cartesian3.subtract(leftCenter, center, unionScratch).magnitude() + left.radius;
var radius2 = Cartesian3.subtract(rightCenter, center, unionScratch).magnitude() + right.radius;

result.radius = Math.max(radius1, radius2);
Cartesian3.clone(center, result.center);

return result;
};

Expand Down
8 changes: 8 additions & 0 deletions Specs/Core/BoundingSphereSpec.js
Expand Up @@ -202,6 +202,14 @@ defineSuite([
expect(bs1.union(bs2)).toEqual(expected);
});

it('union result parameter is caller', function() {
var bs1 = new BoundingSphere(Cartesian3.UNIT_X.negate().multiplyByScalar(3.0), 3.0);
var bs2 = new BoundingSphere(Cartesian3.UNIT_X, 1.0);
var expected = new BoundingSphere(Cartesian3.UNIT_X.negate(), 5.0);
bs1.union(bs2, bs1);
expect(bs1).toEqual(expected);
});

it('expands to contain another point', function() {
var bs = new BoundingSphere(Cartesian3.UNIT_X.negate(), 1.0);
var point = Cartesian3.UNIT_X;
Expand Down

0 comments on commit 28036ac

Please sign in to comment.