Ray.js 12 KB

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