BsMatrixNxM.h 1.7 KB

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