worldoptimiser.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * WorldOptimiser implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <shape.h>
  10. #include <group.h>
  11. #include <objfile.h>
  12. #include <world.h>
  13. #include <worldoptimiser.h>
  14. /* This function is meant to move all infinite object to the root group */
  15. void WorldOptimiser::moveInfiniteObjects(Shape *s)
  16. {
  17. if (s == nullptr)
  18. {
  19. s = this->root;
  20. }
  21. if (s->getType() == Shape::OBJFILE)
  22. {
  23. /* Special case */
  24. OBJFile *obj = (OBJFile *)s;
  25. s = obj->getBaseGroup();
  26. }
  27. if (s->getType() == Shape::GROUP)
  28. {
  29. int i;
  30. Group *grp = (Group *)s;
  31. if (grp->getUnboxableCount() > 0)
  32. {
  33. for(i = 0; i < grp->getUnboxableCount(); i++)
  34. {
  35. Shape *shp = grp->getUnboxable(i);
  36. if (this->root != s)
  37. {
  38. if (shp->getType() == Shape::GROUP)
  39. {
  40. /* Issue a warning if it is a group */
  41. printf("WARNING: The group '%s' in '%s' have infinite bounds, all items part of it will not be optimised."
  42. "That may affect performances!",
  43. ((Group *)shp)->getName(),
  44. grp->getName());
  45. }
  46. this->root->addObject(shp);
  47. grp->removeObject(shp);
  48. /* We remove an object from that list, so need to do some stuffs. */
  49. i -= 1;
  50. }
  51. }
  52. }
  53. /* Now let's traverse the rest of that group */
  54. if (grp->getObjectCount() > 0)
  55. {
  56. for(i = 0; i < grp->getObjectCount(); i++)
  57. {
  58. Shape *shp = grp->getObject(i);
  59. this->moveInfiniteObjects(shp);
  60. }
  61. }
  62. }
  63. /* If it is not a group, there is nothing to be done there. */
  64. }
  65. void WorldOptimiser::moveAllObjects(Shape *s)
  66. {
  67. if (s == nullptr)
  68. {
  69. s = this->root;
  70. }
  71. if (s->getType() == Shape::OBJFILE)
  72. {
  73. /* Special case */
  74. OBJFile *obj = (OBJFile *)s;
  75. s = obj->getBaseGroup();
  76. }
  77. /* We should be here only when it is a group, but better being safe. */
  78. if (s->getType() == Shape::GROUP)
  79. {
  80. int i;
  81. Group *grp = (Group *)s;
  82. /* Now let's traverse the rest of that group */
  83. if (grp->getObjectCount() > 0)
  84. {
  85. for(i = 0; i < grp->getObjectCount(); i++)
  86. {
  87. Shape *shp = grp->getObject(i);
  88. switch(shp->getType())
  89. {
  90. default:
  91. /* Don't move if we are on the same leaf */
  92. if (this->root != s)
  93. {
  94. /* It is not a group type object so, move it! */
  95. this->root->addObject(shp);
  96. grp->removeObject(shp);
  97. /* We remove an object from that list, so need to do some stuffs. */
  98. i -= 1;
  99. }
  100. break;
  101. case Shape::GROUP:
  102. case Shape::OBJFILE:
  103. this->moveAllObjects(shp);
  104. }
  105. }
  106. }
  107. }
  108. /* If it is not a group, there is nothing to be done there. */
  109. }