Spherical.tests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* global QUnit */
  2. import { Spherical } from '../../../../src/math/Spherical';
  3. import { Vector3 } from '../../../../src/math/Vector3';
  4. import {
  5. eps
  6. } from './Constants.tests';
  7. export default QUnit.module( 'Maths', () => {
  8. QUnit.module( 'Spherical', () => {
  9. // INSTANCING
  10. QUnit.test( "Instancing", ( assert ) => {
  11. var a = new Spherical();
  12. var radius = 10.0;
  13. var phi = Math.acos( - 0.5 );
  14. var theta = Math.sqrt( Math.PI ) * phi;
  15. assert.strictEqual( a.radius, 1.0, "Default values: check radius" );
  16. assert.strictEqual( a.phi, 0, "Default values: check phi" );
  17. assert.strictEqual( a.theta, 0, "Default values: check theta" );
  18. var a = new Spherical( radius, phi, theta );
  19. assert.strictEqual( a.radius, radius, "Custom values: check radius" );
  20. assert.strictEqual( a.phi, phi, "Custom values: check phi" );
  21. assert.strictEqual( a.theta, theta, "Custom values: check theta" );
  22. } );
  23. // PUBLIC STUFF
  24. QUnit.todo( "isSpherical", ( assert ) => {
  25. assert.ok( false, "everything's gonna be alright" );
  26. } );
  27. QUnit.test( "set", ( assert ) => {
  28. var a = new Spherical();
  29. var radius = 10.0;
  30. var phi = Math.acos( - 0.5 );
  31. var theta = Math.sqrt( Math.PI ) * phi;
  32. a.set( radius, phi, theta );
  33. assert.strictEqual( a.radius, radius, "Check radius" );
  34. assert.strictEqual( a.phi, phi, "Check phi" );
  35. assert.strictEqual( a.theta, theta, "Check theta" );
  36. } );
  37. QUnit.test( "clone", ( assert ) => {
  38. var radius = 10.0;
  39. var phi = Math.acos( - 0.5 );
  40. var theta = Math.sqrt( Math.PI ) * phi;
  41. var a = new Spherical( radius, phi, theta );
  42. var b = a.clone();
  43. assert.propEqual( a, b, "Check a and b are equal after clone()" );
  44. a.radius = 2.0;
  45. assert.notPropEqual( a, b, "Check a and b are not equal after modification" );
  46. } );
  47. QUnit.test( "copy", ( assert ) => {
  48. var radius = 10.0;
  49. var phi = Math.acos( - 0.5 );
  50. var theta = Math.sqrt( Math.PI ) * phi;
  51. var a = new Spherical( radius, phi, theta );
  52. var b = new Spherical().copy( a );
  53. assert.propEqual( a, b, "Check a and b are equal after copy()" );
  54. a.radius = 2.0;
  55. assert.notPropEqual( a, b, "Check a and b are not equal after modification" );
  56. } );
  57. QUnit.test( "makeSafe", ( assert ) => {
  58. var EPS = 0.000001; // from source
  59. var tooLow = 0.0;
  60. var tooHigh = Math.PI;
  61. var justRight = 1.5;
  62. var a = new Spherical( 1, tooLow, 0 );
  63. a.makeSafe();
  64. assert.strictEqual( a.phi, EPS, "Check if small values are set to EPS" );
  65. a.set( 1, tooHigh, 0 );
  66. a.makeSafe();
  67. assert.strictEqual( a.phi, Math.PI - EPS, "Check if high values are set to (Math.PI - EPS)" );
  68. a.set( 1, justRight, 0 );
  69. a.makeSafe();
  70. assert.strictEqual( a.phi, justRight, "Check that valid values don't get changed" );
  71. } );
  72. QUnit.test( "setFromVector3", ( assert ) => {
  73. var a = new Spherical( 1, 1, 1 );
  74. var b = new Vector3( 0, 0, 0 );
  75. var c = new Vector3( Math.PI, 1, - Math.PI );
  76. var expected = new Spherical( 4.554032147688322, 1.3494066171539107, 2.356194490192345 );
  77. a.setFromVector3( b );
  78. assert.strictEqual( a.radius, 0, "Zero-length vector: check radius" );
  79. assert.strictEqual( a.phi, 0, "Zero-length vector: check phi" );
  80. assert.strictEqual( a.theta, 0, "Zero-length vector: check theta" );
  81. a.setFromVector3( c );
  82. assert.ok( Math.abs( a.radius - expected.radius ) <= eps, "Normal vector: check radius" );
  83. assert.ok( Math.abs( a.phi - expected.phi ) <= eps, "Normal vector: check phi" );
  84. assert.ok( Math.abs( a.theta - expected.theta ) <= eps, "Normal vector: check theta" );
  85. } );
  86. } );
  87. } );
粤ICP备19079148号