Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShMatrixImpl.hpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 #ifndef SHMATRIXIMPL_HPP 00028 #define SHMATRIXIMPL_HPP 00029 00030 #include <iostream> 00031 #include <algorithm> 00032 #include "ShMatrix.hpp" 00033 #include "ShUtility.hpp" 00034 00035 namespace SH { 00036 00037 //Constructors, destructors 00038 template<int Rows, int Cols, ShBindingType Binding, typename T> 00039 ShMatrix<Rows, Cols, Binding, T>::ShMatrix() 00040 { 00041 for (int i = 0; i < std::min(Rows, Cols); i++) 00042 m_data[i][i] = 1.0; 00043 } 00044 00045 template<int Rows, int Cols, ShBindingType Binding, typename T> 00046 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding, T>& other) 00047 { 00048 for (int i = 0; i < Rows; i++) 00049 m_data[i] = other[i]; 00050 } 00051 00052 template<int Rows, int Cols, ShBindingType Binding, typename T> 00053 template<ShBindingType Binding2> 00054 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding2, T>& other) 00055 { 00056 for (int i = 0; i < Rows; i++) 00057 m_data[i] = other[i]; 00058 } 00059 00060 template<int Rows, int Cols, ShBindingType Binding, typename T> 00061 ShMatrix<Rows, Cols, Binding, T>::~ShMatrix() 00062 { 00063 } 00064 00065 //Operators 00066 template<int Rows, int Cols, ShBindingType Binding, typename T> 00067 ShMatrix<Rows, Cols, Binding, T>& 00068 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding, T>& other) 00069 { 00070 for (int i = 0; i < Rows; i++) 00071 m_data[i] = other[i]; 00072 return *this; 00073 } 00074 00075 template<int Rows, int Cols, ShBindingType Binding, typename T> 00076 template<ShBindingType Binding2> 00077 ShMatrix<Rows, Cols, Binding, T>& 00078 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding2, T>& other) 00079 { 00080 for (int i = 0; i < Rows; i++) 00081 m_data[i] = other[i]; 00082 return *this; 00083 } 00084 00085 template<int Rows, int Cols, ShBindingType Binding, typename T> 00086 ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i) 00087 { 00088 return m_data[i]; 00089 } 00090 00091 template<int Rows, int Cols, ShBindingType Binding, typename T> 00092 const ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i) const 00093 { 00094 return m_data[i]; 00095 } 00096 00097 template<int Rows, int Cols, ShBindingType Binding, typename T> 00098 template<ShBindingType Binding2> 00099 ShMatrix<Rows, Cols, Binding, T>& 00100 ShMatrix<Rows, Cols, Binding, T>::operator+=(const ShMatrix<Rows, Cols, Binding2, T>& other) 00101 { 00102 for (int i = 0; i < Rows; i++) 00103 m_data[i] += other[i]; 00104 return *this; 00105 } 00106 00107 template<int Rows, int Cols, ShBindingType Binding, typename T> 00108 template<ShBindingType Binding2> 00109 ShMatrix<Rows, Cols, Binding, T>& 00110 ShMatrix<Rows, Cols, Binding, T>::operator-=(const ShMatrix<Rows, Cols, Binding2, T>& other) 00111 { 00112 for (int i = 0; i < Rows; i++) 00113 m_data[i] -= other[i]; 00114 return *this; 00115 } 00116 00117 template<int Rows, int Cols, ShBindingType Binding, typename T> 00118 template<ShBindingType Binding2> 00119 ShMatrix<Rows, Cols, Binding, T>& 00120 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShMatrix<Rows, Cols, Binding2, T>& other) 00121 { 00122 for (int i = 0; i < Rows; i++) 00123 m_data[i] /= other[i]; 00124 return *this; 00125 } 00126 00127 template<int Rows, int Cols, ShBindingType Binding, typename T> 00128 ShMatrix<Rows, Cols, Binding, T>& 00129 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShGeneric<1, T>& other) 00130 { 00131 for (int i = 0; i < Rows; i++) 00132 m_data[i] *= other; 00133 return *this; 00134 } 00135 00136 template<int Rows, int Cols, ShBindingType Binding, typename T> 00137 ShMatrix<Rows, Cols, Binding, T>& 00138 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShGeneric<1, T>& other) 00139 { 00140 for (int i = 0; i < Rows; i++) 00141 m_data[i] /= other; 00142 return *this; 00143 } 00144 00145 template<int Rows, int Cols, ShBindingType Binding, typename T> 00146 ShMatrixRows<Rows, Cols, T> 00147 ShMatrix<Rows, Cols, Binding, T>::operator()() const 00148 { 00149 return ShMatrixRows<Rows, Cols, T>(*this); 00150 } 00151 00152 template<int Rows, int Cols, ShBindingType Binding, typename T> 00153 ShMatrixRows<1, Cols, T> 00154 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0) const 00155 { 00156 return ShMatrixRows<1, Cols, T>(*this, i0); 00157 } 00158 00159 template<int Rows, int Cols, ShBindingType Binding, typename T> 00160 ShMatrixRows<2, Cols, T> 00161 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1) const 00162 { 00163 return ShMatrixRows<2, Cols, T>(*this, i0, i1); 00164 } 00165 00166 template<int Rows, int Cols, ShBindingType Binding, typename T> 00167 ShMatrixRows<3, Cols, T> 00168 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2) const 00169 { 00170 return ShMatrixRows<3, Cols, T>(*this, i0, i1, i2); 00171 } 00172 00173 template<int Rows, int Cols, ShBindingType Binding, typename T> 00174 ShMatrixRows<4, Cols, T> 00175 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2, int i3) const 00176 { 00177 return ShMatrixRows<4, Cols, T>(*this, i0, i1, i2, i3); 00178 } 00179 00180 template<int Rows, int Cols, ShBindingType Binding, typename T> 00181 std::ostream& operator<<(std::ostream& out, 00182 const ShMatrix<Rows, Cols, Binding, T>& m) 00183 { 00184 for (int k = 0; k < Rows; k++) { 00185 out << m[k]; 00186 out << std::endl; 00187 } 00188 00189 return out; 00190 } 00191 00192 00193 template<int Rows, int Cols, ShBindingType Binding, typename T> 00194 ShMatrix<Rows - 1, Cols -1, SH_TEMP, T> 00195 ShMatrix<Rows, Cols, Binding, T>::subMatrix(int rowToRemove, 00196 int columnToRemove) const 00197 { 00198 ShMatrix<Rows - 1, Cols - 1, SH_TEMP, T> myMatrix; 00199 00200 int indices[Cols - 1]; 00201 for (int i = 0; i < columnToRemove; i++) 00202 indices[i] = i; 00203 for (int i = columnToRemove + 1; i < Cols; i++) 00204 indices[i - 1] = i; 00205 00206 for (int i = 0; i < rowToRemove; i++) { 00207 myMatrix[i].clone(m_data[i].template swiz<Cols - 1>(indices)); 00208 } 00209 00210 for (int i = rowToRemove + 1; i < Rows; i++) { 00211 myMatrix[i - 1].clone(m_data[i].template swiz<Cols - 1>(indices)); 00212 } 00213 00214 return myMatrix; 00215 } 00216 00217 template<int Rows,int Cols,ShBindingType Binding, typename T> 00218 void ShMatrix<Rows, Cols, Binding, T>::setTranslation(const ShGeneric<Rows-1, T>& trans){ 00219 00220 for(int i = 0;i<(Rows-1);i++) 00221 m_data[i][(Cols-1)] = trans[i]; 00222 } 00223 00224 00225 template<int Rows,int Cols,ShBindingType Binding, typename T> 00226 void ShMatrix<Rows, Cols, Binding, T>::setScaling(const ShGeneric<Rows-1, T>& scale){ 00227 for(int i = 0;i<(Rows-1);i++) 00228 m_data[i][i] = scale[i]; 00229 } 00230 00232 // ShMatrixRows stuff // 00234 00235 00236 template<int Rows, int Cols, typename T> 00237 template<int OR, ShBindingType Binding> 00238 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source, 00239 int i0) 00240 { 00241 SH_STATIC_CHECK(Rows == 1, Constructing_Non_1_Row_Matrix_From_1_Row); 00242 00243 m_data[0].clone(source[i0]); 00244 } 00245 00246 template<int Rows, int Cols, typename T> 00247 template<int OR, ShBindingType Binding> 00248 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source, 00249 int i0, int i1) 00250 { 00251 SH_STATIC_CHECK(Rows == 2, Constructing_Non_2_Row_Matrix_From_2_Rows); 00252 00253 m_data[0].clone(source[i0]); 00254 m_data[1].clone(source[i1]); 00255 } 00256 00257 template<int Rows, int Cols, typename T> 00258 template<int OR, ShBindingType Binding> 00259 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source, 00260 int i0, int i1, int i2) 00261 { 00262 SH_STATIC_CHECK(Rows == 3, Constructing_Non_3_Row_Matrix_From_3_Rows); 00263 00264 m_data[0].clone(source[i0]); 00265 m_data[1].clone(source[i1]); 00266 m_data[2].clone(source[i2]); 00267 } 00268 00269 template<int Rows, int Cols, typename T> 00270 template<int OR, ShBindingType Binding> 00271 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source, 00272 int i0, int i1, int i2, int i3) 00273 { 00274 SH_STATIC_CHECK(Rows == 4, Constructing_Non_4_Row_Matrix_From_4_Rows); 00275 00276 m_data[0].clone(source[i0]); 00277 m_data[1].clone(source[i1]); 00278 m_data[2].clone(source[i2]); 00279 m_data[3].clone(source[i3]); 00280 } 00281 00282 template<int Rows, int Cols, typename T> 00283 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrixRows<Rows, Cols, T>& other) 00284 { 00285 // TODO: clone? 00286 for (int i = 0; i < Rows; i++) 00287 m_data[i] = other.m_data[i]; 00288 } 00289 00290 template<int Rows, int Cols, typename T> 00291 ShMatrixRows<Rows, Cols, T>& 00292 ShMatrixRows<Rows, Cols, T>::operator=(const ShMatrixRows<Rows, Cols, T>& other) 00293 { 00294 // TODO: clone? 00295 for (int i = 0; i < Rows; i++) 00296 m_data[i] = other.m_data[i]; 00297 } 00298 00299 template<int Rows, int Cols, typename T> 00300 ShMatrix<Rows, Cols, SH_TEMP, T> 00301 ShMatrixRows<Rows, Cols, T>::operator()() const 00302 { 00303 ShMatrix<Rows, Cols, SH_TEMP, T> r; 00304 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i]); 00305 return r; 00306 } 00307 00308 template<int Rows, int Cols, typename T> 00309 ShMatrix<Rows, 1, SH_TEMP, T> 00310 ShMatrixRows<Rows, Cols, T>::operator()(int i0) const 00311 { 00312 ShMatrix<Rows, 1, SH_TEMP, T> r; 00313 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0)); 00314 return r; 00315 } 00316 00317 template<int Rows, int Cols, typename T> 00318 ShMatrix<Rows, 2, SH_TEMP, T> 00319 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1) const 00320 { 00321 ShMatrix<Rows, 2, SH_TEMP, T> r; 00322 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1)); 00323 return r; 00324 } 00325 00326 template<int Rows, int Cols, typename T> 00327 ShMatrix<Rows, 3, SH_TEMP, T> 00328 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2) const 00329 { 00330 ShMatrix<Rows, 3, SH_TEMP, T> r; 00331 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2)); 00332 return r; 00333 } 00334 00335 template<int Rows, int Cols, typename T> 00336 ShMatrix<Rows, 4, SH_TEMP, T> 00337 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2, int i3) const 00338 { 00339 ShMatrix<Rows, 4, SH_TEMP, T> r; 00340 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2, i3)); 00341 return r; 00342 } 00343 00344 template<int Rows, int Cols, ShBindingType Binding, typename T> 00345 void ShMatrix<Rows, Cols, Binding, T>::name(const std::string& name) 00346 { 00347 for (int i = 0; i < Rows; i++) { 00348 std::ostringstream os; 00349 os << name << "_row" << i; 00350 m_data[i].name(os.str()); 00351 m_data[i].meta("matrixname", name); 00352 } 00353 } 00354 00355 template<int Rows, int Cols, ShBindingType Binding, typename T> 00356 std::string ShMatrix<Rows, Cols, Binding, T>::name() const 00357 { 00358 return m_data[0].meta("matrixname"); 00359 } 00360 00361 template<int Rows, int Cols, ShBindingType Binding, typename T> 00362 void ShMatrix<Rows, Cols, Binding, T>::range(T low, T high) 00363 { 00364 for (int i = 0; i < Rows; i++) m_data[i].range(low, high); 00365 } 00366 00367 template<int Rows, int Cols, ShBindingType Binding, typename T> 00368 void ShMatrix<Rows, Cols, Binding, T>::internal(bool setting) 00369 { 00370 for (int i = 0; i < Rows; i++) m_data[i].internal(setting); 00371 } 00372 00373 00374 } 00375 00376 #endif

Generated on Mon Oct 18 14:17:39 2004 for Sh by doxygen 1.3.7