Box3.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. module( "Box3" );
  5. test( "constructor", function() {
  6. var a = new THREE.Box3();
  7. console.log( "BOX3: constructor");
  8. console.log( a );
  9. console.log( negInf3);
  10. console.log( posInf3 );
  11. ok( a.min.equals( posInf3 ), "Passed!" );
  12. ok( a.max.equals( negInf3 ), "Passed!" );
  13. a = new THREE.Box3( zero3 );
  14. ok( a.min.equals( zero3 ), "Passed!" );
  15. ok( a.max.equals( zero3 ), "Passed!" );
  16. a = new THREE.Box3( zero3, one3 );
  17. ok( a.min.equals( zero3 ), "Passed!" );
  18. ok( a.max.equals( one3 ), "Passed!" );
  19. });
  20. test( "copy", function() {
  21. var a = new THREE.Box3( zero3, one3 );
  22. var b = new THREE.Box3().copy( a );
  23. ok( b.min.equals( zero3 ), "Passed!" );
  24. ok( b.max.equals( one3 ), "Passed!" );
  25. // ensure that it is a true copy
  26. a.min = zero3;
  27. a.max = one3;
  28. ok( b.min.equals( zero3 ), "Passed!" );
  29. ok( b.max.equals( one3 ), "Passed!" );
  30. });
  31. test( "set", function() {
  32. var a = new THREE.Box3();
  33. a.set( zero3, one3 )
  34. ok( a.min.equals( zero3 ), "Passed!" );
  35. ok( a.max.equals( one3 ), "Passed!" );
  36. });
  37. test( "empty/makeEmpty", function() {
  38. var a = new THREE.Box3();
  39. ok( a.empty(), "Passed!" );
  40. var a = new THREE.Box3( zero3, one3 );
  41. ok( ! a.empty(), "Passed!" );
  42. a.makeEmpty();
  43. ok( a.empty(), "Passed!" );
  44. });
  45. test( "volume", function() {
  46. var a = new THREE.Box3( zero3, one3 );
  47. ok( a.volume() == 1, "Passed!" );
  48. });
  49. test( "center", function() {
  50. var a = new THREE.Box3( zero3 );
  51. ok( a.center().equals( zero3 ), "Passed!" );
  52. a = new THREE.Box3( zero3, one3 );
  53. var midpoint = one3.clone().multiplyScalar( 0.5 );
  54. ok( a.center().equals( midpoint ), "Passed!" );
  55. });
  56. test( "size", function() {
  57. var a = new THREE.Box3( zero3 );
  58. ok( a.size().equals( zero3 ), "Passed!" );
  59. a = new THREE.Box3( zero3, one3 );
  60. ok( a.size().equals( one3 ), "Passed!" );
  61. });
  62. test( "expandByPoint", function() {
  63. var a = new THREE.Box3( zero3 );
  64. a.expandByPoint( zero3 );
  65. ok( a.size().equals( zero3 ), "Passed!" );
  66. a.expandByPoint( one3 );
  67. ok( a.size().equals( one3 ), "Passed!" );
  68. a.expandByPoint( one3.clone().negate() );
  69. ok( a.size().equals( one3.clone().multiplyScalar( 2 ) ), "Passed!" );
  70. ok( a.center().equals( zero3 ), "Passed!" );
  71. });
  72. test( "expandByVector", function() {
  73. var a = new THREE.Box3( zero3 );
  74. a.expandByVector( zero3 );
  75. ok( a.size().equals( zero3 ), "Passed!" );
  76. a.expandByVector( one3 );
  77. ok( a.size().equals( one3.clone().multiplyScalar( 2 ) ), "Passed!" );
  78. ok( a.center().equals( zero3 ), "Passed!" );
  79. });
  80. test( "expandByScalar", function() {
  81. var a = new THREE.Box3( zero3 );
  82. a.expandByScalar( 0 );
  83. ok( a.size().equals( zero3 ), "Passed!" );
  84. a.expandByScalar( 1 );
  85. ok( a.size().equals( one3.clone().multiplyScalar( 2 ) ), "Passed!" );
  86. ok( a.center().equals( zero3 ), "Passed!" );
  87. });
  88. test( "containsPoint", function() {
  89. var a = new THREE.Box3( zero3 );
  90. ok( a.containsPoint( zero3 ), "Passed!" );
  91. ok( ! a.containsPoint( one3 ), "Passed!" );
  92. a.expandByScalar( 1 );
  93. ok( a.containsPoint( zero3 ), "Passed!" );
  94. ok( a.containsPoint( one3 ), "Passed!" );
  95. ok( a.containsPoint( one3.clone().negate() ), "Passed!" );
  96. });
  97. test( "containsBox", function() {
  98. var a = new THREE.Box3( zero3 );
  99. var b = new THREE.Box3( zero3, one3 );
  100. var c = new THREE.Box3( one3.clone().negate(), one3 );
  101. ok( a.containsBox( a ), "Passed!" );
  102. ok( ! a.containsBox( b ), "Passed!" );
  103. ok( ! a.containsBox( c ), "Passed!" );
  104. ok( b.containsBox( a ), "Passed!" );
  105. ok( c.containsBox( a ), "Passed!" );
  106. ok( ! b.containsBox( c ), "Passed!" );
  107. });
  108. test( "getParameter", function() {
  109. var a = new THREE.Box3( zero3, one3 );
  110. var b = new THREE.Box3( one3.clone().negate(), one3 );
  111. ok( a.getParameter( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  112. ok( a.getParameter( new THREE.Vector3( 1, 1, 1 ) ).equals( new THREE.Vector3( 1, 1, 1 ) ), "Passed!" );
  113. ok( b.getParameter( new THREE.Vector3( -1, -1, -1 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  114. ok( b.getParameter( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0.5, 0.5, 0.5 ) ), "Passed!" );
  115. ok( b.getParameter( new THREE.Vector3( 1, 1, 1 ) ).equals( new THREE.Vector3( 1, 1, 1 ) ), "Passed!" );
  116. });
  117. test( "clampPoint", function() {
  118. var a = new THREE.Box3( zero3, zero3 );
  119. var b = new THREE.Box3( one3.clone().negate(), one3 );
  120. ok( a.clampPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  121. ok( a.clampPoint( new THREE.Vector3( 1, 1, 1 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  122. ok( a.clampPoint( new THREE.Vector3( -1, -1, -1 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  123. ok( b.clampPoint( new THREE.Vector3( 2, 2, 2 ) ).equals( new THREE.Vector3( 1, 1, 1 ) ), "Passed!" );
  124. ok( b.clampPoint( new THREE.Vector3( 1, 1, 1 ) ).equals( new THREE.Vector3( 1, 1, 1 ) ), "Passed!" );
  125. ok( b.clampPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  126. ok( b.clampPoint( new THREE.Vector3( -1, -1, -1 ) ).equals( new THREE.Vector3( -1, -1, -1 ) ), "Passed!" );
  127. ok( b.clampPoint( new THREE.Vector3( -2, -2, -2 ) ).equals( new THREE.Vector3( -1, -1, -1 ) ), "Passed!" );
  128. });
  129. test( "distanceToPoint", function() {
  130. var a = new THREE.Box3( zero3, zero3 );
  131. var b = new THREE.Box3( one3.clone().negate(), one3 );
  132. ok( a.distanceToPoint( new THREE.Vector3( 0, 0, 0 ) ) == 0, "Passed!" );
  133. ok( a.distanceToPoint( new THREE.Vector3( 1, 1, 1 ) ) == Math.sqrt( 3 ), "Passed!" );
  134. ok( a.distanceToPoint( new THREE.Vector3( -1, -1, -1 ) ) == Math.sqrt( 3 ), "Passed!" );
  135. ok( b.distanceToPoint( new THREE.Vector3( 2, 2, 2 ) ) == Math.sqrt( 3 ), "Passed!" );
  136. ok( b.distanceToPoint( new THREE.Vector3( 1, 1, 1 ) ) == 0, "Passed!" );
  137. ok( b.distanceToPoint( new THREE.Vector3( 0, 0, 0 ) ) == 0, "Passed!" );
  138. ok( b.distanceToPoint( new THREE.Vector3( -1, -1, -1 ) ) == 0, "Passed!" );
  139. ok( b.distanceToPoint( new THREE.Vector3( -2, -2, -2 ) ) == Math.sqrt( 3 ), "Passed!" );
  140. });
  141. test( "distanceToPoint", function() {
  142. var a = new THREE.Box3( zero3, zero3 );
  143. var b = new THREE.Box3( one3.clone().negate(), one3 );
  144. ok( a.distanceToPoint( new THREE.Vector3( 0, 0, 0 ) ) == 0, "Passed!" );
  145. ok( a.distanceToPoint( new THREE.Vector3( 1, 1, 1 ) ) == Math.sqrt( 3 ), "Passed!" );
  146. ok( a.distanceToPoint( new THREE.Vector3( -1, -1, -1 ) ) == Math.sqrt( 3 ), "Passed!" );
  147. ok( b.distanceToPoint( new THREE.Vector3( 2, 2, 2 ) ) == Math.sqrt( 3 ), "Passed!" );
  148. ok( b.distanceToPoint( new THREE.Vector3( 1, 1, 1 ) ) == 0, "Passed!" );
  149. ok( b.distanceToPoint( new THREE.Vector3( 0, 0, 0 ) ) == 0, "Passed!" );
  150. ok( b.distanceToPoint( new THREE.Vector3( -1, -1, -1 ) ) == 0, "Passed!" );
  151. ok( b.distanceToPoint( new THREE.Vector3( -2, -2, -2 ) ) == Math.sqrt( 3 ), "Passed!" );
  152. });
  153. test( "isIntersection", function() {
  154. var a = new THREE.Box3( zero3 );
  155. var b = new THREE.Box3( zero3, one3 );
  156. var c = new THREE.Box3( one3.clone().negate(), one3 );
  157. ok( a.isIntersection( a ), "Passed!" );
  158. ok( a.isIntersection( b ), "Passed!" );
  159. ok( a.isIntersection( c ), "Passed!" );
  160. ok( b.isIntersection( a ), "Passed!" );
  161. ok( c.isIntersection( a ), "Passed!" );
  162. ok( b.isIntersection( c ), "Passed!" );
  163. b.translate( new THREE.Vector3( 2, 2, 2 ) );
  164. ok( ! a.isIntersection( b ), "Passed!" );
  165. ok( ! b.isIntersection( a ), "Passed!" );
  166. ok( ! c.isIntersection( a ), "Passed!" );
  167. ok( ! b.isIntersection( c ), "Passed!" );
  168. });
  169. test( "intersect", function() {
  170. var a = new THREE.Box3( zero3 );
  171. var b = new THREE.Box3( zero3, one3 );
  172. var c = new THREE.Box3( one3.clone().negate(), one3 );
  173. ok( a.clone().intersect( a ).equals( a ), "Passed!" );
  174. ok( a.clone().intersect( b ).equals( a ), "Passed!" );
  175. ok( b.clone().intersect( b ).equals( b ), "Passed!" );
  176. ok( a.clone().intersect( c ).equals( a ), "Passed!" );
  177. ok( b.clone().intersect( c ).equals( b ), "Passed!" );
  178. ok( c.clone().intersect( c ).equals( c ), "Passed!" );
  179. });
  180. test( "union", function() {
  181. var a = new THREE.Box3( zero3 );
  182. var b = new THREE.Box3( zero3, one3 );
  183. var c = new THREE.Box3( one3.clone().negate(), one3 );
  184. ok( a.clone().union( a ).equals( a ), "Passed!" );
  185. ok( a.clone().union( b ).equals( b ), "Passed!" );
  186. ok( a.clone().union( c ).equals( c ), "Passed!" );
  187. ok( b.clone().union( c ).equals( c ), "Passed!" );
  188. });
  189. test( "translate", function() {
  190. var a = new THREE.Box3( zero3 );
  191. var b = new THREE.Box3( zero3, one3 );
  192. var c = new THREE.Box3( one3.clone().negate(), one3 );
  193. var d = new THREE.Box3( one3.clone().negate(), zero3 );
  194. ok( a.clone().translate( one3 ).equals( new THREE.Box3( one3, one3 ) ), "Passed!" );
  195. ok( a.clone().translate( one3 ).translate( one3.clone().negate() ).equals( a ), "Passed!" );
  196. ok( d.clone().translate( one3 ).equals( b ), "Passed!" );
  197. ok( b.clone().translate( one3.clone().negate() ).equals( d ), "Passed!" );
  198. });
  199. test( "scale", function() {
  200. var a = new THREE.Box3( zero3 );
  201. var b = new THREE.Box3( zero3, one3 );
  202. var c = new THREE.Box3( one3.clone().negate(), one3 );
  203. var d = new THREE.Box3( one3.clone().negate(), zero3 );
  204. ok( a.clone().scale( 0 ).equals( a ), "Passed!" );
  205. ok( c.clone().scale( 0 ).equals( a ), "Passed!" );
  206. ok( b.clone().scale( 3 ).equals( new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 2, 2, 2 ) ) ), "Passed!" );
  207. ok( d.clone().scale( 3 ).equals( new THREE.Box3( new THREE.Vector3( 2, 2, 2 ).negate(), new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
  208. });