00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027 #ifndef SHMATRIXIMPL_HPP
00028 #define SHMATRIXIMPL_HPP
00029
00030 #include <iostream>
00031 #include <algorithm>
00032 #include <string>
00033 #include <sstream>
00034 #include "ShMatrix.hpp"
00035 #include "ShUtility.hpp"
00036
00037 namespace SH {
00038
00039
00040 template<int Rows, int Cols, ShBindingType Binding, typename T>
00041 ShMatrix<Rows, Cols, Binding, T>::ShMatrix()
00042 {
00043 for (int i = 0; i < std::min(Rows, Cols); i++)
00044 m_data[i][i] = 1.0;
00045 }
00046
00047 template<int Rows, int Cols, ShBindingType Binding, typename T>
00048 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding, T>& other)
00049 {
00050 for (int i = 0; i < Rows; i++)
00051 m_data[i] = other[i];
00052 }
00053
00054 template<int Rows, int Cols, ShBindingType Binding, typename T>
00055 template<ShBindingType Binding2>
00056 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding2, T>& other)
00057 {
00058 for (int i = 0; i < Rows; i++)
00059 m_data[i] = other[i];
00060 }
00061
00062 template<int Rows, int Cols, ShBindingType Binding, typename T>
00063 ShMatrix<Rows, Cols, Binding, T>::~ShMatrix()
00064 {
00065 }
00066
00067
00068 template<int Rows, int Cols, ShBindingType Binding, typename T>
00069 ShMatrix<Rows, Cols, Binding, T>&
00070 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding, T>& other)
00071 {
00072 for (int i = 0; i < Rows; i++)
00073 m_data[i] = other[i];
00074 return *this;
00075 }
00076
00077 template<int Rows, int Cols, ShBindingType Binding, typename T>
00078 template<ShBindingType Binding2>
00079 ShMatrix<Rows, Cols, Binding, T>&
00080 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00081 {
00082 for (int i = 0; i < Rows; i++)
00083 m_data[i] = other[i];
00084 return *this;
00085 }
00086
00087 template<int Rows, int Cols, ShBindingType Binding, typename T>
00088 ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i)
00089 {
00090 return m_data[i];
00091 }
00092
00093 template<int Rows, int Cols, ShBindingType Binding, typename T>
00094 const ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i) const
00095 {
00096 return m_data[i];
00097 }
00098
00099 template<int Rows, int Cols, ShBindingType Binding, typename T>
00100 template<ShBindingType Binding2>
00101 ShMatrix<Rows, Cols, Binding, T>&
00102 ShMatrix<Rows, Cols, Binding, T>::operator+=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00103 {
00104 for (int i = 0; i < Rows; i++)
00105 m_data[i] += other[i];
00106 return *this;
00107 }
00108
00109 template<int Rows, int Cols, ShBindingType Binding, typename T>
00110 template<ShBindingType Binding2>
00111 ShMatrix<Rows, Cols, Binding, T>&
00112 ShMatrix<Rows, Cols, Binding, T>::operator-=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00113 {
00114 for (int i = 0; i < Rows; i++)
00115 m_data[i] -= other[i];
00116 return *this;
00117 }
00118
00119 template<int Rows, int Cols, ShBindingType Binding, typename T>
00120 template<ShBindingType Binding2>
00121 ShMatrix<Rows, Cols, Binding, T>&
00122 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00123 {
00124 for (int i = 0; i < Rows; i++)
00125 m_data[i] /= other[i];
00126 return *this;
00127 }
00128
00129 template<int Rows, int Cols, ShBindingType Binding, typename T>
00130 ShMatrix<Rows, Cols, Binding, T>&
00131 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShGeneric<1, T>& other)
00132 {
00133 for (int i = 0; i < Rows; i++)
00134 m_data[i] *= other;
00135 return *this;
00136 }
00137
00138 template<int Rows, int Cols, ShBindingType Binding, typename T>
00139 ShMatrix<Rows, Cols, Binding, T>&
00140 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShGeneric<1, T>& other)
00141 {
00142 for (int i = 0; i < Rows; i++)
00143 m_data[i] /= other;
00144 return *this;
00145 }
00146
00147 template<int Rows, int Cols, ShBindingType Binding, typename T>
00148 ShMatrixRows<Rows, Cols, T>
00149 ShMatrix<Rows, Cols, Binding, T>::operator()() const
00150 {
00151 return ShMatrixRows<Rows, Cols, T>(*this);
00152 }
00153
00154 template<int Rows, int Cols, ShBindingType Binding, typename T>
00155 ShMatrixRows<1, Cols, T>
00156 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0) const
00157 {
00158 return ShMatrixRows<1, Cols, T>(*this, i0);
00159 }
00160
00161 template<int Rows, int Cols, ShBindingType Binding, typename T>
00162 ShMatrixRows<2, Cols, T>
00163 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1) const
00164 {
00165 return ShMatrixRows<2, Cols, T>(*this, i0, i1);
00166 }
00167
00168 template<int Rows, int Cols, ShBindingType Binding, typename T>
00169 ShMatrixRows<3, Cols, T>
00170 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2) const
00171 {
00172 return ShMatrixRows<3, Cols, T>(*this, i0, i1, i2);
00173 }
00174
00175 template<int Rows, int Cols, ShBindingType Binding, typename T>
00176 ShMatrixRows<4, Cols, T>
00177 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2, int i3) const
00178 {
00179 return ShMatrixRows<4, Cols, T>(*this, i0, i1, i2, i3);
00180 }
00181
00182 template<int Rows, int Cols, ShBindingType Binding, typename T>
00183 std::ostream& operator<<(std::ostream& out,
00184 const ShMatrix<Rows, Cols, Binding, T>& m)
00185 {
00186 for (int k = 0; k < Rows; k++) {
00187 out << m[k];
00188 out << std::endl;
00189 }
00190
00191 return out;
00192 }
00193
00194
00195 template<int Rows, int Cols, ShBindingType Binding, typename T>
00196 ShMatrix<Rows - 1, Cols -1, SH_TEMP, T>
00197 ShMatrix<Rows, Cols, Binding, T>::subMatrix(int rowToRemove,
00198 int columnToRemove) const
00199 {
00200 ShMatrix<Rows - 1, Cols - 1, SH_TEMP, T> myMatrix;
00201
00202 int indices[Cols - 1];
00203 for (int i = 0; i < columnToRemove; i++)
00204 indices[i] = i;
00205 for (int i = columnToRemove + 1; i < Cols; i++)
00206 indices[i - 1] = i;
00207
00208 for (int i = 0; i < rowToRemove; i++) {
00209 myMatrix[i].clone(m_data[i].template swiz<Cols - 1>(indices));
00210 }
00211
00212 for (int i = rowToRemove + 1; i < Rows; i++) {
00213 myMatrix[i - 1].clone(m_data[i].template swiz<Cols - 1>(indices));
00214 }
00215
00216 return myMatrix;
00217 }
00218
00219 template<int Rows,int Cols,ShBindingType Binding, typename T>
00220 void ShMatrix<Rows, Cols, Binding, T>::setTranslation(const ShGeneric<Rows-1, T>& trans){
00221
00222 for(int i = 0;i<(Rows-1);i++)
00223 m_data[i][(Cols-1)] = trans[i];
00224 }
00225
00226
00227 template<int Rows,int Cols,ShBindingType Binding, typename T>
00228 void ShMatrix<Rows, Cols, Binding, T>::setScaling(const ShGeneric<Rows-1, T>& scale){
00229 for(int i = 0;i<(Rows-1);i++)
00230 m_data[i][i] = scale[i];
00231 }
00232
00234
00236
00237
00238 template<int Rows, int Cols, typename T>
00239 template<int OR, ShBindingType Binding>
00240 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00241 int i0)
00242 {
00243 SH_STATIC_CHECK(Rows == 1, Constructing_Non_1_Row_Matrix_From_1_Row);
00244
00245 m_data[0].clone(source[i0]);
00246 }
00247
00248 template<int Rows, int Cols, typename T>
00249 template<int OR, ShBindingType Binding>
00250 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00251 int i0, int i1)
00252 {
00253 SH_STATIC_CHECK(Rows == 2, Constructing_Non_2_Row_Matrix_From_2_Rows);
00254
00255 m_data[0].clone(source[i0]);
00256 m_data[1].clone(source[i1]);
00257 }
00258
00259 template<int Rows, int Cols, typename T>
00260 template<int OR, ShBindingType Binding>
00261 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00262 int i0, int i1, int i2)
00263 {
00264 SH_STATIC_CHECK(Rows == 3, Constructing_Non_3_Row_Matrix_From_3_Rows);
00265
00266 m_data[0].clone(source[i0]);
00267 m_data[1].clone(source[i1]);
00268 m_data[2].clone(source[i2]);
00269 }
00270
00271 template<int Rows, int Cols, typename T>
00272 template<int OR, ShBindingType Binding>
00273 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00274 int i0, int i1, int i2, int i3)
00275 {
00276 SH_STATIC_CHECK(Rows == 4, Constructing_Non_4_Row_Matrix_From_4_Rows);
00277
00278 m_data[0].clone(source[i0]);
00279 m_data[1].clone(source[i1]);
00280 m_data[2].clone(source[i2]);
00281 m_data[3].clone(source[i3]);
00282 }
00283
00284 template<int Rows, int Cols, typename T>
00285 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrixRows<Rows, Cols, T>& other)
00286 {
00287
00288 for (int i = 0; i < Rows; i++)
00289 m_data[i] = other.m_data[i];
00290 }
00291
00292 template<int Rows, int Cols, typename T>
00293 ShMatrixRows<Rows, Cols, T>&
00294 ShMatrixRows<Rows, Cols, T>::operator=(const ShMatrixRows<Rows, Cols, T>& other)
00295 {
00296
00297 for (int i = 0; i < Rows; i++)
00298 m_data[i] = other.m_data[i];
00299 }
00300
00301 template<int Rows, int Cols, typename T>
00302 ShMatrix<Rows, Cols, SH_TEMP, T>
00303 ShMatrixRows<Rows, Cols, T>::operator()() const
00304 {
00305 ShMatrix<Rows, Cols, SH_TEMP, T> r;
00306 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i]);
00307 return r;
00308 }
00309
00310 template<int Rows, int Cols, typename T>
00311 ShMatrix<Rows, 1, SH_TEMP, T>
00312 ShMatrixRows<Rows, Cols, T>::operator()(int i0) const
00313 {
00314 ShMatrix<Rows, 1, SH_TEMP, T> r;
00315 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0));
00316 return r;
00317 }
00318
00319 template<int Rows, int Cols, typename T>
00320 ShMatrix<Rows, 2, SH_TEMP, T>
00321 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1) const
00322 {
00323 ShMatrix<Rows, 2, SH_TEMP, T> r;
00324 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1));
00325 return r;
00326 }
00327
00328 template<int Rows, int Cols, typename T>
00329 ShMatrix<Rows, 3, SH_TEMP, T>
00330 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2) const
00331 {
00332 ShMatrix<Rows, 3, SH_TEMP, T> r;
00333 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2));
00334 return r;
00335 }
00336
00337 template<int Rows, int Cols, typename T>
00338 ShMatrix<Rows, 4, SH_TEMP, T>
00339 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2, int i3) const
00340 {
00341 ShMatrix<Rows, 4, SH_TEMP, T> r;
00342 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2, i3));
00343 return r;
00344 }
00345
00346 template<int Rows, int Cols, ShBindingType Binding, typename T>
00347 void ShMatrix<Rows, Cols, Binding, T>::range(H low, H high)
00348 {
00349 for (int i = 0; i < Rows; i++) m_data[i].range(low, high);
00350 }
00351
00352 template<int Rows, int Cols, ShBindingType Binding, typename T>
00353 std::string ShMatrix<Rows, Cols, Binding, T>::name() const
00354 {
00355 return ShMeta::name();
00356 }
00357
00358 template<int Rows, int Cols, ShBindingType Binding, typename T>
00359 void ShMatrix<Rows, Cols, Binding, T>::name(const std::string& n)
00360 {
00361 ShMeta::name(n);
00362
00363 for (int i = 0; i < Rows; i++)
00364 {
00365 std::stringstream s;
00366 s << n << ".row[" << i << "]";
00367 m_data[i].name(s.str());
00368 }
00369 }
00370
00371 template<int Rows, int Cols, ShBindingType Binding, typename T>
00372 bool ShMatrix<Rows, Cols, Binding, T>:: has_name() const
00373 {
00374 return ShMeta::has_name();
00375 }
00376
00377 template<int Rows, int Cols, ShBindingType Binding, typename T>
00378 bool ShMatrix<Rows, Cols, Binding, T>::internal() const
00379 {
00380 return ShMeta::internal();
00381 }
00382
00383 template<int Rows, int Cols, ShBindingType Binding, typename T>
00384 void ShMatrix<Rows, Cols, Binding, T>::internal(bool internal)
00385 {
00386 ShMeta::internal(internal);
00387
00388 for (int i = 0; i < Rows; i++)
00389 {
00390 m_data[i].internal(internal);
00391 }
00392 }
00393
00394 template<int Rows, int Cols, ShBindingType Binding, typename T>
00395 std::string ShMatrix<Rows, Cols, Binding, T>::title() const
00396 {
00397 return ShMeta::title();
00398 }
00399
00400 template<int Rows, int Cols, ShBindingType Binding, typename T>
00401 void ShMatrix<Rows, Cols, Binding, T>::title(const std::string& t)
00402 {
00403 ShMeta::title(t);
00404
00405 for (int i = 0; i < Rows; i++)
00406 {
00407 std::stringstream s;
00408 s << t << ".row[" << i << "]";
00409 m_data[i].title(s.str());
00410 }
00411 }
00412
00413 template<int Rows, int Cols, ShBindingType Binding, typename T>
00414 std::string ShMatrix<Rows, Cols, Binding, T>::description() const
00415 {
00416 return ShMeta::description();
00417 }
00418
00419 template<int Rows, int Cols, ShBindingType Binding, typename T>
00420 void ShMatrix<Rows, Cols, Binding, T>::description(const std::string& d)
00421 {
00422 ShMeta::description(d);
00423
00424 for (int i = 0; i < Rows; i++)
00425 {
00426 std::stringstream s;
00427 s << d << ".row[" << i << "]";
00428 m_data[i].description(s.str());
00429 }
00430 }
00431
00432 template<int Rows, int Cols, ShBindingType Binding, typename T>
00433 std::string ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key) const
00434 {
00435 return ShMeta::meta(key);
00436 }
00437
00438 template<int Rows, int Cols, ShBindingType Binding, typename T>
00439 void ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key, const std::string& value)
00440 {
00441 ShMeta::meta(key, value);
00442
00443 for (int i = 0; i < Rows; i++)
00444 {
00445 std::stringstream s;
00446 s << value << ".row[" << i << "]";
00447 m_data[i].meta(key, s.str());
00448 }
00449 }
00450
00451
00452 }
00453
00454 #endif