group.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. bool Group::includes(Shape *b)
  64. {
  65. if (this->objectCount > 0)
  66. {
  67. int i;
  68. for (i = 0 ; i < this->objectCount ; i++)
  69. {
  70. if (this->objectList[i] == b)
  71. {
  72. return true;
  73. }
  74. }
  75. }
  76. /* We are force to do them all the time */
  77. if (this->unboxableObjectCount > 0)
  78. {
  79. int i;
  80. for(i = 0; i < this->unboxableObjectCount; i++)
  81. {
  82. if (this->unboxableObjectList[i] == b)
  83. {
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. Intersect Group::localIntersect(Ray r)
  91. {
  92. return Intersect();
  93. }
  94. Tuple Group::localNormalAt(Tuple point, Intersection *hit)
  95. {
  96. return Vector(1, 0, 0);
  97. }
  98. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  99. void Group::addObject(Shape *s)
  100. {
  101. if (s->haveFiniteBounds())
  102. {
  103. if ((this->objectCount + 1) > this->allocatedObjectCount)
  104. {
  105. this->allocatedObjectCount *= 2;
  106. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  107. }
  108. s->parent = this;
  109. s->updateTransform();
  110. this->objectList[this->objectCount++] = s;
  111. this->bounds | s->getBounds();
  112. }
  113. else
  114. {
  115. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  116. {
  117. this->allocatedUnboxableObjectCount *= 2;
  118. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  119. }
  120. s->parent = this;
  121. s->updateTransform();
  122. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  123. }
  124. }
  125. bool Group::isEmpty()
  126. {
  127. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  128. }
  129. BoundingBox Group::getLocalBounds()
  130. {
  131. return this->bounds;
  132. }
  133. BoundingBox Group::getBounds()
  134. {
  135. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  136. return this->bounds;
  137. }
  138. void Group::updateBoundingBox()
  139. {
  140. this->bounds.reset();
  141. if (this->objectCount > 0)
  142. {
  143. int i;
  144. for(i = 0; i < this->objectCount; i++)
  145. {
  146. if (this->objectList[i]->haveFiniteBounds())
  147. {
  148. BoundingBox objB = this->objectList[i]->getBounds();
  149. this->bounds | objB;
  150. }
  151. }
  152. }
  153. }
  154. void Group::updateTransform()
  155. {
  156. int i;
  157. Shape::updateTransform();
  158. if (this->objectCount > 0)
  159. {
  160. for (i = 0 ; i < this->objectCount ; i++)
  161. {
  162. this->objectList[i]->updateTransform();
  163. }
  164. }
  165. if (this->unboxableObjectCount > 0)
  166. {
  167. for(i = 0; i < this->unboxableObjectCount; i++)
  168. {
  169. this->unboxableObjectList[i]->updateTransform();
  170. }
  171. }
  172. /* Once the full stack being notified of the changes, let's update the
  173. * bounding box
  174. */
  175. this->updateBoundingBox();
  176. }
  177. void Group::dumpMe(FILE *fp)
  178. {
  179. int i;
  180. fprintf(fp, "\"Type\": \"Group\",\n");
  181. if (this->objectCount > 0)
  182. {
  183. fprintf(fp, "\"Objects\": {\n");
  184. for(i = 0; i < this->objectCount; i++)
  185. {
  186. fprintf(fp, "\"%d\": {\n", i);
  187. this->objectList[i]->dumpMe(fp);
  188. fprintf(fp, "},\n");
  189. }
  190. fprintf(fp, "},\n");
  191. }
  192. if (this->unboxableObjectCount > 0)
  193. {
  194. fprintf(fp, "\"UnboxableObjects\": {\n");
  195. for(i = 0; i < this->objectCount; i++)
  196. {
  197. fprintf(fp, "\"%d\": {\n", i);
  198. this->objectList[i]->dumpMe(fp);
  199. fprintf(fp, "},\n");
  200. }
  201. fprintf(fp, "},\n");
  202. }
  203. Shape::dumpMe(fp);
  204. }