testVector2d.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. template<class T>
  7. static bool compareVectors(const core::vector2d<T> & compare,
  8. const core::vector2d<T> & with)
  9. {
  10. if (!compare.equals(with))
  11. {
  12. logTestString("\nERROR: vector2d %.16f, %.16f != vector2d %.16f, %.16f\n",
  13. (f64)compare.X, (f64)compare.Y, (f64)with.X, (f64)with.Y);
  14. assert_log(compare == with);
  15. return false;
  16. }
  17. return true;
  18. }
  19. template <class T>
  20. static bool doTests()
  21. {
  22. #define COMPARE_VECTORS(compare, with)\
  23. if(!compareVectors(compare, with)) return false;
  24. vector2d<T> vec(5, 5);
  25. vector2d<T> otherVec(10, 20);
  26. if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
  27. {
  28. logTestString("vector2d::getDistanceFrom() failed\n");
  29. assert_log(0);
  30. return false;
  31. }
  32. vec.rotateBy(45); // Test implicit (0, 0) center
  33. COMPARE_VECTORS(vec, vector2d<T>(0, (T)7.0710678118654755));
  34. vec.normalize();
  35. COMPARE_VECTORS(vec, vector2d<T>(0, (T)1.0000000461060017));
  36. vec.set(10, 10);
  37. vector2d<T> center(5, 5);
  38. vec.rotateBy(-5, center);
  39. // -5 means rotate clockwise slightly, so expect the X to increase
  40. // slightly and the Y to decrease slightly.
  41. COMPARE_VECTORS(vec, vector2d<T>((T)10.416752204197017, (T)9.5451947767204359));
  42. vec.set(5, 5);
  43. vec.normalize();
  44. COMPARE_VECTORS(vec, vector2d<T>((T)0.7071068137884140, (T)0.7071068137884140));
  45. vec.set(5, 5);
  46. otherVec.set(10, 20);
  47. logTestString("vector2d interpolation\n");
  48. vector2d<T> interpolated;
  49. (void)interpolated.interpolate(vec, otherVec, 0.f);
  50. COMPARE_VECTORS(interpolated, otherVec); // 0.f means all the second vector
  51. (void)interpolated.interpolate(vec, otherVec, 0.25f);
  52. COMPARE_VECTORS(interpolated, vector2d<T>((T)8.75, (T)16.25));
  53. (void)interpolated.interpolate(vec, otherVec, 0.75f);
  54. COMPARE_VECTORS(interpolated, vector2d<T>((T)6.25, (T)8.75));
  55. (void)interpolated.interpolate(vec, otherVec, 1.f);
  56. COMPARE_VECTORS(interpolated, vec); // 1.f means all the first vector
  57. interpolated = vec.getInterpolated(otherVec, 0.f);
  58. COMPARE_VECTORS(interpolated, otherVec); // 0.f means all the second vector
  59. interpolated = vec.getInterpolated(otherVec, 0.25f);
  60. COMPARE_VECTORS(interpolated, vector2d<T>((T)8.75, (T)16.25));
  61. interpolated = vec.getInterpolated(otherVec, 0.75f);
  62. COMPARE_VECTORS(interpolated, vector2d<T>((T)6.25, (T)8.75));
  63. interpolated = vec.getInterpolated(otherVec, 1.f);
  64. COMPARE_VECTORS(interpolated, vec); // 1.f means all the first vector
  65. logTestString("vector2d quadratic interpolation\n");
  66. vector2d<T> thirdVec(20, 10);
  67. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.f);
  68. COMPARE_VECTORS(interpolated, vec); // 0.f means all the 1st vector
  69. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
  70. COMPARE_VECTORS(interpolated, vector2d<T>((T)7.8125, (T)10.9375));
  71. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
  72. COMPARE_VECTORS(interpolated, vector2d<T>((T)11.25, (T)13.75));
  73. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
  74. COMPARE_VECTORS(interpolated, vector2d<T>((T)15.3125, (T)13.4375));
  75. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
  76. COMPARE_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
  77. // check if getAngle returns values matching those of the double precision version
  78. logTestString("vector2d getAngle\n");
  79. for (s32 i=0; i<200; ++i)
  80. {
  81. core::vector2d<T> tmp((T)-1, (T)(-100+i));
  82. core::vector2d<f64> ref(-1, -100+i);
  83. if (!equals(tmp.getAngle(),ref.getAngle(), 0.0003))
  84. {
  85. logTestString("\nERROR: angle %.16f != angle %.16f\n",
  86. tmp.getAngle(), ref.getAngle());
  87. return false;
  88. }
  89. f32 val = atan2f((float)tmp.Y, (float)tmp.X)*core::RADTODEG;
  90. if (val<=0)
  91. val=-val;
  92. else
  93. val=360-val;
  94. if (!equals((f32)tmp.getAngle(),val, 0.5f))
  95. {
  96. logTestString("\nERROR: angle %.16f != atan2 %.16f\n vector %.16f, %.16f\n",
  97. tmp.getAngle(), val, tmp.X, tmp.Y);
  98. return false;
  99. }
  100. tmp = core::vector2d<T>((T)1, (T)(-100+i));
  101. ref = core::vector2d<f64>(1, -100+i);
  102. if (!equals(tmp.getAngle(),ref.getAngle(), 0.0003))
  103. {
  104. logTestString("\nERROR: angle %.16f != angle %.16f\n",
  105. tmp.getAngle(), ref.getAngle());
  106. return false;
  107. }
  108. val = atan2f((f32)tmp.Y, (f32)tmp.X)*core::RADTODEG;
  109. if (val<=0)
  110. val=-val;
  111. else
  112. val=360-val;
  113. if (!equals((f32)tmp.getAngle(),val, 0.5f))
  114. {
  115. logTestString("\nERROR: angle %.16f != atan2 %.16f\n vector %.16f, %.16f\n",
  116. tmp.getAngle(), val, tmp.X, tmp.Y);
  117. return false;
  118. }
  119. }
  120. core::vector2d<T> tmp(0, -100);
  121. core::vector2d<f64> ref(0, -100);
  122. if (!equals(tmp.getAngle(),ref.getAngle()))
  123. {
  124. logTestString("\nERROR: angle %.16f != angle %.16f\n",
  125. tmp.getAngle(), ref.getAngle());
  126. return false;
  127. }
  128. tmp = core::vector2d<T>(0, 100);
  129. ref = core::vector2d<f64>(0, 100);
  130. if (!equals(tmp.getAngle(),ref.getAngle()))
  131. {
  132. logTestString("\nERROR: angle %.16f != angle %.16f\n",
  133. tmp.getAngle(), ref.getAngle());
  134. return false;
  135. }
  136. tmp = core::vector2d<T>(static_cast<T>(-1.53080559e-16), static_cast<T>(2.49999523));
  137. ref = core::vector2d<f64>(-1.53080559e-16, 2.49999523);
  138. if (!equals(tmp.getAngle(),ref.getAngle()))
  139. {
  140. logTestString("\nERROR: angle %.16f != angle %.16f\n",
  141. tmp.getAngle(), ref.getAngle());
  142. return false;
  143. }
  144. core::vector2d<T> zeroZero(0, 0);
  145. core::vector2d<T> oneOne(1, 1);
  146. // Check if comparing (0.0, 0.0) with (1.0, 1.0) returns false.
  147. if(zeroZero == oneOne)
  148. {
  149. logTestString("\nERROR: vector2d %.16f, %.16f == vector2d %.16f, %.16f\n",
  150. (f64)zeroZero.X, (f64)zeroZero.Y, (f64)oneOne.X, (f64)oneOne.Y);
  151. return false;
  152. }
  153. return true;
  154. }
  155. /** Test the functionality of vector2d<T>, particularly methods that
  156. involve calculations done using different precision than <T>.
  157. Note that all reference vector2d<T>s are creating using double precision
  158. values cast to (T), as we need to test <f64>. */
  159. bool testVector2d(void)
  160. {
  161. bool f32Success = doTests<f32>();
  162. if(f32Success)
  163. logTestString("vector2df tests passed\n\n");
  164. else
  165. logTestString("\n*** vector2df tests failed ***\n\n");
  166. bool f64Success = doTests<f64>();
  167. if(f64Success)
  168. logTestString("vector2d<f64> tests passed\n\n");
  169. else
  170. logTestString("\n*** vector2d<f64> tests failed ***\n\n");
  171. bool s32Success = doTests<s32>();
  172. if(s32Success)
  173. logTestString("vector2di tests passed\n\n");
  174. else
  175. logTestString("\n*** vector2di tests failed ***\n\n");
  176. return f32Success && f64Success && s32Success;
  177. }