Sphere.tests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { Ray } from '../../../../src/math/Ray';
  7. import { Box3 } from '../../../../src/math/Box3';
  8. import { Vector3 } from '../../../../src/math/Vector3';
  9. import { Sphere } from '../../../../src/math/Sphere';
  10. import { Plane } from '../../../../src/math/Plane';
  11. import { Matrix4 } from '../../../../src/math/Matrix4';
  12. import {
  13. zero3,
  14. one3,
  15. two3,
  16. eps
  17. } from './Constants.tests';
  18. export default QUnit.module( 'Maths', () => {
  19. QUnit.module( 'Sphere', () => {
  20. // INSTANCING
  21. QUnit.test( "Instancing", ( assert ) => {
  22. var a = new Sphere();
  23. assert.ok( a.center.equals( zero3 ), "Passed!" );
  24. assert.ok( a.radius == 0, "Passed!" );
  25. var a = new Sphere( one3.clone(), 1 );
  26. assert.ok( a.center.equals( one3 ), "Passed!" );
  27. assert.ok( a.radius == 1, "Passed!" );
  28. } );
  29. // PUBLIC STUFF
  30. QUnit.todo( "isSphere", ( assert ) => {
  31. assert.ok( false, "everything's gonna be alright" );
  32. } );
  33. QUnit.test( "set", ( assert ) => {
  34. var a = new Sphere();
  35. assert.ok( a.center.equals( zero3 ), "Passed!" );
  36. assert.ok( a.radius == 0, "Passed!" );
  37. a.set( one3, 1 );
  38. assert.ok( a.center.equals( one3 ), "Passed!" );
  39. assert.ok( a.radius == 1, "Passed!" );
  40. } );
  41. QUnit.test( "setFromPoints", ( assert ) => {
  42. var a = new Sphere();
  43. var expectedCenter = new Vector3( 0.9330126941204071, 0, 0 );
  44. var expectedRadius = 1.3676668773461689;
  45. var optionalCenter = new Vector3( 1, 1, 1 );
  46. var points = [
  47. new Vector3( 1, 1, 0 ), new Vector3( 1, 1, 0 ),
  48. new Vector3( 1, 1, 0 ), new Vector3( 1, 1, 0 ),
  49. new Vector3( 1, 1, 0 ), new Vector3( 0.8660253882408142, 0.5, 0 ),
  50. new Vector3( - 0, 0.5, 0.8660253882408142 ), new Vector3( 1.8660253882408142, 0.5, 0 ),
  51. new Vector3( 0, 0.5, - 0.8660253882408142 ), new Vector3( 0.8660253882408142, 0.5, - 0 ),
  52. new Vector3( 0.8660253882408142, - 0.5, 0 ), new Vector3( - 0, - 0.5, 0.8660253882408142 ),
  53. new Vector3( 1.8660253882408142, - 0.5, 0 ), new Vector3( 0, - 0.5, - 0.8660253882408142 ),
  54. new Vector3( 0.8660253882408142, - 0.5, - 0 ), new Vector3( - 0, - 1, 0 ),
  55. new Vector3( - 0, - 1, 0 ), new Vector3( 0, - 1, 0 ),
  56. new Vector3( 0, - 1, - 0 ), new Vector3( - 0, - 1, - 0 ),
  57. ];
  58. a.setFromPoints( points );
  59. assert.ok( Math.abs( a.center.x - expectedCenter.x ) <= eps, "Default center: check center.x" );
  60. assert.ok( Math.abs( a.center.y - expectedCenter.y ) <= eps, "Default center: check center.y" );
  61. assert.ok( Math.abs( a.center.z - expectedCenter.z ) <= eps, "Default center: check center.z" );
  62. assert.ok( Math.abs( a.radius - expectedRadius ) <= eps, "Default center: check radius" );
  63. var expectedRadius = 2.5946195770400102;
  64. a.setFromPoints( points, optionalCenter );
  65. assert.ok( Math.abs( a.center.x - optionalCenter.x ) <= eps, "Optional center: check center.x" );
  66. assert.ok( Math.abs( a.center.y - optionalCenter.y ) <= eps, "Optional center: check center.y" );
  67. assert.ok( Math.abs( a.center.z - optionalCenter.z ) <= eps, "Optional center: check center.z" );
  68. assert.ok( Math.abs( a.radius - expectedRadius ) <= eps, "Optional center: check radius" );
  69. } );
  70. QUnit.todo( "clone", ( assert ) => {
  71. assert.ok( false, "everything's gonna be alright" );
  72. } );
  73. QUnit.test( "copy", ( assert ) => {
  74. var a = new Sphere( one3.clone(), 1 );
  75. var b = new Sphere().copy( a );
  76. assert.ok( b.center.equals( one3 ), "Passed!" );
  77. assert.ok( b.radius == 1, "Passed!" );
  78. // ensure that it is a true copy
  79. a.center = zero3;
  80. a.radius = 0;
  81. assert.ok( b.center.equals( one3 ), "Passed!" );
  82. assert.ok( b.radius == 1, "Passed!" );
  83. } );
  84. QUnit.test( "empty", ( assert ) => {
  85. var a = new Sphere();
  86. assert.ok( a.empty(), "Passed!" );
  87. a.set( one3, 1 );
  88. assert.ok( ! a.empty(), "Passed!" );
  89. } );
  90. QUnit.test( "containsPoint", ( assert ) => {
  91. var a = new Sphere( one3.clone(), 1 );
  92. assert.ok( ! a.containsPoint( zero3 ), "Passed!" );
  93. assert.ok( a.containsPoint( one3 ), "Passed!" );
  94. } );
  95. QUnit.test( "distanceToPoint", ( assert ) => {
  96. var a = new Sphere( one3.clone(), 1 );
  97. assert.ok( ( a.distanceToPoint( zero3 ) - 0.7320 ) < 0.001, "Passed!" );
  98. assert.ok( a.distanceToPoint( one3 ) === - 1, "Passed!" );
  99. } );
  100. QUnit.test( "intersectsSphere", ( assert ) => {
  101. var a = new Sphere( one3.clone(), 1 );
  102. var b = new Sphere( zero3.clone(), 1 );
  103. var c = new Sphere( zero3.clone(), 0.25 );
  104. assert.ok( a.intersectsSphere( b ), "Passed!" );
  105. assert.ok( ! a.intersectsSphere( c ), "Passed!" );
  106. } );
  107. QUnit.test( "intersectsBox", ( assert ) => {
  108. var a = new Sphere();
  109. var b = new Sphere( new Vector3( - 5, - 5, - 5 ) );
  110. var box = new Box3( zero3, one3 );
  111. assert.strictEqual( a.intersectsBox( box ), true, "Check default sphere" );
  112. assert.strictEqual( b.intersectsBox( box ), false, "Check shifted sphere" );
  113. } );
  114. QUnit.test( "intersectsPlane", ( assert ) => {
  115. var a = new Sphere( zero3.clone(), 1 );
  116. var b = new Plane( new Vector3( 0, 1, 0 ), 1 );
  117. var c = new Plane( new Vector3( 0, 1, 0 ), 1.25 );
  118. var d = new Plane( new Vector3( 0, - 1, 0 ), 1.25 );
  119. assert.ok( a.intersectsPlane( b ), "Passed!" );
  120. assert.ok( ! a.intersectsPlane( c ), "Passed!" );
  121. assert.ok( ! a.intersectsPlane( d ), "Passed!" );
  122. } );
  123. QUnit.test( "clampPoint", ( assert ) => {
  124. var a = new Sphere( one3.clone(), 1 );
  125. assert.ok( a.clampPoint( new Vector3( 1, 1, 3 ) ).equals( new Vector3( 1, 1, 2 ) ), "Passed!" );
  126. assert.ok( a.clampPoint( new Vector3( 1, 1, - 3 ) ).equals( new Vector3( 1, 1, 0 ) ), "Passed!" );
  127. } );
  128. QUnit.test( "getBoundingBox", ( assert ) => {
  129. var a = new Sphere( one3.clone(), 1 );
  130. assert.ok( a.getBoundingBox().equals( new Box3( zero3, two3 ) ), "Passed!" );
  131. a.set( zero3, 0 );
  132. assert.ok( a.getBoundingBox().equals( new Box3( zero3, zero3 ) ), "Passed!" );
  133. } );
  134. QUnit.test( "applyMatrix4", ( assert ) => {
  135. var a = new Sphere( one3.clone(), 1 );
  136. var m = new Matrix4().makeTranslation( 1, - 2, 1 );
  137. assert.ok( a.clone().applyMatrix4( m ).getBoundingBox().equals( a.getBoundingBox().applyMatrix4( m ) ), "Passed!" );
  138. } );
  139. QUnit.test( "translate", ( assert ) => {
  140. var a = new Sphere( one3.clone(), 1 );
  141. a.translate( one3.clone().negate() );
  142. assert.ok( a.center.equals( zero3 ), "Passed!" );
  143. } );
  144. QUnit.test( "equals", ( assert ) => {
  145. var a = new Sphere();
  146. var b = new Sphere( new Vector3( 1, 0, 0 ) );
  147. var c = new Sphere( new Vector3( 1, 0, 0 ), 1.0 );
  148. assert.strictEqual( a.equals( b ), false, "a does not equal b" );
  149. assert.strictEqual( a.equals( c ), false, "a does not equal c" );
  150. assert.strictEqual( b.equals( c ), false, "b does not equal c" );
  151. a.copy( b );
  152. assert.strictEqual( a.equals( b ), true, "a equals b after copy()" );
  153. } );
  154. } );
  155. } );
粤ICP备19079148号