Ray.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. module( "Ray" );
  5. test( "constructor/equals", function() {
  6. var a = new THREE.Ray();
  7. ok( a.origin.equals( zero3 ), "Passed!" );
  8. ok( a.direction.equals( zero3 ), "Passed!" );
  9. a = new THREE.Ray( two3, one3 );
  10. ok( a.origin.equals( two3 ), "Passed!" );
  11. ok( a.direction.equals( one3 ), "Passed!" );
  12. });
  13. test( "copy/equals", function() {
  14. var a = new THREE.Ray( zero3, one3 );
  15. var b = new THREE.Ray().copy( a );
  16. ok( b.origin.equals( zero3 ), "Passed!" );
  17. ok( b.direction.equals( one3 ), "Passed!" );
  18. // ensure that it is a true copy
  19. a.origin = zero3;
  20. a.direction = one3;
  21. ok( b.origin.equals( zero3 ), "Passed!" );
  22. ok( b.direction.equals( one3 ), "Passed!" );
  23. });
  24. test( "set", function() {
  25. var a = new THREE.Ray();
  26. a.set( one3, one3 )
  27. ok( a.origin.equals( one3 ), "Passed!" );
  28. ok( a.direction.equals( one3 ), "Passed!" );
  29. });
  30. test( "at", function() {
  31. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  32. ok( a.at( 0 ).equals( one3 ), "Passed!" );
  33. ok( a.at( -1 ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  34. ok( a.at( 1 ).equals( new THREE.Vector3( 1, 1, 2 ) ), "Passed!" );
  35. });
  36. test( "recast/recastSelf/clone", function() {
  37. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  38. ok( a.recastSelf( 0 ).equals( a ), "Passed!" );
  39. var b = a.clone();
  40. ok( b.recastSelf( -1 ).equals( new THREE.Ray( new THREE.Vector3( 1, 1, 0 ), new THREE.Vector3( 0, 0, 1 ) ) ), "Passed!" );
  41. var c = a.clone();
  42. ok( c.recastSelf( 1 ).equals( new THREE.Ray( new THREE.Vector3( 1, 1, 2 ), new THREE.Vector3( 0, 0, 1 ) ) ), "Passed!" );
  43. var d = a.clone();
  44. var e = d.recast( 1 );
  45. ok( d.equals( a ), "Passed!" );
  46. ok( ! e.equals( d ), "Passed!" );
  47. ok( e.equals( c ), "Passed!" );
  48. });
  49. test( "flip", function() {
  50. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  51. var b = a.clone();
  52. b.flip();
  53. ok( b.direction.equals( new THREE.Vector3( 0, 0, -1 ) ), "Passed!" );
  54. ok( ! b.equals( a ), "Passed!" );
  55. // and let's flip back to original direction
  56. b.flip();
  57. ok( b.equals( a ), "Passed!" );
  58. });
  59. test( "closestPointToPoint", function() {
  60. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  61. // nearby the ray
  62. var b = a.closestPointToPoint( zero3 );
  63. ok( b.equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  64. // exactly on the ray
  65. var c = a.closestPointToPoint( one3 );
  66. ok( c.equals( one3 ), "Passed!" );
  67. });
  68. test( "distanceToPoint", function() {
  69. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  70. // nearby the ray
  71. var b = a.distanceToPoint( zero3 );
  72. ok( b == Math.sqrt( 2 ), "Passed!" );
  73. // exactly on the ray
  74. var c = a.distanceToPoint( one3 );
  75. ok( c == 0, "Passed!" );
  76. });
  77. /*
  78. test( "distanceToRay", function() {
  79. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  80. // parallel ray
  81. var b = new THREE.Ray( zero3, new THREE.Vector3( 0, 0, 1 ) );
  82. console.log( a );
  83. console.log( b );
  84. console.log( a.distanceToRay( b ) );
  85. console.log( a.closestPointToRay( b ) );
  86. ok( a.distanceToRay( b ) == Math.sqrt( 2 ), "Passed!" );
  87. // perpendical ray that intersects
  88. var c = new THREE.Ray( one3, new THREE.Vector3( 1, 0, 0 ) );
  89. ok( a.distanceToRay( c ) == 0, "Passed!" );
  90. // perpendical ray that doesn't intersects
  91. var d = new THREE.Ray( one3.clone().subSelf( new THREE.Vector3( 0, 0, -1 ) ), new THREE.Vector3( 1, 0, 0 ) );
  92. ok( a.distanceToRay( d ) == 1, "Passed!" );
  93. });
  94. test( "closestPointToRay", function() {
  95. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  96. // parallel ray
  97. var b = new THREE.Ray( zero3, new THREE.Vector3( 0, 0, 1 ) );
  98. ok( a.closestPointToRay( b ).equals( one3 ), "Passed!" );
  99. // perpendical ray that intersects
  100. var c = new THREE.Ray( one3, new THREE.Vector3( 1, 0, 0 ) );
  101. ok( a.closestPointToRay( c ).equals( zero3 ), "Passed!" );
  102. // perpendical ray that doesn't intersects
  103. var d = new THREE.Ray( one3.clone().subSelf( new THREE.Vector3( 0, 0, -1 ) ), new THREE.Vector3( 1, 0, 0 ) );
  104. ok( a.closestPointToRay( d ).equals( new THREE.Vector3( 0, 0, 1 ) ), "Passed!" );
  105. });
  106. */
  107. test( "isIntersectionSphere", function() {
  108. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  109. var b = new THREE.Sphere( zero3, 0.5 );
  110. var c = new THREE.Sphere( zero3, 1.5 );
  111. var d = new THREE.Sphere( one3, 0.1 );
  112. var e = new THREE.Sphere( two3, 0.1 );
  113. var f = new THREE.Sphere( two3, 1 );
  114. ok( ! a.isIntersectionSphere( b ), "Passed!" );
  115. ok( a.isIntersectionSphere( c ), "Passed!" );
  116. ok( a.isIntersectionSphere( d ), "Passed!" );
  117. ok( ! a.isIntersectionSphere( e ), "Passed!" );
  118. ok( ! a.isIntersectionSphere( f ), "Passed!" );
  119. });
  120. test( "isIntersectionBox", function() {
  121. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  122. var b = new THREE.Box3( zero3, one3 );
  123. var c = new THREE.Box3( one3.clone().negate(), zero3 );
  124. ok( a.isIntersectionBox( b ), "Passed!" );
  125. ok( ! a.isIntersectionBox( c ), "Passed!" );
  126. });
  127. test( "intersectBox", function() {
  128. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  129. var b = new THREE.Box3( zero3, one3 );
  130. var c = new THREE.Box3( one3.clone().negate(), zero3 );
  131. ok( a.intersectBox( b ).equals( one3 ), "Passed!" );
  132. ok( a.intersectBox( c ) === undefined, "Passed!" );
  133. });
  134. test( "isIntersectionPlane", function() {
  135. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  136. // parallel plane behind
  137. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().subSelf( new THREE.Vector3( 0, 0, -1 ) ) );
  138. ok( a.isIntersectionPlane( b ), "Passed!" );
  139. // parallel plane coincident with origin
  140. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().subSelf( new THREE.Vector3( 0, 0, 0 ) ) );
  141. ok( a.isIntersectionPlane( c ), "Passed!" );
  142. // parallel plane infront
  143. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().subSelf( new THREE.Vector3( 0, 0, 1 ) ) );
  144. ok( a.isIntersectionPlane( d ), "Passed!" );
  145. // perpendical ray that overlaps exactly
  146. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  147. ok( a.isIntersectionPlane( e ), "Passed!" );
  148. // perpendical ray that doesn't overlap
  149. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  150. ok( ! a.isIntersectionPlane( f ), "Passed!" );
  151. });
  152. test( "intersectPlane", function() {
  153. var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
  154. // parallel plane behind
  155. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, -1 ) );
  156. ok( a.intersectPlane( b ).equals( new THREE.Vector3( 1, 1, -1 ) ), "Passed!" );
  157. // parallel plane coincident with origin
  158. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 0 ) );
  159. ok( a.intersectPlane( c ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  160. // parallel plane infront
  161. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 1 ) );
  162. ok( a.intersectPlane( d ).equals( new THREE.Vector3( 1, 1, 1 ) ), "Passed!" );
  163. // perpendical ray that overlaps exactly
  164. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  165. ok( a.intersectPlane( e ).equals( a.origin ), "Passed!" );
  166. // perpendical ray that doesn't overlap
  167. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  168. ok( a.intersectPlane( f ) === undefined, "Passed!" );
  169. });