Hermes2D  3.0
view.h
Go to the documentation of this file.
1 // This file is part of Hermes2D.
2 // Hermes2D is free software: you can redistribute it and/or modify
3 // it under the terms of the GNU General Public License as published by
4 // the Free Software Foundation, either version 2 of the License, or
5 // (at your option) any later version.
6 //
7 // Hermes2D 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. See the
10 // GNU General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef __H2D_VIEW_H
18 #define __H2D_VIEW_H
19 
20 #include "../global.h"
21 #include "orderizer.h"
22 
23 namespace Hermes
24 {
25  namespace Hermes2D
26  {
28  namespace Views
29  {
30  // Constants
31 #define H2D_DEFAULT_X_POS -1
32 #define H2D_DEFAULT_Y_POS -1
33 #define H2D_DEFAULT_WIDTH 600
34 #define H2D_DEFAULT_HEIGHT 400
35 #define H2DV_SCALE_LOG_BASE 1.005
36 
37 #define transform_x(x) ((x * scale + trans_x) + center_x)
38 #define transform_y(y) (center_y - (y * scale + trans_y))
39 
44  };
45 
46  struct WinGeom {
47  int x;
48  int y;
49  int width;
50  int height;
51 
52  WinGeom(int x, int y, int width, int height) {
53  this->x = x;
54  this->y = y;
55  this->width = width;
56  this->height = height;
57  };
58  };
59 
66  };
67 
68  // you can define NOGLUT to turn off all OpenGL stuff in Hermes2D
69 #ifndef NOGLUT
70 
80  class HERMES_API View : public Hermes::Mixins::TimeMeasurable, public Hermes::Mixins::Loggable
81  {
82  public:
83 
84  void init();
85  View(const char* title, WinGeom* wg = nullptr);
86  View(char* title, WinGeom* wg = nullptr);
87  ~View();
88 
89  int create();
90  void close();
92  void refresh();
93 
95  const char* get_title() const;
97  void set_title(const char* msg, ...);
98 
99  void set_min_max_range(double min, double max);
100  void auto_min_max_range();
101  void get_min_max_range(double& min, double& max);
102 
103  void show_scale(bool show = true);
104  void set_scale_position(int horz, int vert);
105  void set_scale_size(int width, int height, int numticks);
106  void set_scale_format(const char* fmt);
107  void fix_scale_width(int width = 80);
108 
111  void save_screenshot(const char* bmpname, bool high_quality = false);
114  void save_numbered_screenshot(const char* format, int number, bool high_quality = false);
115 
116  void set_palette(ViewPaletteType type);
117  void set_num_palette_steps(int num);
118  void set_palette_filter(bool linear);
119 
121  static void wait_for_keypress(const char* text = nullptr);
122  void wait_for_close();
123  void wait_for_draw();
124 
126  static void wait(const char* text);
128  static void wait(ViewWaitEvent wait_event = HERMES_WAIT_CLOSE, const char* text = nullptr);
129  void draw_help();
131  virtual void reset_view(bool force_reset);
132 
133  protected: //FPS measurement
134 #define FPS_FRAME_SIZE 5
135  double rendering_frames[FPS_FRAME_SIZE];
140  void draw_fps();
142  static double get_tick_count();
143 
144  protected: //view
148  double vertices_min_x, vertices_max_x, vertices_min_y, vertices_max_y;
149  double scale, log_scale, trans_x, trans_y;
150  double center_x, center_y;
151  int margin, lspace, rspace;
152  int mouse_x, mouse_y;
153  int scx, scy;
154  double objx, objy;
155  bool dragging, scaling;
156 
157  virtual void on_create(int output_id);
158  virtual void on_display() {};
159  virtual void on_reshape(int width, int height);
160  virtual void on_mouse_move(int x, int y);
161  virtual void on_left_mouse_down(int x, int y);
162  virtual void on_left_mouse_up(int x, int y);
163  virtual void on_left_mouse_double_click(int x, int y) {}
164  virtual void on_right_mouse_down(int x, int y);
165  virtual void on_right_mouse_up(int x, int y);
166  virtual void on_right_mouse_double_click(int x, int y) {}
167  virtual void on_middle_mouse_down(int x, int y) {}
168  virtual void on_middle_mouse_up(int x, int y) {}
169  virtual void on_middle_mouse_double_click(int x, int y) {}
170  virtual void on_key_down(unsigned char key, int x, int y);
171  virtual void on_special_key(int key, int x, int y);
172  virtual void on_entry(int state) {}
173  virtual void on_close();
174 
176  virtual void update_layout();
177 
178  protected:
179  std::string title;
180  int output_id;
181  int output_x, output_y, output_width, output_height;
182  float jitter_x, jitter_y;
183  bool hq_frame, frame_ready;
184 
185  ViewPaletteType pal_type;
186  int pal_steps, pal_filter;
187  double tex_scale, tex_shift;
188  bool range_auto;
189  double range_min, range_max;
190 
191  bool b_scale, b_help;
192  bool scale_focused, scale_dragging;
193  int pos_horz, pos_vert;
194  int scale_x, scale_y;
195  int scale_width, scale_height, labels_width;
196  int scale_numticks, scale_box_height, scale_box_skip;
197  char scale_fmt[20];
198  int scale_fixed_width;
199 
200  bool want_screenshot;
201  static int screenshot_no;
202  std::string screenshot_filename;
203 
204  protected: //palette
206  unsigned int gl_pallete_tex_id;
207 
209  void create_gl_palette();
211  virtual void get_palette_color(double x, float* gl_color);
212 
213  protected: //internal functions
214  inline double untransform_x(double x) { return (x - center_x - trans_x) / scale; }
215  inline double untransform_y(double y) { return (center_y - y - trans_y) / scale; }
216 
218  virtual void clear_background();
219  void pre_display();
220  void display_antialiased();
221 
222  void set_ortho_projection(bool no_jitter = false);
223  void set_3d_projection(int fov, double znear, double zfar);
224 
225  void draw_text(double x, double y, const char* text, int align = -1);
226  int get_text_width(const char* text);
227 
228  char *get_screenshot_file_name();
229  void save_screenshot_internal(const char* filename);
230 
231  virtual void scale_dispatch();
232  virtual int measure_scale_labels();
233  void draw_continuous_scale(char* title, bool righttext);
234  void draw_discrete_scale(int numboxes, const char* boxnames[], const float boxcolors[][3]);
235 
236  void update_tex_adjust();
237 
238  virtual const char* get_help_text() const = 0;
239 
240  friend void on_display_stub(void);
241  friend void on_reshape_stub(int, int);
242  friend void on_mouse_move_stub(int, int);
243  friend void on_mouse_click_stub(int, int, int, int);
244  friend void on_key_down_stub(unsigned char, int, int);
245  friend void on_special_key_stub(int, int, int);
246  friend void on_entry_stub(int);
247  friend void on_idle_stub();
248  friend void on_close_stub();
249  friend int add_view_in_thread(void*);
250  friend int remove_view_in_thread(void*);
251  friend void on_create(int);
252  };
253 #else
254  class HERMES_API View
255  {
256  public:
257 
258  void init() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
259  View() {}
260  View(const char* title, WinGeom* wg = nullptr) {}
261  View(char* title, WinGeom* wg = nullptr) {}
262 
263  int create() { throw Hermes::Exceptions::Exception("GLUT disabled."); return -1; }
264  void close() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
265  void refresh() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
266 
268  void set_title(const char* msg, ...) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
269 
270  void set_min_max_range(double min, double max) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
271  void auto_min_max_range() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
272  void get_min_max_range(double& min, double& max) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
273 
274  void show_scale(bool show = true) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
275  void set_scale_position(int horz, int vert) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
276  void set_scale_size(int width, int height, int numticks) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
277  void set_scale_format(const char* fmt) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
278  void fix_scale_width(int width = 80) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
279 
282  void save_screenshot(const char* bmpname, bool high_quality = false) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
285  void save_numbered_screenshot(const char* format, int number, bool high_quality = false) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
286 
287  void set_palette(ViewPaletteType type) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
288  void set_num_palette_steps(int num) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
289  void set_palette_filter(bool linear) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
290 
291  static void wait_for_keypress(const char* text = nullptr) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
292  void wait_for_close() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
293  void wait_for_draw() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
294 
295  static void wait(const char* text) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
296  static void wait(ViewWaitEvent wait_event = HERMES_WAIT_CLOSE, const char* text = nullptr) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
297  };
298 #endif
299  }
300  }
301 }
302 #endif
Definition: adapt.h:24
unsigned int gl_pallete_tex_id
OpenGL texture object ID.
Definition: view.h:206
Represents a simple visualization window.
Definition: view.h:80
Wait for all windows to close.
Definition: view.h:42
ViewWaitEvent
Wait events.
Definition: view.h:41
::xsd::cxx::tree::type type
C++ type corresponding to the anyType XML Schema built-in type.
bool view_not_reset
True if the view was not reset and therefore it has to be.
Definition: view.h:146
ViewPaletteType
View palette type.
Definition: view.h:61
Maximum ID of view palette type.
Definition: view.h:65
int rendering_frames_top
the new_ location of the next FPS
Definition: view.h:138
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
Wait for any unprocessed keypress to happen.
Definition: view.h:43
A palette based on hue scale.
Definition: view.h:62
double vertices_min_x
AABB of shown mesh.
Definition: view.h:148