Sphere.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. QUnit.module( "Sphere" );
  5. QUnit.test( "constructor" , function( assert ) {
  6. var a = new THREE.Sphere();
  7. assert.ok( a.center.equals( zero3 ), "Passed!" );
  8. assert.ok( a.radius == 0, "Passed!" );
  9. a = new THREE.Sphere( one3.clone(), 1 );
  10. assert.ok( a.center.equals( one3 ), "Passed!" );
  11. assert.ok( a.radius == 1, "Passed!" );
  12. });
  13. QUnit.test( "copy" , function( assert ) {
  14. var a = new THREE.Sphere( one3.clone(), 1 );
  15. var b = new THREE.Sphere().copy( a );
  16. assert.ok( b.center.equals( one3 ), "Passed!" );
  17. assert.ok( b.radius == 1, "Passed!" );
  18. // ensure that it is a true copy
  19. a.center = zero3;
  20. a.radius = 0;
  21. assert.ok( b.center.equals( one3 ), "Passed!" );
  22. assert.ok( b.radius == 1, "Passed!" );
  23. });
  24. QUnit.test( "set" , function( assert ) {
  25. var a = new THREE.Sphere();
  26. assert.ok( a.center.equals( zero3 ), "Passed!" );
  27. assert.ok( a.radius == 0, "Passed!" );
  28. a.set( one3, 1 );
  29. assert.ok( a.center.equals( one3 ), "Passed!" );
  30. assert.ok( a.radius == 1, "Passed!" );
  31. });
  32. QUnit.test( "empty" , function( assert ) {
  33. var a = new THREE.Sphere();
  34. assert.ok( a.empty(), "Passed!" );
  35. a.set( one3, 1 );
  36. assert.ok( ! a.empty(), "Passed!" );
  37. });
  38. QUnit.test( "containsPoint" , function( assert ) {
  39. var a = new THREE.Sphere( one3.clone(), 1 );
  40. assert.ok( ! a.containsPoint( zero3 ), "Passed!" );
  41. assert.ok( a.containsPoint( one3 ), "Passed!" );
  42. });
  43. QUnit.test( "distanceToPoint" , function( assert ) {
  44. var a = new THREE.Sphere( one3.clone(), 1 );
  45. assert.ok( ( a.distanceToPoint( zero3 ) - 0.7320 ) < 0.001, "Passed!" );
  46. assert.ok( a.distanceToPoint( one3 ) === -1, "Passed!" );
  47. });
  48. QUnit.test( "intersectsSphere" , function( assert ) {
  49. var a = new THREE.Sphere( one3.clone(), 1 );
  50. var b = new THREE.Sphere( zero3.clone(), 1 );
  51. var c = new THREE.Sphere( zero3.clone(), 0.25 );
  52. assert.ok( a.intersectsSphere( b ) , "Passed!" );
  53. assert.ok( ! a.intersectsSphere( c ) , "Passed!" );
  54. });
  55. QUnit.test( "intersectsPlane" , function( assert ) {
  56. var a = new THREE.Sphere( zero3.clone(), 1 );
  57. var b = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 1 );
  58. var c = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 1.25 );
  59. var d = new THREE.Plane( new THREE.Vector3( 0, -1, 0 ), 1.25 );
  60. assert.ok( a.intersectsPlane( b ) , "Passed!" );
  61. assert.ok( ! a.intersectsPlane( c ) , "Passed!" );
  62. assert.ok( ! a.intersectsPlane( d ) , "Passed!" );
  63. });
  64. QUnit.test( "clampPoint" , function( assert ) {
  65. var a = new THREE.Sphere( one3.clone(), 1 );
  66. assert.ok( a.clampPoint( new THREE.Vector3( 1, 1, 3 ) ).equals( new THREE.Vector3( 1, 1, 2 ) ), "Passed!" );
  67. assert.ok( a.clampPoint( new THREE.Vector3( 1, 1, -3 ) ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  68. });
  69. QUnit.test( "getBoundingBox" , function( assert ) {
  70. var a = new THREE.Sphere( one3.clone(), 1 );
  71. assert.ok( a.getBoundingBox().equals( new THREE.Box3( zero3, two3 ) ), "Passed!" );
  72. a.set( zero3, 0 );
  73. assert.ok( a.getBoundingBox().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" );
  74. });
  75. QUnit.test( "applyMatrix4" , function( assert ) {
  76. var a = new THREE.Sphere( one3.clone(), 1 );
  77. var m = new THREE.Matrix4().makeTranslation( 1, -2, 1 );
  78. assert.ok( a.clone().applyMatrix4( m ).getBoundingBox().equals( a.getBoundingBox().applyMatrix4( m ) ), "Passed!" );
  79. });
  80. QUnit.test( "translate" , function( assert ) {
  81. var a = new THREE.Sphere( one3.clone(), 1 );
  82. a.translate( one3.clone().negate() );
  83. assert.ok( a.center.equals( zero3 ), "Passed!" );
  84. });