-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurveutil.cpp
297 lines (262 loc) · 8.93 KB
/
curveutil.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "curveutil.h"
int CurveUtil::FindSpan(int degree, std::vector<double>& knotVector, double u)
{
int n = knotVector.size() - degree - 2;
if (MathUtil::IsAlmostEqualTo(u, knotVector[n + 1]))
{
return n;
}
int low = degree;
int high = n + 1;
int mid = (low+high)/2.0;
while (u < knotVector[mid] ||
u>= knotVector[mid + 1])
{
if (u < knotVector[mid])
{
high = mid;
}
else
{
low = mid;
}
mid = (low + high) / 2.0;
}
return mid;
}
void CurveUtil::GetKnotVector(std::vector<double>& T,std::vector<std::vector<Point3dW>> m_controlPoints,int nCount,int num,int k, bool bU)
{
//小于等于曲线次数k的节点值为0
for(int i = 0;i <= k;i++)
{
T[i] = 0.0;
}
//大于n的节点值为1
for(int i = num+1;i <= num + k + 1;i++)
{
T[i] = 1.0;
}
//计算其他节点值
for(int i = k+1;i <= num; i++)
{
double sum = 0.0;
for(int j = k + 1;j <= i;j++)
{
double numerator = 0.0;//计算分子
for (int loop = j - k;loop <= j-1; loop++)
{
if(bU)//选择计算节点矢量U还是计算节点矢量V
numerator+=(m_controlPoints[nCount][loop].GetX()-m_controlPoints[nCount][loop-1].GetX())*
(m_controlPoints[nCount][loop].GetX()-m_controlPoints[nCount][loop-1].GetX())
+(m_controlPoints[nCount][loop].GetY()-m_controlPoints[nCount][loop-1].GetY())*
(m_controlPoints[nCount][loop].GetY()-m_controlPoints[nCount][loop-1].GetY());
else
numerator+=(m_controlPoints[loop][nCount].GetX()-m_controlPoints[loop-1][nCount].GetX())*
(m_controlPoints[loop][nCount].GetX()-m_controlPoints[loop-1][nCount].GetX())
+(m_controlPoints[loop][nCount].GetY()-m_controlPoints[loop-1][nCount].GetY())*
(m_controlPoints[loop][nCount].GetY()-m_controlPoints[loop-1][nCount].GetY());
}
double denominator = 0.0;//计算分母
for(int loop1 = k+1;loop1 <= num+1;loop1++)
{
for (int loop2 = loop1 - k;loop2 <= loop1 - 1;loop2++)
{
if(bU)
{
denominator+=(m_controlPoints[nCount][loop2].GetX()-m_controlPoints[nCount][loop2-1].GetX())*
(m_controlPoints[nCount][loop2].GetX()-m_controlPoints[nCount][loop2-1].GetX())
+(m_controlPoints[nCount][loop2].GetY()-m_controlPoints[nCount][loop2-1].GetY())*
(m_controlPoints[nCount][loop2].GetY()-m_controlPoints[nCount][loop2-1].GetY());
}
else
{
denominator+=(m_controlPoints[loop2][nCount].GetX()-m_controlPoints[loop2-1][nCount].GetX())*
(m_controlPoints[loop2][nCount].GetX()-m_controlPoints[loop2-1][nCount].GetX())+
(m_controlPoints[loop2][nCount].GetY()-m_controlPoints[loop2-1][nCount].GetY())*
(m_controlPoints[loop2][nCount].GetY()-m_controlPoints[loop2-1][nCount].GetY());
}
}
}
sum+=numerator/denominator;
}
T[i] = sum;
}
}
double CurveUtil::BasicFunctions(double t,int i,int k,std::vector<double>& T)
{
double value1,value2,value;
if(k==0)
{
if(t>=T[i] && t<T[i+1])
return 1.0;
else
return 0.0;
}
if(k>0)
{
if(t<T[i]||t>T[i+k+1])
return 0.0;
else
{
double coffcient1,coffcient2;//凸组合系数1,凸组合系数2
double denominator=0.0;//分母
denominator=T[i+k]-T[i];//递推公式第一项分母
if(denominator==0.0)//约定0/0
coffcient1=0.0;
else
coffcient1=(t-T[i])/denominator;
denominator=T[i+k+1]-T[i+1];//递推公式第二项分母
if(0.0==denominator)//约定0/0
coffcient2=0.0;
else
coffcient2=(T[i+k+1]-t)/denominator;
value1=coffcient1*BasicFunctions(t,i,k-1,T);//递推公式第一项的值
value2=coffcient2*BasicFunctions(t,i+1,k-1,T);//递推公式第二项的值
value=value1+value2;//基函数的值
}
}
return value;
}
std::vector<double> CurveUtil::BasisFunctions(double t, int i, int k, std::vector<double> &T)
{
std::vector<double> basisFunctions(k + 1);
basisFunctions[0] = 1.0;
std::vector<double> left(k + 1);
std::vector<double> right(k + 1);
for (int j = 1; j <= k; j++)
{
left[j] = t - T[i + 1 - j];
right[j] = T[i + j] - t;
double saved = 0.0;
for (int r = 0; r < j; r++)
{
double temp = basisFunctions[r] / (right[r + 1] + left[j - r]);
basisFunctions[r] = saved + right[r + 1] * temp;
saved = left[j - r] * temp;
}
basisFunctions[j] = saved;
}
return basisFunctions;
}
std::vector<std::vector<double>> CurveUtil::BasisFunctionsDerivs(int spanIndex, int degree, int derivative, const std::vector<double>& knotVector, double paramT)
{
std::vector<std::vector<double>> derivatives(derivative + 1, std::vector<double>(degree + 1));
std::vector<std::vector<double>> ndu(degree + 1,std::vector<double>(degree + 1));
ndu[0][0] = 1.0;
std::vector<double> left(degree + 1);
std::vector<double> right(degree + 1);
double saved = 0.0;
double temp = 0.0;
for (int j = 1; j <= degree; j++)
{
left[j] = paramT - knotVector[spanIndex + 1 - j];
right[j] = knotVector[spanIndex + j] - paramT;
saved = 0.0;
for (int r = 0; r < j; r++)
{
ndu[j][r] = right[r + 1] + left[j - r];
temp = ndu[r][j - 1] / ndu[j][r];
ndu[r][j] = saved + right[r + 1] * temp;
saved = left[j - r] * temp;
}
ndu[j][j] = saved;
}
for (int j = 0; j <= degree; j++)
{
derivatives[0][j] = ndu[j][degree];
}
std::vector<std::vector<double>> a(2,std::vector<double>(degree + 1));
for (int r = 0; r <= degree; r++)
{
int s1 = 0;
int s2 = 1;
a[0][0] = 1.0;
for (int k = 1; k <= derivative; k++)
{
double d = 0.0;
int rk = r - k;
int pk = degree - k;
if (r >= k)
{
a[s2][0] = a[s1][0] / ndu[pk + 1][rk];
d = a[s2][0] * ndu[rk][pk];
}
int j1 = 0;
int j2 = 0;
if (rk >= -1)
j1 = 1;
else
j1 = -rk;
if (r - 1 <= pk)
j2 = k - 1;
else
j2 = degree - r;
for (int j = j1; j <= j2; j++)
{
a[s2][j] = (a[s1][j] - a[s1][j - 1]) / ndu[pk + 1][rk + j];
d += a[s2][j] * ndu[rk + j][pk];
}
if (r <= pk)
{
a[s2][k] = -a[s1][k - 1] / ndu[pk + 1][r];
d += a[s2][k] * ndu[r][pk];
}
derivatives[k][r] = d;
int temp = s1;
s1 = s2;
s2 = temp;
}
}
int r = degree;
for (int k = 1; k <= derivative; k++)
{
for (int j = 0; j <= degree; j++)
{
derivatives[k][j] *= r;
}
r *= degree - k;
}
return derivatives;
}
std::vector<double> CurveUtil::GetUkByThroughPoints(const std::vector<Point3d> throughPoints)
{
int n = throughPoints.size() - 1;
//计算弦长
double d = 0.0;
for (int i = 1; i <= n; i++)
{
d += throughPoints[i].Distance(throughPoints[i-1]);
}
//计算uk
std::vector<double> uk;
uk.resize(n+1,0.0);
uk[n] = 1.0;
for (int i = 1; i <= n - 1; i++)
{
uk[i] = uk[i-1] + (throughPoints[i].Distance(throughPoints[i-1]))/d;
}
return uk;
}
std::vector<double> CurveUtil::GetKnotVectorByThroughPoints(int k,int pointsCount,const std::vector<double> _uk)
{
std::vector<double> knotVector;
std::vector<double> uk = _uk;
int size = pointsCount;
int n = size - 1;
int m = n + k + 1;
knotVector.resize(m + 1, 0.0);
for (int i = m - k; i <= m; i++)
{
knotVector[i] = 1.0;
}
for (int j = 1; j <= static_cast<int>(n - k + 1); j++)
{
double temp = 0.0;
for (int i = j; i <= static_cast<int>(j + k - 1); i++)
{
temp += uk[i];
}
knotVector[j + k] = (1.0 / k) * temp;
}
return knotVector;
}