group.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Group 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 <group.h>
  12. #include <cone.h>
  13. #include <math_helper.h>
  14. #define MIN_ALLOC (2)
  15. Group::Group() : Shape(SHAPE_GROUP)
  16. {
  17. this->allocatedObjectCount = MIN_ALLOC;
  18. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  19. this->objectCount = 0;
  20. this->allocatedUnboxableObjectCount = MIN_ALLOC;
  21. this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  22. this->unboxableObjectCount = 0;
  23. }
  24. Intersect Group::intersect(Ray r)
  25. {
  26. Intersect ret;
  27. int i, j;
  28. if (this->objectCount > 0)
  29. {
  30. if (this->bounds.intesectMe(r))
  31. {
  32. for (i = 0 ; i < this->objectCount ; i++)
  33. {
  34. Intersect xs = this->objectList[i]->intersect(r);
  35. if (xs.count() > 0)
  36. {
  37. for (j = 0 ; j < xs.count() ; j++)
  38. {
  39. ret.add(xs[j]);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. /* We are force to do them all the time */
  46. if (this->unboxableObjectCount > 0)
  47. {
  48. for(i = 0; i < this->unboxableObjectCount; i++)
  49. {
  50. Intersect xs = this->unboxableObjectList[i]->intersect(r);
  51. if (xs.count() > 0)
  52. {
  53. for(j = 0; j < xs.count(); j++)
  54. {
  55. ret.add(xs[j]);
  56. }
  57. }
  58. }
  59. }
  60. return ret;
  61. }
  62. Intersect Group::localIntersect(Ray r)
  63. {
  64. return Intersect();
  65. }
  66. Tuple Group::localNormalAt(Tuple point)
  67. {
  68. return Vector(1, 0, 0);
  69. }
  70. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  71. void Group::addObject(Shape *s)
  72. {
  73. if (s->haveFiniteBounds())
  74. {
  75. if ((this->objectCount + 1) > this->allocatedObjectCount)
  76. {
  77. this->allocatedObjectCount *= 2;
  78. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  79. }
  80. s->parent = this;
  81. s->updateTransform();
  82. this->objectList[this->objectCount++] = s;
  83. this->bounds | s->getBounds();
  84. }
  85. else
  86. {
  87. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  88. {
  89. this->allocatedUnboxableObjectCount *= 2;
  90. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  91. }
  92. s->parent = this;
  93. s->updateTransform();
  94. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  95. }
  96. }
  97. bool Group::isEmpty()
  98. {
  99. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  100. }
  101. BoundingBox Group::getBounds()
  102. {
  103. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  104. return this->bounds;
  105. }
  106. void Group::updateBoundingBox()
  107. {
  108. this->bounds.reset();
  109. if (this->objectCount > 0)
  110. {
  111. int i;
  112. for(i = 0; i < this->objectCount; i++)
  113. {
  114. if (!this->objectList[i]->haveFiniteBounds())
  115. {
  116. BoundingBox objB = this->objectList[i]->getBounds();
  117. this->bounds | objB;
  118. }
  119. }
  120. }
  121. }