matrix.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. Matrix::Matrix(int width)
  13. {
  14. int i;
  15. this->width = width;
  16. for(i = 0; i < width*width; i++)
  17. {
  18. this->data[i] = 0;
  19. }
  20. };
  21. Matrix::Matrix(double values[], int width)
  22. {
  23. int x, y;
  24. this->width = width;
  25. for(y = 0; y < this->width; y++)
  26. {
  27. for (x = 0 ; x < this->width ; x++)
  28. {
  29. this->data[this->width * x + y] = values[this->width * x + y];
  30. }
  31. }
  32. };
  33. bool Matrix::operator==(const Matrix &b) const
  34. {
  35. int i;
  36. if (this->width != b.width)
  37. {
  38. /* If they are not the same size don't even bother */
  39. return false;
  40. }
  41. for(i = 0; i < this->width*this->width; i++)
  42. {
  43. if (!double_equal(this->data[i], b.data[i]))
  44. {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. bool Matrix::operator!=(const Matrix &b) const
  51. {
  52. int i;
  53. if (this->width != b.width)
  54. {
  55. /* If they are not the same size don't even bother */
  56. return true;
  57. }
  58. for(i = 0; i < this->width*this->width; i++)
  59. {
  60. if (!double_equal(this->data[i], b.data[i]))
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. Matrix Matrix::operator*(const Matrix &b) const
  68. {
  69. int x, y, k;
  70. Matrix ret = Matrix(this->width);
  71. if (this->width == b.width)
  72. {
  73. for (y = 0 ; y < this->width ; y++)
  74. {
  75. for (x = 0 ; x < this->width ; x++)
  76. {
  77. double v = 0;
  78. for (k = 0 ; k < this->width ; k++)
  79. {
  80. v += this->get(x, k) * b.get(k, y);
  81. }
  82. ret.set(x, y, v);
  83. }
  84. }
  85. }
  86. return ret;
  87. }
  88. Tuple Matrix::operator*(const Tuple &b) const
  89. {
  90. 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),
  91. b.x * this->get(1, 0) + b.y * this->get(1, 1) + b.z * this->get(1, 2) + b.w * this->get(1, 3),
  92. b.x * this->get(2, 0) + b.y * this->get(2, 1) + b.z * this->get(2, 2) + b.w * this->get(2, 3),
  93. b.x * this->get(3, 0) + b.y * this->get(3, 1) + b.z * this->get(3, 2) + b.w * this->get(3, 3));
  94. }
  95. Matrix Matrix::identity()
  96. {
  97. int i;
  98. for(i = 0; i < this->width; i++)
  99. {
  100. this->set(i, i, 1);
  101. }
  102. return *this;
  103. }
  104. Matrix Matrix::transpose()
  105. {
  106. int x, y;
  107. Matrix ret = Matrix(this->width);
  108. for (y = 0 ; y < this->width ; y++)
  109. {
  110. for (x = 0 ; x < this->width ; x++)
  111. {
  112. ret.set(y, x, this->get(x, y));
  113. }
  114. }
  115. return ret;
  116. }