OGLplus  (0.59.0) a C++ wrapper for rendering APIs

topology.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SHAPES_TOPOLOGY_HPP
10 #define EAGINE_SHAPES_TOPOLOGY_HPP
11 
12 #include "gen_base.hpp"
13 #include <eagine/config/basic.hpp>
14 #include <eagine/flat_map.hpp>
15 #include <iosfwd>
16 #include <vector>
17 
18 namespace eagine {
19 namespace shapes {
20 //------------------------------------------------------------------------------
21 struct topology_data;
22 class mesh_triangle;
23 
28 class mesh_edge {
29 public:
30  mesh_edge(
31  mesh_triangle& a,
32  std::uint8_t aeb,
33  std::uint8_t aee,
34  mesh_triangle& b,
35  std::uint8_t beb,
36  std::uint8_t bee) noexcept
37  : _triangles{{&a, &b}}
38  , _edge_indices{{aeb, aee, beb, bee}} {}
39 
42  auto triangle(span_size_t i) const noexcept -> const mesh_triangle& {
43  EAGINE_ASSERT(i >= 0 && i < 2);
44  return *_triangles[std_size(i)];
45  }
46 
49  auto edge_vertices(span_size_t i) const noexcept
50  -> std::tuple<unsigned, unsigned> {
51  EAGINE_ASSERT(i >= 0 && i < 2);
52  return {
53  _edge_indices[std_size(2 * i)], _edge_indices[std_size(2 * i + 1)]};
54  }
55 
56 private:
57  std::array<mesh_triangle*, 2> _triangles;
58  std::array<std::uint8_t, 4> _edge_indices;
59 };
60 //------------------------------------------------------------------------------
66 public:
68  std::size_t tri_idx,
69  unsigned a,
70  unsigned b,
71  unsigned c) noexcept
72  : _tri_idx{tri_idx}
73  , _indices{{a, b, c}} {}
74 
76  auto index() const noexcept -> span_size_t {
77  return span_size(_tri_idx);
78  }
79 
80  static auto setup_adjacent(
81  mesh_triangle& l,
82  mesh_triangle& r,
83  const topology_data& d) noexcept
84  -> std::tuple<bool, std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
85 
88  auto vertex_index(span_size_t v) const noexcept {
89  EAGINE_ASSERT(v >= 0 && v < 3);
90  return _indices[std_size(v)];
91  }
92 
96  auto adjacent_triangle(span_size_t v) const noexcept
97  -> const mesh_triangle* {
98  EAGINE_ASSERT(v >= 0 && v < 3);
99  return _adjacent[std_size(v)];
100  }
101 
106  auto opposite_vertex(span_size_t v) const noexcept -> unsigned {
107  EAGINE_ASSERT(v >= 0 && v < 3);
108  return _opposite[std_size(v)];
109  }
110 
115  auto opposite_index(span_size_t v) const noexcept -> unsigned {
116  if(auto tri{adjacent_triangle(v)}) {
117  return extract(tri).vertex_index(opposite_vertex(v));
118  }
119  return _indices[(v + 2) % 3];
120  }
121 
122 private:
123  auto curri(std::size_t i) const noexcept -> unsigned {
124  return _indices[i];
125  }
126 
127  static auto prevv(std::size_t i) noexcept -> unsigned {
128  return (i + 2) % 3;
129  }
130 
131  auto previ(std::size_t i) const noexcept -> unsigned {
132  return _indices[prevv(i)];
133  }
134 
135  static auto nextv(std::size_t i) noexcept -> unsigned {
136  return (i + 1) % 3;
137  }
138 
139  auto nexti(std::size_t i) const noexcept -> unsigned {
140  return _indices[nextv(i)];
141  }
142 
143  static auto narrow(std::size_t i) noexcept -> std::uint8_t {
144  return limit_cast<std::uint8_t>(i);
145  }
146 
147  std::size_t _tri_idx{~0U};
148  std::array<unsigned, 3> _indices{};
149  std::array<mesh_triangle*, 3> _adjacent{{nullptr, nullptr, nullptr}};
150  std::array<std::uint8_t, 3> _opposite{{0, 0, 0}};
151 };
152 //------------------------------------------------------------------------------
157 class topology {
158 public:
161  std::shared_ptr<generator> gen,
162  drawing_variant var,
164  : _gen{std::move(gen)} {
165  _scan_topology(var, vav);
166  }
167 
169  topology(const std::shared_ptr<generator>& gen)
170  : topology{gen, gen->draw_variant(0), {vertex_attrib_kind::position}} {}
171 
173  auto triangle_count() const noexcept -> span_size_t {
174  return span_size(_triangles.size());
175  }
176 
178  auto triangle(span_size_t i) const noexcept -> const mesh_triangle& {
179  return _triangles[std_size(i)];
180  }
181 
182  auto print_dot(std::ostream& out) const -> std::ostream&;
183 
184 private:
185  template <typename I>
186  static auto to_index(I i) noexcept
187  -> std::enable_if_t<std::is_integral_v<I>, unsigned> {
188  return limit_cast<unsigned>(i);
189  }
190 
191  void _scan_topology(drawing_variant var, vertex_attrib_variant vav);
192 
193  std::shared_ptr<generator> _gen;
194  std::vector<mesh_triangle> _triangles;
195  flat_map<std::tuple<unsigned, unsigned>, mesh_edge> _edges;
196 };
197 //------------------------------------------------------------------------------
198 } // namespace shapes
199 } // namespace eagine
200 
201 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
202 #include <eagine/shapes/topology.inl>
203 #endif
204 
205 #endif // EAGINE_SHAPES_TOPOLOGY_HPP
auto edge_vertices(span_size_t i) const noexcept -> std::tuple< unsigned, unsigned >
Returns a pair of vertex indices (0,1 or 2) defining the i-th edge.
Definition: topology.hpp:49
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
Class storing information about a mesh tirangular face.
Definition: topology.hpp:65
auto opposite_vertex(span_size_t v) const noexcept -> unsigned
Returns opposite vertex index (0,1 or 2) in the v-th adjacent triangle.
Definition: topology.hpp:106
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Class holding information about the topology of a generated shape.
Definition: topology.hpp:157
Class designating an vertex attribute variant in a shape generator.
Definition: vertex_attrib.hpp:104
auto opposite_index(span_size_t v) const noexcept -> unsigned
Returns the in-mesh index of the v-th adjacent vertex.
Definition: topology.hpp:115
auto vertex_index(span_size_t v) const noexcept
Returns the v-th vertex index.
Definition: topology.hpp:88
Class storing information about an edge between two mesh faces.
Definition: topology.hpp:28
auto triangle(span_size_t i) const noexcept -> const mesh_triangle &
Returns one of the two adjacent triangle faces.
Definition: topology.hpp:42
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
span_size_t drawing_variant
Alias for shape drawing variant index type.
Definition: gen_base.hpp:31
topology(std::shared_ptr< generator > gen, drawing_variant var, vertex_attrib_variant vav)
Construction from a generator, drawing and attribute variant.
Definition: topology.hpp:160
auto triangle(span_size_t i) const noexcept -> const mesh_triangle &
Returns the i-th triangle in the mesh.
Definition: topology.hpp:178
auto adjacent_triangle(span_size_t v) const noexcept -> const mesh_triangle *
Returns the triangle adjacent through the v-th edge.
Definition: topology.hpp:96
auto triangle_count() const noexcept -> span_size_t
Returns the number of triangles in the mesh.
Definition: topology.hpp:173
auto index() const noexcept -> span_size_t
Returns the index of this triangle within the mesh.
Definition: topology.hpp:76
topology(const std::shared_ptr< generator > &gen)
Construction from a shape generator.
Definition: topology.hpp:169

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).