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