Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSiteNodeGraphic.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 
23 #include "TYSiteNodeGraphic.h"
24 
26 {
27  _texture = 0;
28  _imgDataPtr = NULL;
29  _imgXSize = 0;
30  _imgYSize = 0;
32  _visible = true;
33 }
34 
36 {
37  if (_imgDataPtr != NULL)
38  {
39  delete[] _imgDataPtr;
40  _imgDataPtr = NULL;
41  }
42 }
43 
44 void TYSiteNodeGraphic::update(bool force /*=false*/)
45 {
47 }
48 
49 void TYSiteNodeGraphic::getChilds(TYListPtrTYElementGraphic& childs, bool recursif /*=true*/)
50 {
51  TYElementGraphic* pTYElementGraphic = nullptr;
52 
53  pTYElementGraphic = getElement()->getTopographie()->getGraphicObject().getRealPointer();
54  childs.push_back(pTYElementGraphic);
55  if (recursif)
56  {
57  pTYElementGraphic->getChilds(childs, recursif);
58  }
59 
60  pTYElementGraphic = getElement()->getInfrastructure()->getGraphicObject().getRealPointer();
61  childs.push_back(pTYElementGraphic);
62  if (recursif)
63  {
64  pTYElementGraphic->getChilds(childs, recursif);
65  }
66 
67  // Sites childs
68  TYSiteNode* pTYSiteNode = getElement();
69  TYTabSiteNodeGeoNode& TabSite = pTYSiteNode->getListSiteNode();
70  for (unsigned int i = 0; i < TabSite.size(); i++)
71  {
72  pTYElementGraphic = TabSite.at(i)->getGraphicObject().getRealPointer();
73  childs.push_back(pTYElementGraphic);
74  if (recursif)
75  {
76  pTYElementGraphic->getChilds(childs, recursif);
77  }
78  }
79 }
80 
81 void TYSiteNodeGraphic::display(TYElement* pModelerElement /*= nullptr*/, GLenum mode /*= GL_RENDER*/)
82 {
83  if (!getElement()->isInCurrentCalcul())
84  {
85  return;
86  }
87 
88  TYElementGraphic::display(pModelerElement, mode);
89 
90  if ((getElement()->getUseTopoFile()) && (_visible))
91  {
92  TYSiteNode* pSite = getElement();
93  std::string TopoFile = pSite->getTopoFileName();
94  const char* sTopoFile = TopoFile.data();
95  if ((_sOldTopoFile != QString(sTopoFile)) || (_imgDataPtr == NULL))
96  {
97  _sOldTopoFile = QString(sTopoFile);
98  if (_imgDataPtr != NULL)
99  {
100  delete[] _imgDataPtr;
101  _imgDataPtr = NULL;
102  }
103  QImage img(sTopoFile);
104  if (!img.isNull())
105  {
106  QImage glImg = QGLWidget::convertToGLFormat(img);
107 
108  _imgXSize = glImg.width();
109  _imgYSize = glImg.height();
110 
111  int tabSize = glImg.bytesPerLine() * glImg.height();
112  _imgDataPtr = new unsigned char[tabSize];
113  memcpy(_imgDataPtr, glImg.bits(), tabSize);
114  _imgBytesPerPixel = glImg.bytesPerLine() / glImg.width();
115  }
116  }
117 
118  if (_imgDataPtr != NULL)
119  {
120  glColor3f(1.0, 1.0, 1.0);
121  if (0 == _texture)
122  {
123  glGenTextures(1, &_texture);
124  } // az++
125  glBindTexture(GL_TEXTURE_2D, _texture);
126 
127  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
128  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
129 
130  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
131  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
132 
133  GLenum format = GL_LUMINANCE;
134  int internalFormat = _imgBytesPerPixel;
135  switch (_imgBytesPerPixel)
136  {
137  case 1:
138  format = GL_LUMINANCE;
139  break;
140  case 2:
141  format = GL_LUMINANCE_ALPHA;
142  break;
143  case 3:
144  format = GL_RGB;
145  break;
146  case 4:
147  format = GL_RGBA;
148  break;
149  }
150 
151  glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, _imgXSize, _imgYSize, 0, format, GL_UNSIGNED_BYTE,
152  (const GLvoid*)_imgDataPtr);
153 
154  // don't accept fragments if they have zero opacity. this will stop the
155  // zbuffer from be blocked by totally transparent texture fragments.
156  glAlphaFunc(GL_GREATER, (GLclampf)0);
157  glEnable(GL_ALPHA_TEST);
158 
159  TYPoint pt = getElement()->getPosition();
160 
161  // Adjust background image height
162  float fImgZOffset = 0.0f;
163  if (getElement()->getUseEmpriseAsCrbNiv())
164  {
165  fImgZOffset = (float)getElement()->getAltiEmprise();
166  }
167 
168  double angle = 0.0;
169  OVector3D vecOrientation = getElement()->getOrientation().toVector3D();
170 
171  // On verifie que le vecteur orientation n'est pas nul
172  if (vecOrientation.norme() != 0)
173  {
174  // Calcul de l'angle
175  angle = SIGNE(vecOrientation._y) * acos(vecOrientation._x / vecOrientation.norme());
176  angle = (M_PI / 2.0) - angle;
177  }
178 
179  float x = _imgXSize / 2 * getElement()->getEchelle();
180  float y = _imgYSize / 2 * getElement()->getEchelle();
181 
182  glTranslatef(0.0f, 0.0f, fImgZOffset);
183  glEnable(GL_TEXTURE_2D);
184 
185  glBindTexture(GL_TEXTURE_2D, _texture);
186  glPushMatrix();
187  glRotatef(angle * 180 / M_PI, 0.0, 0.0, 1.0);
188  glBegin(GL_QUADS);
189 
190  glTexCoord2f(0.0, 0.0);
191  glVertex3f(pt._x - x, pt._y - y, pt._z);
192 
193  glTexCoord2f(0.0, 1.0);
194  glVertex3f(pt._x - x, pt._y + y, pt._z);
195 
196  glTexCoord2f(1.0, 1.0);
197  glVertex3f(pt._x + x, pt._y + y, pt._z);
198 
199  glTexCoord2f(1.0, 0.0);
200  glVertex3f(pt._x + x, pt._y - y, pt._z);
201 
202  glEnd();
203  glPopMatrix();
204 
205  glDisable(GL_TEXTURE_2D);
206 
207  glTranslatef(0.0f, 0.0f, -fImgZOffset);
208 
209  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()->getTopographie()->getGraphicObject()))
210  ->setBackgroundImage(_sOldTopoFile, x, y, pt, vecOrientation);
211  }
212  }
213 
214  if (!getElement()->getUseTopoFile())
215  {
216  ((TYTopographieGraphic*)((TYElementGraphic*)getElement()->getTopographie()->getGraphicObject()))
217  ->unsetBackgroundImage();
218  }
219 
220  getElement()->getTopographie()->getGraphicObject()->display(pModelerElement, mode);
221  getElement()->getInfrastructure()->getGraphicObject()->display(pModelerElement, mode);
222  // Sites childs
223  TYSiteNode* pTYSiteNode = getElement(); // az++
224 
225  glTranslatef(0.0f, 0.0f, 0.01f);
226  TYTabSiteNodeGeoNode& TabSite = pTYSiteNode->getListSiteNode(); // az++
227  for (unsigned int i = 0; i < TabSite.size(); i++)
228  {
229  // az++
230  // Le site n'est affiché que s'il est dans le calcul courant
231  if (TabSite.at(i)->getElement()->isInCurrentCalcul())
232  {
233  TabSite.at(i)->getGraphicObject()->display(pModelerElement, mode); // az++
234  }
235  } // az++
236 
237  glTranslatef(0.0f, 0.0f, -0.01f);
238 }
239 
240 #if TY_USE_IHM
241 void TYSiteNodeGraphic::connectUpdateSignal(QObject* pReceiver, const char* member)
242 {
243  // _pUpdateSignal->connect(pReceiver, member);
244 }
245 #endif // TY_USE_IHM
246 
247 #if TY_USE_IHM
248 void TYSiteNodeGraphic::disconnectUpdateSignal(QObject* pReceiver, const char* member)
249 {
250  // _pUpdateSignal->disconnect(pReceiver, member);
251 }
252 #endif // TY_USE_IHM
double SIGNE(double a)
Return the number sign.
Definition: 3d.h:78
list< TYElementGraphic * > TYListPtrTYElementGraphic
List de pointeur de TYElement.
Representation graphique d'un ensemble de sites (fichier header)
std::vector< LPTYSiteNodeGeoNode > TYTabSiteNodeGeoNode
Collection de noeuds geometriques de type TYSiteNode.
Definition: TYSiteNode.h:40
Representation graphique d'une topographie (fichier header)
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 vector class.
Definition: 3d.h:298
double norme() const
Computes the length of this vector.
Definition: 3d.cpp:215
classe graphique pour un element de base
bool _visible
Inique si l'element est visible.
virtual void update(bool force=false)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
virtual void update(bool force=false)
unsigned char * _imgDataPtr
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
TYSiteNodeGraphic(TYSiteNode *pElement)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
std::string getTopoFileName() const
Definition: TYSiteNode.h:240
TYTabSiteNodeGeoNode & getListSiteNode()
Definition: TYSiteNode.h:336
classe graphique pour une topographie
#define M_PI
Pi.
Definition: color.cpp:25