Hermes2D  3.0
order_view.cpp
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D 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 //
8 // Hermes2D 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. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 // $Id: view4.cpp 1086 2008-10-21 09:05:44Z jakub $
16 
17 #include "order_view.h"
18 
19 #ifndef NOGLUT
20 #include <GL/freeglut.h>
21 #include "global.h"
22 #include "space.h"
23 namespace Hermes
24 {
25  namespace Hermes2D
26  {
27  namespace Views
28  {
29  OrderView::OrderView(const char* title, WinGeom* wg)
30  : View(title, wg)
31  {
32  b_scale = true;
33  b_orders = false;
34  scale_width = 36;
35  scale_box_height = 25;
36  scale_box_skip = 9;
37  }
38 
39  OrderView::OrderView(char* title, WinGeom* wg)
40  : View(title, wg)
41  {
42  b_scale = true;
43  b_orders = false;
44  scale_width = 36;
45  scale_box_height = 25;
46  scale_box_skip = 9;
47  }
48 
49  static int order_palette[] =
50  {
51  0x7f7f7f,
52  0x7f2aff,
53  0x2a2aff,
54  0x2a7fff,
55  0x00d4aa,
56  0x00aa44,
57  0xabc837,
58  0xffd42a,
59  0xc87137,
60  0xc83737,
61  0xff0000
62  };
63 
64  template<typename Scalar>
65  void OrderView::show(SpaceSharedPtr<Scalar> space, bool show_edge_orders)
66  {
67  if (!space->is_up_to_date())
68  throw Hermes::Exceptions::Exception("The space is not up to date.");
69 
70  ord.lock_data();
71  ord.process_space(space, show_edge_orders);
72  ord.calc_vertices_aabb(&vertices_min_x, &vertices_max_x, &vertices_min_y, &vertices_max_y);
73  init_order_palette(ord.get_vertices());
74  ord.unlock_data();
75 
76  create();
77  update_layout();
78  reset_view(false);
79  refresh();
80  wait_for_draw();
81  }
82 
83  void OrderView::init_order_palette(double3* vert)
84  {
85  int min = 1, max = (int)vert[0][2];
86  for (int i = 0; i < ord.get_num_vertices(); i++)
87  {
88  if ((int)vert[i][2] < min) min = (int)vert[i][2];
89  if ((int)vert[i][2] > max) max = (int)vert[i][2];
90  }
91 
92  num_boxes = max - min + 1;
93  char* buf = text_buffer;
94  for (int i = 0; i < num_boxes; i++)
95  {
96  get_palette_color((i + min) / (double)H2DV_MAX_VIEWABLE_ORDER, &order_colors[i + min][0]);
97 
98  sprintf(buf, "%d", i + min);
99  box_names[i] = buf;
100  buf += strlen(buf) + 1;
101  }
102 
103  scale_height = num_boxes * scale_box_height + (num_boxes - 1) * scale_box_skip;
104  order_min = min;
105  }
106 
107  void OrderView::set_b_orders(bool set)
108  {
109  b_orders = set;
110  refresh();
111  }
112 
113  void OrderView::on_display()
114  {
115  set_ortho_projection();
116  glDisable(GL_TEXTURE_1D);
117  glDisable(GL_LIGHTING);
118  glDisable(GL_DEPTH_TEST);
119 
120  // transform all vertices
121  ord.lock_data();
122  int i, nv = ord.get_num_vertices();
123  double3* vert = ord.get_vertices();
124  double2* tvert = malloc_with_check<double2>(nv);
125  for (i = 0; i < nv; i++)
126  {
127  tvert[i][0] = transform_x(vert[i][0]);
128  tvert[i][1] = transform_y(vert[i][1]);
129  }
130 
131  // draw all triangles
132  int3* tris = ord.get_triangles();
133  glBegin(GL_TRIANGLES);
134  for (i = 0; i < ord.get_num_triangles(); i++)
135  {
136  const float* color = order_colors[(int)vert[tris[i][0]][2]];
137  glColor3f(color[0], color[1], color[2]);
138 
139  glVertex2d(tvert[tris[i][0]][0], tvert[tris[i][0]][1]);
140  glVertex2d(tvert[tris[i][1]][0], tvert[tris[i][1]][1]);
141  glVertex2d(tvert[tris[i][2]][0], tvert[tris[i][2]][1]);
142  }
143  glEnd();
144 
145  // draw all edges
146  if (pal_type == 0)
147  glColor3f(0.4f, 0.4f, 0.4f);
148  else if (pal_type == 1)
149  glColor3f(1.0f, 1.0f, 1.0f);
150  else
151  glColor3f(0.0f, 0.0f, 0.0f);
152  glBegin(GL_LINES);
153  int2* edges = ord.get_edges();
154  for (i = 0; i < ord.get_num_edges(); i++)
155  {
156  glVertex2d(tvert[edges[i][0]][0], tvert[edges[i][0]][1]);
157  glVertex2d(tvert[edges[i][1]][0], tvert[edges[i][1]][1]);
158  }
159  glEnd();
160 
161  // draw labels
162  if (b_orders)
163  {
164  int* lvert;
165  char** ltext;
166  double2* lbox;
167  int nl = ord.get_labels(lvert, ltext, lbox);
168  for (i = 0; i < nl; i++)
169  if (lbox[i][0] * scale > get_text_width(ltext[i]) &&
170  lbox[i][1] * scale > 13)
171  {
172  //color = get_palette_color((vert[lvert[i]][2] - 1) / 9.0);
173  const float* color = order_colors[(int)vert[lvert[i]][2]];
174  if ((color[0] * 0.39f + color[1] * 0.50f + color[2] * 0.11f) > 0.5f)
175  glColor3f(0, 0, 0);
176  else
177  glColor3f(1, 1, 1);
178 
179  draw_text(tvert[lvert[i]][0], tvert[lvert[i]][1], ltext[i], 0);
180  }
181  }
182 
183  free_with_check(tvert);
184  ord.unlock_data();
185  }
186 
187  int OrderView::measure_scale_labels()
188  {
189  return 0;
190  }
191 
192  void OrderView::scale_dispatch()
193  {
194  draw_discrete_scale(num_boxes, box_names, order_colors + order_min);
195  }
196 
197  void OrderView::on_key_down(unsigned char key, int x, int y)
198  {
199  switch (key)
200  {
201  case 'c':
202  reset_view(true);
203  refresh();
204  break;
205 
206  case 'm':
207  b_orders = !b_orders;
208  refresh();
209  break;
210 
211  case 'p':
212  {
213  switch (pal_type)
214  {
215  case H2DV_PT_HUESCALE: pal_type = H2DV_PT_GRAYSCALE; break;
216  case H2DV_PT_GRAYSCALE: pal_type = H2DV_PT_INVGRAYSCALE; break;
217  case H2DV_PT_INVGRAYSCALE: pal_type = H2DV_PT_HUESCALE; break;
218  default: throw Hermes::Exceptions::Exception("Invalid palette type");
219  }
220  ord.lock_data();
221  init_order_palette(ord.get_vertices());
222  ord.unlock_data();
223  refresh();
224  break;
225  }
226 
227  default:
228  View::on_key_down(key, x, y);
229  break;
230  }
231  }
232 
233  const char* OrderView::get_help_text() const
234  {
235  return
236  "OrderView\n\n"
237  "Controls:\n"
238  " Left mouse - pan\n"
239  " Right mouse - zoom\n"
240  " C - center image\n"
241  " M - toggle element orders\n"
242  " H - render high-quality frame\n"
243  " P - cycle palettes\n"
244  " S - save screenshot\n"
245  " F1 - this help\n"
246  " Esc, Q - quit";
247  }
248  }
249  }
250 }
251 #endif
252 
253 namespace Hermes
254 {
255  namespace Hermes2D
256  {
257  namespace Views
258  {
259  template HERMES_API void OrderView::show<double>(const SpaceSharedPtr<double> space, bool);
260  template HERMES_API void OrderView::show<std::complex<double> >(const SpaceSharedPtr<std::complex<double> > space, bool);
261  }
262  }
263 }
Definition: adapt.h:24
Common definitions for Hermes2D.
A palette based on hue scale.
Definition: view.h:62