Plane.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 normal = one3.clone().normalize();
  70. var a = new THREE.Plane().setFromNormalAndCoplanarPoint( normal, zero3 );
  71. ok( a.normal.equals( normal ), "Passed!" );
  72. ok( a.constant == 0, "Passed!" );
  73. });
  74. test( "normalize", function() {
  75. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), 2 );
  76. a.normalize();
  77. ok( a.normal.length() == 1, "Passed!" );
  78. ok( a.normal.equals( new THREE.Vector3( 1, 0, 0 ) ), "Passed!" );
  79. ok( a.constant == 1, "Passed!" );
  80. });
  81. test( "negate/distanceToPoint", function() {
  82. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
  83. a.normalize();
  84. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  85. ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  86. a.negate();
  87. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === -3, "Passed!" );
  88. ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  89. });
  90. test( "distanceToPoint", function() {
  91. var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
  92. a.normalize();
  93. ok( a.distanceToPoint( a.projectPoint( zero3.clone() ) ) === 0, "Passed!" );
  94. ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  95. });
  96. test( "distanceToSphere", function() {
  97. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  98. var b = new THREE.Sphere( new THREE.Vector3( 2, 0, 0 ), 1 );
  99. ok( a.distanceToSphere( b ) === 1, "Passed!" );
  100. a.set( new THREE.Vector3( 1, 0, 0 ), 2 );
  101. ok( a.distanceToSphere( b ) === 3, "Passed!" );
  102. a.set( new THREE.Vector3( 1, 0, 0 ), -2 );
  103. ok( a.distanceToSphere( b ) === -1, "Passed!" );
  104. });
  105. test( "isInterestionLine/intersectLine", function() {
  106. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  107. var l1 = new THREE.Line3( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) );
  108. ok( a.isIntersectionLine( l1 ), "Passed!" );
  109. ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  110. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -3 );
  111. ok( a.isIntersectionLine( l1 ), "Passed!" );
  112. ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 3, 0, 0 ) ), "Passed!" );
  113. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -11 );
  114. ok( ! a.isIntersectionLine( l1 ), "Passed!" );
  115. ok( a.intersectLine( l1 ) === undefined, "Passed!" );
  116. a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 11 );
  117. ok( ! a.isIntersectionLine( l1 ), "Passed!" );
  118. ok( a.intersectLine( l1 ) === undefined, "Passed!" );
  119. });
  120. test( "projectPoint", function() {
  121. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  122. ok( a.projectPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( zero3 ), "Passed!" );
  123. ok( a.projectPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( zero3 ), "Passed!" );
  124. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  125. ok( a.projectPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  126. ok( a.projectPoint( new THREE.Vector3( 0, 1, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  127. });
  128. test( "orthoPoint", function() {
  129. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  130. ok( a.orthoPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
  131. ok( a.orthoPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( new THREE.Vector3( -10, 0, 0 ) ), "Passed!" );
  132. });
  133. test( "coplanarPoint", function() {
  134. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  135. ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
  136. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  137. ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
  138. });
  139. test( "applyMatrix4/translate", function() {
  140. var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
  141. var m = new THREE.Matrix4();
  142. m.makeRotationZ( Math.PI * 0.5 );
  143. ok( comparePlane( a.clone().applyMatrix4( m ), new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 0 ) ), "Passed!" );
  144. a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
  145. ok( comparePlane( a.clone().applyMatrix4( m ), new THREE.Plane( new THREE.Vector3( -1, 0, 0 ), -1 ) ), "Passed!" );
  146. m.makeTranslation( 1, 1, 1 );
  147. ok( comparePlane( a.clone().applyMatrix4( m ), a.clone().translate( new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
  148. });