tuple.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Tuples header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_TUPLE_H
  10. #define DORAYME_TUPLE_H
  11. #include <math_helper.h>
  12. class Tuple
  13. {
  14. public:
  15. double x, y, z, w;
  16. public:
  17. Tuple() : x(0), y(0), z(0), w(0.0) {};
  18. Tuple(double x, double y, double z) : x(x), y(y), z(z), w(0.0) {};
  19. Tuple(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {};
  20. bool isPoint() { return (this->w == 1.0); };
  21. bool isVector() { return (this->w == 0.0); };
  22. bool operator==(const Tuple &b) const { return double_equal(this->x, b.x) &&
  23. double_equal(this->y, b.y) &&
  24. double_equal(this->z, b.z) &&
  25. double_equal(this->w, b.w); };
  26. bool operator!=(const Tuple &b) const { return !(*this == b); };
  27. Tuple operator+(const Tuple &b) const { return Tuple(this->x + b.x, this->y + b.y,
  28. this->z + b.z, this->w + b.w); };
  29. Tuple operator-(const Tuple &b) const { return Tuple(this->x - b.x, this->y - b.y,
  30. this->z - b.z, this->w - b.w); };
  31. Tuple operator-() const { return Tuple(-this->x, -this->y, -this->z, -this->w); };
  32. Tuple operator*(const double &b) const { return Tuple(this->x * b, this->y * b,
  33. this->z * b, this->w * b); };
  34. Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
  35. this->z / b, this->w / b); };
  36. bool isRepresentable();
  37. void set(double nX, double nY, double nZ) { this->x = nX; this->y = nY; this->z = nZ; };
  38. double magnitude();
  39. Tuple normalise();
  40. double dot(const Tuple &b) {
  41. return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
  42. }
  43. Tuple cross(const Tuple &b) const {
  44. return Tuple(this->y * b.z - this->z * b.y,
  45. this->z * b.x - this->x * b.z,
  46. this->x * b.y - this->y * b.x,
  47. 0);
  48. }
  49. Tuple reflect(const Tuple &normal);
  50. };
  51. class Point: public Tuple
  52. {
  53. public:
  54. Point() : Tuple(0, 0, 0, 1.0) {};
  55. Point(double x, double y, double z) : Tuple(x, y, z, 1.0) {};
  56. };
  57. class Vector: public Tuple
  58. {
  59. public:
  60. Vector() : Tuple(0, 0, 0, 0.0) {};
  61. Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
  62. };
  63. #endif /* DORAYME_TUPLE_H */