Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYAltimetrieGraphic.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 #ifdef _MSC_VER
24  #pragma warning(disable : 4284)
25 #endif
26 
27 #include "Tympan/core/color.h"
29 
31 
34 
35 #include "TYAltimetrieGraphic.h"
36 
37 #include <math.h>
38 
39 #define IMG(id) OLocalizator::getPicture("TYAltimetrieGraphic", (id))
40 
42 {
43  float hueRange[2] = {0.4f, 0.15f};
44  float saturationRange[2] = {1.0f, 0.8f};
45  float valueRange[2] = {0.5f, 0.25f};
46  TYColorManager::getLinearColorTable(256, hueRange, saturationRange, valueRange, _oColorMap);
47  _pTex = new OGLTexture2D();
48  _isBgImage = false;
49  _theTexture = 0;
50  _angle = 0.0;
52  _imgDataPtr = NULL;
53  _imgXSize = 0;
54  _imgYSize = 0;
56  _visible = false;
57 }
58 
60 {
61  if (_imgDataPtr != NULL)
62  {
63  delete[] _imgDataPtr;
64  _imgDataPtr = NULL;
65  }
66 }
67 
68 void TYAltimetrieGraphic::update(bool force /*=false*/)
69 {
70  if (getModified() || force)
71  {
72  float hsvMinValue[3] = {80.0f, 255.0f, 255.0f};
73  float hsvMaxValue[3] = {120.0f, 255.0f, 112.0f};
74 
75 #if TY_USE_IHM
76  // Couleurs
77  if ((TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMinR")) &&
78  (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMaxR")))
79  {
80  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMin", hsvMinValue[0],
81  hsvMinValue[1], hsvMinValue[2]);
82  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMax", hsvMaxValue[0],
83  hsvMaxValue[1], hsvMaxValue[2]);
84  }
85  else
86  {
87  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMin", hsvMinValue[0],
88  hsvMinValue[1], hsvMinValue[2]);
89  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMax", hsvMaxValue[0],
90  hsvMaxValue[1], hsvMaxValue[2]);
91  }
92 #endif
93 
95  }
96 
98 }
99 
100 void TYAltimetrieGraphic::getChilds(TYListPtrTYElementGraphic& childs, bool recursif /*=true*/)
101 {
102  for (int i = 0; i < getElement()->getListFaces().size(); i++)
103  {
104  TYElementGraphic* pTYElementGraphic = getElement()->getFace(i)->getGraphicObject();
105  childs.push_back(pTYElementGraphic);
106  if (recursif)
107  {
108  pTYElementGraphic->getChilds(childs, recursif);
109  }
110  }
111 }
112 
114 {
115  OBox reset;
116  _boundingBox = reset;
117 
118  for (int i = 0; i < getElement()->getListFaces().size(); i++)
119  {
120  _boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[0]._x),
121  (float)(getElement()->getFace(i)->getPoints()[0]._y),
122  (float)(getElement()->getFace(i)->getPoints()[0]._z));
123  _boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[1]._x),
124  (float)(getElement()->getFace(i)->getPoints()[1]._y),
125  (float)(getElement()->getFace(i)->getPoints()[1]._z));
126  _boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[2]._x),
127  (float)(getElement()->getFace(i)->getPoints()[2]._y),
128  (float)(getElement()->getFace(i)->getPoints()[2]._z));
129  }
130 }
131 
132 void TYAltimetrieGraphic::display(TYElement* pModelerElement /*= nullptr*/, GLenum mode /*= GL_RENDER*/)
133 {
134  if (getElement() == NULL)
135  {
136  return;
137  }
138 
139  if (_visible)
140  {
141  update(true);
142 
143  if (mode == GL_SELECT)
144  {
145  TYPickingTable::addElement(getElement());
146  glPushName((GLuint)(TYPickingTable::getIndex()));
147  }
148 
149  double scalar = NAN; // az++
150  double zmin = NAN, zmax = NAN, zrange = NAN;
151  zmin = _boundingBox._min._z;
152  zmax = _boundingBox._max._z;
153  zrange = zmax - zmin;
154  double color0[3], color1[3], color2[3];
155 
156  bool toTex = false;
157  // if(getElement()->getSol()->getVegetActive())
158  //{
159  // toTex = true;
160  //}
161  bindTexture();
162 
163  if (toTex)
164  {
165  glEnable(GL_TEXTURE_2D);
166  }
167 
168  for (int i = 0; i < getElement()->getListFaces().size(); i++)
169  {
170  if (zrange > 0)
171  {
172  scalar = (getElement()->getFace(i)->getPoints()[0]._z - zmin) / zrange;
173  OColor oColor0 = _oColorMap[scalar * 255];
174  color0[0] = oColor0.r;
175  color0[1] = oColor0.g;
176  color0[2] = oColor0.b;
177 
178  scalar = (getElement()->getFace(i)->getPoints()[1]._z - zmin) / zrange;
179  OColor oColor1 = _oColorMap[scalar * 255];
180  color1[0] = oColor1.r;
181  color1[1] = oColor1.g;
182  color1[2] = oColor1.b;
183 
184  scalar = (getElement()->getFace(i)->getPoints()[2]._z - zmin) / zrange;
185  OColor oColor2 = _oColorMap[scalar * 255];
186  color2[0] = oColor2.r;
187  color2[1] = oColor2.g;
188  color2[2] = oColor2.b;
189  }
190  else
191  {
192  scalar = (getElement()->getFace(i)->getPoints()[0]._z - zmin);
193  OColor oColor0 = _oColorMap[scalar * 255];
194  color0[0] = oColor0.r;
195  color0[1] = oColor0.g;
196  color0[2] = oColor0.b;
197 
198  scalar = (getElement()->getFace(i)->getPoints()[1]._z - zmin);
199  OColor oColor1 = _oColorMap[scalar * 255];
200  color1[0] = oColor1.r;
201  color1[1] = oColor1.g;
202  color1[2] = oColor1.b;
203 
204  scalar = (getElement()->getFace(i)->getPoints()[2]._z - zmin);
205  OColor oColor2 = _oColorMap[scalar * 255];
206  color2[0] = oColor2.r;
207  color2[1] = oColor2.g;
208  color2[2] = oColor2.b;
209  }
210 
211  TYElementGraphic* pTYElementGraphic = getElement()->getFace(i)->getGraphicObject();
212  LPTYPolygonGraphic pPolygonGraphic = (TYPolygonGraphic*)pTYElementGraphic;
213  if (pPolygonGraphic != NULL)
214  {
215  pPolygonGraphic->unsetTextureBg();
216  pPolygonGraphic->setTexture(toTex);
217  if (_isBgImage)
218  {
219  glColor4fv(OColor::WHITE);
220  pPolygonGraphic->setTextureBg(_semiX, _semiY, _imagePosition, _bgImageOrientation);
221  }
222  pPolygonGraphic->setAltimetrieColor(color0, color1, color2);
223  pPolygonGraphic->display(pModelerElement, mode);
224  }
225  }
226 
227  if (toTex || _isBgImage)
228  {
229  glDisable(GL_TEXTURE_2D);
230  }
231 
232  if (mode == GL_SELECT)
233  {
234  glPopName();
235  }
236  }
237 }
238 
240 {
241  // Si le sol est de type vegetation...
242  // if(getElement()->getSol()->getVegetActive())
243  //{
244  // double r = getElement()->getSol()->getResistivite();
245 
246  // // On charge la texture qui va bien.
247  // if(r>=5.0 && r<20.0)
248  // _pTex->load(IMG("id_terrain_0").toLatin1().data());
249  // else if(r>=20.0 && r<50.0)
250  // _pTex->load(IMG("id_terrain_1").toLatin1().data());
251  // else if(r>=50.0 && r<100.0)
252  // _pTex->load(IMG("id_terrain_2").toLatin1().data());
253  // else if(r>=100.0 && r<200.0)
254  // _pTex->load(IMG("id_terrain_3").toLatin1().data());
255  // else if(r>=200.0 && r<500.0)
256  // _pTex->load(IMG("id_terrain_4").toLatin1().data());
257  // else if(r>=500.0 && r<1000.0)
258  // _pTex->load(IMG("id_terrain_5").toLatin1().data());
259  // else if(r>=1000.0 && r<5000.0)
260  // _pTex->load(IMG("id_terrain_6").toLatin1().data());
261  // else if(r>=5000.0 && r<10000.0)
262  // _pTex->load(IMG("id_terrain_7").toLatin1().data());
263  // else if(r>=10000.0 && r<20000.0)
264  // _pTex->load(IMG("id_terrain_8").toLatin1().data());
265  // else if(r>=20000.0)
266  // _pTex->load(IMG("id_terrain_9").toLatin1().data());
267 
268  // // On met la couleur en blanc pour etre sur qu'il n'y a pas de melange de couleur.
269  // glColor4fv(OColor::WHITE);
270 
271  // // On applique la texture.
272  // _pTex->bind();
273  //}
274  // else
275  if (_isBgImage)
276  {
277  if ((_sOldTopoFileName != _sTopoFileName) || (_imgDataPtr == NULL))
278  {
280  if (_imgDataPtr != NULL)
281  {
282  delete[] _imgDataPtr;
283  _imgDataPtr = NULL;
284  }
285  QImage img(_sOldTopoFileName);
286  if (!img.isNull())
287  {
288  QImage glImg = QGLWidget::convertToGLFormat(img);
289 
290  _imgXSize = glImg.width();
291  _imgYSize = glImg.height();
292 
293  int tabSize = glImg.bytesPerLine() * glImg.height();
294  _imgDataPtr = new unsigned char[tabSize];
295  memcpy(_imgDataPtr, glImg.bits(), tabSize);
296  _imgBytesPerPixel = glImg.bytesPerLine() / glImg.width();
297  }
298  }
299 
300  glColor3f(1.0, 1.0, 1.0);
301 
302  if (0 == _theTexture)
303  {
304  glGenTextures(1, &_theTexture);
305  } // az++
306  glBindTexture(GL_TEXTURE_2D, _theTexture);
307 
308  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
309  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
310 
311  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
312  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
313 
314  GLenum format = GL_LUMINANCE;
315  int internalFormat = _imgBytesPerPixel;
316  switch (_imgBytesPerPixel)
317  {
318  case 1:
319  format = GL_LUMINANCE;
320  break;
321  case 2:
322  format = GL_LUMINANCE_ALPHA;
323  break;
324  case 3:
325  format = GL_RGB;
326  break;
327  case 4:
328  format = GL_RGBA;
329  break;
330  }
331 
332  glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, _imgXSize, _imgYSize, 0, format, GL_UNSIGNED_BYTE,
333  (const GLvoid*)_imgDataPtr);
334 
335  // don't accept fragments if they have zero opacity. this will stop the
336  // zbuffer from be blocked by totally transparent texture fragments.
337  glAlphaFunc(GL_GREATER, (GLclampf)0);
338  glEnable(GL_ALPHA_TEST);
339 
340  _angle = 0.0;
341 
342  // On verifie que le vecteur orientation n'est pas nul
343  if (_bgImageOrientation.norme() != 0)
344  {
345  // Calcul de l'angle
346  _angle =
348  _angle = (M_PI / 2.0) - _angle;
349  }
350 
351  glEnable(GL_TEXTURE_2D);
352 
353  glBindTexture(GL_TEXTURE_2D, _theTexture);
354  }
355 }
356 
357 void TYAltimetrieGraphic::setBackgroundImage(QString sTopoFileName, int semiX, int semiY, TYPoint ptPosition,
358  OVector3D bgImageOrientation)
359 {
360  _isBgImage = true;
361  _semiX = semiX;
362  _semiY = semiY;
363  _imagePosition = ptPosition;
364  _bgImageOrientation = bgImageOrientation;
365  _sTopoFileName = sTopoFileName;
366 }
367 
369 {
370  _isBgImage = false;
371  _semiX = 0;
372  _semiY = 0;
375  _sTopoFileName = QString();
376  if (_imgDataPtr != NULL)
377  {
378  delete[] _imgDataPtr;
379  _imgDataPtr = NULL;
380  }
381 }
double SIGNE(double a)
Return the number sign.
Definition: 3d.h:78
Representation graphique de l'altimetrie (fichier header)
list< TYElementGraphic * > TYListPtrTYElementGraphic
List de pointeur de TYElement.
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:52
Gestion de la table de correspondance indice/element pour le picking (fichier header)
The box class.
Definition: 3d.h:1294
virtual void Enlarge(const OPoint3D &pt)
Enlarge the box with the point if the point is outside the box.
Definition: 3d.cpp:1614
OPoint3D _min
Minimal coordinates of the OBox.
Definition: 3d.h:1371
OPoint3D _max
Maximal coordinates of the OBox.
Definition: 3d.h:1372
Definition: color.h:31
float b
Definition: color.h:33
static const OColor WHITE
Definition: color.h:93
float r
Definition: color.h:33
float g
Definition: color.h:33
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
void setBackgroundImage(QString sTopoFileName, int semiX, int semiY, TYPoint ptPosition, OVector3D bgImageOrientation)
unsigned char * _imgDataPtr
virtual void computeBoundingBox()
TYAltimetrieGraphic(TYAltimetrie *pElement)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
virtual void update(bool force=false)
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
Assigne une altitude a chaque point de l'espace.
Definition: TYAltimetrie.h:35
static void getLinearColorTable(const unsigned int &nbColors, const float *hueRange, const float *saturationRange, const float *valueRange, OLookupTable &outColors)
Generation de la table de couleurs basee sur une rampe lineaire ( y = x+0.5/255 )
Definition: color.cpp:308
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)
bool getModified(bool andReset=false)
static int getIndex()
static void addElement(TYElement *pElt)
classe graphique pour un polygone
#define M_PI
Pi.
Definition: color.cpp:25