💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.9 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "csg.hpp"
  20. #include <tuple>
  21. namespace geom {
  22. namespace csg {
  23. enum class polygon_classification
  24. {
  25. coplanar,
  26. front,
  27. back,
  28. spanning
  29. };
  30. /**
  31. * Classifies a polygon relative to a partitioning plane.
  32. *
  33. * @param partition Partitioning plane relative to which the polygon should be classified.
  34. * @param poly Polygon to be classified.
  35. * @return Classification of the polygon, relative to the plane.
  36. */
  37. static polygon_classification classify_polygon(const plane& partition, const polygon& poly)
  38. {
  39. for (const float3& vertex: poly.vertices)
  40. {
  41. }
  42. return polygon_classification::coplanar;
  43. }
  44. /**
  45. * Splits a polygon along a partitioning plane.
  46. *
  47. * @param poly Polygon to split.
  48. * @param partition Partitioning plane along which the polygon should be split.
  49. * @return List of polygons which were formed by splitting the specified polygon along the partitioning plane, along with their respective classifications relative to the partition.
  50. */
  51. std::list<std::tuple<polygon, polygon_classification>> split_polygon(const polygon& poly, const plane& partition)
  52. {
  53. return {};
  54. }
  55. bsp_tree::bsp_tree(const std::list<polygon>& polygons):
  56. front(nullptr),
  57. back(nullptr)
  58. {
  59. //partition = polygons.front();
  60. std::list<polygon> front_polygons;
  61. std::list<polygon> back_polygons;
  62. // Classify all polygons relative to this node's partitioning plane
  63. for (const polygon& p: polygons)
  64. {
  65. polygon_classification classification = classify_polygon(partition, p);
  66. switch (classification)
  67. {
  68. case polygon_classification::coplanar:
  69. coplanar_polygons.push_back(p);
  70. break;
  71. case polygon_classification::front:
  72. front_polygons.push_back(p);
  73. break;
  74. case polygon_classification::back:
  75. back_polygons.push_back(p);
  76. break;
  77. case polygon_classification::spanning:
  78. break;
  79. }
  80. }
  81. if (!front_polygons.empty())
  82. {
  83. // Make subtree containing all polygons in front of this node's plane
  84. front = new bsp_tree(front_polygons);
  85. }
  86. if (!back_polygons.empty())
  87. {
  88. // Make subtree containing all polygons behind this node's plane
  89. back = new bsp_tree(back_polygons);
  90. }
  91. }
  92. bsp_tree::~bsp_tree()
  93. {
  94. delete front;
  95. delete back;
  96. }
  97. } // namespace csg
  98. } // namespace geom