matrix.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Matrix header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_MATRIX_H
  10. #define DORAYME_MATRIX_H
  11. #include <tuples.h>
  12. class Matrix
  13. {
  14. private:
  15. /* 4x4 is the default */
  16. double data[4*4];
  17. int width;
  18. public:
  19. Matrix(int width) : width(width)
  20. {
  21. int i;
  22. for(i = 0; i < width*width; i++)
  23. {
  24. this->data[i] = 0;
  25. }
  26. };
  27. Matrix(double values[], int width)
  28. {
  29. int x, y;
  30. this->width = width;
  31. for(y = 0; y < this->width; y++)
  32. {
  33. for (x = 0 ; x < this->width ; x++)
  34. {
  35. this->data[this->width * x + y] = values[this->width * x + y];
  36. }
  37. }
  38. };
  39. double get(int x, int y) const { return this->data[this->width * x + y]; };
  40. void set(int x, int y, double v) { this->data[this->width * x + y] = v; };
  41. Matrix identity()
  42. {
  43. int i;
  44. for(i = 0; i < this->width; i++)
  45. {
  46. this->set(i, i, 1);
  47. }
  48. return *this;
  49. }
  50. Matrix transpose()
  51. {
  52. int x, y;
  53. Matrix ret = Matrix(this->width);
  54. for(y = 0; y < this->width; y++)
  55. {
  56. for (x = 0 ; x < this->width ; x++)
  57. {
  58. ret.set(y, x, this->get(x, y));
  59. }
  60. }
  61. return ret;
  62. }
  63. bool operator==(const Matrix &b) const;
  64. bool operator!=(const Matrix &b) const;
  65. Matrix operator*(const Matrix &b) const;
  66. Tuple operator*(const Tuple &b) const;
  67. };
  68. class Matrix4: public Matrix
  69. {
  70. public:
  71. Matrix4() : Matrix(4) { };
  72. Matrix4(double values[]) : Matrix(values, 4) { };
  73. };
  74. class Matrix2 : public Matrix
  75. {
  76. public:
  77. Matrix2() : Matrix(2) { };
  78. Matrix2(double values[]) : Matrix(values, 2) { };
  79. };
  80. class Matrix3 : public Matrix
  81. {
  82. public:
  83. Matrix3() : Matrix(3) { };
  84. Matrix3(double values[]) : Matrix(values, 3) { };
  85. };
  86. #endif /* DORAYME_MATRIX_H */