Spherical.tests.js 3.6 KB

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