Permalink
Browse files

Functions useful for creating bounding spheres from boxes

  • Loading branch information...
1 parent ff628af commit dfa7c364e071181a168a3a1e472a5aab159868e3 @pjcozzi pjcozzi committed Mar 7, 2013
Showing with 176 additions and 0 deletions.
  1. +37 −0 Source/Core/BoundingSphere.js
  2. +69 −0 Source/Core/Cartesian3.js
  3. +24 −0 Specs/Core/BoundingSphereSpec.js
  4. +46 −0 Specs/Core/Cartesian3Spec.js
View
37 Source/Core/BoundingSphere.js
@@ -482,6 +482,43 @@ define([
return result;
};
+
+
+ /**
+ * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
+ * tighly and fully encompases the box.
+ *
+ * @memberof BoundingSphere
+ *
+ * @param {Number} [corner] The minimum height over the extent.
+ * @param {Number} [oppositeCorner] The maximum height over the extent.
+ * @param {BoundingSphere} [result] The object onto which to store the result.
+ *
+ * @return {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
+ *
+ * @exception {DeveloperError} corner and oppositeCorner are required.
+ *
+ * @example
+ * // Create a bounding sphere around the unit cube
+ * var sphere = BoundingSphere.fromCornerPoints(new Cartesian3(-0.5, -0.5, -0.5), new Cartesian3(0.5, 0.5, 0.5));
+ */
+ BoundingSphere.fromCornerPoints = function(corner, oppositeCorner, result) {
+ if ((typeof corner === 'undefined') || (typeof oppositeCorner === 'undefined')) {
+ throw new DeveloperError('corner and oppositeCorner are required.');
+ }
+
+ if (typeof result === 'undefined') {
+ result = new BoundingSphere();
+ }
+
+ var center = Cartesian3.add(corner, oppositeCorner);
+ Cartesian3.multiplyByScalar(center, 0.5, center);
+
+ result.center = center;
+ result.radius = Cartesian3.distance(center, oppositeCorner);
+ return result;
+ };
+
/**
* Duplicates a BoundingSphere instance.
* @memberof BoundingSphere
View
69 Source/Core/Cartesian3.js
@@ -69,6 +69,49 @@ define([
};
/**
+ * Creates a Cartesian3 from three consecutive elements in an array.
+ * @memberof Cartesian3
+ *
+ * @param {Array} values The Spherical to be converted to Cartesian3.
+ * @param {Number} [offset=0] The offset into the array of the first element, which corresponds to the x component.
+ * @param {Cartesian3} [result] The object onto which to store the result.
+ *
+ * @return {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
+ *
+ * @exception {DeveloperError} values is required.
+ * @exception {DeveloperError} offset + 3 is greater than the length of the array.
+ *
+ * @example
+ * // Create a Cartesian3 with (1.0, 2.0, 3.0)
+ * var v = [1.0, 2.0, 3.0];
+ * var p = Cartesian3.fromArray(v);
+ *
+ * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
+ * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
+ * var p2 = Cartesian3.fromArray(v2, 2);
+ */
+ Cartesian3.fromArray = function(values, offset, result) {
+ if (typeof values === 'undefined') {
+ throw new DeveloperError('values is required.');
+ }
+
+ if (offset + 3 > values.length) {
+ throw new DeveloperError('offset + 3 is greater than the length of the array.');
+ }
+
+ offset = defaultValue(offset, 0);
+
+ if (typeof result === 'undefined') {
+ result = new Cartesian3();
+ }
+
+ result.x = values[offset + 0];
+ result.y = values[offset + 1];
+ result.z = values[offset + 2];
+ return result;
+ };
+
+ /**
* Duplicates a Cartesian3 instance.
* @memberof Cartesian3
*
@@ -167,6 +210,32 @@ define([
return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
};
+ var distanceScratch = new Cartesian3();
+
+ /**
+ * Computes the distance between two points
+ * @memberof Cartesian3
+ *
+ * @param {Cartesian3} left The first point to compute the distance from.
+ * @param {Cartesian3} right The second point to compute the distance to.
+ *
+ * @return {Number} The distance between two points.
+ *
+ * @exception {DeveloperError} left and right are required.
+ *
+ * @example
+ * // Returns 1.0
+ * var d = Cartesian3.distance(new Cartesian3(1.0, 0.0, 0.0), new Cartesian3(2.0, 0.0, 0.0));
+ */
+ Cartesian3.distance = function(left, right) {
+ if ((typeof left === 'undefined') || (typeof right === 'undefined')) {
+ throw new DeveloperError('left and right are required.');
+ }
+
+ Cartesian3.subtract(left, right, distanceScratch);
+ return Cartesian3.magnitude(distanceScratch);
+ };
+
/**
* Computes the normalized form of the supplied Cartesian.
* @memberof Cartesian3
View
24 Specs/Core/BoundingSphereSpec.js
@@ -287,6 +287,30 @@ defineSuite([
expect(BoundingSphere.fromExtent3D(extent, ellipsoid)).toEqual(expected);
});
+ it('fromCornerPoints', function() {
+ var sphere = BoundingSphere.fromCornerPoints(new Cartesian3(-1.0, -0.0, 0.0), new Cartesian3(1.0, 0.0, 0.0));
+ expect(sphere).toEqual(new BoundingSphere(Cartesian3.ZERO, 1.0));
+ });
+
+ it('fromCornerPoints with a result parameter', function() {
+ var sphere = new BoundingSphere;
+ var result = BoundingSphere.fromCornerPoints(new Cartesian3(0.0, -1.0, 0.0), new Cartesian3(0.0, 1.0, 0.0), sphere);
+ expect(result).toBe(sphere);
+ expect(result).toEqual(new BoundingSphere(Cartesian3.ZERO, 1.0));
+ });
+
+ it('fromCornerPoints throws without corner', function() {
+ expect(function() {
+ BoundingSphere.fromCornerPoints();
+ }).toThrow();
+ });
+
+ it('fromCornerPoints throws without oppositeCorner', function() {
+ expect(function() {
+ BoundingSphere.fromCornerPoints(Cartesian3.UNIT_X);
+ }).toThrow();
+ });
+
it('sphere on the positive side of a plane', function() {
var sphere = new BoundingSphere(Cartesian3.ZERO, 0.5);
var normal = Cartesian3.UNIT_X.negate();
View
46 Specs/Core/Cartesian3Spec.js
@@ -41,6 +41,35 @@ defineSuite([
expect(cartesian).toEqualEpsilon(existing, CesiumMath.EPSILON15);
});
+ it('fromArray creates a Cartesian3', function() {
+ var cartesian = Cartesian3.fromArray([1.0, 2.0, 3.0]);
+ expect(cartesian).toEqual(new Cartesian3(1.0, 2.0, 3.0));
+ });
+
+ it('fromArray with an offset creates a Cartesian3', function() {
+ var cartesian = Cartesian3.fromArray([0.0, 1.0, 2.0, 3.0, 0.0], 1);
+ expect(cartesian).toEqual(new Cartesian3(1.0, 2.0, 3.0));
+ });
+
+ it('fromArray creates a Cartesian3 with a result parameter', function() {
+ var cartesian = new Cartesian3();
+ var result = Cartesian3.fromArray([1.0, 2.0, 3.0], 0, cartesian);
+ expect(result).toBe(cartesian);
+ expect(result).toEqual(new Cartesian3(1.0, 2.0, 3.0));
+ });
+
+ it('fromArray throws without values', function() {
+ expect(function() {
+ Cartesian3.fromArray();
+ }).toThrow();
+ });
+
+ it('fromArray throws with an invalid offset', function() {
+ expect(function() {
+ Cartesian3.fromArray([0.0, 0.0, 0.0], 1);
+ }).toThrow();
+ });
+
it('clone without a result parameter', function() {
var cartesian = new Cartesian3(1.0, 2.0, 3.0);
var result = cartesian.clone();
@@ -103,6 +132,23 @@ defineSuite([
expect(cartesian.magnitude()).toEqual(Math.sqrt(50.0));
});
+ it('distance', function() {
+ var distance = Cartesian3.distance(new Cartesian3(1.0, 0.0, 0.0), new Cartesian3(2.0, 0.0, 0.0));
+ expect(distance).toEqual(1.0);
+ });
+
+ it('distance throws without left', function() {
+ expect(function() {
+ Cartesian3.distance();
+ }).toThrow();
+ });
+
+ it('distance throws without right', function() {
+ expect(function() {
+ Cartesian3.distance(Cartesian3.UNIT_X);
+ }).toThrow();
+ });
+
it('normalize works without a result parameter', function() {
var cartesian = new Cartesian3(2.0, 0.0, 0.0);
var expectedResult = new Cartesian3(1.0, 0.0, 0.0);

0 comments on commit dfa7c36

Please sign in to comment.