Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPaletteGraphic.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 <limits>
24 
25 #include <boost/foreach.hpp>
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/math/special_functions/fpclassify.hpp>
28 
31 
32 #include "TYPaletteGraphic.h"
33 
34 #include <math.h>
35 #include "gui/gl/TYRenderContext.h"
36 #include <QtOpenGL>
37 
38 #define IMG(id) OLocalizator::getPicture("TYPaletteGraphic", (id))
39 
41 {
42  _font = new OGLFont();
43 
44  // Position
45  _posX = 0.0;
46  _posY = 0.0;
47 
48  // Taille de la barre
49  _width = 0.1;
50  _height = 0.1;
51 }
52 
54 
55 void TYPaletteGraphic::update(bool force /*= false*/)
56 {
58 }
59 
61 {
62  LPTYPalette pPalette = getElement();
63  assert(pPalette && "How the hell pPalette could be NULL ?!?");
64 
65  if (pPalette->getNbColors() == 0)
66  {
67  return;
68  } // XXX Shouldn't this be an assert ?!?
69 
70  if (_visible)
71  {
72 
73  GLint viewport[4];
74  glGetIntegerv(GL_VIEWPORT, viewport);
75  unsigned int width = viewport[2];
76  unsigned int height = viewport[3];
77  // On sauvegarde le mode d'affichage des polygones
78  GLdouble polygonMode[2];
79  glGetDoublev(GL_POLYGON_MODE, polygonMode);
80 
81  // Mode GL_FILL
82  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
83 
84  // Nouvelle projection
85  glMatrixMode(GL_PROJECTION);
86  glPushMatrix();
87  glLoadIdentity();
88  glOrtho(0, width, 0.0, height, 0.0, 1.0);
89 
90  // Nouvelle vue
91  glMatrixMode(GL_MODELVIEW);
92  glPushMatrix();
93  glLoadIdentity();
94 
95  // Taille relative des bornes inf et sup
96  const double relBoundSize = 0.2;
97 
98  // Initialisation des variables de position
99  double x = _posX;
100  double y = _posY;
101  const double width_but_bounds = (_width * (1.0 - 2 * relBoundSize));
102  const double dy = _height;
103 
104  // On dessine un quad
105  glBegin(GL_QUADS);
106 
107  TYPalette::values_type value = NAN;
108  OColor color;
109  TYPalette::values_type prev_value = -std::numeric_limits<TYPalette::values_type>::infinity();
110  // On affiche chaque echellon
111  BOOST_FOREACH (boost::tie(value, color), pPalette->getColorMap())
112  {
113  // Handle specially first iteration
114  const double dx =
115  boost::math::isinf(value - prev_value)
116  ? relBoundSize * _width
117  : (pPalette->normalize(value) - pPalette->normalize(prev_value)) * width_but_bounds;
118  prev_value = value;
119  glColor3fv(color);
120  glVertex2d(x, y);
121  glVertex2d(x, y - dy);
122  glVertex2d(x + dx, y - dy);
123  glVertex2d(x + dx, y);
124  x += dx;
125  }
126 
127  // Fin du quad
128  glEnd();
129 
130  // On convertie en string
131  std::ostringstream minValue;
132  minValue << pPalette->getValueMin();
133 
134  std::ostringstream midValue;
135  midValue << " -- ";
136 
137  std::ostringstream maxValue;
138  maxValue << pPalette->getValueMax();
139 
140  // On charge la texture des polices pour la valeur min
141  _font->load(IMG("id_font").toLatin1().data());
142  _font->bind();
143 
144  // On affiche la borne inf
145  // Delta en x : 0.0
146  // Delta en y : 3.0
147  x = _posX;
148  y = _posY + _height + 3.0;
149  _font->drawText("<", OColor::BLACK, x, y);
150 
151  // On affiche la valeur min
152  // Delta en x : -3.0
153  // Delta en y : 3.0
154  x = _posX + _width * 0.2 - 3.0; // XXX What is that ?
155  y = _posY + _height + 3.0;
156  _font->drawText(minValue.str(), OColor::BLACK, x, y);
157 
158  // On affiche la valeur mid
159  // Delta en x : -6.0
160  // Delta en y : 3.0
161  x = _posX + _width * 0.5 - 6.0;
162  y = _posY + _height + 3.0;
163  _font->drawText(midValue.str(), OColor::BLACK, x, y);
164 
165  // On affiche la valeur max
166  // Delta en x : -9.0
167  // Delta en y : 3.0
168  x = _posX + _width * 0.8 - 9.0; // XXX What is that ?
169  y = _posY + _height + 3.0;
170  _font->drawText(maxValue.str(), OColor::BLACK, x, y);
171 
172  // On affiche la borne sup
173  // Delta en x : -8.0
174  // Delta en y : 3.0
175  x = _posX + _width - 8.0; // XXX What is that ?
176  y = _posY + _height + 3.0;
177  _font->drawText(">", OColor::BLACK, x, y);
178 
179  // Ancienne projection
180  glMatrixMode(GL_PROJECTION);
181  glPopMatrix();
182 
183  // Ancienne vue
184  glMatrixMode(GL_MODELVIEW);
185  glPopMatrix();
186 
187  // On restaure le mode d'affichage des polygones
188  glPolygonMode(GL_FRONT_AND_BACK, polygonMode[0]);
189  }
190 }
#define IMG(id)
Representation d'une palette graphique (fichier header)
Contexte de rendu utilisé par les fonctions d'affichage.
Definition: color.h:31
static const OColor BLACK
Definition: color.h:86
bool load(const char *filename)
Definition: OGLFont.cpp:30
void drawText(const std::string &msg, const OColor &color, double x, double y) const
Definition: OGLFont.cpp:64
virtual void bind()
Definition: OGLTexture.cpp:32
classe graphique pour un element de base
bool _visible
Inique si l'element est visible.
virtual void update(bool force=false)
virtual void update(bool force=false)
virtual void display(TYRenderContext &renderContext)
TYPaletteGraphic(TYPalette *pElement)
virtual ~TYPaletteGraphic()
Classe de definition d'une palette.
Definition: TYPalette.h:41
const color_map_type & getColorMap() const
Get de la lookup table.
Definition: TYPalette.h:226
size_t getNbColors() const
Getter for the number of colors.
Definition: TYPalette.h:122
values_type getValueMax() const
Get de la valeur max.
Definition: TYPalette.h:112
float values_type
The real number type used to store values (typically double or float)
Definition: TYPalette.h:49
values_type normalize(values_type value) const
Normalize a value, ie convert a value between min and max to a percentage.
Definition: TYPalette.cpp:483
values_type getValueMin() const
Get de la valeur min.
Definition: TYPalette.h:102