csg.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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->setParent(this);
  18. this->right->setParent(this);
  19. this->bounds | this->left->getBounds();
  20. this->bounds | this->right->getBounds();
  21. }
  22. void CSG::localIntersect(Ray r, Intersect &xs)
  23. {
  24. this->intersect(r, xs);
  25. }
  26. void CSG::intersect(Ray &r, Intersect &xs)
  27. {
  28. int i;
  29. Intersect tmp = Intersect();
  30. if (this->bounds.intesectMe(r))
  31. {
  32. this->left->intersect(r, tmp);
  33. this->right->intersect(r, tmp);
  34. this->filterIntersections(tmp, xs);
  35. }
  36. }
  37. Tuple CSG::localNormalAt(Tuple point, Intersection *hit)
  38. {
  39. return Vector(1, 0, 0);
  40. }
  41. BoundingBox CSG::getLocalBounds()
  42. {
  43. return this->bounds;
  44. }
  45. BoundingBox CSG::getBounds()
  46. {
  47. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  48. return this->bounds;
  49. }
  50. void CSG::updateBoundingBox()
  51. {
  52. this->bounds.reset();
  53. this->bounds | this->left->getBounds();
  54. this->bounds | this->right->getBounds();
  55. }
  56. void CSG::updateTransform()
  57. {
  58. Shape::updateTransform();
  59. this->left->updateTransform();
  60. this->right->updateTransform();
  61. /* Once the full stack being notified of the changes, let's update the
  62. * bounding box
  63. */
  64. this->updateBoundingBox();
  65. }
  66. bool CSG::includes(Shape *b)
  67. {
  68. if (this->left->includes(b)) { return true; }
  69. if (this->right->includes(b)) { return true; }
  70. if (this == b) { return true; }
  71. return false;
  72. }
  73. bool CSG::intersectionAllowed(bool leftHit, bool inLeft, bool inRight)
  74. {
  75. switch(this->operation)
  76. {
  77. case CSG::UNION: return (leftHit && !inRight) || (!leftHit && !inLeft);
  78. case CSG::INTERSECTION: return (!leftHit && inLeft) || (leftHit && inRight);
  79. case CSG::DIFFERENCE: return (leftHit && !inRight) || (!leftHit && inLeft);
  80. }
  81. return false;
  82. }
  83. void CSG::filterIntersections(Intersect &xs, Intersect &ret)
  84. {
  85. bool inl = false;
  86. bool inr = false;
  87. int i;
  88. for(i = 0; i < xs.count(); i++)
  89. {
  90. bool lhit = this->left->includes(xs[i].object);
  91. if (this->intersectionAllowed(lhit, inl, inr))
  92. {
  93. ret.add(xs[i]);
  94. }
  95. if (lhit)
  96. {
  97. inl = !inl;
  98. }
  99. else
  100. {
  101. inr = !inr;
  102. }
  103. }
  104. }
  105. void CSG::lock()
  106. {
  107. Shape::lock();
  108. if(this->left)
  109. {
  110. this->left->lock();
  111. }
  112. if(this->right)
  113. {
  114. this->right->lock();
  115. }
  116. }
  117. void CSG::dumpMe(FILE *fp)
  118. {
  119. }