testVector3d.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #define EQUAL_VECTORS(compare, with)\
  7. if(!equalVectors(cmp_equal<core::vector3d<T> >(compare), with)) {assert(false); return false;}
  8. #define LESS_VECTORS(compare, with)\
  9. if(!equalVectors(cmp_less<core::vector3d<T> >(compare), with)) return false;
  10. #define LESS_EQUAL_VECTORS(compare, with)\
  11. if(!equalVectors(cmp_less_equal<core::vector3d<T> >(compare), with)) return false;
  12. #define MORE_VECTORS(compare, with)\
  13. if(!equalVectors(cmp_more<core::vector3d<T> >(compare), with)) return false;
  14. #define MORE_EQUAL_VECTORS(compare, with)\
  15. if(!equalVectors(cmp_more_equal<core::vector3d<T> >(compare), with)) return false;
  16. // check if the vector contains a NAN (a==b is guaranteed to return false in this case)
  17. template<class T>
  18. static bool is_nan(const core::vector3d<T> &vec )
  19. {
  20. return ( !(vec.X == vec.X)
  21. || !(vec.Y == vec.Y)
  22. || !(vec.Z == vec.Z) );
  23. }
  24. template<class T>
  25. struct cmp_less
  26. {
  27. cmp_less(const T& a) : val(a) {}
  28. bool operator()(const T& other) const
  29. {
  30. return val<other;
  31. }
  32. const char* getName() const {return "<";}
  33. const T val;
  34. };
  35. template<class T>
  36. struct cmp_less_equal
  37. {
  38. cmp_less_equal(const T& a) : val(a) {}
  39. bool operator()(const T& other) const
  40. {
  41. return val<=other;
  42. }
  43. const char* getName() const {return "<=";}
  44. const T val;
  45. };
  46. template<class T>
  47. struct cmp_more
  48. {
  49. cmp_more(const T& a) : val(a) {}
  50. bool operator()(const T& other) const
  51. {
  52. return val>other;
  53. }
  54. const char* getName() const {return ">";}
  55. const T val;
  56. };
  57. template<class T>
  58. struct cmp_more_equal
  59. {
  60. cmp_more_equal(const T& a) : val(a) {}
  61. bool operator()(const T& other) const
  62. {
  63. return val>=other;
  64. }
  65. const char* getName() const {return ">=";}
  66. const T val;
  67. };
  68. template<class T>
  69. struct cmp_equal
  70. {
  71. cmp_equal(const T& a) : val(a) {}
  72. bool operator()(const T& other) const
  73. {
  74. return val.equals(other);
  75. }
  76. const char* getName() const {return "==";}
  77. const T val;
  78. };
  79. template<class S, class T>
  80. static bool equalVectors(const S& compare,
  81. const core::vector3d<T> & with)
  82. {
  83. if (!compare(with))
  84. {
  85. logTestString("\nERROR: vector3d %.16f, %.16f, %.16f %s vector3d %.16f, %.16f, %.16f\n",
  86. (f64)compare.val.X, (f64)compare.val.Y, (f64)compare.val.Z, compare.getName(),
  87. (f64)with.X, (f64)with.Y, (f64)with.Z);
  88. assert_log(compare(with));
  89. return false;
  90. }
  91. return true;
  92. }
  93. template <class T>
  94. static bool checkInterpolation()
  95. {
  96. core::vector3d<T> vec(5, 5, 0);
  97. core::vector3d<T> otherVec(10, 20, 40);
  98. vector3d<T> interpolated;
  99. (void)interpolated.interpolate(vec, otherVec, 0.f);
  100. EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
  101. (void)interpolated.interpolate(vec, otherVec, 0.25f);
  102. EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
  103. (void)interpolated.interpolate(vec, otherVec, 0.75f);
  104. EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
  105. (void)interpolated.interpolate(vec, otherVec, 1.f);
  106. EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
  107. interpolated = vec.getInterpolated(otherVec, 0.f);
  108. EQUAL_VECTORS(interpolated, otherVec); // 0.f means all the second vector
  109. interpolated = vec.getInterpolated(otherVec, 0.25f);
  110. EQUAL_VECTORS(interpolated, vector3d<T>((T)8.75, (T)16.25, 30));
  111. interpolated = vec.getInterpolated(otherVec, 0.75f);
  112. EQUAL_VECTORS(interpolated, vector3d<T>((T)6.25, (T)8.75, 10));
  113. interpolated = vec.getInterpolated(otherVec, 1.f);
  114. EQUAL_VECTORS(interpolated, vec); // 1.f means all the first vector
  115. vector3d<T> thirdVec(20, 10, -30);
  116. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.f);
  117. EQUAL_VECTORS(interpolated, vec); // 0.f means all the 1st vector
  118. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.25f);
  119. EQUAL_VECTORS(interpolated, vector3d<T>((T)7.8125, (T)10.9375, (T)13.125));
  120. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.5f);
  121. EQUAL_VECTORS(interpolated, vector3d<T>((T)11.25, (T)13.75, (T)12.5));
  122. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 0.75f);
  123. EQUAL_VECTORS(interpolated, vector3d<T>((T)15.3125, (T)13.4375, (T)-1.875));
  124. interpolated = vec.getInterpolated_quadratic(otherVec, thirdVec, 1.f);
  125. EQUAL_VECTORS(interpolated, thirdVec); // 1.f means all the 3rd vector
  126. return true;
  127. }
  128. template <class T>
  129. static bool checkAngleCalculations()
  130. {
  131. core::vector3d<T> vec(5, 5, 0);
  132. EQUAL_VECTORS(vec.getHorizontalAngle(), vector3d<T>(315, (T)90.0, 0));
  133. EQUAL_VECTORS(vec.getSphericalCoordinateAngles(), vector3d<T>((T)45.0, 0, 0));
  134. return true;
  135. }
  136. template <class T>
  137. static bool checkRotations()
  138. {
  139. core::vector3d<T> vec(5, 5, 0);
  140. vector3d<T> center(0, 0, 0);
  141. vec.rotateXYBy(45, center);
  142. EQUAL_VECTORS(vec, vector3d<T>(0, (T)7.0710678118654755, 0));
  143. vec.normalize();
  144. // TODO: This breaks under Linux/gcc due to FP differences, but is no bug
  145. if (((T)0.5f)>0.f)
  146. EQUAL_VECTORS(vec, vector3d<T>(0, (T)1.0, 0));
  147. vec.set(10, 10, 10);
  148. center.set(5, 5, 10);
  149. vec.rotateXYBy(-5, center);
  150. // -5 means rotate clockwise slightly, so expect the X to increase
  151. // slightly and the Y to decrease slightly.
  152. EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, (T)9.5451947767204359, 10));
  153. vec.set(10, 10, 10);
  154. center.set(5, 10, 5);
  155. vec.rotateXZBy(-5, center);
  156. EQUAL_VECTORS(vec, vector3d<T>((T)10.416752204197017, 10, (T)9.5451947767204359));
  157. vec.set(10, 10, 10);
  158. center.set(10, 5, 5);
  159. vec.rotateYZBy(-5, center);
  160. EQUAL_VECTORS(vec, vector3d<T>(10, (T)10.416752204197017, (T)9.5451947767204359));
  161. vec.set(5, 5, 0);
  162. vec.normalize();
  163. EQUAL_VECTORS(vec, vector3d<T>((T)0.70710681378841400, (T)0.70710681378841400, 0));
  164. return true;
  165. }
  166. template <class T>
  167. static bool doTests()
  168. {
  169. vector3d<T> vec(-5, 5, 0);
  170. vector3d<T> otherVec((T)-5.1, 5, 0);
  171. if(!vec.equals(otherVec, (T)0.1))
  172. {
  173. logTestString("vector3d::equals failed\n");
  174. assert_log(0);
  175. return false;
  176. }
  177. vec.set(5, 5, 0);
  178. otherVec.set(10, 20, 0);
  179. if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
  180. {
  181. logTestString("vector3d::getDistanceFrom() failed\n");
  182. assert_log(0);
  183. return false;
  184. }
  185. if (!checkRotations<T>())
  186. return false;
  187. if (!checkInterpolation<T>())
  188. return false;
  189. if (!checkAngleCalculations<T>())
  190. return false;
  191. vec.set(0,0,0);
  192. vec.setLength(99);
  193. if ( is_nan(vec) )
  194. return false;
  195. core::vector3d<T> zeroZero(0, 0, 0);
  196. core::vector3d<T> oneOne(1, 1, 1);
  197. // Check if comparing (0.0, 0.0, 0.0) with (1.0, 1.0, 1.0) returns false.
  198. if(zeroZero == oneOne)
  199. {
  200. logTestString("\nERROR: vector3d %.16f, %.16f, %.16f == vector3d %.16f, %.16f, %.16f\n",
  201. (f64)zeroZero.X, (f64)zeroZero.Y, (f64)zeroZero.Z,
  202. (f64)oneOne.X, (f64)oneOne.Y, (f64)oneOne.Z);
  203. return false;
  204. }
  205. vec.set(5, 5, 0);
  206. otherVec.set(10, 20, 40);
  207. LESS_VECTORS(vec, otherVec);
  208. LESS_EQUAL_VECTORS(vec, otherVec);
  209. MORE_VECTORS(otherVec, vec);
  210. MORE_EQUAL_VECTORS(otherVec, vec);
  211. vec.set(-1,-1,1);
  212. otherVec.set(1,-1,1);
  213. LESS_VECTORS(vec, otherVec);
  214. LESS_EQUAL_VECTORS(vec, otherVec);
  215. MORE_VECTORS(otherVec, vec);
  216. MORE_EQUAL_VECTORS(otherVec, vec);
  217. LESS_EQUAL_VECTORS(vec, vec);
  218. MORE_EQUAL_VECTORS(vec, vec);
  219. return true;
  220. }
  221. /** Test the functionality of vector3d<T>, particularly methods that
  222. involve calculations done using different precision than <T>.
  223. Note that all reference vector3d<T>s are creating using double precision
  224. values cast to (T), as we need to test <f64>. */
  225. bool testVector3d(void)
  226. {
  227. const bool f32Success = doTests<f32>();
  228. if (f32Success)
  229. logTestString("vector3df tests passed\n\n");
  230. else
  231. logTestString("\n*** vector3df tests failed ***\n\n");
  232. const bool f64Success = doTests<f64>();
  233. if (f64Success)
  234. logTestString("vector3d<f64> tests passed\n\n");
  235. else
  236. logTestString("\n*** vector3d<f64> tests failed ***\n\n");
  237. const bool s32Success = doTests<s32>();
  238. if (s32Success)
  239. logTestString("vector3di tests passed\n\n");
  240. else
  241. logTestString("\n*** vector3di tests failed ***\n\n");
  242. return f32Success && f64Success && s32Success;
  243. }