testVector3d.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. otherVec = vector3d<T>(1,2,3);
  178. otherVec[0] = vec[0];
  179. otherVec[1] = vec[1];
  180. otherVec[2] = vec[2];
  181. if(!vec.equals(otherVec))
  182. {
  183. logTestString("vector3d::operator[] failed\n");
  184. assert_log(0);
  185. return false;
  186. }
  187. vec.set(5, 5, 0);
  188. otherVec.set(10, 20, 0);
  189. if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))
  190. {
  191. logTestString("vector3d::getDistanceFrom() failed\n");
  192. assert_log(0);
  193. return false;
  194. }
  195. if (!checkRotations<T>())
  196. return false;
  197. if (!checkInterpolation<T>())
  198. return false;
  199. if (!checkAngleCalculations<T>())
  200. return false;
  201. vec.set(0,0,0);
  202. vec.setLength(99);
  203. if ( is_nan(vec) )
  204. return false;
  205. core::vector3d<T> zeroZero(0, 0, 0);
  206. core::vector3d<T> oneOne(1, 1, 1);
  207. // Check if comparing (0.0, 0.0, 0.0) with (1.0, 1.0, 1.0) returns false.
  208. if(zeroZero == oneOne)
  209. {
  210. logTestString("\nERROR: vector3d %.16f, %.16f, %.16f == vector3d %.16f, %.16f, %.16f\n",
  211. (f64)zeroZero.X, (f64)zeroZero.Y, (f64)zeroZero.Z,
  212. (f64)oneOne.X, (f64)oneOne.Y, (f64)oneOne.Z);
  213. return false;
  214. }
  215. vec.set(5, 5, 0);
  216. otherVec.set(10, 20, 40);
  217. LESS_VECTORS(vec, otherVec);
  218. LESS_EQUAL_VECTORS(vec, otherVec);
  219. MORE_VECTORS(otherVec, vec);
  220. MORE_EQUAL_VECTORS(otherVec, vec);
  221. vec.set(-1,-1,1);
  222. otherVec.set(1,-1,1);
  223. LESS_VECTORS(vec, otherVec);
  224. LESS_EQUAL_VECTORS(vec, otherVec);
  225. MORE_VECTORS(otherVec, vec);
  226. MORE_EQUAL_VECTORS(otherVec, vec);
  227. LESS_EQUAL_VECTORS(vec, vec);
  228. MORE_EQUAL_VECTORS(vec, vec);
  229. return true;
  230. }
  231. /** Test the functionality of vector3d<T>, particularly methods that
  232. involve calculations done using different precision than <T>.
  233. Note that all reference vector3d<T>s are creating using double precision
  234. values cast to (T), as we need to test <f64>. */
  235. bool testVector3d(void)
  236. {
  237. const bool f32Success = doTests<f32>();
  238. if (f32Success)
  239. logTestString("vector3df tests passed\n\n");
  240. else
  241. logTestString("\n*** vector3df tests failed ***\n\n");
  242. const bool f64Success = doTests<f64>();
  243. if (f64Success)
  244. logTestString("vector3d<f64> tests passed\n\n");
  245. else
  246. logTestString("\n*** vector3d<f64> tests failed ***\n\n");
  247. const bool s32Success = doTests<s32>();
  248. if (s32Success)
  249. logTestString("vector3di tests passed\n\n");
  250. else
  251. logTestString("\n*** vector3di tests failed ***\n\n");
  252. return f32Success && f64Success && s32Success;
  253. }