1 |
bearsoft |
1.1 |
#include "../String/String.h"
|
2 |
|
|
#include "../String/StringInput.h"
|
3 |
|
|
#include "../System/System.h"
|
4 |
|
|
#include "Spline.h"
|
5 |
|
|
#include "../System/SystemDefine.h"
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
Spline::Spline(int iNumberOfSplines)
|
9 |
|
|
{
|
10 |
|
|
numberOfSplines=iNumberOfSplines;
|
11 |
|
|
motionValuesPointers=new int[numberOfSplines];
|
12 |
|
|
motionSplinePointers=new int[numberOfSplines];
|
13 |
|
|
|
14 |
|
|
for ( int n=0; n<numberOfSplines ; n++ )
|
15 |
|
|
{
|
16 |
|
|
motionValuesPointers[n]=0;
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
for ( int n2=0; n2<numberOfSplines ; n2++ )
|
20 |
|
|
{
|
21 |
|
|
motionSplinePointers[n2]=0;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
motionFrames=0;
|
25 |
|
|
motionLinear=0;
|
26 |
|
|
motionTension=0;
|
27 |
|
|
motionContinuity=0;
|
28 |
|
|
motionBias=0;
|
29 |
|
|
|
30 |
|
|
sceneFrame=0;
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
Spline::~Spline()
|
34 |
|
|
{
|
35 |
|
|
for ( int n=0; n<numberOfSplines ; n++ )
|
36 |
|
|
{
|
37 |
|
|
float *motionValues=(float*)motionValuesPointers[n];
|
38 |
|
|
if ( motionValues != 0 )
|
39 |
|
|
delete []motionValues;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
for ( int n2=0; n2<numberOfSplines ; n2++ )
|
43 |
|
|
{
|
44 |
|
|
float *motionSpline=(float*)motionSplinePointers[n2];
|
45 |
|
|
if ( motionSpline != 0 )
|
46 |
|
|
delete []motionSpline;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
if ( motionValuesPointers != 0 )
|
50 |
|
|
delete []motionValuesPointers;
|
51 |
|
|
if ( motionSplinePointers != 0 )
|
52 |
|
|
delete []motionSplinePointers;
|
53 |
|
|
if ( motionFrames != 0 )
|
54 |
|
|
delete []motionFrames;
|
55 |
|
|
if ( motionLinear != 0 )
|
56 |
|
|
delete []motionLinear;
|
57 |
|
|
if ( motionTension != 0 )
|
58 |
|
|
delete []motionTension;
|
59 |
|
|
if ( motionContinuity != 0 )
|
60 |
|
|
delete []motionContinuity;
|
61 |
|
|
if ( motionBias != 0 )
|
62 |
|
|
delete []motionBias;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
float Spline::div( float a, float b)
|
66 |
|
|
{
|
67 |
|
|
if ( b > 0 )
|
68 |
|
|
{
|
69 |
|
|
return a/b;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
return b=0;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
void Spline::hermite(float t,float *h1,float *h2,float *h3,float *h4)
|
76 |
|
|
{
|
77 |
|
|
double t2,t3,z;
|
78 |
|
|
|
79 |
|
|
t2=t*t;
|
80 |
|
|
t3=t*t2;
|
81 |
|
|
z=3.0*t2-t3-t3;
|
82 |
|
|
|
83 |
|
|
*h1=(float )(1.0-z);
|
84 |
|
|
*h2=(float )(z);
|
85 |
|
|
*h3=(float )(t3-t2-t2+t);
|
86 |
|
|
*h4=(float )(t3-t2);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
float Spline::calculateSpline(float t1, float t2, float t3, float t4, float p1, float p2 , float p3 , float p4, float tensionLast, float tensionNext, float continuityLast, float continuityNext, float biasLast, float biasNext)
|
90 |
|
|
{
|
91 |
|
|
float length=t3-t2;
|
92 |
|
|
float t=div((sceneFrame-t2),length);
|
93 |
|
|
|
94 |
|
|
int forward=0;
|
95 |
|
|
|
96 |
|
|
// TCB spline
|
97 |
|
|
|
98 |
|
|
float h1,h2,h3,h4;
|
99 |
|
|
|
100 |
|
|
hermite(t,&h1,&h2,&h3,&h4);
|
101 |
|
|
float dd0a=(float )((1.0-tensionLast) * (1.0+continuityLast) * (1.0+biasLast));
|
102 |
|
|
float dd0b=(float )((1.0-tensionLast) * (1.0-continuityLast) * (1.0-biasLast));
|
103 |
|
|
float ds1a=(float )((1.0-tensionNext) * (1.0-continuityNext) * (1.0+biasNext));
|
104 |
|
|
float ds1b=(float )((1.0-tensionNext) * (1.0+continuityNext) * (1.0-biasNext));
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
float adj0=div(length,(t3-t1));
|
108 |
|
|
float adj1=div(length,(t4-t2));
|
109 |
|
|
float v=p3-p2;
|
110 |
|
|
|
111 |
|
|
float dd0=0;
|
112 |
|
|
float ds1=0;
|
113 |
|
|
|
114 |
|
|
if (prev)
|
115 |
|
|
{
|
116 |
|
|
dd0=(float)0.5*(dd0a+dd0b)*v;
|
117 |
|
|
}
|
118 |
|
|
else
|
119 |
|
|
{
|
120 |
|
|
dd0=adj0*(dd0a*(p2-p1)+dd0b*v);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
if (next)
|
124 |
|
|
{
|
125 |
|
|
ds1=(float)0.5*(ds1a+ds1b)*v;
|
126 |
|
|
}
|
127 |
|
|
else
|
128 |
|
|
{
|
129 |
|
|
ds1=adj1*(ds1a*v+ds1b*(p4-p3));
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
return (p2*h1+p3*h2+dd0*h3+ds1*h4);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
void Spline::update()
|
136 |
|
|
{
|
137 |
|
|
int i=0;
|
138 |
|
|
int stop=0;
|
139 |
|
|
int key=0;
|
140 |
|
|
|
141 |
|
|
if ( sceneFrame > motionFrames[sNumberOfKeys] )
|
142 |
|
|
{
|
143 |
|
|
sceneFrame=motionFrames[sNumberOfKeys];
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
while ( i<numberOfKeys && stop == 0 )
|
147 |
|
|
{
|
148 |
|
|
if ( sceneFrame <= motionFrames[i+2] )
|
149 |
|
|
{
|
150 |
|
|
stop=1;
|
151 |
|
|
}
|
152 |
|
|
else
|
153 |
|
|
{
|
154 |
|
|
key++;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
i++;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
keyP1=key+0;
|
161 |
|
|
keyP2=key+1;
|
162 |
|
|
keyP3=key+2;
|
163 |
|
|
keyP4=key+3;
|
164 |
|
|
|
165 |
|
|
mKeyP1=keyP1*3;
|
166 |
|
|
mKeyP2=keyP2*3;
|
167 |
|
|
mKeyP3=keyP3*3;
|
168 |
|
|
mKeyP4=keyP4*3;
|
169 |
|
|
|
170 |
|
|
prev=false;
|
171 |
|
|
next=false;
|
172 |
|
|
|
173 |
|
|
if (key == 0)
|
174 |
|
|
{
|
175 |
|
|
prev=true;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
if ((key+3) == sNumberOfKeys || (key+2) == sNumberOfKeys)
|
179 |
|
|
{
|
180 |
|
|
next=true;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
for ( int n=0; n<numberOfSplines ; n++ )
|
184 |
|
|
{
|
185 |
|
|
float *motionValues=(float*)motionValuesPointers[n];
|
186 |
|
|
float *motionSpline=(float*)motionSplinePointers[n];
|
187 |
|
|
|
188 |
|
|
for ( int n2=0; n2<3 ; n2++ )
|
189 |
|
|
{
|
190 |
|
|
motionSpline[n2]=calculateSpline(motionFrames[keyP1], motionFrames[keyP2], motionFrames[keyP3], motionFrames[keyP4], motionValues[mKeyP1+n2], motionValues[mKeyP2+n2] , motionValues[mKeyP3+n2] , motionValues[mKeyP4+n2] ,motionTension[keyP2], motionTension[keyP3],motionContinuity[keyP2], motionContinuity[keyP3],motionBias[keyP2], motionBias[keyP3]);
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
float *Spline::getSplineChannel(int index)
|
196 |
|
|
{
|
197 |
|
|
return (float*)motionSplinePointers[index];
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
void Spline::increaseTime(float time)
|
201 |
|
|
{
|
202 |
|
|
sceneFrame+=time;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
void Spline::insertdata( int p, int p2)
|
206 |
|
|
{
|
207 |
|
|
for ( int n=0; n<3 ; n++ )
|
208 |
|
|
{
|
209 |
|
|
for ( int n2=0; n2<numberOfSplines ; n2++ )
|
210 |
|
|
{
|
211 |
|
|
float *motionValues=(float*)motionValuesPointers[n2];
|
212 |
|
|
motionValues[p*3+n]=motionValues[p2*3+n];
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
motionFrames[p]=motionFrames[p2];
|
217 |
|
|
motionLinear[p]=motionLinear[p2];
|
218 |
|
|
motionTension[p]=motionTension[p2];
|
219 |
|
|
motionContinuity[p]=motionContinuity[p2];
|
220 |
|
|
motionBias[p]=motionBias[p2];
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
void Spline::insertdata2( int p, int p2, int t, int t2)
|
224 |
|
|
{
|
225 |
|
|
for ( int n2=t ; n2>=t2 ; --n2 )
|
226 |
|
|
{
|
227 |
|
|
for ( int n=0; n<3 ; n++ )
|
228 |
|
|
{
|
229 |
|
|
for ( int n3=0; n3<numberOfSplines ; n3++ )
|
230 |
|
|
{
|
231 |
|
|
float *motionValues=(float*)motionValuesPointers[n3];
|
232 |
|
|
motionValues[(n2+p)*3+n]=motionValues[(n2+p2)*3+n];
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
motionFrames[n2+p]=motionFrames[n2+p2];
|
237 |
|
|
motionLinear[n2+p]=motionLinear[n2+p2];
|
238 |
|
|
motionTension[n2+p]=motionTension[n2+p2];
|
239 |
|
|
motionContinuity[n2+p]=motionContinuity[n2+p2];
|
240 |
|
|
motionBias[n2+p]=motionBias[n2+p2];
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
void Spline::insertdata3( int p, int t, int t2)
|
245 |
|
|
{
|
246 |
|
|
for ( int n2=t ; n2<t2 ; n2++ )
|
247 |
|
|
{
|
248 |
|
|
for ( int n=0; n<3 ; n++ )
|
249 |
|
|
{
|
250 |
|
|
for ( int n3=0; n3<numberOfSplines ; n3++ )
|
251 |
|
|
{
|
252 |
|
|
float *motionValues=(float*)motionValuesPointers[n3];
|
253 |
|
|
motionValues[n2*3+n]=motionValues[p*3+n];
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
motionFrames[n2]=motionFrames[p];
|
258 |
|
|
motionLinear[n2]=motionLinear[p];
|
259 |
|
|
motionTension[n2]=motionTension[p];
|
260 |
|
|
motionContinuity[n2]=motionContinuity[p];
|
261 |
|
|
motionBias[n2]=motionBias[p];
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
void Spline::read(float *buffer)
|
266 |
|
|
{
|
267 |
|
|
numberOfKeys=(int)buffer[0];
|
268 |
|
|
sNumberOfKeys=numberOfKeys+1;
|
269 |
|
|
|
270 |
|
|
int plus=4;
|
271 |
|
|
|
272 |
|
|
for ( int v=0; v<numberOfSplines ; v++ )
|
273 |
|
|
{
|
274 |
|
|
motionValuesPointers[v]=(int)new int[(sNumberOfKeys+plus)*3];
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
for ( int v2=0; v2<numberOfSplines ; v2++ )
|
278 |
|
|
{
|
279 |
|
|
motionSplinePointers[v2]=(int)new int[3];
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
motionTension=new float[(sNumberOfKeys+plus)];
|
283 |
|
|
motionContinuity=new float[(sNumberOfKeys+plus)];
|
284 |
|
|
motionBias=new float[(sNumberOfKeys+plus)];
|
285 |
|
|
motionFrames=new float[(sNumberOfKeys+plus)];
|
286 |
|
|
motionLinear=new int[(sNumberOfKeys+plus)];
|
287 |
|
|
|
288 |
|
|
int forward=(numberOfSplines*3)+5;
|
289 |
|
|
|
290 |
|
|
for ( int n=0; n<numberOfKeys ; n++ )
|
291 |
|
|
{
|
292 |
|
|
int step=0;
|
293 |
|
|
|
294 |
|
|
for ( int n2=0; n2<numberOfSplines ; n2++ )
|
295 |
|
|
{
|
296 |
|
|
float *motionValues=(float*)motionValuesPointers[n2];
|
297 |
|
|
motionValues[n*3+n2]=buffer[n*forward+n2+1+(step*3)];
|
298 |
|
|
step+=3;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
motionFrames[n]=buffer[n*forward+1+step];
|
302 |
|
|
motionLinear[n]=(int)buffer[n*forward+1+step+1];
|
303 |
|
|
motionTension[n]=buffer[n*forward+1+step+2];
|
304 |
|
|
motionContinuity[n]=buffer[n*forward+1+step+3];
|
305 |
|
|
motionBias[n]=buffer[n*forward+1+step+4];
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
if ( numberOfKeys == 1 )
|
309 |
|
|
{
|
310 |
|
|
insertdata3(0, 1, 5);
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
if ( numberOfKeys == 2 )
|
314 |
|
|
{
|
315 |
|
|
--sNumberOfKeys;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
if ( numberOfKeys > 1 )
|
319 |
|
|
{
|
320 |
|
|
insertdata2(0,-1,(numberOfKeys+1),1);
|
321 |
|
|
insertdata(1,0);
|
322 |
|
|
insertdata(numberOfKeys+1,numberOfKeys);
|
323 |
|
|
}
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|