CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
SubdivideShape.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 "ShapeVertex.hpp"
12#include "Utility.hpp"
13
15
17
19 template<typename Shape> class SubdivideShape {
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>(mShape->mVertexCache.size()) + i_ / 2};
30
31 return Edge{static_cast<int>(mShape->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 SubdivideShape& shape) : mShape{&shape}, mEdges{shape.mShape.edges()}, i_{0} {}
48
49 friend class SubdivideShape;
50 };
51
52 class Vertices {
53 public:
54 [[nodiscard]] bool done() const noexcept { return vertexIndex_ == mShape->mVertexCache.size() && mEdges.done(); }
55
59
60 const Edge edge = mEdges.generate();
61 const ShapeVertex& v1 = mShape->mVertexCache[edge.vertices[0]];
62 const ShapeVertex& v2 = mShape->mVertexCache[edge.vertices[1]];
63
64 ShapeVertex vertex;
65 vertex.position = mix(v1.position, v2.position, 0.5);
66 vertex.tangent = normalize(mix(v1.tangent, v2.tangent, 0.5));
67 vertex.texCoord = 0.5 * v1.texCoord + 0.5 * v2.texCoord;
68 return vertex;
69 }
70
71 void next() {
74 else
75 mEdges.next();
76 }
77
78 private:
80
82
84
85 Vertices(const SubdivideShape& shape) : mShape{&shape}, vertexIndex_{0}, mEdges{shape.mShape.edges()} {}
86
87 friend class SubdivideShape;
88 };
89
91 for(const ShapeVertex& vertex : mShape.vertices()) {
92 mVertexCache.push_back(vertex);
93 }
94 }
95
96 [[nodiscard]] Edges edges() const noexcept { return *this; }
97
98 [[nodiscard]] Vertices vertices() const noexcept { return *this; }
99
100 private:
102
104 };
105
106} // namespace CeresEngine::MeshGenerator
decltype(std::declval< const Primitive * >() ->edges()) Type
Definition Utility.hpp:25
Definition Edge.hpp:14
Vector2i vertices
Definition Edge.hpp:16
A point on a path.
Definition ShapeVertex.hpp:16
Vector2 position
Definition ShapeVertex.hpp:18
Vector2 tangent
Unit length vector parallel to the shape at this point.
Definition ShapeVertex.hpp:22
double texCoord
Definition ShapeVertex.hpp:24
bool done() const noexcept
Definition SubdivideShape.hpp:23
Edge generate() const
Definition SubdivideShape.hpp:25
void next()
Definition SubdivideShape.hpp:34
Edges(const SubdivideShape &shape)
Definition SubdivideShape.hpp:47
const SubdivideShape * mShape
Definition SubdivideShape.hpp:41
int i_
Definition SubdivideShape.hpp:45
EdgeGeneratorType< Shape >::Type mEdges
Definition SubdivideShape.hpp:43
ShapeVertex generate() const
Definition SubdivideShape.hpp:56
int vertexIndex_
Definition SubdivideShape.hpp:81
bool done() const noexcept
Definition SubdivideShape.hpp:54
Vertices(const SubdivideShape &shape)
Definition SubdivideShape.hpp:85
void next()
Definition SubdivideShape.hpp:71
EdgeGeneratorType< Shape >::Type mEdges
Definition SubdivideShape.hpp:83
const SubdivideShape * mShape
Definition SubdivideShape.hpp:79
Cuts each edge in half.
Definition SubdivideShape.hpp:19
Vector< ShapeVertex > mVertexCache
Definition SubdivideShape.hpp:103
Edges edges() const noexcept
Definition SubdivideShape.hpp:96
Vertices vertices() const noexcept
Definition SubdivideShape.hpp:98
Shape mShape
Definition SubdivideShape.hpp:101
SubdivideShape(Shape shape)
Definition SubdivideShape.hpp:90
A type that describes a conjunction of shapes that can be filled and stroked.
Definition Shape.hpp:198
Definition AnyGenerator.hpp:12
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