group.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <math_helper.h>
  13. #include <renderstat.h>
  14. #define MIN_ALLOC (2)
  15. Group::Group() : Shape(SHAPE_GROUP)
  16. {
  17. stats.addGroup();
  18. this->allocatedObjectCount = MIN_ALLOC;
  19. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  20. this->objectCount = 0;
  21. this->allocatedUnboxableObjectCount = MIN_ALLOC;
  22. this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  23. this->unboxableObjectCount = 0;
  24. }
  25. Intersect Group::intersect(Ray r)
  26. {
  27. Intersect ret;
  28. int i, j;
  29. if (this->objectCount > 0)
  30. {
  31. if (this->bounds.intesectMe(r))
  32. {
  33. for (i = 0 ; i < this->objectCount ; i++)
  34. {
  35. Intersect xs = this->objectList[i]->intersect(r);
  36. if (xs.count() > 0)
  37. {
  38. for (j = 0 ; j < xs.count() ; j++)
  39. {
  40. ret.add(xs[j]);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. /* We are force to do them all the time */
  47. if (this->unboxableObjectCount > 0)
  48. {
  49. for(i = 0; i < this->unboxableObjectCount; i++)
  50. {
  51. Intersect xs = this->unboxableObjectList[i]->intersect(r);
  52. if (xs.count() > 0)
  53. {
  54. for(j = 0; j < xs.count(); j++)
  55. {
  56. ret.add(xs[j]);
  57. }
  58. }
  59. }
  60. }
  61. return ret;
  62. }
  63. Intersect Group::localIntersect(Ray r)
  64. {
  65. return Intersect();
  66. }
  67. Tuple Group::localNormalAt(Tuple point)
  68. {
  69. return Vector(1, 0, 0);
  70. }
  71. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  72. void Group::addObject(Shape *s)
  73. {
  74. if (s->haveFiniteBounds())
  75. {
  76. if ((this->objectCount + 1) > this->allocatedObjectCount)
  77. {
  78. this->allocatedObjectCount *= 2;
  79. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  80. }
  81. s->parent = this;
  82. s->updateTransform();
  83. this->objectList[this->objectCount++] = s;
  84. this->bounds | s->getBounds();
  85. }
  86. else
  87. {
  88. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  89. {
  90. this->allocatedUnboxableObjectCount *= 2;
  91. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  92. }
  93. s->parent = this;
  94. s->updateTransform();
  95. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  96. }
  97. }
  98. bool Group::isEmpty()
  99. {
  100. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  101. }
  102. BoundingBox Group::getLocalBounds()
  103. {
  104. return this->bounds;
  105. }
  106. BoundingBox Group::getBounds()
  107. {
  108. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  109. return this->bounds;
  110. }
  111. void Group::updateBoundingBox()
  112. {
  113. this->bounds.reset();
  114. if (this->objectCount > 0)
  115. {
  116. int i;
  117. for(i = 0; i < this->objectCount; i++)
  118. {
  119. if (this->objectList[i]->haveFiniteBounds())
  120. {
  121. BoundingBox objB = this->objectList[i]->getBounds();
  122. this->bounds | objB;
  123. }
  124. }
  125. }
  126. }
  127. void Group::updateTransform()
  128. {
  129. int i;
  130. Shape::updateTransform();
  131. if (this->objectCount > 0)
  132. {
  133. for (i = 0 ; i < this->objectCount ; i++)
  134. {
  135. this->objectList[i]->updateTransform();
  136. }
  137. }
  138. /* We are force to do them all the time */
  139. if (this->unboxableObjectCount > 0)
  140. {
  141. for(i = 0; i < this->unboxableObjectCount; i++)
  142. {
  143. this->unboxableObjectList[i]->updateTransform();
  144. }
  145. }
  146. /* Once the full stack being notified of the changes, let's update the
  147. * bounding box
  148. */
  149. this->updateBoundingBox();
  150. }
  151. void Group::dumpMe(FILE *fp)
  152. {
  153. int i;
  154. fprintf(fp, "\"Type\": \"Group\",\n");
  155. if (this->objectCount > 0)
  156. {
  157. fprintf(fp, "\"Objects\": {\n");
  158. for(i = 0; i < this->objectCount; i++)
  159. {
  160. fprintf(fp, "\"%d\": {\n", i);
  161. this->objectList[i]->dumpMe(fp);
  162. fprintf(fp, "},\n");
  163. }
  164. fprintf(fp, "},\n");
  165. }
  166. if (this->unboxableObjectCount > 0)
  167. {
  168. fprintf(fp, "\"UnboxableObjects\": {\n");
  169. for(i = 0; i < this->objectCount; i++)
  170. {
  171. fprintf(fp, "\"%d\": {\n", i);
  172. this->objectList[i]->dumpMe(fp);
  173. fprintf(fp, "},\n");
  174. }
  175. fprintf(fp, "},\n");
  176. }
  177. Shape::dumpMe(fp);
  178. }