group.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if (this->unboxableObjectCount > 0)
  86. {
  87. int i;
  88. for(i = 0; i < this->unboxableObjectCount; i++)
  89. {
  90. if (this->unboxableObjectList[i] == b)
  91. {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. Intersect Group::localIntersect(Ray r)
  99. {
  100. return Intersect();
  101. }
  102. Tuple Group::localNormalAt(Tuple point, Intersection *hit)
  103. {
  104. return Vector(1, 0, 0);
  105. }
  106. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  107. void Group::addObject(Shape *s)
  108. {
  109. if (s->haveFiniteBounds())
  110. {
  111. if ((this->objectCount + 1) > this->allocatedObjectCount)
  112. {
  113. this->allocatedObjectCount *= 2;
  114. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  115. }
  116. s->setParent(this);
  117. s->updateTransform();
  118. this->objectList[this->objectCount++] = s;
  119. this->bounds | s->getBounds();
  120. }
  121. else
  122. {
  123. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  124. {
  125. this->allocatedUnboxableObjectCount *= 2;
  126. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  127. }
  128. s->setParent(this);
  129. s->updateTransform();
  130. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  131. }
  132. }
  133. void Group::removeObject(Shape *s)
  134. {
  135. int i;
  136. if (s->haveFiniteBounds())
  137. {
  138. for (i = 0; i < this->objectCount; i++)
  139. {
  140. if (this->objectList[i] == s)
  141. {
  142. this->objectCount --;
  143. this->objectList[i] = this->objectList[this->objectCount];
  144. this->objectList[this->objectCount] = nullptr;
  145. break;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. for (i = 0; i < this->unboxableObjectCount; i++)
  152. {
  153. if (this->unboxableObjectList[i] == s)
  154. {
  155. this->unboxableObjectCount --;
  156. this->unboxableObjectList[i] = this->unboxableObjectList[this->unboxableObjectCount];
  157. this->unboxableObjectList[this->unboxableObjectCount] = nullptr;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. bool Group::isEmpty()
  164. {
  165. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  166. }
  167. BoundingBox Group::getLocalBounds()
  168. {
  169. return this->bounds;
  170. }
  171. BoundingBox Group::getBounds()
  172. {
  173. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  174. return this->bounds;
  175. }
  176. void Group::updateBoundingBox()
  177. {
  178. this->bounds.reset();
  179. if (this->objectCount > 0)
  180. {
  181. int i;
  182. for(i = 0; i < this->objectCount; i++)
  183. {
  184. if (this->objectList[i]->haveFiniteBounds())
  185. {
  186. BoundingBox objB = this->objectList[i]->getBounds();
  187. this->bounds | objB;
  188. }
  189. }
  190. }
  191. }
  192. void Group::updateTransform()
  193. {
  194. int i;
  195. Shape::updateTransform();
  196. if (this->objectCount > 0)
  197. {
  198. for (i = 0 ; i < this->objectCount ; i++)
  199. {
  200. this->objectList[i]->updateTransform();
  201. }
  202. }
  203. if (this->unboxableObjectCount > 0)
  204. {
  205. for(i = 0; i < this->unboxableObjectCount; i++)
  206. {
  207. this->unboxableObjectList[i]->updateTransform();
  208. }
  209. }
  210. /* Once the full stack being notified of the changes, let's update the
  211. * bounding box
  212. */
  213. this->updateBoundingBox();
  214. }
  215. void Group::dumpMe(FILE *fp)
  216. {
  217. int i;
  218. fprintf(fp, "\"Type\": \"Group\",\n");
  219. fprintf(fp, "\"Name\": \"%s\",\n", this->name);
  220. if (this->objectCount > 0)
  221. {
  222. fprintf(fp, "\"Objects\": {\n");
  223. for(i = 0; i < this->objectCount; i++)
  224. {
  225. fprintf(fp, "\"%d\": {\n", i);
  226. this->objectList[i]->dumpMe(fp);
  227. fprintf(fp, "},\n");
  228. }
  229. fprintf(fp, "},\n");
  230. }
  231. if (this->unboxableObjectCount > 0)
  232. {
  233. fprintf(fp, "\"UnboxableObjects\": {\n");
  234. for(i = 0; i < this->objectCount; i++)
  235. {
  236. fprintf(fp, "\"%d\": {\n", i);
  237. this->objectList[i]->dumpMe(fp);
  238. fprintf(fp, "},\n");
  239. }
  240. fprintf(fp, "},\n");
  241. }
  242. Shape::dumpMe(fp);
  243. }
  244. void Group::lock()
  245. {
  246. Shape::lock();
  247. /* Now notify included object they have to lock */
  248. int i;
  249. if (this->objectCount > 0)
  250. {
  251. for (i = 0 ; i < this->objectCount ; i++)
  252. {
  253. this->objectList[i]->lock();
  254. }
  255. }
  256. if (this->unboxableObjectCount > 0)
  257. {
  258. for(i = 0; i < this->unboxableObjectCount; i++)
  259. {
  260. this->unboxableObjectList[i]->lock();
  261. }
  262. }
  263. }