matrix.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /* Some **** linux distro seems to define "minor" as a macro
  13. * and wreak havoc.
  14. * Let's make sure we are clean here
  15. */
  16. #ifdef minor
  17. #undef minor
  18. #endif
  19. class Matrix
  20. {
  21. protected:
  22. /* 4x4 is the default */
  23. double data[4*4];
  24. int size;
  25. public:
  26. Matrix(int size = 4);
  27. Matrix(double values[], int size);
  28. double get(int x, int y) const { return this->data[this->size * x + y]; };
  29. void set(int x, int y, double v) { this->data[this->size * x + y] = v; };
  30. Matrix identity();
  31. Matrix transpose();
  32. double determinant();
  33. Matrix submatrix(int row, int column);
  34. Matrix inverse();
  35. double minor(int row, int column) { return this->submatrix(row, column).determinant(); }
  36. double cofactor(int row, int column) { return (((column+row)&1)?-1:1) * this->minor(row, column); }
  37. bool operator==(const Matrix &b) const;
  38. bool operator!=(const Matrix &b) const;
  39. bool isInvertible() { return this->determinant() != 0; }
  40. Matrix operator*(const Matrix &b) const;
  41. Tuple operator*(const Tuple &b) const;
  42. };
  43. class Matrix4: public Matrix
  44. {
  45. public:
  46. Matrix4() : Matrix(4) { };
  47. Matrix4(double values[]) : Matrix(values, 4) { };
  48. };
  49. class Matrix3 : public Matrix
  50. {
  51. public:
  52. Matrix3() : Matrix(3) { };
  53. Matrix3(double values[]) : Matrix(values, 3) { };
  54. };
  55. class Matrix2 : public Matrix
  56. {
  57. private:
  58. using Matrix::data;
  59. public:
  60. Matrix2() : Matrix(2) { };
  61. Matrix2(double values[]) : Matrix(values, 2) { };
  62. };
  63. #endif /* DORAYME_MATRIX_H */