BsMatrixNxM.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Math
  7. * @{
  8. */
  9. /**
  10. * Class representing a NxM matrix.
  11. *
  12. * @note If you need to use matrices for more than just data storage then
  13. * it is suggested you use specialized Matrix3 or Matrix4 classes
  14. * as they provide a wide range of functionality.
  15. */
  16. template<int N, int M>
  17. class MatrixNxM
  18. {
  19. public:
  20. MatrixNxM() {}
  21. MatrixNxM(float data[N*M])
  22. {
  23. memcpy(m, data, N*M * sizeof(float));
  24. }
  25. /** Returns a transpose of the matrix (switched columns and rows). */
  26. MatrixNxM<N, M> transpose() const
  27. {
  28. MatrixNxM<N, M> matTranspose;
  29. for (UINT32 row = 0; row < N; row++)
  30. {
  31. for (UINT32 col = 0; col < M; col++)
  32. matTranspose[row][col] = m[col][row];
  33. }
  34. return matTranspose;
  35. }
  36. /** Returns a row of the matrix. */
  37. float* operator[] (UINT32 row) const
  38. {
  39. assert(row < N);
  40. return (float*)m[row];
  41. }
  42. MatrixNxM<N, M>& operator= (const MatrixNxM<N, M>& rhs)
  43. {
  44. memcpy(m, rhs.m, (N*M)*sizeof(float));
  45. return *this;
  46. }
  47. bool operator== (const MatrixNxM<N, M>& rhs) const
  48. {
  49. for (UINT32 row = 0; row < N; row++)
  50. {
  51. for (UINT32 col = 0; col < M; col++)
  52. {
  53. if (m[row][col] != rhs.m[row][col])
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. bool operator!= (const MatrixNxM<N, M>& rhs) const
  60. {
  61. return !operator==(rhs);
  62. }
  63. float m[N][M];
  64. };
  65. typedef MatrixNxM<2, 2> Matrix2;
  66. typedef MatrixNxM<2, 3> Matrix2x3;
  67. typedef MatrixNxM<2, 4> Matrix2x4;
  68. typedef MatrixNxM<3, 2> Matrix3x2;
  69. typedef MatrixNxM<3, 4> Matrix3x4;
  70. typedef MatrixNxM<4, 2> Matrix4x2;
  71. typedef MatrixNxM<4, 3> Matrix4x3;
  72. /** @} */
  73. }