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