Coordinates.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*!
  2. * \file Coordinates.h
  3. * \author The VLE Development Team
  4. * See the AUTHORS or Authors.txt file
  5. * \version 2.0
  6. * \date 3 juin 2013
  7. */
  8. /*
  9. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  10. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COORDINATES_H_
  26. #define COORDINATES_H_
  27. #include <iostream>
  28. #include <sstream>
  29. #include <fstream>
  30. #include "Parameters.h"
  31. /*! \class Coordinates
  32. * \brief représente les coordonnées en 2D d'un point du bassin versant.
  33. */
  34. class Coordinates
  35. {
  36. private :
  37. short _line; /*!< numéro de ligne */
  38. short _column; /*!< numéro de colonne */
  39. public :
  40. static int _columnsNumber; /*!< nombre de colonnes */
  41. /*!
  42. * \brief Constructeur
  43. *
  44. * Constructeur par défaut de la classe Coordinates.
  45. * Initialise les attributs à 0.
  46. *
  47. * \return void.
  48. */
  49. Coordinates(): _line(0), _column(0) {};
  50. /*!
  51. * \brief Constructeur
  52. *
  53. * Constructeur avec paramètres de la classe Coordinates.
  54. *
  55. * \param line : numéro de la ligne.
  56. * \param column : : numéro de la colonne.
  57. * \return void.
  58. */
  59. Coordinates(short line, short column) : _line(line),_column(column) {}
  60. /*!
  61. * \brief Constructeur
  62. *
  63. * Constructeur par copie de la classe Coordinates.
  64. *
  65. * \param c : Instance de la classe Coordonnées à copier.
  66. * \return void.
  67. */
  68. Coordinates(const Coordinates &c): _line(c.getLine()), _column(c.getColumn()){}
  69. /*!
  70. * \brief Constructeur
  71. *
  72. * Constructeur avec paramètres de la classe Coordinates.
  73. *
  74. * \param nodeCoordinates : Coordonnées de base indispensable pour le calcul de la coordonnée voisine.
  75. * \param identifiantVoisin : détermine la position du voisin par rapport à la coordonnées de base.
  76. * \return void.
  77. */
  78. Coordinates(const Coordinates & nodeCoordinates, char identifiantVoisin)
  79. {
  80. switch (identifiantVoisin)
  81. {
  82. case '1' :
  83. _line = nodeCoordinates.getLine() - 1 ;
  84. _column = nodeCoordinates.getColumn() ;
  85. break ;
  86. case '2' :
  87. _line = nodeCoordinates.getLine() - 1 ;
  88. _column = nodeCoordinates.getColumn() + 1 ;
  89. break ;
  90. case '3':
  91. _line = nodeCoordinates.getLine() ;
  92. _column = nodeCoordinates.getColumn() + 1 ;
  93. break ;
  94. case '4' :
  95. _line = nodeCoordinates.getLine() + 1 ;
  96. _column = nodeCoordinates.getColumn() + 1 ;
  97. break ;
  98. case '5' :
  99. _line = nodeCoordinates.getLine() + 1 ;
  100. _column = nodeCoordinates.getColumn() ;
  101. break ;
  102. case '6' :
  103. _line = nodeCoordinates.getLine() + 1 ;
  104. _column = nodeCoordinates.getColumn() - 1 ;
  105. break ;
  106. case '7' :
  107. _line = nodeCoordinates.getLine() ;
  108. _column = nodeCoordinates.getColumn() - 1 ;
  109. break ;
  110. case '8' :
  111. _line = nodeCoordinates.getLine() - 1 ;
  112. _column = nodeCoordinates.getColumn() - 1 ;
  113. break ;
  114. }
  115. }
  116. void operator = (const Coordinates& c)
  117. {
  118. _line = c.getLine();
  119. _column = c.getColumn();
  120. }
  121. /*!
  122. * \brief Destructeur
  123. *
  124. * Destructeur de la classe Coordinates
  125. */
  126. virtual ~Coordinates()
  127. {
  128. }
  129. short getColumn() const
  130. {
  131. return _column;
  132. }
  133. void setColumn(short colonne)
  134. {
  135. this->_column = colonne;
  136. }
  137. short getLine() const
  138. {
  139. return _line;
  140. }
  141. void setLine(int ligne)
  142. {
  143. this->_line = ligne;
  144. }
  145. /*!
  146. * \brief Transformer en chaîne.
  147. *
  148. * Représentation chaînée de l'objet
  149. *
  150. * \param void
  151. * \return string
  152. */
  153. const std::string toString() const
  154. {
  155. std::stringstream ss;
  156. ss<<"Ligne "<<_line<<" Colonne "<<_column<<" ";
  157. return ss.str();
  158. }
  159. /*!
  160. * \brief clé.
  161. *
  162. * Génère un identifiant unique (sans doublons) en fonction du numéro de la ligne et de la colonne et du nombre de colonnes.
  163. *
  164. * \param void
  165. * \return identifiant de la coordonnées.
  166. *
  167. */
  168. int key() const
  169. { return (_line * _columnsNumber + _column); }
  170. virtual bool operator==(const Coordinates& coordinates) const
  171. { return key() == coordinates.key(); }
  172. virtual bool operator!=(const Coordinates& coordinates) const
  173. {return key() != coordinates.key();}
  174. virtual bool operator<(const Coordinates& coordinates) const
  175. { return key() < coordinates.key(); }
  176. /*!
  177. * \brief est valide ?
  178. *
  179. * Vérifie que la coordonnées correspond bien à un point du bassin versant.
  180. * \param p : paramètres du programme et du bassin versant.
  181. * \return true si elle est située dans le bassin versant, false sinon.
  182. *
  183. */
  184. bool isValid (Parameters * p)
  185. {
  186. return (_line>=0 && _line <p->_linesNumber && _column>=0 && _column < p->_columnsNumber) ;
  187. }
  188. /*!
  189. * \brief est sur la même ligne ?
  190. *
  191. * Vérifie que si les lignes sont identiques.
  192. * \param line : numéro de la ligne.
  193. * \return true si elles sont identiques, false sinon.
  194. *
  195. */
  196. bool isSameLine(short line) const
  197. {
  198. return (_line == line);
  199. }
  200. /*!
  201. * \brief est sur la même colonne ?
  202. *
  203. * Vérifie que si les colonnes sont identiques.
  204. * \param column : numéro de la colonne.
  205. * \return true si elles sont identiques, false sinon.
  206. *
  207. */
  208. bool isSameColumn(short column) const
  209. {
  210. return (_column == column);
  211. }
  212. /*!
  213. * \brief est sur la même ligne ?
  214. *
  215. * Vérifie que si les lignes sont identiques.
  216. * \param c : Coordonnées à tester.
  217. * \return true si elles sont identiques, false sinon.
  218. *
  219. */
  220. bool isSameLine(const Coordinates & c) const
  221. {
  222. return (_line == c.getLine());
  223. }
  224. /*!
  225. * \brief est sur la même colonne ?
  226. *
  227. * Vérifie que si les colonnes sont identiques.
  228. * \param c : Coordonnées à tester.
  229. * \return true si elles sont identiques, false sinon.
  230. *
  231. */
  232. bool isSameColumn(const Coordinates & c) const
  233. {
  234. return (_column == c.getColumn());
  235. }
  236. /*!
  237. * \brief est sur le contour ?
  238. *
  239. * Vérifie si la coordonnées se situe sur le contour du bassin versant.
  240. * \param p : paramètres du programme et du bassin versant.
  241. * \return true si elle est située sur le contour du bassin versant, false sinon.
  242. *
  243. */
  244. bool isOnShore(const Parameters * p) const
  245. {
  246. if (_line == 0)
  247. return true;
  248. else if( _column == 0)
  249. return true;
  250. else if(_line == p->_linesNumber - 1)
  251. return true;
  252. else if (_column == p->_columnsNumber - 1)
  253. return true;
  254. else
  255. return false;
  256. }
  257. /*!
  258. * \brief Envoyer vers fichier binaire
  259. *
  260. * Écrit l'objet dans un fichier binaire.
  261. * \param file : fichier ou écrire les données.
  262. * \return void.
  263. *
  264. */
  265. void toBinaryFile(std::ofstream & file)
  266. {
  267. file.write(reinterpret_cast<char *>(&_line),sizeof(short));
  268. file.write(reinterpret_cast<char *>(&_column),sizeof(short));
  269. }
  270. };
  271. #endif /* COORDINATES_H_ */