Ray.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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( "isIntersectionPlane", function() {
  87. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  88. // parallel plane in front of the ray
  89. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, -1 ) ) );
  90. ok( a.isIntersectionPlane( b ), "Passed!" );
  91. // parallel plane coincident with origin
  92. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, 0 ) ) );
  93. ok( a.isIntersectionPlane( c ), "Passed!" );
  94. // parallel plane behind the ray
  95. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), one3.clone().sub( new THREE.Vector3( 0, 0, 1 ) ) );
  96. ok( ! a.isIntersectionPlane( d ), "Passed!" );
  97. // perpendical ray that overlaps exactly
  98. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  99. ok( a.isIntersectionPlane( e ), "Passed!" );
  100. // perpendical ray that doesn't overlap
  101. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  102. ok( ! a.isIntersectionPlane( f ), "Passed!" );
  103. });
  104. test( "intersectPlane", function() {
  105. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  106. // parallel plane behind
  107. var b = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, -1 ) );
  108. ok( a.intersectPlane( b ) === null, "Passed!" );
  109. // parallel plane coincident with origin
  110. var c = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 0 ) );
  111. ok( a.intersectPlane( c ) === null, "Passed!" );
  112. // parallel plane infront
  113. var d = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 1, 1, 1 ) );
  114. ok( a.intersectPlane( d ).equals( a.origin ), "Passed!" );
  115. // perpendical ray that overlaps exactly
  116. var e = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), one3 );
  117. ok( a.intersectPlane( e ).equals( a.origin ), "Passed!" );
  118. // perpendical ray that doesn't overlap
  119. var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
  120. ok( a.intersectPlane( f ) === null, "Passed!" );
  121. });
  122. test( "applyMatrix4", function() {
  123. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  124. var m = new THREE.Matrix4();
  125. ok( a.clone().applyMatrix4( m ).equals( a ), "Passed!" );
  126. a = new THREE.Ray( zero3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  127. m.makeRotationZ( Math.PI );
  128. ok( a.clone().applyMatrix4( m ).equals( a ), "Passed!" );
  129. m.makeRotationX( Math.PI );
  130. var b = a.clone();
  131. b.direction.negate();
  132. var a2 = a.clone().applyMatrix4( m );
  133. ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
  134. ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
  135. a.origin = new THREE.Vector3( 0, 0, 1 );
  136. b.origin = new THREE.Vector3( 0, 0, -1 );
  137. var a2 = a.clone().applyMatrix4( m );
  138. ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
  139. ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
  140. });
  141. test( "distanceSqToSegment", function() {
  142. var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
  143. var ptOnLine = new THREE.Vector3();
  144. var ptOnSegment = new THREE.Vector3();
  145. //segment in front of the ray
  146. var v0 = new THREE.Vector3( 3, 5, 50 );
  147. var v1 = new THREE.Vector3( 50, 50, 50 ); // just a far away point
  148. var distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  149. ok( ptOnSegment.distanceTo( v0 ) < 0.0001, "Passed!" );
  150. ok( ptOnLine.distanceTo( new THREE.Vector3(1, 1, 50) ) < 0.0001, "Passed!" );
  151. // ((3-1) * (3-1) + (5-1) * (5-1) = 4 + 16 = 20
  152. ok( Math.abs( distSqr - 20 ) < 0.0001, "Passed!" );
  153. //segment behind the ray
  154. v0 = new THREE.Vector3( -50, -50, -50 ); // just a far away point
  155. v1 = new THREE.Vector3( -3, -5, -4 );
  156. distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  157. ok( ptOnSegment.distanceTo( v1 ) < 0.0001, "Passed!" );
  158. ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );
  159. // ((-3-1) * (-3-1) + (-5-1) * (-5-1) + (-4-1) + (-4-1) = 16 + 36 + 25 = 77
  160. ok( Math.abs( distSqr - 77 ) < 0.0001, "Passed!" );
  161. //exact intersection between the ray and the segment
  162. v0 = new THREE.Vector3( -50, -50, -50 );
  163. v1 = new THREE.Vector3( 50, 50, 50 );
  164. distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
  165. ok( ptOnSegment.distanceTo( one3 ) < 0.0001, "Passed!" );
  166. ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );
  167. ok( distSqr < 0.0001, "Passed!" );
  168. });
  169. test( "intersectBox", function() {
  170. var TOL = 0.0001;
  171. var box = new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 1, 1, 1 ) );
  172. var a = new THREE.Ray( new THREE.Vector3( -2, 0, 0 ), new THREE.Vector3( 1, 0, 0) );
  173. //ray should intersect box at -1,0,0
  174. ok( a.isIntersectionBox(box) === true, "Passed!" );
  175. ok( a.intersectBox(box).distanceTo( new THREE.Vector3( -1, 0, 0 ) ) < TOL, "Passed!" );
  176. var b = new THREE.Ray( new THREE.Vector3( -2, 0, 0 ), new THREE.Vector3( -1, 0, 0) );
  177. //ray is point away from box, it should not intersect
  178. ok( b.isIntersectionBox(box) === false, "Passed!" );
  179. ok( b.intersectBox(box) === null, "Passed!" );
  180. var c = new THREE.Ray( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0) );
  181. // ray is inside box, should return exit point
  182. ok( c.isIntersectionBox(box) === true, "Passed!" );
  183. ok( c.intersectBox(box).distanceTo( new THREE.Vector3( 1, 0, 0 ) ) < TOL, "Passed!" );
  184. var d = new THREE.Ray( new THREE.Vector3( 0, 2, 1 ), new THREE.Vector3( 0, -1, -1).normalize() );
  185. //tilted ray should intersect box at 0,1,0
  186. ok( d.isIntersectionBox(box) === true, "Passed!" );
  187. ok( d.intersectBox(box).distanceTo( new THREE.Vector3( 0, 1, 0 ) ) < TOL, "Passed!" );
  188. var e = new THREE.Ray( new THREE.Vector3( 1, -2, 1 ), new THREE.Vector3( 0, 1, 0).normalize() );
  189. //handle case where ray is coplanar with one of the boxes side - box in front of ray
  190. ok( e.isIntersectionBox(box) === true, "Passed!" );
  191. ok( e.intersectBox(box).distanceTo( new THREE.Vector3( 1, -1, 1 ) ) < TOL, "Passed!" );
  192. var f = new THREE.Ray( new THREE.Vector3( 1, -2, 0 ), new THREE.Vector3( 0, -1, 0).normalize() );
  193. //handle case where ray is coplanar with one of the boxes side - box behind ray
  194. ok( f.isIntersectionBox(box) === false, "Passed!" );
  195. ok( f.intersectBox(box) == null, "Passed!" );
  196. });