-
Notifications
You must be signed in to change notification settings - Fork 1
/
realsurf_frag_stub.glsl
executable file
·2237 lines (1954 loc) · 58.7 KB
/
realsurf_frag_stub.glsl
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define MAX_LIGHTS 3
uniform mat4 surface_transform_inverse;
uniform mat4 surface_transform;
uniform int clip_to;
varying vec3 eye_space_eye;
varying vec3 eye_space_dir;
varying vec3 surface_space_eye;
varying vec3 surface_space_dir;
varying vec3 clipping_space_eye;
varying vec3 clipping_space_dir;
vec3 ese_opt, esd_opt, cse_opt, csd_opt, sse_opt, ssd_opt;
polynomial calc_coefficients( vec3 eye, vec3 dir );
vec3 gradient( vec3 p );
#define CLIP_TO_SPHERE 1
#define CLIP_TO_CUBE 2
#ifdef METHOD_LAGRANGE_INTERPOLATION
/******************************************************
* Die hier verwendete Interpolationsmethode ist eine
* Portierung des optimierten Lagrange-Algorithmus aus
*
* Numerical Recipes in C: The Art of Scientific Computing
* William H. Press et al.
* Cambridge University Press
* 2. Auflage (1996)
* ISBN 0-521-43108-5
* Seite 121
******************************************************/
polynomial calc_coefficients( vec3 eye, vec3 dir, vec2 clipping_interval )
{
// DEGREE + 1 Stützpunkte auf Strahl eye + t * pos berechnen
float y[ SIZE ];
float x[ SIZE ];
float ci_width = clipping_interval[ 1 ] - clipping_interval[ 0 ];
for( int i = 0; i <= DEGREE; i++ )
{
x[ i ] = clipping_interval[ 0 ] + ( float( i ) / float( DEGREE ) ) * ci_width;
vec3 p = eye + x[ i ] * dir;
y[ i ] = f( p.x, p.y, p.z );
}
polynomial p;
float s[ SIZE ];
for( int i = 0; i < SIZE; i++ )
s[ i ] = p.a[ i ] = 0.0;
s[ SIZE - 1 ] = -x[ 0 ];
for( int i = 1; i < SIZE; i++ )
{
for( int j = SIZE - 1 - i; j < SIZE - 1; j++ )
s[ j ] -= x[ i ] * s[ j + 1 ];
s[ SIZE - 1 ] -= x[ i ];
}
for( int j = 0; j < SIZE; j++ )
{
float phi = float( SIZE );
for( int k = SIZE - 1; k >= 1; k-- )
phi = float( k ) * s[ k ] + x[ j ] * phi;
float ff = y[ j ] / phi;
float b = 1.0;
for( int k = SIZE - 1; k >= 0; k-- )
{
p.a[ k ] += b * ff;
b = s[ k ] + x[ j ] * b;
}
}
return p;
}
#endif
#ifdef METHOD_NEWTON_INTERPOLATION
polynomial calc_coefficients( vec3 eye, vec3 dir, vec2 clipping_interval )
{
// DEGREE + 1 Stützpunkte auf Strahl eye + t * pos berechnen
float y[ SIZE ];
float x[ SIZE ];
float ci_width = clipping_interval[ 1 ] - clipping_interval[ 0 ];
for( int i = 0; i <= DEGREE; i++ )
{
x[ i ] = clipping_interval[ 0 ] + ( float( i ) / float( DEGREE ) ) * ci_width;
vec3 p = eye + x[ i ] * dir;
y[ i ] = f( p.x, p.y, p.z );
}
// dividierte Differenzen berechen
for( int i = 1; i <= DEGREE; i++ )
for( int j = DEGREE; j >= i; j-- )
y[ j ] = ( y[ j ] - y[ j - 1 ] ) / ( x[ j ] - x[ j - i ] );
// schrittweise Koeffizienten mit Newton-Interpolationsformel berechnen
polynomial p;
float newton_basis[ SIZE ];
newton_basis[ DEGREE ] = 1.0;
p.a[ 0 ] = y[ 0 ];
for( int i = 1; i <= DEGREE; i++ )
{
// ( ai*x^i + ... + a0 ) + ( ai*x^i + ... + a0 ) * ( x - x[ i ] ) * y[ i ] = ( ai*x^i + ... + a0 ) * x - x[ i ] * y[ i ] * ( ai*x^i + ... + a0 ) berechnen
// 1. Koeffizienten der Newton-Basis um eine Potenz erhöhen (=shiften)
newton_basis[ DEGREE - i ] = 0.0;
p.a[ i ] = 0.0;
// 2. alte Koeffizienten der Newton-Basis multipliziert mit x[ i - 1 ] subtrahieren
for( int j = DEGREE - i; j < DEGREE; j++ )
newton_basis[ j ] = newton_basis[ j ] - newton_basis[ j + 1 ] * x[ i - 1 ];
// 3. y[ i ] * ( neue Newton-Basis ) auf alte Koeffizienten addieren
for( int j = 0; j <= i; j++ )
p.a[ j ] += newton_basis[ DEGREE - i + j ] * y[ i ];
}
return p;
}
#endif
#ifdef METHOD_COMPUTER_ALGEBRA
polynomial create_poly_0( float a0 )
{
polynomial res;
for( int i = 1; i <= DEGREE; i++ )
res.a[ i ] = 0.0;
res.a[ 0 ] = a0;
return res;
}
polynomial create_poly_1( float a0, float a1 )
{
polynomial res;
#if DEGREE > 1
for( int i = 2; i <= DEGREE; i++ )
res.a[ i ] = 0.0;
#endif
res.a[ 0 ] = a0;
res.a[ 1 ] = a1;
return res;
}
polynomial add( polynomial p1, polynomial p2, int res_degree )
{
for( int i = 0; i <= res_degree; i++ )
p1.a[ i ] += p2.a[ i ];
return p1;
}
polynomial sub( polynomial p1, polynomial p2, int res_degree )
{
for( int i = 0; i <= res_degree; i++ )
/* works with this line: */ // p1.a[ i ] = -( p2.a[ i ] - p1.a[ i ] );
p1.a[ i ] = p1.a[ i ] - p2.a[ i ];
return p1;
}
polynomial mult( polynomial p1, polynomial p2, int res_degree )
{
polynomial res = p1;
for( int i = 0; i <= res_degree; i++ )
{
res.a[ i ] = 0.0;
for( int j = 0; j <= i; j++ )
res.a[ i ] += p1.a[ j ] * p2.a[ i - j ];
}
return res;
}
polynomial neg( polynomial p, int res_degree )
{
for( int i = 0; i <= res_degree; i++ )
p.a[ i ] = -p.a[ i ];
return p;
}
/*
// in Theorie schneller .. in Praxis bei kleinen Potenzen nicht ;-)
// beruht auf Idee: a^n = (a^(n/2))^2
polynomial power( polynomial p, int exp, int degree )
{
polynomial result = create_poly_0( 1.0 );
if( exp != 0 )
{
polynomial y = p;
int n = exp;
int m = exp;
for( int i = 0; i < int( log2( float( exp ) ) ); i++ )
{
m = m / 2;
degree = degree + degree;
if( n > 2 * m )
result = mult( result, y, degree );
y = mult( y, y, degree );
n = m;
}
result = mult( result, y, degree );
}
return result;
}
*/
polynomial power( polynomial p, int exp, int degree )
{
polynomial res = create_poly_0( 1.0 );
for( int res_degree = degree; res_degree < degree * exp + 1; res_degree += degree )
res = mult( res, p, res_degree );
return res;
}
polynomial power_1( polynomial p, int exp )
{
// return power( p, exp, 1 );
// compute powers of p.a[ 0 ] and p.a[ 1 ]
float a0 = p.a[ 0 ];
float a1 = p.a[ 1 ];
float powers_0[ SIZE ];
float powers_1[ SIZE ];
powers_0[ 0 ] = 1.0;
powers_0[ 1 ] = a0;
powers_1[ 0 ] = 1.0;
powers_1[ 1 ] = a1;
for( int i = 2; i <= exp; i++ )
{
powers_0[ i ] = powers_0[ i - 1 ] * a0;
powers_1[ i ] = powers_1[ i - 1 ] * a1;
}
// compute coefficients of polynomials by binomial expansion
polynomial res = create_poly_0( 0.0 );
int a1_exp = exp;
int a0_exp = 0;
int bin_coeff = 1;
for( int deg = exp; deg >= 0; deg-- )
{
res.a[ deg ] = float( bin_coeff ) * powers_1[ a1_exp ] * powers_0[ a0_exp ];
a0_exp++;
bin_coeff = ( bin_coeff * a1_exp ) / a0_exp;
a1_exp--;
}
return res;
}
#endif
// Ersatz für fehlerhafte NVidia-pow-Funktion ...
float power( float base, int exp )
{
float res = 1.0;
for( int i = 0; i < exp; i++ )
res *= base;
return res;
}
/**
* methods, that operate with that algebraic function
*/
#ifdef METHOD_DESCARTES
#extension GL_EXT_gpu_shader4 : enable
float epsilon = 0.0001;
struct roots
{
float x[ DEGREE + 2 ];
bool valid[ DEGREE + 2 ];
};
float eval_p( const in polynomial p, float x )
{
float fx = p.a[ SIZE - 1 ];
for( int i = SIZE - 2; i >= 0; i-- )
fx = fx * x + p.a[ i ];
return fx;
}
float bisect( const in polynomial p, float lowerBound, float upperBound )
{
float center = lowerBound;
float old_center = upperBound;
float fl = eval_p( p, lowerBound );
float fu = eval_p( p, upperBound );
while( abs( upperBound - lowerBound ) > epsilon )
{
old_center = center;
center = 0.5 * ( lowerBound + upperBound );
float fc = eval_p( p, center );
if( fc * fl < 0.0 )
{
upperBound = center;
fu = fc;
}
else if( fc == 0.0 )
{
break;
}
else
{
lowerBound = center;
fl = fc;
}
}
return ( upperBound + lowerBound ) * 0.5;
}
/*
int stretchShiftDescartesRuleOfSignReverseShift1( polynomial p, float scale, float shift, inout polynomial tmpCoeffs )
{
float multiplier = 1.0;//pow( scale, -float( DEGREE ) );
for( int i = 0; i < SIZE; i++ )
{
tmpCoeffs.a[ SIZE - 1 - i ] = multiplier * p.a[ i ];
multiplier *= scale;
}
for( int i = 1; i <= SIZE; i++ )
for( int j = SIZE - 2; j >= i - 1; j-- )
tmpCoeffs.a[ SIZE - 1 - j ] = tmpCoeffs.a[ SIZE - 1 - j ] + shift * tmpCoeffs.a[ SIZE - 1 - ( j + 1 ) ];
int signChanges = 0;
float lastNonZeroCoeff = 0.0;
for( int i = 1; i <= SIZE; i++ )
{
for( int j = SIZE - 2; j >= i - 1; j-- )
tmpCoeffs.a[ j ] = tmpCoeffs.a[ j ] + tmpCoeffs.a[ j + 1 ];
if( tmpCoeffs.a[ i - 1 ] != 0.0 )
{
if( tmpCoeffs.a[ i - 1 ] * lastNonZeroCoeff < 0.0 )
signChanges++;
if( signChanges > 1 )
return signChanges;
lastNonZeroCoeff = tmpCoeffs.a[ i - 1 ];
}
}
return signChanges;
}
polynomial stretchNormalize0_5( polynomial p )
{
polynomial result;
result.a[ SIZE - 1 ] = p.a[ SIZE - 1 ];
float multiplier = 2.0;
for( int i = SIZE - 2; i >= 0; i-- )
{
result.a[ i ] = p.a[ i ] * multiplier;
multiplier *= 2.0;
}
return result;
}
polynomial shift1( polynomial p )
{
for( int i = 1; i <= SIZE; i++ )
for( int j = SIZE - 2; j >= i - 1; j-- )
p.a[ j ] = p.a[ j ] + p.a[ j + 1 ];
return p;
}
int descartesRuleOfSignReverseShift1( polynomial p )
{
int signChanges = 0;
float[ SIZE ] hornerCoeffs;
for( int i = 0; i < SIZE; i++ )
hornerCoeffs[ i ] = p.a[ SIZE - i - 1 ];
float lastNonZeroCoeff = 0.0;
for( int i = 1; i <= SIZE; i++ )
{
for( int j = SIZE - 2; j >= i - 1; j-- )
hornerCoeffs[ j ] = hornerCoeffs[ j ] + hornerCoeffs[ j + 1 ];
if( hornerCoeffs[ i - 1 ] != 0.0 )
{
if( hornerCoeffs[ i - 1 ] * lastNonZeroCoeff < 0.0 )
signChanges++;
if( signChanges > 1 )
return signChanges;
lastNonZeroCoeff = hornerCoeffs[ i - 1 ];
}
}
return signChanges;
}
polynomial stretchShift( polynomial p, float scale, float shift )
{
polynomial result;
float multiplier = 1.0;
for( int i = 0; i < SIZE; i++ )
{
result.a[ i ] = multiplier * p.a[ i ];
multiplier *= scale;
}
for( int i = 1; i <= SIZE; i++ )
for( int j = SIZE - 2; j >= i - 1; j-- )
result.a[ j ] = result.a[ j ] + shift * result.a[ j + 1 ];
return result;
}
polynomial stretch( polynomial p, float scale )
{
polynomial result;
float multiplier = pow( scale, -0.5 * float( DEGREE ) );
for( int i = 0; i < SIZE; i++ )
{
result.a[ i ] = multiplier * p.a[ i ];
multiplier *= scale;
}
return result;
}
polynomial shift( polynomial p, float shift )
{
polynomial result;
for( int i = 0; i < SIZE; i++ )
result.a[ i ] = p.a[ i ];
for( int i = 1; i <= SIZE; i++ )
for( int j = SIZE - 2; j >= i - 1; j-- )
result.a[ j ] = result.a[ j ] + shift * result.a[ j + 1 ];
return result;
}
*/
int shiftStretchDescartesRuleOfSignReverseShift1( in polynomial p, float shift, float scale, inout polynomial tmpCoeffs )
{
for( int i = 0; i < SIZE; i++ )
tmpCoeffs.a[ SIZE - 1 - i ] = p.a[ i ];
for( int i = 1; i <= SIZE; i++ )
for( int j = SIZE - 2; j >= i - 1; j-- )
tmpCoeffs.a[ SIZE - 1 - j ] = tmpCoeffs.a[ SIZE - 1 - j ] + shift * tmpCoeffs.a[ SIZE - 1 - ( j + 1 ) ];
float multiplier = 1.0;
for( int i = 0; i < SIZE; i++ )
multiplier *= 1.0 / scale;
for( int i = 0; i < SIZE; i++ )
{
tmpCoeffs.a[ SIZE - 1 - i ] = multiplier * tmpCoeffs.a[ SIZE - 1 - i ];
multiplier *= scale;
}
if( tmpCoeffs.a[ SIZE - 1 ] == 0.0 )
return -1;
int signChanges = 0;
float lastNonZeroCoeff = 0.0;
for( int i = 1; i <= SIZE; i++ )
{
for( int j = SIZE - 2; j >= i - 1; j-- )
tmpCoeffs.a[ j ] = tmpCoeffs.a[ j ] + tmpCoeffs.a[ j + 1 ];
if( tmpCoeffs.a[ i - 1 ] != 0.0 )
{
if( tmpCoeffs.a[ i - 1 ] * lastNonZeroCoeff < 0.0 )
signChanges++;
if( signChanges > 1 )
return signChanges;
lastNonZeroCoeff = tmpCoeffs.a[ i - 1 ];
}
}
return signChanges;
}
float findSmallestPositveRootBelow( in polynomial p, const float upperBound, inout polynomial tmp )
{
if( shiftStretchDescartesRuleOfSignReverseShift1( p, 0.0, upperBound, tmp ) > 0 )
{
float size = 0.5;
int id = 0;
while( size < 1.0 )
{
float scale = upperBound * size;
int v = shiftStretchDescartesRuleOfSignReverseShift1( p, scale * float( id ), scale, tmp );
if( v > 1 )
{
// go deeper on left side
id *= 2;
size /= 2.0;
}
else if( v == 0 )
{
// go right
while( id % 2 == 1 )
{
id /= 2;
size *= 2.0;
}
id++;
}
else if( v == 1 )
{
return bisect( p, scale * float( id ), scale * float( id + 1 ) );
}
else
{
return scale * float( id );
}
}
}
return upperBound + 1.0;
}
/**
* uses nearly the same algorithm as for positive roots:
* 1. transforms x |-> -x (the new task is to find the largest positive root below -lowerBound)
* 2. use bisection as in findSmallestPositveRootBelow, but start from the upper bound (=-lowerBound) of the search interval
*/
float findSmallestNegativeRootAbove( in polynomial p, const float lowerBound, inout polynomial tmp )
{
for( int i = 0; i < SIZE; i++ )
if( i % 2 == 1 )
p.a[ i ] = -p.a[ i ];
float upperBound = -lowerBound;
if( shiftStretchDescartesRuleOfSignReverseShift1( p, 0.0, upperBound, tmp ) > 0 )
{
float size = 0.5;
int id = 0;
while( size < 1.0 )
{
float intervalStart = upperBound * ( 1.0 - size * float( id + 1 ) );
int v = shiftStretchDescartesRuleOfSignReverseShift1( p, intervalStart, size * upperBound, tmp );
if( v > 1 )
{
// go deeper on left side
id *= 2;
size /= 2.0;
}
else if( v == 0 )
{
// go right
while( id % 2 == 1 )
{
id /= 2;
size *= 2.0;
}
id++;
}
else if( v == 1 )
{
return -bisect( p, intervalStart, intervalStart + upperBound * size );
}
else
{
return intervalStart;
}
}
}
return lowerBound - 1.0;
}
float findFirstRootIn( in polynomial p, float lowerBound, float upperBound )
{
polynomial tmp;
if( lowerBound < 0.0 )
{
float root = findSmallestNegativeRootAbove( p, lowerBound, tmp );
if( root >= lowerBound )
return root;
}
if( p.a[ 0 ] == 0.0 )
return 0.0;
if( upperBound > 0.0 )
{
return findSmallestPositveRootBelow( p, upperBound, tmp );
}
return lowerBound - 1.0;
}
roots solve( in polynomial p, const in vec2 trace_interval )
{
float intervalSize = trace_interval[ 1 ] - trace_interval[ 0 ];
// init result array
roots res;
res.x[ 0 ] = trace_interval[ 0 ];
res.valid[ 0 ] = false;
for( int i = 1; i < DEGREE + 2; i++ )
{
res.x[ i ] = trace_interval[ 1 ];
res.valid[ i ] = false;
}
res.x[ 1 ] = findFirstRootIn( p, trace_interval[ 0 ], trace_interval[ 1 ] );
if( res.x[ 1 ] >= trace_interval[ 0 ] && res.x[ 1 ] <= trace_interval[ 1 ] )
res.valid[ 1 ] = true;
return res;
}
#endif
#ifdef METHOD_STURM
float epsilon = 0.0001;
float sturm_chain[ ( SIZE * ( SIZE + 1 ) ) / 2 ];
int sc_index( int f_index, int c_index )
{
//~ int result = 0;
//~ for( int i = DEGREE; i > f_index; i-- )
//~ result += i;
//~ return result + c_index;
return ( SIZE * ( SIZE + 1 ) ) / 2 - 1 - ( ( f_index + 1 ) * ( f_index + 2 ) ) / 2 + c_index;
}
float eval_f( float where )
{
float res = 0.0;
for( int i = DEGREE; i >= 0; i-- )
res = sturm_chain[ sc_index( DEGREE, i ) ] + where * res;
return res;
}
float bisection( float x0, float x1 )
{
float f0 = eval_f( x0 );
float f1 = eval_f( x1 );
float x2 = x0;
while( abs( x0 - x1 ) > epsilon )
{
x2 = 0.5 * ( x0 + x1 );
float f2 = eval_f( x2 );
if( f2 * f0 < 0.0 )
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
}
return x2;
}
bool contains( const in vec2 interval, const in float value )
{
return ( interval[ 0 ] < value && value < interval[ 1 ] ) || ( interval[ 0 ] > value && value > interval[ 1 ] );
}
void polynom_div( int dividend, int divisor, int remainder )
{
// copy dividend to be the current remainder
int i;
for( i = 0; i <= dividend; i++ )
sturm_chain[ sc_index( remainder, i )] = sturm_chain[ sc_index( dividend, i ) ];
int degree_diff = dividend - divisor;
for( i = dividend; i >= divisor; i-- )
{
// calculate quotient of highest coefficient
float quotient = sturm_chain[ sc_index( remainder, i ) ] / sturm_chain[ sc_index( divisor, divisor ) ];
// after this step the highest coeff. of the old remainder is actually zero
//sturm_chain[ remainder * SIZE + i ] = 0.0; // unnecessary calculation, because value is known
// calculate new coeffs. of the remainder
for( int j = 0; j < divisor; j++ )
sturm_chain[ sc_index( remainder, j + degree_diff ) ] = sturm_chain[ sc_index( remainder, j + degree_diff ) ] - sturm_chain[ sc_index( divisor, j ) ] * quotient;
degree_diff--;
}
}
void construct_sturm_chain()
{
// calculate first derivate of f
int i;
//sturm_chain[ DEGREE - 1 ][ DEGREE ] = 0.0f;
for( i = 1; i <= DEGREE; i++ )
sturm_chain[ sc_index( DEGREE - 1, i - 1 ) ] = float( i ) * sturm_chain[ sc_index( DEGREE, i ) ];// / ( DEGREE * sturm_chain[ DEGREE ][ DEGREE ] );
// calculate sturm chain
for( i = DEGREE - 2; i >= 0; i-- )
{
// polynom division, which outputs the remainder to the sturm_chain-array
polynom_div( i + 2, i + 1, i );
// flip the sign of the remainder and normalize polynom
for( int j = 0; j <= i; j++ )
sturm_chain[ sc_index( i, j ) ] = -sturm_chain[ sc_index( i, j ) ];
}
}
float f_sturm( int num, float t )
{
float res = 0.0;
for( int i = num; i >= 0; i-- )
res = sturm_chain[ sc_index( num, i ) ] + t * res;
return res;
}
int sign_change( float t )
{
int sign_sum = 0;
float last_sign, cur_sign;
// #Vorzeichenwechsel an t berechnen
last_sign = sign( f_sturm( DEGREE, t ) );
for( int i = DEGREE - 1; i >= 0; i-- )
{
cur_sign = sign( f_sturm( i, t ) );
sign_sum += ( last_sign != cur_sign ) ? 1 : 0;
if( cur_sign != 0.0 )
last_sign = cur_sign;
}
return sign_sum;
}
float bisection_sturm( float x0, float x1 )
{
float x2 = x0 - 1.0;
int sign_change_0 = sign_change( x0 );
int sign_change_1 = sign_change( x1 );
if( sign_change_0 - sign_change_1 != 0 )
{
{
float f0 = eval_f( x0 );
float f1 = eval_f( x1 );
while( !( sign_change_0 - sign_change_1 == 1 && f0 * f1 < 0.0 ) )
{
x2 = 0.5 * ( x0 + x1 );
int sign_change_2 = sign_change( x2 );
float f2 = eval_f( x2 );
if( sign_change_0 - sign_change_2 > 0 )
{
// there is a root in the first interval -> search in first
x1 = x2;
f1 = f2;
sign_change_1 = sign_change_2;
}
else
{
// there is no root in the first interval -> search in second
x0 = x2;
f0 = f2;
sign_change_0 = sign_change_2;
}
}
}
x2 = bisection( x0, x1 );
}
return x2;
}
struct roots
{
float x[ DEGREE + 2 ];
bool valid[ DEGREE + 2 ];
};
roots solve( const in polynomial p, const in vec2 trace_interval )
{
#if DEGREE > 1
// fill sturm chain array
for( int i = 0; i < SIZE; i++ )
sturm_chain[ sc_index( DEGREE, i ) ] = p.a[ i ];
construct_sturm_chain();
#endif
// init result array
roots res;
res.x[ 0 ] = trace_interval[ 0 ];
res.valid[ 0 ] = false;
for( int i = 1; i < DEGREE + 2; i++ )
{
res.x[ i ] = trace_interval[ 1 ];
res.valid[ i ] = false;
}
#if DEGREE > 1
// apply sturm's algorithm
res.x[ 1 ] = bisection_sturm( trace_interval[ 0 ], trace_interval[ 1 ] );
#else
// solve linear equation directly
res.x[ 1 ] = -p.a[ 0 ] / p.a[ 1 ];
#endif
res.valid[ 1 ] = contains( trace_interval, res.x[ 1 ] );
return res;
}
#endif
#ifdef METHOD_D_CHAIN
float epsilon = 0.000001;
polynomial derivatives[ DEGREE ];
float eval_f( int which, float where )
{
float res = 0.0;
for( int i = DEGREE - which; i >= 0; i-- )
res = derivatives[ which ].a[ i ] + where * res;
return res;
}
void calc_derivatives()
{
for( int derivate = 1; derivate < DEGREE; derivate++ )
for( int j = 0; j <= DEGREE - derivate; j++ )
derivatives[ derivate ].a[ j ] = float( j + 1 ) * derivatives[ derivate - 1 ].a[ j + 1 ];
}
#ifdef SUB_METHOD_BISECTION
float bisection( int f_index, float x0, float f0, float x1, float f1 )
{
float x2 = x0;
while( abs( x0 - x1 ) > epsilon )
{
x2 = 0.5 * ( x0 + x1 );
float f2 = eval_f( f_index, x2 );
if( f2 * f0 < 0.0 )
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
}
return x2;
}
#endif
#ifdef SUB_METHOD_REGULA_FALSI
float regula_falsi( int f_index, float x0, float f0, float x1, float f1 )
{
float x2 = x0;
while( abs( x0 - x1 ) > epsilon )
{
x2 = x0 - f0 * ( x1 - x0 ) / ( f1 - f0 );
float f2 = eval_f( f_index, x2 );
if( f2 * f0 < 0.0 )
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
}
return x2;
}
#endif
struct roots
{
float x[ DEGREE + 2 ];
bool valid[ DEGREE + 2 ];
};
bool contains( const in vec2 interval, const in float value )
{
return ( interval[ 0 ] < value && value < interval[ 1 ] ) || ( interval[ 0 ] > value && value > interval[ 1 ] );
}
void calc_roots( const in int derivate_num, const in roots read_from, inout roots write_to )
{
// for each root pair of the current derivate: look for roots of previous derivate
for( int interval_num = 0; interval_num < DEGREE + 2 - 1 - derivate_num; interval_num++ )
{
write_to.x[ interval_num + 1 ] = write_to.x[ interval_num ];
write_to.valid[ interval_num + 1 ] = false;
if( read_from.valid[ interval_num + 1 ] )
{
float f0 = eval_f( derivate_num, read_from.x[ interval_num ] );
float f1 = eval_f( derivate_num, read_from.x[ interval_num + 1 ] );
if( write_to.valid[ interval_num + 1 ] = ( f0 * f1 < 0.0 ) )
// there is one root in current interval and root finder will converge
#ifdef SUB_METHOD_BISECTION
write_to.x[ interval_num + 1 ] = bisection( derivate_num, read_from.x[ interval_num ], f0, read_from.x[ interval_num + 1 ], f1 );
#endif
#ifdef SUB_METHOD_REGULA_FALSI
write_to.x[ interval_num + 1 ] = regula_falsi( derivate_num, read_from.x[ interval_num ], f0, read_from.x[ interval_num + 1 ], f1 );
#endif
}
}
write_to.x[ DEGREE - derivate_num + 1 ] = read_from.x[ DEGREE - derivate_num + 1 ];
write_to.valid[ DEGREE - derivate_num + 1 ] = true;
}
roots solve( in polynomial p, const in vec2 trace_interval )
{
// copy coefficients in polynom array
derivatives[ 0 ] = p;
// fill derivatives array
calc_derivatives();
// arrays of roots which are swaped after each iteration
roots read_from, write_to;
read_from.x[ 0 ] = write_to.x[ 0 ] = trace_interval[ 0 ];
read_from.valid[ 0 ] = write_to.valid[ 0 ] = true;
read_from.x[ 1 ] = write_to.x[ 1 ] = trace_interval[ 0 ];
read_from.valid[ 1 ] = write_to.valid[ 1 ] = false;
read_from.x[ 2 ] = write_to.x[ 2 ] = trace_interval[ 1 ];
read_from.valid[ 2 ] = write_to.valid[ 2 ] = true;
for( int i = 3; i < DEGREE + 2; i++ )
{
read_from.x[ i ] = write_to.x[ i ] = trace_interval[ 1 ];
read_from.valid[ i ] = write_to.valid[ i ] = false;
}
// basic case: calculate root of degree 1 derivate
float lin_root = -derivatives[ DEGREE - 1 ].a[ 0 ] / derivatives[ DEGREE - 1 ].a[ 1 ];
if( contains( trace_interval, lin_root ) )
{
read_from.x[ 1 ] = lin_root;
read_from.valid[ 1 ] = true;
}
#if DEGREE > 1
for( int i = DEGREE - 2; i >= 0; i-- )
{
calc_roots( i, read_from, write_to );
// copy result to the input buffer of the next iteration
read_from = write_to;
}
#endif
return read_from;
}
#endif
#ifdef METHOD_INTERVAL
polynomial derivatives[ DEGREE + 1 ];
float eval_f( int which, float where )
{
float res = 0.0;
for( int i = which; i >= 0; i-- )
res = derivatives[ which ].a[ i ] + where * res;
return res;
}
void calc_derivatives()
{
for( int derivate = DEGREE - 1; derivate >= 0; derivate-- )
for( int j = 0; j <= derivate; j++ )
derivatives[ derivate ].a[ j ] = float( j + 1 ) * derivatives[ derivate + 1 ].a[ j + 1 ];
}
// interval operations
vec2 mult_01( vec2 i ) { return vec2( min( 0.0, i[ 0 ] ), max( 0.0, i[ 1 ] ) ); }
vec2 make_n11( float f ) { return vec2( min( -f, f ), max( -f, f ) ); }
#ifdef SUB_METHOD_BISECTION
float epsilon = 0.003;
vec2 taylor_bound( vec2 i_x )
{
#if DEGREE == 1
float f0 = eval_f( DEGREE, i_x[ 0 ] );
float f1 = eval_f( DEGREE, i_x[ 1 ] );
return vec2( min( f0, f1 ), max( f0, f1 ) );
#else
float x_0 = ( i_x[ 0 ] + i_x[ 1 ] ) * 0.5;
float x_1 = ( i_x[ 1 ] - i_x[ 0 ] ) * 0.5;
#if DEGREE / 2 == SIZE / 2
// base case
vec2 bound = vec2( eval_f( 0, x_0 ) );
// iterative variant of recursive taylor method
for( int derivate = 2; derivate <= DEGREE; derivate += 2 )
bound = vec2( eval_f( derivate, x_0 ) ) + make_n11( x_1 * eval_f( derivate - 1, x_0 ) ) + ( 0.5 * x_1 * x_1 ) * mult_01( bound );
#else
// base case
vec2 bound = vec2( eval_f( 1, x_0 ) ) + make_n11( x_1 * eval_f( 0, x_0 ) );
// iterative variant of recursive taylor method
for( int derivate = 3; derivate <= DEGREE; derivate += 2 )
bound = vec2( eval_f( derivate, x_0 ) ) + make_n11( x_1 * eval_f( derivate - 1, x_0 ) ) + ( 0.5 * x_1 * x_1 ) * mult_01( bound );
#endif
return bound;
#endif
}
float bisection_interval( float x0, float x1 )
{
float x_upper_bound = x1;
float x2 = x1;
vec2 bound = taylor_bound( vec2( x0, x1 ) );
while( ( bound[ 0 ] <= 0.0 && bound[ 1 ] >= 0.0 ) && x1 - x0 > epsilon && x0 < x_upper_bound )
{