Toast Engine
Loading...
Searching...
No Matches
curve.hpp
Go to the documentation of this file.
1
7
8#pragma once
9#include "core_types.hpp"
10
11#include <glm/glm.hpp>
12#include <tinysplinecxx.h>
13
14namespace assets {
15
16enum class SplineType : uint8_t {
17 linear,
21};
22
23enum class CurveDimension : uint8_t {
24 d1 = 1,
25 d2 = 2,
26 d3 = 3,
27};
28
29class TOAST_API Curve : public Asset, public ISaveable {
30public:
31 using Points = std::vector<float>;
32
33 Curve(std::vector<Points> channels, SplineType type, float t_scale = 1.0f);
34
35 [[nodiscard]]
36 static auto fromToml(const toml::table& tbl) -> std::unique_ptr<Curve>;
37
38 [[nodiscard]]
39 auto type() const -> std::string_view override {
40 return "curve";
41 }
42
43 [[nodiscard]]
44 auto serialize(SaveMode mode) const -> std::vector<uint8_t> override;
45
46 [[nodiscard]]
47 auto eval1D(float t) const -> float;
48
49 [[nodiscard]]
50 auto eval2D(float t) const -> glm::vec2;
51
52 [[nodiscard]]
53 auto eval3D(float t) const -> glm::vec3;
54
55 [[nodiscard]]
56 auto evalChannel(size_t channel, float t) const -> float;
57
58 [[nodiscard]]
59 auto evalAtX(float x) const -> float {
60 return sampleChannel(0, x);
61 }
62
63 [[nodiscard]]
64 auto xRange() const noexcept -> glm::vec2;
65
66 [[nodiscard]]
67 auto dimension() const noexcept -> CurveDimension {
68 return static_cast<CurveDimension>(m_channels.size());
69 }
70
71 [[nodiscard]]
72 auto splineType() const noexcept -> SplineType {
73 return m_spline_type;
74 }
75
76 [[nodiscard]]
77 auto tScale() const noexcept -> float {
78 return m_t_scale;
79 }
80
81 [[nodiscard]]
82 auto numPoints(size_t channel = 0) const noexcept -> size_t {
83 return channel < m_channels.size() ? m_channels[channel].points.size() / 2 : 0;
84 }
85
86 void setPoints(size_t channel, Points points);
87
89 void reload(const toml::table& tbl);
90
91private:
92 struct Channel {
93 Points points;
94 tinyspline::BSpline spline;
95 };
96
98 [[nodiscard]]
99 auto sampleChannel(size_t channel, float x) const -> float;
100
101 void rebuildSpline(Channel& channel) const;
102
103 std::vector<Channel> m_channels;
104 SplineType m_spline_type;
105 float m_t_scale = 1.0f;
106};
107
108}
void reload(const toml::table &tbl)
Throws and keeps the old curve when the table is not a curve, nothing may sample it while this runs.
Definition curve.cpp:163
Inherit from this class to allow an asset to be saved.
Definition core_types.hpp:33
Base types for the asset system.
SaveMode
Definition core_types.hpp:25
SplineType
Definition curve.hpp:16
@ bspline
approximates central points
Definition curve.hpp:19
@ bezier
bezier
Definition curve.hpp:20
@ catmull_rom
automatic tangents
Definition curve.hpp:18