tuple.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(double x, double y, double z) : x(x), y(y), z(z), w(0.0) {};
  18. Tuple(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {};
  19. bool isPoint() { return (this->w == 1.0); };
  20. bool isVector() { return (this->w == 0.0); };
  21. bool operator==(const Tuple &b) const { return double_equal(this->x, b.x) &&
  22. double_equal(this->y, b.y) &&
  23. double_equal(this->z, b.z) &&
  24. double_equal(this->w, b.w); };
  25. bool operator!=(const Tuple &b) const { return !(*this == b); };
  26. Tuple operator+(const Tuple &b) const { return Tuple(this->x + b.x, this->y + b.y,
  27. this->z + b.z, this->w + b.w); };
  28. Tuple operator-(const Tuple &b) const { return Tuple(this->x - b.x, this->y - b.y,
  29. this->z - b.z, this->w - b.w); };
  30. Tuple operator-() const { return Tuple(-this->x, -this->y, -this->z, -this->w); };
  31. Tuple operator*(const double &b) const { return Tuple(this->x * b, this->y * b,
  32. this->z * b, this->w * b); };
  33. Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
  34. this->z / b, this->w / b); };
  35. double magnitude();
  36. Tuple normalise();
  37. double dot(const Tuple &b);
  38. Tuple cross(const Tuple &b) const;
  39. Tuple reflect(const Tuple &normal);
  40. };
  41. class Point: public Tuple
  42. {
  43. public:
  44. Point(double x, double y, double z) : Tuple(x, y, z, 1.0) {};
  45. };
  46. class Vector: public Tuple
  47. {
  48. public:
  49. Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
  50. };
  51. #endif /* DORAYME_TUPLE_H */