StrengthLine.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. std::pair < float, float >
  37. StrengthLine::equation () const
  38. {
  39. // This method can be called only if a and b are
  40. // not aligned into a vertical or horizontal line
  41. float a = ((p1->y () - p2->y ()) * 1.0) / (p1->x () - p2->x ());
  42. float b = p1->y () - a * p1->x ();
  43. return std::make_pair (a, b);
  44. }
  45. std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const
  46. {
  47. // Methode qui va calculer les points pour l'affichage des droites
  48. // sur l'image passées en paramètre. Calcul rapide donc
  49. // les points peuvent sortir de l'image.
  50. // ligne verticale
  51. if (p1->x () == p2->x ())
  52. return std::make_pair (new QPoint (p1->x (), 0),
  53. new QPoint (p1->x (), img->height ()));
  54. // ligne horizontale
  55. if (p1->y () == p2->y ())
  56. return std::make_pair (new QPoint (0, p1->y ()),
  57. new QPoint (img->width (), p1->y ()));
  58. // Equation de la droite et on prend les points gauche et droite de la ligne
  59. std::pair < float, float >
  60. eq = this->equation ();
  61. float
  62. a = std::get < 0 > (eq);
  63. float
  64. b = std::get < 1 > (eq);
  65. return std::make_pair (new QPoint (0, b),
  66. new QPoint (img->width (), a * img->width () + b));
  67. }
  68. std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
  69. {
  70. // Methode qui va calculer les points sur les bords des images
  71. // en fonction des deux points attributs qui définissent
  72. // la ligne.
  73. // ligne verticale
  74. if (p1->x () == p2->x ())
  75. return std::make_pair (new QPoint (p1->x (), 0),
  76. new QPoint (p1->x (), img->height ()));
  77. // ligne horizontale
  78. if (p1->y () == p2->y ())
  79. return std::make_pair (new QPoint (0, p1->y ()),
  80. new QPoint (img->width (), p1->y ()));
  81. // Equation de la droite et on prend les points gauche et droite de la ligne
  82. std::pair < float, float >
  83. eq = this->equation ();
  84. float
  85. a = std::get < 0 > (eq);
  86. float
  87. b = std::get < 1 > (eq);
  88. if(a>0)
  89. {
  90. if(b<0)
  91. {
  92. if((a*img->width()+b) > img->height())
  93. {
  94. // |------------------------------------------|
  95. // | * |
  96. // | * |
  97. // | * |
  98. // | * |
  99. // | * |
  100. // | * |
  101. // | * |
  102. // | * |
  103. // | * |
  104. // |------------------------------------------|
  105. return std::make_pair (new QPoint (-1*(b/a), 0),
  106. new QPoint ((img->height()-b)/a, img->height ()));
  107. }
  108. else
  109. {
  110. // |------------------------------------------|
  111. // | *** |
  112. // | *** |
  113. // | *** |
  114. // | *** |
  115. // | ***|
  116. // | |
  117. // | |
  118. // | |
  119. // | |
  120. // |------------------------------------------|
  121. return std::make_pair (new QPoint (-1*(b/a), 0),
  122. new QPoint (img->width (), a * img->width () + b));
  123. }
  124. }
  125. else // a>0 and b>0
  126. {
  127. if((a*img->width()+b) > img->height())
  128. {
  129. // |------------------------------------------|
  130. // | |
  131. // | |
  132. // |* |
  133. // | * |
  134. // | * |
  135. // | * |
  136. // | * |
  137. // | * |
  138. // | * |
  139. // |------------------------------------------|
  140. return std::make_pair (new QPoint (0, b),
  141. new QPoint ((img->height () -b )/a, img->height ()));
  142. }
  143. else
  144. {
  145. // |------------------------------------------|
  146. // | |
  147. // |******** |
  148. // | ******** |
  149. // | ******** |
  150. // | ******** |
  151. // | ******** |
  152. // | **|
  153. // | |
  154. // | |
  155. // |------------------------------------------|
  156. return std::make_pair (new QPoint (0, b),
  157. new QPoint (img->width(), a*img->width ()+b));
  158. }
  159. }
  160. }
  161. else // a<0
  162. {
  163. if((a*img->width()+b)>0)
  164. {
  165. if(b > img->height())
  166. {
  167. // |------------------------------------------|
  168. // | |
  169. // | |
  170. // | * |
  171. // | * |
  172. // | * |
  173. // | * |
  174. // | * |
  175. // | * |
  176. // | * |
  177. // |------------------------------------------|
  178. return std::make_pair (new QPoint ((img->height()-b)/a, img->height()),
  179. new QPoint (img->width(), a*img->width()+b));
  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. else // a>0 and (a*img->width()+b) < 0
  199. {
  200. if(b > img->height())
  201. {
  202. // |------------------------------------------|
  203. // | * |
  204. // | * |
  205. // | * |
  206. // | * |
  207. // | * |
  208. // | * |
  209. // | * |
  210. // | * |
  211. // | * |
  212. // |------------------------------------------|
  213. return std::make_pair (new QPoint (-1*(b/a), 0),
  214. new QPoint ((img->height()-b)/a, img->height ()));
  215. }
  216. else
  217. {
  218. // |------------------------------------------|
  219. // | * |
  220. // | * |
  221. // | * |
  222. // | * |
  223. // |* |
  224. // | |
  225. // | |
  226. // | |
  227. // | |
  228. // |------------------------------------------|
  229. return std::make_pair (new QPoint (0, b),
  230. new QPoint (-1*(b/a), 0));
  231. }
  232. }
  233. }
  234. return std::make_pair (new QPoint (*p1),
  235. new QPoint (*p2));
  236. }