boundingbox.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <stdio.h>
  13. struct BoundingBox
  14. {
  15. private:
  16. bool isReset;
  17. public:
  18. Tuple min;
  19. Tuple max;
  20. BoundingBox() : min(INFINITY, INFINITY, INFINITY, 1.0), max(-INFINITY, -INFINITY, -INFINITY, 1.0), isReset(true) { };
  21. BoundingBox(Tuple min, Tuple max) : min(min), max(max), isReset(false) { };
  22. void operator|(const BoundingBox &b) {
  23. isReset = false;
  24. if (this->min.x > b.min.x) { this->min.x = b.min.x; }
  25. if (this->min.y > b.min.y) { this->min.y = b.min.y; }
  26. if (this->min.z > b.min.z) { this->min.z = b.min.z; }
  27. if (this->max.x < b.max.x) { this->max.x = b.max.x; }
  28. if (this->max.y < b.max.y) { this->max.y = b.max.y; }
  29. if (this->max.z < b.max.z) { this->max.z = b.max.z; }
  30. }
  31. void operator|(const Tuple &b) {
  32. isReset = false;
  33. if (this->min.x > b.x) { this->min.x = b.x; }
  34. if (this->min.y > b.y) { this->min.y = b.y; }
  35. if (this->min.z > b.z) { this->min.z = b.z; }
  36. if (this->max.x < b.x) { this->max.x = b.x; }
  37. if (this->max.y < b.y) { this->max.y = b.y; }
  38. if (this->max.z < b.z) { this->max.z = b.z; }
  39. }
  40. bool haveFiniteBounds() { return this->min.isRepresentable() && this->max.isRepresentable(); };
  41. bool fitsIn(const BoundingBox &other) {
  42. bool fits = true;
  43. if (this->min.x > other.min.x) { fits = false; }
  44. if (this->min.y > other.min.y) { fits = false; }
  45. if (this->min.z > other.min.z) { fits = false; }
  46. if (this->max.x < other.max.x) { fits = false; }
  47. if (this->max.y < other.max.y) { fits = false; }
  48. if (this->max.z < other.max.z) { fits = false; }
  49. return fits;
  50. }
  51. void checkAxis(double axeOrigin, double axeDirection, double xMin, double xMax, double *axeMin, double *axeMax)
  52. {
  53. double tMinNumerator = (xMin - axeOrigin);
  54. double tMaxNumerator = (xMax - axeOrigin);
  55. if (fabs(axeDirection) >= getEpsilon())
  56. {
  57. *axeMin = tMinNumerator / axeDirection;
  58. *axeMax = tMaxNumerator / axeDirection;
  59. }
  60. else
  61. {
  62. *axeMin = tMinNumerator * INFINITY;
  63. *axeMax = tMaxNumerator * INFINITY;
  64. }
  65. if (*axeMin > *axeMax)
  66. {
  67. double swap = *axeMax;
  68. *axeMax = *axeMin;
  69. *axeMin = swap;
  70. }
  71. }
  72. void reset()
  73. {
  74. this->isReset = true;
  75. min.x = min.y = min.z = INFINITY;
  76. max.x = max.y = max.z = -INFINITY;
  77. }
  78. bool isEmpty() { return this->isReset; };
  79. bool intesectMe(Ray r) {
  80. double xtMin, xtMax, ytMin, ytMax, ztMin, ztMax;
  81. double tMin, tMax;
  82. this->checkAxis(r.origin.x, r.direction.x, this->min.x, this->max.x, &xtMin, &xtMax);
  83. this->checkAxis(r.origin.y, r.direction.y, this->min.y, this->max.y, &ytMin, &ytMax);
  84. this->checkAxis(r.origin.z, r.direction.z, this->min.z, this->max.z, &ztMin, &ztMax);
  85. tMin = max3(xtMin, ytMin, ztMin);
  86. tMax = min3(xtMax, ytMax, ztMax);
  87. if (tMin <= tMax)
  88. {
  89. return true;
  90. }
  91. stats.addDiscardedIntersect();
  92. return false;
  93. }
  94. void dumpMe(FILE *fp)
  95. {
  96. Tuple t = this->min;
  97. fprintf(fp, "\"min\": { \"x\": %f, \"y\": %f, \"z\": %f}, \n",
  98. t.x, t.y, t.z);
  99. t = this->max;
  100. fprintf(fp, "\"max\": { \"x\": %f, \"y\": %f, \"z\": %f}, \n",
  101. t.x, t.y, t.z);
  102. }
  103. };
  104. #endif /* DORAYME_BOUNDINGBOX_H */