Hermes2D  3.0
base_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 "base_view.h"
18 
19 #ifndef NOGLUT
20 
21 #include <GL/freeglut.h>
22 #include "global.h"
23 #include "filter.h"
24 
25 namespace Hermes
26 {
27  namespace Hermes2D
28  {
29  namespace Views
30  {
31  template<typename Scalar>
32  BaseView<Scalar>::BaseView(const char* title, WinGeom* wg)
33  : ScalarView((char*)title, wg), sln(MeshFunctionSharedPtr<Scalar>(new Solution<Scalar>())), complex_filter(MeshFunctionSharedPtr<double>())
34  {
35  this->show_edges = true;
36  basic_title.assign(title);
37  }
38 
39  template<typename Scalar>
40  BaseView<Scalar>::BaseView(char* title, WinGeom* wg)
41  : ScalarView(title, wg), sln(MeshFunctionSharedPtr<Scalar>(new Solution<Scalar>())), complex_filter(MeshFunctionSharedPtr<double>())
42  {
43  this->show_edges = true;
44  basic_title.assign(title);
45  }
46 
47  template<typename Scalar>
48  void BaseView<Scalar>::show(SpaceSharedPtr<Scalar> space, int item)
49  {
50  this->space = space;
51  free();
52  ndof = this->space->get_num_dofs();
53  base_index = 0;
54  this->eps = eps;
55  this->item = item;
56  update_solution();
57  }
58 
59  template<typename Scalar>
60  void BaseView<Scalar>::free()
61  {
62  }
63 
64  template<>
65  void BaseView<double>::update_solution()
66  {
67  double* coeffs = malloc_with_check<double>(ndof);
68  memset(coeffs, 0, sizeof(double)* ndof);
69  if (base_index >= 0)
70  {
71  if (base_index < ndof)
72  coeffs[base_index] = 1.0;
73  Solution<double>::vector_to_solution(coeffs, space, sln, false);
74  }
75  else
76  {
77  Solution<double>::vector_to_solution(coeffs, space, sln, true);
78  }
79 
80  ScalarView::show(sln, item);
81  update_title();
82 
83  free_with_check(coeffs);
84  }
85  template<>
86  void BaseView<std::complex<double> >::update_solution()
87  {
88  std::complex<double>* coeffs = malloc_with_check<std::complex<double> >(ndof);
89  memset(coeffs, 0, sizeof(std::complex<double>) * ndof);
90  if (base_index >= 0)
91  {
92  if (base_index < ndof) coeffs[base_index] = 1.0;
93  Solution<std::complex<double> >::vector_to_solution(coeffs, space, sln, false);
94  }
95  else
96  {
97  Solution<std::complex<double> >::vector_to_solution(coeffs, space, sln, true);
98  }
99 
100  complex_filter = MeshFunctionSharedPtr<double>(new Hermes::Hermes2D::RealFilter(sln.get()));
101  ScalarView::show(complex_filter, item);
102  update_title();
103 
104  free_with_check(coeffs);
105  }
106 
107  template<typename Scalar>
108  void BaseView<Scalar>::update_title()
109  {
110  std::stringstream str;
111  str << basic_title << " - dof = " << base_index;
112  if (base_index < 0)
113  str << " (Dirichlet lift)";
114  View::set_title(str.str().c_str());
115  }
116 
117  template<typename Scalar>
118  void BaseView<Scalar>::on_special_key(int key, int x, int y)
119  {
120  switch (key)
121  {
122  case GLUT_KEY_LEFT:
123  if (base_index > -1) base_index--;
124  update_solution();
125  break;
126 
127  case GLUT_KEY_RIGHT:
128  if (base_index < ndof - 1) base_index++;
129  update_solution();
130  break;
131 
132  default:
133  ScalarView::on_special_key(key, x, y);
134  }
135  }
136 
137  template<typename Scalar>
138  const char* BaseView<Scalar>::get_help_text() const
139  {
140  return
141  "BaseView\n\n"
142  "Controls:\n"
143  " Left mouse - pan\n"
144  " Right mouse - zoom\n"
145  " Left arrow - previous basis function\n"
146  " Right arrow - next basis function\n"
147  " 3 - toggle 3D mode\n"
148  " C - center image\n"
149  " F - toggle smooth palette\n"
150  " H - render high-quality frame\n"
151  " M - toggle mesh\n"
152  " P - cycle palettes\n"
153  " S - save screenshot\n"
154  " F1 - this help\n"
155  " Esc, Q - quit\n\n"
156  "3D mode:\n"
157  " Left mouse - rotate\n"
158  " Right mouse - zoom\n"
159  " Middle mouse - pan\n"
160  " * - increase Z scale\n"
161  " / - decrease Z scale";
162  }
163  }
164  }
165 }
166 #endif
167 
168 namespace Hermes
169 {
170  namespace Hermes2D
171  {
172  namespace Views
173  {
174  template class HERMES_API BaseView < double > ;
175  template class HERMES_API BaseView < std::complex<double> > ;
176  }
177  }
178 }
Definition: adapt.h:24
Common definitions for Hermes2D.
Removes the imaginary part from a function.
Definition: filter.h:289