BsMatrixNxM.h 1.9 KB

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