testLine2d.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. static bool testLines(line2df const & line1,
  7. line2df const & line2,
  8. bool expectedHit,
  9. const vector2df & expectedIntersection)
  10. {
  11. bool gotExpectedResult = true;
  12. logTestString("\nLine 1 = %.1f %.1f to %.1f %.1f \n",
  13. line1.start.X, line1.start.Y,
  14. line1.end.X, line1.end.Y);
  15. logTestString("Line 2 = %.1f %.1f to %.1f %.1f\n",
  16. line2.start.X, line2.start.Y,
  17. line2.end.X, line2.end.Y);
  18. vector2df intersection;
  19. logTestString("line1 with line2 = ");
  20. if(line1.intersectWith(line2, intersection))
  21. {
  22. logTestString("hit at %.1f %.1f - ",
  23. intersection.X, intersection.Y);
  24. if(!line1.isPointOnLine(intersection) || !line2.isPointOnLine(intersection))
  25. {
  26. logTestString("ERROR! point is not on both lines - ");
  27. gotExpectedResult = false;
  28. }
  29. if(expectedHit)
  30. {
  31. if(intersection == expectedIntersection)
  32. {
  33. logTestString("expected\n");
  34. }
  35. else
  36. {
  37. logTestString("unexpected intersection (expected %.1f %.1f)\n",
  38. expectedIntersection.X, expectedIntersection.Y);
  39. gotExpectedResult = false;
  40. }
  41. }
  42. else
  43. {
  44. logTestString("UNEXPECTED\n");
  45. gotExpectedResult = false;
  46. }
  47. }
  48. else
  49. {
  50. logTestString("miss - ");
  51. if(!expectedHit)
  52. {
  53. logTestString("expected\n");
  54. }
  55. else
  56. {
  57. logTestString("UNEXPECTED\n");
  58. gotExpectedResult = false;
  59. }
  60. }
  61. logTestString("line2 with line1 = ");
  62. if(line2.intersectWith(line1, intersection))
  63. {
  64. logTestString("hit at %.1f %.1f - ",
  65. intersection.X, intersection.Y);
  66. if(!line1.isPointOnLine(intersection) || !line2.isPointOnLine(intersection))
  67. {
  68. logTestString("ERROR! point is not on both lines - ");
  69. gotExpectedResult = false;
  70. }
  71. if(expectedHit)
  72. {
  73. if(intersection == expectedIntersection)
  74. {
  75. logTestString("expected\n");
  76. }
  77. else
  78. {
  79. logTestString("unexpected intersection (expected %.1f %.1f)\n",
  80. expectedIntersection.X, expectedIntersection.Y);
  81. gotExpectedResult = false;
  82. }
  83. }
  84. else
  85. {
  86. logTestString("UNEXPECTED\n");
  87. gotExpectedResult = false;
  88. }
  89. }
  90. else
  91. {
  92. logTestString("miss - ");
  93. if(!expectedHit)
  94. {
  95. logTestString("expected\n");
  96. }
  97. else
  98. {
  99. logTestString("UNEXPECTED\n");
  100. gotExpectedResult = false;
  101. }
  102. }
  103. return gotExpectedResult;
  104. }
  105. // Test the functionality of line2d>T>::intersectWith().
  106. /** Validation is done with assert_log() against expected results. */
  107. bool line2dIntersectWith(void)
  108. {
  109. bool allExpected = true;
  110. // Crossing lines, horizontal and vertical
  111. allExpected &= testLines(line2df(vector2df(1,1),vector2df(1,3)),
  112. line2df(vector2df(0,2),vector2df(2,2)),
  113. true, vector2df(1,2));
  114. assert_log(allExpected);
  115. // Crossing lines, both diagonal
  116. allExpected &= testLines(line2df(vector2df(0,0),vector2df(2,2)),
  117. line2df(vector2df(0,2),vector2df(2,0)),
  118. true, vector2df(1,1));
  119. assert_log(allExpected);
  120. // Non-crossing lines, horizontal and vertical
  121. allExpected &= testLines(line2df(vector2df(1,1),vector2df(1,3)),
  122. line2df(vector2df(0,4),vector2df(2,4)),
  123. false, vector2df());
  124. assert_log(allExpected);
  125. // Non-crossing lines, both diagonal
  126. allExpected &= testLines(line2df(vector2df(0,0),vector2df(2,2)),
  127. line2df(vector2df(3,4),vector2df(4,3)),
  128. false, vector2df());
  129. assert_log(allExpected);
  130. // Meeting at a common point
  131. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  132. line2df(vector2df(1,0),vector2df(2,0)),
  133. true, vector2df(1,0));
  134. assert_log(allExpected);
  135. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  136. line2df(vector2df(1,0),vector2df(0,1)),
  137. true, vector2df(1,0));
  138. assert_log(allExpected);
  139. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  140. line2df(vector2df(1,0),vector2df(0,-1)),
  141. true, vector2df(1,0));
  142. assert_log(allExpected);
  143. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  144. line2df(vector2df(0,1),vector2df(1,1)),
  145. true, vector2df(0,1));
  146. assert_log(allExpected);
  147. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  148. line2df(vector2df(0,1),vector2df(1,-1)),
  149. true, vector2df(0,1));
  150. assert_log(allExpected);
  151. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  152. line2df(vector2df(0,1),vector2df(0,2)),
  153. true, vector2df(0,1));
  154. assert_log(allExpected);
  155. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  156. line2df(vector2df(1,0),vector2df(2,0)),
  157. true, vector2df(1,0));
  158. assert_log(allExpected);
  159. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,1)),
  160. line2df(vector2df(1,1),vector2df(0,2)),
  161. true, vector2df(1,1));
  162. assert_log(allExpected);
  163. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,1)),
  164. line2df(vector2df(1,1),vector2df(2,0)),
  165. true, vector2df(1,1));
  166. assert_log(allExpected);
  167. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,1)),
  168. line2df(vector2df(1,1),vector2df(2,2)),
  169. true, vector2df(1,1));
  170. assert_log(allExpected);
  171. // Parallel lines, no intersection
  172. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  173. line2df(vector2df(0,1),vector2df(1,1)),
  174. false, vector2df());
  175. assert_log(allExpected);
  176. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  177. line2df(vector2df(1,0),vector2df(1,1)),
  178. false, vector2df());
  179. assert_log(allExpected);
  180. // Non parallel lines, no intersection
  181. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  182. line2df(vector2df(0,1),vector2df(0,2)),
  183. false, vector2df());
  184. assert_log(allExpected);
  185. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  186. line2df(vector2df(1,0),vector2df(2,0)),
  187. false, vector2df());
  188. assert_log(allExpected);
  189. // Coincident (and thus parallel) lines
  190. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  191. line2df(vector2df(0,0),vector2df(1,0)),
  192. true, vector2df(0,0));
  193. assert_log(allExpected);
  194. allExpected &= testLines(line2df(vector2df(2,0),vector2df(0,2)),
  195. line2df(vector2df(2,0),vector2df(0,2)),
  196. true, vector2df(2,0));
  197. assert_log(allExpected);
  198. // Two segments of the same unlimited line, but no intersection
  199. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,1)),
  200. line2df(vector2df(2,2),vector2df(3,3)),
  201. false, vector2df());
  202. assert_log(allExpected);
  203. allExpected &= testLines(line2df(vector2df(0,0),vector2df(1,0)),
  204. line2df(vector2df(2,0),vector2df(3,0)),
  205. false, vector2df());
  206. assert_log(allExpected);
  207. allExpected &= testLines(line2df(vector2df(0,0),vector2df(0,1)),
  208. line2df(vector2df(0,2),vector2df(0,3)),
  209. false, vector2df());
  210. assert_log(allExpected);
  211. // Overlapping parallel lines
  212. allExpected &= testLines(line2df(vector2df(1,0),vector2df(2,0)),
  213. line2df(vector2df(0,0),vector2df(3,0)),
  214. true, vector2df(1.5f, 0));
  215. assert_log(allExpected);
  216. allExpected &= testLines(line2df(vector2df(0,1),vector2df(0,2)),
  217. line2df(vector2df(0,0),vector2df(0,3)),
  218. true, vector2df(0, 1.5f));
  219. assert_log(allExpected);
  220. allExpected &= testLines(line2df(vector2df(1,0),vector2df(2,0)),
  221. line2df(vector2df(0,0),vector2df(3,0)),
  222. true, vector2df(1.5f, 0));
  223. assert_log(allExpected);
  224. allExpected &= testLines(line2df(vector2df(0,1),vector2df(0,2)),
  225. line2df(vector2df(0,0),vector2df(0,3)),
  226. true, vector2df(0, 1.5f));
  227. assert_log(allExpected);
  228. allExpected &= testLines(line2df(vector2df(1,1),vector2df(2,2)),
  229. line2df(vector2df(0,0),vector2df(3,3)),
  230. true, vector2df(1.5f, 1.5f));
  231. assert_log(allExpected);
  232. allExpected &= testLines(line2df(vector2df(1,2),vector2df(2,1)),
  233. line2df(vector2df(0,3),vector2df(3,0)),
  234. true, vector2df(1.5f, 1.5f));
  235. assert_log(allExpected);
  236. allExpected &= testLines(line2df(vector2df(0,0),vector2df(10,8)),
  237. line2df(vector2df(2.5f,2.0f),vector2df(5.0f,4.0f)),
  238. true, vector2df(3.75f, 3.0f));
  239. assert_log(allExpected);
  240. allExpected &= testLines(line2df(vector2df(0,0),vector2df(2000,1000)),
  241. line2df(vector2df(2,1),vector2df(2.2f,1.4f)),
  242. true, vector2df(2.0f, 1.0f));
  243. assert_log(allExpected);
  244. if(!allExpected)
  245. logTestString("\nline2dIntersectWith failed\n");
  246. return allExpected;
  247. }
  248. bool getClosestPoint(void)
  249. {
  250. // testcase that fails when integers are handled like floats
  251. irr::core::line2di line(-283, -372, 374, 289);
  252. irr::core::vector2di p1 = line.getClosestPoint( irr::core::vector2di(290,372) );
  253. irr::core::vector2di p2 = line.getClosestPoint( irr::core::vector2di(135,372) );
  254. if( p1 == p2 )
  255. {
  256. logTestString("getClosestPoint failed\n");
  257. return false;
  258. }
  259. return true;
  260. }
  261. bool testLine2d(void)
  262. {
  263. bool allExpected = true;
  264. allExpected &= line2dIntersectWith();
  265. allExpected &= getClosestPoint();
  266. if(allExpected)
  267. logTestString("\nAll tests passed\n");
  268. else
  269. logTestString("\nFAIL!\n");
  270. return allExpected;
  271. }