CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
BezierShape.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 "ParametricShape.hpp"
11
12#include <algorithm>
13#include <limits>
14
16
20 template<int D> class BezierShape {
21 private:
22 static_assert(D > 1, "D must be > 1.");
23
26
27 struct ArrayWrapper {
29
30 ArrayWrapper(const Vector2 (&p)[D]) { std::copy(&p[0], &p[0] + D, &data[0]); }
31 };
32
33 explicit BezierShape(const ArrayWrapper& p, int segments)
35 ShapeVertex vertex;
36
37 vertex.position = bezier(p.data, t);
38 vertex.tangent = bezierDerivative<1>(p.data, t);
39
40 // If tangent is zero try again near by.
41 const double e = std::numeric_limits<double>::epsilon();
42 if(dot(vertex.tangent, vertex.tangent) < e) {
43 vertex.tangent = bezierDerivative<1>(p.data, t + 10.0 * e);
44 }
45
46 vertex.tangent = normalize(vertex.tangent);
47 vertex.texCoord = t;
48
49 return vertex;
50 },
51 segments} {}
52
53 public:
56 explicit BezierShape(const Vector2 (&p)[D], int segments = 16)
57 : // Work around a msvc lambda capture bug by wrapping the array.
59
60 using Edges = typename Impl::Edges;
61 [[nodiscard]] Edges edges() const noexcept { return mParametricShape.edges(); }
62
63 using Vertices = typename Impl::Vertices;
64 [[nodiscard]] Vertices vertices() const noexcept { return mParametricShape.vertices(); }
65 };
66
67} // namespace CeresEngine::MeshGenerator
A bezier curve with D control points.
Definition BezierShape.hpp:20
Impl mParametricShape
Definition BezierShape.hpp:25
Edges edges() const noexcept
Definition BezierShape.hpp:61
typename Impl::Edges Edges
Definition BezierShape.hpp:60
typename Impl::Vertices Vertices
Definition BezierShape.hpp:63
BezierShape(const Vector2(&p)[D], int segments=16)
Definition BezierShape.hpp:56
BezierShape(const ArrayWrapper &p, int segments)
Definition BezierShape.hpp:33
Vertices vertices() const noexcept
Definition BezierShape.hpp:64
A shape with values evaluated using a callback function.
Definition ParametricShape.hpp:18
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
Definition AnyGenerator.hpp:12
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
ArrayWrapper(const Vector2(&p)[D])
Definition BezierShape.hpp:30
Vector2 data[D]
Definition BezierShape.hpp:28