StrengthLine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. // Methode qui va calculer les points pour l'affichage des droites
  60. // sur l'image passées en paramètre. Calcul rapide donc
  61. // les points peuvent sortir de l'image.
  62. // ligne verticale
  63. if (p1->x () == p2->x ())
  64. return std::make_pair (new QPoint (p1->x (), 0),
  65. new QPoint (p1->x (), img->height ()));
  66. // ligne horizontale
  67. if (p1->y () == p2->y ())
  68. return std::make_pair (new QPoint (0, p1->y ()),
  69. new QPoint (img->width (), p1->y ()));
  70. // Equation de la droite et on prend les points gauche et droite de la ligne
  71. std::pair < float, float >
  72. eq = this->equation ();
  73. float
  74. a = std::get < 0 > (eq);
  75. float
  76. b = std::get < 1 > (eq);
  77. return std::make_pair (new QPoint (0, b),
  78. new QPoint (img->width (), a * img->width () + b));
  79. }
  80. std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
  81. {
  82. // Methode qui va calculer les points sur les bords des images
  83. // en fonction des deux points attributs qui définissent
  84. // la ligne.
  85. // ligne verticale
  86. if (p1->x () == p2->x ())
  87. return std::make_pair (new QPoint (p1->x (), 0),
  88. new QPoint (p1->x (), img->height ()));
  89. // ligne horizontale
  90. if (p1->y () == p2->y ())
  91. return std::make_pair (new QPoint (0, p1->y ()),
  92. new QPoint (img->width (), p1->y ()));
  93. // Equation de la droite et on prend les points gauche et droite de la ligne
  94. std::pair < float, float >
  95. eq = this->equation ();
  96. float
  97. a = std::get < 0 > (eq);
  98. float
  99. b = std::get < 1 > (eq);
  100. if(a>0)
  101. {
  102. if(b<0)
  103. {
  104. if((a*img->width()+b) > img->height())
  105. {
  106. // |------------------------------------------|
  107. // | * |
  108. // | * |
  109. // | * |
  110. // | * |
  111. // | * |
  112. // | * |
  113. // | * |
  114. // | * |
  115. // | * |
  116. // |------------------------------------------|
  117. return std::make_pair (new QPoint (-1*(b/a), 0),
  118. new QPoint ((img->height()-b)/a, img->height ()));
  119. }
  120. else
  121. {
  122. // |------------------------------------------|
  123. // | *** |
  124. // | *** |
  125. // | *** |
  126. // | *** |
  127. // | ***|
  128. // | |
  129. // | |
  130. // | |
  131. // | |
  132. // |------------------------------------------|
  133. return std::make_pair (new QPoint (-1*(b/a), 0),
  134. new QPoint (img->width (), a * img->width () + b));
  135. }
  136. }
  137. else // a>0 and b>0
  138. {
  139. if((a*img->width()+b) > img->height())
  140. {
  141. // |------------------------------------------|
  142. // | |
  143. // | |
  144. // |* |
  145. // | * |
  146. // | * |
  147. // | * |
  148. // | * |
  149. // | * |
  150. // | * |
  151. // |------------------------------------------|
  152. return std::make_pair (new QPoint (0, b),
  153. new QPoint ((img->height () -b )/a, img->height ()));
  154. }
  155. else
  156. {
  157. // |------------------------------------------|
  158. // | |
  159. // |******** |
  160. // | ******** |
  161. // | ******** |
  162. // | ******** |
  163. // | ******** |
  164. // | **|
  165. // | |
  166. // | |
  167. // |------------------------------------------|
  168. return std::make_pair (new QPoint (0, b),
  169. new QPoint (img->width(), a*img->width ()+b));
  170. }
  171. }
  172. }
  173. else // a<0
  174. {
  175. if((a*img->width()+b)>0)
  176. {
  177. if(b > img->height())
  178. {
  179. // |------------------------------------------|
  180. // | |
  181. // | |
  182. // | * |
  183. // | * |
  184. // | * |
  185. // | * |
  186. // | * |
  187. // | * |
  188. // | * |
  189. // |------------------------------------------|
  190. return std::make_pair (new QPoint ((img->height()-b)/a, img->height()),
  191. new QPoint (img->width(), a*img->width()+b));
  192. }
  193. else
  194. {
  195. // |------------------------------------------|
  196. // | |
  197. // | |
  198. // | **|
  199. // | ******** |
  200. // | ******** |
  201. // | ******** |
  202. // | ******** |
  203. // |******** |
  204. // | |
  205. // |------------------------------------------|
  206. return std::make_pair (new QPoint (0, b),
  207. new QPoint (img->width (), a * img->width () + b));
  208. }
  209. }
  210. else // a>0 and (a*img->width()+b) < 0
  211. {
  212. if(b > img->height())
  213. {
  214. // |------------------------------------------|
  215. // | * |
  216. // | * |
  217. // | * |
  218. // | * |
  219. // | * |
  220. // | * |
  221. // | * |
  222. // | * |
  223. // | * |
  224. // |------------------------------------------|
  225. return std::make_pair (new QPoint (-1*(b/a), 0),
  226. new QPoint ((img->height()-b)/a, img->height ()));
  227. }
  228. else
  229. {
  230. // |------------------------------------------|
  231. // | * |
  232. // | * |
  233. // | * |
  234. // | * |
  235. // |* |
  236. // | |
  237. // | |
  238. // | |
  239. // | |
  240. // |------------------------------------------|
  241. return std::make_pair (new QPoint (0, b),
  242. new QPoint (-1*(b/a), 0));
  243. }
  244. }
  245. }
  246. return std::make_pair (new QPoint (*p1),
  247. new QPoint (*p2));
  248. }
  249. StrengthLine* StrengthLine::getRandomLine (QImage * img)
  250. {
  251. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  252. int x1rand = std::rand() % img->width();
  253. int x2rand = std::rand() % img->width();
  254. int y1rand = std::rand() % img->height();
  255. int y2rand = std::rand() % img->height();
  256. while ( x1rand == x2rand && y1rand==y2rand )
  257. {
  258. x2rand = std::rand() % img->width();
  259. y2rand = std::rand() % img->height();
  260. }
  261. return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
  262. }
  263. StrengthLine* StrengthLine::getRandomLine (int width, int height)
  264. {
  265. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  266. int x1rand = std::rand() % width;
  267. int x2rand = std::rand() % width;
  268. int y1rand = std::rand() % height;
  269. int y2rand = std::rand() % height;
  270. while ( x1rand == x2rand && y1rand==y2rand )
  271. {
  272. x2rand = std::rand() % width;
  273. y2rand = std::rand() % height;
  274. }
  275. return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
  276. }