-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spline.h
187 lines (159 loc) · 4.79 KB
/
Spline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef CATMULL_ROM_SPLINE_H
#define CATMULL_ROM_SPLINE_H
#include <Urho3D/Container/Vector.h>
#include <Urho3D/Math/Vector3.h>
#include <algorithm>
using namespace Urho3D;
template <class T> class CRSpline
{
public:
CRSpline() {}
~CRSpline() {}
void Push(T p)
{
points_.Push(p);
}
T Solve(float t)
{
// Which segment?
int psize=points_.Size();
float segsize=1.0f/(float)(psize-1);
int segment=(int)(t/segsize);
float segt=t/segsize-(float)segment;
Vector3 p0=points_[GetIndex(segment-1)];
Vector3 p1=points_[GetIndex(segment)];
Vector3 p2=points_[GetIndex(segment+1)];
Vector3 p3=points_[GetIndex(segment+2)];
return ((p1*2.0f) + (p2-p0)*segt + (p0*2.0f - p1*5.0f + p2*4.0f - p3) * segt*segt + (p1*3.0f - p2*3.0f + p3 - p0) * segt*segt*segt)*0.5f;
}
T Tangent(float t)
{
// Which segment?
int psize=points_.Size();
float segsize=1.0f/(float)(psize-1);
int segment=(int)(t/segsize);
float segt=t/segsize-(float)segment;
Vector3 p0=points_[GetIndex(segment-1)];
Vector3 p1=points_[GetIndex(segment)];
Vector3 p2=points_[GetIndex(segment+1)];
Vector3 p3=points_[GetIndex(segment+2)];
return ((p2-p0) + (p0*2.0f - p1*5.0f + p2*4.0f - p3)*segt*2.0 + (p1*3.0f - p2*3.0f + p3 - p0)*segt*segt*3.0f)*0.5f;
}
void PointTangent(T &point, T &tangent, float t)
{
int psize=points_.Size();
float segsize=1.0f/(float)(psize-1);
int segment=(int)(t/segsize);
float segt=t/segsize-(float)segment;
Vector3 p0=points_[GetIndex(segment-1)];
Vector3 p1=points_[GetIndex(segment)];
Vector3 p2=points_[GetIndex(segment+1)];
Vector3 p3=points_[GetIndex(segment+2)];
point=((p1*2.0f) + (p2-p0)*segt + (p0*2.0f - p1*5.0f + p2*4.0f - p3) * segt*segt + (p1*3.0f - p2*3.0f + p3 - p0) * segt*segt*segt)*0.5f;
tangent=((p2-p0) + (p0*2.0f - p1*5.0f + p2*4.0f - p3)*segt*2.0 + (p1*3.0f - p2*3.0f + p3 - p0)*segt*segt*3.0f)*0.5f;
}
void Solve(Vector<T> &out, int divsperseg, float mergedistance=0.0f)
{
int divs=divsperseg*(points_.Size()-1);
float inc=1.0f / (float)(divs-1);
float t=0.0f;
out.Clear();
for(int c=0; c<divs; ++c)
{
T res=Solve(t);
if (out.Size()>0)
{
T d=res-out[out.Size()-1];
if(d.Length() > mergedistance) out.Push(res);
}
else out.Push(res);
t+=inc;
}
}
void Solve(Vector<T> &points, Vector<T> &tangents, int divsperseg, float mergedistance=0.0f)
{
int divs=divsperseg*(points_.Size()-1);
float inc=1.0f / (float)(divs-1);
float t=0.0f;
points.Clear();
tangents.Clear();
for(int c=0; c<divs; ++c)
{
T point,tang;
PointTangent(point,tang,t);
if (points.Size()>0)
{
T d=point-points[points.Size()-1];
if(d.Length() > mergedistance)
{
points.Push(point);
tangents.Push(tang);
}
}
else
{
points.Push(point);
tangents.push(tang);
}
t+=inc;
}
}
void Solve(Vector<T> &points, Vector<T> &tangents, Vector<float> &distances, int divsperseg, float mergedistance=0.0f)
{
int divs=divsperseg*(points_.Size()-1);
float inc=1.0f / (float)(divs-1);
float t=0.0f;
float dist=0.0f;
points.Clear();
tangents.Clear();
distances.Clear();
for(int c=0; c<divs; ++c)
{
T point,tang;
PointTangent(point,tang,t);
if (points.Size()>0)
{
T d=point-points[points.Size()-1];
if(d.Length() > mergedistance)
{
points.Push(point);
tangents.Push(tang);
}
}
else
{
points.Push(point);
tangents.Push(tang);
}
t+=inc;
}
for(int c=0; c<points.Size(); ++c)
{
if(c==0) distances.Push(dist);
else
{
T vec=points[c]-points[c-1];
dist += vec.Length();
distances.Push(dist);
}
}
}
void Clear()
{
points_.Clear();
}
int NumPoints()
{
return points_.Size();
}
private:
Vector<T> points_;
int GetIndex(int which)
{
return std::max(0, std::min((int)points_.Size()-1, which));
}
};
void BuildQuadStripA(Vector<Vector3> &points, Vector<Vector3> &out, float width, Vector3 up=Vector3(0,0,-1));
void BuildQuadStripVaryingA(Vector<Vector3> &points, Vector<Vector3> &out, float swidth, float ewidth, Vector3 up=Vector3(0,0,-1));
void BuildQuadStripB(Vector<Vector3> &points, Vector<Vector3> &tangents, Vector<float> &distances, Vector<Vector3> &outpoints, Vector<float> &outdistances, float width, Vector3 up);
#endif