TrinityCore
Loading...
Searching...
No Matches
Spline.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYSERVER_SPLINE_H
19#define TRINITYSERVER_SPLINE_H
20
21#include "MovementTypedefs.h"
22#include "Errors.h"
23#include <G3D/Vector3.h>
24#include <limits>
25#include <vector>
26
27namespace Movement {
28
30{
31public:
32 typedef int index_type;
33 typedef std::vector<Vector3> ControlArray;
34
43
44protected:
46
49
51 bool cyclic;
53
54 enum{
55 // could be modified, affects segment length evaluation precision
56 // lesser value saves more performance in cost of lover precision
57 // minimal value is 1
58 // client's value is 20, blizzs use 2-3 steps to compute length
60 };
61 static_assert(STEPS_PER_SEGMENT > 0, "STEPS_PER_SEGMENT shouldn't be lesser than 1");
62
63protected:
64 void EvaluateLinear(index_type, float, Vector3&) const;
65 void EvaluateCatmullRom(index_type, float, Vector3&) const;
66 void EvaluateBezier3(index_type, float, Vector3&) const;
67 typedef void (SplineBase::*EvaluationMethtod)(index_type, float, Vector3&) const;
69
70 void EvaluateDerivativeLinear(index_type, float, Vector3&) const;
71 void EvaluateDerivativeCatmullRom(index_type, float, Vector3&) const;
72 void EvaluateDerivativeBezier3(index_type, float, Vector3&) const;
74
75 float SegLengthLinear(index_type) const;
77 float SegLengthBezier3(index_type) const;
78 typedef float (SplineBase::*SegLenghtMethtod)(index_type) const;
80
81 void InitLinear(Vector3 const*, index_type, index_type);
82 void InitCatmullRom(Vector3 const*, index_type, index_type);
83 void InitBezier3(Vector3 const*, index_type, index_type);
84 typedef void (SplineBase::*InitMethtod)(Vector3 const*, index_type, index_type);
86
87 void UninitializedSplineEvaluationMethod(index_type, float, Vector3&) const { ABORT(); }
88 float UninitializedSplineSegLenghtMethod(index_type) const { ABORT(); return 0.0f; }
90
91public:
92
94
99 void evaluate_percent(index_type Idx, float u, Vector3& c) const {(this->*evaluators[m_mode])(Idx, u, c);}
100
105 void evaluate_derivative(index_type Idx, float u, Vector3& hermite) const {(this->*derivative_evaluators[m_mode])(Idx, u, hermite);}
106
108 index_type first() const { return index_lo;}
109 index_type last() const { return index_hi;}
110
111 bool empty() const { return index_lo == index_hi;}
113 bool isCyclic() const { return cyclic;}
114
115 ControlArray const& getPoints() const { return points;}
116 index_type getPointCount() const { return points.size();}
117 Vector3 const& getPoint(index_type i) const { return points[i];}
118
120 void init_spline(const Vector3 * controls, index_type count, EvaluationMode m, float orientation);
121 void init_cyclic_spline(const Vector3 * controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation);
122
125 template<class Init> inline void init_spline_custom(Init& initializer)
126 {
127 initializer(m_mode, cyclic, points, index_lo, index_hi);
128 }
129
130 void clear();
131
133 float SegLength(index_type i) const { return (this->*seglengths[m_mode])(i);}
134
135 std::string ToString() const;
136};
137
138template<typename length_type>
139class Spline : public SplineBase
140{
141public:
142 typedef length_type LengthType;
143 typedef std::vector<length_type> LengthArray;
144protected:
145
147
148 index_type computeIndexInBounds(length_type length) const;
149public:
150
151 explicit Spline(){ }
152
155 void evaluate_percent(float t, Vector3 & c) const;
156
159 void evaluate_derivative(float t, Vector3& hermite) const;
160
164 void evaluate_percent(index_type Idx, float u, Vector3& c) const { SplineBase::evaluate_percent(Idx, u, c);}
165
169 void evaluate_derivative(index_type Idx, float u, Vector3& c) const { SplineBase::evaluate_derivative(Idx, u, c);}
170
171 // Assumes that t in range [0, 1]
172 index_type computeIndexInBounds(float t) const;
173 void computeIndex(float t, index_type& out_idx, float& out_u) const;
174
176 void init_spline(const Vector3 * controls, index_type count, EvaluationMode m, float orientation = 0) { SplineBase::init_spline(controls, count, m, orientation);}
177 void init_cyclic_spline(const Vector3 * controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation = 0) { SplineBase::init_cyclic_spline(controls, count, m, cyclic_point, orientation);}
178
180 void initLengths();
181
184 template<class T> inline void initLengths(T& cacher)
185 {
187 lengths.resize(index_hi+1);
188 length_type prev_length = 0, new_length = 0;
189 while (i < index_hi)
190 {
191 new_length = cacher(*this, i);
192 // length overflowed, assign to max positive value
193 if (new_length < 0)
194 new_length = std::numeric_limits<length_type>::max();
195 lengths[++i] = new_length;
196
197 ASSERT(prev_length <= new_length);
198 prev_length = new_length;
199 }
200 }
201
203 length_type length() const
204 {
205 if (lengths.empty())
206 return 0;
207 return lengths[index_hi];
208 }
210 length_type length(index_type first, index_type last) const { return lengths[last]-lengths[first];}
211 length_type length(index_type Idx) const { return lengths[Idx];}
212
213 void set_length(index_type i, length_type length) { lengths[i] = length;}
214 void clear();
215};
216
217}
218
219#include "SplineImpl.h"
220
221#endif // TRINITYSERVER_SPLINE_H
uint8_t uint8
Definition Define.h:135
#define ABORT
Definition Errors.h:74
#define ASSERT
Definition Errors.h:68
void init_spline_custom(Init &initializer)
Definition Spline.h:125
bool isCyclic() const
Definition Spline.h:113
std::vector< Vector3 > ControlArray
Definition Spline.h:33
float SegLengthLinear(index_type) const
Definition Spline.cpp:152
ControlArray const & getPoints() const
Definition Spline.h:115
void init_cyclic_spline(const Vector3 *controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation)
Definition Spline.cpp:210
static InitMethtod initializers[ModesEnd]
Definition Spline.h:85
ControlArray points
Definition Spline.h:45
void EvaluateCatmullRom(index_type, float, Vector3 &) const
Definition Spline.cpp:120
void EvaluateDerivativeCatmullRom(index_type, float, Vector3 &) const
Definition Spline.cpp:139
void evaluate_percent(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:99
void init_spline(const Vector3 *controls, index_type count, EvaluationMode m, float orientation)
Definition Spline.cpp:201
void evaluate_derivative(index_type Idx, float u, Vector3 &hermite) const
Definition Spline.h:105
void EvaluateBezier3(index_type, float, Vector3 &) const
Definition Spline.cpp:126
float SegLengthBezier3(index_type) const
Definition Spline.cpp:178
float UninitializedSplineSegLenghtMethod(index_type) const
Definition Spline.h:88
float SegLengthCatmullRom(index_type) const
Definition Spline.cpp:158
bool empty() const
Definition Spline.h:111
float initialOrientation
Definition Spline.h:52
void UninitializedSplineInitMethod(Vector3 const *, index_type, index_type)
Definition Spline.h:89
float SegLength(index_type i) const
Definition Spline.h:133
index_type first() const
Definition Spline.h:108
void EvaluateDerivativeBezier3(index_type, float, Vector3 &) const
Definition Spline.cpp:145
void EvaluateDerivativeLinear(index_type, float, Vector3 &) const
Definition Spline.cpp:133
Vector3 const & getPoint(index_type i) const
Definition Spline.h:117
void InitBezier3(Vector3 const *, index_type, index_type)
Definition Spline.cpp:272
void(SplineBase::* EvaluationMethtod)(index_type, float, Vector3 &) const
Definition Spline.h:67
void(SplineBase::* InitMethtod)(Vector3 const *, index_type, index_type)
Definition Spline.h:84
void EvaluateLinear(index_type, float, Vector3 &) const
Definition Spline.cpp:114
index_type last() const
Definition Spline.h:109
void InitLinear(Vector3 const *, index_type, index_type)
Definition Spline.cpp:219
index_type index_hi
Definition Spline.h:48
std::string ToString() const
Definition Spline.cpp:292
float(SplineBase::* SegLenghtMethtod)(index_type) const
Definition Spline.h:78
static EvaluationMethtod derivative_evaluators[ModesEnd]
Definition Spline.h:73
static SegLenghtMethtod seglengths[ModesEnd]
Definition Spline.h:79
index_type getPointCount() const
Definition Spline.h:116
void UninitializedSplineEvaluationMethod(index_type, float, Vector3 &) const
Definition Spline.h:87
index_type index_lo
Definition Spline.h:47
static EvaluationMethtod evaluators[ModesEnd]
Definition Spline.h:68
EvaluationMode mode() const
Definition Spline.h:112
void InitCatmullRom(Vector3 const *, index_type, index_type)
Definition Spline.cpp:239
std::vector< length_type > LengthArray
Definition Spline.h:143
void evaluate_percent(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:164
LengthArray lengths
Definition Spline.h:146
void init_spline(const Vector3 *controls, index_type count, EvaluationMode m, float orientation=0)
Definition Spline.h:176
length_type LengthType
Definition Spline.h:142
length_type length(index_type first, index_type last) const
Definition Spline.h:210
void computeIndex(float t, index_type &out_idx, float &out_u) const
Definition SplineImpl.h:63
void initLengths(T &cacher)
Definition Spline.h:184
length_type length(index_type Idx) const
Definition Spline.h:211
void set_length(index_type i, length_type length)
Definition Spline.h:213
index_type computeIndexInBounds(length_type length) const
Definition SplineImpl.h:36
length_type length() const
Definition Spline.h:203
void evaluate_derivative(float t, Vector3 &hermite) const
Definition SplineImpl.h:28
void init_cyclic_spline(const Vector3 *controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation=0)
Definition Spline.h:177
void evaluate_percent(float t, Vector3 &c) const
Definition SplineImpl.h:20
void evaluate_derivative(index_type Idx, float u, Vector3 &c) const
Definition Spline.h:169