HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #ifndef FUNCTIONVALUEGRID_H 00022 #define FUNCTIONVALUEGRID_H 00023 00024 #include "VertexGrid.h" 00025 00026 #include "ParametricFunction.h" 00027 00028 #include "LoopHelper.h" 00029 #include "MultiDimensionalVector.h" 00030 #include "Vector.h" 00031 00032 #include <memory> 00033 00035 00046 template <unsigned N, unsigned P, typename NUM = double> 00047 class FunctionValueGrid: public VertexGrid<N, P, NUM> { 00048 00049 public: 00050 00052 typedef typename VertexGrid<N, P, NUM>::grid_size_type grid_size_type; 00054 typedef typename VertexGrid<N, P, NUM>::value_storage_type value_storage_type; 00055 00057 typedef ParametricFunction<N, P, NUM> function_type; 00059 typedef std::shared_ptr<function_type> function_ptr_type; 00061 typedef VecMath::Vector<P, NUM> boundary_type; 00062 00064 FunctionValueGrid(); 00066 FunctionValueGrid(const function_ptr_type &f); 00068 FunctionValueGrid(const function_ptr_type &f, 00069 const grid_size_type &grid_size); 00071 FunctionValueGrid(const function_ptr_type &f, 00072 const grid_size_type &grid_size, 00073 const boundary_type &x_min, const boundary_type &x_max); 00074 00075 virtual ~FunctionValueGrid() { } 00076 00078 void setGridSize(const grid_size_type &grid_size); 00080 void setBoundaries(const boundary_type &x_min, const boundary_type &x_max); 00081 00082 private: 00083 00085 void recalculate_grid(); 00086 00088 function_ptr_type _f; 00090 boundary_type _x_min; 00092 boundary_type _x_max; 00093 00095 static constexpr double EPSILON = 1e-8; 00096 }; 00097 00098 template <unsigned N, unsigned P, typename NUM> 00099 FunctionValueGrid<N, P, NUM>::FunctionValueGrid(): 00100 VertexGrid<N, P, NUM>(), 00101 _f(), _x_min(), _x_max() { } 00102 00105 template <unsigned N, unsigned P, typename NUM> 00106 FunctionValueGrid<N, P, NUM>::FunctionValueGrid( 00107 const FunctionValueGrid::function_ptr_type &f): 00108 VertexGrid<N, P, NUM>(), 00109 _f(f), _x_min(), _x_max() { 00110 setBoundaries(f->getDefaultXMin(), f->getDefaultXMax()); 00111 } 00112 00116 template <unsigned N, unsigned P, typename NUM> 00117 FunctionValueGrid<N, P, NUM>::FunctionValueGrid( 00118 const FunctionValueGrid::function_ptr_type &f, 00119 const FunctionValueGrid::grid_size_type &grid_size): 00120 VertexGrid<N, P, NUM>(), 00121 _f(f), _x_min(), _x_max() { 00122 setGridSize(grid_size); 00123 setBoundaries(f->getDefaultXMin(), f->getDefaultXMax()); 00124 } 00125 00131 template <unsigned N, unsigned P, typename NUM> 00132 FunctionValueGrid<N, P, NUM>::FunctionValueGrid( 00133 const FunctionValueGrid::function_ptr_type &f, 00134 const FunctionValueGrid::grid_size_type &grid_size, 00135 const FunctionValueGrid::boundary_type &x_min, 00136 const FunctionValueGrid::boundary_type &x_max): 00137 VertexGrid<N, P, NUM>(), 00138 _f(f), _x_min(), _x_max() { 00139 setGridSize(grid_size); 00140 setBoundaries(x_min, x_max); 00141 } 00142 00145 template <unsigned N, unsigned P, typename NUM> 00146 void FunctionValueGrid<N, P, NUM>::setGridSize( 00147 const FunctionValueGrid::grid_size_type &grid_size) { 00148 if (grid_size == VertexGrid<N, P, NUM>::_grid_size) return; 00149 VertexGrid<N, P, NUM>::_grid_size = grid_size; 00150 if (abs(VecMath::sqnorm(_x_max-_x_min)) > EPSILON) recalculate_grid(); 00151 } 00152 00156 template <unsigned N, unsigned P, typename NUM> 00157 void FunctionValueGrid<N, P, NUM>::setBoundaries( 00158 const FunctionValueGrid::boundary_type &x_min, const FunctionValueGrid::boundary_type &x_max) { 00159 if (abs(VecMath::sqnorm(_x_max-x_max)) < EPSILON && abs(VecMath::sqnorm(_x_min-x_min)) < EPSILON) return; 00160 _x_min = x_min; 00161 _x_max = x_max; 00162 if (VecMath::sqnorm(VertexGrid<N, P, NUM>::_grid_size)) recalculate_grid(); 00163 } 00164 00167 template <unsigned N, unsigned P, typename NUM> 00168 void FunctionValueGrid<N, P, NUM>::recalculate_grid() { 00169 LoopHelper< N, P, P, NUM > looper( 00170 _x_min, _x_max, VertexGrid<N, P, NUM>::_grid_size, _f); 00171 VecMath::Vector<P, NUM> x = _x_min; 00172 looper.recalculateOneDimensionOfGrid(VertexGrid<N, P, NUM>::_vertices, x); 00173 } 00174 00175 #endif // FUNCTIONVALUEGRID_H