group.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <cone.h>
  13. #include <math_helper.h>
  14. #define MIN_ALLOC (2)
  15. Group::Group() : Shape(SHAPE_GROUP)
  16. {
  17. this->allocatedObjectCount = MIN_ALLOC;
  18. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  19. this->objectCount = 0;
  20. }
  21. Intersect Group::intersect(Ray r)
  22. {
  23. Intersect ret;
  24. int i, j;
  25. if (this->objectCount > 0)
  26. {
  27. for(i = 0; i < this->objectCount; i++)
  28. {
  29. Intersect xs = this->objectList[i]->intersect(r);
  30. if (xs.count() > 0)
  31. {
  32. for(j = 0; j < xs.count(); j++)
  33. {
  34. ret.add(xs[j]);
  35. }
  36. }
  37. }
  38. }
  39. return ret;
  40. }
  41. Intersect Group::localIntersect(Ray r)
  42. {
  43. return Intersect();
  44. }
  45. Tuple Group::localNormalAt(Tuple point)
  46. {
  47. return Vector(1, 0, 0);
  48. }
  49. void Group::addObject(Shape *s)
  50. {
  51. if ((this->objectCount + 1) > this->allocatedObjectCount)
  52. {
  53. this->allocatedObjectCount *= 2;
  54. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  55. }
  56. s->parent = this;
  57. s->updateTransform();
  58. this->objectList[this->objectCount++] = s;
  59. }
  60. bool Group::isEmpty()
  61. {
  62. return (this->objectCount == 0);
  63. }
  64. BoundingBox Group::getBounds()
  65. {
  66. BoundingBox ret;
  67. if (this->objectCount > 0)
  68. {
  69. ret.min = Point(INFINITY, INFINITY, INFINITY);
  70. ret.max = Point(-INFINITY, -INFINITY, -INFINITY);
  71. int i;
  72. for(i = 0; i < this->objectCount; i++)
  73. {
  74. BoundingBox obj = this->objectList[i]->getBounds();
  75. if (ret.min.x > obj.min.x) { ret.min.x = obj.min.x; }
  76. if (ret.min.y > obj.min.y) { ret.min.y = obj.min.y; }
  77. if (ret.min.z > obj.min.z) { ret.min.z = obj.min.z; }
  78. if (ret.max.x < obj.max.x) { ret.max.x = obj.max.x; }
  79. if (ret.max.y < obj.max.y) { ret.max.y = obj.max.y; }
  80. if (ret.max.z < obj.max.z) { ret.max.z = obj.max.z; }
  81. }
  82. }
  83. return ret;
  84. }