StrengthLine.cpp 11 KB

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