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

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