Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Spline.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SKYFIRESERVER_SPLINE_H
7#define SKYFIRESERVER_SPLINE_H
8
9#include "MovementTypedefs.h"
10#include <G3D/Vector3.h>
11#include <limits>
12
13namespace Movement
14{
16 {
17 public:
18 typedef int index_type;
19 typedef std::vector<Vector3> ControlArray;
20
29
30 protected:
32
35
37 bool cyclic;
38
39 enum {
40 // could be modified, affects segment length evaluation precision
41 // lesser value saves more performance in cost of lover precision
42 // minimal value is 1
43 // client's value is 20, blizzs use 2-3 steps to compute length
45 };
46 static_assert(STEPS_PER_SEGMENT > 0, "STEPS_PER_SEGMENT shouldn't be lesser than 1");
47
48 protected:
49 void EvaluateLinear(index_type, float, Vector3&) const;
50 void EvaluateCatmullRom(index_type, float, Vector3&) const;
51 void EvaluateBezier3(index_type, float, Vector3&) const;
52 typedef void (SplineBase::* EvaluationMethtod)(index_type, float, Vector3&) const;
54
55 void EvaluateDerivativeLinear(index_type, float, Vector3&) const;
56 void EvaluateDerivativeCatmullRom(index_type, float, Vector3&) const;
57 void EvaluateDerivativeBezier3(index_type, float, Vector3&) const;
59
60 float SegLengthLinear(index_type) const;
62 float SegLengthBezier3(index_type) const;
63 typedef float (SplineBase::* SegLenghtMethtod)(index_type) const;
65
66 void InitLinear(const Vector3*, index_type, bool, index_type);
67 void InitCatmullRom(const Vector3*, index_type, bool, index_type);
68 void InitBezier3(const Vector3*, index_type, bool, index_type);
69 typedef void (SplineBase::* InitMethtod)(const Vector3*, index_type, bool, index_type);
71
72 void UninitializedSpline() const { ASSERT(false); }
73
74 public:
75 explicit SplineBase() : index_lo(0), index_hi(0), m_mode(UninitializedMode), cyclic(false) { }
76
81 void evaluate_percent(index_type Idx, float u, Vector3& c) const { (this->*evaluators[m_mode])(Idx, u, c); }
82
87 void evaluate_derivative(index_type Idx, float u, Vector3& hermite) const { (this->*derivative_evaluators[m_mode])(Idx, u, hermite); }
88
90 index_type first() const { return index_lo; }
91 index_type last() const { return index_hi; }
92
93 bool empty() const { return index_lo == index_hi; }
95 bool isCyclic() const { return cyclic; }
96
97 const ControlArray& getPoints() const { return points; }
98 index_type getPointCount() const { return points.size(); }
99 const Vector3& getPoint(index_type i) const { return points[i]; }
100
102 void init_spline(const Vector3* controls, index_type count, EvaluationMode m);
103 void init_cyclic_spline(const Vector3* controls, index_type count, EvaluationMode m, index_type cyclic_point);
104
107 template<class Init> inline void init_spline(Init& initializer)
108 {
109 initializer(m_mode, cyclic, points, index_lo, index_hi);
110 }
111
112 void clear();
113
115 float SegLength(index_type i) const { return (this->*seglengths[m_mode])(i); }
116
117 std::string ToString() const;
118 };
119
120 template<typename length_type>
121 class Spline : public SplineBase
122 {
123 public:
124 typedef length_type LengthType;
125 typedef std::vector<length_type> LengthArray;
126
127 protected:
130
131 public:
132 explicit Spline() { }
133
136 void evaluate_percent(float t, Vector3& c) const;
137
140 void evaluate_derivative(float t, Vector3& hermite) const;
141
145 void evaluate_percent(index_type Idx, float u, Vector3& c) const { SplineBase::evaluate_percent(Idx, u, c); }
146
150 void evaluate_derivative(index_type Idx, float u, Vector3& c) const { SplineBase::evaluate_derivative(Idx, u, c); }
151
152 // Assumes that t in range [0, 1]
154 void computeIndex(float t, index_type& out_idx, float& out_u) const;
155
157 void init_spline(const Vector3* controls, index_type count, EvaluationMode m) { SplineBase::init_spline(controls, count, m); }
158 void init_cyclic_spline(const Vector3* controls, index_type count, EvaluationMode m, index_type cyclic_point) { SplineBase::init_cyclic_spline(controls, count, m, cyclic_point); }
159
162
165 template<class T> inline void initLengths(T& cacher)
166 {
168 lengths.resize(index_hi + 1);
169 length_type prev_length = 0, new_length = 0;
170 while (i < index_hi)
171 {
172 new_length = cacher(*this, i);
173 // length overflowed, assign to max positive value
174 if (new_length < 0)
175 new_length = std::numeric_limits<length_type>::max();
176 lengths[++i] = new_length;
177
178 ASSERT(prev_length <= new_length);
179 prev_length = new_length;
180 }
181 }
182
184 length_type length() const { return lengths[index_hi]; }
186 length_type length(index_type first, index_type last) const { return lengths[last] - lengths[first]; }
187 length_type length(index_type Idx) const { return lengths[Idx]; }
188
189 void set_length(index_type i, length_type length) { lengths[i] = length; }
190 void clear();
191 };
192
193}
194
195#include "SplineImpl.h"
196
197#endif // SKYFIRESERVER_SPLINE_H
std::uint8_t uint8
Definition Define.h:79
#define ASSERT
Definition Errors.h:29
bool isCyclic() const
Definition Spline.h:95
std::vector< Vector3 > ControlArray
Definition Spline.h:19
float SegLengthLinear(index_type) const
Definition Spline.cpp:140
void UninitializedSpline() const
Definition Spline.h:72
const Vector3 & getPoint(index_type i) const
Definition Spline.h:99
static InitMethtod initializers[ModesEnd]
Definition Spline.h:70
ControlArray points
Definition Spline.h:31
void EvaluateCatmullRom(index_type, float, Vector3 &) const
Definition Spline.cpp:108
void EvaluateDerivativeCatmullRom(index_type, float, Vector3 &) const
Definition Spline.cpp:127
void evaluate_percent(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:81
void evaluate_derivative(index_type Idx, float u, Vector3 &hermite) const
Definition Spline.h:87
void EvaluateBezier3(index_type, float, Vector3 &) const
Definition Spline.cpp:114
void(SplineBase::* InitMethtod)(const Vector3 *, index_type, bool, index_type)
Definition Spline.h:69
float SegLengthBezier3(index_type) const
Definition Spline.cpp:166
float SegLengthCatmullRom(index_type) const
Definition Spline.cpp:146
bool empty() const
Definition Spline.h:93
void InitLinear(const Vector3 *, index_type, bool, index_type)
Definition Spline.cpp:205
float SegLength(index_type i) const
Definition Spline.h:115
void init_spline(Init &initializer)
Definition Spline.h:107
index_type first() const
Definition Spline.h:90
void EvaluateDerivativeBezier3(index_type, float, Vector3 &) const
Definition Spline.cpp:133
void EvaluateDerivativeLinear(index_type, float, Vector3 &) const
Definition Spline.cpp:121
void init_spline(const Vector3 *controls, index_type count, EvaluationMode m)
Definition Spline.cpp:189
void(SplineBase::* EvaluationMethtod)(index_type, float, Vector3 &) const
Definition Spline.h:52
const ControlArray & getPoints() const
Definition Spline.h:97
void EvaluateLinear(index_type, float, Vector3 &) const
Definition Spline.cpp:102
index_type last() const
Definition Spline.h:91
index_type index_hi
Definition Spline.h:34
std::string ToString() const
Definition Spline.cpp:278
void InitBezier3(const Vector3 *, index_type, bool, index_type)
Definition Spline.cpp:258
float(SplineBase::* SegLenghtMethtod)(index_type) const
Definition Spline.h:63
void init_cyclic_spline(const Vector3 *controls, index_type count, EvaluationMode m, index_type cyclic_point)
Definition Spline.cpp:197
void InitCatmullRom(const Vector3 *, index_type, bool, index_type)
Definition Spline.cpp:225
static EvaluationMethtod derivative_evaluators[ModesEnd]
Definition Spline.h:58
static SegLenghtMethtod seglengths[ModesEnd]
Definition Spline.h:64
index_type getPointCount() const
Definition Spline.h:98
index_type index_lo
Definition Spline.h:33
static EvaluationMethtod evaluators[ModesEnd]
Definition Spline.h:53
EvaluationMode mode() const
Definition Spline.h:94
void init_spline(const Vector3 *controls, index_type count, EvaluationMode m)
Definition Spline.h:157
std::vector< double > LengthArray
Definition Spline.h:125
void evaluate_percent(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:145
void init_cyclic_spline(const Vector3 *controls, index_type count, EvaluationMode m, index_type cyclic_point)
Definition Spline.h:158
length_type length(index_type first, index_type last) const
Definition Spline.h:186
void computeIndex(float t, index_type &out_idx, float &out_u) const
Definition SplineImpl.h:51
void initLengths(T &cacher)
Definition Spline.h:165
length_type length(index_type Idx) const
Definition Spline.h:187
void set_length(index_type i, length_type length)
Definition Spline.h:189
index_type computeIndexInBounds(length_type length) const
Definition SplineImpl.h:24
index_type computeIndexInBounds(float t) const
Definition SplineImpl.h:60
double length() const
Definition Spline.h:184
void evaluate_derivative(float t, Vector3 &hermite) const
Definition SplineImpl.h:16
void evaluate_percent(float t, Vector3 &c) const
Definition SplineImpl.h:8
void evaluate_derivative(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:150