group.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. void Group::removeObject(Shape *s)
  135. {
  136. int i;
  137. if (s->haveFiniteBounds())
  138. {
  139. for (i = 0; i < this->objectCount; i++)
  140. {
  141. if (this->objectList[i] == s)
  142. {
  143. this->objectCount --;
  144. this->objectList[i] = this->objectList[this->objectCount];
  145. this->objectList[this->objectCount] = nullptr;
  146. break;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. for (i = 0; i < this->unboxableObjectCount; i++)
  153. {
  154. if (this->unboxableObjectList[i] == s)
  155. {
  156. this->unboxableObjectCount --;
  157. this->unboxableObjectList[i] = this->unboxableObjectList[this->unboxableObjectCount];
  158. this->unboxableObjectList[this->unboxableObjectCount] = nullptr;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. bool Group::isEmpty()
  165. {
  166. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  167. }
  168. BoundingBox Group::getLocalBounds()
  169. {
  170. return this->bounds;
  171. }
  172. BoundingBox Group::getBounds()
  173. {
  174. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  175. return this->bounds;
  176. }
  177. void Group::updateBoundingBox()
  178. {
  179. this->bounds.reset();
  180. if (this->objectCount > 0)
  181. {
  182. int i;
  183. for(i = 0; i < this->objectCount; i++)
  184. {
  185. if (this->objectList[i]->haveFiniteBounds())
  186. {
  187. BoundingBox objB = this->objectList[i]->getBounds();
  188. this->bounds | objB;
  189. }
  190. }
  191. }
  192. }
  193. void Group::updateTransform()
  194. {
  195. int i;
  196. Shape::updateTransform();
  197. if (this->objectCount > 0)
  198. {
  199. for (i = 0 ; i < this->objectCount ; i++)
  200. {
  201. this->objectList[i]->updateTransform();
  202. }
  203. }
  204. if (this->unboxableObjectCount > 0)
  205. {
  206. for(i = 0; i < this->unboxableObjectCount; i++)
  207. {
  208. this->unboxableObjectList[i]->updateTransform();
  209. }
  210. }
  211. /* Once the full stack being notified of the changes, let's update the
  212. * bounding box
  213. */
  214. this->updateBoundingBox();
  215. }
  216. void Group::dumpMe(FILE *fp)
  217. {
  218. int i;
  219. fprintf(fp, "\"Type\": \"Group\",\n");
  220. fprintf(fp, "\"Name\": \"%s\",\n", this->name);
  221. if (this->objectCount > 0)
  222. {
  223. fprintf(fp, "\"Objects\": {\n");
  224. for(i = 0; i < this->objectCount; i++)
  225. {
  226. fprintf(fp, "\"%d\": {\n", i);
  227. this->objectList[i]->dumpMe(fp);
  228. fprintf(fp, "},\n");
  229. }
  230. fprintf(fp, "},\n");
  231. }
  232. if (this->unboxableObjectCount > 0)
  233. {
  234. fprintf(fp, "\"UnboxableObjects\": {\n");
  235. for(i = 0; i < this->objectCount; i++)
  236. {
  237. fprintf(fp, "\"%d\": {\n", i);
  238. this->objectList[i]->dumpMe(fp);
  239. fprintf(fp, "},\n");
  240. }
  241. fprintf(fp, "},\n");
  242. }
  243. Shape::dumpMe(fp);
  244. }