Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYRectangularMaillageGraphic.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 
21 #include "Tympan/core/color.h"
32 #include <math.h>
33 
35  : TYElementGraphic(pElement)
36 {
38 
39  // XXX What are those constants ?
40  _scalarX = 0.8;
41  _scalarY = 0.25;
42 
43  _scalarW = 150.0;
44  _scalarH = 15.0;
45 
46  _panelW = 125.0;
47  _panelH = 40.0;
48 
49  TYElementGraphic* pTYElementGraphic = getElement()->getPalette()->getGraphicObject();
50  ((TYPaletteGraphic*)pTYElementGraphic)->setWidth(_scalarW);
51  ((TYPaletteGraphic*)pTYElementGraphic)->setHeight(_scalarH);
52 
53  pTYElementGraphic = getElement()->getPanel()->getGraphicObject();
54  ((TYPanelGraphic*)pTYElementGraphic)->setWidth(_panelW);
55  ((TYPanelGraphic*)pTYElementGraphic)->setHeight(_panelH);
56  pTYElementGraphic->setVisible(false);
57 }
58 
60 
61 void TYRectangularMaillageGraphic::update(bool force /*=false*/)
62 {
63  TYRectangularMaillage* pRectangularMaillage = getElement();
64  // TODO We do not want regular scale any longer.
65 
66  // And now... the mesh
67  pRectangularMaillage->computeMesh(_mesh);
68 
69  // And ... the isocurve
70  pRectangularMaillage->computeIsoCurve(_mesh, _isoCurve);
71 
72  pRectangularMaillage->getPalette()->getGraphicObject()->update(force);
73 
75 }
76 
77 //===================================================================
78 
80 {
82 
83  int index1 = 0, index2 = 0;
84  unsigned long largeur = 0, hauteur = 0;
85 
86  TYRectangularMaillage* pMaillage = getElement();
87  OVector3D vecX, vecY;
88  pMaillage->getDimensionsAndSteps(largeur, hauteur, vecX, vecY);
89 
90  const unsigned int larg = largeur; // +1;
91  const unsigned int haut = hauteur; // +1;
92 
93  TYTabLPPointCalcul* pPtsCalcul = &pMaillage->getPtsCalcul();
94 
95  for (int i = 0; i < haut - 1; i++)
96  {
97  for (int j = 0; j < larg; j++)
98  {
99 
100  index1 = i * larg + j;
101  TYPointCalcul* pPtCalcul = pPtsCalcul->at(index1);
102  boundingBox.Enlarge((float)(pPtCalcul->_x), (float)(pPtCalcul->_y), (float)(pPtCalcul->_z));
103  index2 = (i + 1) * larg + j; // nextPoint(pPtsCalcul, index1, (i+1)*larg+j);
104  pPtCalcul = pPtsCalcul->at(index2);
105  boundingBox.Enlarge((float)(pPtCalcul->_x), (float)(pPtCalcul->_y), (float)(pPtCalcul->_z));
106  }
107  }
108  return boundingBox;
109 }
110 
112 {
113  // Pas affiche si inactif
114  if (getElement()->etat() == false)
115  {
116  return;
117  }
118 
119  if (_mesh.empty())
120  {
121  update();
122  }
123 
124  // Les points de ce maillage
125  TYRectangularMaillage* pMaillage = getElement();
126 
127  if (_visible)
128  {
129 
130  if (renderContext.type == TYRenderType::Picking)
131  {
132  TYPickingTable::addElement(getElement());
133  glPushName((GLuint)(TYPickingTable::getIndex()));
134  }
135 
136  switch (_Rendering)
137  {
138  case RENDER_SURF:
139  displaySurface(renderContext);
140  break;
141  case RENDER_LINES:
142  displayLines(renderContext);
143  break;
144  case RENDER_MIXED:
145  displayMixed(renderContext);
146  break;
147  default:
148  displaySurface(renderContext);
149  break;
150  }
151 
152  if (renderContext.type == TYRenderType::Picking)
153  {
154  glPopName();
155  }
156 
157  if (_highlight)
158  {
159  GLint viewport[4];
160  glGetIntegerv(GL_VIEWPORT, viewport);
161  unsigned int width = viewport[2];
162  unsigned int height = viewport[3];
163 
164  GLfloat pointSize = NAN;
165  glGetFloatv(GL_POINT_SIZE, &pointSize);
166  float tmpColor[4];
167  glGetFloatv(GL_CURRENT_COLOR, tmpColor);
168 
169  if (renderContext.pass == TYRenderPass::Overlay)
170  {
171  drawName(renderContext);
172  }
173  glColor3f(tmpColor[0], tmpColor[1], tmpColor[2]);
174 
175  glPointSize(pointSize);
176 
177  // Affichage de la palette graphique
178  TYElementGraphic* pTYElementGraphic = pMaillage->getPalette()->getGraphicObject();
179  TYPaletteGraphic* pTYPaletteGraphic = (TYPaletteGraphic*)pTYElementGraphic;
180  pTYPaletteGraphic->setPosX(width * _scalarX);
181  pTYPaletteGraphic->setPosY(height * _scalarY);
182  pTYPaletteGraphic->display(renderContext);
183  }
184 
185  // Affichage du panel
186  TYElementGraphic* pTYElementGraphic = pMaillage->getPanel()->getGraphicObject();
187  TYPanelGraphic* pTYPanelGraphic = (TYPanelGraphic*)pTYElementGraphic;
188  pTYPanelGraphic->display(renderContext);
189  }
190 }
191 
193 // Mode de Rendu Surface
195 
197 {
198  // Opacite par defaut
199  float opacity = 1.0;
200 
201  // Anticrénelage par défaut
202  bool antialiasing = true;
203 
204 #if TY_USE_IHM
205  // static const char prefName[] = "MaillageOpacity";
206 
207  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "MaillageOpacity"))
208  {
209  opacity = TYPreferenceManager::getFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity");
210  }
211  else
212  {
213  TYPreferenceManager::setFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity", opacity);
214  }
215 
216  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "Antialiasing"))
217  {
218  antialiasing = TYPreferenceManager::getBool(TYDIRPREFERENCEMANAGER, "Antialiasing");
219  }
220  else
221  {
222  TYPreferenceManager::setBool(TYDIRPREFERENCEMANAGER, "Antialiasing", antialiasing);
223  }
224 
225 #endif // TY_USE_IHM
226 
227  // TODO (issue xxx) : instead of a single loop it does
228  // several loops (by batch of 1000 triangles)... for no reason at all?
229  // Maybe an (old) issue with the number of primitives between the glBegin()
230  // and glEnd()... but i've never heard of limit in this case...
231  size_t bmin = 0;
232  size_t bmax = 0;
233  size_t inc = 0;
234 
235  size_t nbTriangles = _mesh.size();
236 
237  if (nbTriangles > 1000)
238  {
239  inc = 1000;
240  }
241  else
242  {
243  inc = nbTriangles;
244  }
245 
246  bmin = 0;
247  bmax += inc;
248  bmax = (bmax <= nbTriangles ? bmax : nbTriangles);
249 
250  // Palette
251  TYRectangularMaillage* pMaillage = getElement();
252  LPTYPalette pPalette = pMaillage->getPalette();
253 
254  // Couleurs des sommets du triangle
255  OColor color0 = OColor();
256  OColor color1 = OColor();
257  OColor color2 = OColor();
258 
259  if (antialiasing)
260  {
261  do
262  {
263  glBegin(GL_TRIANGLES);
264  for (size_t i = bmin; i < bmax; i++)
265  {
266  const MTriangle& vtx = _mesh[i];
267  const MPoint& v1 = vtx.pts[0];
268  const MPoint& v2 = vtx.pts[1];
269  const MPoint& v3 = vtx.pts[2];
270 
271  color0 = pPalette->getColorFromValue(v1.scalar);
272  color1 = pPalette->getColorFromValue(v2.scalar);
273  color2 = pPalette->getColorFromValue(v3.scalar);
274 
275  glColor4f(color0.r, color0.g, color0.b, opacity);
276  glVertex3f(v1.pt._x, v1.pt._y, v1.pt._z);
277 
278  glColor4f(color1.r, color1.g, color1.b, opacity);
279  glVertex3f(v2.pt._x, v2.pt._y, v2.pt._z);
280 
281  glColor4f(color2.r, color2.g, color2.b, opacity);
282  glVertex3f(v3.pt._x, v3.pt._y, v3.pt._z);
283  }
284  glEnd();
285 
286  bmin = bmax;
287  bmax += inc;
288  bmax = (bmax <= nbTriangles ? bmax : nbTriangles);
289  } while (bmin < nbTriangles);
290  }
291  else
292  {
293  glBegin(GL_QUADS);
294  for (size_t i = 0; i < nbTriangles; i = i + 2)
295  {
296  const MTriangle& vtx1 = _mesh[i];
297  const MTriangle& vtx2 = _mesh[i + 1];
298  const MPoint& v1 = vtx1.pts[0];
299  const MPoint& v2 = vtx1.pts[1];
300  const MPoint& v3 = vtx2.pts[2];
301  const MPoint& v4 = vtx1.pts[2];
302 
303  color0 = pPalette->getColorFromValue((v1.scalar + v2.scalar + v3.scalar + v4.scalar) * 0.25f);
304 
305  glColor4f(color0.r, color0.g, color0.b, opacity);
306  glVertex3f(v1.pt._x, v1.pt._y, v1.pt._z);
307 
308  glColor4f(color0.r, color0.g, color0.b, opacity);
309  glVertex3f(v2.pt._x, v2.pt._y, v2.pt._z);
310 
311  glColor4f(color0.r, color0.g, color0.b, opacity);
312  glVertex3f(v3.pt._x, v3.pt._y, v3.pt._z);
313 
314  glColor4f(color0.r, color0.g, color0.b, opacity);
315  glVertex3f(v4.pt._x, v4.pt._y, v4.pt._z);
316  }
317  glEnd();
318  }
319 }
320 
322 // Mode de Rendu Courbes
324 
325 void TYRectangularMaillageGraphic::displayLines(TYRenderContext& renderContext, bool invertColors)
326 {
327  // Opacite par defaut
328  float opacity = 1.0;
329 
330 #if TY_USE_IHM
331  // static const char prefName[] = "MaillageOpacity";
332 
333  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "MaillageOpacity"))
334  {
335  opacity = TYPreferenceManager::getFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity");
336  }
337  else
338  {
339  TYPreferenceManager::setFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity", opacity);
340  }
341 #endif // TY_USE_IHM
342 
343  float lw = NAN;
344  glGetFloatv(GL_LINE_WIDTH, &lw);
345  glLineWidth(3.0f);
346 
347  std::vector<MPoint>::iterator it;
348 
349  // Palette
350  TYRectangularMaillage* pMaillage = getElement();
351  LPTYPalette pPalette = pMaillage->getPalette();
352 
353  glBegin(GL_LINES);
354  for (it = _isoCurve.begin(); it != _isoCurve.end(); it++)
355  {
356  const MPoint& mp = *it;
357  const OColor& color = pPalette->getColorFromValue(mp.scalar);
358  if (invertColors)
359  {
360  glColor4f(1.0f - color.r, 1.0f - color.g, 1.0f - color.b, opacity);
361  }
362  else
363  {
364  glColor4f(color.r, color.g, color.b, opacity);
365  }
366  glVertex3f(mp.pt._x, mp.pt._y,
367  mp.pt._z + 0.01); // 0.01 offset so that we can display the lines from top view
368  }
369  glEnd();
370 
371  glLineWidth(lw);
372 }
373 
375 {
376  displaySurface(renderContext);
377  displayLines(renderContext, true);
378 }
379 
380 int TYRectangularMaillageGraphic::nextPoint(const TYTabLPPointCalcul* pPtsCalcul, const int& index1,
381  const int& index2)
382 {
383 
384  int indice = index2;
385 
386  while ((pPtsCalcul->at(indice)->etat() == false) && (indice > 0))
387  {
388  indice--;
389  }
390 
391  return indice;
392 }
All base classes related to 3D manipulation.
std::vector< LPTYPointCalcul > TYTabLPPointCalcul
Collection de pointeurs de TYPointCalcul.
Definition: TYDefines.h:358
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:51
Representation d'une palette graphique (fichier header)
Representation graphique d'un panel d'informations (fichier header)
Gestion de la table de correspondance indice/element pour le picking (fichier header)
Representation graphique d'un maillage rectangulaire (fichier header)
Contexte de rendu utilisé par les fonctions d'affichage.
@ Picking
The current render is only done for picking purpose.
@ Overlay
The current render pass is for overlay elements.
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
Definition: color.h:36
float b
Definition: color.h:38
float r
Definition: color.h:38
float g
Definition: color.h:38
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
classe graphique pour un element de base
void drawName(TYRenderContext &renderContext)
bool _visible
Inique si l'element est visible.
OBox boundingBox() const
bool _highlight
Indique si le highlight est active pour cet element.
virtual void update(bool force=false)
void setVisible(bool visible=true)
TYTabLPPointCalcul & getPtsCalcul()
Set/Get de la liste des points de calcul.
Definition: TYMaillage.h:123
void computeMesh(std::vector< MTriangle > &mesh) const
Compute the mesh.
Definition: TYMaillage.cpp:457
void computeIsoCurve(std::vector< MTriangle > &mesh, std::vector< MPoint > &isoCurve) const
Compute the isocurve.
Definition: TYMaillage.cpp:533
LPTYPanel getPanel()
Get du panel.
Definition: TYMaillage.h:284
LPTYPalette getPalette()
Get de la palette (_pPalette)
Definition: TYMaillage.h:265
Classe graphique pour representer une palette.
void setPosX(double x)
virtual void display(TYRenderContext &renderContext)
void setPosY(double y)
const OColor & getColorFromValue(values_type value) const
Set de la lookup table.
Definition: TYPalette.cpp:491
classe graphique pour un panel d'informations
virtual void display(TYRenderContext &renderContext)
static int getIndex()
static void addElement(TYElement *pElt)
Classe de definition d'un point de calcul.C'est une classe derivee a TYPoint avec en plus un spectrep...
Definition: TYPointCalcul.h:33
virtual void displaySurface(TYRenderContext &renderContext)
virtual void display(TYRenderContext &renderContext)
enum TYRectangularMaillageGraphic::RenderingMode _Rendering
virtual void displayMixed(TYRenderContext &renderContext)
virtual void update(bool force=false)
virtual void displayLines(TYRenderContext &renderContext, bool invertColors=false)
int nextPoint(const TYTabLPPointCalcul *pPtsCalcul, const int &index1, const int &index2)
TYRectangularMaillageGraphic(TYRectangularMaillage *pElement)
Classe de definition d'un maillage rectangulaire.
void getDimensionsAndSteps(unsigned long &x, unsigned long &y, OVector3D &stepx, OVector3D &stepy) const
Renvoie le nombre de lignes et de colonnes du maillage.En fonction de ses dimensions en 3D et de la d...
TYRenderType type
TYRenderPass pass
TYPalette::values_type scalar
Definition: TYMaillage.h:33
OVector3D pt
Definition: TYMaillage.h:32
MPoint pts[3]
Definition: TYMaillage.h:38