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

88 lines
2.0 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. #ifndef ANTKEEPER_GEOM_CSG_HPP
  20. #define ANTKEEPER_GEOM_CSG_HPP
  21. #include "utility/fundamental-types.hpp"
  22. #include <list>
  23. namespace geom {
  24. /// Constructive solid geometry (CSG)
  25. namespace csg {
  26. struct plane
  27. {
  28. float3 normal;
  29. float distance;
  30. };
  31. struct polygon
  32. {
  33. std::list<float3> vertices;
  34. void* shared;
  35. };
  36. /**
  37. * 3D solid represented by a collection of polygons.
  38. */
  39. typedef std::list<polygon> solid;
  40. /**
  41. * BSP tree node.
  42. */
  43. class bsp_tree
  44. {
  45. public:
  46. /**
  47. * Recursively constructs a BSP tree from a collection of polygons.
  48. *
  49. * @param polygons Collection of polygons from which to create the BSP tree.
  50. */
  51. explicit bsp_tree(const std::list<polygon>& polygons);
  52. /**
  53. * Destroys a BSP tree.
  54. */
  55. ~bsp_tree();
  56. private:
  57. /// Partition which separates the front and back polygons.
  58. plane partition;
  59. /// Set of polygons which are coplanar with the partition.
  60. std::list<polygon> coplanar_polygons;
  61. /// Subtree containing all polygons in front of the partition.
  62. bsp_tree* front;
  63. /// Subtree containing all polygons behind the partition.
  64. bsp_tree* back;
  65. };
  66. solid op_union(const solid& a, const solid& b);
  67. solid op_difference(const solid& a, const solid& b);
  68. solid op_intersect(const solid& a, const solid& b);
  69. } // namespace csg
  70. } // namespace geom
  71. #endif // ANTKEEPER_GEOM_CSG_HPP