Skylicht Engine
Loading...
Searching...
No Matches
CInterpolator.h
1/*
2!@
3MIT License
4
5Copyright (c) 2020 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include <set>
28
29namespace Skylicht
30{
39 {
41 float X;
43 float Value[4];
44
50 {
51 X = x;
52 for (int i = 0; i < 4; i++)
53 Value[i] = 0.0f;
54 }
55
60 {
61 X = 0.0f;
62 for (int i = 0; i < 4; i++)
63 Value[i] = 0.0f;
64 }
65
70 void operator=(const SInterpolatorEntry& entry)
71 {
72 X = entry.X;
73 for (int i = 0; i < 4; i++)
74 Value[i] = entry.Value[i];
75 }
76 };
77
81 inline bool operator<(const SInterpolatorEntry& entry0, const SInterpolatorEntry& entry1)
82 {
83 return entry0.X < entry1.X;
84 }
85
89 inline bool operator==(const SInterpolatorEntry& entry0, const SInterpolatorEntry& entry1)
90 {
91 return entry0.X == entry1.X;
92 }
93
99 {
114
116 core::vector2df Position;
118 core::vector2df Left;
120 core::vector2df Right;
123
128 {
129 Type = Auto;
130 }
131 };
132
136 typedef core::array<core::vector2df> ArrayPoint2df;
137
145 class SKYLICHT_API CInterpolator
146 {
147 public:
162
163 protected:
164 std::set<SInterpolatorEntry> m_graph;
165
166 std::vector<SControlPoint> m_controls[4];
167
168 EInterpolatorType m_type;
169
170 public:
175
179 virtual ~CInterpolator();
180
186 {
187 return m_type;
188 }
189
194 inline void setType(EInterpolatorType type)
195 {
196 m_type = type;
197 }
198
203 void operator=(const CInterpolator& other)
204 {
205 m_type = other.m_type;
206 m_graph = other.m_graph;
207 for (int i = 0; i < 4; i++)
208 m_controls[i] = other.m_controls[i];
209 }
210
215 inline bool empty()
216 {
217 return m_graph.empty();
218 }
219
225 void getMinMaxXY(core::vector2df& min, core::vector2df& max);
226
232 float interpolate(float x);
233
239 core::vector2df interpolateVec2(float x);
240
246 core::vector3df interpolateVec3(float x);
247
253 SColorf interpolateColorf(float x);
254
259 inline std::set<SInterpolatorEntry>& getGraph()
260 {
261 return m_graph;
262 }
263
268 inline const std::set<SInterpolatorEntry>& getGraph() const
269 {
270 return m_graph;
271 }
272
278 inline std::vector<SControlPoint>& getControlPoints(int layer)
279 {
280 return m_controls[layer];
281 }
282
288 inline const std::vector<SControlPoint>& getControlPoints(int layer) const
289 {
290 return m_controls[layer];
291 }
292
299
305 inline bool addEntry(const SInterpolatorEntry& entry)
306 {
307 return m_graph.insert(entry).second;
308 }
309
316 inline bool addEntry(float x, float y)
317 {
318 SInterpolatorEntry entry;
319 entry.X = x;
320 entry.Value[0] = y;
321 return addEntry(entry);
322 }
323
331 inline bool addEntry(int layer, float x, float y)
332 {
333 SInterpolatorEntry entry;
334 entry.X = x;
335 entry.Value[layer] = y;
336 return addEntry(entry);
337 }
338
345 inline bool addEntry(float x, const core::vector2df& v)
346 {
347 SInterpolatorEntry entry;
348 entry.X = x;
349 entry.Value[0] = v.X;
350 entry.Value[1] = v.Y;
351 return addEntry(entry);
352 }
353
360 inline bool addEntry(float x, const core::vector3df& v)
361 {
362 SInterpolatorEntry entry;
363 entry.X = x;
364 entry.Value[0] = v.X;
365 entry.Value[1] = v.Y;
366 entry.Value[2] = v.Z;
367 return addEntry(entry);
368 }
369
376 inline bool addEntry(float x, const video::SColorf& c)
377 {
378 SInterpolatorEntry entry;
379 entry.X = x;
380 entry.Value[0] = c.r;
381 entry.Value[1] = c.g;
382 entry.Value[2] = c.b;
383 entry.Value[3] = c.a;
384 return addEntry(entry);
385 }
386
390 inline void clearGraph()
391 {
392 m_graph.clear();
393
394 for (int i = 0; i < 4; i++)
395 m_controls[i].clear();
396 }
397
403 void generateGraph(int layer, int bezierStep);
404
411 void computeLine(int layer, std::vector<ArrayPoint2df>& lines, int bezierStep);
412
420 void computeBezier(const SControlPoint& p1, const SControlPoint& p2, ArrayPoint2df& points, int bezierStep);
421 };
422}
void getMinMaxXY(core::vector2df &min, core::vector2df &max)
Compute the min and max X/Y bounds of the stored graph entries.
bool addEntry(float x, float y)
Add a scalar graph entry.
Definition CInterpolator.h:316
virtual ~CInterpolator()
Destroy the interpolator.
std::vector< SControlPoint > & getControlPoints(int layer)
Get mutable Bezier control points for a value layer.
Definition CInterpolator.h:278
bool addEntry(int layer, float x, float y)
Add a single channel value to a graph entry.
Definition CInterpolator.h:331
const std::vector< SControlPoint > & getControlPoints(int layer) const
Get read-only Bezier control points for a value layer.
Definition CInterpolator.h:288
bool addEntry(float x, const core::vector2df &v)
Add a 2D vector graph entry.
Definition CInterpolator.h:345
SColorf interpolateColorf(float x)
Sample the graph as a floating-point color.
bool addEntry(float x, const core::vector3df &v)
Add a 3D vector graph entry.
Definition CInterpolator.h:360
EInterpolatorType
Value layout used by the interpolator.
Definition CInterpolator.h:152
@ Color
Four float color channels.
Definition CInterpolator.h:160
@ Vector2
Two float channels.
Definition CInterpolator.h:156
@ Vector3
Three float channels.
Definition CInterpolator.h:158
@ Float
One float channel.
Definition CInterpolator.h:154
SControlPoint & addControlPoint(int layer=0)
Add a control point to a value layer.
void generateGraph(int layer, int bezierStep)
Generate graph entries for a layer from its Bezier control points.
const std::set< SInterpolatorEntry > & getGraph() const
Get read-only access to graph entries.
Definition CInterpolator.h:268
void computeLine(int layer, std::vector< ArrayPoint2df > &lines, int bezierStep)
Compute Bezier polyline segments for one layer.
EInterpolatorType getType()
Get the current value layout.
Definition CInterpolator.h:185
void operator=(const CInterpolator &other)
Copy another interpolator.
Definition CInterpolator.h:203
void setType(EInterpolatorType type)
Set the value layout.
Definition CInterpolator.h:194
CInterpolator()
Construct an empty float interpolator.
std::set< SInterpolatorEntry > & getGraph()
Get mutable access to graph entries.
Definition CInterpolator.h:259
bool empty()
Check whether the interpolator has no graph entries.
Definition CInterpolator.h:215
core::vector3df interpolateVec3(float x)
Sample the graph as a 3D vector.
bool addEntry(float x, const video::SColorf &c)
Add a color graph entry.
Definition CInterpolator.h:376
core::vector2df interpolateVec2(float x)
Sample the graph as a 2D vector.
bool addEntry(const SInterpolatorEntry &entry)
Add a raw graph entry.
Definition CInterpolator.h:305
void clearGraph()
Remove all graph entries and control points.
Definition CInterpolator.h:390
float interpolate(float x)
Sample the graph as a scalar value.
void computeBezier(const SControlPoint &p1, const SControlPoint &p2, ArrayPoint2df &points, int bezierStep)
Compute sampled points for the Bezier segment between two control points.
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
core::array< core::vector2df > ArrayPoint2df
Dynamic array of 2D points used for generated curve segments.
Definition CInterpolator.h:136
bool operator<(const SInterpolatorEntry &entry0, const SInterpolatorEntry &entry1)
Sort interpolation entries by their X key.
Definition CInterpolator.h:81
bool operator==(const SInterpolatorEntry &entry0, const SInterpolatorEntry &entry1)
Compare interpolation entries by their X key.
Definition CInterpolator.h:89
Bezier control point used to generate interpolation graph entries.
Definition CInterpolator.h:99
core::vector2df Left
Left tangent offset.
Definition CInterpolator.h:118
EControlType Type
Control point tangent type.
Definition CInterpolator.h:122
core::vector2df Position
Point position.
Definition CInterpolator.h:116
EControlType
Tangent behavior for a control point.
Definition CInterpolator.h:104
@ Linear
Segment is treated as linear.
Definition CInterpolator.h:112
@ Smooth
Tangents remain smooth through the point.
Definition CInterpolator.h:108
@ Auto
Tangents are generated automatically.
Definition CInterpolator.h:106
@ Broken
Left and right tangents may move independently.
Definition CInterpolator.h:110
core::vector2df Right
Right tangent offset.
Definition CInterpolator.h:120
SControlPoint()
Construct an automatic control point.
Definition CInterpolator.h:127
One keyed interpolation value.
Definition CInterpolator.h:39
SInterpolatorEntry(float x)
Construct an entry at a key position with zeroed values.
Definition CInterpolator.h:49
float X
Key position on the X axis.
Definition CInterpolator.h:41
SInterpolatorEntry()
Construct a zero entry.
Definition CInterpolator.h:59
void operator=(const SInterpolatorEntry &entry)
Copy another entry.
Definition CInterpolator.h:70
float Value[4]
Interpolated channel values.
Definition CInterpolator.h:43