-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBezier.cpp
More file actions
227 lines (198 loc) · 5.74 KB
/
Bezier.cpp
File metadata and controls
227 lines (198 loc) · 5.74 KB
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
#include "Bezier.h"
const FLOAT Bezier::CUBIC_FITTING_ERROR = 0.000099;
Bezier::Bezier(void)
{
//ctor
}
Bezier::~Bezier(void)
{
//dtor
}
void Bezier::cubicBezierLeastsquare(const CVector3f *q,const int n,CVector3f &p1,CVector3f &p2)
{
assert(n > 3);
CVector3f p0 = q[0],p3 = q[n-1];
FLOAT A1 = 0,A2 = 0, A12 = 0;
CVector3f C1(0,0,0),C2(0,0,0);
for(int i = 0;i < n;i++)
{
FLOAT t = i*1.0/(n-1);
FLOAT t2 = t*t , t3 = t2*t , t4 = t3*t;
FLOAT _t = 1-t ;
FLOAT _t2 = _t*_t , _t3 = _t2*_t, _t4 = _t3*_t;
A1 += t2 * _t4;
A2 += t4 * _t2;
A12+= t3 * _t3;
CVector3f tmp;
tmp = (q[i] - _t3*p0 - t3*p3);
C1 += ( 3*t*_t2*tmp );
C2 += ( 3*t2*_t*tmp );
}
//P1 = (A2C1-A12C2)/(A1A2-A12A12)
//P2 = (A1C2-A12C1)/(A1A2-A12A12)
A1 *= 9; A2 *= 9; A12 *= 9;
FLOAT base = A1*A2 - A12*A12;
assert(base != 0);
p1 = (A2*C1 - A12*C2)/base;
p2 = (A1*C2 - A12*C1)/base;
}
void Bezier::cubicBezierLeastsquare(const FLOAT *q,const int n,const int dim,FLOAT *p1,FLOAT *p2)
{
assert(n > 3);
const FLOAT *p0 = &q[0*dim],*p3 = &q[(n-1)*dim];
FLOAT A1 = 0,A2 = 0, A12 = 0;
FLOAT *C1 = new FLOAT[dim]; memset(C1,0,sizeof(FLOAT)*dim);
FLOAT *C2 = new FLOAT[dim]; memset(C2,0,sizeof(FLOAT)*dim);
#ifndef __STDC_IEC_559__
assert(1 == 0);/*Maybe there are some problems with memset, which is used to initialize FLOAT value */
#endif
for(int i = 0;i < n;i++)
{
FLOAT t = i*1.0/(n-1);
FLOAT t2 = t*t , t3 = t2*t , t4 = t3*t;
FLOAT _t = 1-t;
FLOAT _t2 = _t*_t , _t3 = _t2*_t, _t4 = _t3*_t;
A1 += t2 * _t4;
A2 += t4 * _t2;
A12+= t3 * _t3;
for(int j = 0;j < dim;j++)
{
FLOAT tmp = (q[i*dim+j] - _t3*p0[j] - t3*p3[j]);
C1[j] += ( 3*t*_t2*tmp );
C2[j] += ( 3*t2*_t*tmp );
}
}
//P1 = (A2C1-A12C2)/(A1A2-A12A12)
//P2 = (A1C2-A12C1)/(A1A2-A12A12)
A1 *= 9; A2 *= 9; A12 *= 9;
FLOAT base = A1*A2 - A12*A12;
assert(base != 0);
for(int j = 0;j < dim;j++)
{
p1[j] = (A2*C1[j] - A12*C2[j])/base;
p2[j] = (A1*C2[j] - A12*C1[j])/base;
}
delete [] C1; C1 = 0;
delete [] C2; C2 = 0;
p0 = p3 = 0;
}
int Bezier::cubicBezierFitting(const CVector3f *q,const int n,const CVector3f &P1,const CVector3f &P2)
{
/*p(t) = P0 * (1-t)^3 + P1 * 3*t(1-t)^2 + P2 * 3*t^2*(1-t) + P3 * t^3*/
CVector3f P0 = q[0],P3 = q[n-1];
CVector3f a,b,c,p;
c.x = 3 * (P1.x - P0.x);
c.y = 3 * (P1.y - P0.y);
c.z = 3 * (P1.z - P0.z);
b.x = 3 * (P2.x - P1.x) - c.x;
b.y = 3 * (P2.y - P1.y) - c.y;
b.z = 3 * (P2.z - P1.z) - c.z;
a.x = P3.x - P0.x - c.x - b.x;
a.y = P3.y - P0.y - c.y - b.y;
a.z = P3.z - P0.z - c.z - b.z;
FLOAT max_error = 0; int max_id = -1;
for(int i = 1;i < n-1;i++)//zero-based
{
FLOAT t = i*1.0/(n-1);
p.x = a.x * t * t * t + b.x * t * t + c.x * t + P0.x;
p.y = a.y * t * t * t + b.y * t * t + c.y * t + P0.y;
p.z = a.z * t * t * t + b.z * t * t + c.z * t + P0.z;
FLOAT dist = manhattanDist(q[i],p)/3;//sum / 3
if(H3DMath::complf(dist - max_error) > 0)
{
max_error = dist;
max_id = i;
}
}
if(H3DMath::complf(max_error - Bezier::CUBIC_FITTING_ERROR) > 0)
{
return max_id;
}
return -1;
}
int Bezier::cubicBezierFitting(const FLOAT *q,const int n,const int dim,const FLOAT *P1,const FLOAT *P2)
{
const FLOAT *P0 = &q[0*dim],*P3 = &q[(n-1)*dim];
//p(t) = P0 * (1-t)^3 + P1 * 3*t(1-t)^2 + P2 * 3*t^2*(1-t) + P3 * t^3
FLOAT *a = new FLOAT[dim];
FLOAT *b = new FLOAT[dim];
FLOAT *c = new FLOAT[dim];
FLOAT p;
for(int j = 0;j < dim;j++)
{
c[j] = 3 * (P1[j] - P0[j]);
b[j] = 3 * (P2[j] - P1[j]) - c[j];
a[j] = P3[j] - P0[j] - c[j] - b[j];
}
FLOAT max_error = 0; int max_id = -1;
for(int i = 1;i < n-1;i++)//zero-based
{
FLOAT t = i*1.0/(n-1);
int index = i*dim;
for(int j = 0;j < dim;j++)
{
p = a[j] * t * t * t + b[j] * t * t + c[j] * t + P0[j];
p = fabs(q[index+j] - p);
if(H3DMath::complf(p - max_error) > 0)
{
max_error = p;
max_id = i;
}
}
}
delete [] a;a = 0;
delete [] b;b = 0;
delete [] c;c = 0;
if(H3DMath::complf(max_error - Bezier::CUBIC_FITTING_ERROR) > 0)
{
return max_id;
}
return -1;
}
void Bezier::cubicBezierInterp(CVector3f *q,const int n,const CVector3f &P1,const CVector3f &P2)
{
CVector3f P0 = q[0],P3 = q[n-1];
CVector3f a,b,c,p;
c.x = 3 * (P1.x - P0.x);
c.y = 3 * (P1.y - P0.y);
c.z = 3 * (P1.z - P0.z);
b.x = 3 * (P2.x - P1.x) - c.x;
b.y = 3 * (P2.y - P1.y) - c.y;
b.z = 3 * (P2.z - P1.z) - c.z;
a.x = P3.x - P0.x - c.x - b.x;
a.y = P3.y - P0.y - c.y - b.y;
a.z = P3.z - P0.z - c.z - b.z;
for(int i = 1;i < n-1;i++)
{
FLOAT t = i*1.0/(n-1);
q[i].x = a.x * t * t * t + b.x * t * t + c.x * t + P0.x;
q[i].y = a.y * t * t * t + b.y * t * t + c.y * t + P0.y;
q[i].z = a.z * t * t * t + b.z * t * t + c.z * t + P0.z;
}
}
void Bezier::cubicBezierInterp(FLOAT *q,const int n,const int dim,const FLOAT *P1,const FLOAT *P2)
{
const FLOAT *P0 = &q[0*dim],*P3 = &q[(n-1)*dim];
//p(t) = P0 * (1-t)^3 + P1 * 3*t(1-t)^2 + P2 * 3*t^2*(1-t) + P3 * t^3
FLOAT *a = new FLOAT[dim];
FLOAT *b = new FLOAT[dim];
FLOAT *c = new FLOAT[dim];
for(int j = 0;j < dim;j++)
{
c[j] = 3 * (P1[j] - P0[j]);
b[j] = 3 * (P2[j] - P1[j]) - c[j];
a[j] = P3[j] - P0[j] - c[j] - b[j];
}
for(int i = 1;i < n-1;i++)//zero-based
{
FLOAT t = i*1.0/(n-1);
int index = i*dim;
for(int j = 0;j < dim;j++)
{
q[index+j] = a[j] * t * t * t + b[j] * t * t + c[j] * t + P0[j];
}
}
delete [] a;a = 0;
delete [] b;b = 0;
delete [] c;c = 0;
}