matrix.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Matrix implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <matrix.h>
  10. #include <tuples.h>
  11. #include <math_helper.h>
  12. bool Matrix::operator==(const Matrix &b) const
  13. {
  14. int i;
  15. if (this->width != b.width)
  16. {
  17. /* If they are not the same size don't even bother */
  18. return false;
  19. }
  20. for(i = 0; i < this->width*this->width; i++)
  21. {
  22. if (!double_equal(this->data[i], b.data[i]))
  23. {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. bool Matrix::operator!=(const Matrix &b) const
  30. {
  31. int i;
  32. if (this->width != b.width)
  33. {
  34. /* If they are not the same size don't even bother */
  35. return true;
  36. }
  37. for(i = 0; i < this->width*this->width; i++)
  38. {
  39. if (!double_equal(this->data[i], b.data[i]))
  40. {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. Matrix Matrix::operator*(const Matrix &b) const
  47. {
  48. int x, y, k;
  49. Matrix ret = Matrix(this->width);
  50. if (this->width == b.width)
  51. {
  52. for (y = 0 ; y < this->width ; y++)
  53. {
  54. for (x = 0 ; x < this->width ; x++)
  55. {
  56. double v = 0;
  57. for (k = 0 ; k < this->width ; k++)
  58. {
  59. v += this->get(x, k) * b.get(k, y);
  60. }
  61. ret.set(x, y, v);
  62. }
  63. }
  64. }
  65. return ret;
  66. }
  67. Tuple Matrix::operator*(const Tuple &b) const
  68. {
  69. return Tuple(b.x * this->get(0, 0) + b.y * this->get(0, 1) + b.z * this->get(0, 2) + b.w * this->get(0, 3),
  70. b.x * this->get(1, 0) + b.y * this->get(1, 1) + b.z * this->get(1, 2) + b.w * this->get(1, 3),
  71. b.x * this->get(2, 0) + b.y * this->get(2, 1) + b.z * this->get(2, 2) + b.w * this->get(2, 3),
  72. b.x * this->get(3, 0) + b.y * this->get(3, 1) + b.z * this->get(3, 2) + b.w * this->get(3, 3));
  73. }