group.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. void Group::intersect(Ray &r, Intersect &xs)
  35. {
  36. int i, j;
  37. if (this->objectCount > 0)
  38. {
  39. if (this->bounds.intesectMe(r))
  40. {
  41. for (i = 0 ; i < this->objectCount ; i++)
  42. {
  43. this->objectList[i]->intersect(r, xs);
  44. }
  45. }
  46. }
  47. /* We are force to do them all the time */
  48. if (this->unboxableObjectCount > 0)
  49. {
  50. for(i = 0; i < this->unboxableObjectCount; i++)
  51. {
  52. this->unboxableObjectList[i]->intersect(r, xs);
  53. }
  54. }
  55. }
  56. bool Group::includes(Shape *b)
  57. {
  58. if (this->objectCount > 0)
  59. {
  60. int i;
  61. for (i = 0 ; i < this->objectCount ; i++)
  62. {
  63. if (this->objectList[i] == b)
  64. {
  65. return true;
  66. }
  67. }
  68. }
  69. if (this->unboxableObjectCount > 0)
  70. {
  71. int i;
  72. for(i = 0; i < this->unboxableObjectCount; i++)
  73. {
  74. if (this->unboxableObjectList[i] == b)
  75. {
  76. return true;
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82. void Group::localIntersect(Ray r, Intersect &xs)
  83. {
  84. this->intersect(r, xs);
  85. }
  86. Tuple Group::localNormalAt(Tuple point, Intersection *hit)
  87. {
  88. return Vector(1, 0, 0);
  89. }
  90. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  91. void Group::addObject(Shape *s)
  92. {
  93. if (s->haveFiniteBounds())
  94. {
  95. if ((this->objectCount + 1) > this->allocatedObjectCount)
  96. {
  97. this->allocatedObjectCount *= 2;
  98. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  99. }
  100. s->setParent(this);
  101. s->updateTransform();
  102. this->objectList[this->objectCount++] = s;
  103. this->bounds | s->getBounds();
  104. }
  105. else
  106. {
  107. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  108. {
  109. this->allocatedUnboxableObjectCount *= 2;
  110. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  111. }
  112. s->setParent(this);
  113. s->updateTransform();
  114. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  115. }
  116. }
  117. void Group::removeObject(Shape *s)
  118. {
  119. int i;
  120. if (s->haveFiniteBounds())
  121. {
  122. for (i = 0; i < this->objectCount; i++)
  123. {
  124. if (this->objectList[i] == s)
  125. {
  126. this->objectCount --;
  127. this->objectList[i] = this->objectList[this->objectCount];
  128. this->objectList[this->objectCount] = nullptr;
  129. break;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. for (i = 0; i < this->unboxableObjectCount; i++)
  136. {
  137. if (this->unboxableObjectList[i] == s)
  138. {
  139. this->unboxableObjectCount --;
  140. this->unboxableObjectList[i] = this->unboxableObjectList[this->unboxableObjectCount];
  141. this->unboxableObjectList[this->unboxableObjectCount] = nullptr;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. bool Group::isEmpty()
  148. {
  149. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  150. }
  151. BoundingBox Group::getLocalBounds()
  152. {
  153. return this->bounds;
  154. }
  155. BoundingBox Group::getBounds()
  156. {
  157. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  158. return this->bounds;
  159. }
  160. void Group::updateBoundingBox()
  161. {
  162. this->bounds.reset();
  163. if (this->objectCount > 0)
  164. {
  165. int i;
  166. for(i = 0; i < this->objectCount; i++)
  167. {
  168. if (this->objectList[i]->haveFiniteBounds())
  169. {
  170. BoundingBox objB = this->objectList[i]->getBounds();
  171. this->bounds | objB;
  172. }
  173. }
  174. }
  175. }
  176. void Group::updateTransform()
  177. {
  178. int i;
  179. Shape::updateTransform();
  180. if (this->objectCount > 0)
  181. {
  182. for (i = 0 ; i < this->objectCount ; i++)
  183. {
  184. this->objectList[i]->updateTransform();
  185. }
  186. }
  187. if (this->unboxableObjectCount > 0)
  188. {
  189. for(i = 0; i < this->unboxableObjectCount; i++)
  190. {
  191. this->unboxableObjectList[i]->updateTransform();
  192. }
  193. }
  194. /* Once the full stack being notified of the changes, let's update the
  195. * bounding box
  196. */
  197. this->updateBoundingBox();
  198. }
  199. void Group::dumpMe(FILE *fp)
  200. {
  201. int i;
  202. fprintf(fp, "\"Type\": \"Group\",\n");
  203. fprintf(fp, "\"Name\": \"%s\",\n", this->name);
  204. if (this->objectCount > 0)
  205. {
  206. fprintf(fp, "\"Objects\": {\n");
  207. for(i = 0; i < this->objectCount; i++)
  208. {
  209. fprintf(fp, "\"%d\": {\n", i);
  210. this->objectList[i]->dumpMe(fp);
  211. fprintf(fp, "},\n");
  212. }
  213. fprintf(fp, "},\n");
  214. }
  215. if (this->unboxableObjectCount > 0)
  216. {
  217. fprintf(fp, "\"UnboxableObjects\": {\n");
  218. for(i = 0; i < this->objectCount; i++)
  219. {
  220. fprintf(fp, "\"%d\": {\n", i);
  221. this->objectList[i]->dumpMe(fp);
  222. fprintf(fp, "},\n");
  223. }
  224. fprintf(fp, "},\n");
  225. }
  226. Shape::dumpMe(fp);
  227. }
  228. void Group::lock()
  229. {
  230. Shape::lock();
  231. /* Now notify included object they have to lock */
  232. int i;
  233. if (this->objectCount > 0)
  234. {
  235. for (i = 0 ; i < this->objectCount ; i++)
  236. {
  237. this->objectList[i]->lock();
  238. }
  239. }
  240. if (this->unboxableObjectCount > 0)
  241. {
  242. for(i = 0; i < this->unboxableObjectCount; i++)
  243. {
  244. this->unboxableObjectList[i]->lock();
  245. }
  246. }
  247. }