Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYFaceSet.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"
17 #include "Tympan/core/chrono.h"
20 #if TY_USE_IHM
23 #endif
24 #include "TYFaceSet.h"
25 
28 
30 {
32 }
33 
35 {
36  *this = other;
37 }
38 
40 {
41  // purge();
42 }
43 
45 {
46  if (this != &other)
47  {
48  TYElement::operator=(other);
49  _volEnglob = other._volEnglob;
50  _rayonSphere = other._rayonSphere;
52 
53  _faces = other._faces;
54  _sommets = other._sommets;
55  _normals = other._normals;
56  }
57  return *this;
58 }
59 
60 bool TYFaceSet::operator==(const TYFaceSet& other) const
61 {
62  if (this != &other)
63  {
64  if (TYElement::operator!=(other))
65  {
66  return false;
67  }
68  if (_faces != other._faces)
69  {
70  return false;
71  }
72  if (_sommets != other._sommets)
73  {
74  return false;
75  }
76  if (_normals != other._normals)
77  {
78  return false;
79  }
80  }
81  return true;
82 }
83 
84 bool TYFaceSet::operator!=(const TYFaceSet& other) const
85 {
86  return !operator==(other);
87 }
88 
89 bool TYFaceSet::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
90 {
91  if (!TYElement::deepCopy(pOther, copyId))
92  {
93  return false;
94  }
95 
96  TYFaceSet* pOtherFaceSet = (TYFaceSet*)pOther;
97 
98  purge();
99 
100  TYTabLPPolygon otherFaces = pOtherFaceSet->faces();
101  TYPolygon* pFace = NULL;
102  for (int i = 0; i < pOtherFaceSet->getNbFaces(); i++)
103  {
104  pFace = new TYPolygon();
105  pFace->deepCopy(otherFaces[i], copyId);
106  _faces.push_back(pFace);
107  }
108 
109  _sommets = pOtherFaceSet->_sommets;
110  _normals = pOtherFaceSet->_normals;
111 
112  _volEnglob = pOtherFaceSet->_volEnglob;
113  _rayonSphere = pOtherFaceSet->_rayonSphere;
114  _centreGravite = pOtherFaceSet->_centreGravite;
115 
116  return true;
117 }
118 
119 std::string TYFaceSet::toString() const
120 {
121  std::string str = "TYFaceSet";
122  for (unsigned int i = 0; i < _faces.size(); i++)
123  {
124  str += "\n\tFace" + intToStr(i) + "=" + _faces[i]->toString();
125  }
126 
127  return str;
128 }
129 
131 {
132  DOM_Element domNewElem = TYElement::toXML(domElement);
133 
134  TYPolygon* pFace = NULL;
135  for (unsigned int i = 0; i < _faces.size(); i++)
136  {
137  pFace = _faces[i];
138  if (pFace != NULL)
139  {
140  pFace->toXML(domNewElem);
141  }
142  }
143  return domNewElem;
144 }
145 
147 {
148  TYElement::fromXML(domElement);
149 
150  purge();
151 
152  TYPoint pt;
153  DOM_Element elemCur;
154 
155  QDomNodeList childs = domElement.childNodes();
156 
157  for (unsigned int i = 0; i < childs.length(); i++)
158  {
159  elemCur = childs.item(i).toElement();
160 
161  if (pt.callFromXMLIfEqual(elemCur))
162  {
163  // _pts.push_back(pt);
164  }
165  }
166 
167  UpdateInternals();
168 
169  return 1;
170 }
171 
172 double TYFaceSet::volume() const
173 {
174  double res = 0.0;
175 
176  TYBox bbox = getVolEnglob();
177  // Calcul du volume du bounding rect
178  res = bbox._sizeX * bbox._sizeY * bbox._sizeZ;
179 
180  return res;
181 }
182 
183 double TYFaceSet::surface() const
184 {
185  double res = 0.0;
186 
187  TYBox bbox = getVolEnglob();
188  // Calcul de la surface du bounding rect
189  res = 2 * (bbox._sizeX * bbox._sizeY + bbox._sizeX * bbox._sizeZ + bbox._sizeY * bbox._sizeZ);
190 
191  return res;
192 }
193 
195 {
196  double res = 0.0;
197  return res;
198 }
199 
201 {
202  double res = 0.0;
203  return res;
204 }
205 
207 {
208  return _normals;
209 }
210 
212 {
213  return _sommets;
214 }
215 
217 {
218  return _faces;
219 }
220 
222 {
223  TYPoint centre;
224  size_t nbPts = _sommets.size();
225  float x = 0.0f, y = 0.0f, z = 0.0f;
226 
227  if (nbPts > 0)
228  {
229  x = 0.0f;
230  y = 0.0f;
231  z = 0.0f;
232 
233  TYPoint pt;
234  for (size_t i = 0; i < nbPts; i++)
235  {
236  pt = _sommets[i];
237  x += pt._x;
238  y += pt._y;
239  z += pt._z;
240  }
241 
242  x /= (float)nbPts;
243  y /= (float)nbPts;
244  z /= (float)nbPts;
245  }
246 
247  centre._x = x;
248  centre._y = y;
249  centre._z = z;
250 
251  return centre;
252 }
253 
254 int TYFaceSet::intersects(const OSegment3D& seg, TYTabPoint& ptList) const
255 {
256  return TYVolumeInterface::intersects(seg, ptList);
257 }
258 
259 int TYFaceSet::isInside(const TYPoint& pt) const
260 {
261  int inside = INTERS_NULLE;
262  // find a second point outside the volume using the bounding box of the object.
263  TYBox bbox = getVolEnglob();
264  TYPoint scdPt = bbox.getPosition();
265  scdPt._x += bbox._sizeX * 2.0f;
266 
267  OSegment3D seg(pt, scdPt);
268 
269  // Pour eviter des calculs inutiles...
270  TYTabPoint pointTest;
271  if (volEnglob().intersects(seg, pointTest) == INTERS_NULLE)
272  {
273  return inside;
274  }
275 
276  // Recherche de l'intersection du segment avec chacune des faces
277  // composant ce volume
278  TYTabLPPolygon tabFaces = faces();
279  TYPoint tmpPt;
280  int nbIntersects = 0;
281 
282  for (unsigned int i = 0; i < tabFaces.size(); i++)
283  {
284  LPTYPolygon pFace = tabFaces[i];
285  assert(pFace);
286 
287  if (pFace->intersects(seg, tmpPt) == INTERS_OUI)
288  {
289  // Increment du nb d'intersections trouvees
290  nbIntersects++;
291  }
292  }
293 
294  // Si le nb d'intersection est impaire, le point est en dedans, sinon dehors.
295  if ((nbIntersects % 2) != 0)
296  {
297  inside = INTERS_OUI;
298  }
299  else
300  {
301  inside = INTERS_NULLE;
302  }
303 
304  return inside;
305 }
306 
308 {
309  // Free all the polygon pointers in the list first
310  TYPolygon* pFace = NULL;
311  for (unsigned int i = 0; i < _faces.size(); i++)
312  {
313  pFace = _faces[i];
314  delete pFace;
315  }
316  // then clear list content...
317  _faces.clear();
318 
319  UpdateInternals();
320 
321  setIsGeometryModified(true);
322 }
323 
324 void TYFaceSet::transform(const OMatrix& matrix)
325 {
326  TYPolygon* pFace = NULL;
327  for (unsigned int i = 0; i < _faces.size(); i++)
328  {
329  pFace = _faces[i];
330  pFace->transform(matrix);
331  }
332 
333  UpdateInternals();
334 }
335 
337 {
338  _normals.clear();
339  _sommets.clear();
340  unsigned int i = 0;
341 
342  if (_faces.size() > 0)
343  {
344  // update normals list
345  TYPolygon* pFace = NULL;
346  for (i = 0; i < _faces.size(); i++)
347  {
348  pFace = _faces[i];
349  _normals.push_back(pFace->normal());
350  }
351 
352  // update vertice list
353  pFace = NULL;
354  OPoint3D pt;
355  bool notFound = false;
356  for (i = 0; i < _faces.size(); i++)
357  {
358  pFace = _faces[i];
359 
360  for (int j = 0; j < 4; j++) // Four vertice per face
361  {
362  pt = pFace->getPoint(j);
363  notFound = true;
364  for (unsigned int k = 0; k < _sommets.size(); k++)
365  {
366  if (pt == _sommets[k])
367  {
368  notFound = false;
369  }
370  }
371  if (notFound)
372  {
373  _sommets.push_back(pt);
374  }
375  }
376  }
377  }
378 
379  // update other internal data
381  _volEnglob = volEnglob();
383 }
384 
386 {
387  for (unsigned int i = 0; i < _faces.size(); i++)
388  {
389  _faces[i]->inverseNormale();
390  }
391 }
All base classes related to 3D manipulation.
#define INTERS_OUI
The intersection exists.
Definition: 3d.h:33
#define INTERS_NULLE
No intersection.
Definition: 3d.h:35
QDomElement DOM_Element
Definition: QT2DOM.h:30
std::vector< TYPoint > TYTabPoint
Collection de TYPoint.
Definition: TYDefines.h:340
std::vector< OVector3D > TYTabVector
Collection de OVector3D.
Definition: TYDefines.h:398
std::vector< LPTYPolygon > TYTabLPPolygon
Collection de pointeurs de TYPolygon.
Definition: TYDefines.h:349
Representation d'un ensemble de faces (fichier header)
Outil IHM pour un ensemble de faces (fichier header)
TY_EXTENSION_INST(TYFaceSet)
TY_EXT_GRAPHIC_INST(TYFaceSet)
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
The 4x4 matrix class.
Definition: 3d.h:625
The 3D point class.
Definition: 3d.h:487
virtual const char * getClassName() const
Definition: TYElement.h:249
Class to define a segment.
Definition: 3d.h:1089
Definition: TYBox.h:34
TYPoint & getPosition()
Definition: TYBox.h:113
double _sizeX
Dimension en X.
Definition: TYBox.h:128
double _sizeZ
Dimension en Z.
Definition: TYBox.h:132
double _sizeY
Dimension en Y.
Definition: TYBox.h:130
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
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
virtual void setIsGeometryModified(bool isModified)
Definition: TYElement.cpp:253
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYFaceSet.cpp:130
virtual double volume() const
Definition: TYFaceSet.cpp:172
void purge()
Definition: TYFaceSet.cpp:307
virtual int intersects(const OSegment3D &seg, TYTabPoint &ptList) const
Definition: TYFaceSet.cpp:254
TYTabLPPolygon _faces
Faces.
Definition: TYFaceSet.h:144
TYTabVector _normals
Normales.
Definition: TYFaceSet.h:148
TYTabPoint _sommets
Sommets.
Definition: TYFaceSet.h:146
void UpdateInternals()
Definition: TYFaceSet.cpp:336
void transform(const OMatrix &matrix)
Definition: TYFaceSet.cpp:324
virtual TYPoint centreGravite() const
Definition: TYFaceSet.cpp:221
virtual ~TYFaceSet()
Definition: TYFaceSet.cpp:39
size_t getNbFaces() const
Definition: TYFaceSet.h:119
virtual double surface() const
Definition: TYFaceSet.cpp:183
virtual std::string toString() const
Definition: TYFaceSet.cpp:119
bool operator!=(const TYFaceSet &other) const
Operateur !=.
Definition: TYFaceSet.cpp:84
TYFaceSet & operator=(const TYFaceSet &other)
Operateur =.
Definition: TYFaceSet.cpp:44
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYFaceSet.cpp:89
virtual TYTabVector normals() const
Definition: TYFaceSet.cpp:206
virtual void inverseNormales()
Definition: TYFaceSet.cpp:385
bool operator==(const TYFaceSet &other) const
Operateur ==.
Definition: TYFaceSet.cpp:60
virtual int isInside(const TYPoint &pt) const
Definition: TYFaceSet.cpp:259
virtual int fromXML(DOM_Element domElement)
Definition: TYFaceSet.cpp:146
virtual TYTabLPPolygon faces() const
Definition: TYFaceSet.cpp:216
virtual TYTabPoint sommets() const
Definition: TYFaceSet.cpp:211
virtual double activeSurface() const
Definition: TYFaceSet.cpp:194
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
virtual OVector3D normal() const
Definition: TYPolygon.cpp:243
OPoint3D getPoint(size_t index) const
Definition: TYPolygon.h:134
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYPolygon.cpp:112
virtual int intersects(const TYSurfaceInterface *pSurf, OSegment3D &seg) const
Definition: TYPolygon.cpp:271
void transform(const OMatrix &matrix)
Definition: TYPolygon.cpp:417
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYPolygon.cpp:147
const TYBox & getVolEnglob() const
virtual int intersects(const OSegment3D &seg, TYTabPoint &ptList) const
virtual TYBox volEnglob() const
void calculRayonSphere(const TYBox &volEnglob)
virtual void calculCentreGravite()
std::string intToStr(int val)
Definition: macros.h:158