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 (Seriously, has anyone ever done this?) 00019 00020 */ 00021 00022 #ifndef LOOP_HELPER_H 00023 #define LOOP_HELPER_H 00024 00025 #include "MultiDimensionalVector.h" 00026 #include "Vector.impl.h" 00027 00028 #include <memory> 00029 00031 00037 template <unsigned N, unsigned P, unsigned dimension, typename NUM> 00038 class LoopHelper { 00039 00040 public: 00041 00043 LoopHelper( 00044 const VecMath::Vector<P, NUM> &x_min, 00045 const VecMath::Vector<P, NUM> &x_max, 00046 const VecMath::Vector<P, unsigned> &grid_size, 00047 std::shared_ptr< ParametricFunction< N, P, NUM > > f 00048 ); 00049 00051 void recalculateOneDimensionOfGrid( 00052 VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, dimension > &values, 00053 VecMath::Vector<P, NUM> ¤t_x 00054 ); 00055 00056 private: 00057 00059 VecMath::Vector<P, NUM> _x_min; 00061 VecMath::Vector<P, NUM> _x_max; 00063 VecMath::Vector<P, unsigned> _grid_size; 00065 std::shared_ptr< ParametricFunction< N, P, NUM > > _f; 00066 00067 }; 00068 00070 template <unsigned N, unsigned P, typename NUM> 00071 class LoopHelper< N, P, 1, NUM > { 00072 00073 public: 00074 00076 LoopHelper( 00077 const VecMath::Vector<P, NUM> &x_min, 00078 const VecMath::Vector<P, NUM> &x_max, 00079 const VecMath::Vector<P, unsigned> &grid_size, 00080 std::shared_ptr< ParametricFunction< N, P, NUM > > f 00081 ); 00082 00084 void recalculateOneDimensionOfGrid( 00085 VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, 1 > &values, 00086 VecMath::Vector<P, NUM> ¤t_x 00087 ); 00088 00089 private: 00090 00092 VecMath::Vector<P, NUM> _x_min; 00094 VecMath::Vector<P, NUM> _x_max; 00096 VecMath::Vector<P, unsigned> _grid_size; 00098 std::shared_ptr< ParametricFunction< N, P, NUM > > _f; 00099 }; 00100 00101 template <unsigned N, unsigned P, unsigned dimension, typename NUM> 00102 LoopHelper<N, P, dimension, NUM>::LoopHelper( 00103 const VecMath::Vector<P, NUM> &x_min, 00104 const VecMath::Vector<P, NUM> &x_max, 00105 const VecMath::Vector<P, unsigned> &grid_size, 00106 std::shared_ptr< ParametricFunction< N, P, NUM > > f): 00107 _x_min(x_min), _x_max(x_max), _grid_size(grid_size), _f(f) { } 00108 00109 template <unsigned N, unsigned P, typename NUM> 00110 LoopHelper<N, P, 1, NUM>::LoopHelper( 00111 const VecMath::Vector<P, NUM> &x_min, 00112 const VecMath::Vector<P, NUM> &x_max, 00113 const VecMath::Vector<P, unsigned> &grid_size, 00114 std::shared_ptr< ParametricFunction< N, P, NUM > > f): 00115 _x_min(x_min), _x_max(x_max), _grid_size(grid_size), _f(f) { } 00116 00117 template <unsigned N, unsigned P, unsigned dimension, typename NUM> 00118 void 00119 LoopHelper<N, P, dimension, NUM>:: 00120 recalculateOneDimensionOfGrid( 00121 VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, dimension > &values, 00122 VecMath::Vector<P, NUM> ¤t_x) { 00123 unsigned grid_size_in_current_dim = _grid_size[dimension-1]; 00124 NUM x_min_in_current_dim = _x_min[dimension-1]; 00125 NUM x_max_in_current_dim = _x_max[dimension-1]; 00126 00127 values.resize(grid_size_in_current_dim); 00128 00129 for (unsigned i = 0; i < grid_size_in_current_dim; ++i) { 00130 NUM x = (x_max_in_current_dim-x_min_in_current_dim)*(NUM)i/(NUM)(grid_size_in_current_dim-1)+x_min_in_current_dim; 00131 LoopHelper< N, P, dimension-1, NUM > sub_looper( 00132 _x_min, _x_max, _grid_size, _f); 00133 current_x[dimension-1] = x; 00134 sub_looper.recalculateOneDimensionOfGrid(values[i], current_x); 00135 } 00136 } 00137 00138 template <unsigned N, unsigned P, typename NUM> 00139 void 00140 LoopHelper<N, P, 1, NUM>:: 00141 recalculateOneDimensionOfGrid( 00142 VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, 1> &values, 00143 VecMath::Vector<P, NUM> ¤t_x) { 00144 assert(_f != NULL); 00145 unsigned grid_size_in_current_dim = _grid_size[0]; 00146 NUM x_min_in_current_dim = _x_min[0]; 00147 NUM x_max_in_current_dim = _x_max[0]; 00148 values.resize(grid_size_in_current_dim); 00149 00150 for (unsigned i = 0; i < grid_size_in_current_dim; ++i) { 00151 NUM x = (x_max_in_current_dim-x_min_in_current_dim)*(NUM)i/(NUM)(grid_size_in_current_dim-1)+x_min_in_current_dim; 00152 current_x[0] = x; 00153 VecMath::Vector<N, NUM> fx = _f->f(current_x); 00154 values[i] = fx; 00155 } 00156 } 00157 00158 00159 #endif