TrinityCore
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
36 {
42 };
43
44protected:
46
49
51 bool cyclic;
53
54 // could be modified, affects segment length evaluation precision
55 // lesser value saves more performance in cost of lover precision
56 // minimal value is 1
57 // client's value is 20, blizzs use 2-3 steps to compute length
59
60protected:
61 void EvaluateLinear(index_type, float, Vector3&) const;
62 void EvaluateCatmullRom(index_type, float, Vector3&) const;
63 void EvaluateBezier3(index_type, float, Vector3&) const;
64 typedef void (SplineBase::*EvaluationMethtod)(index_type, float, Vector3&) const;
66
67 void EvaluateDerivativeLinear(index_type, float, Vector3&) const;
68 void EvaluateDerivativeCatmullRom(index_type, float, Vector3&) const;
69 void EvaluateDerivativeBezier3(index_type, float, Vector3&) const;
71
72 float SegLengthLinear(index_type) const;
74 float SegLengthBezier3(index_type) const;
75 typedef float (SplineBase::*SegLenghtMethtod)(index_type) const;
77
78 void InitLinear(Vector3 const*, index_type, index_type);
79 void InitCatmullRom(Vector3 const*, index_type, index_type);
80 void InitBezier3(Vector3 const*, index_type, index_type);
81 typedef void (SplineBase::*InitMethtod)(Vector3 const*, index_type, index_type);
83
84 void UninitializedSplineEvaluationMethod(index_type, float, Vector3&) const { ABORT(); }
85 float UninitializedSplineSegLenghtMethod(index_type) const { ABORT(); return 0.0f; }
87
88public:
89
90 explicit SplineBase();
91 SplineBase(SplineBase const& right) = delete;
92 SplineBase(SplineBase&& right) = delete;
93 SplineBase& operator=(SplineBase const& right) = delete;
94 SplineBase& operator=(SplineBase&& right) = delete;
95 virtual ~SplineBase();
96
101 void evaluate_percent(index_type Idx, float u, Vector3& c) const {(this->*evaluators[m_mode])(Idx, u, c);}
102
107 void evaluate_derivative(index_type Idx, float u, Vector3& hermite) const {(this->*derivative_evaluators[m_mode])(Idx, u, hermite);}
108
110 index_type first() const { return index_lo;}
111 index_type last() const { return index_hi;}
112
113 bool empty() const { return index_lo == index_hi;}
115 bool isCyclic() const { return cyclic;}
116
117 ControlArray const& getPoints() const { return points; }
118 index_type getPointCount() const { return index_type(points.size());}
119 Vector3 const& getPoint(index_type i) const { return points[i]; }
120
122 void init_spline(const Vector3 * controls, index_type count, EvaluationMode m, float orientation = 0.0f);
123 void init_cyclic_spline(const Vector3 * controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation = 0.0f);
124
127 template<class Init>
128 void init_spline_custom(Init& initializer)
129 {
130 initializer(m_mode, cyclic, points, index_lo, index_hi);
131 }
132
133 virtual void clear();
134
136 float SegLength(index_type i) const { return (this->*seglengths[m_mode])(i);}
137
138 void set_steps_per_segment(index_type newStepsPerSegment) { stepsPerSegment = newStepsPerSegment; }
139
140 std::string ToString() const;
141};
142
143template<typename length_type>
144class Spline : public SplineBase
145{
146public:
147 typedef length_type LengthType;
148 typedef std::vector<length_type> LengthArray;
149protected:
150
152
153 index_type computeIndexInBounds(length_type length) const;
154public:
155
156 explicit Spline(){ }
157
160 void evaluate_percent(float t, Vector3 & c) const;
161
163
166 void evaluate_derivative(float t, Vector3& hermite) const;
167
169
170 // Assumes that t in range [0, 1]
171 index_type computeIndexInBounds(float t) const;
172 void computeIndex(float t, index_type& out_idx, float& out_u) const;
173
175 void initLengths();
176
179 template<class T>
180 void initLengths(T& cacher)
181 {
183 lengths.resize(index_hi+1);
184 length_type prev_length = 0, new_length = 0;
185 while (i < index_hi)
186 {
187 new_length = cacher(*this, i);
188 // length overflowed, assign to max positive value
189 if (new_length < 0)
190 new_length = std::numeric_limits<length_type>::max();
191 lengths[++i] = new_length;
192
193 ASSERT(prev_length <= new_length);
194 prev_length = new_length;
195 }
196 }
197
199 length_type length() const
200 {
201 if (lengths.empty())
202 return 0;
203 return lengths[index_hi];
204 }
206 length_type length(index_type first, index_type last) const { return lengths[last]-lengths[first];}
207 length_type length(index_type Idx) const { return lengths[Idx];}
208
209 void set_length(index_type i, length_type length) { lengths[i] = length;}
210 void clear() override;
211};
212
213}
214
215#include "SplineImpl.h"
216
217#endif // TRINITYSERVER_SPLINE_H
uint8_t uint8
Definition: Define.h:144
#define ABORT
Definition: Errors.h:74
#define ASSERT
Definition: Errors.h:68
SplineBase(SplineBase &&right)=delete
void init_spline_custom(Init &initializer)
Definition: Spline.h:128
bool isCyclic() const
Definition: Spline.h:115
std::vector< Vector3 > ControlArray
Definition: Spline.h:33
float SegLengthLinear(index_type) const
Definition: Spline.cpp:152
ControlArray const & getPoints() const
Definition: Spline.h:117
SplineBase & operator=(SplineBase &&right)=delete
SplineBase & operator=(SplineBase const &right)=delete
static InitMethtod initializers[ModesEnd]
Definition: Spline.h:82
ControlArray points
Definition: Spline.h:45
void EvaluateCatmullRom(index_type, float, Vector3 &) const
Definition: Spline.cpp:120
virtual void clear()
Definition: Spline.cpp:291
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:101
void evaluate_derivative(index_type Idx, float u, Vector3 &hermite) const
Definition: Spline.h:107
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:85
float SegLengthCatmullRom(index_type) const
Definition: Spline.cpp:158
bool empty() const
Definition: Spline.h:113
float initialOrientation
Definition: Spline.h:52
void UninitializedSplineInitMethod(Vector3 const *, index_type, index_type)
Definition: Spline.h:86
float SegLength(index_type i) const
Definition: Spline.h:136
index_type first() const
Definition: Spline.h:110
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:119
void init_cyclic_spline(const Vector3 *controls, index_type count, EvaluationMode m, index_type cyclic_point, float orientation=0.0f)
Definition: Spline.cpp:210
void init_spline(const Vector3 *controls, index_type count, EvaluationMode m, float orientation=0.0f)
Definition: Spline.cpp:201
void InitBezier3(Vector3 const *, index_type, index_type)
Definition: Spline.cpp:272
SplineBase(SplineBase const &right)=delete
void(SplineBase::* EvaluationMethtod)(index_type, float, Vector3 &) const
Definition: Spline.h:64
void(SplineBase::* InitMethtod)(Vector3 const *, index_type, index_type)
Definition: Spline.h:81
index_type stepsPerSegment
Definition: Spline.h:58
void EvaluateLinear(index_type, float, Vector3 &) const
Definition: Spline.cpp:114
index_type last() const
Definition: Spline.h:111
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:298
float(SplineBase::* SegLenghtMethtod)(index_type) const
Definition: Spline.h:75
static EvaluationMethtod derivative_evaluators[ModesEnd]
Definition: Spline.h:70
static SegLenghtMethtod seglengths[ModesEnd]
Definition: Spline.h:76
void set_steps_per_segment(index_type newStepsPerSegment)
Definition: Spline.h:138
index_type getPointCount() const
Definition: Spline.h:118
void UninitializedSplineEvaluationMethod(index_type, float, Vector3 &) const
Definition: Spline.h:84
index_type index_lo
Definition: Spline.h:47
static EvaluationMethtod evaluators[ModesEnd]
Definition: Spline.h:65
EvaluationMode mode() const
Definition: Spline.h:114
void InitCatmullRom(Vector3 const *, index_type, index_type)
Definition: Spline.cpp:239
std::vector< length_type > LengthArray
Definition: Spline.h:148
LengthArray lengths
Definition: Spline.h:151
length_type LengthType
Definition: Spline.h:147
length_type length(index_type first, index_type last) const
Definition: Spline.h:206
void computeIndex(float t, index_type &out_idx, float &out_u) const
Definition: SplineImpl.h:63
void initLengths(T &cacher)
Definition: Spline.h:180
length_type length(index_type Idx) const
Definition: Spline.h:207
void set_length(index_type i, length_type length)
Definition: Spline.h:209
void clear() override
Definition: SplineImpl.h:90
index_type computeIndexInBounds(length_type length) const
Definition: SplineImpl.h:36
length_type length() const
Definition: Spline.h:199
void initLengths()
Definition: SplineImpl.h:78
void evaluate_derivative(float t, Vector3 &hermite) const
Definition: SplineImpl.h:28
void evaluate_percent(float t, Vector3 &c) const
Definition: SplineImpl.h:20
TC_COMMON_API void Init()
Definition: Locales.cpp:28