Layer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. *
  3. * \file Layer.h
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 3 juin 2013
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  11. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #ifndef LAYER_H_
  27. #define LAYER_H_
  28. #include <iostream>
  29. #include <fstream>
  30. #include <boost/lexical_cast.hpp>
  31. #include <boost/algorithm/string.hpp>
  32. #include <boost/tokenizer.hpp>
  33. #include "Constantes.h"
  34. #include "Exception.h"
  35. #include "Functions.h"
  36. class Layer
  37. {
  38. private :
  39. std::string _label ;
  40. int _color ;
  41. int _active ;
  42. int _type ;
  43. bool _modified ;
  44. unsigned char * _data; /*!< données sur couches */
  45. public :
  46. Layer():_label(""),_color(0),_active(0),_type(0),_modified(0), _data(NULL){}
  47. Layer(std::string label, int color,int active,int type, bool modified)
  48. {
  49. _label = label;
  50. _color = color;
  51. _active = active;
  52. _type = type;
  53. _modified = modified;
  54. _data = NULL;
  55. }
  56. Layer(std::string & label, int color,int active,int type, bool modified)
  57. {
  58. _label = label;
  59. _color = color;
  60. _active = active;
  61. _type = type;
  62. _modified = modified;
  63. _data = NULL;
  64. }
  65. Layer(const Layer & c)
  66. {
  67. _label = c.getLabel();
  68. _color = c.getColor();
  69. _active = c.getActive();
  70. _type = c.getType();
  71. _modified = c.isModified();
  72. _data = c.getData();
  73. }
  74. /*!
  75. *
  76. * \brief Constructeur
  77. *
  78. * Constructeur avec paramètres de la classe Layer, remplit les attributs de la classe avec les données de 'ligne'.
  79. *
  80. * \param ligne : chaîne de caractères contenant des informations pour la classe
  81. * \return void
  82. */
  83. Layer(const std::string & line)
  84. {
  85. _data = NULL ;
  86. using boost::lexical_cast ;
  87. using boost::bad_lexical_cast ;
  88. boost::char_separator<char> sep("\"");
  89. boost::tokenizer <boost::char_separator<char> >tokens(line,sep);
  90. boost::tokenizer<boost::char_separator<char> >::iterator it = tokens.begin();
  91. _label = *it;
  92. ++it;
  93. try
  94. {
  95. _color = lexical_cast<int>(*it);
  96. _active = lexical_cast<int>(*it);
  97. _type = lexical_cast<int>(*it);
  98. }
  99. catch(const bad_lexical_cast & e)
  100. {
  101. std::cerr<<e.what();
  102. }
  103. _modified = false ;
  104. }
  105. bool operator==(const Layer& layer) const
  106. { return (_type == layer.getType() and (_type == CONTOUR_BASSIN or _type == RESEAU_HYDRO or _type == EXUTOIRES));}
  107. void toTextFile(std::ofstream & file) const
  108. {
  109. file<<"\""<<_label<<"\""<<" "<<_color<<" "<<_active<<" "<<_type<<"\n";
  110. }
  111. ~Layer()
  112. {
  113. if(_data !=NULL)
  114. delete [] _data;
  115. }
  116. int getActive() const
  117. {
  118. return _active;
  119. }
  120. int getColor() const
  121. {
  122. return _color;
  123. }
  124. const std::string& getLabel() const
  125. {
  126. return _label;
  127. }
  128. bool isModified() const
  129. {
  130. return _modified;
  131. }
  132. int getType() const
  133. {
  134. return _type;
  135. }
  136. void setLabel(const std::string& label) {
  137. _label = label;
  138. }
  139. void setColor(int couleur) {
  140. #ifdef IHM
  141. _color = couleur;
  142. #endif
  143. }
  144. void setActive(int active)
  145. {
  146. _active = active;
  147. }
  148. void setType(int type)
  149. {
  150. _type = type;
  151. }
  152. static void fill(std::vector<Layer> & layers)
  153. {
  154. layers.push_back(Layer(std::string("Reseau Hydrographique"), 0,0,RESEAU_HYDRO,false));
  155. layers.push_back(Layer(std::string("Contour des Bassins Versants"), 0,0,CONTOUR_BASSIN,false));
  156. layers.push_back(Layer(std::string(""), 0,0,CONTOUR_IMAGE_BINAIRE,false));
  157. layers.push_back(Layer(std::string("Exutoires du Reseau Hydro"), 0,0,EXUTOIRES,false));
  158. }
  159. static int find(std::vector<Layer> & layers, int type)
  160. {
  161. for(unsigned short int i = 0; i < layers.size(); i++)
  162. {
  163. if(layers.at(i).getType() == type && (type == CONTOUR_BASSIN || type == RESEAU_HYDRO || type == EXUTOIRES))
  164. return i;
  165. }
  166. return -1 ;
  167. }
  168. const std::string toString() const
  169. {
  170. std::stringstream ss;
  171. ss <<"label "<<_label <<" couleur "<<_color<<" active "<<_active<<" type"<<_type;
  172. return ss.str();
  173. }
  174. void display(Parameters * p) const
  175. {
  176. std::cout<<toString()<<std::endl;
  177. std::cout<<"Données Couches "<<std::endl;
  178. for(int i = 0; i< p->_linesNumber; i++)
  179. {
  180. for(int j = 0; j< p->_columnsNumber; j++)
  181. {
  182. std::cout<<isNetwork(Coordinates(p->_linesNumber - 1 - i , j),p )<<std::endl;
  183. }
  184. std::cout<<"\n";
  185. }
  186. }
  187. unsigned char* getData() const
  188. {
  189. return _data;
  190. }
  191. void setData(unsigned char* data)
  192. {
  193. _data = data;
  194. }
  195. /*!
  196. * \brief Retourner le bit de coordonnées i, j d'une matrice
  197. *
  198. * \param m : matrice des couches.
  199. * \param c : Coordonnées 2D afin de se situer dans la matrice.
  200. * \param p : Paramètres du bassin versant.
  201. * \return la valeur du bit correspondant au pixel i, j dans la matrice m.
  202. */
  203. bool getmatbit (const Coordinates & c, const Parameters * p) const
  204. {
  205. return (((*( _data + (c.getLine() *p->_charColumnsNumber+(c.getColumn()>>3))))>>(c.getColumn()&7)) & 1);
  206. }
  207. /*!
  208. * \brief Affecte la valeur 1 au bit correspondant au pixel i, j dans la matrice m.
  209. *
  210. * \param m : Matrice des couches.
  211. * \param c : Coordonnées 2D afin de se situer dans la matrice.
  212. * \param p : Paramètres du bassin versant.
  213. * \return void.
  214. */
  215. void matbiton (const Coordinates & c, const Parameters * p)
  216. {
  217. *(_data + (c.getLine() * p->_charColumnsNumber +(c.getColumn()>>3))) |= 1<<(c.getColumn()&7);
  218. }
  219. /*!
  220. * \brief Affecte la valeur 0 au bit correspondant au pixel i, j dans la matrice m.
  221. *
  222. * \param m : Matrice des couches.
  223. * \param c : Coordonnées 2D afin de se situer dans la matrice.
  224. * \param p : Paramètres du bassin versant.
  225. * \return void.
  226. */
  227. void matbitoff (Coordinates &c, const Parameters * p)
  228. {
  229. *(_data + (c.getLine() * p->_charColumnsNumber + (c.getColumn()>>3))) &= ~(1<<(c.getColumn()&7)) ;
  230. }
  231. bool isNetwork(const Coordinates & c, const Parameters * p) const
  232. {
  233. return getmatbit(c, p);
  234. }
  235. };
  236. #endif /* LAYER_H_ */