00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020 #ifndef SHLIBMATRIXIMPL_HPP
00021 #define SHLIBMATRIXIMPL_HPP
00022
00023 #include "ShLibMatrix.hpp"
00024
00025 namespace SH {
00026
00027 #define SH_MATRIX_MSVC_OPERATOR_HACK(op, semantic, N, NminusOne) \
00028 template<int M, ShBindingType Binding1, ShBindingType Binding2, typename T1, typename T2, bool swizzled> \
00029 ShAttrib<NminusOne, SH_TEMP, CT1T2, semantic> \
00030 op (const ShMatrix<M, N, Binding1, T1>& a, const ShAttrib<NminusOne, Binding2, T2, semantic, swizzled>& b) \
00031 { \
00032 return op<M, N, Binding1, Binding2, T1, T2, swizzled>(a, b); \
00033 }
00034
00035 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_NORMAL, 4, 3)
00036 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_NORMAL, 3, 2)
00037 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_NORMAL, 4, 3)
00038 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_NORMAL, 3, 2)
00039 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_PLANE, 4, 3)
00040 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_PLANE, 3, 2)
00041 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_PLANE, 4, 3)
00042 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_PLANE, 3, 2)
00043 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_POINT, 4, 3)
00044 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_POINT, 3, 2)
00045 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_POINT, 4, 3)
00046 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_POINT, 3, 2)
00047 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_TEXCOORD, 4, 3)
00048 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_TEXCOORD, 3, 2)
00049 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_TEXCOORD, 4, 3)
00050 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_TEXCOORD, 3, 2)
00051 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_VECTOR, 4, 3)
00052 SH_MATRIX_MSVC_OPERATOR_HACK(operator|, SH_VECTOR, 3, 2)
00053 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_VECTOR, 4, 3)
00054 SH_MATRIX_MSVC_OPERATOR_HACK(operator*, SH_VECTOR, 3, 2)
00055
00056 template<int M, int N, ShBindingType Binding, ShBindingType Binding2, typename T1, typename T2>
00057 ShMatrix<N, M, SH_TEMP, CT1T2>
00058 operator+(const ShMatrix<N, M, Binding, T1>& a,
00059 const ShMatrix<N, M, Binding2, T2>& b)
00060 {
00061 ShMatrix<N, M, SH_TEMP, CT1T2> r(a);
00062 r += b;
00063 return r;
00064 }
00065
00066 template<int M, int N, ShBindingType Binding, ShBindingType Binding2, typename T1, typename T2>
00067 ShMatrix<N, M, SH_TEMP, CT1T2>
00068 operator-(const ShMatrix<N, M, Binding, T1>& a,
00069 const ShMatrix<N, M, Binding2, T2>& b)
00070 {
00071 ShMatrix<N, M, SH_TEMP, CT1T2> r(a);
00072 r -= b;
00073 return r;
00074 }
00075
00076 template<int M, int N, ShBindingType Binding, ShBindingType Binding2, typename T1, typename T2>
00077 ShMatrix<N, M, SH_TEMP, CT1T2>
00078 operator/(const ShMatrix<N, M, Binding, T1>& a,
00079 const ShMatrix<N, M, Binding2, T2>& b)
00080 {
00081 ShMatrix<N, M, SH_TEMP, CT1T2> r(a);
00082 r /= b;
00083 return r;
00084 }
00085
00086 template<int M, int N1, int N2, int P, ShBindingType Binding, ShBindingType Binding2, typename T1, typename T2>
00087 ShMatrix<M, P, SH_TEMP, CT1T2>
00088 operator|(const ShMatrix<M, N1, Binding, T1>& a,
00089 const ShMatrix<N2, P, Binding2, T2>& b)
00090 {
00091 ShMatrix<P, N2, SH_TEMP, T2> tb = transpose(b);
00092 ShMatrix<M, P, SH_TEMP, CT1T2> result;
00093
00094 if (N1 > N2) {
00095
00096 for (int i = 0; i < M; i++) {
00097 for (int j = 0; j < P; j++) {
00098 ShAttrib<1, SH_TEMP, CT1T2> sum = a[i][0] * tb[j][0];
00099 for (int k = 1; k < N2; k++) {
00100 sum += a[i][k] * tb[j][k];
00101 }
00102 for (int k = N2; k < N1; k++) {
00103 if (k == j) sum += a[i][k];
00104 }
00105 result[i][j] = sum;
00106 }
00107 }
00108 } else if (N2 > N1) {
00109
00110 for (int i = 0; i < M; i++) {
00111 for (int j = 0; j < P; j++) {
00112 ShAttrib<1, SH_TEMP, CT1T2> sum = a[i][0] * tb[j][0];
00113 for (int k = 1; k < N1; k++) {
00114 sum += a[i][k] * tb[j][k];
00115 }
00116 for (int k = N1; k < N2; k++) {
00117 if (k == i) sum += tb[j][k];
00118 }
00119 result[i][j] = sum;
00120 }
00121 }
00122 } else {
00123
00124 for (int i = 0; i < M; i++) {
00125 for (int j = 0; j < P; j++) {
00126 result[i][j] = dot(a[i], tb[j]);
00127 }
00128 }
00129 }
00130 return result;
00131 }
00132
00133 template<int M, int N1, int N2, int P, ShBindingType Binding, ShBindingType Binding2, typename T1, typename T2>
00134 inline
00135 ShMatrix<M, P, SH_TEMP, CT1T2>
00136 operator*(const ShMatrix<M, N1, Binding, T1>& a,
00137 const ShMatrix<N2, P, Binding2, T2>& b)
00138 {
00139 return a | b;
00140 }
00141
00142 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00143 ShGeneric<M, CT1T2> operator|(const ShMatrix<M, N, Binding, T1>& a, const ShGeneric<N, T2>& b)
00144 {
00145 ShAttrib<M, SH_TEMP, CT1T2> ret;
00146 for (int i = 0; i < M; i++) {
00147 ret[i] = dot(a[i], b);
00148 }
00149 return ret;
00150 }
00151
00152 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00153 ShGeneric<N, CT1T2> operator|(const ShGeneric<M, T1>& a, const ShMatrix<M, N, Binding, T2>& b)
00154 {
00155 ShAttrib<N, SH_TEMP, CT1T2> ret;
00156 for (int i = 0; i < N; i++) {
00157 ret[i] = 0;
00158 for (int j=0; j < M; j++) {
00159 ret[i] += a[j] * b[j][i];
00160 }
00161 }
00162 return ret;
00163 }
00164
00165 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00166 inline
00167 ShGeneric<N, CT1T2> operator*(const ShGeneric<M, T1>& a, const ShMatrix<M, N, Binding, T2>& b)
00168 {
00169 return a | b;
00170 }
00171
00172 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00173 inline
00174 ShGeneric<M, CT1T2> operator*(const ShMatrix<M, N, Binding, T1>& a, const ShGeneric<N, T2>& b)
00175 {
00176 return a | b;
00177 }
00178
00179 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00180 typename T1, typename T2, bool swizzled>
00181 ShAttrib<N-1, SH_TEMP, CT1T2, SH_VECTOR>
00182 operator|(const ShMatrix<M, N, Binding1, T1>& a,
00183 const ShAttrib<N-1, Binding2, T2, SH_VECTOR, swizzled>& b)
00184 {
00185 ShAttrib<M, SH_TEMP, CT1T2, SH_VECTOR> ret;
00186 for (int i = 0; i < M; i++) {
00187 ret[i] = dot(cast<N-1>(a[i]), b);
00188 }
00189 return cast<N-1>(ret);
00190 }
00191
00192 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00193 typename T1, typename T2, bool swizzled>
00194 ShAttrib<N-1, SH_TEMP, CT1T2, SH_NORMAL>
00195 operator|(const ShMatrix<M, N, Binding1, T1>& a,
00196 const ShAttrib<N-1, Binding2, T2, SH_NORMAL, swizzled>& b)
00197 {
00198 ShAttrib<M, SH_TEMP, CT1T2, SH_NORMAL> ret;
00199 for (int i = 0; i < M; i++) {
00200 ret[i] = dot(cast<N-1>(a[i]), b);
00201 }
00202 return cast<N-1>(ret);
00203 }
00204
00205 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00206 typename T1, typename T2, bool swizzled>
00207 inline
00208 ShAttrib<N-1, SH_TEMP, CT1T2, SH_VECTOR>
00209 operator*(const ShMatrix<M, N, Binding1, T1>& a,
00210 const ShAttrib<N-1, Binding2, T2, SH_VECTOR, swizzled>& b)
00211 {
00212 return a | b;
00213 }
00214
00215 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00216 typename T1, typename T2, bool swizzled>
00217 inline
00218 ShAttrib<N-1, SH_TEMP, CT1T2, SH_NORMAL>
00219 operator*(const ShMatrix<M, N, Binding1, T1>& a,
00220 const ShAttrib<N-1, Binding2, T2, SH_NORMAL, swizzled>& b)
00221 {
00222 return a | b;
00223 }
00224
00225 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00226 typename T1, typename T2, bool swizzled>
00227 ShAttrib<N-1, SH_TEMP, CT1T2, SH_TEXCOORD>
00228 operator|(const ShMatrix<M, N, Binding1, T1>& a,
00229 const ShAttrib<N-1, Binding2, T2, SH_TEXCOORD, swizzled>& b)
00230 {
00231 ShAttrib<N, SH_TEMP, T2, SH_TEXCOORD, swizzled> tmp = cast<N>(b);
00232 tmp[N-1] = static_cast<T2>(1);
00233 return cast<N-1>(a | tmp);
00234 }
00235
00236 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00237 typename T1, typename T2, bool swizzled>
00238 ShAttrib<N-1, SH_TEMP, CT1T2, SH_POINT>
00239 operator|(const ShMatrix<M, N, Binding1, T1>& a,
00240 const ShAttrib<N-1, Binding2, T2, SH_POINT, swizzled>& b)
00241 {
00242 ShAttrib<N, SH_TEMP, T2, SH_POINT, swizzled> tmp = cast<N>(b);
00243 tmp[N-1] = static_cast<T2>(1);
00244 return cast<N-1>(a | tmp);
00245 }
00246
00247 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00248 typename T1, typename T2, bool swizzled>
00249 ShAttrib<N-1, SH_TEMP, CT1T2, SH_PLANE>
00250 operator|(const ShMatrix<M, N, Binding1, T1>& a,
00251 const ShAttrib<N-1, Binding2, T2, SH_PLANE, swizzled>& b)
00252 {
00253 ShAttrib<N, SH_TEMP, T2, SH_PLANE, swizzled> tmp = cast<N>(b);
00254 tmp[N-1] = static_cast<T2>(1);
00255 return cast<N-1>(a | tmp);
00256 }
00257
00258 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00259 typename T1, typename T2, bool swizzled>
00260 inline
00261 ShAttrib<N-1, SH_TEMP, CT1T2, SH_TEXCOORD>
00262 operator*(const ShMatrix<M, N, Binding1, T1>& a,
00263 const ShAttrib<N-1, Binding2, T2, SH_TEXCOORD, swizzled>& b)
00264 {
00265 return a | b;
00266 }
00267
00268 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00269 typename T1, typename T2, bool swizzled>
00270 inline
00271 ShAttrib<N-1, SH_TEMP, CT1T2, SH_POINT>
00272 operator*(const ShMatrix<M, N, Binding1, T1>& a,
00273 const ShAttrib<N-1, Binding2, T2, SH_POINT, swizzled>& b)
00274 {
00275 return a | b;
00276 }
00277
00278 template<int M, int N, ShBindingType Binding1, ShBindingType Binding2,
00279 typename T1, typename T2, bool swizzled>
00280 inline
00281 ShAttrib<N-1, SH_TEMP, CT1T2, SH_PLANE>
00282 operator*(const ShMatrix<M, N, Binding1, T1>& a,
00283 const ShAttrib<N-1, Binding2, T2, SH_PLANE, swizzled>& b)
00284 {
00285 return a | b;
00286 }
00287
00288 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00289 ShMatrix<M, N, SH_TEMP, CT1T2>
00290 operator*(const ShMatrix<M, N, Binding, T1>& a, const ShGeneric<1, T2>& b)
00291 {
00292 ShMatrix<M, N, SH_TEMP, CT1T2> r(a);
00293 r *= b;
00294 return r;
00295 }
00296
00297 template<int M, ShBindingType Binding, typename T1, typename T2>
00298 ShMatrix<M, 1, SH_TEMP, CT1T2>
00299 operator*(const ShMatrix<M, 1, Binding, T1>& a, const ShGeneric<1, T2>& b)
00300 {
00301 ShMatrix<M, 1, SH_TEMP, CT1T2> r(a);
00302 r *= b;
00303 return r;
00304 }
00305
00306 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00307 ShMatrix<M, N, SH_TEMP, CT1T2>
00308 operator*(const ShGeneric<1, T1>& a, const ShMatrix<M, N, Binding, T2>& b)
00309 {
00310 ShMatrix<M, N, SH_TEMP, CT1T2> r(b);
00311 r *= a;
00312 return r;
00313 }
00314
00315 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00316 ShMatrix<1, N, SH_TEMP, CT1T2>
00317 operator*(const ShGeneric<1, T1>& a, const ShMatrix<1, N, Binding, T2>& b)
00318 {
00319 ShMatrix<1, N, SH_TEMP, CT1T2> r(b);
00320 r *= a;
00321 return r;
00322 }
00323
00324 template<int M, int N, ShBindingType Binding, typename T1, typename T2>
00325 ShMatrix<M, N, SH_TEMP, CT1T2>
00326 operator/(const ShMatrix<M, N, Binding, T1>& a, const ShGeneric<1, T2>& b)
00327 {
00328 ShMatrix<M, N, SH_TEMP, CT1T2> r(a);
00329 r /= b;
00330 return r;
00331 }
00332
00333
00334
00335 template<ShBindingType Binding, typename T>
00336 inline
00337 ShAttrib1f
00338 det(const ShMatrix<1, 1, Binding, T>& matrix)
00339 {
00340 return matrix[0][0];
00341 }
00342
00343
00344 template<ShBindingType Binding, typename T>
00345 ShAttrib1f
00346 det(const ShMatrix<2, 2, Binding, T>& matrix)
00347 {
00348 return (matrix[0][0]*matrix[1][1] - matrix[0][1] * matrix[1][0]);
00349 }
00350
00351 template<ShBindingType Binding, typename T>
00352 ShAttrib1f
00353 det(const ShMatrix<3, 3, Binding, T>& matrix)
00354 {
00355 return ((matrix[0] * matrix[1](1,2,0)) | matrix[2](2,0,1)) -
00356 ((matrix[0] * matrix[1](2,0,1)) | matrix[2](1,2,0));
00357 }
00358
00359 template<int RowsCols, ShBindingType Binding, typename T>
00360 ShAttrib1f
00361 det(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix)
00362 {
00363 ShAttrib1f ret = 0.0;
00364 bool flip = (RowsCols % 2 == 0);
00365
00366 for (int i = 0; i < RowsCols; i++) {
00367 if (flip) {
00368 ret -= matrix[RowsCols - 1][i] * det(matrix.subMatrix(RowsCols - 1, i));
00369 } else {
00370 ret += matrix[RowsCols - 1][i] * det(matrix.subMatrix(RowsCols - 1, i));
00371 }
00372 flip = !flip;
00373 }
00374 return ret;
00375 }
00376
00377
00378 template<ShBindingType Binding, typename T>
00379 inline
00380 ShMatrix<1, 1, SH_TEMP, T>
00381 cofactors(const ShMatrix<1, 1, Binding, T>& matrix)
00382 {
00383 return matrix;
00384 }
00385
00386 template<ShBindingType Binding, typename T>
00387 ShMatrix<2, 2, SH_TEMP, T>
00388 cofactors(const ShMatrix<2, 2, Binding, T>& matrix)
00389 {
00390 ShMatrix<2, 2, Binding, T> r;
00391 r[0][0] = matrix[1][1];
00392 r[1][0] = -matrix[0][1];
00393 r[0][1] = -matrix[1][0];
00394 r[1][1] = matrix[0][0];
00395 return r;
00396 }
00397
00398 template<int RowsCols, ShBindingType Binding, typename T>
00399 ShMatrix<RowsCols, RowsCols, SH_TEMP, T>
00400 cofactors(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix)
00401 {
00402 ShMatrix<RowsCols, RowsCols, Binding, T> r;
00403
00404 for (int i = 0; i < RowsCols; i++) {
00405 for (int j = 0; j < RowsCols; j++) {
00406 if((i+j) % 2 == 0) {
00407 r[i][j] = det(matrix.subMatrix(i,j));
00408 } else {
00409 r[i][j] = -det(matrix.subMatrix(i,j));
00410 }
00411 }
00412 }
00413 return r;
00414 }
00415
00416
00417 template<int M, int N, ShBindingType Binding, typename T>
00418 ShMatrix<N, M, SH_TEMP, T>
00419 transpose(const ShMatrix<M, N, Binding, T>& matrix)
00420 {
00421 ShMatrix<N, M, SH_TEMP, T> r;
00422
00423 for (int i = 0; i < N; i++) {
00424 for (int j = 0; j < M; j++) {
00425 r[i][j] = matrix[j][i];
00426 }
00427 }
00428
00429 return r;
00430 }
00431
00432
00433 template<int RowsCols, ShBindingType Binding, typename T>
00434 ShMatrix<RowsCols, RowsCols, SH_TEMP, T>
00435 adjoint(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix)
00436 {
00437 return transpose(cofactors(matrix));
00438 }
00439
00440
00441 template<int RowsCols, ShBindingType Binding, typename T>
00442 ShMatrix<RowsCols, RowsCols, SH_TEMP, T>
00443 inverse(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix)
00444 {
00445 return adjoint(matrix)/det(matrix);
00446 }
00447
00448 template<int RowsCols, ShBindingType Binding, typename T>
00449 ShGeneric<1, T>
00450 trace(const ShMatrix<RowsCols, RowsCols, Binding, T>& matrix)
00451 {
00452 ShAttrib<1, SH_TEMP, T> r(matrix[0][0]);
00453 for (int i=1; i < RowsCols; i++) {
00454 r += matrix[i][i];
00455 }
00456 return r;
00457 }
00458
00459 template<int N, typename T>
00460 ShMatrix<1, N, SH_TEMP, T>
00461 rowmat(const ShGeneric<N, T>& s0)
00462 {
00463 ShMatrix<1, N, SH_TEMP, T> r;
00464 r[0] = s0;
00465 return r;
00466 }
00467
00468 template<int N, typename T>
00469 ShMatrix<2, N, SH_TEMP, T>
00470 rowmat(const ShGeneric<N, T>& s0,
00471 const ShGeneric<N, T>& s1)
00472 {
00473 ShMatrix<2, N, SH_TEMP, T> r;
00474 r[0] = s0;
00475 r[1] = s1;
00476 return r;
00477 }
00478
00479 template<int N, typename T>
00480 ShMatrix<3, N, SH_TEMP, T>
00481 rowmat(const ShGeneric<N, T>& s0,
00482 const ShGeneric<N, T>& s1,
00483 const ShGeneric<N, T>& s2)
00484 {
00485 ShMatrix<3, N, SH_TEMP, T> r;
00486 r[0] = s0;
00487 r[1] = s1;
00488 r[2] = s2;
00489 return r;
00490 }
00491
00492
00493 template<int N, typename T>
00494 ShMatrix<4, N, SH_TEMP, T>
00495 rowmat(const ShGeneric<N, T>& s0,
00496 const ShGeneric<N, T>& s1,
00497 const ShGeneric<N, T>& s2,
00498 const ShGeneric<N, T>& s3)
00499 {
00500 ShMatrix<4, N, SH_TEMP, T> r;
00501 r[0] = s0;
00502 r[1] = s1;
00503 r[2] = s2;
00504 r[3] = s3;
00505 return r;
00506 }
00507
00508
00509 template<int N, typename T>
00510 ShMatrix<N, 1, SH_TEMP, T>
00511 colmat(const ShGeneric<N, T>& s0)
00512 {
00513 ShMatrix<N, 1, SH_TEMP, T> r;
00514 for (int i = 0; i < N; ++i) {
00515 r[i][0] = s0[i];
00516 }
00517 return r;
00518 }
00519
00520 template<int N, typename T>
00521 ShMatrix<N, 2, SH_TEMP, T>
00522 colmat(const ShGeneric<N, T>& s0,
00523 const ShGeneric<N, T>& s1)
00524 {
00525 ShMatrix<N, 2, SH_TEMP, T> r;
00526 for (int i = 0; i < N; ++i) {
00527 r[i][0] = s0[i];
00528 r[i][1] = s1[i];
00529 }
00530 return r;
00531 }
00532
00533 template<int N, typename T>
00534 ShMatrix<N, 3, SH_TEMP, T>
00535 colmat(const ShGeneric<N, T>& s0,
00536 const ShGeneric<N, T>& s1,
00537 const ShGeneric<N, T>& s2)
00538 {
00539 ShMatrix<N, 3, SH_TEMP, T> r;
00540 for (int i = 0; i < N; ++i) {
00541 r[i][0] = s0[i];
00542 r[i][1] = s1[i];
00543 r[i][2] = s2[i];
00544 }
00545 return r;
00546 }
00547
00548
00549 template<int N, typename T>
00550 ShMatrix<N, 4, SH_TEMP, T>
00551 colmat(const ShGeneric<N, T>& s0,
00552 const ShGeneric<N, T>& s1,
00553 const ShGeneric<N, T>& s2,
00554 const ShGeneric<N, T>& s3)
00555 {
00556 ShMatrix<N, 4, SH_TEMP, T> r;
00557 for (int i = 0; i < N; ++i) {
00558 r[i][0] = s0[i];
00559 r[i][1] = s1[i];
00560 r[i][2] = s2[i];
00561 r[i][3] = s3[i];
00562 }
00563 return r;
00564 }
00565
00566
00567 template<int N, typename T>
00568 ShMatrix<N, N, SH_TEMP, T>
00569 diag(const ShGeneric<N, T>& a)
00570 {
00571 ShMatrix<N, N, SH_TEMP, T> r;
00572
00573 for (int i = 0; i < N; ++i) r[i][i] = a[i];
00574 return r;
00575 }
00576
00577 template<typename T>
00578 ShMatrix<4, 4, SH_TEMP, T>
00579 rotate(const ShGeneric<3, T>& axis,
00580 const ShGeneric<1, T>& angle)
00581 {
00582 ShGeneric<1, T> c = cos(angle);
00583 ShGeneric<1, T> s = sin(angle);
00584 ShGeneric<3, T> xyz = normalize(axis);
00585
00586 ShMatrix<4, 4, SH_TEMP, T> result;
00587
00588 result[0](0,1,2) = fillcast<3>((1.0 - c) * xyz(0));
00589 result[1](0,1,2) = fillcast<3>((1.0 - c) * xyz(1));
00590 result[2](0,1,2) = fillcast<3>((1.0 - c) * xyz(2));
00591
00592 result[0](1,2) *= xyz(1,2);
00593 result[0][1] -= xyz(2) * s;
00594 result[0][2] += xyz(1) * s;
00595
00596 result[1](0,2) *= xyz(0,2);
00597 result[1][0] += xyz(2) * s;
00598 result[1][2] -= xyz(0) * s;
00599
00600 result[2](0,1) *= xyz(0,1);
00601 result[2][0] -= xyz(1) * s;
00602 result[2][1] += xyz(0) * s;
00603
00604 result[0][0] *= xyz(0); result[0][0] += c;
00605 result[1][1] *= xyz(1); result[1][1] += c;
00606 result[2][2] *= xyz(2); result[2][2] += c;
00607
00608 return result;
00609 }
00610
00611 template<typename T>
00612 ShMatrix<3, 3, SH_TEMP, T>
00613 rotate(const ShGeneric<1, T>& angle)
00614 {
00615 ShMatrix<3, 3, SH_TEMP, T> result;
00616
00617 ShGeneric<1, T> c = cos(angle);
00618 ShGeneric<1, T> s = sin(angle);
00619 result[0] = ShAttrib<3, SH_TEMP, T>(c, -s, 0.0f);
00620 result[1] = ShAttrib<3, SH_TEMP, T>(c, s, 0.0f);
00621 result[2] = ShAttrib<3, SH_TEMP, T>(0.0f, 0.0f, 1.0f);
00622
00623 return result;
00624 }
00625
00626 template<typename T>
00627 ShMatrix<4, 4, SH_TEMP, T>
00628 translate(const ShGeneric<3, T>& a)
00629 {
00630 ShMatrix<4, 4, SH_TEMP, T> result;
00631
00632 for (int i = 0; i < 3; i++) {
00633 result[i][3] = a[i];
00634 }
00635
00636 return result;
00637 }
00638
00639 template<typename T>
00640 ShMatrix<3, 3, SH_TEMP, T>
00641 translate(const ShGeneric<2, T>& a)
00642 {
00643 ShMatrix<3, 3, SH_TEMP, T> result;
00644
00645 result[0][2] = a[0];
00646 result[1][2] = a[1];
00647
00648 return result;
00649 }
00650
00651
00652 template<typename T>
00653 inline
00654 ShMatrix<4, 4, SH_TEMP, T>
00655 scale(const ShGeneric<3, T>& a)
00656 {
00657 return diag(join(a, ShConstAttrib1f(1.0)));
00658 }
00659
00660 template<typename T>
00661 inline
00662 ShMatrix<3, 3, SH_TEMP, T>
00663 scale(const ShGeneric<2, T>& a)
00664 {
00665 return diag(join(a, ShConstAttrib1f(1.0)));
00666 }
00667
00668 template<int N, typename T>
00669 inline
00670 ShMatrix<N, N, SH_TEMP, T>
00671 scale(const ShGeneric<1, T>& a)
00672 {
00673 return diag(join(fillcast<N - 1>(a), ShConstAttrib1f(1.0)));
00674 }
00675
00676 }
00677
00678 #endif