group.cpp 5.4 KB

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