CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
SubdividePath.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Edge.hpp"
11#include "PathVertex.hpp"
12#include "ShapeVertex.hpp"
13#include "Utility.hpp"
14
16
18
19 template<typename Path> class SubdividePath {
20 public:
21 class Edges {
22 public:
23 [[nodiscard]] bool done() const noexcept { return mEdges.done(); }
24
25 [[nodiscard]] Edge generate() const {
26 Edge edge_ = mEdges.generate();
27
28 if(i_ % 2 == 0)
29 return Edge{{edge_.vertices[0], static_cast<int>(mPath->mVertexCache.size()) + i_ / 2}};
30
31 return Edge{{static_cast<int>(mPath->mVertexCache.size()) + i_ / 2, edge_.vertices[1]}};
32 }
33
34 void next() {
35 ++i_;
36 if(i_ % 2 == 0)
37 mEdges.next();
38 }
39
40 private:
42
44
45 int i_;
46
47 Edges(const SubdividePath& path) : mPath{&path}, mEdges{path.mPath.edges()}, i_{0} {}
48
49 friend class SubdividePath;
50 };
51
52 class Vertices {
53 public:
54 [[nodiscard]] bool done() const noexcept { return vertexIndex_ == mPath->mVertexCache.size() && mEdges.done(); }
55
59
60 const Edge edge = mEdges.generate();
61 const PathVertex& v1 = mPath->mVertexCache[edge.vertices[0]];
62 const PathVertex& v2 = mPath->mVertexCache[edge.vertices[1]];
63
64 PathVertex vertex;
65 vertex.position = mix(v1.position, v2.position, 0.5);
66 vertex.tangent = normalize(mix(v1.tangent, v2.tangent, 0.5));
67 vertex.normal = normalize(mix(v1.normal, v2.normal, 0.5));
68 vertex.texCoord = 0.5 * v1.texCoord + 0.5 * v2.texCoord;
69 return vertex;
70 }
71
72 void next() {
75 else
76 mEdges.next();
77 }
78
79 private:
81
83
85
86 Vertices(const SubdividePath& path) : mPath{&path}, vertexIndex_{0}, mEdges{path.mPath.edges()} {}
87
88 friend class SubdividePath;
89 };
90
92 for(const PathVertex& vertex : mPath.vertices()) {
93 mVertexCache.push_back(vertex);
94 }
95 }
96
97 [[nodiscard]] Edges edges() const { return *this; }
98
99 [[nodiscard]] Vertices vertices() const { return *this; }
100
101 private:
103
105 };
106
107 template<typename Path> SubdividePath<Path> subdividePath(Path path) { return SubdividePath<Path>{std::move(path)}; }
108
109} // namespace CeresEngine::MeshGenerator
decltype(std::declval< const Primitive * >() ->edges()) Type
Definition Utility.hpp:25
Definition Edge.hpp:14
Vector2i vertices
Definition Edge.hpp:16
Definition PathVertex.hpp:14
double texCoord
Definition PathVertex.hpp:26
Vector3 normal
Unit length vector perpendicular to the path at this point.
Definition PathVertex.hpp:18
Vector3 tangent
Unit length vector parallel to the path at this point.
Definition PathVertex.hpp:24
Vector3 position
Definition PathVertex.hpp:20
EdgeGeneratorType< Path >::Type mEdges
Definition SubdividePath.hpp:43
Edge generate() const
Definition SubdividePath.hpp:25
Edges(const SubdividePath &path)
Definition SubdividePath.hpp:47
bool done() const noexcept
Definition SubdividePath.hpp:23
const SubdividePath * mPath
Definition SubdividePath.hpp:41
int i_
Definition SubdividePath.hpp:45
void next()
Definition SubdividePath.hpp:34
PathVertex generate() const
Definition SubdividePath.hpp:56
bool done() const noexcept
Definition SubdividePath.hpp:54
void next()
Definition SubdividePath.hpp:72
EdgeGeneratorType< Path >::Type mEdges
Definition SubdividePath.hpp:84
Vertices(const SubdividePath &path)
Definition SubdividePath.hpp:86
const SubdividePath * mPath
Definition SubdividePath.hpp:80
int vertexIndex_
Definition SubdividePath.hpp:82
Definition SubdividePath.hpp:19
Vertices vertices() const
Definition SubdividePath.hpp:99
Edges edges() const
Definition SubdividePath.hpp:97
SubdividePath(Path path)
Definition SubdividePath.hpp:91
Path mPath
Definition SubdividePath.hpp:102
Vector< PathVertex > mVertexCache
Definition SubdividePath.hpp:104
Definition AnyGenerator.hpp:12
SubdividePath< Path > subdividePath(Path path)
Definition SubdividePath.hpp:107
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668