Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYAltimetrieGraphic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
23 #include <qopengltexture.h>
24 #ifdef _MSC_VER
25  #pragma warning(disable : 4284)
26 #endif
27 
28 #include "Tympan/core/color.h"
30 
32 
35 
36 #include "TYAltimetrieGraphic.h"
38 #include <QtOpenGL>
39 
40 #include <math.h>
41 
42 #define IMG(id) OLocalizator::getPicture("TYAltimetrieGraphic", (id))
43 
45  : TYElementGraphic(pElement), _glTexture(QOpenGLTexture::Target2D)
46 {
47  float hueRange[2] = {0.4f, 0.15f};
48  float saturationRange[2] = {1.0f, 0.8f};
49  float valueRange[2] = {0.5f, 0.25f};
50  TYColorManager::getLinearColorTable(256, hueRange, saturationRange, valueRange, _oColorMap);
51  _glTextureIsDirty = true;
52  _angle = 0.0;
53  _visible = false;
54 }
55 
57 
58 void TYAltimetrieGraphic::update(bool force /*=false*/)
59 {
60  if (getModified() || force)
61  {
62  float hsvMinValue[3] = {80.0f, 255.0f, 255.0f};
63  float hsvMaxValue[3] = {120.0f, 255.0f, 112.0f};
64 
65 #if TY_USE_IHM
66  // Couleurs
67  if ((TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMinR")) &&
68  (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMaxR")))
69  {
70  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMin", hsvMinValue[0],
71  hsvMinValue[1], hsvMinValue[2]);
72  TYPreferenceManager::getColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMax", hsvMaxValue[0],
73  hsvMaxValue[1], hsvMaxValue[2]);
74  }
75  else
76  {
77  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMin", hsvMinValue[0],
78  hsvMinValue[1], hsvMinValue[2]);
79  TYPreferenceManager::setColor(TYDIRPREFERENCEMANAGER, "AltiGraphicColorMax", hsvMaxValue[0],
80  hsvMaxValue[1], hsvMaxValue[2]);
81  }
82 #endif
83  }
84 
86 }
87 
88 void TYAltimetrieGraphic::getChilds(TYListPtrTYElementGraphic& childs, bool recursif /*=true*/)
89 {
90  for (int i = 0; i < getElement()->getListFaces().size(); i++)
91  {
92  TYElementGraphic* pTYElementGraphic = getElement()->getFace(i)->getGraphicObject();
93  childs.push_back(pTYElementGraphic);
94  if (recursif)
95  {
96  pTYElementGraphic->getChilds(childs, recursif);
97  }
98  }
99 }
100 
102 {
104 
105  for (int i = 0; i < getElement()->getListFaces().size(); i++)
106  {
107  boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[0]._x),
108  (float)(getElement()->getFace(i)->getPoints()[0]._y),
109  (float)(getElement()->getFace(i)->getPoints()[0]._z));
110  boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[1]._x),
111  (float)(getElement()->getFace(i)->getPoints()[1]._y),
112  (float)(getElement()->getFace(i)->getPoints()[1]._z));
113  boundingBox.Enlarge((float)(getElement()->getFace(i)->getPoints()[2]._x),
114  (float)(getElement()->getFace(i)->getPoints()[2]._y),
115  (float)(getElement()->getFace(i)->getPoints()[2]._z));
116  }
117  return boundingBox;
118 }
119 
121 {
122  if (getElement() == NULL)
123  {
124  return;
125  }
126 
127  if (_visible)
128  {
129  update(true);
130 
131  if (renderContext.type == TYRenderType::Picking)
132  {
133  TYPickingTable::addElement(getElement());
134  glPushName((GLuint)(TYPickingTable::getIndex()));
135  }
136 
137  double scalar = NAN; // az++
138  double zmin = NAN, zmax = NAN, zrange = NAN;
139  zmin = boundingBox()._min._z;
140  zmax = boundingBox()._max._z;
141  zrange = zmax - zmin;
142  double color0[3], color1[3], color2[3];
143 
144  bindTexture();
145 
146  for (int i = 0; i < getElement()->getListFaces().size(); i++)
147  {
148  if (zrange > 0)
149  {
150  scalar = (getElement()->getFace(i)->getPoints()[0]._z - zmin) / zrange;
151  OColor oColor0 = _oColorMap[scalar * 255];
152  color0[0] = oColor0.r;
153  color0[1] = oColor0.g;
154  color0[2] = oColor0.b;
155 
156  scalar = (getElement()->getFace(i)->getPoints()[1]._z - zmin) / zrange;
157  OColor oColor1 = _oColorMap[scalar * 255];
158  color1[0] = oColor1.r;
159  color1[1] = oColor1.g;
160  color1[2] = oColor1.b;
161 
162  scalar = (getElement()->getFace(i)->getPoints()[2]._z - zmin) / zrange;
163  OColor oColor2 = _oColorMap[scalar * 255];
164  color2[0] = oColor2.r;
165  color2[1] = oColor2.g;
166  color2[2] = oColor2.b;
167  }
168  else
169  {
170  scalar = (getElement()->getFace(i)->getPoints()[0]._z - zmin);
171  OColor oColor0 = _oColorMap[scalar * 255];
172  color0[0] = oColor0.r;
173  color0[1] = oColor0.g;
174  color0[2] = oColor0.b;
175 
176  scalar = (getElement()->getFace(i)->getPoints()[1]._z - zmin);
177  OColor oColor1 = _oColorMap[scalar * 255];
178  color1[0] = oColor1.r;
179  color1[1] = oColor1.g;
180  color1[2] = oColor1.b;
181 
182  scalar = (getElement()->getFace(i)->getPoints()[2]._z - zmin);
183  OColor oColor2 = _oColorMap[scalar * 255];
184  color2[0] = oColor2.r;
185  color2[1] = oColor2.g;
186  color2[2] = oColor2.b;
187  }
188 
189  TYElementGraphic* pTYElementGraphic = getElement()->getFace(i)->getGraphicObject();
190  LPTYPolygonGraphic pPolygonGraphic = (TYPolygonGraphic*)pTYElementGraphic;
191  if (pPolygonGraphic != NULL)
192  {
193  pPolygonGraphic->unsetTextureBg();
194  pPolygonGraphic->setTexture(false);
195  if (!_image.isNull())
196  {
197  glColor4fv(OColor::WHITE);
198  pPolygonGraphic->setTextureBg(_semiX, _semiY, _imagePosition, _bgImageOrientation);
199  }
200  pPolygonGraphic->setAltimetrieColor(color0, color1, color2);
201  pPolygonGraphic->display(renderContext);
202  }
203  }
204 
205  if (!_image.isNull())
206  {
207  glDisable(GL_TEXTURE_2D);
208  }
209 
210  if (renderContext.type == TYRenderType::Picking)
211  {
212  glPopName();
213  }
214  }
215 }
216 
218 {
219  // Si le sol est de type vegetation...
220  // if(getElement()->getSol()->getVegetActive())
221  //{
222  // double r = getElement()->getSol()->getResistivite();
223 
224  // // On charge la texture qui va bien.
225  // if(r>=5.0 && r<20.0)
226  // _pTex->load(IMG("id_terrain_0").toLatin1().data());
227  // else if(r>=20.0 && r<50.0)
228  // _pTex->load(IMG("id_terrain_1").toLatin1().data());
229  // else if(r>=50.0 && r<100.0)
230  // _pTex->load(IMG("id_terrain_2").toLatin1().data());
231  // else if(r>=100.0 && r<200.0)
232  // _pTex->load(IMG("id_terrain_3").toLatin1().data());
233  // else if(r>=200.0 && r<500.0)
234  // _pTex->load(IMG("id_terrain_4").toLatin1().data());
235  // else if(r>=500.0 && r<1000.0)
236  // _pTex->load(IMG("id_terrain_5").toLatin1().data());
237  // else if(r>=1000.0 && r<5000.0)
238  // _pTex->load(IMG("id_terrain_6").toLatin1().data());
239  // else if(r>=5000.0 && r<10000.0)
240  // _pTex->load(IMG("id_terrain_7").toLatin1().data());
241  // else if(r>=10000.0 && r<20000.0)
242  // _pTex->load(IMG("id_terrain_8").toLatin1().data());
243  // else if(r>=20000.0)
244  // _pTex->load(IMG("id_terrain_9").toLatin1().data());
245 
246  // // On met la couleur en blanc pour etre sur qu'il n'y a pas de melange de couleur.
247  // glColor4fv(OColor::WHITE);
248 
249  // // On applique la texture.
250  // _pTex->bind();
251  //}
252  // else
253  if (_glTextureIsDirty)
254  {
255  _glTextureIsDirty = false;
256  if (_glTexture.isCreated())
257  {
258  _glTexture.destroy();
259  }
260  if (!_image.isNull())
261  {
262  _glTexture.setData(_image, QOpenGLTexture::GenerateMipMaps);
263  _glTexture.setMinificationFilter(QOpenGLTexture::Nearest);
264  _glTexture.setMagnificationFilter(QOpenGLTexture::Nearest);
265  _glTexture.setWrapMode(QOpenGLTexture::Repeat);
266  }
267  }
268 
269  if (_glTexture.isCreated())
270  {
271  glEnable(GL_TEXTURE_2D);
272  _glTexture.bind();
273 
274  glColor3f(1.0, 1.0, 1.0);
275 
276  // don't accept fragments if they have zero opacity. this will stop the
277  // zbuffer from be blocked by totally transparent texture fragments.
278  glAlphaFunc(GL_GREATER, (GLclampf)0);
279  glEnable(GL_ALPHA_TEST);
280 
281  _angle = 0.0;
282  // On verifie que le vecteur orientation n'est pas nul
283  if (_bgImageOrientation.norme() != 0)
284  {
285  // Calcul de l'angle
286  _angle =
288  _angle = (M_PI / 2.0) - _angle;
289  }
290  }
291 }
292 
293 void TYAltimetrieGraphic::setBackgroundImage(QImage& image, int semiX, int semiY, TYPoint ptPosition,
294  OVector3D bgImageOrientation)
295 {
296  _image = image;
297  _glTextureIsDirty = true;
298  _semiX = semiX;
299  _semiY = semiY;
300  _imagePosition = ptPosition;
301  _bgImageOrientation = bgImageOrientation;
302 }
303 
305 {
306  _image = QImage();
307  _glTextureIsDirty = true;
308  _semiX = 0;
309  _semiY = 0;
312 }
double SIGNE(double a)
Return the number sign.
Definition: 3d.h:78
QColor getColor
Representation graphique de l'altimetrie (fichier header)
list< TYElementGraphic * > TYListPtrTYElementGraphic
List de pointeur de TYElement.
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:51
Gestion de la table de correspondance indice/element pour le picking (fichier header)
Contexte de rendu utilisé par les fonctions d'affichage.
@ Picking
The current render is only done for picking purpose.
The box class.
Definition: 3d.h:1346
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:1423
OPoint3D _max
Maximal coordinates of the OBox.
Definition: 3d.h:1424
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(QImage &image, int semiX, int semiY, TYPoint ptPosition, OVector3D bgImageOrientation)
virtual OBox computeBoundingBox() const
TYAltimetrieGraphic(TYAltimetrie *pElement)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
virtual void update(bool force=false)
virtual void display(TYRenderContext &renderContext)
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.
OBox boundingBox() const
virtual void update(bool force=false)
virtual void getChilds(TYListPtrTYElementGraphic &childs, bool recursif=true)
bool getModified() const
static int getIndex()
static void addElement(TYElement *pElt)
classe graphique pour un polygone
const TYRenderType type
#define M_PI
Pi.
Definition: color.cpp:25