Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYBox.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"
19 #if TY_USE_IHM
21 #endif
22 #include "TYBox.h"
23 
25 
26 TYBox::TYBox() : _sizeX(1), _sizeY(1), _sizeZ(1)
27 {
29 
30  _position = TYPoint(0, 0, 0);
31 }
32 
33 TYBox::TYBox(const TYBox& other)
34 {
35  *this = other;
36 }
37 
39 
41 {
42  if (this != &other)
43  {
44  TYElement::operator=(other);
46  _sizeX = other._sizeX;
47  _sizeY = other._sizeY;
48  _sizeZ = other._sizeZ;
49  _position = other._position;
50  }
51  return *this;
52 }
53 
54 bool TYBox::operator==(const TYBox& other) const
55 {
56  if (this != &other)
57  {
58  if (TYElement::operator!=(other))
59  {
60  return false;
61  }
62  if (TYColorInterface::operator!=(other))
63  {
64  return false;
65  }
66  if (_sizeX != other._sizeX)
67  {
68  return false;
69  }
70  if (_sizeY != other._sizeY)
71  {
72  return false;
73  }
74  if (_sizeZ != other._sizeZ)
75  {
76  return false;
77  }
78  if (_position != other._position)
79  {
80  return false;
81  }
82  }
83  return true;
84 }
85 
86 bool TYBox::operator!=(const TYBox& other) const
87 {
88  return !operator==(other);
89 }
90 
91 bool TYBox::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
92 {
93  if (!TYElement::deepCopy(pOther, copyId))
94  {
95  return false;
96  }
97 
99 
100  TYBox* pOtherBox = (TYBox*)pOther;
101 
102  _sizeX = pOtherBox->_sizeX;
103  _sizeY = pOtherBox->_sizeY;
104  _sizeZ = pOtherBox->_sizeZ;
105  _position = pOtherBox->_position;
106 
107  return true;
108 }
109 
110 std::string TYBox::toString() const
111 {
112  std::string str =
113  "TYBox: X=" + doubleToStr(_sizeX) + " Y=" + doubleToStr(_sizeY) + " Z=" + doubleToStr(_sizeZ);
114  return str;
115 }
116 
118 {
119  DOM_Element domNewElem = TYElement::toXML(domElement);
120 
121  TYColorInterface::toXML(domNewElem);
122 
123  domNewElem.setAttribute("sizeX", doubleToStr(_sizeX).data());
124  domNewElem.setAttribute("sizeY", doubleToStr(_sizeY).data());
125  domNewElem.setAttribute("sizeZ", doubleToStr(_sizeZ).data());
126 
127  _position.toXML(domNewElem);
128 
129  return domNewElem;
130 }
131 
133 {
134  TYElement::fromXML(domElement);
135 
136  TYColorInterface::fromXML(domElement);
137 
138  _sizeX = TYXMLTools::getElementAttributeToDouble(domElement, "sizeX");
139  _sizeY = TYXMLTools::getElementAttributeToDouble(domElement, "sizeY");
140  _sizeZ = TYXMLTools::getElementAttributeToDouble(domElement, "sizeZ");
141 
142  DOM_Element elemCur;
143 
144  QDomNodeList childs = domElement.childNodes();
145 
146  for (unsigned int i = 0; i < childs.length(); i++)
147  {
148  elemCur = childs.item(i).toElement();
149 
150  _position.callFromXMLIfEqual(elemCur);
151  }
152 
153  return 1;
154 }
155 
156 int TYBox::intersects(const OSegment3D& seg, TYTabPoint& ptList) const
157 {
158  int res = INTERS_NULLE;
159  OPoint3D pt;
160  int nbIntersects = 0;
161 
162  // Construction des faces
163  TYPoint pt1, pt2, pt3, pt4;
164  TYRectangle faces[6];
165  double demiSizeX = _sizeX / 2.0;
166  double demiSizeY = _sizeY / 2.0;
167  double demiSizeZ = _sizeZ / 2.0;
168 
169  pt1.set(+demiSizeX, -demiSizeY, +demiSizeZ);
170  pt2.set(+demiSizeX, +demiSizeY, +demiSizeZ);
171  pt3.set(+demiSizeX, +demiSizeY, -demiSizeZ);
172  pt4.set(+demiSizeX, -demiSizeY, -demiSizeZ);
173  faces[0] = TYRectangle(pt1, pt2, pt3, pt4);
174 
175  pt1.set(-demiSizeX, -demiSizeY, +demiSizeZ);
176  pt2.set(+demiSizeX, -demiSizeY, +demiSizeZ);
177  pt3.set(+demiSizeX, -demiSizeY, -demiSizeZ);
178  pt4.set(-demiSizeX, -demiSizeY, -demiSizeZ);
179  faces[1] = TYRectangle(pt1, pt2, pt3, pt4);
180 
181  pt1.set(-demiSizeX, +demiSizeY, +demiSizeZ);
182  pt2.set(-demiSizeX, -demiSizeY, +demiSizeZ);
183  pt3.set(-demiSizeX, -demiSizeY, -demiSizeZ);
184  pt4.set(-demiSizeX, +demiSizeY, -demiSizeZ);
185  faces[2] = TYRectangle(pt1, pt2, pt3, pt4);
186 
187  pt1.set(+demiSizeX, +demiSizeY, +demiSizeZ);
188  pt2.set(-demiSizeX, +demiSizeY, +demiSizeZ);
189  pt3.set(-demiSizeX, +demiSizeY, -demiSizeZ);
190  pt4.set(+demiSizeX, +demiSizeY, -demiSizeZ);
191  faces[3] = TYRectangle(pt1, pt2, pt3, pt4);
192 
193  pt1.set(-demiSizeX, -demiSizeY, -demiSizeZ);
194  pt2.set(+demiSizeX, -demiSizeY, -demiSizeZ);
195  pt3.set(+demiSizeX, +demiSizeY, -demiSizeZ);
196  pt4.set(-demiSizeX, +demiSizeY, -demiSizeZ);
197  faces[4] = TYRectangle(pt1, pt2, pt3, pt4);
198 
199  pt1.set(-demiSizeX, +demiSizeY, +demiSizeZ);
200  pt2.set(+demiSizeX, +demiSizeY, +demiSizeZ);
201  pt3.set(+demiSizeX, -demiSizeY, +demiSizeZ);
202  pt4.set(-demiSizeX, -demiSizeY, +demiSizeZ);
203  faces[5] = TYRectangle(pt1, pt2, pt3, pt4);
204 
205  // Passage en coord locales
206  OPoint3D ptTmp1 = OVector3D(seg._ptA) - OVector3D(_position);
207  OPoint3D ptTmp2 = OVector3D(seg._ptB) - OVector3D(_position);
208  OSegment3D segTmp(ptTmp1, ptTmp2);
209 
210  // Pour chaque face
211  for (int i = 0; i < 6; i++)
212  {
213  // Recherche de l'intersection du segment avec la face
214  if (faces[i].intersects(segTmp, pt) != INTERS_NULLE)
215  {
216  // On ramene le point en coord d'origine
217  pt = OVector3D(pt) + OVector3D(_position);
218 
219  // Ajout du pt d'intersection trouve a la liste
220  ptList.push_back(pt);
221 
222  // Increment du nb d'intersections trouvees
223  nbIntersects++;
224  }
225  }
226 
227  // Une intersection a-t-elle ete trouvee ?
228  if (nbIntersects > 0)
229  {
230  res = INTERS_OUI;
231  }
232 
233  return res;
234 }
235 
236 int TYBox::isInside(const TYPoint& pt) const
237 {
238  int res = INTERS_NULLE;
239  double demiSizeX = _sizeX / 2.0;
240  double demiSizeY = _sizeY / 2.0;
241  double demiSizeZ = _sizeZ / 2.0;
242 
243  TYPoint ptTmp = OVector3D(pt) - OVector3D(_position);
244 
245  if ((ptTmp._x >= -demiSizeX) && (ptTmp._x <= demiSizeX) && (ptTmp._y >= -demiSizeY) &&
246  (ptTmp._y <= demiSizeY) && (ptTmp._z >= -demiSizeZ) && (ptTmp._z <= demiSizeZ))
247  {
248  res = INTERS_OUI;
249  }
250 
251  return res;
252 }
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
outil IHM pour une boite (fichier header)
TY_EXTENSION_INST(TYBox)
std::vector< TYPoint > TYTabPoint
Collection de TYPoint.
Definition: TYDefines.h:340
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 3D point class.
Definition: 3d.h:487
virtual void set(double x, double y, double z)
Definition: 3d.cpp:381
virtual const char * getClassName() const
Definition: TYElement.h:249
Class to define a segment.
Definition: 3d.h:1089
OPoint3D _ptA
Point A of the segment.
Definition: 3d.h:1201
OPoint3D _ptB
Point B of the segment.
Definition: 3d.h:1203
The 3D vector class.
Definition: 3d.h:298
Definition: TYBox.h:34
TYBox & operator=(const TYBox &other)
Operateur =.
Definition: TYBox.cpp:40
bool operator!=(const TYBox &other) const
Operateur !=.
Definition: TYBox.cpp:86
int intersects(const OSegment3D &seg, TYTabPoint &ptList) const
Definition: TYBox.cpp:156
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYBox.cpp:117
int isInside(const TYPoint &pt) const
Definition: TYBox.cpp:236
bool operator==(const TYBox &other) const
Operateur ==.
Definition: TYBox.cpp:54
virtual int fromXML(DOM_Element domElement)
Definition: TYBox.cpp:132
double _sizeX
Dimension en X.
Definition: TYBox.h:128
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYBox.cpp:91
TYPoint _position
Position du centre de cette boite.
Definition: TYBox.h:136
virtual std::string toString() const
Definition: TYBox.cpp:110
virtual ~TYBox()
Definition: TYBox.cpp:38
double _sizeZ
Dimension en Z.
Definition: TYBox.h:132
TYBox()
Definition: TYBox.cpp:26
double _sizeY
Dimension en Y.
Definition: TYBox.h:130
int fromXML(DOM_Element domElement)
TYColorInterface & operator=(const TYColorInterface &other)
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
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 DOM_Element toXML(DOM_Element &domElement)
Definition: TYPoint.cpp:112
static double getElementAttributeToDouble(DOM_Element parentElem, DOMString attName, bool *ok=NULL)
Definition: TYXMLTools.cpp:288
std::string doubleToStr(double val)
Definition: macros.h:188