Revision c6dec65b
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/type/Dynamic.java | ||
---|---|---|
103 | 103 |
protected float[] mFactor; |
104 | 104 |
protected float[] mNoise; |
105 | 105 |
protected float[][] baseV; |
106 |
private float[] buffer; |
|
106 |
private float[] buf; |
|
107 |
private float[] old; |
|
107 | 108 |
|
108 | 109 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
109 | 110 |
// the coefficients of the X(t), Y(t) and Z(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx etc. |
... | ... | |
155 | 156 |
mDimension = dimension; |
156 | 157 |
|
157 | 158 |
baseV = new float[mDimension][mDimension]; |
158 |
buffer= new float[mDimension]; |
|
159 |
buf= new float[mDimension]; |
|
160 |
old= new float[mDimension]; |
|
159 | 161 |
} |
160 | 162 |
|
161 | 163 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
168 | 170 |
} |
169 | 171 |
else |
170 | 172 |
{ |
171 |
float x = (float)currentDuration/mDuration;
|
|
173 |
double x = (double)currentDuration/mDuration;
|
|
172 | 174 |
|
173 | 175 |
if( x<=mCount || mCount<=0.0f ) |
174 | 176 |
{ |
175 |
interpolate(buffer,offset,x-(int)x);
|
|
177 |
interpolate(buffer,offset, (float)(x-(int)x) );
|
|
176 | 178 |
} |
177 | 179 |
} |
178 | 180 |
} |
... | ... | |
187 | 189 |
return false; |
188 | 190 |
} |
189 | 191 |
|
190 |
float x = (float)currentDuration/mDuration;
|
|
192 |
double x = (double)currentDuration/mDuration;
|
|
191 | 193 |
|
192 | 194 |
if( x<=mCount || mCount<=0.0f ) |
193 | 195 |
{ |
194 |
interpolate(buffer,offset,x-(int)x);
|
|
196 |
interpolate(buffer,offset, (float)(x-(int)x) );
|
|
195 | 197 |
|
196 | 198 |
if( currentDuration+step > mDuration*mCount && mCount>0.0f ) |
197 | 199 |
{ |
... | ... | |
296 | 298 |
} |
297 | 299 |
} |
298 | 300 |
|
301 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
302 |
|
|
303 |
private void checkAngle(int index) |
|
304 |
{ |
|
305 |
float cosA = 0.0f; |
|
306 |
|
|
307 |
for(int k=0;k<mDimension; k++) |
|
308 |
cosA += baseV[index][k]*old[k]; |
|
309 |
|
|
310 |
if( cosA<0.0f ) |
|
311 |
{ |
|
312 |
/* |
|
313 |
/// DEBUGGING //// |
|
314 |
String s = index+" ("; |
|
315 |
float t; |
|
316 |
|
|
317 |
for(int j=0; j<mDimension; j++) |
|
318 |
{ |
|
319 |
t = ((int)(100*baseV[index][j]))/(100.0f); |
|
320 |
s+=(" "+t); |
|
321 |
} |
|
322 |
s += ") ("; |
|
323 |
|
|
324 |
for(int j=0; j<mDimension; j++) |
|
325 |
{ |
|
326 |
t = ((int)(100*old[j]))/(100.0f); |
|
327 |
s+=(" "+t); |
|
328 |
} |
|
329 |
s+= ")"; |
|
330 |
|
|
331 |
android.util.Log.e("dynamic", "kat: " + s); |
|
332 |
/// END DEBUGGING /// |
|
333 |
*/ |
|
334 |
for(int j=0; j<mDimension; j++) |
|
335 |
baseV[index][j] = -baseV[index][j]; |
|
336 |
} |
|
337 |
} |
|
338 |
|
|
299 | 339 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
300 | 340 |
// helper function in case we are interpolating through exactly 2 points |
301 | 341 |
|
... | ... | |
414 | 454 |
} |
415 | 455 |
else |
416 | 456 |
{ |
417 |
for(int i=0; i<mDimension-1; i++) |
|
418 |
for(int j=0; j<mDimension; j++) |
|
419 |
{ |
|
420 |
if( (i<last_non_zero && j==i) || (i>=last_non_zero && j==i+1) ) |
|
421 |
baseV[i+1][j]= baseV[0][last_non_zero]; |
|
422 |
else |
|
423 |
baseV[i+1][j]= 0.0f; |
|
424 |
} |
|
425 |
|
|
426 |
// That's it if velocity vector is already one of the standard orthonormal |
|
427 |
// vectors. Otherwise (i.e. non_zeros>1) velocity is linearly independent |
|
428 |
// to what's in baseV right now and we can use (modified!) Gram-Schmidt. |
|
457 |
// We can use (modified!) Gram-Schmidt. |
|
429 | 458 |
// |
430 | 459 |
// b[0] = b[0] |
431 | 460 |
// b[1] = b[1] - (<b[1],b[0]>/<b[0],b[0]>)*b[0] |
432 | 461 |
// b[2] = b[2] - (<b[2],b[0]>/<b[0],b[0]>)*b[0] - (<b[2],b[1]>/<b[1],b[1]>)*b[1] |
433 | 462 |
// b[3] = b[3] - (<b[3],b[0]>/<b[0],b[0]>)*b[0] - (<b[3],b[1]>/<b[1],b[1]>)*b[1] - (<b[3],b[2]>/<b[2],b[2]>)*b[2] |
434 |
// |
|
463 |
// (...)
|
|
435 | 464 |
// then b[i] = b[i] / |b[i]| |
436 | 465 |
|
437 |
if( non_zeros>1 ) |
|
466 |
float tmp; |
|
467 |
|
|
468 |
for(int i=1; i<mDimension; i++) /// one iteration computes baseV[i][*], the i-th orthonormal vector. |
|
438 | 469 |
{ |
439 |
float tmp;
|
|
470 |
buf[i-1]=0.0f;
|
|
440 | 471 |
|
441 |
for(int i=1; i<mDimension; i++)
|
|
472 |
for(int k=0; k<mDimension; k++)
|
|
442 | 473 |
{ |
443 |
buffer[i-1]=0.0f; |
|
444 |
|
|
445 |
for(int k=0; k<mDimension; k++) |
|
446 |
{ |
|
447 |
value = baseV[i-1][k]; |
|
448 |
buffer[i-1] += value*value; |
|
449 |
} |
|
450 |
|
|
451 |
for(int j=0; j<i; j++) |
|
452 |
{ |
|
453 |
tmp = 0.0f; |
|
474 |
old[k] = baseV[i][k]; |
|
454 | 475 |
|
455 |
for(int k=0;k<mDimension; k++) |
|
456 |
{ |
|
457 |
tmp += baseV[i][k]*baseV[j][k]; |
|
458 |
} |
|
459 |
|
|
460 |
tmp /= buffer[j]; |
|
476 |
if( (i<=last_non_zero && k==i-1) || (i>=(last_non_zero+1) && k==i) ) |
|
477 |
baseV[i][k]= baseV[0][last_non_zero]; |
|
478 |
else |
|
479 |
baseV[i][k]= 0.0f; |
|
461 | 480 |
|
462 |
for(int k=0;k<mDimension; k++) |
|
463 |
{ |
|
464 |
baseV[i][k] -= tmp*baseV[j][k]; |
|
465 |
} |
|
466 |
} |
|
481 |
value = baseV[i-1][k]; |
|
482 |
buf[i-1] += value*value; |
|
467 | 483 |
} |
468 | 484 |
|
469 |
buffer[mDimension-1]=0.0f; |
|
470 |
for(int k=0; k<mDimension; k++) |
|
485 |
for(int j=0; j<i; j++) |
|
471 | 486 |
{ |
472 |
value = baseV[mDimension-1][k]; |
|
473 |
buffer[mDimension-1] += value*value; |
|
474 |
} |
|
487 |
tmp = 0.0f; |
|
475 | 488 |
|
476 |
for(int i=1; i<mDimension; i++) |
|
477 |
{ |
|
478 |
tmp = (float)Math.sqrt(buffer[0]/buffer[i]); |
|
489 |
for(int k=0;k<mDimension; k++) |
|
490 |
{ |
|
491 |
tmp += baseV[i][k]*baseV[j][k]; |
|
492 |
} |
|
493 |
|
|
494 |
tmp /= buf[j]; |
|
479 | 495 |
|
480 | 496 |
for(int k=0;k<mDimension; k++) |
481 | 497 |
{ |
482 |
baseV[i][k] *= tmp;
|
|
498 |
baseV[i][k] -= tmp*baseV[j][k];
|
|
483 | 499 |
} |
484 | 500 |
} |
485 |
} |
|
501 |
|
|
502 |
if( i>=2 ) checkAngle(i); |
|
503 |
} /// end compute baseV[i][*] |
|
504 |
|
|
505 |
buf[mDimension-1]=0.0f; /// Normalize |
|
506 |
for(int k=0; k<mDimension; k++) // |
|
507 |
{ // |
|
508 |
value = baseV[mDimension-1][k]; // |
|
509 |
buf[mDimension-1] += value*value; // |
|
510 |
} // |
|
511 |
// |
|
512 |
for(int i=1; i<mDimension; i++) // |
|
513 |
{ // |
|
514 |
tmp = (float)Math.sqrt(buf[0]/buf[i]); // |
|
515 |
// |
|
516 |
for(int k=0;k<mDimension; k++) // |
|
517 |
{ // |
|
518 |
baseV[i][k] *= tmp; // |
|
519 |
} // |
|
520 |
} /// End Normalize |
|
486 | 521 |
} |
487 | 522 |
|
488 | 523 |
//printBase("end"); |
Also available in: Unified diff
1. Attempt to deal with unstable Orthonormal Base in Dynamic class (so far unsuccessful)
2. Improvements to the 'Dynamic' (and by necessity, 'MovingEffects') applications (to be able to debug the previous)