1
// Gmsh - Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
3
// See the LICENSE.txt file for license information. Please report all
4
// bugs and problems to <gmsh@geuz.org>.
9
#include "PViewOptions.h"
10
#include "PViewData.h"
16
static void drawScaleBar(PView *p, double xmin, double ymin, double width,
17
double height, double tic, int horizontal)
19
PViewOptions *opt = p->getOptions();
21
double box = (horizontal ? width : height) / (opt->NbIso ? opt->NbIso : 1);
23
for(int i = 0; i < opt->NbIso; i++) {
24
if(opt->IntervalsType == PViewOptions::Discrete){
25
unsigned int col = opt->getColor(i, opt->NbIso);
26
glColor4ubv((GLubyte *) &col);
29
glVertex2d(xmin + i * box, ymin);
30
glVertex2d(xmin + (i + 1) * box, ymin);
31
glVertex2d(xmin + (i + 1) * box, ymin + height);
32
glVertex2d(xmin + i * box, ymin + height);
35
glVertex2d(xmin, ymin + i * box);
36
glVertex2d(xmin + width, ymin + i * box);
37
glVertex2d(xmin + width, ymin + (i + 1) * box);
38
glVertex2d(xmin, ymin + (i + 1) * box);
42
else if(opt->IntervalsType == PViewOptions::Continuous){
44
double dv = (opt->TmpMax - opt->TmpMin) / (opt->NbIso ? opt->NbIso : 1);
45
double v1 = opt->TmpMin + i * dv;
46
unsigned int col1 = opt->getColor(v1, opt->TmpMin, opt->TmpMax, true);
47
glColor4ubv((GLubyte *) &col1);
49
glVertex2d(xmin + i * box, ymin + height);
50
glVertex2d(xmin + i * box, ymin);
53
glVertex2d(xmin, ymin + i * box);
54
glVertex2d(xmin + width, ymin + i * box);
56
double v2 = opt->TmpMin + (i + 1) * dv;
57
unsigned int col2 = opt->getColor(v2, opt->TmpMin, opt->TmpMax, true);
58
glColor4ubv((GLubyte *) &col2);
60
glVertex2d(xmin + (i + 1) * box, ymin);
61
glVertex2d(xmin + (i + 1) * box, ymin + height);
64
glVertex2d(xmin + width, ymin + (i + 1) * box);
65
glVertex2d(xmin, ymin + (i + 1) * box);
70
unsigned int col = opt->getColor(i, opt->NbIso);
71
glColor4ubv((GLubyte *) &col);
74
glVertex2d(xmin + box / 2. + i * box, ymin);
75
glVertex2d(xmin + box / 2. + i * box, ymin + height);
78
glVertex2d(xmin, ymin + box / 2. + i * box);
79
glVertex2d(xmin + width, ymin + box / 2. + i * box);
86
static void drawScaleValues(PView *p, double xmin, double ymin, double width,
87
double height, double tic, int horizontal)
89
PViewOptions *opt = p->getOptions();
91
if(!opt->NbIso) return;
93
gl_font(CTX.gl_font_enum, CTX.gl_fontsize);
94
double font_h = gl_height(); // total font height
95
double font_a = gl_height() - gl_descent(); // height above ref pt
98
sprintf(label, opt->Format, -M_PI * 1.e-4);
99
double maxw = gl_width(label);
101
int nbv = opt->NbIso;
102
double f = (opt->IntervalsType == PViewOptions::Discrete ||
103
opt->IntervalsType == PViewOptions::Continuous) ? 2 : 2.5;
105
if(horizontal && width < nbv * maxw){
106
if(width < f * maxw) nbv = 1;
109
else if(!horizontal && height < nbv * font_h){
110
if(height < f * font_h) nbv = 1;
114
double box = (horizontal ? width : height) / opt->NbIso;
115
double vbox = (horizontal ? width : height) / nbv;
117
glColor4ubv((GLubyte *) & CTX.color.text);
119
if(opt->IntervalsType == PViewOptions::Discrete ||
120
opt->IntervalsType == PViewOptions::Continuous){
121
for(int i = 0; i < nbv + 1; i++) {
122
double v = opt->getScaleValue(i, nbv + 1, opt->TmpMin, opt->TmpMax);
123
sprintf(label, opt->Format, v);
125
glRasterPos2d(xmin + i * vbox, ymin + height + tic);
126
Draw_String_Center(label);
129
glRasterPos2d(xmin + width + tic, ymin + i * vbox - font_a / 3.);
135
if(opt->NbIso > 2 && (nbv == 1 || nbv == 2)){
136
vbox = (vbox * nbv - box) / nbv;
139
for(int i = 0; i < nbv; i++) {
140
double v = opt->getScaleValue(i, nbv, opt->TmpMin, opt->TmpMax);
141
sprintf(label, opt->Format, v);
143
glRasterPos2d(xmin + box / 2. + i * vbox, ymin + height + tic);
144
Draw_String_Center(label);
147
glRasterPos2d(xmin + width + tic, ymin + box / 2. + i * vbox - font_a / 3.);
154
static void drawScaleLabel(PView *p, double xmin, double ymin, double width,
155
double height, double tic, int horizontal)
157
PViewData *data = p->getData();
158
PViewOptions *opt = p->getOptions();
160
gl_font(CTX.gl_font_enum, CTX.gl_fontsize);
161
double font_h = gl_height();
165
int nt = data->getNumTimeSteps();
166
if((opt->ShowTime == 1 && nt > 1) || opt->ShowTime == 2){
168
sprintf(tmp, opt->Format, data->getTime(opt->TimeStep));
169
sprintf(label, "%s (%s)", data->getName().c_str(), tmp);
171
else if((opt->ShowTime == 3 && nt > 1) || opt->ShowTime == 4){
172
sprintf(label, "%s (%d)", data->getName().c_str(), opt->TimeStep);
175
sprintf(label, "%s", data->getName().c_str());
178
glRasterPos2d(xmin + width / 2., ymin + height + tic + 1.4 * font_h);
179
Draw_String_Center(label);
182
glRasterPos2d(xmin, ymin - 2 * font_h);
187
static void drawScale(PView *p, double xmin, double ymin, double width,
188
double height, double tic, int horizontal)
190
// use adaptive data if available
191
PViewData *data = p->getData(true);
192
PViewOptions *opt = p->getOptions();
194
if(opt->ExternalViewIndex >= 0){
195
opt->TmpMin = opt->ExternalMin;
196
opt->TmpMax = opt->ExternalMax;
198
else if(opt->RangeType == PViewOptions::Custom){
199
opt->TmpMin = opt->CustomMin;
200
opt->TmpMax = opt->CustomMax;
202
else if(opt->RangeType == PViewOptions::PerTimeStep){
203
opt->TmpMin = data->getMin(opt->TimeStep);
204
opt->TmpMax = data->getMax(opt->TimeStep);
207
opt->TmpMin = data->getMin();
208
opt->TmpMax = data->getMax();
211
drawScaleBar(p, xmin, ymin, width, height, tic, horizontal);
212
drawScaleValues(p, xmin, ymin, width, height, tic, horizontal);
213
drawScaleLabel(p, xmin, ymin, width, height, tic, horizontal);
218
std::vector<PView*> scales;
219
for(unsigned int i = 0; i < PView::list.size(); i++){
220
PViewData *data = PView::list[i]->getData();
221
PViewOptions *opt = PView::list[i]->getOptions();
222
if(!data->getDirty() && opt->Visible && opt->ShowScale &&
223
opt->Type == PViewOptions::Plot3D && data->getNumElements())
224
scales.push_back(PView::list[i]);
226
if(scales.empty()) return;
228
gl_font(CTX.gl_font_enum, CTX.gl_fontsize);
231
for(unsigned int i = 0; i < scales.size(); i++) {
232
PViewOptions *opt = scales[i]->getOptions();
233
sprintf(label, opt->Format, -M_PI * 1.e-4);
234
maxw = std::max(maxw, gl_width(label));
237
const double tic = 10., bar_size = 16.;
238
double width = 0., width_prev = 0., width_total = 0.;
240
for(unsigned int i = 0; i < scales.size(); i++) {
241
PView *p = scales[i];
242
PViewData *data = p->getData();
243
PViewOptions *opt = p->getOptions();
245
if(!opt->AutoPosition) {
246
double w = opt->Size[0], h = opt->Size[1];
247
double x = opt->Position[0], y = opt->Position[1] - h;
248
int c = Fix2DCoordinates(&x, &y);
249
if(c & 1) x -= w / 2.;
250
if(c & 2) y += h / 2.;
251
drawScale(p, x, y, w, h, tic, CTX.post.horizontal_scales);
253
else if(CTX.post.horizontal_scales){
255
double xc = (CTX.viewport[2] - CTX.viewport[0]) / 2.;
256
if(scales.size() == 1){
257
double w = (CTX.viewport[2] - CTX.viewport[0]) / 2., h = bar_size;
258
double x = xc - w / 2., y = CTX.viewport[1] + ysep;
259
drawScale(p, x, y, w, h, tic, 1);
262
double xsep = maxw / 4. + (CTX.viewport[2] - CTX.viewport[0]) / 10.;
263
double w = (CTX.viewport[2] - CTX.viewport[0] - 4 * xsep) / 2.;
266
double x = xc - (i % 2 ? -xsep / 1.5 : w + xsep / 1.5);
267
double y = CTX.viewport[1] + ysep +
268
(i / 2) * (bar_size + tic + 2 * gl_height() + ysep);
269
drawScale(p, x, y, w, h, tic, 1);
274
double dy = 2. * gl_height();
275
if(scales.size() == 1){
276
double ysep = (CTX.viewport[3] - CTX.viewport[1]) / 6.;
277
double w = bar_size, h = CTX.viewport[3] - CTX.viewport[1] - 2 * ysep - dy;
278
double x = CTX.viewport[0] + xsep, y = CTX.viewport[1] + ysep + dy;
279
drawScale(p, x, y, w, h, tic, 0);
282
double ysep = (CTX.viewport[3] - CTX.viewport[1]) / 15.;
284
double h = (CTX.viewport[3] - CTX.viewport[1] - 3 * ysep - 2.5 * dy) / 2.;
285
double x = CTX.viewport[0] + xsep + width_total + (i / 2) * xsep;
286
double y = CTX.viewport[1] + ysep + dy + (1 - i % 2) * (h + 1.5 * dy + ysep);
287
drawScale(p, x, y, w, h, tic, 0);
291
sprintf(label, opt->Format, -M_PI * 1.e-4);
292
width = bar_size + tic + gl_width(label);
295
sprintf(tmp, opt->Format, data->getTime(opt->TimeStep));
296
sprintf(label, "%s (%s)", data->getName().c_str(), tmp);
299
sprintf(label, "%s", data->getName().c_str());
300
width = std::max(width, gl_width(label));
301
if(i % 2) width_total += std::max(bar_size + width, bar_size + width_prev);