Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Ramp.hpp
1// INFINITY_API_PUBLIC
2
3#pragma once
4
5#include <Infinity/Types/Core/Data.hpp>
6#include <Infinity/Types/Math/Stop.hpp>
7
8#include <vector>
9#include <algorithm>
10
12{
21 enum class INFINITY_API_TEMPLATE Interpolation
22 {
23 Linear,
24 Cubic
25 };
26
88 template<typename T>
89 class INFINITY_API_TEMPLATE Ramp : public Core::Data
90 {
91 public:
99 std::vector<Stop<T>> stops;
100
108 Interpolation interpolation = Interpolation::Linear;
109
115 Ramp() = default;
116
117 std::unique_ptr<Core::Base> clone() const override
118 {
119 return std::make_unique<Ramp<T>>(*this);
120 }
121
140 Ramp(std::vector<Stop<T>> inputStops, Interpolation interp = Interpolation::Linear) : interpolation(interp)
141 {
142 stops = std::move(inputStops);
143 std::sort(stops.begin(), stops.end(), [](const Stop<T>& a, const Stop<T>& b) {
144 return a.position < b.position;
145 });
146 }
147
157 bool operator==(const Ramp<T>& other) const
158 {
159 return stops == other.stops && interpolation == other.interpolation;
160 }
161
168 bool operator!=(const Ramp<T>& other) const
169 {
170 return !(*this == other);
171 }
172
178 const Infinity::Types::TypeID& typeId() const override { return Infinity::Types::getTypeID<Ramp<T>>(); }
179
205 T sample(float position) const
206 {
207 if (stops.empty())
208 {
209 return T(); // Default constructed value
210 }
211
212 if (position <= stops.front().position)
213 {
214 return stops.front().value;
215 }
216
217 if (position >= stops.back().position)
218 {
219 return stops.back().value;
220 }
221
222 for (size_t i = 0; i < stops.size() - 1; ++i)
223 {
224 const auto& a = stops[i];
225 const auto& b = stops[i + 1];
226
227 if (position >= a.position && position <= b.position)
228 {
229 float t = (position - a.position) / (b.position - a.position);
230 return (interpolation == Interpolation::Linear)
231 ? lerp(a.value, b.value, t)
232 : cubicInterp(a.value, b.value, t);
233 }
234 }
235
236 return T(); // this should never happen
237 }
238
239 private:
250 T lerp(const T& a, const T& b, float t) const
251 {
252 return a + (b - a) * t;
253 }
254
267 T cubicInterp(const T& a, const T& b, float t) const {
268 float t2 = t * t;
269 float t3 = t2 * t;
270 return a * (2.0f * t3 - 3.0f * t2 + 1.0f) +
271 b * (-2.0f * t3 + 3.0f * t2);
272 }
273 };
274
286
297
309
320}
321
322// Register Ramp specializations in the type system
Base class for complex data types with memory wrapping and property support.
Definition Data.hpp:49
Interpolated gradient for smoothly transitioning between values.
Definition Ramp.hpp:90
std::unique_ptr< Core::Base > clone() const override
Definition Ramp.hpp:117
const Infinity::Types::TypeID & typeId() const override
Gets the runtime type identifier for this Ramp<T> specialization.
Definition Ramp.hpp:178
T sample(float position) const
Samples the ramp at a given position.
Definition Ramp.hpp:205
std::vector< Stop< T > > stops
Collection of control points defining the ramp.
Definition Ramp.hpp:99
bool operator!=(const Ramp< T > &other) const
Inequality comparison operator.
Definition Ramp.hpp:168
Interpolation interpolation
Interpolation method used between stops.
Definition Ramp.hpp:108
Ramp(std::vector< Stop< T > > inputStops, Interpolation interp=Interpolation::Linear)
Constructs a ramp from a vector of stops.
Definition Ramp.hpp:140
bool operator==(const Ramp< T > &other) const
Equality comparison operator.
Definition Ramp.hpp:157
Ramp()=default
Default constructor.
Definition Math.hpp:9
Vector3 INFINITY_API_PUBLIC lerp(const Vector3 &a, const Vector3 &b, float t)
Linear interpolation between two 3D vectors.
Represents a control point in a gradient or interpolation ramp.
Definition Stop.hpp:64
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71