StrengthLine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*********************************************************************/
  2. /* */
  3. /* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr */
  4. /* */
  5. /* This file is part of DSL. */
  6. /* This software uses Qt to build the Graphical User Interface */
  7. /* https://www.qt.io/ */
  8. /* */
  9. /* DSL is free software: you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published */
  11. /* by the Free Software Foundation, either version 3 of the License, */
  12. /* or (at your option) any later version. */
  13. /* */
  14. /* DSL is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU General Public License for more details. */
  18. /* */
  19. /* You should have received a copy of the GNU General Public License */
  20. /* along with DSL. If not, see <http://www.gnu.org/licenses/>. */
  21. /* */
  22. /*********************************************************************/
  23. #include <cstdlib>
  24. #include <ctime>
  25. #include <stdexcept>
  26. #include "StrengthLine.hpp"
  27. StrengthLine::StrengthLine (QPoint * p1, QPoint * p2)
  28. {
  29. if (p1 == NULL || p2 == NULL)
  30. throw std::
  31. invalid_argument ("Points defining a strength line can't be NULL.");
  32. if (p1->x () == p2->x () && p1->y () == p2->y ())
  33. throw std::
  34. invalid_argument ("Points defining a strength line can't be equals.");
  35. this->p1 = p1;
  36. this->p2 = p2;
  37. }
  38. QPoint*
  39. StrengthLine::getP1() const
  40. {
  41. return new QPoint(*p1);
  42. }
  43. QPoint*
  44. StrengthLine::getP2() const
  45. {
  46. return new QPoint(*p2);
  47. }
  48. std::pair < float, float >
  49. StrengthLine::equation () const
  50. {
  51. // This method can be called only if a and b are
  52. // not aligned into a vertical or horizontal line
  53. float a = ((p1->y () - p2->y ()) * 1.0) / (p1->x () - p2->x ());
  54. float b = p1->y () - a * p1->x ();
  55. return std::make_pair (a, b);
  56. }
  57. std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const
  58. {
  59. /*
  60. // Methode qui va calculer les points pour l'affichage des droites
  61. // sur l'image passées en paramètre. Calcul rapide donc
  62. // les points peuvent sortir de l'image.
  63. // ligne verticale
  64. if (p1->x () == p2->x ())
  65. return std::make_pair (new QPoint (p1->x (), 0),
  66. new QPoint (p1->x (), img->height ()));
  67. // ligne horizontale
  68. if (p1->y () == p2->y ())
  69. return std::make_pair (new QPoint (0, p1->y ()),
  70. new QPoint (img->width (), p1->y ()));
  71. // Equation de la droite et on prend les points gauche et droite de la ligne
  72. std::pair < float, float >
  73. eq = this->equation ();
  74. float
  75. a = std::get < 0 > (eq);
  76. float
  77. b = std::get < 1 > (eq);
  78. return std::make_pair (new QPoint (0, b),
  79. new QPoint (img->width (), a * img->width () + b));
  80. */
  81. return this->toDraw(img->width(), img->height());
  82. }
  83. std::pair < QPoint *, QPoint * >StrengthLine::toDraw (int imgWidth, int imgHeight) const
  84. {
  85. // Methode qui va calculer les points pour l'affichage des droites
  86. // sur l'image passées en paramètre. Calcul rapide donc
  87. // les points peuvent sortir de l'image.
  88. // ligne verticale
  89. if (p1->x () == p2->x ())
  90. return std::make_pair (new QPoint (p1->x (), 0),
  91. new QPoint (p1->x (), imgHeight));
  92. // ligne horizontale
  93. if (p1->y () == p2->y ())
  94. return std::make_pair (new QPoint (0, p1->y ()),
  95. new QPoint (imgWidth, p1->y ()));
  96. // Equation de la droite et on prend les points gauche et droite de la ligne
  97. std::pair < float, float >
  98. eq = this->equation ();
  99. float
  100. a = std::get < 0 > (eq);
  101. float
  102. b = std::get < 1 > (eq);
  103. return std::make_pair (new QPoint (0, b),
  104. new QPoint (imgWidth, a * imgWidth + b));
  105. }
  106. std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
  107. {
  108. // Methode qui va calculer les points sur les bords des images
  109. // en fonction des deux points attributs qui définissent
  110. // la ligne.
  111. // ligne verticale
  112. if (p1->x () == p2->x ())
  113. return std::make_pair (new QPoint (p1->x (), 0),
  114. new QPoint (p1->x (), img->height ()));
  115. // ligne horizontale
  116. if (p1->y () == p2->y ())
  117. return std::make_pair (new QPoint (0, p1->y ()),
  118. new QPoint (img->width (), p1->y ()));
  119. // Equation de la droite et on prend les points gauche et droite de la ligne
  120. std::pair < float, float >
  121. eq = this->equation ();
  122. float
  123. a = std::get < 0 > (eq);
  124. float
  125. b = std::get < 1 > (eq);
  126. if(a>0)
  127. {
  128. if(b<0)
  129. {
  130. if((a*img->width()+b) > img->height())
  131. {
  132. // |------------------------------------------|
  133. // | * |
  134. // | * |
  135. // | * |
  136. // | * |
  137. // | * |
  138. // | * |
  139. // | * |
  140. // | * |
  141. // | * |
  142. // |------------------------------------------|
  143. return std::make_pair (new QPoint (-1*(b/a), 0),
  144. new QPoint ((img->height()-b)/a, img->height ()));
  145. }
  146. else
  147. {
  148. // |------------------------------------------|
  149. // | *** |
  150. // | *** |
  151. // | *** |
  152. // | *** |
  153. // | ***|
  154. // | |
  155. // | |
  156. // | |
  157. // | |
  158. // |------------------------------------------|
  159. return std::make_pair (new QPoint (-1*(b/a), 0),
  160. new QPoint (img->width (), a * img->width () + b));
  161. }
  162. }
  163. else // a>0 and b>0
  164. {
  165. if((a*img->width()+b) > img->height())
  166. {
  167. // |------------------------------------------|
  168. // | |
  169. // | |
  170. // |* |
  171. // | * |
  172. // | * |
  173. // | * |
  174. // | * |
  175. // | * |
  176. // | * |
  177. // |------------------------------------------|
  178. return std::make_pair (new QPoint (0, b),
  179. new QPoint ((img->height () -b )/a, img->height ()));
  180. }
  181. else
  182. {
  183. // |------------------------------------------|
  184. // | |
  185. // |******** |
  186. // | ******** |
  187. // | ******** |
  188. // | ******** |
  189. // | ******** |
  190. // | **|
  191. // | |
  192. // | |
  193. // |------------------------------------------|
  194. return std::make_pair (new QPoint (0, b),
  195. new QPoint (img->width(), a*img->width ()+b));
  196. }
  197. }
  198. }
  199. else // a<0
  200. {
  201. if((a*img->width()+b)>0)
  202. {
  203. if(b > img->height())
  204. {
  205. // |------------------------------------------|
  206. // | |
  207. // | |
  208. // | * |
  209. // | * |
  210. // | * |
  211. // | * |
  212. // | * |
  213. // | * |
  214. // | * |
  215. // |------------------------------------------|
  216. return std::make_pair (new QPoint ((img->height()-b)/a, img->height()),
  217. new QPoint (img->width(), a*img->width()+b));
  218. }
  219. else
  220. {
  221. // |------------------------------------------|
  222. // | |
  223. // | |
  224. // | **|
  225. // | ******** |
  226. // | ******** |
  227. // | ******** |
  228. // | ******** |
  229. // |******** |
  230. // | |
  231. // |------------------------------------------|
  232. return std::make_pair (new QPoint (0, b),
  233. new QPoint (img->width (), a * img->width () + b));
  234. }
  235. }
  236. else // a>0 and (a*img->width()+b) < 0
  237. {
  238. if(b > img->height())
  239. {
  240. // |------------------------------------------|
  241. // | * |
  242. // | * |
  243. // | * |
  244. // | * |
  245. // | * |
  246. // | * |
  247. // | * |
  248. // | * |
  249. // | * |
  250. // |------------------------------------------|
  251. return std::make_pair (new QPoint (-1*(b/a), 0),
  252. new QPoint ((img->height()-b)/a, img->height ()));
  253. }
  254. else
  255. {
  256. // |------------------------------------------|
  257. // | * |
  258. // | * |
  259. // | * |
  260. // | * |
  261. // |* |
  262. // | |
  263. // | |
  264. // | |
  265. // | |
  266. // |------------------------------------------|
  267. return std::make_pair (new QPoint (0, b),
  268. new QPoint (-1*(b/a), 0));
  269. }
  270. }
  271. }
  272. return std::make_pair (new QPoint (*p1),
  273. new QPoint (*p2));
  274. }
  275. double StrengthLine::distance(const StrengthLine *sl) const
  276. {
  277. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  278. return std::rand();
  279. }
  280. StrengthLine* StrengthLine::getRandomLine (QImage * img)
  281. {
  282. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  283. int x1rand = std::rand() % img->width();
  284. int x2rand = std::rand() % img->width();
  285. int y1rand = std::rand() % img->height();
  286. int y2rand = std::rand() % img->height();
  287. while ( x1rand == x2rand && y1rand==y2rand )
  288. {
  289. x2rand = std::rand() % img->width();
  290. y2rand = std::rand() % img->height();
  291. }
  292. return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
  293. }
  294. StrengthLine* StrengthLine::getRandomLine (int width, int height)
  295. {
  296. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  297. int x1rand = std::rand() % width;
  298. int x2rand = std::rand() % width;
  299. int y1rand = std::rand() % height;
  300. int y2rand = std::rand() % height;
  301. while ( x1rand == x2rand && y1rand==y2rand )
  302. {
  303. x2rand = std::rand() % width;
  304. y2rand = std::rand() % height;
  305. }
  306. return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
  307. }