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