💿🐜 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.

115 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 csg {
  22. enum class polygon_classification
  23. {
  24. coplanar,
  25. front,
  26. back,
  27. spanning
  28. };
  29. /**
  30. * Classifies a polygon relative to a partitioning plane.
  31. *
  32. * @param partition Partitioning plane relative to which the polygon should be classified.
  33. * @param poly Polygon to be classified.
  34. * @return Classification of the polygon, relative to the plane.
  35. */
  36. static polygon_classification classify_polygon(const plane& partition, const polygon& poly)
  37. {
  38. for (const float3& vertex: poly.vertices)
  39. {
  40. }
  41. return polygon_classification::coplanar;
  42. }
  43. /**
  44. * Splits a polygon along a partitioning plane.
  45. *
  46. * @param poly Polygon to split.
  47. * @param partition Partitioning plane along which the polygon should be split.
  48. * @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.
  49. */
  50. std::list<std::tuple<polygon, polygon_classification>> split_polygon(const polygon& poly, const plane& partition)
  51. {
  52. return {};
  53. }
  54. bsp_tree::bsp_tree(const std::list<polygon>& polygons):
  55. front(nullptr),
  56. back(nullptr)
  57. {
  58. //partition = polygons.front();
  59. std::list<polygon> front_polygons;
  60. std::list<polygon> back_polygons;
  61. // Classify all polygons relative to this node's partitioning plane
  62. for (const polygon& p: polygons)
  63. {
  64. polygon_classification classification = classify_polygon(partition, p);
  65. switch (classification)
  66. {
  67. case polygon_classification::coplanar:
  68. coplanar_polygons.push_back(p);
  69. break;
  70. case polygon_classification::front:
  71. front_polygons.push_back(p);
  72. break;
  73. case polygon_classification::back:
  74. back_polygons.push_back(p);
  75. break;
  76. case polygon_classification::spanning:
  77. break;
  78. }
  79. }
  80. if (!front_polygons.empty())
  81. {
  82. // Make subtree containing all polygons in front of this node's plane
  83. front = new bsp_tree(front_polygons);
  84. }
  85. if (!back_polygons.empty())
  86. {
  87. // Make subtree containing all polygons behind this node's plane
  88. back = new bsp_tree(back_polygons);
  89. }
  90. }
  91. bsp_tree::~bsp_tree()
  92. {
  93. delete front;
  94. delete back;
  95. }
  96. } // namespace csg