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