Ray.js 13 KB

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