Browse Source

add equals, clone to Box2, Box3, Plane and Sphere. Adopt +Inf/-Inf in Box2 and Box3 per @mrdoob'd recommendation. Begin Box3 unit test.

Ben Houston 12 years ago
parent
commit
be29d9f64d
6 changed files with 108 additions and 5 deletions
  1. 15 2
      src/core/Box2.js
  2. 17 3
      src/core/Box3.js
  3. 12 0
      src/core/Plane.js
  4. 12 0
      src/core/Sphere.js
  5. 51 0
      test/core/Box3.js
  6. 1 0
      test/testsuite.html

+ 15 - 2
src/core/Box2.js

@@ -80,8 +80,8 @@ THREE.Box2.prototype = {
 
 	makeEmpty: function () {
 
-		this.min.x = this.min.y = Number.MAX_VALUE;
-		this.max.x = this.max.y = -Number.MAX_VALUE;
+		this.min.x = this.min.y = Infinity;
+		this.max.x = this.max.y = -Infinity;
 
 		return this;
 	},
@@ -215,7 +215,20 @@ THREE.Box2.prototype = {
 		this.expandByVector( sizeDeltaHalf );
 
 		return this;
+	},
+
+	equals: function ( box ) {
+
+		return box.min.equals( this.min ) && box.max.equals( this.max );
+
+	},
+
+	clone: function () {
+
+		return new THREE.Box2().copy( this );
+
 	}
+
 	
 };
 

+ 17 - 3
src/core/Box3.js

@@ -7,7 +7,9 @@ THREE.Box3 = function ( min, max ) {
 	if( ! min && ! max ) {			
 		this.min = new THREE.Vector3();
 		this.max = new THREE.Vector3();
-		this.makeEmpty();
+		this.min.x = this.min.y = this.min.z = Infinity;
+		this.max.x = this.max.y = this.max.z = -Infinity;
+		console.log( this );
 	}
 	else {
 		this.min = min || new THREE.Vector3();
@@ -87,8 +89,8 @@ THREE.Box3.prototype = {
 
 	makeEmpty: function () {
 
-		this.min.x = this.min.y = this.min.z = Number.MAX_VALUE;
-		this.max.x = this.max.y = this.max.z = -Number.MAX_VALUE;
+		this.min.x = this.min.y = this.min.z = Infinity;
+		this.max.x = this.max.y = this.max.z = -Infinity;
 
 		return this;
 	},
@@ -228,6 +230,18 @@ THREE.Box3.prototype = {
 		this.expandByVector( sizeDeltaHalf );
 
 		return this;
+	},
+
+	equals: function ( box ) {
+
+		return box.min.equals( this.min ) && box.max.equals( this.max );
+
+	},
+
+	clone: function () {
+
+		return new THREE.Box3().copy( this );
+
 	}
 	
 };

+ 12 - 0
src/core/Plane.js

@@ -114,6 +114,18 @@ THREE.Plane.prototype = {
 		this.constant =	- offset.dot( normal );
 
 		return this;
+	},
+
+	equals: function ( plane ) {
+
+		return plane.normal.equals( this.normal ) && ( sphere.constant == this.constant );
+
+	},
+
+	clone: function () {
+
+		return new THREE.Plane3().copy( this );
+
 	}
 
 };

+ 12 - 0
src/core/Sphere.js

@@ -98,6 +98,18 @@ THREE.Sphere.prototype = {
 		this.radius *= factor;
 		
 		return this;
+	},
+
+	equals: function ( sphere ) {
+
+		return sphere.center.equals( this.center ) && ( sphere.radius == this.radius );
+
+	},
+
+	clone: function () {
+
+		return new THREE.Sphere3().copy( this );
+
 	}
 
 };

+ 51 - 0
test/core/Box3.js

@@ -0,0 +1,51 @@
+/**
+ * @author bhouston / http://exocortex.com
+ */
+
+var x = 2;
+var y = 3;
+var z = 4;
+var w = 5;
+
+var zero = new THREE.Vector3();
+var one = new THREE.Vector3( 1, 1, 1 );
+
+module( "Box3" );
+
+test( "constructor", function() {
+	var a = new THREE.Box3();
+	console.log( a );
+	console.log( a.empty() );
+	ok( a.empty(), "Passed!" );
+	ok( a.volume() < 0, "Passed!" );
+
+	a = new THREE.Box3( zero );
+	ok( a.min.equals( zero ), "Passed!" );
+	ok( a.max.equals( zero ), "Passed!" );
+
+	a = new THREE.Box3( zero, one );
+	ok( a.min.equals( zero ), "Passed!" );
+	ok( a.max.equals( one ), "Passed!" );
+});
+
+test( "copy", function() {
+	var a = new THREE.Box3( zero, one );
+	var b = new THREE.Box3().copy( a );
+	ok( b.min.equals( zero ), "Passed!" );
+	ok( b.max.equals( one ), "Passed!" );
+
+	// ensure that it is a true copy
+	a.makeEmpty();
+	ok( b.min.equals( zero ), "Passed!" );
+	ok( b.max.equals( one ), "Passed!" );
+});
+
+test( "set", function() {
+	var a = new THREE.Box3();
+	ok( a.empty(), "Passed!" );
+
+	a.set( zero, one )
+	ok( a.min.equals( zero ), "Passed!" );
+	ok( a.max.equals( one ), "Passed!" );
+});
+

+ 1 - 0
test/testsuite.html

@@ -12,5 +12,6 @@
   <script src="core/Vector2.js"></script>
   <script src="core/Vector3.js"></script>
   <script src="core/Vector4.js"></script>
+  <script src="core/Box3.js"></script>
 </body>
 </html>