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

181 lines
4.3 KiB

  1. /*
  2. * Copyright (C) 2020 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_MESH_HPP
  20. #define ANTKEEPER_MESH_HPP
  21. #include <vector>
  22. #include "utility/fundamental-types.hpp"
  23. /**
  24. * Half-edge mesh.
  25. *
  26. * @see http://kaba.hilvi.org/homepage/blog/halfedge/halfedge.htm
  27. */
  28. class mesh
  29. {
  30. public:
  31. struct vertex;
  32. struct edge;
  33. struct face;
  34. typedef std::vector<edge*> loop;
  35. /**
  36. * Creates a mesh.
  37. */
  38. mesh() = default;
  39. /**
  40. * Destroys a mesh.
  41. */
  42. ~mesh();
  43. /**
  44. * Adds a vertex to the mesh. This vertex initially has a null edge.
  45. *
  46. * @param position Position of the vertex.
  47. * @return Pointer to the added vertex.
  48. */
  49. mesh::vertex* add_vertex(const float3& position);
  50. /**
  51. * Adds an edge to the mesh.
  52. *
  53. * @param a The vertex from which the edge originates.
  54. * @param b The vertex at which the edge ends.
  55. * @return Pointer to the added edge.
  56. */
  57. mesh::edge* add_edge(mesh::vertex* a, mesh::vertex* b);
  58. /**
  59. * Adds a face to the mesh.
  60. *
  61. * @param loop List of edges which form the face.
  62. * @return Pointer to the added face.
  63. */
  64. mesh::face* add_face(const loop& loop);
  65. /**
  66. * Removes a face from the mesh.
  67. *
  68. * @param face Face to be removed. This face will be deallocated after removal.
  69. */
  70. void remove_face(mesh::face* face);
  71. /**
  72. * Removes an edge and all dependent faces from the mesh.
  73. *
  74. * @param edge Edge to be removed. This edge will be deallocated after removal.
  75. */
  76. void remove_edge(mesh::edge* edge);
  77. /**
  78. * Removes a vertex, all dependent edges, and all dependent faces from the mesh.
  79. *
  80. * @param vertex Vertex to be removed. This vertex will be deallocated after removal.
  81. */
  82. void remove_vertex(mesh::vertex* vertex);
  83. /// Returns the mesh vertices
  84. const std::vector<mesh::vertex*>& get_vertices() const;
  85. /// Returns the mesh edges
  86. const std::vector<mesh::edge*>& get_edges() const;
  87. /// Returns the mesh faces
  88. const std::vector<mesh::face*>& get_faces() const;
  89. /**
  90. * Half-edge vertex which contains a pointer to its parent edge, a position vector, and an index.
  91. */
  92. struct vertex
  93. {
  94. /// Pointer to the edge to which this vertex belongs
  95. mesh::edge* edge;
  96. /// Vertex position
  97. float3 position;
  98. /// Index of this vertex
  99. std::size_t index;
  100. };
  101. /**
  102. * Half-edge edge which contains pointers to its starting vertex, parent face, and related edges.
  103. */
  104. struct edge
  105. {
  106. /// Pointer to the vertex at which the edge starts
  107. mesh::vertex* vertex;
  108. /// Pointer to the face on the left of this edge
  109. mesh::face* face;
  110. /// Pointer to the previous edge in the parent face
  111. mesh::edge* previous;
  112. /// Pointer to the next edge in the parent face
  113. mesh::edge* next;
  114. /// Pointer to the symmetric edge
  115. mesh::edge* symmetric;
  116. /// Index of this edge
  117. std::size_t index;
  118. };
  119. /**
  120. * Half-edge face which contains a pointer to its first edge and its normal vector.
  121. */
  122. struct face
  123. {
  124. /// Pointer to the first edge in this face
  125. mesh::edge* edge;
  126. /// Index of this face
  127. std::size_t index;
  128. };
  129. private:
  130. mesh::edge* find_free_incident(mesh::vertex* vertex) const;
  131. mesh::edge* find_free_incident(mesh::edge* start_edge, mesh::edge* end_edge) const;
  132. bool make_adjacent(mesh::edge* in_edge, mesh::edge* out_edge);
  133. std::vector<mesh::vertex*> vertices;
  134. std::vector<mesh::edge*> edges;
  135. std::vector<mesh::face*> faces;
  136. };
  137. inline const std::vector<mesh::vertex*>& mesh::get_vertices() const
  138. {
  139. return vertices;
  140. }
  141. inline const std::vector<mesh::edge*>& mesh::get_edges() const
  142. {
  143. return edges;
  144. }
  145. inline const std::vector<mesh::face*>& mesh::get_faces() const
  146. {
  147. return faces;
  148. }
  149. #endif // ANTKEEPER_MESH_HPP