Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYRectangularMaillageGraphic.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 
21 #include "Tympan/core/color.h"
32 
33 #include <math.h>
34 
36  : TYElementGraphic(pElement)
37 {
39 
40  // XXX What are those constants ?
41  _scalarX = 0.8;
42  _scalarY = 0.25;
43 
44  _scalarW = 150.0;
45  _scalarH = 15.0;
46 
47  _panelW = 125.0;
48  _panelH = 40.0;
49 
50  TYElementGraphic* pTYElementGraphic = getElement()->getPalette()->getGraphicObject();
51  ((TYPaletteGraphic*)pTYElementGraphic)->setWidth(_scalarW);
52  ((TYPaletteGraphic*)pTYElementGraphic)->setHeight(_scalarH);
53 
54  pTYElementGraphic = getElement()->getPanel()->getGraphicObject();
55  ((TYPanelGraphic*)pTYElementGraphic)->setWidth(_panelW);
56  ((TYPanelGraphic*)pTYElementGraphic)->setHeight(_panelH);
57  pTYElementGraphic->setVisible(false);
58 }
59 
61 
62 void TYRectangularMaillageGraphic::update(bool force /*=false*/)
63 {
64  TYRectangularMaillage* pRectangularMaillage = getElement();
65  // TODO We do not want regular scale any longer.
66 
67  // And now... the mesh
68  pRectangularMaillage->computeMesh(_mesh);
69 
70  // And ... the isocurve
71  pRectangularMaillage->computeIsoCurve(_mesh, _isoCurve);
72 
73  pRectangularMaillage->getPalette()->getGraphicObject()->update(force);
74 
76 }
77 
78 //===================================================================
79 
81 {
82  OBox reset;
83 
84  _boundingBox = reset;
85  int index1 = 0, index2 = 0;
86  unsigned long largeur = 0, hauteur = 0;
87 
88  TYRectangularMaillage* pMaillage = getElement();
89  OVector3D vecX, vecY;
90  pMaillage->getDimensionsAndSteps(largeur, hauteur, vecX, vecY);
91 
92  const unsigned int larg = largeur; // +1;
93  const unsigned int haut = hauteur; // +1;
94 
95  TYTabLPPointCalcul* pPtsCalcul = &pMaillage->getPtsCalcul();
96 
97  for (int i = 0; i < haut - 1; i++)
98  {
99  for (int j = 0; j < larg; j++)
100  {
101 
102  index1 = i * larg + j;
103  TYPointCalcul* pPtCalcul = pPtsCalcul->at(index1);
104  _boundingBox.Enlarge((float)(pPtCalcul->_x), (float)(pPtCalcul->_y), (float)(pPtCalcul->_z));
105  index2 = (i + 1) * larg + j; // nextPoint(pPtsCalcul, index1, (i+1)*larg+j);
106  pPtCalcul = pPtsCalcul->at(index2);
107  _boundingBox.Enlarge((float)(pPtCalcul->_x), (float)(pPtCalcul->_y), (float)(pPtCalcul->_z));
108  }
109  }
110 }
111 
112 void TYRectangularMaillageGraphic::display(TYElement* pModelerElement /*= nullptr*/,
113  GLenum mode /*= GL_RENDER*/)
114 {
115  // Pas affiche si inactif
116  if (getElement()->etat() == false)
117  {
118  return;
119  }
120 
121  if (_mesh.empty())
122  {
123  update();
124  }
125 
126  // Les points de ce maillage
127  TYRectangularMaillage* pMaillage = getElement();
128 
129  if (_visible)
130  {
131 
132  if (mode == GL_SELECT)
133  {
134  TYPickingTable::addElement(getElement());
135  glPushName((GLuint)(TYPickingTable::getIndex()));
136  }
137 
138  switch (_Rendering)
139  {
140  case RENDER_SURF:
141  displaySurface(mode);
142  break;
143  case RENDER_LINES:
144  displayLines(mode);
145  break;
146  case RENDER_MIXED:
147  displayMixed(mode);
148  break;
149  default:
150  displaySurface(mode);
151  break;
152  }
153 
154  if (mode == GL_SELECT)
155  {
156  glPopName();
157  }
158 
159  if (_highlight)
160  {
161  GLfloat pointSize = NAN;
162  glGetFloatv(GL_POINT_SIZE, &pointSize);
163  float tmpColor[4];
164  glGetFloatv(GL_CURRENT_COLOR, tmpColor);
165 
166  if (_bFirstDisp)
167  {
169  _bFirstDisp = false;
170  }
172  if (mode == GL_COMPILE)
173  {
174  drawName(pModelerElement);
175  }
176  glColor3f(tmpColor[0], tmpColor[1], tmpColor[2]);
177 
178  glPointSize(pointSize);
179 
180  // Affichage de la palette graphique
181  TYElementGraphic* pTYElementGraphic = pMaillage->getPalette()->getGraphicObject();
182  TYPaletteGraphic* pTYPaletteGraphic = (TYPaletteGraphic*)pTYElementGraphic;
183  pTYPaletteGraphic->setPosX(OSizeManager::get()->getWidth() * _scalarX);
184  pTYPaletteGraphic->setPosY(OSizeManager::get()->getHeight() * _scalarY);
185  pTYPaletteGraphic->display();
186  }
187 
188  // Affichage du panel
189  TYElementGraphic* pTYElementGraphic = pMaillage->getPanel()->getGraphicObject();
190  TYPanelGraphic* pTYPanelGraphic = (TYPanelGraphic*)pTYElementGraphic;
191  pTYPanelGraphic->display();
192 
193  // Calcul du volume englobant pour le fit:
195  }
196 }
197 
199 // Mode de Rendu Surface
201 
202 void TYRectangularMaillageGraphic::displaySurface(GLenum mode) // GLenum mode = GL_RENDER
203 {
204  // Opacite par defaut
205  float opacity = 1.0;
206 
207  // Anticrénelage par défaut
208  bool antialiasing = true;
209 
210 #if TY_USE_IHM
211  // static const char prefName[] = "MaillageOpacity";
212 
213  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "MaillageOpacity"))
214  {
215  opacity = TYPreferenceManager::getFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity");
216  }
217  else
218  {
219  TYPreferenceManager::setFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity", opacity);
220  }
221 
222  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "Antialiasing"))
223  {
224  antialiasing = TYPreferenceManager::getBool(TYDIRPREFERENCEMANAGER, "Antialiasing");
225  }
226  else
227  {
228  TYPreferenceManager::setBool(TYDIRPREFERENCEMANAGER, "Antialiasing", antialiasing);
229  }
230 
231 #endif // TY_USE_IHM
232 
233  // TODO (issue xxx) : instead of a single loop it does
234  // several loops (by batch of 1000 triangles)... for no reason at all?
235  // Maybe an (old) issue with the number of primitives between the glBegin()
236  // and glEnd()... but i've never heard of limit in this case...
237  size_t bmin = 0;
238  size_t bmax = 0;
239  size_t inc = 0;
240 
241  size_t nbTriangles = _mesh.size();
242 
243  if (nbTriangles > 1000)
244  {
245  inc = 1000;
246  }
247  else
248  {
249  inc = nbTriangles;
250  }
251 
252  bmin = 0;
253  bmax += inc;
254  bmax = (bmax <= nbTriangles ? bmax : nbTriangles);
255 
256  // Palette
257  TYRectangularMaillage* pMaillage = getElement();
258  LPTYPalette pPalette = pMaillage->getPalette();
259 
260  do
261  {
262  glBegin(GL_TRIANGLES);
263  for (size_t i = bmin; i < bmax; i++)
264  {
265  const MTriangle& vtx = _mesh[i];
266  const MPoint& v1 = vtx.pts[0];
267  const MPoint& v2 = vtx.pts[1];
268  const MPoint& v3 = vtx.pts[2];
269 
270  OColor color0 = OColor();
271  OColor color2 = OColor();
272  if (antialiasing)
273  {
274  color0 = pPalette->getColorFromValue(v1.scalar);
275  color2 = pPalette->getColorFromValue(v3.scalar);
276  }
277  else
278  {
279  color0 = pPalette->getColorFromValue(v2.scalar);
280  color2 = pPalette->getColorFromValue(v2.scalar);
281  }
282 
283  const OColor& color1 = pPalette->getColorFromValue(v2.scalar);
284 
285  glColor4f(color0.r, color0.g, color0.b, opacity);
286  glVertex3f(v1.pt._x, v1.pt._y, v1.pt._z);
287 
288  glColor4f(color1.r, color1.g, color1.b, opacity);
289  glVertex3f(v2.pt._x, v2.pt._y, v2.pt._z);
290 
291  glColor4f(color2.r, color2.g, color2.b, opacity);
292  glVertex3f(v3.pt._x, v3.pt._y, v3.pt._z);
293  }
294  glEnd();
295 
296  bmin = bmax;
297  bmax += inc;
298  bmax = (bmax <= nbTriangles ? bmax : nbTriangles);
299  } while (bmin < nbTriangles);
300 }
301 
303 // Mode de Rendu Courbes
305 
306 void TYRectangularMaillageGraphic::displayLines(GLenum mode, bool invertColors)
307 {
308  // Opacite par defaut
309  float opacity = 1.0;
310 
311 #if TY_USE_IHM
312  // static const char prefName[] = "MaillageOpacity";
313 
314  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "MaillageOpacity"))
315  {
316  opacity = TYPreferenceManager::getFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity");
317  }
318  else
319  {
320  TYPreferenceManager::setFloat(TYDIRPREFERENCEMANAGER, "MaillageOpacity", opacity);
321  }
322 #endif // TY_USE_IHM
323 
324  float lw = NAN;
325  glGetFloatv(GL_LINE_WIDTH, &lw);
326  glLineWidth(3.0f);
327 
328  std::vector<MPoint>::iterator it;
329 
330  // Palette
331  TYRectangularMaillage* pMaillage = getElement();
332  LPTYPalette pPalette = pMaillage->getPalette();
333 
334  glBegin(GL_LINES);
335  for (it = _isoCurve.begin(); it != _isoCurve.end(); it++)
336  {
337  const MPoint& mp = *it;
338  const OColor& color = pPalette->getColorFromValue(mp.scalar);
339  if (invertColors)
340  {
341  glColor4f(1.0f - color.r, 1.0f - color.g, 1.0f - color.b, opacity);
342  }
343  else
344  {
345  glColor4f(color.r, color.g, color.b, opacity);
346  }
347  glVertex3f(mp.pt._x, mp.pt._y,
348  mp.pt._z + 0.01); // 0.01 offset so that we can display the lines from top view
349  }
350  glEnd();
351 
352  glLineWidth(lw);
353 }
354 
356 {
357  displaySurface(mode);
358  displayLines(mode, true);
359 }
360 
361 int TYRectangularMaillageGraphic::nextPoint(const TYTabLPPointCalcul* pPtsCalcul, const int& index1,
362  const int& index2)
363 {
364 
365  int indice = index2;
366 
367  while ((pPtsCalcul->at(indice)->etat() == false) && (indice > 0))
368  {
369  indice--;
370  }
371 
372  return indice;
373 }
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:52
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)
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
Definition: color.h:31
float b
Definition: color.h:33
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
static LPOSizeManager get()
The 3D vector class.
Definition: 3d.h:298
classe graphique pour un element de base
void drawName(TYElement *pModelerElement=nullptr)
static OBox _globalBoundingBox
bool _visible
Inique si l'element est visible.
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:564
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)
void setPosY(double y)
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
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(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
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 displayMixed(GLenum mode=GL_RENDER)
virtual void displayLines(GLenum mode=GL_RENDER, bool invertColors=false)
enum TYRectangularMaillageGraphic::RenderingMode _Rendering
virtual void update(bool force=false)
virtual void display(TYElement *pModelerElement=nullptr, GLenum mode=GL_RENDER)
int nextPoint(const TYTabLPPointCalcul *pPtsCalcul, const int &index1, const int &index2)
TYRectangularMaillageGraphic(TYRectangularMaillage *pElement)
virtual void displaySurface(GLenum mode=GL_RENDER)
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...
TYPalette::values_type scalar
Definition: TYMaillage.h:33
OVector3D pt
Definition: TYMaillage.h:32
MPoint pts[3]
Definition: TYMaillage.h:38