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