Hermes2D  2.0
view.h
1 // This file is part of Hermes2D.
2 //
3 // Copyright 2005-2008 Jakub Cerveny <jakub.cerveny@gmail.com>
4 // Copyright 2005-2008 Lenka Dubcova <dubcova@gmail.com>
5 // Copyright 2005-2008 Pavel Solin <solin@unr.edu>
6 //
7 // Hermes2D is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // Hermes2D is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
19 
20 // $Id: view.h 1086 2008-10-21 09:05:44Z jakub $
21 
22 #ifndef __H2D_VIEW_H
23 #define __H2D_VIEW_H
24 
25 #include "../global.h"
26 #include "vectorizer.h"
27 #include "orderizer.h"
28 
29 namespace Hermes
30 {
31  namespace Hermes2D
32  {
34  namespace Views
35  {
36  // Constants
37 #define H2D_DEFAULT_X_POS -1
38 #define H2D_DEFAULT_Y_POS -1
39 #define H2D_DEFAULT_WIDTH 600
40 #define H2D_DEFAULT_HEIGHT 400
41 #define H2DV_SCALE_LOG_BASE 1.005
42 
43 
47  };
48 
49  struct WinGeom {
50  int x;
51  int y;
52  int width;
53  int height;
54 
55  WinGeom(int x, int y, int width, int height) {
56  this->x = x;
57  this->y = y;
58  this->width = width;
59  this->height = height;
60  };
61  };
62 
70  };
71 
72  // you can define NOGLUT to turn off all OpenGL stuff in Hermes2D
73 #ifndef NOGLUT
74 
84  class HERMES_API View : public Hermes::Mixins::TimeMeasurable, public Hermes::Mixins::Loggable
85  {
86  public:
87 
88  void init();
89  View(const char* title, WinGeom* wg = NULL);
90  View(char* title, WinGeom* wg = NULL);
91  ~View();
92 
93  int create();
94  void close();
95  void refresh();
96 
98  void set_title(const char* title);
99 
100  void set_min_max_range(double min, double max);
101  void auto_min_max_range();
102  void get_min_max_range(double& min, double& max);
103 
104  void show_scale(bool show = true);
105  void set_scale_position(int horz, int vert);
106  void set_scale_size(int width, int height, int numticks);
107  void set_scale_format(const char* fmt);
108  void fix_scale_width(int width = 80);
109 
112  void save_screenshot(const char* bmpname, bool high_quality = false);
115  void save_numbered_screenshot(const char* format, int number, bool high_quality = false);
116 
117  void set_palette(ViewPaletteType type);
118  void set_num_palette_steps(int num);
119  void set_palette_filter(bool linear);
120 
121  void wait_for_keypress(const char* text = NULL);
122  void wait_for_close();
123  void wait_for_draw();
124 
125  static void wait(const char* text);
126  static void wait(ViewWaitEvent wait_event = HERMES_WAIT_CLOSE, const char* text = NULL);
127  void draw_help();
128  virtual void reset_view(bool force_reset);
129 
130  protected: //FPS measurement
131 #define FPS_FRAME_SIZE 5
132  double rendering_frames[FPS_FRAME_SIZE];
134  void draw_fps();
135  static double get_tick_count();
136 
137  protected: //view
139  double vertices_min_x, vertices_max_x, vertices_min_y, vertices_max_y;
140  double scale, log_scale, trans_x, trans_y;
141  double center_x, center_y;
142  int margin, lspace, rspace;
143  int mouse_x, mouse_y;
144  int scx, scy;
145  double objx, objy;
146  bool dragging, scaling;
147 
148  virtual void on_create(int output_id);
149  virtual void on_display() {};
150  virtual void on_reshape(int width, int height);
151  virtual void on_mouse_move(int x, int y);
152  virtual void on_left_mouse_down(int x, int y);
153  virtual void on_left_mouse_up(int x, int y);
154  virtual void on_left_mouse_double_click(int x, int y) {}
155  virtual void on_right_mouse_down(int x, int y);
156  virtual void on_right_mouse_up(int x, int y);
157  virtual void on_right_mouse_double_click(int x, int y) {}
158  virtual void on_middle_mouse_down(int x, int y) {}
159  virtual void on_middle_mouse_up(int x, int y) {}
160  virtual void on_middle_mouse_double_click(int x, int y) {}
161  virtual void on_key_down(unsigned char key, int x, int y);
162  virtual void on_special_key(int key, int x, int y);
163  virtual void on_entry(int state) {}
164  virtual void on_close();
165 
166  virtual void update_layout();
167 
168  protected:
169  std::string title;
170  int output_id;
171  int output_x, output_y, output_width, output_height;
172  float jitter_x, jitter_y;
173  bool hq_frame, frame_ready;
174 
175  ViewPaletteType pal_type;
176  int pal_steps, pal_filter;
177  double tex_scale, tex_shift;
178  bool range_auto;
179  double range_min, range_max;
180 
181  bool b_scale, b_help;
182  bool scale_focused, scale_dragging;
183  int pos_horz, pos_vert;
184  int scale_x, scale_y;
185  int scale_width, scale_height, labels_width;
186  int scale_numticks, scale_box_height, scale_box_skip;
187  char scale_fmt[20];
188  int scale_fixed_width;
189 
190  bool want_screenshot;
191  static int screenshot_no;
192  std::string screenshot_filename;
193 
194  protected: //palette
195  unsigned int gl_pallete_tex_id;
196 
197  void create_gl_palette();
198  virtual void get_palette_color(double x, float* gl_color);
199 
200  protected: //internal functions
201  inline double transform_x(double x) { return (x * scale + trans_x) + center_x; }
202  inline double transform_y(double y) { return center_y - (y * scale + trans_y); }
203  inline double untransform_x(double x) { return (x - center_x - trans_x) / scale; }
204  inline double untransform_y(double y) { return (center_y - y - trans_y) / scale; }
205 
206  virtual void clear_background();
207  void pre_display();
208  void display_antialiased();
209 
210  void set_ortho_projection(bool no_jitter = false);
211  void set_3d_projection(int fov, double znear, double zfar);
212 
213  void draw_text(double x, double y, const char* text, int align = -1);
214  int get_text_width(const char* text);
215 
216  char *get_screenshot_file_name();
217  void save_screenshot_internal(const char* filename);
218 
219  virtual void scale_dispatch();
220  virtual int measure_scale_labels();
221  void draw_continuous_scale(char* title, bool righttext);
222  void draw_discrete_scale(int numboxes, const char* boxnames[], const float boxcolors[][3]);
223 
224  void update_tex_adjust();
225 
226  virtual const char* get_help_text() const = 0;
227 
228  friend void on_display_stub(void);
229  friend void on_reshape_stub(int, int);
230  friend void on_mouse_move_stub(int, int);
231  friend void on_mouse_click_stub(int, int, int, int);
232  friend void on_key_down_stub(unsigned char, int, int);
233  friend void on_special_key_stub(int, int, int);
234  friend void on_entry_stub(int);
235  friend void on_idle_stub();
236  friend void on_close_stub();
237  friend int add_view_in_thread(void*);
238  friend int remove_view_in_thread(void*);
239  friend void on_create(int);
240  };
241 #else
242 class HERMES_API View
243  {
244  public:
245 
246  void init() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
247  View() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
248  View(const char* title, WinGeom* wg = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
249  View(char* title, WinGeom* wg = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
250 
251  int create() { throw Hermes::Exceptions::Exception("GLUT disabled."); return -1; }
252  void close() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
253  void refresh() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
254 
256  void set_title(const char* title) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
257 
258  void set_min_max_range(double min, double max) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
259  void auto_min_max_range() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
260  void get_min_max_range(double& min, double& max) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
261 
262  void show_scale(bool show = true) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
263  void set_scale_position(int horz, int vert) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
264  void set_scale_size(int width, int height, int numticks) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
265  void set_scale_format(const char* fmt) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
266  void fix_scale_width(int width = 80) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
267 
270  void save_screenshot(const char* bmpname, bool high_quality = false) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
273  void save_numbered_screenshot(const char* format, int number, bool high_quality = false) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
274 
275  void set_palette(ViewPaletteType type) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
276  void set_num_palette_steps(int num) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
277  void set_palette_filter(bool linear) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
278 
279  void wait_for_keypress(const char* text = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
280  void wait_for_close() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
281  void wait_for_draw() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
282 
283  static void wait(const char* text) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
284  static void wait(ViewWaitEvent wait_event = HERMES_WAIT_CLOSE, const char* text = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
285  };
286 #endif
287  }
288  }
289 }
290 #endif