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

ShLibMatrixImpl.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 SHLIBMATRIXIMPL_HPP 00028 #define SHLIBMATRIXIMPL_HPP 00029 00030 #include "ShLibMatrix.hpp" 00031 00032 namespace SH { 00033 00034 template<int M, int N, int P, ShBindingType Binding, ShBindingType Binding2, typename T> 00035 inline 00036 ShMatrix<M, P, SH_TEMP, T> 00037 operator|(const ShMatrix<M, N, Binding, T>& a, 00038 const ShMatrix<N, P, Binding2, T>& b) 00039 { 00040 ShMatrix<P, N, SH_TEMP, T> tb = transpose(b); 00041 00042 ShMatrix<M, P, SH_TEMP, T> result; 00043 for (int i = 0; i < M; i++) { 00044 for (int j = 0; j < P; j++) { 00045 result[i][j] = dot(a[i], tb[j]); 00046 } 00047 } 00048 return result; 00049 } 00050 00051 template<int M, int N, int P, ShBindingType Binding, ShBindingType Binding2, typename T> 00052 inline 00053 ShMatrix<M, P, SH_TEMP, T> 00054 operator*(const ShMatrix<M, N, Binding, T>& a, 00055 const ShMatrix<N, P, Binding2, T>& b) 00056 { 00057 return a | b; 00058 } 00059 00060 template<int M, int N, ShBindingType Binding, typename T> 00061 inline 00062 ShGeneric<M, T> operator|(const ShMatrix<M, N, Binding, T>& a, const ShGeneric<N, T>& b) 00063 { 00064 ShAttrib<M, SH_TEMP, T> ret; 00065 for (int i = 0; i < M; i++) { 00066 ret[i] = dot(a[i], b); 00067 } 00068 return ret; 00069 } 00070 00071 template<int M, int N, ShBindingType Binding, typename T> 00072 inline 00073 ShGeneric<N, T> operator|(const ShGeneric<M, T>& a, const ShMatrix<M, N, Binding, T>& b) 00074 { 00075 ShAttrib<N, SH_TEMP, T> ret; 00076 for (int i = 0; i < N; i++) { 00077 ret[i] = dot(a, b()(i)); 00078 } 00079 return ret; 00080 } 00081 00082 template<int M, int N, ShBindingType Binding, typename T> 00083 inline 00084 ShGeneric<N, T> operator*(const ShGeneric<M, T>& a, const ShMatrix<M, N, Binding, T>& b) 00085 { 00086 return a | b; 00087 } 00088 00089 template<int M, int N, ShBindingType Binding, typename T> 00090 inline 00091 ShGeneric<M, T> operator*(const ShMatrix<M, N, Binding, T>& a, const ShGeneric<N, T>& b) 00092 { 00093 return a | b; 00094 } 00095 00096 template<int M, int N, ShBindingType Binding, typename T> 00097 ShMatrix<M, N, SH_TEMP, T> 00098 operator*(const ShMatrix<M, N, Binding, T>& a, const ShGeneric<1, T>& b) 00099 { 00100 ShMatrix<M, N, SH_TEMP, T> r(a); 00101 r *= b; 00102 return r; 00103 } 00104 00105 template<int M, ShBindingType Binding, typename T> 00106 ShMatrix<M, 1, SH_TEMP, T> 00107 operator*(const ShMatrix<M, 1, Binding, T>& a, const ShGeneric<1, T>& b) 00108 { 00109 ShMatrix<M, 1, SH_TEMP, T> r(a); 00110 r *= b; 00111 return r; 00112 } 00113 00114 template<int M, int N, ShBindingType Binding, typename T> 00115 ShMatrix<M, N, SH_TEMP, T> 00116 operator*(const ShGeneric<1, T>& a, const ShMatrix<M, N, Binding, T>& b) 00117 { 00118 ShMatrix<M, N, SH_TEMP, T> r(b); 00119 r *= a; 00120 return r; 00121 } 00122 00123 template<int M, int N, ShBindingType Binding, typename T> 00124 ShMatrix<1, N, SH_TEMP, T> 00125 operator*(const ShGeneric<1, T>& a, const ShMatrix<1, N, Binding, T>& b) 00126 { 00127 ShMatrix<1, N, SH_TEMP, T> r(b); 00128 r *= a; 00129 return r; 00130 } 00131 00132 template<int M, int N, ShBindingType Binding, typename T> 00133 ShMatrix<M, N, SH_TEMP, T> 00134 operator/(const ShMatrix<M, N, Binding, T>& a, const ShGeneric<1, T>& b) 00135 { 00136 ShMatrix<M, N, SH_TEMP, T> r(a); 00137 r /= b; 00138 return r; 00139 } 00140 00141 00142 00143 template<ShBindingType Binding, typename T> 00144 ShAttrib1f 00145 det(const ShMatrix<1, 1, Binding, T>& matrix) 00146 { 00147 return matrix[0][0]; 00148 } 00149 00150 00151 template<ShBindingType Binding, typename T> 00152 ShAttrib1f 00153 det(const ShMatrix<2, 2, Binding, T>& matrix) 00154 { 00155 return (matrix[0][0]*matrix[1][1] - matrix[0][1] * matrix[1][0]); 00156 } 00157 00158 template<int RowsCols, ShBindingType Binding, typename T> 00159 ShAttrib1f 00160 det(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix) 00161 { 00162 ShAttrib1f ret = 0.0; 00163 bool flip = (RowsCols % 2 == 0); 00164 00165 for (int i = 0; i < RowsCols; i++) { 00166 if (flip) { 00167 ret -= matrix[RowsCols - 1][i] * det(matrix.subMatrix(RowsCols - 1, i)); 00168 } else { 00169 ret += matrix[RowsCols - 1][i] * det(matrix.subMatrix(RowsCols - 1, i)); 00170 } 00171 flip = !flip; 00172 } 00173 return ret; 00174 } 00175 00176 //Matrix of Cofactors 00177 template<ShBindingType Binding, typename T> 00178 ShMatrix<1,1,SH_TEMP,T> 00179 cofactors(const ShMatrix<1, 1, Binding, T>& matrix){ 00180 return matrix; 00181 } 00182 00183 template<ShBindingType Binding, typename T> 00184 ShMatrix<2,2,SH_TEMP,T> 00185 cofactors(const ShMatrix<2, 2, Binding, T>& matrix) 00186 { 00187 ShMatrix<2,2,Binding,T> r; 00188 r.m_data[0][0]= matrix[1][1]; 00189 r.m_data[1][0]=-matrix[0][1]; 00190 r.m_data[0][1]=-matrix[1][0]; 00191 r.m_data[1][1]= matrix[0][0]; 00192 00193 return r; 00194 //return matrix; 00195 } 00196 00197 template<int RowsCols,ShBindingType Binding, typename T> 00198 ShMatrix<RowsCols,RowsCols, SH_TEMP, T> 00199 cofactors(const ShMatrix<RowsCols,RowsCols, Binding, T>& matrix) 00200 { 00201 ShMatrix<RowsCols,RowsCols,Binding,T> r; 00202 00203 for (int i = 0; i < RowsCols; i++) { 00204 for (int j = 0; j < RowsCols; j++) { 00205 if( (i+j)%2 ==0) 00206 r[i][j]= det(matrix.subMatrix(i,j)); 00207 else 00208 r[i][j]=-det(matrix.subMatrix(i,j)); 00209 } 00210 } 00211 return r; 00212 } 00213 00214 //Transpose 00215 template<int M, int N, ShBindingType Binding, typename T> 00216 ShMatrix<N, M, SH_TEMP, T> 00217 transpose(const ShMatrix<M, N, Binding, T>& matrix) 00218 { 00219 ShMatrix<N, M, SH_TEMP, T> r; 00220 00221 for (int i = 0; i < N; i++) { 00222 for (int j = 0; j < M; j++) { 00223 r[i][j]= matrix[j][i]; 00224 } 00225 } 00226 00227 return r; 00228 } 00229 00230 // Adjoint 00231 template<int RowsCols, ShBindingType Binding, typename T> 00232 ShMatrix<RowsCols, RowsCols, SH_TEMP, T> 00233 adjoint(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix) 00234 { 00235 return transpose(cofactors(matrix)); 00236 } 00237 00238 //Inverse 00239 template<int RowsCols, ShBindingType Binding, typename T> 00240 ShMatrix<RowsCols,RowsCols, SH_TEMP, T> 00241 inverse(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix) 00242 { 00243 return adjoint(matrix)/det(matrix); 00244 } 00245 00246 template<int N, typename T> 00247 ShMatrix<1, N, SH_TEMP, T> 00248 rowmat(const ShGeneric<N, T>& s0) 00249 { 00250 ShMatrix<1, N, SH_TEMP, T> r; 00251 r[0] = s0; 00252 return r; 00253 } 00254 00255 template<int N, typename T> 00256 ShMatrix<2, N, SH_TEMP, T> 00257 rowmat(const ShGeneric<N, T>& s0, 00258 const ShGeneric<N, T>& s1) 00259 { 00260 ShMatrix<2, N, SH_TEMP, T> r; 00261 r[0] = s0; 00262 r[1] = s1; 00263 return r; 00264 } 00265 00266 template<int N, typename T> 00267 ShMatrix<3, N, SH_TEMP, T> 00268 rowmat(const ShGeneric<N, T>& s0, 00269 const ShGeneric<N, T>& s1, 00270 const ShGeneric<N, T>& s2) 00271 { 00272 ShMatrix<3, N, SH_TEMP, T> r; 00273 r[0] = s0; 00274 r[1] = s1; 00275 r[2] = s2; 00276 return r; 00277 } 00278 00279 00280 template<int N, typename T> 00281 ShMatrix<4, N, SH_TEMP, T> 00282 rowmat(const ShGeneric<N, T>& s0, 00283 const ShGeneric<N, T>& s1, 00284 const ShGeneric<N, T>& s2, 00285 const ShGeneric<N, T>& s3) 00286 { 00287 ShMatrix<4, N, SH_TEMP, T> r; 00288 r[0] = s0; 00289 r[1] = s1; 00290 r[2] = s2; 00291 r[3] = s3; 00292 return r; 00293 } 00294 00295 00296 template<int N, typename T> 00297 ShMatrix<N, 1, SH_TEMP, T> 00298 colmat(const ShGeneric<N, T>& s0) 00299 { 00300 ShMatrix<N, 1, SH_TEMP, T> r; 00301 for (int i = 0; i < N; ++i) { 00302 r[i][0] = s0[i]; 00303 } 00304 return r; 00305 } 00306 00307 template<int N, typename T> 00308 ShMatrix<N, 2, SH_TEMP, T> 00309 colmat(const ShGeneric<N, T>& s0, 00310 const ShGeneric<N, T>& s1) 00311 { 00312 ShMatrix<N, 2, SH_TEMP, T> r; 00313 for (int i = 0; i < N; ++i) { 00314 r[i][0] = s0[i]; 00315 r[i][1] = s1[i]; 00316 } 00317 return r; 00318 } 00319 00320 template<int N, typename T> 00321 ShMatrix<N, 3, SH_TEMP, T> 00322 colmat(const ShGeneric<N, T>& s0, 00323 const ShGeneric<N, T>& s1, 00324 const ShGeneric<N, T>& s2) 00325 { 00326 ShMatrix<N, 3, SH_TEMP, T> r; 00327 for (int i = 0; i < N; ++i) { 00328 r[i][0] = s0[i]; 00329 r[i][1] = s1[i]; 00330 r[i][2] = s2[i]; 00331 } 00332 return r; 00333 } 00334 00335 00336 template<int N, typename T> 00337 ShMatrix<N, 4, SH_TEMP, T> 00338 colmat(const ShGeneric<N, T>& s0, 00339 const ShGeneric<N, T>& s1, 00340 const ShGeneric<N, T>& s2, 00341 const ShGeneric<N, T>& s3) 00342 { 00343 ShMatrix<N, 4, SH_TEMP, T> r; 00344 for (int i = 0; i < N; ++i) { 00345 r[i][0] = s0[i]; 00346 r[i][1] = s1[i]; 00347 r[i][2] = s2[i]; 00348 r[i][3] = s3[i]; 00349 } 00350 return r; 00351 } 00352 00353 00354 template<int N, typename T> 00355 ShMatrix<N, N, SH_TEMP, T> 00356 diag(const ShGeneric<N, T>& a) 00357 { 00358 ShMatrix<N, N, SH_TEMP, T> r; 00359 00360 for (int i = 0; i < N; ++i) r[i][i] = a[i]; 00361 return r; 00362 } 00363 00364 template<typename T> 00365 ShMatrix<4, 4, SH_TEMP, T> 00366 rotate(const ShGeneric<3, T>& axis, 00367 const ShGeneric<1, T>& angle) 00368 { 00369 ShGeneric<1, T> c = cos(angle); 00370 ShGeneric<1, T> s = sin(angle); 00371 ShGeneric<3, T> xyz = normalize(axis); 00372 00373 ShMatrix<4, 4, SH_TEMP, T> result; 00374 00375 result[0](0,1,2) = fillcast<3>((1.0 - c) * xyz(0)); 00376 result[1](0,1,2) = fillcast<3>((1.0 - c) * xyz(1)); 00377 result[2](0,1,2) = fillcast<3>((1.0 - c) * xyz(2)); 00378 00379 result[0](1,2) *= xyz(1,2); 00380 result[0][1] -= xyz(2) * s; 00381 result[0][2] += xyz(1) * s; 00382 00383 result[1](0,2) *= xyz(0,2); 00384 result[1][0] += xyz(2) * s; 00385 result[1][2] -= xyz(0) * s; 00386 00387 result[2](0,1) *= xyz(0,1); 00388 result[2][0] -= xyz(1) * s; 00389 result[2][1] += xyz(0) * s; 00390 00391 result[0][0] *= 2.0; result[0][0] += c; 00392 result[1][1] *= 2.0; result[1][1] += c; 00393 result[2][2] *= 2.0; result[2][2] += c; 00394 00395 return result; 00396 } 00397 00398 template<typename T> 00399 ShMatrix<3, 3, SH_TEMP, T> 00400 rotate(const ShGeneric<1, T>& angle) 00401 { 00402 ShMatrix<3, 3, SH_TEMP, T> result; 00403 00404 ShGeneric<1, T> c = cos(angle); 00405 ShGeneric<1, T> s = sin(angle); 00406 result[0] = ShAttrib<3, SH_TEMP, T>(c, -s, 0.0f); 00407 result[1] = ShAttrib<3, SH_TEMP, T>(c, s, 0.0f); 00408 result[2] = ShAttrib<3, SH_TEMP, T>(0.0f, 0.0f, 1.0f); 00409 00410 return result; 00411 } 00412 00413 template<typename T> 00414 ShMatrix<4, 4, SH_TEMP, T> 00415 translate(const ShGeneric<3, T>& a) 00416 { 00417 ShMatrix<4, 4, SH_TEMP, T> result; 00418 00419 for (int i = 0; i < 3; i++) { 00420 result[i][3] = a[i]; 00421 } 00422 00423 return result; 00424 } 00425 00426 template<typename T> 00427 ShMatrix<3, 3, SH_TEMP, T> 00428 translate(const ShGeneric<2, T>& a) 00429 { 00430 ShMatrix<3, 3, SH_TEMP, T> result; 00431 00432 result[0][2] = a[0]; 00433 result[1][2] = a[1]; 00434 00435 return result; 00436 } 00437 00438 00439 template<typename T> 00440 ShMatrix<4, 4, SH_TEMP, T> 00441 scale(const ShGeneric<3, T>& a) 00442 { 00443 return diag(join(a, ShConstAttrib1f(1.0))); 00444 } 00445 00446 template<typename T> 00447 ShMatrix<3, 3, SH_TEMP, T> 00448 scale(const ShGeneric<2, T>& a) 00449 { 00450 return diag(join(a, ShConstAttrib1f(1.0))); 00451 } 00452 00453 template<int N, typename T> 00454 ShMatrix<N, N, SH_TEMP, T> 00455 scale(const ShGeneric<1, T>& a) 00456 { 00457 return diag(join(fillcast<N - 1>(a), ShConstAttrib1f(1.0))); 00458 } 00459 00460 00461 } 00462 00463 #endif

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