Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYCourbeNiveau.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"
20 #include "TYCourbeNiveau.h"
21 
22 #if TY_USE_IHM
25 #endif
26 
29 
30 /*static*/ double TYCourbeNiveau::_defaultDistMax = setDefaultDistMax();
31 
32 TYCourbeNiveau::TYCourbeNiveau() : _isDMaxDefault(false), _altitude(0.0), _closed(false)
33 {
35 
37 
38  updateColor();
39 }
40 
42 {
43  *this = other;
44 }
45 
47 {
48  setListPoints(pts);
49  setAltitude(alt);
51  _isDMaxDefault = false;
52  _closed = false;
53 
54  updateColor();
55 }
56 
58 
60 {
61  if (this != &other)
62  {
63  TYElement::operator=(other);
65  _altitude = other._altitude;
66  _closed = other._closed;
67  _listPoints = other._listPoints;
68  }
69  return *this;
70 }
71 
73 {
74  if (this != &other)
75  {
76  if (TYElement::operator!=(other))
77  {
78  return false;
79  }
80  if (TYColorInterface::operator!=(other))
81  {
82  return false;
83  }
84  if (_altitude != other._altitude)
85  {
86  return false;
87  }
88  if (_closed != other._closed)
89  {
90  return false;
91  }
92  if (!(_listPoints == other._listPoints))
93  {
94  return false;
95  }
96  }
97  return true;
98 }
99 
101 {
102  return !operator==(other);
103 }
104 
105 bool TYCourbeNiveau::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
106 {
107  if (!TYElement::deepCopy(pOther, copyId, true))
108  {
109  return false;
110  }
111 
112  TYCourbeNiveau* pOtherCrbNiv = (TYCourbeNiveau*)pOther;
113 
114  TYColorInterface::deepCopy((TYColorInterface*)pOtherCrbNiv, copyId);
115 
116  _altitude = pOtherCrbNiv->_altitude;
117 
118  _closed = pOtherCrbNiv->_closed;
119 
120  _listPoints.clear();
121  for (unsigned int i = 0; i < pOtherCrbNiv->_listPoints.size(); i++)
122  {
123  TYPoint pt;
124  pt.deepCopy(&pOtherCrbNiv->_listPoints[i], copyId);
125  _listPoints.push_back(pt);
126  }
127 
128  return true;
129 }
130 
131 std::string TYCourbeNiveau::toString() const
132 {
133  return "TYCourbeNiveau";
134 }
135 
137 {
138  DOM_Element domNewElem = TYElement::toXML(domElement);
139 
140  TYColorInterface::toXML(domNewElem);
141 
142  TYXMLTools::addElementDoubleValue(domNewElem, "altitude", _altitude);
143  TYXMLTools::addElementBoolValue(domNewElem, "closed", _closed);
144 
145  if (_isDMaxDefault == true) // Si on utilise une valeur specifique, on l'enregistre
146  {
147  TYXMLTools::addElementDoubleValue(domNewElem, "distmaxpts", _distMax);
148  }
149 
150  TYXMLTools::addElementUIntValue(domNewElem, "nbPoints", _listPoints.size());
151 
152  for (unsigned int i = 0; i < _listPoints.size(); i++)
153  {
154  _listPoints.at(i).toXML(domNewElem);
155  }
156 
157  return domNewElem;
158 }
159 
161 {
162  TYElement::fromXML(domElement);
163 
164  TYColorInterface::fromXML(domElement);
165 
166  _closed = false;
167  _listPoints.clear();
168 
169  bool altitudeOk = false;
170  bool closedOk = false;
171  bool nbPointsOk = false;
172  bool distMaxOk = false;
173  int nbPoints = 0;
174  TYPoint pt;
175  DOM_Element elemCur;
176 
177  TYUUID currentId = this->getID();
178  if (TYElement::testId(currentId, this))
179  {
180  this->regenerateID();
181  }
182 
183  QDomNodeList childs = domElement.childNodes();
184  for (unsigned int i = 0; i < childs.length(); i++)
185  {
186  elemCur = childs.item(i).toElement();
187  TYXMLTools::getElementDoubleValue(elemCur, "altitude", _altitude, altitudeOk);
188  TYXMLTools::getElementBoolValue(elemCur, "closed", _closed, closedOk);
189  TYXMLTools::getElementDoubleValue(elemCur, "distmaxpts", _distMax, distMaxOk);
190  TYXMLTools::getElementIntValue(elemCur, "nbPoints", nbPoints, nbPointsOk);
191 
192  if (pt.callFromXMLIfEqual(elemCur))
193  {
194  _listPoints.push_back(pt);
195  }
196  }
197 
198  if (distMaxOk)
199  {
200  _isDMaxDefault = true;
201  } // Si une distance entre points est trouvee, on met le flag
202 
204 
205  return 1;
206 }
207 
209 {
211 
212  if (_pParent)
213  {
214  _pParent->setIsGeometryModified(isModified);
215  }
216 }
217 
219 {
220  _listPoints = pts;
221 
222  setIsGeometryModified(true);
223 
224  // Tant qu'il y a un parent
225  TYElement* pParent = getParent();
226  while (pParent)
227  {
228  // Tentative de cast du parent en Topo
229  TYTopographie* pTopo = TYTopographie::safeDownCast(pParent);
230 
231  // Si le parent est effectivement une Topo
232  if (pTopo)
233  {
234  // L'alti n'est plus a jour non plus...
235  LPTYSiteNode pSite = dynamic_cast<TYSiteNode*>(pTopo->getParent());
236  assert(pSite != nullptr && "The parent of a TYTopographie element must be a TYSiteNode");
237  pSite->getAltimetry()->setIsGeometryModified(true);
238  break;
239  }
240 
241  // Parent du parent...
242  pParent = pParent->getParent();
243  }
244 }
245 
246 void TYCourbeNiveau::close(bool closed)
247 {
248  if (_listPoints.size() < 3)
249  {
250  _closed = false;
251  return;
252  }
253 
254  // Fermeture de la courbe en ajoutant le premier point a la fin de la liste
255  if (closed)
256  {
257  _listPoints.push_back(_listPoints[0]);
258  _closed = true;
259  }
260  else
261  {
262  _closed = false;
263  }
264 }
265 
267 {
268  _altitude = alt;
269 
271 }
272 
274 {
275  for (unsigned int i = 0; i < _listPoints.size(); i++)
276  {
277  _listPoints[i]._z = _altitude;
278  }
279 
280  setIsGeometryModified(true);
281 }
282 
284 {
285  double valeur = 200.0;
286 #if TY_USE_IHM
287  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "DistMinPtCrbNiv"))
288  {
289  valeur = TYPreferenceManager::getDouble(TYDIRPREFERENCEMANAGER, "DistMinPtCrbNiv");
290  }
291  else
292  {
293  TYPreferenceManager::setDouble(TYDIRPREFERENCEMANAGER, "DistMinPtCrbNiv", valeur);
294  }
295 #endif
296 
297  return valeur; // Valeur par defaut si parametre non defini
298 }
299 
301 {
302  updateDistMax(); // Il faut mettre a jour car le projet peut avoir change la valeurs par defaut
303  return _distMax;
304 }
305 
307 {
308  // Mise a jour de la valeur par defaut (si necessaire)
310 }
311 
312 void TYCourbeNiveau::setIsDMaxDefault(const bool& etat)
313 {
314  _isDMaxDefault = etat;
315 
316  if (!_isDMaxDefault)
317  {
319  }
320 }
321 
322 /*virtual*/ void TYCourbeNiveau::updateColor()
323 {
324  // Couleur par default
325  float r = 255.0f, g = 0.0f, b = 0.0f;
326 
327 #if TY_USE_IHM
328  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "TYCourbeNiveauGraphicColorR"))
329  {
330  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "TYCourbeNiveauGraphicColor", r, g, b);
331  }
332  else
333  {
334  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "TYCourbeNiveauGraphicColor", r, g, b);
335  }
336 #endif
337 
338  OColor color;
339  color.r = r / 255;
340  color.g = g / 255;
341  color.b = b / 255;
342 
343  setColor(color);
344 }
345 
346 TYTabPoint::iterator TYCourbeNiveau::getPointRef(const TYPoint& pt)
347 {
348  double distance = 1.E9, dist_tmp = 0.0;
349  TYTabPoint::iterator iterRet = _listPoints.begin();
350  for (TYTabPoint::iterator iter = _listPoints.begin(); iter != _listPoints.end(); iter++)
351  {
352  dist_tmp = (*iter).distFrom(pt);
353  if (dist_tmp < distance)
354  {
355  distance = dist_tmp;
356  iterRet = iter;
357  }
358  }
359 
360  return iterRet;
361 }
362 
364 {
365  LPTYCourbeNiveau pCurve = nullptr;
366 
367  // find an iterator to the closest point
368  TYTabPoint::iterator iterP = getPointRef(pt);
369 
370  // Cas d'une courbe fermee
371  if (_closed)
372  {
373  // The the curve is restructured putting pt at begin and opening the curve
374  restructure(iterP);
375  _closed = false;
376  return pCurve; // nulle à ce stade
377  }
378 
379  // Cas d'une courbe ouverte
380  // pt est le dernier point de la liste
381  // --> on supprime le dernier point
382  // --> la nouvelle courbe n'est pas crée
383  TYTabPoint::iterator iterLast = _listPoints.end();
384  iterLast--;
385  if (iterP == iterLast)
386  {
387  _listPoints.erase(iterLast);
388  return pCurve; // nulle à ce stade
389  }
390 
391  pCurve = new TYCourbeNiveau();
392  pCurve->setParent(_pParent);
393 
394  // On recopie tous les points à partir du point suivant le point donne
395  TYTabPoint::iterator iter = iterP;
396  while (iter != _listPoints.end())
397  {
398  pCurve->addPoint((*iter));
399  iter = _listPoints.erase(iter);
400  }
401 
402  // 2 cas
403  // 1. le point pt etait le 1er ou le 2eme
404  // --> la courbe resultat = la courbe initiale (pt = 1er point)
405  // --> le resultat est la courbe initiale moins le 1er (pt = 2eme point)
406  // --> la nouvelle courbe remplace la courbe initiale
407  // 2. le point pt etait le dernier
408  // --> la courbe resultat est vide
409  if (_listPoints.size() < 2)
410  {
411  setListPoints(pCurve->getListPoints());
412  pCurve._pObj = nullptr;
413  }
414  else if (pCurve->getListPoints().size() == 0)
415  {
416  pCurve._pObj = nullptr;
417  }
418 
419  return pCurve;
420 }
421 
422 void TYCourbeNiveau::restructure(TYTabPoint::iterator itPt)
423 {
424  if (!_closed)
425  {
426  return;
427  }
428 
429  TYTabPoint tab;
430 
431  // On part du point jusqu'à la fin de la liste de points
432  for (TYTabPoint::iterator it = itPt; it != _listPoints.end(); it++)
433  {
434  tab.push_back((*it));
435  }
436 
437  // On ajoute ensuite les points à partir du 2nd (le 1er etant identique au dernier !)
438  // jusqu'au point (non inclu)
439  for (TYTabPoint::iterator it = ++_listPoints.begin(); it != itPt; it++)
440  {
441  tab.push_back((*it));
442  }
443 
444  _listPoints = tab;
445 }
446 
448 {
449  for (unsigned int i = 0; i < _listPoints.size(); i++)
450  {
451  _listPoints[i]._x += 10;
452  _listPoints[i]._y -= 10;
453  }
454 }
QDomElement DOM_Element
Definition: QT2DOM.h:30
Representation graphique d'une courbe de niveau (fichier header)
Outil IHM pour une courbe de niveau (fichier header)
TY_EXTENSION_INST(TYCourbeNiveau)
TY_EXT_GRAPHIC_INST(TYCourbeNiveau)
class OGenID TYUUID
Definition: TYDefines.h:59
std::vector< TYPoint > TYTabPoint
Collection de TYPoint.
Definition: TYDefines.h:340
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:52
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
static OPrototype * safeDownCast(OPrototype *pObject)
Definition: TYElement.cpp:71
T * _pObj
The real pointer, must derived IRefCount.
Definition: smartptr.h:307
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)
bool operator!=(const TYCourbeNiveau &other) const
Operateur !=.
LPTYCourbeNiveau split(const TYPoint &pt)
return a new level curve by spliting the actual one at indicated point
virtual ~TYCourbeNiveau()
virtual int fromXML(DOM_Element domElement)
virtual DOM_Element toXML(DOM_Element &domElement)
TYCourbeNiveau & operator=(const TYCourbeNiveau &other)
Operateur =.
double _altitude
Altitude.
void setListPoints(const TYTabPoint &pts)
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
TYTabPoint _listPoints
Liste de points.
void restructure(TYTabPoint::iterator itPt)
virtual void updateColor()
void addPoint(const TYPoint &pt)
virtual std::string toString() const
static double _defaultDistMax
TYTabPoint::iterator getPointRef(const TYPoint &pt)
return an iterator to the curve point closest to the point passed
void close(bool closed)
virtual void setIsGeometryModified(bool isModified)
TYTabPoint & getListPoints()
bool _closed
close status
bool operator==(const TYCourbeNiveau &other) const
Operateur ==.
static double setDefaultDistMax()
void setAltitude(double alt)
void applyAlitudeToPoints()
void setIsDMaxDefault(const bool &etat)
TYElement * getParent() const
Definition: TYElement.h:699
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
TYElement * _pParent
Reference sur l'element parent.
Definition: TYElement.h:969
void setParent(TYElement *pParent)
Definition: TYElement.h:692
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
virtual void setIsGeometryModified(bool isModified)
Definition: TYElement.cpp:253
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
LPTYAltimetrie getAltimetry() const
static void addElementDoubleValue(DOM_Element &parentElem, DOMString nodeName, double nodeValue)
Definition: TYXMLTools.cpp:87
static bool getElementBoolValue(DOM_Element parentElem, DOMString nodeName, bool &nodeValue)
Definition: TYXMLTools.cpp:179
static bool getElementIntValue(DOM_Element parentElem, DOMString nodeName, int &nodeValue)
Definition: TYXMLTools.cpp:129
static bool getElementDoubleValue(DOM_Element parentElem, DOMString nodeName, double &nodeValue)
Definition: TYXMLTools.cpp:243
static void addElementUIntValue(DOM_Element &parentElem, DOMString nodeName, unsigned int nodeValue)
Definition: TYXMLTools.cpp:42
static void addElementBoolValue(DOM_Element &parentElem, DOMString nodeName, bool nodeValue)
Definition: TYXMLTools.cpp:77