Plane.tests.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { Plane } from '../../../../src/math/Plane';
  7. import { Vector3 } from '../../../../src/math/Vector3';
  8. import { Line3 } from '../../../../src/math/Line3';
  9. import { Sphere } from '../../../../src/math/Sphere';
  10. import { Matrix4 } from '../../../../src/math/Matrix4';
  11. import {
  12. x,
  13. y,
  14. z,
  15. w,
  16. zero3,
  17. one3
  18. } from './Constants.tests';
  19. function comparePlane( a, b, threshold ) {
  20. threshold = threshold || 0.0001;
  21. return ( a.normal.distanceTo( b.normal ) < threshold &&
  22. Math.abs( a.constant - b.constant ) < threshold );
  23. }
  24. export default QUnit.module( 'Maths', () => {
  25. QUnit.module( 'Plane', () => {
  26. // INSTANCING
  27. QUnit.test( "Instancing", ( assert ) => {
  28. var a = new Plane();
  29. assert.ok( a.normal.x == 1, "Passed!" );
  30. assert.ok( a.normal.y == 0, "Passed!" );
  31. assert.ok( a.normal.z == 0, "Passed!" );
  32. assert.ok( a.constant == 0, "Passed!" );
  33. var a = new Plane( one3.clone(), 0 );
  34. assert.ok( a.normal.x == 1, "Passed!" );
  35. assert.ok( a.normal.y == 1, "Passed!" );
  36. assert.ok( a.normal.z == 1, "Passed!" );
  37. assert.ok( a.constant == 0, "Passed!" );
  38. var a = new Plane( one3.clone(), 1 );
  39. assert.ok( a.normal.x == 1, "Passed!" );
  40. assert.ok( a.normal.y == 1, "Passed!" );
  41. assert.ok( a.normal.z == 1, "Passed!" );
  42. assert.ok( a.constant == 1, "Passed!" );
  43. } );
  44. // PUBLIC STUFF
  45. QUnit.todo( "isPlane", ( assert ) => {
  46. assert.ok( false, "everything's gonna be alright" );
  47. } );
  48. QUnit.test( "set", ( assert ) => {
  49. var a = new Plane();
  50. assert.ok( a.normal.x == 1, "Passed!" );
  51. assert.ok( a.normal.y == 0, "Passed!" );
  52. assert.ok( a.normal.z == 0, "Passed!" );
  53. assert.ok( a.constant == 0, "Passed!" );
  54. var b = a.clone().set( new Vector3( x, y, z ), w );
  55. assert.ok( b.normal.x == x, "Passed!" );
  56. assert.ok( b.normal.y == y, "Passed!" );
  57. assert.ok( b.normal.z == z, "Passed!" );
  58. assert.ok( b.constant == w, "Passed!" );
  59. } );
  60. QUnit.test( "setComponents", ( assert ) => {
  61. var a = new Plane();
  62. assert.ok( a.normal.x == 1, "Passed!" );
  63. assert.ok( a.normal.y == 0, "Passed!" );
  64. assert.ok( a.normal.z == 0, "Passed!" );
  65. assert.ok( a.constant == 0, "Passed!" );
  66. var b = a.clone().setComponents( x, y, z, w );
  67. assert.ok( b.normal.x == x, "Passed!" );
  68. assert.ok( b.normal.y == y, "Passed!" );
  69. assert.ok( b.normal.z == z, "Passed!" );
  70. assert.ok( b.constant == w, "Passed!" );
  71. } );
  72. QUnit.test( "setFromNormalAndCoplanarPoint", ( assert ) => {
  73. var normal = one3.clone().normalize();
  74. var a = new Plane().setFromNormalAndCoplanarPoint( normal, zero3 );
  75. assert.ok( a.normal.equals( normal ), "Passed!" );
  76. assert.ok( a.constant == 0, "Passed!" );
  77. } );
  78. QUnit.test( "setFromCoplanarPoints", ( assert ) => {
  79. var a = new Plane();
  80. var v1 = new Vector3( 2.0, 0.5, 0.25 );
  81. var v2 = new Vector3( 2.0, - 0.5, 1.25 );
  82. var v3 = new Vector3( 2.0, - 3.5, 2.2 );
  83. var normal = new Vector3( 1, 0, 0 );
  84. var constant = - 2;
  85. a.setFromCoplanarPoints( v1, v2, v3 );
  86. assert.ok( a.normal.equals( normal ), "Check normal" );
  87. assert.strictEqual( a.constant, constant, "Check constant" );
  88. } );
  89. QUnit.todo( "clone", ( assert ) => {
  90. assert.ok( false, "everything's gonna be alright" );
  91. } );
  92. QUnit.test( "copy", ( assert ) => {
  93. var a = new Plane( new Vector3( x, y, z ), w );
  94. var b = new Plane().copy( a );
  95. assert.ok( b.normal.x == x, "Passed!" );
  96. assert.ok( b.normal.y == y, "Passed!" );
  97. assert.ok( b.normal.z == z, "Passed!" );
  98. assert.ok( b.constant == w, "Passed!" );
  99. // ensure that it is a true copy
  100. a.normal.x = 0;
  101. a.normal.y = - 1;
  102. a.normal.z = - 2;
  103. a.constant = - 3;
  104. assert.ok( b.normal.x == x, "Passed!" );
  105. assert.ok( b.normal.y == y, "Passed!" );
  106. assert.ok( b.normal.z == z, "Passed!" );
  107. assert.ok( b.constant == w, "Passed!" );
  108. } );
  109. QUnit.test( "normalize", ( assert ) => {
  110. var a = new Plane( new Vector3( 2, 0, 0 ), 2 );
  111. a.normalize();
  112. assert.ok( a.normal.length() == 1, "Passed!" );
  113. assert.ok( a.normal.equals( new Vector3( 1, 0, 0 ) ), "Passed!" );
  114. assert.ok( a.constant == 1, "Passed!" );
  115. } );
  116. QUnit.test( "negate/distanceToPoint", ( assert ) => {
  117. var a = new Plane( new Vector3( 2, 0, 0 ), - 2 );
  118. a.normalize();
  119. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  120. assert.ok( a.distanceToPoint( new Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  121. a.negate();
  122. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === - 3, "Passed!" );
  123. assert.ok( a.distanceToPoint( new Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
  124. } );
  125. QUnit.test( "distanceToPoint", ( assert ) => {
  126. var a = new Plane( new Vector3( 2, 0, 0 ), - 2 );
  127. var point = new Vector3();
  128. a.normalize().projectPoint( zero3.clone(), point );
  129. assert.ok( a.distanceToPoint( point ) === 0, "Passed!" );
  130. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
  131. } );
  132. QUnit.test( "distanceToSphere", ( assert ) => {
  133. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  134. var b = new Sphere( new Vector3( 2, 0, 0 ), 1 );
  135. assert.ok( a.distanceToSphere( b ) === 1, "Passed!" );
  136. a.set( new Vector3( 1, 0, 0 ), 2 );
  137. assert.ok( a.distanceToSphere( b ) === 3, "Passed!" );
  138. a.set( new Vector3( 1, 0, 0 ), - 2 );
  139. assert.ok( a.distanceToSphere( b ) === - 1, "Passed!" );
  140. } );
  141. QUnit.test( "projectPoint", ( assert ) => {
  142. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  143. var point = new Vector3();
  144. a.projectPoint( new Vector3( 10, 0, 0 ), point );
  145. assert.ok( point.equals( zero3 ), "Passed!" );
  146. a.projectPoint( new Vector3( - 10, 0, 0 ), point );
  147. assert.ok( point.equals( zero3 ), "Passed!" );
  148. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  149. a.projectPoint( new Vector3( 0, 0, 0 ), point );
  150. assert.ok( point.equals( new Vector3( 0, 1, 0 ) ), "Passed!" );
  151. a.projectPoint( new Vector3( 0, 1, 0 ), point );
  152. assert.ok( point.equals( new Vector3( 0, 1, 0 ) ), "Passed!" );
  153. } );
  154. QUnit.test( "isInterestionLine/intersectLine", ( assert ) => {
  155. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  156. var point = new Vector3();
  157. var l1 = new Line3( new Vector3( - 10, 0, 0 ), new Vector3( 10, 0, 0 ) );
  158. a.intersectLine( l1, point );
  159. assert.ok( point.equals( new Vector3( 0, 0, 0 ) ), "Passed!" );
  160. var a = new Plane( new Vector3( 1, 0, 0 ), - 3 );
  161. a.intersectLine( l1, point );
  162. assert.ok( point.equals( new Vector3( 3, 0, 0 ) ), "Passed!" );
  163. } );
  164. QUnit.todo( "intersectsBox", ( assert ) => {
  165. assert.ok( false, "everything's gonna be alright" );
  166. } );
  167. QUnit.todo( "intersectsSphere", ( assert ) => {
  168. assert.ok( false, "everything's gonna be alright" );
  169. } );
  170. QUnit.test( "coplanarPoint", ( assert ) => {
  171. var point = new Vector3();
  172. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  173. a.coplanarPoint( point );
  174. assert.ok( a.distanceToPoint( point ) === 0, "Passed!" );
  175. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  176. a.coplanarPoint( point );
  177. assert.ok( a.distanceToPoint( point ) === 0, "Passed!" );
  178. } );
  179. QUnit.test( "applyMatrix4/translate", ( assert ) => {
  180. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  181. var m = new Matrix4();
  182. m.makeRotationZ( Math.PI * 0.5 );
  183. assert.ok( comparePlane( a.clone().applyMatrix4( m ), new Plane( new Vector3( 0, 1, 0 ), 0 ) ), "Passed!" );
  184. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  185. assert.ok( comparePlane( a.clone().applyMatrix4( m ), new Plane( new Vector3( - 1, 0, 0 ), - 1 ) ), "Passed!" );
  186. m.makeTranslation( 1, 1, 1 );
  187. assert.ok( comparePlane( a.clone().applyMatrix4( m ), a.clone().translate( new Vector3( 1, 1, 1 ) ) ), "Passed!" );
  188. } );
  189. QUnit.test( "equals", ( assert ) => {
  190. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  191. var b = new Plane( new Vector3( 1, 0, 0 ), 1 );
  192. var c = new Plane( new Vector3( 0, 1, 0 ), 0 );
  193. assert.ok( a.normal.equals( b.normal ), "Normals: equal" );
  194. assert.notOk( a.normal.equals( c.normal ), "Normals: not equal" );
  195. assert.notStrictEqual( a.constant, b.constant, "Constants: not equal" );
  196. assert.strictEqual( a.constant, c.constant, "Constants: equal" );
  197. assert.notOk( a.equals( b ), "Planes: not equal" );
  198. assert.notOk( a.equals( c ), "Planes: not equal" );
  199. a.copy( b );
  200. assert.ok( a.normal.equals( b.normal ), "Normals after copy(): equal" );
  201. assert.strictEqual( a.constant, b.constant, "Constants after copy(): equal" );
  202. assert.ok( a.equals( b ), "Planes after copy(): equal" );
  203. } );
  204. } );
  205. } );
  206. QUnit.module( "Plane" );