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 VertexGrid_H 00022 #define VertexGrid_H 00023 00024 #include "ParametricFunction.h" 00025 00026 #include "LoopHelper.h" 00027 #include "MultiDimensionalVector.h" 00028 #include "Vector.h" 00029 00030 #include <memory> 00031 00033 00044 template <unsigned N, unsigned P, typename NUM = double> 00045 class VertexGrid { 00046 00047 public: 00048 00050 typedef VecMath::Vector<P, unsigned> grid_size_type; 00052 typedef VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, P > value_storage_type; 00053 00055 VertexGrid(); 00057 VertexGrid(const VertexGrid::grid_size_type &grid_size); 00058 00059 VertexGrid(const value_storage_type &vertices); 00061 VertexGrid(const VertexGrid<N, P, NUM> &other) { 00062 _vertices = other._vertices; 00063 _grid_size = other._grid_size; 00064 } 00065 00066 VertexGrid<N, P, NUM> & operator=(const VertexGrid<N, P, NUM> & other) { 00067 if (this == &other) return *this; 00068 _vertices = other._vertices; 00069 _grid_size = other._grid_size; 00070 return *this; 00071 } 00072 00073 virtual ~VertexGrid() { } 00074 00076 void setGridSize(const VertexGrid::grid_size_type &grid_size); 00077 00079 const value_storage_type &getValues() const { return _vertices; } 00080 00082 value_storage_type &getValuesNonConst() { return _vertices; } 00083 00084 protected: 00085 00087 grid_size_type _grid_size; 00089 value_storage_type _vertices; 00090 00091 }; 00092 00093 namespace VertexGridUtility { 00094 template <typename T, unsigned D> 00095 void resizeRecursively( 00096 VecMath::MultiDimensionalVector<T, D> &vec, 00097 const VecMath::Vector<D, unsigned> &size 00098 ); 00099 00100 template <typename T> 00101 void resizeRecursively( 00102 VecMath::MultiDimensionalVector<T, 1> &vec, 00103 const VecMath::Vector<1, unsigned> &size 00104 ); 00105 } 00106 template <unsigned N, unsigned P, typename NUM> 00107 VertexGrid<N, P, NUM>::VertexGrid(): 00108 _grid_size(), _vertices() { } 00109 00113 template <unsigned N, unsigned P, typename NUM> 00114 VertexGrid<N, P, NUM>::VertexGrid( 00115 const VertexGrid::grid_size_type &grid_size): 00116 _grid_size(), _vertices() { 00117 setGridSize(grid_size); 00118 } 00119 00120 template <unsigned N, unsigned P, typename NUM> 00121 VertexGrid<N, P, NUM>::VertexGrid( 00122 const VertexGrid::value_storage_type &values): 00123 _grid_size(), _vertices(values) { 00124 _grid_size = values.size(); 00125 } 00126 00129 template <unsigned N, unsigned P, typename NUM> 00130 void VertexGrid<N, P, NUM>::setGridSize( 00131 const VertexGrid::grid_size_type &grid_size) { 00132 if (grid_size == _grid_size) return; 00133 _grid_size = grid_size; 00134 VertexGridUtility::resizeRecursively(_vertices, _grid_size); 00135 } 00136 00137 namespace VertexGridUtility { 00138 template <unsigned D> 00139 VecMath::Vector<D-1, unsigned> 00140 clipSizeVector(const VecMath::Vector<D, unsigned> &size) { 00141 VecMath::Vector<D-1, unsigned> size_rest; 00142 for (unsigned i = 1; i < D; ++i) size_rest[i] = size[i]; 00143 return size_rest; 00144 } 00145 00146 template <typename T, unsigned D> 00147 void resizeRecursively( 00148 VecMath::MultiDimensionalVector<T, D> &vec, 00149 const VecMath::Vector<D, unsigned> &size 00150 ) { 00151 vec.resize(size[0]); 00152 auto size_rest = clipSizeVector(size); 00153 00154 for (auto it = vec.begin(); it != vec.end(); ++it) { 00155 resizeRecursively(*it, size_rest); 00156 } 00157 } 00158 00159 template <typename T> 00160 void resizeRecursively( 00161 VecMath::MultiDimensionalVector<T, 1> &vec, 00162 const VecMath::Vector<1, unsigned> &size 00163 ) { 00164 vec.resize(size[0]); 00165 } 00166 } 00167 #endif // VertexGrid_H