boundingbox.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Bounding box header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_BOUNDINGBOX_H
  10. #define DORAYME_BOUNDINGBOX_H
  11. #include <renderstat.h>
  12. struct BoundingBox
  13. {
  14. private:
  15. bool isReset;
  16. public:
  17. Tuple min;
  18. Tuple max;
  19. BoundingBox() : min(INFINITY, INFINITY, INFINITY, 1.0), max(-INFINITY, -INFINITY, -INFINITY, 1.0), isReset(true) { };
  20. BoundingBox(Tuple min, Tuple max) : min(min), max(max), isReset(false) { };
  21. void operator|(const BoundingBox &b) {
  22. isReset = false;
  23. if (this->min.x > b.min.x) { this->min.x = b.min.x; }
  24. if (this->min.y > b.min.y) { this->min.y = b.min.y; }
  25. if (this->min.z > b.min.z) { this->min.z = b.min.z; }
  26. if (this->max.x < b.max.x) { this->max.x = b.max.x; }
  27. if (this->max.y < b.max.y) { this->max.y = b.max.y; }
  28. if (this->max.z < b.max.z) { this->max.z = b.max.z; }
  29. }
  30. void operator|(const Tuple &b) {
  31. isReset = false;
  32. if (this->min.x > b.x) { this->min.x = b.x; }
  33. if (this->min.y > b.y) { this->min.y = b.y; }
  34. if (this->min.z > b.z) { this->min.z = b.z; }
  35. if (this->max.x < b.x) { this->max.x = b.x; }
  36. if (this->max.y < b.y) { this->max.y = b.y; }
  37. if (this->max.z < b.z) { this->max.z = b.z; }
  38. }
  39. bool haveFiniteBounds() { return this->min.isRepresentable() && this->max.isRepresentable(); };
  40. bool fitsIn(const BoundingBox &other) {
  41. bool fits = true;
  42. if (this->min.x > other.min.x) { fits = false; }
  43. if (this->min.y > other.min.y) { fits = false; }
  44. if (this->min.z > other.min.z) { fits = false; }
  45. if (this->max.x < other.max.x) { fits = false; }
  46. if (this->max.y < other.max.y) { fits = false; }
  47. if (this->max.z < other.max.z) { fits = false; }
  48. return fits;
  49. }
  50. void checkAxis(double axeOrigin, double axeDirection, double xMin, double xMax, double *axeMin, double *axeMax)
  51. {
  52. double tMinNumerator = (xMin - axeOrigin);
  53. double tMaxNumerator = (xMax - axeOrigin);
  54. if (fabs(axeDirection) >= getEpsilon())
  55. {
  56. *axeMin = tMinNumerator / axeDirection;
  57. *axeMax = tMaxNumerator / axeDirection;
  58. }
  59. else
  60. {
  61. *axeMin = tMinNumerator * INFINITY;
  62. *axeMax = tMaxNumerator * INFINITY;
  63. }
  64. if (*axeMin > *axeMax)
  65. {
  66. double swap = *axeMax;
  67. *axeMax = *axeMin;
  68. *axeMin = swap;
  69. }
  70. }
  71. void reset()
  72. {
  73. this->isReset = true;
  74. min.x = min.y = min.z = INFINITY;
  75. max.x = max.y = max.z = -INFINITY;
  76. }
  77. bool isEmpty() { return this->isReset; };
  78. bool intesectMe(Ray r) {
  79. double xtMin, xtMax, ytMin, ytMax, ztMin, ztMax;
  80. double tMin, tMax;
  81. this->checkAxis(r.origin.x, r.direction.x, this->min.x, this->max.x, &xtMin, &xtMax);
  82. this->checkAxis(r.origin.y, r.direction.y, this->min.y, this->max.y, &ytMin, &ytMax);
  83. this->checkAxis(r.origin.z, r.direction.z, this->min.z, this->max.z, &ztMin, &ztMax);
  84. tMin = max3(xtMin, ytMin, ztMin);
  85. tMax = min3(xtMax, ytMax, ztMax);
  86. if (tMin <= tMax)
  87. {
  88. return true;
  89. }
  90. stats.addDiscardedIntersect();
  91. return false;
  92. }
  93. };
  94. #endif /* DORAYME_BOUNDINGBOX_H */