matrix.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <tuple.h>
  12. class Matrix
  13. {
  14. protected:
  15. /* 4x4 is the default */
  16. double data[4*4];
  17. int size;
  18. public:
  19. Matrix(int size);
  20. Matrix(double values[], int size);
  21. double get(int x, int y) const { return this->data[this->size * x + y]; };
  22. void set(int x, int y, double v) { this->data[this->size * x + y] = v; };
  23. Matrix identity();
  24. Matrix transpose();
  25. double determinant();
  26. Matrix submatrix(int row, int column);
  27. Matrix inverse();
  28. double minor(int row, int column) { return this->submatrix(row, column).determinant(); }
  29. double cofactor(int row, int column) { return (((column+row)&1)?-1:1) * this->minor(row, column); }
  30. bool operator==(const Matrix &b) const;
  31. bool operator!=(const Matrix &b) const;
  32. bool isInvertible() { return this->determinant() != 0; }
  33. Matrix operator*(const Matrix &b) const;
  34. Tuple operator*(const Tuple &b) const;
  35. };
  36. class Matrix4: public Matrix
  37. {
  38. public:
  39. Matrix4() : Matrix(4) { };
  40. Matrix4(double values[]) : Matrix(values, 4) { };
  41. };
  42. class Matrix3 : public Matrix
  43. {
  44. public:
  45. Matrix3() : Matrix(3) { };
  46. Matrix3(double values[]) : Matrix(values, 3) { };
  47. };
  48. class Matrix2 : public Matrix
  49. {
  50. private:
  51. using Matrix::data;
  52. public:
  53. Matrix2() : Matrix(2) { };
  54. Matrix2(double values[]) : Matrix(values, 2) { };
  55. };
  56. #endif /* DORAYME_MATRIX_H */