Hermes2D  2.0
scalar_view.h
1 // This file is part of Hermes2D.
2 //
3 // Copyright 2009-2010 Ivo Hanak <hanak@byte.cz>
4 // Copyright 2005-2008 Jakub Cerveny <jakub.cerveny@gmail.com>
5 // Copyright 2005-2008 Lenka Dubcova <dubcova@gmail.com>
6 // Copyright 2005-2008 Pavel Solin <solin@unr.edu>
7 //
8 // Hermes2D is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Hermes2D is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
20 
21 // $Id: view.h 1086 2008-10-21 09:05:44Z jakub $
22 
23 #ifndef __H2D_SCALAR_VIEW_H
24 #define __H2D_SCALAR_VIEW_H
25 
26 #include "view.h"
27 
28 namespace Hermes
29 {
30  namespace Hermes2D
31  {
32  namespace Views
33  {
34  // you can define NOGLUT to turn off all OpenGL stuff in Hermes2D
35 #ifndef NOGLUT
36 
41  class HERMES_API ScalarView : public View
42  {
43  public:
44 
45  void init();
46 #ifndef _MSC_VER
47  ScalarView(const char* title = "ScalarView", WinGeom* wg = NULL);
48 #else
49  ScalarView(WinGeom* wg = NULL);
50 #endif
51  ScalarView(char* title, WinGeom* wg = NULL);
52  ~ScalarView();
53 
54  void show(MeshFunction<double>* sln, double eps = HERMES_EPS_NORMAL, int item = H2D_FN_VAL_0,
55  MeshFunction<double>* xdisp = NULL, MeshFunction<double>* ydisp = NULL, double dmult = 1.0);
56 
57  void show_linearizer_data(double eps = HERMES_EPS_NORMAL, int item = H2D_FN_VAL_0);
58 
59  inline void show_mesh(bool show = true) { show_edges = show; refresh(); }
60  inline void show_bounding_box(bool show = true) { show_aabb = show; refresh(); }
61  void show_contours(double step, double orig = 0.0);
62  inline void hide_contours() { contours = false; refresh(); }
63  void set_3d_mode(bool enable = true);
64  void set_vertical_scaling(double sc);
65  void set_min_max_range(double min, double max);
66 
67  virtual void reset_view(bool force_reset);
68 
70  Linearizer* get_linearizer();
71 
72  protected:
75 
78  {
79  float x, y;
80  int id;
81  bool selected;
82  VertexNodeInfo() {};
83  VertexNodeInfo(int id, float x, float y) : x(x), y(y), id(id), selected(false) {};
84  };
85  Hermes::vector<VertexNodeInfo> vertex_nodes;
87 
88  bool allow_node_selection;
89  unsigned int pointed_node_widget;
90  unsigned int selected_node_widget;
91 
92  const int node_pixel_radius;
94 
95  void init_vertex_nodes(const Mesh* mesh);
96  VertexNodeInfo* find_nearest_node_in_range(float x, float y, float radius);
97  static bool compare_vertex_nodes_x(const VertexNodeInfo& a, const VertexNodeInfo& b);
98  void draw_vertex_nodes();
99  void draw_single_vertex_node(const VertexNodeInfo& node);
100  void create_nodes_widgets();
101 
102  protected:
103  struct ElementInfo
104  {
105  float x, y;
106  float width, height;
107  int id;
108  ElementInfo() : x(0), y(0), width(0), height(0), id(-1) {};
109  ElementInfo(int id, float x, float y, float width, float height) : x(x), y(y), width(width), height(height), id(id) {};
110  };
111  Hermes::vector<ElementInfo> element_infos;
112 
113  unsigned int element_id_widget;
114 
116 
117  void init_element_info(const Mesh* mesh);
118  void create_element_info_widgets();
119  void draw_element_infos_2d();
120 
121  protected: //values
122 #define H2DV_GL_MAX_EDGE_BUFFER 128
123 #pragma pack(push)
124 #pragma pack(1)
125  struct GLVertex2
126  {
127  float x, y;
128  float coord;
129  GLVertex2() {};
130  GLVertex2(float x, float y, float coord) : x(x), y(y), coord(coord) {};
131  static const size_t H2D_OFFSETOF_COORD = 2*sizeof(float);
132  };
133 #pragma pack(pop)
134 
135  bool lin_updated;
136 
137  unsigned int gl_coord_buffer;
138  unsigned int gl_index_buffer;
139  unsigned int gl_edge_inx_buffer;
143 
144  bool show_values;
145 
146  void prepare_gl_geometry();
147  void draw_values_2d();
148  void draw_edges_2d();
149 
150  void draw_normals_3d();
151 
152  protected: //edges
153  bool show_edges;
154  bool show_aabb;
155  float edges_color[3];
156 
157  typedef void (*DrawSingleEdgeCallback)(int inx_vert_a, int inx_vert_b, ScalarView* viewer, void* param);
158 
159  void calculate_mesh_aabb(double* x_min, double* x_max, double* y_min, double* y_max);
160 
161  static void draw_gl_edge(int inx_vert_a, int inx_vert_b, ScalarView* viewer, void* param);
162  void draw_edges(DrawSingleEdgeCallback draw_single_edge, void* param, bool boundary_only);
163  void draw_aabb();
164 
165  protected:
166  bool contours;
167  double cont_orig, cont_step;
168  float cont_color[3];
170  bool is_constant;
171 
172  // Perspective projection parameters.
173  static const int fovy;
174  static const double znear;
175  static const double zfar;
176 
177  bool pmode, mode3d, panning;
178  double xrot, yrot, xtrans, ytrans, ztrans;
179  double xzscale, yscale, xctr, yctr, zctr;
180 
182  double value_irange, value_range_avg;
183 
184  double3* normals;
185 
198  double calculate_ztrans_to_fit_view();
199  virtual void update_layout();
200 
201  void draw_tri_contours(double3* vert, int3* tri);
202  void calculate_normals(double3* verts, int num_verts, int3* tris, int num_tris);
203  void init_lighting();
204  void update_mesh_info();
205 
206  virtual void on_display();
207  virtual void on_key_down(unsigned char key, int x, int y);
208  virtual void on_mouse_move(int x, int y);
209  virtual void on_right_mouse_down(int x, int y);
210  virtual void on_middle_mouse_down(int x, int y);
211  virtual void on_middle_mouse_up(int x, int y);
212  virtual const char* get_help_text() const;
213  virtual void on_close();
214  };
215 #else
216 class HERMES_API ScalarView : public View
217  {
218  public:
219  void init() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
220 #ifndef _MSC_VER
221  ScalarView(const char* title = "ScalarView", WinGeom* wg = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
222 #endif
223  ScalarView(char* title, WinGeom* wg = NULL) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
224 
225  void show(MeshFunction<double>* sln, double eps = HERMES_EPS_NORMAL, int item = H2D_FN_VAL_0,
226  MeshFunction<double>* xdisp = NULL, MeshFunction<double>* ydisp = NULL, double dmult = 1.0) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
227 
228  void show_linearizer_data(double eps = HERMES_EPS_NORMAL, int item = H2D_FN_VAL_0) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
229 
230  inline void show_mesh(bool show = true) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
231  inline void show_bounding_box(bool show = true) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
232  void show_contours(double step, double orig = 0.0) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
233  inline void hide_contours() { throw Hermes::Exceptions::Exception("GLUT disabled."); }
234  inline void set_3d_mode(bool enable = true) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
235  void set_vertical_scaling(double sc) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
236  void set_min_max_range(double min, double max) { throw Hermes::Exceptions::Exception("GLUT disabled."); }
237  };
238 #endif
239  }
240  }
241 }
242 #endif