csg.cpp 2.9 KB

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