Unravel Engine C++ Reference
Loading...
Searching...
No Matches
subdivide_path.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_SUBDIVIDEPATH_HPP
2#define GENERATOR_SUBDIVIDEPATH_HPP
3
4#include "edge.hpp"
5#include "path_vertex.hpp"
6#include "shape_vertex.hpp"
7#include "utils.hpp"
8
9namespace generator
10{
11
12template<typename path_t>
14{
15public:
16 class edges_t
17 {
18 public:
19 bool done() const noexcept
20 {
21 return edges_.done();
22 }
23
25 {
26 edge_t edge_ = edges_.generate();
27
28 if(i_ % 2 == 0)
29 return edge_t{{edge_.vertices[0], static_cast<int>(path_->vertex_cache_.size()) + i_ / 2}};
30
31 return edge_t{{static_cast<int>(path_->vertex_cache_.size()) + i_ / 2, edge_.vertices[1]}};
32 }
33
34 void next()
35 {
36 ++i_;
37 if(i_ % 2 == 0)
38 edges_.next();
39 }
40
41 private:
42 const subdivide_path_t* path_;
43
45
46 int i_;
47
48 explicit edges_t(const subdivide_path_t& path) : path_{&path}, edges_{path.path_.edges()}, i_{0}
49 {
50 }
51
52 friend class subdivide_path_t;
53 };
54
56 {
57 public:
58 bool done() const noexcept
59 {
60 return vertex_index_ == path_->vertex_cache_.size() && edges_.done();
61 }
62
64 {
65 if(vertex_index_ < path_->vertex_cache_.size())
66 return path_->vertex_cache_[vertex_index_];
67
68 const edge_t edge = edges_.generate();
69 const path_vertex_t& v1 = path_->vertex_cache_[edge.vertices[0]];
70 const path_vertex_t& v2 = path_->vertex_cache_[edge.vertices[1]];
71
72 path_vertex_t vertex;
73 vertex.position = gml::mix(v1.position, v2.position, 0.5);
74 vertex.tangent = gml::normalize(gml::mix(v1.tangent, v2.tangent, 0.5));
75 vertex.normal = gml::normalize(gml::mix(v1.normal, v2.normal, 0.5));
76 vertex.tex_coord = 0.5 * v1.tex_coord + 0.5 * v2.tex_coord;
77 return vertex;
78 }
79
80 void next()
81 {
82 if(vertex_index_ < path_->vertex_cache_.size())
83 ++vertex_index_;
84 else
85 edges_.next();
86 }
87
88 private:
89 const subdivide_path_t* path_;
90
91 int vertex_index_;
92
94
95 explicit vertices_t(const subdivide_path_t& path) : path_{&path}, vertex_index_{0}, edges_{path.path_.edges()}
96 {
97 }
98
99 friend class subdivide_path_t;
100 };
101
102 subdivide_path_t(path_t path) : path_(std::move(path)), vertex_cache_{}
103 {
104 for(const path_vertex_t& vertex : path_.vertices())
105 {
106 vertex_cache_.push_back(vertex);
107 }
108 }
109
111 {
112 return edges_t{*this};
113 }
114
116 {
117 return vertices_t{*this};
118 }
119
120private:
121 path_t path_;
122
123 std::vector<path_vertex_t> vertex_cache_;
124};
125
126template<typename path_t>
128{
129 return subdivide_path_t<path_t>{std::move(path)};
130}
131} // namespace generator
132
133#endif
gml::ivec2 vertices
Definition edge.hpp:12
subdivide_path_t< path_t > subdivide_path(path_t path)