Ray.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.clone(), one3.clone() );
  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.clone(), one3.clone() );
  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.clone(), 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/clone", function() {
  37. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  38. ok( a.recast( 0 ).equals( a ), "Passed!" );
  39. var b = a.clone();
  40. ok( b.recast( -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.recast( 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.clone().recast( 1 );
  45. ok( d.equals( a ), "Passed!" );
  46. ok( ! e.equals( d ), "Passed!" );
  47. ok( e.equals( c ), "Passed!" );
  48. });
  49. test( "closestPointToPoint", function() {
  50. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  51. // behind the ray
  52. var b = a.closestPointToPoint( zero3 );
  53. ok( b.equals( one3 ), "Passed!" );
  54. // front of the ray
  55. var c = a.closestPointToPoint( new THREE.Vector3( 0, 0, 50 ) );
  56. ok( c.equals( new THREE.Vector3( 1, 1, 50 ) ), "Passed!" );
  57. // exactly on the ray
  58. var d = a.closestPointToPoint( one3 );
  59. ok( d.equals( one3 ), "Passed!" );
  60. });
  61. test( "distanceToPoint", function() {
  62. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  63. // behind the ray
  64. var b = a.distanceToPoint( zero3 );
  65. ok( b === Math.sqrt( 3 ), "Passed!" );
  66. // front of the ray
  67. var c = a.distanceToPoint( new THREE.Vector3( 0, 0, 50 ) );
  68. ok( c === Math.sqrt( 2 ), "Passed!" );
  69. // exactly on the ray
  70. var d = a.distanceToPoint( one3 );
  71. ok( d === 0, "Passed!" );
  72. });
  73. test( "distanceSqToPoint", function() {
  74. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  75. // behind the ray
  76. var b = a.distanceSqToPoint( zero3 );
  77. ok( b === 3, "Passed!" );
  78. // front of the ray
  79. var c = a.distanceSqToPoint( new THREE.Vector3( 0, 0, 50 ) );
  80. ok( c === 2, "Passed!" );
  81. // exactly on the ray
  82. var d = a.distanceSqToPoint( one3 );
  83. ok( d === 0, "Passed!" );
  84. });
  85. test( "intersectsSphere", function() {
  86. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  87. var b = new THREE.Sphere( zero3, 0.5 );
  88. var c = new THREE.Sphere( zero3, 1.5 );
  89. var d = new THREE.Sphere( one3, 0.1 );
  90. var e = new THREE.Sphere( two3, 0.1 );
  91. var f = new THREE.Sphere( two3, 1 );
  92. ok( ! a.intersectsSphere( b ), "Passed!" );
  93. ok( ! a.intersectsSphere( c ), "Passed!" );
  94. ok( a.intersectsSphere( d ), "Passed!" );
  95. ok( ! a.intersectsSphere( e ), "Passed!" );
  96. ok( ! a.intersectsSphere( f ), "Passed!" );
  97. });
  98. test( "intersectSphere", function() {
  99. var TOL = 0.0001;
  100. // ray a0 origin located at ( 0, 0, 0 ) and points outward in negative-z direction
  101. var a0 = new THREE.Ray( zero3.clone(), new THREE.Vector3( 0, 0, -1 ) );
  102. // ray a1 origin located at ( 1, 1, 1 ) and points left in negative-x direction
  103. var a1 = new THREE.Ray( one3.clone(), new THREE.Vector3( -1, 0, 0 ) );
  104. // sphere (radius of 2) located behind ray a0, should result in null
  105. var b = new THREE.Sphere( new THREE.Vector3( 0, 0, 3 ), 2 );
  106. ok( a0.intersectSphere( b ) === null, "Passed!" );
  107. // sphere (radius of 2) located in front of, but too far right of ray a0, should result in null
  108. var b = new THREE.Sphere( new THREE.Vector3( 3, 0, -1 ), 2 );
  109. ok( a0.intersectSphere( b ) === null, "Passed!" );
  110. // sphere (radius of 2) located below ray a1, should result in null
  111. var b = new THREE.Sphere( new THREE.Vector3( 1, -2, 1 ), 2 );
  112. ok( a1.intersectSphere( b ) === null, "Passed!" );
  113. // sphere (radius of 1) located to the left of ray a1, should result in intersection at 0, 1, 1
  114. var b = new THREE.Sphere( new THREE.Vector3( -1, 1, 1 ), 1 );
  115. ok( a1.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 1, 1 ) ) < TOL, "Passed!" );
  116. // sphere (radius of 1) located in front of ray a0, should result in intersection at 0, 0, -1
  117. var b = new THREE.Sphere( new THREE.Vector3( 0, 0, -2 ), 1 );
  118. ok( a0.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 0, -1 ) ) < TOL, "Passed!" );
  119. // sphere (radius of 2) located in front & right of ray a0, should result in intersection at 0, 0, -1, or left-most edge of sphere
  120. var b = new THREE.Sphere( new THREE.Vector3( 2, 0, -1 ), 2 );
  121. ok( a0.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 0, -1 ) ) < TOL, "Passed!" );
  122. // same situation as above, but move the sphere a fraction more to the right, and ray a0 should now just miss
  123. var b = new THREE.Sphere( new THREE.Vector3( 2.01, 0, -1 ), 2 );
  124. ok( a0.intersectSphere( b ) === null, "Passed!" );
  125. // following tests are for situations where the ray origin is inside the sphere
  126. // sphere (radius of 1) center located at ray a0 origin / sphere surrounds the ray origin, so the first intersect point 0, 0, 1,
  127. // is behind ray a0. Therefore, second exit point on back of sphere will be returned: 0, 0, -1
  128. // thus keeping the intersection point always in front of the ray.
  129. var b = new THREE.Sphere( zero3.clone(), 1 );
  130. ok( a0.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 0, -1 ) ) < TOL, "Passed!" );
  131. // sphere (radius of 4) center located behind ray a0 origin / sphere surrounds the ray origin, so the first intersect point 0, 0, 5,
  132. // is behind ray a0. Therefore, second exit point on back of sphere will be returned: 0, 0, -3
  133. // thus keeping the intersection point always in front of the ray.
  134. var b = new THREE.Sphere( new THREE.Vector3( 0, 0, 1 ), 4 );
  135. ok( a0.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 0, -3 ) ) < TOL, "Passed!" );
  136. // sphere (radius of 4) center located in front of ray a0 origin / sphere surrounds the ray origin, so the first intersect point 0, 0, 3,
  137. // is behind ray a0. Therefore, second exit point on back of sphere will be returned: 0, 0, -5
  138. // thus keeping the intersection point always in front of the ray.
  139. var b = new THREE.Sphere( new THREE.Vector3( 0, 0, -1 ), 4 );
  140. ok( a0.intersectSphere( b ).distanceTo( new THREE.Vector3( 0, 0, -5 ) ) < TOL, "Passed!" );
  141. });
  142. test( "intersectsPlane", function() {
  143. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  144. // parallel plane in front of the ray
  145. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, -1 ) ) );
  146. ok( a.intersectsPlane( b ), "Passed!" );
  147. // parallel plane coincident with origin
  148. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, 0 ) ) );
  149. ok( a.intersectsPlane( c ), "Passed!" );
  150. // parallel plane behind the ray
  151. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, 1 ) ) );
  152. ok( ! a.intersectsPlane( d ), "Passed!" );
  153. // perpendical ray that overlaps exactly
  154. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  155. ok( a.intersectsPlane( e ), "Passed!" );
  156. // perpendical ray that doesn't overlap
  157. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  158. ok( ! a.intersectsPlane( f ), "Passed!" );
  159. });
  160. test( "intersectPlane", function() {
  161. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  162. // parallel plane behind
  163. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, -1 ) );
  164. ok( a.intersectPlane( b ) === null, "Passed!" );
  165. // parallel plane coincident with origin
  166. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 0 ) );
  167. ok( a.intersectPlane( c ) === null, "Passed!" );
  168. // parallel plane infront
  169. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 1 ) );
  170. ok( a.intersectPlane( d ).equals( a.origin ), "Passed!" );
  171. // perpendical ray that overlaps exactly
  172. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  173. ok( a.intersectPlane( e ).equals( a.origin ), "Passed!" );
  174. // perpendical ray that doesn't overlap
  175. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  176. ok( a.intersectPlane( f ) === null, "Passed!" );
  177. });
  178. test( "applyMatrix4", function() {
  179. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  180. var m = new THREE.Matrix4();
  181. ok( a.clone().applyMatrix4( m ).equals( a ), "Passed!" );
  182. a = new THREE.Ray( zero3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  183. m.makeRotationZ( Math.PI );
  184. ok( a.clone().applyMatrix4( m ).equals( a ), "Passed!" );
  185. m.makeRotationX( Math.PI );
  186. var b = a.clone();
  187. b.direction.negate();
  188. var a2 = a.clone().applyMatrix4( m );
  189. ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
  190. ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
  191. a.origin = new THREE.Vector3( 0, 0, 1 );
  192. b.origin = new THREE.Vector3( 0, 0, -1 );
  193. var a2 = a.clone().applyMatrix4( m );
  194. ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
  195. ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
  196. });
  197. test( "distanceSqToSegment", function() {
  198. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  199. var ptOnLine = new THREE.Vector3();
  200. var ptOnSegment = new THREE.Vector3();
  201. //segment in front of the ray
  202. var v0 = new THREE.Vector3( 3, 5, 50 );
  203. var v1 = new THREE.Vector3( 50, 50, 50 ); // just a far away point
  204. var distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  205. ok( ptOnSegment.distanceTo( v0 ) < 0.0001, "Passed!" );
  206. ok( ptOnLine.distanceTo( new THREE.Vector3(1, 1, 50) ) < 0.0001, "Passed!" );
  207. // ((3-1) * (3-1) + (5-1) * (5-1) = 4 + 16 = 20
  208. ok( Math.abs( distSqr - 20 ) < 0.0001, "Passed!" );
  209. //segment behind the ray
  210. v0 = new THREE.Vector3( -50, -50, -50 ); // just a far away point
  211. v1 = new THREE.Vector3( -3, -5, -4 );
  212. distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  213. ok( ptOnSegment.distanceTo( v1 ) < 0.0001, "Passed!" );
  214. ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );
  215. // ((-3-1) * (-3-1) + (-5-1) * (-5-1) + (-4-1) + (-4-1) = 16 + 36 + 25 = 77
  216. ok( Math.abs( distSqr - 77 ) < 0.0001, "Passed!" );
  217. //exact intersection between the ray and the segment
  218. v0 = new THREE.Vector3( -50, -50, -50 );
  219. v1 = new THREE.Vector3( 50, 50, 50 );
  220. distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  221. ok( ptOnSegment.distanceTo( one3 ) < 0.0001, "Passed!" );
  222. ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );
  223. ok( distSqr < 0.0001, "Passed!" );
  224. });
  225. test( "intersectBox", function() {
  226. var TOL = 0.0001;
  227. var box = new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 1, 1, 1 ) );
  228. var a = new THREE.Ray( new THREE.Vector3( -2, 0, 0 ), new THREE.Vector3( 1, 0, 0) );
  229. //ray should intersect box at -1,0,0
  230. ok( a.intersectsBox(box) === true, "Passed!" );
  231. ok( a.intersectBox(box).distanceTo( new THREE.Vector3( -1, 0, 0 ) ) < TOL, "Passed!" );
  232. var b = new THREE.Ray( new THREE.Vector3( -2, 0, 0 ), new THREE.Vector3( -1, 0, 0) );
  233. //ray is point away from box, it should not intersect
  234. ok( b.intersectsBox(box) === false, "Passed!" );
  235. ok( b.intersectBox(box) === null, "Passed!" );
  236. var c = new THREE.Ray( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0) );
  237. // ray is inside box, should return exit point
  238. ok( c.intersectsBox(box) === true, "Passed!" );
  239. ok( c.intersectBox(box).distanceTo( new THREE.Vector3( 1, 0, 0 ) ) < TOL, "Passed!" );
  240. var d = new THREE.Ray( new THREE.Vector3( 0, 2, 1 ), new THREE.Vector3( 0, -1, -1).normalize() );
  241. //tilted ray should intersect box at 0,1,0
  242. ok( d.intersectsBox(box) === true, "Passed!" );
  243. ok( d.intersectBox(box).distanceTo( new THREE.Vector3( 0, 1, 0 ) ) < TOL, "Passed!" );
  244. var e = new THREE.Ray( new THREE.Vector3( 1, -2, 1 ), new THREE.Vector3( 0, 1, 0).normalize() );
  245. //handle case where ray is coplanar with one of the boxes side - box in front of ray
  246. ok( e.intersectsBox(box) === true, "Passed!" );
  247. ok( e.intersectBox(box).distanceTo( new THREE.Vector3( 1, -1, 1 ) ) < TOL, "Passed!" );
  248. var f = new THREE.Ray( new THREE.Vector3( 1, -2, 0 ), new THREE.Vector3( 0, -1, 0).normalize() );
  249. //handle case where ray is coplanar with one of the boxes side - box behind ray
  250. ok( f.intersectsBox(box) === false, "Passed!" );
  251. ok( f.intersectBox(box) == null, "Passed!" );
  252. });