Plane.tests.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* global QUnit */
  2. import { Plane } from '../../../../src/math/Plane.js';
  3. import { Vector3 } from '../../../../src/math/Vector3.js';
  4. import { Line3 } from '../../../../src/math/Line3.js';
  5. import { Sphere } from '../../../../src/math/Sphere.js';
  6. import { Box3 } from '../../../../src/math/Box3.js';
  7. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  8. import {
  9. x,
  10. y,
  11. z,
  12. w,
  13. zero3,
  14. one3
  15. } from './Constants.tests.js';
  16. function comparePlane( a, b, threshold ) {
  17. threshold = threshold || 0.0001;
  18. return ( a.normal.distanceTo( b.normal ) < threshold &&
  19. Math.abs( a.constant - b.constant ) < threshold );
  20. }
  21. export default QUnit.module( 'Maths', () => {
  22. QUnit.module( 'Plane', () => {
  23. // INSTANCING
  24. QUnit.test( 'Instancing', ( assert ) => {
  25. var a = new Plane();
  26. assert.ok( a.normal.x == 1, 'Passed!' );
  27. assert.ok( a.normal.y == 0, 'Passed!' );
  28. assert.ok( a.normal.z == 0, 'Passed!' );
  29. assert.ok( a.constant == 0, 'Passed!' );
  30. var a = new Plane( one3.clone(), 0 );
  31. assert.ok( a.normal.x == 1, 'Passed!' );
  32. assert.ok( a.normal.y == 1, 'Passed!' );
  33. assert.ok( a.normal.z == 1, 'Passed!' );
  34. assert.ok( a.constant == 0, 'Passed!' );
  35. var a = new Plane( one3.clone(), 1 );
  36. assert.ok( a.normal.x == 1, 'Passed!' );
  37. assert.ok( a.normal.y == 1, 'Passed!' );
  38. assert.ok( a.normal.z == 1, 'Passed!' );
  39. assert.ok( a.constant == 1, 'Passed!' );
  40. } );
  41. // PUBLIC STUFF
  42. QUnit.test( 'isPlane', ( assert ) => {
  43. var a = new Plane();
  44. assert.ok( a.isPlane === true, 'Passed!' );
  45. var b = new Vector3();
  46. assert.ok( ! b.isPlane, 'Passed!' );
  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.test( 'clone', ( assert ) => {
  90. var a = new Plane( new Vector3( 2.0, 0.5, 0.25 ) );
  91. var b = a.clone();
  92. assert.ok( a.equals( b ), 'clones are equal' );
  93. } );
  94. QUnit.test( 'copy', ( assert ) => {
  95. var a = new Plane( new Vector3( x, y, z ), w );
  96. var b = new Plane().copy( a );
  97. assert.ok( b.normal.x == x, 'Passed!' );
  98. assert.ok( b.normal.y == y, 'Passed!' );
  99. assert.ok( b.normal.z == z, 'Passed!' );
  100. assert.ok( b.constant == w, 'Passed!' );
  101. // ensure that it is a true copy
  102. a.normal.x = 0;
  103. a.normal.y = - 1;
  104. a.normal.z = - 2;
  105. a.constant = - 3;
  106. assert.ok( b.normal.x == x, 'Passed!' );
  107. assert.ok( b.normal.y == y, 'Passed!' );
  108. assert.ok( b.normal.z == z, 'Passed!' );
  109. assert.ok( b.constant == w, 'Passed!' );
  110. } );
  111. QUnit.test( 'normalize', ( assert ) => {
  112. var a = new Plane( new Vector3( 2, 0, 0 ), 2 );
  113. a.normalize();
  114. assert.ok( a.normal.length() == 1, 'Passed!' );
  115. assert.ok( a.normal.equals( new Vector3( 1, 0, 0 ) ), 'Passed!' );
  116. assert.ok( a.constant == 1, 'Passed!' );
  117. } );
  118. QUnit.test( 'negate/distanceToPoint', ( assert ) => {
  119. var a = new Plane( new Vector3( 2, 0, 0 ), - 2 );
  120. a.normalize();
  121. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === 3, 'Passed!' );
  122. assert.ok( a.distanceToPoint( new Vector3( 1, 0, 0 ) ) === 0, 'Passed!' );
  123. a.negate();
  124. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === - 3, 'Passed!' );
  125. assert.ok( a.distanceToPoint( new Vector3( 1, 0, 0 ) ) === 0, 'Passed!' );
  126. } );
  127. QUnit.test( 'distanceToPoint', ( assert ) => {
  128. var a = new Plane( new Vector3( 2, 0, 0 ), - 2 );
  129. var point = new Vector3();
  130. a.normalize().projectPoint( zero3.clone(), point );
  131. assert.ok( a.distanceToPoint( point ) === 0, 'Passed!' );
  132. assert.ok( a.distanceToPoint( new Vector3( 4, 0, 0 ) ) === 3, 'Passed!' );
  133. } );
  134. QUnit.test( 'distanceToSphere', ( assert ) => {
  135. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  136. var b = new Sphere( new Vector3( 2, 0, 0 ), 1 );
  137. assert.ok( a.distanceToSphere( b ) === 1, 'Passed!' );
  138. a.set( new Vector3( 1, 0, 0 ), 2 );
  139. assert.ok( a.distanceToSphere( b ) === 3, 'Passed!' );
  140. a.set( new Vector3( 1, 0, 0 ), - 2 );
  141. assert.ok( a.distanceToSphere( b ) === - 1, 'Passed!' );
  142. } );
  143. QUnit.test( 'projectPoint', ( assert ) => {
  144. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  145. var point = new Vector3();
  146. a.projectPoint( new Vector3( 10, 0, 0 ), point );
  147. assert.ok( point.equals( zero3 ), 'Passed!' );
  148. a.projectPoint( new Vector3( - 10, 0, 0 ), point );
  149. assert.ok( point.equals( zero3 ), 'Passed!' );
  150. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  151. a.projectPoint( new Vector3( 0, 0, 0 ), point );
  152. assert.ok( point.equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  153. a.projectPoint( new Vector3( 0, 1, 0 ), point );
  154. assert.ok( point.equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  155. } );
  156. QUnit.test( 'isInterestionLine/intersectLine', ( assert ) => {
  157. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  158. var point = new Vector3();
  159. var l1 = new Line3( new Vector3( - 10, 0, 0 ), new Vector3( 10, 0, 0 ) );
  160. a.intersectLine( l1, point );
  161. assert.ok( point.equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  162. var a = new Plane( new Vector3( 1, 0, 0 ), - 3 );
  163. a.intersectLine( l1, point );
  164. assert.ok( point.equals( new Vector3( 3, 0, 0 ) ), 'Passed!' );
  165. } );
  166. QUnit.test( 'intersectsBox', ( assert ) => {
  167. var a = new Box3( zero3.clone(), one3.clone() );
  168. var b = new Plane( new Vector3( 0, 1, 0 ), 1 );
  169. var c = new Plane( new Vector3( 0, 1, 0 ), 1.25 );
  170. var d = new Plane( new Vector3( 0, - 1, 0 ), 1.25 );
  171. var e = new Plane( new Vector3( 0, 1, 0 ), 0.25 );
  172. var f = new Plane( new Vector3( 0, 1, 0 ), - 0.25 );
  173. var g = new Plane( new Vector3( 0, 1, 0 ), - 0.75 );
  174. var h = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  175. var i = new Plane( new Vector3( 1, 1, 1 ).normalize(), - 1.732 );
  176. var j = new Plane( new Vector3( 1, 1, 1 ).normalize(), - 1.733 );
  177. assert.ok( ! b.intersectsBox( a ), 'Passed!' );
  178. assert.ok( ! c.intersectsBox( a ), 'Passed!' );
  179. assert.ok( ! d.intersectsBox( a ), 'Passed!' );
  180. assert.ok( ! e.intersectsBox( a ), 'Passed!' );
  181. assert.ok( f.intersectsBox( a ), 'Passed!' );
  182. assert.ok( g.intersectsBox( a ), 'Passed!' );
  183. assert.ok( h.intersectsBox( a ), 'Passed!' );
  184. assert.ok( i.intersectsBox( a ), 'Passed!' );
  185. assert.ok( ! j.intersectsBox( a ), 'Passed!' );
  186. } );
  187. QUnit.test( 'intersectsSphere', ( assert ) => {
  188. var a = new Sphere( zero3.clone(), 1 );
  189. var b = new Plane( new Vector3( 0, 1, 0 ), 1 );
  190. var c = new Plane( new Vector3( 0, 1, 0 ), 1.25 );
  191. var d = new Plane( new Vector3( 0, - 1, 0 ), 1.25 );
  192. assert.ok( b.intersectsSphere( a ), 'Passed!' );
  193. assert.ok( ! c.intersectsSphere( a ), 'Passed!' );
  194. assert.ok( ! d.intersectsSphere( a ), 'Passed!' );
  195. } );
  196. QUnit.test( 'coplanarPoint', ( assert ) => {
  197. var point = new Vector3();
  198. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  199. a.coplanarPoint( point );
  200. assert.ok( a.distanceToPoint( point ) === 0, 'Passed!' );
  201. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  202. a.coplanarPoint( point );
  203. assert.ok( a.distanceToPoint( point ) === 0, 'Passed!' );
  204. } );
  205. QUnit.test( 'applyMatrix4/translate', ( assert ) => {
  206. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  207. var m = new Matrix4();
  208. m.makeRotationZ( Math.PI * 0.5 );
  209. assert.ok( comparePlane( a.clone().applyMatrix4( m ), new Plane( new Vector3( 0, 1, 0 ), 0 ) ), 'Passed!' );
  210. var a = new Plane( new Vector3( 0, 1, 0 ), - 1 );
  211. assert.ok( comparePlane( a.clone().applyMatrix4( m ), new Plane( new Vector3( - 1, 0, 0 ), - 1 ) ), 'Passed!' );
  212. m.makeTranslation( 1, 1, 1 );
  213. assert.ok( comparePlane( a.clone().applyMatrix4( m ), a.clone().translate( new Vector3( 1, 1, 1 ) ) ), 'Passed!' );
  214. } );
  215. QUnit.test( 'equals', ( assert ) => {
  216. var a = new Plane( new Vector3( 1, 0, 0 ), 0 );
  217. var b = new Plane( new Vector3( 1, 0, 0 ), 1 );
  218. var c = new Plane( new Vector3( 0, 1, 0 ), 0 );
  219. assert.ok( a.normal.equals( b.normal ), 'Normals: equal' );
  220. assert.notOk( a.normal.equals( c.normal ), 'Normals: not equal' );
  221. assert.notStrictEqual( a.constant, b.constant, 'Constants: not equal' );
  222. assert.strictEqual( a.constant, c.constant, 'Constants: equal' );
  223. assert.notOk( a.equals( b ), 'Planes: not equal' );
  224. assert.notOk( a.equals( c ), 'Planes: not equal' );
  225. a.copy( b );
  226. assert.ok( a.normal.equals( b.normal ), 'Normals after copy(): equal' );
  227. assert.strictEqual( a.constant, b.constant, 'Constants after copy(): equal' );
  228. assert.ok( a.equals( b ), 'Planes after copy(): equal' );
  229. } );
  230. } );
  231. } );
  232. QUnit.module( 'Plane' );