testVector2d.cpp 6.8 KB

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