tuple.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. Tuple operator+(const Tuple &b) const { return Tuple(this->x + b.x, this->y + b.y,
  26. this->z + b.z, this->w + b.w); };
  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 { return Tuple(-this->x, -this->y, -this->z, -this->w); };
  30. Tuple operator*(const double &b) const { return Tuple(this->x * b, this->y * b,
  31. this->z * b, this->w * b); };
  32. Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
  33. this->z / b, this->w / b); };
  34. double magnitude();
  35. Tuple normalise();
  36. double dot(const Tuple &b);
  37. };
  38. class Point: public Tuple
  39. {
  40. public:
  41. Point(double x, double y, double z) : Tuple(x, y, z, 1.0) {};
  42. };
  43. class Vector: public Tuple
  44. {
  45. public:
  46. Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
  47. Vector cross(const Vector &b) const;
  48. };
  49. #endif /*DORAYME_TUPLE_H*/