Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYTerrain.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #include "Tympan/core/logging.h"
18 #include "TYTerrain.h"
19 
20 #if TY_USE_IHM
24 #endif
25 
28 
29 TYTerrain::TYTerrain() : _pVegetation(nullptr), _vegetActive(false)
30 {
32 
33  _pSol = new TYSol();
34  _pSol->setParent(this);
35 
36  float r = 0.0f, g = 85.0f, b = 0.0f;
37 
38 #if TY_USE_IHM
39  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "TYTerrainGraphicColorR"))
40  {
41  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "TYTerrainGraphicColor", r, g, b);
42  }
43  else
44  {
45  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "TYTerrainGraphicColor", r, g, b);
46  }
47 #endif // TY_USE_IHM
48 
49  OColor color;
50  color.r = r / 255;
51  color.g = g / 255;
52  color.b = b / 255;
53 
54  setColor(color);
55 }
56 
58 {
59  *this = other;
60 }
61 
63 
65 {
66  if (this != &other)
67  {
68  TYElement::operator=(other);
70  _pSol = other._pSol;
71  _vegetActive = other._vegetActive;
72  _pVegetation = other._pVegetation;
73  _listPoints = other._listPoints;
74  }
75  return *this;
76 }
77 
78 bool TYTerrain::operator==(const TYTerrain& other) const
79 {
80  if (this != &other)
81  {
82  if (TYElement::operator!=(other))
83  {
84  return false;
85  }
86  if (TYColorInterface::operator!=(other))
87  {
88  return false;
89  }
90  if (_pSol != other._pSol)
91  {
92  return false;
93  }
94  if (_pVegetation != other._pVegetation)
95  {
96  return false;
97  }
98  if (_vegetActive != other._vegetActive)
99  {
100  return false;
101  }
102  if (!(_listPoints == other._listPoints))
103  {
104  return false;
105  }
106  }
107  return true;
108 }
109 
110 bool TYTerrain::operator!=(const TYTerrain& other) const
111 {
112  return !operator==(other);
113 }
114 
115 bool TYTerrain::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
116 {
117  if (!TYElement::deepCopy(pOther, copyId, pUseCopyTag))
118  {
119  return false;
120  }
121 
122  TYTerrain* pOtherTerrain = (TYTerrain*)pOther;
123 
124  TYColorInterface::deepCopy((TYColorInterface*)pOtherTerrain, copyId);
125 
126  _pSol->deepCopy(pOtherTerrain->_pSol, copyId);
127 
128  _vegetActive = pOtherTerrain->_vegetActive;
129 
130  if (_pVegetation)
131  {
132  _pVegetation->deepCopy(pOtherTerrain->_pVegetation, copyId);
133  }
134 
135  _listPoints.clear();
136  for (unsigned int i = 0; i < pOtherTerrain->_listPoints.size(); i++)
137  {
138  TYPoint pt;
139  pt.deepCopy(&pOtherTerrain->_listPoints[i], copyId);
140  _listPoints.push_back(pt);
141  }
142 
143  return true;
144 }
145 
146 std::string TYTerrain::toString() const
147 {
148  return "TYTerrain";
149 }
150 
152 {
153  DOM_Element domNewElem = TYElement::toXML(domElement);
154 
155  TYColorInterface::toXML(domNewElem);
156 
157  _pSol->toXML(domNewElem);
158 
159  TYXMLTools::addElementIntValue(domNewElem, "active", _vegetActive);
160 
161  if (_vegetActive && _pVegetation)
162  {
163  _pVegetation->toXML(domNewElem);
164  }
165 
166  TYXMLTools::addElementUIntValue(domNewElem, "nbPoints", _listPoints.size());
167 
168  for (unsigned int i = 0; i < _listPoints.size(); i++)
169  {
170  _listPoints.at(i).toXML(domNewElem);
171  }
172 
173  return domNewElem;
174 }
175 
177 {
178  TYElement::fromXML(domElement);
179 
180  TYColorInterface::fromXML(domElement);
181 
182  _listPoints.clear();
183  _vegetActive = false;
184  if (_pVegetation)
185  {
186  _pVegetation = nullptr;
187  }
188 
189  // A new vegetation is created in case of vegetation reading
190  // will be destroyed if not used
191  _pVegetation = new TYVegetation();
192 
193  bool nbPointsOk = false;
194  bool bVegetDone = false;
195  bool activeOk = false;
196 
197  int nbPoints = 0;
198  TYPoint pt;
199  DOM_Element elemCur;
200 
201  TYUUID currentId = this->getID();
202  if (TYElement::testId(currentId, this))
203  {
204  this->regenerateID();
205  }
206 
207  QDomNodeList childs = domElement.childNodes();
208  for (unsigned int i = 0; i < childs.length(); i++)
209  {
210  elemCur = childs.item(i).toElement();
211 
212  _pSol->callFromXMLIfEqual(elemCur);
213 
214  TYXMLTools::getElementBoolValue(elemCur, "active", _vegetActive, activeOk);
215 
216  if (_vegetActive && !bVegetDone)
217  {
218  bVegetDone = _pVegetation->callFromXMLIfEqual(elemCur);
219  }
220 
221  TYXMLTools::getElementIntValue(elemCur, "nbPoints", nbPoints, nbPointsOk);
222 
223  if (pt.callFromXMLIfEqual(elemCur))
224  {
225  _listPoints.push_back(pt);
226  }
227  }
228 
229  if (!_vegetActive && _pVegetation)
230  {
231  _pVegetation = nullptr;
232  }
233 
234  return 1;
235 }
236 
238 {
239  TYPolygon polygon(_listPoints);
240  return polygon.surface();
241 }
242 
243 void TYTerrain::useVegetation(bool state /*= true*/)
244 {
245  _vegetActive = state;
246 
247  if (_vegetActive)
248  {
249  if (_pVegetation == nullptr)
250  {
251  _pVegetation = new TYVegetation();
252  _pVegetation->setParent(this);
253  }
254  }
255  else
256  {
257  if (_pVegetation)
258  {
259  _pVegetation = nullptr;
260  }
261  }
262 }
263 
265 {
266  _pVegetation = pVeget;
267 
268  if (_pVegetation != nullptr)
269  {
270  _vegetActive = true;
271  _pVegetation->setParent(this);
272  }
273  else
274  {
275  _vegetActive = false;
276  }
277 }
278 
280 {
281  for (unsigned int i = 0; i < _listPoints.size(); i++)
282  {
283  _listPoints[i]._x += 10;
284  _listPoints[i]._y -= 10;
285  }
286 }
QDomElement DOM_Element
Definition: QT2DOM.h:30
class OGenID TYUUID
Definition: TYDefines.h:59
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:52
Representation graphique d'un terrain (fichier header)
outil IHM pour un terrain (fichier header)
TY_EXTENSION_INST(TYTerrain)
TY_EXT_GRAPHIC_INST(TYTerrain)
Definition: color.h:31
float b
Definition: color.h:33
float r
Definition: color.h:33
float g
Definition: color.h:33
virtual const char * getClassName() const
Definition: TYElement.h:249
int fromXML(DOM_Element domElement)
TYColorInterface & operator=(const TYColorInterface &other)
virtual void setColor(const OColor &color)
void deepCopy(const TYColorInterface *pOther, bool copyId=true, bool pUseCopyTag=false)
DOM_Element toXML(DOM_Element &domElement)
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYElement.cpp:307
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYElement.cpp:368
QString _name
Nom courant de l'element.
Definition: TYElement.h:966
TYElement & operator=(const TYElement &other)
Definition: TYElement.cpp:265
bool callFromXMLIfEqual(DOM_Element &domElement, int *pRetVal=NULL)
Definition: TYElement.cpp:544
static bool testId(const TYUUID &id, const TYElement *pElem)
Definition: TYElement.cpp:216
void regenerateID()
Definition: TYElement.cpp:243
const TYUUID & getID() const
Definition: TYElement.cpp:176
void setParent(TYElement *pParent)
Definition: TYElement.h:692
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYPoint.cpp:92
virtual double surface() const
Definition: TYPolygon.cpp:186
Definition: TYSol.h:25
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYSol.cpp:127
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYSol.cpp:150
bool operator!=(const TYTerrain &other) const
Operateur !=.
Definition: TYTerrain.cpp:110
void useVegetation(bool state=true)
Definition: TYTerrain.cpp:243
double surface()
Definition: TYTerrain.cpp:237
bool operator==(const TYTerrain &other) const
Operateur ==.
Definition: TYTerrain.cpp:78
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYTerrain.cpp:115
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYTerrain.cpp:151
LPTYSol _pSol
Materiau.
Definition: TYTerrain.h:164
LPTYVegetation _pVegetation
Vegetation eventuelle.
Definition: TYTerrain.h:167
TYTabPoint _listPoints
Liste de points.
Definition: TYTerrain.h:173
bool _vegetActive
Etat d'utilisation de la vegetation.
Definition: TYTerrain.h:170
virtual int fromXML(DOM_Element domElement)
Definition: TYTerrain.cpp:176
TYTerrain & operator=(const TYTerrain &other)
Operateur =.
Definition: TYTerrain.cpp:64
void offsetListPoints()
Definition: TYTerrain.cpp:279
virtual std::string toString() const
Definition: TYTerrain.cpp:146
virtual ~TYTerrain()
Definition: TYTerrain.cpp:62
void setVegetation(LPTYVegetation pVeget)
Definition: TYTerrain.cpp:264
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
virtual DOM_Element toXML(DOM_Element &domElement)
static bool getElementBoolValue(DOM_Element parentElem, DOMString nodeName, bool &nodeValue)
Definition: TYXMLTools.cpp:179
static void addElementIntValue(DOM_Element &parentElem, DOMString nodeName, int nodeValue)
Definition: TYXMLTools.cpp:72
static bool getElementIntValue(DOM_Element parentElem, DOMString nodeName, int &nodeValue)
Definition: TYXMLTools.cpp:129
static void addElementUIntValue(DOM_Element &parentElem, DOMString nodeName, unsigned int nodeValue)
Definition: TYXMLTools.cpp:42