Plane.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. module( "Plane" );
  5. var comparePlane = function ( a, b, threshold ) {
  6. threshold = threshold || 0.0001;
  7. return ( a.normal.distanceTo( b.normal ) < threshold &&
  8. Math.abs( a.constant - b.constant ) < threshold );
  9. };
  10. test( "constructor", function() {
  11. var a = new THREE.Plane();
  12. ok( a.normal.x == 1, "Passed!" );
  13. ok( a.normal.y == 0, "Passed!" );
  14. ok( a.normal.z == 0, "Passed!" );
  15. ok( a.constant == 0, "Passed!" );
  16. a = new THREE.Plane( one3.clone(), 0 );
  17. ok( a.normal.x == 1, "Passed!" );
  18. ok( a.normal.y == 1, "Passed!" );
  19. ok( a.normal.z == 1, "Passed!" );
  20. ok( a.constant == 0, "Passed!" );
  21. a = new THREE.Plane( one3.clone(), 1 );
  22. ok( a.normal.x == 1, "Passed!" );
  23. ok( a.normal.y == 1, "Passed!" );
  24. ok( a.normal.z == 1, "Passed!" );
  25. ok( a.constant == 1, "Passed!" );
  26. });
  27. test( "copy", function() {
  28. var a = new THREE.Plane( new THREE.Vector3( x, y, z ), w );
  29. var b = new THREE.Plane().copy( a );
  30. ok( b.normal.x == x, "Passed!" );
  31. ok( b.normal.y == y, "Passed!" );
  32. ok( b.normal.z == z, "Passed!" );
  33. ok( b.constant == w, "Passed!" );
  34. // ensure that it is a true copy
  35. a.normal.x = 0;
  36. a.normal.y = -1;
  37. a.normal.z = -2;
  38. a.constant = -3;
  39. ok( b.normal.x == x, "Passed!" );
  40. ok( b.normal.y == y, "Passed!" );
  41. ok( b.normal.z == z, "Passed!" );
  42. ok( b.constant == w, "Passed!" );
  43. });
  44. test( "set", function() {
  45. var a = new THREE.Plane();
  46. ok( a.normal.x == 1, "Passed!" );
  47. ok( a.normal.y == 0, "Passed!" );
  48. ok( a.normal.z == 0, "Passed!" );
  49. ok( a.constant == 0, "Passed!" );
  50. var b = a.clone().set( new THREE.Vector3( x, y, z ), w );
  51. ok( b.normal.x == x, "Passed!" );
  52. ok( b.normal.y == y, "Passed!" );
  53. ok( b.normal.z == z, "Passed!" );
  54. ok( b.constant == w, "Passed!" );
  55. });
  56. test( "setComponents", function() {
  57. var a = new THREE.Plane();
  58. ok( a.normal.x == 1, "Passed!" );
  59. ok( a.normal.y == 0, "Passed!" );
  60. ok( a.normal.z == 0, "Passed!" );
  61. ok( a.constant == 0, "Passed!" );
  62. var b = a.clone().setComponents( x, y, z , w );
  63. ok( b.normal.x == x, "Passed!" );
  64. ok( b.normal.y == y, "Passed!" );
  65. ok( b.normal.z == z, "Passed!" );
  66. ok( b.constant == w, "Passed!" );
  67. });
  68. test( "setFromNormalAndCoplanarPoint", function() {
  69. var a = new THREE.Plane().setFromNormalAndCoplanarPoint( one3, zero3 );
  70. ok( a.normal.equals( one3.clone().normalize() ), "Passed!" );
  71. ok( a.constant == 0, "Passed!" );
  72. });
  73. test( "normalize", function() {
  74. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), 2 );
  75. a.normalize();
  76. ok( a.normal.length() == 1, "Passed!" );
  77. ok( a.normal.equals( new THREE.Vector3( 1, 0, 0 ) ), "Passed!" );
  78. ok( a.constant == 1, "Passed!" );
  79. });
  80. test( "negate/distanceToPoint", function() {
  81. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
  82. a.normalize();
  83. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  84. ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  85. a.negate();
  86. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === -3, "Passed!" );
  87. ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  88. });
  89. test( "distanceToPoint", function() {
  90. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
  91. a.normalize();
  92. ok( a.distanceToPoint( a.projectPoint( zero3.clone() ) ) === 0, "Passed!" );
  93. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  94. });
  95. test( "distanceToSphere", function() {
  96. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  97. var b = new THREE.Sphere( new THREE.Vector3( 2, 0, 0 ), 1 );
  98. ok( a.distanceToSphere( b ) === 1, "Passed!" );
  99. a.set( new THREE.Vector3( 1, 0, 0 ), 2 );
  100. ok( a.distanceToSphere( b ) === 3, "Passed!" );
  101. a.set( new THREE.Vector3( 1, 0, 0 ), -2 );
  102. ok( a.distanceToSphere( b ) === -1, "Passed!" );
  103. });
  104. test( "isInterestionLine/intersectLine", function() {
  105. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  106. ok( a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  107. ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  108. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -3 );
  109. ok( a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  110. ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 3, 0, 0 ) ), "Passed!" );
  111. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -11 );
  112. ok( ! a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  113. ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ) === undefined, "Passed!" );
  114. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 11 );
  115. ok( ! a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  116. ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ) === undefined, "Passed!" );
  117. });
  118. test( "projectPoint", function() {
  119. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  120. ok( a.projectPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( zero3 ), "Passed!" );
  121. ok( a.projectPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( zero3 ), "Passed!" );
  122. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  123. ok( a.projectPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  124. ok( a.projectPoint( new THREE.Vector3( 0, 1, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  125. });
  126. test( "orthoPoint", function() {
  127. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  128. ok( a.orthoPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  129. ok( a.orthoPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( new THREE.Vector3( -10, 0, 0 ) ), "Passed!" );
  130. });
  131. test( "coplanarPoint", function() {
  132. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  133. ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
  134. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  135. ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
  136. });
  137. test( "transform/translate", function() {
  138. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  139. var m = new THREE.Matrix4();
  140. m.makeRotationZ( Math.PI * 0.5 );
  141. ok( comparePlane( a.clone().transform( m ), new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 0 ) ), "Passed!" );
  142. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  143. ok( comparePlane( a.clone().transform( m ), new THREE.Plane( new THREE.Vector3( -1, 0, 0 ), -1 ) ), "Passed!" );
  144. m.makeTranslation( new THREE.Vector3( 1, 1, 1 ) );
  145. ok( comparePlane( a.clone().transform( m ), a.clone().translate( new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
  146. });