csg.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Constructive Solid Geometry (CSG) implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <tuple.h>
  10. #include <ray.h>
  11. #include <shape.h>
  12. #include <csg.h>
  13. #include <math_helper.h>
  14. CSG::CSG(OperationType operation, Shape *left, Shape *right) : Shape(SHAPE_CSG), operation(operation), left(left), right(right)
  15. {
  16. stats.addCsg();
  17. this->left->parent = this;
  18. this->right->parent = this;
  19. this->bounds | this->left->getBounds();
  20. this->bounds | this->right->getBounds();
  21. }
  22. Intersect CSG::localIntersect(Ray r)
  23. {
  24. int i;
  25. Intersect leftxs = this->left->intersect(r);
  26. Intersect rightxs = this->right->intersect(r);
  27. for(i = 0; i < rightxs.count(); i++)
  28. {
  29. leftxs.add(rightxs[i]);
  30. }
  31. Intersect ret = this->filterIntersections(leftxs);
  32. return ret;
  33. }
  34. Intersect CSG::intersect(Ray r)
  35. {
  36. return localIntersect(r);
  37. }
  38. Tuple CSG::localNormalAt(Tuple point, Intersection *hit)
  39. {
  40. return Vector(1, 0, 0);
  41. }
  42. BoundingBox CSG::getLocalBounds()
  43. {
  44. return this->bounds;
  45. }
  46. BoundingBox CSG::getBounds()
  47. {
  48. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  49. return this->bounds;
  50. }
  51. void CSG::updateBoundingBox()
  52. {
  53. this->bounds.reset();
  54. this->bounds | this->left->getBounds();
  55. this->bounds | this->right->getBounds();
  56. }
  57. void CSG::updateTransform()
  58. {
  59. Shape::updateTransform();
  60. this->left->updateTransform();
  61. this->right->updateTransform();
  62. /* Once the full stack being notified of the changes, let's update the
  63. * bounding box
  64. */
  65. this->updateBoundingBox();
  66. }
  67. bool CSG::includes(Shape *b)
  68. {
  69. if (this->left->includes(b)) { return true; }
  70. if (this->right->includes(b)) { return true; }
  71. if (this == b) { return true; }
  72. return false;
  73. }
  74. bool CSG::intersectionAllowed(bool leftHit, bool inLeft, bool inRight)
  75. {
  76. switch(this->operation)
  77. {
  78. case CSG::UNION: return (leftHit && !inRight) || (!leftHit && !inLeft);
  79. case CSG::INTERSECTION: return (!leftHit && inLeft) || (leftHit && inRight);
  80. case CSG::DIFFERENCE: return (leftHit && !inRight) || (!leftHit && inLeft);
  81. }
  82. return false;
  83. }
  84. Intersect CSG::filterIntersections(Intersect &xs)
  85. {
  86. bool inl = false;
  87. bool inr = false;
  88. Intersect ret = Intersect();
  89. int i;
  90. for(i = 0; i < xs.count(); i++)
  91. {
  92. bool lhit = this->left->includes(xs[i].object);
  93. if (this->intersectionAllowed(lhit, inl, inr))
  94. {
  95. ret.add(xs[i]);
  96. }
  97. if (lhit)
  98. {
  99. inl = !inl;
  100. }
  101. else
  102. {
  103. inr = !inr;
  104. }
  105. }
  106. return ret;
  107. }
  108. void CSG::dumpMe(FILE *fp)
  109. {
  110. }