Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ipo.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8407c66d584e595dd691a091eee953d21958166d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
/* ipo.c
 * 
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): 2008, Joshua Leung (IPO System cleanup)
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <math.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "MEM_guardedalloc.h"

#include "DNA_action_types.h"
#include "DNA_armature_types.h"
#include "DNA_curve_types.h"
#include "DNA_camera_types.h"
#include "DNA_lamp_types.h"
#include "DNA_ipo_types.h"
#include "DNA_key_types.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_object_force.h"
#include "DNA_particle_types.h"
#include "DNA_sequence_types.h"
#include "DNA_scene_types.h"
#include "DNA_sound_types.h"
#include "DNA_texture_types.h"
#include "DNA_view3d_types.h"
#include "DNA_world_types.h"

#include "BLI_blenlib.h"
#include "BLI_arithb.h"

#include "BKE_utildefines.h"

#include "BKE_action.h"
#include "BKE_blender.h"
#include "BKE_curve.h"
#include "BKE_constraint.h"
#include "BKE_global.h"
#include "BKE_ipo.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_object.h"

#ifndef DISABLE_PYTHON
#include "BPY_extern.h" /* for BPY_pydriver_eval() */
#endif

#define SMALL -1.0e-10

/* ***************************** Adrcode Blocktype Defines ********************************* */

/* This array concept was meant to make sure that defines such as OB_LOC_X
   don't have to be enumerated, also for backward compatibility, future changes,
   and to enable it all can be accessed with a for-next loop.
   
   This should whole adrcode system should eventually be replaced by a proper Data API
*/


int co_ar[CO_TOTIPO]= {
	CO_ENFORCE, CO_HEADTAIL
};

int ob_ar[OB_TOTIPO]= {
	OB_LOC_X, OB_LOC_Y, OB_LOC_Z, OB_DLOC_X, OB_DLOC_Y, OB_DLOC_Z, 
	OB_ROT_X, OB_ROT_Y, OB_ROT_Z, OB_DROT_X, OB_DROT_Y, OB_DROT_Z, 
	OB_SIZE_X, OB_SIZE_Y, OB_SIZE_Z, OB_DSIZE_X, OB_DSIZE_Y, OB_DSIZE_Z, 
	OB_LAY, OB_TIME, OB_COL_R, OB_COL_G, OB_COL_B, OB_COL_A,
	OB_PD_FSTR, OB_PD_FFALL, OB_PD_SDAMP, OB_PD_RDAMP, OB_PD_PERM, OB_PD_FMAXD
};

int ac_ar[AC_TOTIPO]= {
	AC_LOC_X, AC_LOC_Y, AC_LOC_Z,  
	 AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z,
	AC_SIZE_X, AC_SIZE_Y, AC_SIZE_Z
};

int ma_ar[MA_TOTIPO]= {
	MA_COL_R, MA_COL_G, MA_COL_B, 
	MA_SPEC_R, MA_SPEC_G, MA_SPEC_B, 
	MA_MIR_R, MA_MIR_G, MA_MIR_B,
	MA_REF, MA_ALPHA, MA_EMIT, MA_AMB, 
	MA_SPEC, MA_HARD, MA_SPTR, MA_IOR, 
	MA_MODE, MA_HASIZE, MA_TRANSLU, MA_RAYM,
	MA_FRESMIR, MA_FRESMIRI, MA_FRESTRA, MA_FRESTRAI, MA_ADD,
	
	MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
	MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
	MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
	MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF, MA_MAP1+MAP_DISP
};

int te_ar[TE_TOTIPO] ={
	
	TE_NSIZE, TE_NDEPTH, TE_NTYPE, TE_TURB,
	
	TE_VNW1, TE_VNW2, TE_VNW3, TE_VNW4,
	TE_VNMEXP, TE_VN_COLT, TE_VN_DISTM,
	
	TE_ISCA, TE_DISTA,
	
	TE_MG_TYP, TE_MGH, TE_MG_LAC, TE_MG_OCT, TE_MG_OFF, TE_MG_GAIN,
	
	TE_N_BAS1, TE_N_BAS2,
	
	TE_COL_R, TE_COL_G, TE_COL_B, TE_BRIGHT, TE_CONTRA
};

int seq_ar[SEQ_TOTIPO]= {
	SEQ_FAC1
};

int cu_ar[CU_TOTIPO]= {
	CU_SPEED
};

int wo_ar[WO_TOTIPO]= {
	WO_HOR_R, WO_HOR_G, WO_HOR_B, WO_ZEN_R, WO_ZEN_G, WO_ZEN_B, 
	WO_EXPOS, WO_MISI, WO_MISTDI, WO_MISTSTA, WO_MISTHI,
	WO_STAR_R, WO_STAR_G, WO_STAR_B, WO_STARDIST, WO_STARSIZE, 

	MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
	MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
	MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
	MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF, MA_MAP1+MAP_NORF, MA_MAP1+MAP_VARF
};

int la_ar[LA_TOTIPO]= {
	LA_ENERGY, LA_COL_R, LA_COL_G, LA_COL_B, 
	LA_DIST, LA_SPOTSI, LA_SPOTBL, 
	LA_QUAD1, LA_QUAD2, LA_HALOINT,  

	MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, 
	MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, 
	MA_MAP1+MAP_R, MA_MAP1+MAP_G, MA_MAP1+MAP_B,
	MA_MAP1+MAP_DVAR, MA_MAP1+MAP_COLF
};

/* yafray: aperture & focal distance curves added */
/* qdn: FDIST now available to Blender as well for defocus node */
int cam_ar[CAM_TOTIPO]= {
	CAM_LENS, CAM_STA, CAM_END, CAM_YF_APERT, CAM_YF_FDIST, CAM_SHIFT_X, CAM_SHIFT_Y
};

int snd_ar[SND_TOTIPO]= {
	SND_VOLUME, SND_PITCH, SND_PANNING, SND_ATTEN
};

int fluidsim_ar[FLUIDSIM_TOTIPO]= {
	FLUIDSIM_VISC, FLUIDSIM_TIME,
	FLUIDSIM_GRAV_X , FLUIDSIM_GRAV_Y , FLUIDSIM_GRAV_Z ,
	FLUIDSIM_VEL_X  , FLUIDSIM_VEL_Y  , FLUIDSIM_VEL_Z  ,
	FLUIDSIM_ACTIVE,
	FLUIDSIM_ATTR_FORCE_STR, FLUIDSIM_ATTR_FORCE_RADIUS,
	FLUIDSIM_VEL_FORCE_STR, FLUIDSIM_VEL_FORCE_RADIUS,
};

int part_ar[PART_TOTIPO]= {
	PART_EMIT_FREQ, PART_EMIT_LIFE, PART_EMIT_VEL, PART_EMIT_AVE, PART_EMIT_SIZE,
	PART_AVE, PART_SIZE, PART_DRAG, PART_BROWN, PART_DAMP, PART_LENGTH, PART_CLUMP,
    PART_GRAV_X, PART_GRAV_Y, PART_GRAV_Z, PART_KINK_AMP, PART_KINK_FREQ, PART_KINK_SHAPE,
	PART_BB_TILT, PART_PD_FSTR, PART_PD_FFALL, PART_PD_FMAXD, PART_PD2_FSTR, PART_PD2_FFALL, PART_PD2_FMAXD
};

/* ************************** Data-Level Functions ************************* */

/* ---------------------- Freeing --------------------------- */

/* frees the ipo curve itself too */
void free_ipo_curve (IpoCurve *icu) 
{
	if (icu == NULL) 
		return;
	
	if (icu->bezt) 
		MEM_freeN(icu->bezt);
	if (icu->driver) 
		MEM_freeN(icu->driver);
	
	MEM_freeN(icu);
}

/* do not free ipo itself */
void free_ipo (Ipo *ipo)
{
	IpoCurve *icu, *icn;
	
	if (ipo == NULL) 
		return;
	
	for (icu= ipo->curve.first; icu; icu= icn) {
		icn= icu->next;
		
		/* must remove the link before freeing, as the curve is freed too */
		BLI_remlink(&ipo->curve, icu);
		free_ipo_curve(icu);
	}
}

/* ---------------------- Init --------------------------- */

/* on adding new ipos, or for empty views */
void ipo_default_v2d_cur (int blocktype, rctf *cur)
{
	switch (blocktype) {
	case ID_CA:
		cur->xmin= (float)G.scene->r.sfra;
		cur->xmax= (float)G.scene->r.efra;
		cur->ymin= 0.0f;
		cur->ymax= 100.0f;
		break;
		
	case ID_MA: case ID_WO: case ID_LA: 
	case ID_CU: case ID_CO:
		cur->xmin= (float)(G.scene->r.sfra - 0.1f);
		cur->xmax= (float)G.scene->r.efra;
		cur->ymin= (float)-0.1f;
		cur->ymax= (float)+1.1f;
		break;
		
	case ID_TE:
		cur->xmin= (float)(G.scene->r.sfra - 0.1f);
		cur->xmax= (float)G.scene->r.efra;
		cur->ymin= (float)-0.1f;
		cur->ymax= (float)+1.1f;
		break;
		
	case ID_SEQ:
		cur->xmin= -5.0f;
		cur->xmax= 105.0f;
		cur->ymin= (float)-0.1f;
		cur->ymax= (float)+1.1f;
		break;
		
	case ID_KE:
		cur->xmin= (float)(G.scene->r.sfra - 0.1f);
		cur->xmax= (float)G.scene->r.efra;
		cur->ymin= (float)-0.1f;
		cur->ymax= (float)+2.1f;
		break;
		
	default:	/* ID_OB and everything else */
		cur->xmin= (float)G.scene->r.sfra;
		cur->xmax= (float)G.scene->r.efra;
		cur->ymin= -5.0f;
		cur->ymax= +5.0f;
		break;
	}
}

/* create a new IPO block (allocates the block) */
Ipo *add_ipo (char name[], int blocktype)
{
	Ipo *ipo;
	
	ipo= alloc_libblock(&G.main->ipo, ID_IP, name);
	ipo->blocktype= blocktype;
	ipo_default_v2d_cur(blocktype, &ipo->cur);

	return ipo;
}

/* ---------------------- Copy --------------------------- */

/* duplicate an IPO block and all its data  */
Ipo *copy_ipo (Ipo *src)
{
	Ipo *dst;
	IpoCurve *icu;
	
	if (src == NULL) 
		return NULL;
	
	dst= copy_libblock(src);
	BLI_duplicatelist(&dst->curve, &src->curve);

	for (icu= src->curve.first; icu; icu= icu->next) {
		icu->bezt= MEM_dupallocN(icu->bezt);
		
		if (icu->driver) 
			icu->driver= MEM_dupallocN(icu->driver);
	}
	
	return dst;
}

/* ---------------------- Relink --------------------------- */

/* uses id->newid to match pointers with other copied data 
 * 	- called after single-user or other such
 */
void ipo_idnew (Ipo *ipo)
{
	if (ipo) {
		IpoCurve *icu;
		
		for (icu= ipo->curve.first; icu; icu= icu->next) {
			if (icu->driver)
				ID_NEW(icu->driver->ob);
		}
	}
}

/* --------------------- Find + Check ----------------------- */

/* find the IPO-curve within a given IPO-block with the adrcode of interest */
IpoCurve *find_ipocurve (Ipo *ipo, int adrcode)
{
	if (ipo) {
		IpoCurve *icu;
		
		for (icu= ipo->curve.first; icu; icu= icu->next) {
			if (icu->adrcode == adrcode) 
				return icu;
		}
	}
	return NULL;
}

/* return whether the given IPO block has a IPO-curve with the given adrcode */
short has_ipo_code(Ipo *ipo, int adrcode)
{
	/* return success of faliure from trying to find such an IPO-curve */
	return (find_ipocurve(ipo, adrcode) != NULL);
}

/* ---------------------- Make Local --------------------------- */


/* make the given IPO local (for Objects)
 * - only lib users: do nothing
 * - only local users: set flag
 * - mixed: make copy
 */
void make_local_obipo (Ipo *src)
{
	Object *ob;
	Ipo *dst;
	int local=0, lib=0;
	
	/* check if only local and/or lib */
	for (ob= G.main->object.first; ob; ob= ob->id.next) {
		if (ob->ipo == src) {
			if (ob->id.lib) lib= 1;
			else local= 1;
		}
	}
	
	/* only local - set flag */
	if (local && lib==0) {
		src->id.lib= 0;
		src->id.flag= LIB_LOCAL;
		new_id(0, (ID *)src, 0);
	}
	/* mixed: make copy */
	else if (local && lib) {
		dst= copy_ipo(src);
		dst->id.us= 0;
		
		for (ob= G.main->object.first; ob; ob= ob->id.next) {
			if (ob->ipo == src) {
				if (ob->id.lib == NULL) {
					ob->ipo= dst;
					dst->id.us++;
					src->id.us--;
				}
			}
		}
	}
}

/* make the given IPO local (for Materials)
 * - only lib users: do nothing
 * - only local users: set flag
 * - mixed: make copy
 */
void make_local_matipo (Ipo *src)
{
	Material *ma;
	Ipo *dst;
	int local=0, lib=0;
	
	/* check if only local and/or lib */
	for (ma= G.main->mat.first; ma; ma= ma->id.next) {
		if (ma->ipo == src) {
			if (ma->id.lib) lib= 1;
			else local= 1;
		}
	}
	
	/* only local - set flag */
	if (local && lib==0) {
		src->id.lib= 0;
		src->id.flag= LIB_LOCAL;
		new_id(0, (ID *)src, 0);
	}
	/* mixed: make copy */
	else if (local && lib) {
		dst= copy_ipo(src);
		dst->id.us= 0;
		
		for (ma= G.main->mat.first; ma; ma= ma->id.next) {
			if (ma->ipo == src) {
				if (ma->id.lib == NULL) {
					ma->ipo= dst;
					dst->id.us++;
					src->id.us--;
				}
			}
		}
	}
}

/* make the given IPO local (for ShapeKeys)
 * - only lib users: do nothing
 * - only local users: set flag
 * - mixed: make copy
 */
void make_local_keyipo (Ipo *src)
{
	Key *key;
	Ipo *dst;
	int local=0, lib=0;
	
	/* check if only local and/or lib */
	for (key= G.main->key.first; key; key= key->id.next) {
		if (key->ipo == src) {
			if (key->id.lib) lib= 1;
			else local= 1;
		}
	}
	
	/* only local - set flag */
	if (local && lib==0) {
		src->id.lib= 0;
		src->id.flag= LIB_LOCAL;
		new_id(0, (ID *)src, 0);
	}
	/* mixed: make copy */
	else if (local && lib) {
		dst= copy_ipo(src);
		dst->id.us= 0;
		
		for (key= G.main->key.first; key; key= key->id.next) {
			if (key->ipo == src) {
				if (key->id.lib == NULL) {
					key->ipo= dst;
					dst->id.us++;
					src->id.us--;
				}
			}
		}
	}
}


/* generic call to make IPO's local */
void make_local_ipo (Ipo *ipo)
{
	/* can't touch lib-linked data */
	if (ipo->id.lib == NULL) 
		return;
		
	/* with only one user, just set local flag */
	if (ipo->id.us == 1) {
		ipo->id.lib= 0;
		ipo->id.flag= LIB_LOCAL;
		new_id(0, (ID *)ipo, 0);
		return;
	}
	
	/* when more than 1 user, can only make local for certain blocktypes */
	switch (ipo->blocktype) {
		case ID_OB:
			make_local_obipo(ipo);
			break;
		case ID_MA:
			make_local_matipo(ipo);
			break;
		case ID_KE:
			make_local_keyipo(ipo);
			break;
	}
}

/* ***************************** Keyframe Column Tools ********************************* */

/* add a BezTriple to a column */
void add_to_cfra_elem(ListBase *lb, BezTriple *bezt)
{
	CfraElem *ce, *cen;
	
	for (ce= lb->first; ce; ce= ce->next) {
		/* double key? */
		if (ce->cfra == bezt->vec[1][0]) {
			if (bezt->f2 & SELECT) ce->sel= bezt->f2;
			return;
		}
		/* should key be inserted before this column? */
		else if (ce->cfra > bezt->vec[1][0]) break;
	}
	
	/* create a new column */
	cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem");	
	if (ce) BLI_insertlinkbefore(lb, ce, cen);
	else BLI_addtail(lb, cen);

	cen->cfra= bezt->vec[1][0];
	cen->sel= bezt->f2;
}

/* make a list of keyframe 'columns' in an IPO block */
void make_cfra_list (Ipo *ipo, ListBase *elems)
{
	IpoCurve *icu;
	BezTriple *bezt;
	int a;
	
	for (icu= ipo->curve.first; icu; icu= icu->next) {
		if (icu->flag & IPO_VISIBLE) {
			/* ... removed old checks for adrcode types from here ...
			 * 	- (was this used for IpoKeys in the past?)
			 */
			
			bezt= icu->bezt;
			if (bezt) {
				for (a=0; a < icu->totvert; a++, bezt++) {
					add_to_cfra_elem(elems, bezt);
				}
			}
		}
	}
}

/* ***************************** Timing Stuff ********************************* */

/* This (evil) function is needed to cope with two legacy Blender rendering features
 * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
 * rendering. Thus, the use of ugly globals from object.c
 */
// BAD... EVIL... JUJU...!!!!
float frame_to_float (int cfra)		/* see also bsystem_time in object.c */
{
	extern float bluroffs;	/* bad stuff borrowed from object.c */
	extern float fieldoffs;
	float ctime;
	
	ctime= (float)cfra;
	ctime+= bluroffs+fieldoffs;
	ctime*= G.scene->r.framelen;
	
	return ctime;
}

/* ***************************** IPO Curve Sanity ********************************* */
/* The functions here are used in various parts of Blender, usually after some editing
 * of keyframe data has occurred. They ensure that keyframe data is properly ordered and
 * that the handles are correctly 
 */

/* This function recalculates the handles of an IPO-Curve 
 * If the BezTriples have been rearranged, sort them first before using this.
 */
void calchandles_ipocurve (IpoCurve *icu)
{
	BezTriple *bezt, *prev, *next;
	int a= icu->totvert;

	/* Error checking:
	 *	- need at least two points
	 *	- need bezier keys
	 *	- only bezier-interpolation has handles (for now)
	 */
	if (ELEM(NULL, icu, icu->bezt) || (a < 2) || ELEM(icu->ipo, IPO_CONST, IPO_LIN)) 
		return;
	
	/* get initial pointers */
	bezt= icu->bezt;
	prev= NULL;
	next= (bezt + 1);
	
	/* loop over all beztriples, adjusting handles */
	while (a--) {
		/* clamp timing of handles to be on either side of beztriple */
		if (bezt->vec[0][0] > bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0];
		if (bezt->vec[2][0] < bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0];
		
		/* calculate autohandles */
		if (icu->flag & IPO_AUTO_HORIZ) 
			calchandleNurb(bezt, prev, next, 2);	/* 2==special autohandle && keep extrema horizontal */
		else
			calchandleNurb(bezt, prev, next, 1);	/* 1==special autohandle */
		
		/* for automatic ease in and out */
		if ((bezt->h1==HD_AUTO) && (bezt->h2==HD_AUTO)) {
			/* only do this on first or last beztriple */
			if ((a==0) || (a==icu->totvert-1)) {
				/* set both handles to have same horizontal value as keyframe */
				if (icu->extrap==IPO_HORIZ) {
					bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
				}
			}
		}
		
		/* advance pointers for next iteration */
		prev= bezt;
		if (a == 1) next= NULL;
		else next++;
		bezt++;
	}
}

/* Use when IPO-Curve with handles has changed
 * It treats all BezTriples with the following rules:
 *  - PHASE 1: do types have to be altered?
 * 		-> Auto handles: become aligned when selection status is NOT(000 || 111)
 * 		-> Vector handles: become 'nothing' when (one half selected AND other not)
 *  - PHASE 2: recalculate handles
*/
void testhandles_ipocurve (IpoCurve *icu)
{
	BezTriple *bezt;
	int a;

	/* only beztriples have handles (bpoints don't though) */
	if (ELEM(NULL, icu, icu->bezt))
		return;
	
	/* loop over beztriples */
	for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) {
		short flag= 0;
		
		/* flag is initialised as selection status
		 * of beztriple control-points (labelled 0,1,2)
		 */
		if (bezt->f1 & SELECT) flag |= (1<<0); // == 1
		if (bezt->f2 & SELECT) flag |= (1<<1); // == 2
		if (bezt->f3 & SELECT) flag |= (1<<2); // == 4
		
		/* one or two handles selected only */
		if (ELEM(flag, 0, 7)==0) {
			/* auto handles become aligned */
			if (bezt->h1==HD_AUTO)
				bezt->h1= HD_ALIGN;
			if(bezt->h2==HD_AUTO)
				bezt->h2= HD_ALIGN;
			
			/* vector handles become 'free' when only one half selected */
			if(bezt->h1==HD_VECT) {
				/* only left half (1 or 2 or 1+2) */
				if (flag < 4) 
					bezt->h1= 0;
			}
			if(bezt->h2==HD_VECT) {
				/* only right half (4 or 2+4) */
				if (flag > 3) 
					bezt->h2= 0;
			}
		}
	}

	/* recalculate handles */
	calchandles_ipocurve(icu);
}

/* This function sorts BezTriples so that they are arranged in chronological order,
 * as tools working on IPO-Curves expect that the BezTriples are in order.
 */
void sort_time_ipocurve(IpoCurve *icu)
{
	short ok= 1;
	
	/* keep adjusting order of beztriples until nothing moves (bubble-sort) */
	while (ok) {
		ok= 0;
		
		/* currently, will only be needed when there are beztriples */
		if (icu->bezt) {
			BezTriple *bezt;
			int a;
			
			/* loop over ALL points to adjust position in array and recalculate handles */
			for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) {
				/* check if thee's a next beztriple which we could try to swap with current */
				if (a < (icu->totvert-1)) {
					/* swap if one is after the other (and indicate that order has changed) */
					if (bezt->vec[1][0] > (bezt+1)->vec[1][0]) {
						SWAP(BezTriple, *bezt, *(bezt+1));
						ok= 1;
					}
					
					/* if either one of both of the points exceeds crosses over the keyframe time... */
					if ( (bezt->vec[0][0] > bezt->vec[1][0]) && (bezt->vec[2][0] < bezt->vec[1][0]) ) {
						/* swap handles if they have switched sides for some reason */
						SWAP(float, bezt->vec[0][0], bezt->vec[2][0]);
						SWAP(float, bezt->vec[0][1], bezt->vec[2][1]);
					}
					else {
						/* clamp handles */
						if (bezt->vec[0][0] > bezt->vec[1][0]) 
							bezt->vec[0][0]= bezt->vec[1][0];
						if (bezt->vec[2][0] < bezt->vec[1][0]) 
							bezt->vec[2][0]= bezt->vec[1][0];
					}
				}
			}
		}
	}
}

/* This function tests if any BezTriples are out of order, thus requiring a sort */
int test_time_ipocurve (IpoCurve *icu)
{
	int a;
	
	/* currently, only need to test beztriples */
	if (icu->bezt) {
		BezTriple *bezt;
		
		/* loop through all beztriples, stopping when one exceeds the one after it */
		for (a=0, bezt= icu->bezt; a < (icu->totvert - 1); a++, bezt++) {
			if (bezt->vec[1][0] > (bezt+1)->vec[1][0])
				return 1;
		}
	}
	
	/* none need any swapping */
	return 0;
}

/* --------- */

/* The total length of the handles is not allowed to be more
 * than the horizontal distance between (v1-v4).
 * This is to prevent curve loops.
*/
void correct_bezpart (float *v1, float *v2, float *v3, float *v4)
{
	float h1[2], h2[2], len1, len2, len, fac;
	
	/* calculate handle deltas */
	h1[0]= v1[0]-v2[0];
	h1[1]= v1[1]-v2[1];
	h2[0]= v4[0]-v3[0];
	h2[1]= v4[1]-v3[1];
	
	/* calculate distances: 
	 * 	- len	= span of time between keyframes 
	 *	- len1	= length of handle of start key
	 *	- len2 	= length of handle of end key
	 */
	len= v4[0]- v1[0];
	len1= (float)fabs(h1[0]);
	len2= (float)fabs(h2[0]);
	
	/* if the handles have no length, no need to do any corrections */
	if ((len1+len2) == 0.0) 
		return;
		
	/* the two handles cross over each other, so force them
	 * apart using the proportion they overlap 
	 */
	if ((len1+len2) > len) {
		fac= len/(len1+len2);
		
		v2[0]= (v1[0]-fac*h1[0]);
		v2[1]= (v1[1]-fac*h1[1]);
		
		v3[0]= (v4[0]-fac*h2[0]);
		v3[1]= (v4[1]-fac*h2[1]);
	}
}

#if 0 // TODO: enable when we have per-segment interpolation
/* This function sets the interpolation mode for an entire Ipo-Curve. 
 * It is primarily used for patching old files, but is also used in the interface
 * to make sure that all segments of the curve use the same interpolation.
 */
void set_interpolation_ipocurve (IpoCurve *icu, short ipo)
{
	BezTriple *bezt;
	int a;
	
	/* validate arguments */
	if (icu == NULL) return;
	if (ELEM3(ipo, IPO_CONST, IPO_LIN, IPO_BEZ)==0) return;

	/* set interpolation mode for whole curve */
	icu->ipo= ipo;
	
	/* set interpolation mode of all beztriples */
	for (a=0, bezt=icu->bezt; a<icu->totvert; a++, bezt++)
		bezt->ipo= ipo;
}
#endif // TODO: enable when we have per-segment interpolation

/* ***************************** Curve Calculations ********************************* */

/* find root/zero */
int findzero (float x, float q0, float q1, float q2, float q3, float *o)
{
	double c0, c1, c2, c3, a, b, c, p, q, d, t, phi;
	int nr= 0;

	c0= q0 - x;
	c1= 3 * (q1 - q0);
	c2= 3 * (q0 - 2*q1 + q2);
	c3= q3 - q0 + 3 * (q1 - q2);
	
	if (c3 != 0.0) {
		a= c2/c3;
		b= c1/c3;
		c= c0/c3;
		a= a/3;
		
		p= b/3 - a*a;
		q= (2*a*a*a - a*b + c) / 2;
		d= q*q + p*p*p;
		
		if (d > 0.0) {
			t= sqrt(d);
			o[0]= (float)(Sqrt3d(-q+t) + Sqrt3d(-q-t) - a);
			
			if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
			else return 0;
		}
		else if (d == 0.0) {
			t= Sqrt3d(-q);
			o[0]= (float)(2*t - a);
			
			if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
			o[nr]= (float)(-t-a);
			
			if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
			else return nr;
		}
		else {
			phi= acos(-q / sqrt(-(p*p*p)));
			t= sqrt(-p);
			p= cos(phi/3);
			q= sqrt(3 - 3*p*p);
			o[0]= (float)(2*t*p - a);
			
			if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
			o[nr]= (float)(-t * (p + q) - a);
			
			if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) nr++;
			o[nr]= (float)(-t * (p - q) - a);
			
			if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
			else return nr;
		}
	}
	else {
		a=c2;
		b=c1;
		c=c0;
		
		if (a != 0.0) {
			// discriminant
			p= b*b - 4*a*c;
			
			if (p > 0) {
				p= sqrt(p);
				o[0]= (float)((-b-p) / (2 * a));
				
				if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++;
				o[nr]= (float)((-b+p)/(2*a));
				
				if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1;
				else return nr;
			}
			else if (p == 0) {
				o[0]= (float)(-b / (2 * a));
				if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
				else return 0;
			}
		}
		else if (b != 0.0) {
			o[0]= (float)(-c/b);
			
			if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1;
			else return 0;
		}
		else if (c == 0.0) {
			o[0]= 0.0;
			return 1;
		}
		
		return 0;	
	}
}

void berekeny (float f1, float f2, float f3, float f4, float *o, int b)
{
	float t, c0, c1, c2, c3;
	int a;

	c0= f1;
	c1= 3.0f * (f2 - f1);
	c2= 3.0f * (f1 - 2.0f*f2 + f3);
	c3= f4 - f1 + 3.0f * (f2 - f3);
	
	for (a=0; a < b; a++) {
		t= o[a];
		o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3;
	}
}

void berekenx (float *f, float *o, int b)
{
	float t, c0, c1, c2, c3;
	int a;

	c0= f[0];
	c1= 3 * (f[3] - f[0]);
	c2= 3 * (f[0] - 2*f[3] + f[6]);
	c3= f[9] - f[0] + 3 * (f[3] - f[6]);
	
	for (a=0; a < b; a++) {
		t= o[a];
		o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3;
	}
}

/* ***************************** IPO - Calculations ********************************* */

/* ---------------------- Curve Evaluation --------------------------- */

/* helper function for evaluating drivers: 
 *	- we need the local transform = current transform - (parent transform + bone transform)
 *	- (local transform is on action channel level)
 */
static void posechannel_get_local_transform (bPoseChannel *pchan, float loc[], float eul[], float size[])
{
	float parmat[4][4], offs_bone[4][4], imat[4][4];
	float diff_mat[4][4];
	
	/* get first the parent + bone transform in parmat */
	if (pchan->parent) {
		/* bone transform itself */
		Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
		
		/* The bone's root offset (is in the parent's coordinate system) */
		VECCOPY(offs_bone[3], pchan->bone->head);
		
		/* Get the length translation of parent (length along y axis) */
		offs_bone[3][1]+= pchan->parent->bone->length;
		
		Mat4MulSerie(parmat, pchan->parent->pose_mat, offs_bone, NULL, NULL, NULL, NULL, NULL, NULL);
		
		/* invert it */
		Mat4Invert(imat, parmat);
	}
	else {
		Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
		VECCOPY(offs_bone[3], pchan->bone->head);
		
		/* invert it */
		Mat4Invert(imat, offs_bone);
	}
	
	/* difference: current transform - (parent transform + bone transform)  */
	Mat4MulMat4(diff_mat, pchan->pose_mat, imat);

	/* extract relevant components */
	if (loc)
		VECCOPY(loc, diff_mat[3]);
	if (eul)
		Mat4ToEul(diff_mat, eul);
	if (size)
		Mat4ToSize(diff_mat, size);
}

/* evaluate an IPO-driver to get a 'time' value to use instead of "ipotime"
 *	- "ipotime" is the frame at which IPO-curve is being evaluated
 * 	- has to return a float value 
 */
static float eval_driver (IpoDriver *driver, float ipotime)
{
#ifndef DISABLE_PYTHON
	/* currently, drivers are either PyDrivers (evaluating a PyExpression, or Object/Pose-Channel transforms) */
	if (driver->type == IPO_DRIVER_TYPE_PYTHON) {
		/* check for empty or invalid expression */
		if ( (driver->name[0] == '\0') ||
			 (driver->flag & IPO_DRIVER_FLAG_INVALID) )
		{
			return 0.0f;
		}
		
		/* this evaluates the expression using Python,and returns its result:
		 * 	- on errors it reports, then returns 0.0f
		 */
		return BPY_pydriver_eval(driver);
	}
	else
#endif /* DISABLE_PYTHON */
	{

		Object *ob= driver->ob;
		
		/* must have an object to evaluate */
		if (ob == NULL) 
			return 0.0f;
			
		/* if a proxy, use the proxy source*/
		if (ob->proxy_from)
			ob= ob->proxy_from;
		
		/* use given object as driver */
		if (driver->blocktype == ID_OB) {
			/* depsgraph failure: ob ipos are calculated in where_is_object, this might get called too late */
			if ((ob->ipo) && (ob->ctime != ipotime)) {
				/* calculate the value of relevant channel on the Object, but do not write the value
				 * calculated on to the Object but onto "ipotime" instead
				 */
				calc_ipo_spec(ob->ipo, driver->adrcode, &ipotime);
				return ipotime;
			}
			
			/* return the value of the relevant channel */
			switch (driver->adrcode) {
			case OB_LOC_X:
				return ob->loc[0];
			case OB_LOC_Y:
				return ob->loc[1];
			case OB_LOC_Z:
				return ob->loc[2];
			case OB_ROT_X:	/* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
				return (float)( ob->rot[0]/(M_PI_2/9.0) );
			case OB_ROT_Y:	/* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
				return (float)( ob->rot[1]/(M_PI_2/9.0) );
			case OB_ROT_Z:	/* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
				return (float)( ob->rot[2]/(M_PI_2/9.0) );
			case OB_SIZE_X:
				return ob->size[0];
			case OB_SIZE_Y:
				return ob->size[1];
			case OB_SIZE_Z:
				return ob->size[2];
			}
		}
		
		/* use given pose-channel as driver */
		else {	/* ID_AR */
			bPoseChannel *pchan= get_pose_channel(ob->pose, driver->name);
			
			/* must have at least 1 bone to use */
			if (pchan && pchan->bone) {
				/* rotation difference is not a simple driver (i.e. value drives value), but the angle between 2 bones is driving stuff... 
				 *	- the name of the second pchan is also stored in driver->name, but packed after the other one by DRIVER_NAME_OFFS chars
				 */
				if (driver->adrcode == OB_ROT_DIFF) {
					bPoseChannel *pchan2= get_pose_channel(ob->pose, driver->name+DRIVER_NAME_OFFS);
					
					if (pchan2 && pchan2->bone) {
						float q1[4], q2[4], quat[4], angle;
						
						Mat4ToQuat(pchan->pose_mat, q1);
						Mat4ToQuat(pchan2->pose_mat, q2);
						
						QuatInv(q1);
						QuatMul(quat, q1, q2);
						angle = 2.0f * (saacos(quat[0]));
						angle= ABS(angle);
						
						return (angle > M_PI) ? (float)((2.0f * M_PI) - angle) : (float)(angle);
					}
				}
				
				/* standard driver */
				else {
					float loc[3], eul[3], size[3];
					
					/* retrieve local transforms to return 
					 *	- we use eulers here NOT quats, so that Objects can be driven by bones easily
					 *	  also, this way is more understandable for users
					 */
					posechannel_get_local_transform(pchan, loc, eul, size);
					
					switch (driver->adrcode) {
					case OB_LOC_X:
						return loc[0];
					case OB_LOC_Y:
						return loc[1];
					case OB_LOC_Z:
						return loc[2];
					case OB_ROT_X: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
						return (float)( eul[0]/(M_PI_2/9.0) );
					case OB_ROT_Y: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
						return (float)( eul[1]/(M_PI_2/9.0) );
					case OB_ROT_Z: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */
						return (float)( eul[2]/(M_PI_2/9.0) );
					case OB_SIZE_X:
						return size[0];
					case OB_SIZE_Y:
						return size[1];
					case OB_SIZE_Z:
						return size[2];
					}
				}
			}
		}
	}	
	
	/* return 0.0f, as couldn't find relevant data to use */
	return 0.0f;
}

/* evaluate and return the value of the given IPO-curve at the specified frame ("evaltime") */
float eval_icu(IpoCurve *icu, float evaltime) 
{
	float cvalue = 0.0f;
	
	/* if there is a driver, evaluate it to find value to use as "evaltime" 
	 *	- this value will also be returned as the value of the 'curve', if there are no keyframes
	 */
	if (icu->driver) {
		/* ipotime now serves as input for the curve */
		evaltime= cvalue= eval_driver(icu->driver, evaltime);
	}
	
	/* there are keyframes (in the form of BezTriples) which can be interpolated between */
	if (icu->bezt) {
		/* get pointers */
		BezTriple *bezt, *prevbezt, *lastbezt;
		float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac;
		float cycdx, cycdy, ofs, cycyofs= 0.0;
		int a, b;
		
		/* get pointers */
		a= icu->totvert-1;
		prevbezt= icu->bezt;
		bezt= prevbezt+1;
		lastbezt= prevbezt + a;
		
		/* extrapolation mode is 'cyclic' - find relative place within a cycle */
		if (icu->extrap & IPO_CYCL) {
			/* ofs is start frame of cycle */
			ofs= prevbezt->vec[1][0];
			
			/* calculate period and amplitude (total height) of a cycle */
			cycdx= lastbezt->vec[1][0] - prevbezt->vec[1][0];
			cycdy= lastbezt->vec[1][1] - prevbezt->vec[1][1];
			
			/* cycle occurs over some period of time (cycdx should be positive all the time) */
			if (cycdx) {
				/* check if 'cyclic extrapolation', and thus calculate y-offset for this cycle
				 *	- IPO_CYCLX = (IPO_CYCL + IPO_DIR)
				 */
				if (icu->extrap & IPO_DIR) {
					cycyofs = (float)floor((evaltime - ofs) / cycdx);
					cycyofs *= cycdy;
				}
				
				/* calculate where in the cycle we are (overwrite evaltime to reflect this) */
				evaltime= (float)(fmod(evaltime-ofs, cycdx) + ofs);
				if (evaltime < ofs) evaltime += cycdx;
			}
		}
		
		/* evaluation time at or past endpoints? */
		// TODO: for per-bezt interpolation, replace all icu->ipo with (bezt)->ipo
		if (prevbezt->vec[1][0] >= evaltime) {
			/* before or on first keyframe */
			if ((icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) {
				/* linear or bezier interpolation */
				if (icu->ipo==IPO_LIN) {
					/* Use the next center point instead of our own handle for
					 * linear interpolated extrapolate 
					 */
					if (icu->totvert == 1) 
						cvalue= prevbezt->vec[1][1];
					else {
						bezt = prevbezt+1;
						dx= prevbezt->vec[1][0] - evaltime;
						fac= bezt->vec[1][0] - prevbezt->vec[1][0];
						
						/* prevent division by zero */
						if (fac) {
							fac= (bezt->vec[1][1] - prevbezt->vec[1][1]) / fac;
							cvalue= prevbezt->vec[1][1] - (fac * dx);
						}
						else 
							cvalue= prevbezt->vec[1][1];
					}
				} 
				else {
					/* Use the first handle (earlier) of first BezTriple to calculate the
					 * gradient and thus the value of the curve at evaltime
					 */
					dx= prevbezt->vec[1][0] - evaltime;
					fac= prevbezt->vec[1][0] - prevbezt->vec[0][0];
					
					/* prevent division by zero */
					if (fac) {
						fac= (prevbezt->vec[1][1] - prevbezt->vec[0][1]) / fac;
						cvalue= prevbezt->vec[1][1] - (fac * dx);
					}
					else 
						cvalue= prevbezt->vec[1][1];
				}
			}
			else {
				/* constant (IPO_HORIZ) extrapolation or constant interpolation, 
				 * so just extend first keyframe's value 
				 */
				cvalue= prevbezt->vec[1][1];
			}
		}
		else if (lastbezt->vec[1][0] <= evaltime) {
			/* after or on last keyframe */
			if( (icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) {
				/* linear or bezier interpolation */
				if (icu->ipo==IPO_LIN) {
					/* Use the next center point instead of our own handle for
					 * linear interpolated extrapolate 
					 */
					if (icu->totvert == 1) 
						cvalue= lastbezt->vec[1][1];
					else {
						prevbezt = lastbezt - 1;
						dx= evaltime - lastbezt->vec[1][0];
						fac= lastbezt->vec[1][0] - prevbezt->vec[1][0];
						
						/* prevent division by zero */
						if (fac) {
							fac= (lastbezt->vec[1][1] - prevbezt->vec[1][1]) / fac;
							cvalue= lastbezt->vec[1][1] + (fac * dx);
						}
						else 
							cvalue= lastbezt->vec[1][1];
					}
				} 
				else {
					/* Use the gradient of the second handle (later) of last BezTriple to calculate the
					 * gradient and thus the value of the curve at evaltime
					 */
					dx= evaltime - lastbezt->vec[1][0];
					fac= lastbezt->vec[2][0] - lastbezt->vec[1][0];
					
					/* prevent division by zero */
					if (fac) {
						fac= (lastbezt->vec[2][1] - lastbezt->vec[1][1]) / fac;
						cvalue= lastbezt->vec[1][1] + (fac * dx);
					}
					else 
						cvalue= lastbezt->vec[1][1];
				}
			}
			else {
				/* constant (IPO_HORIZ) extrapolation or constant interpolation, 
				 * so just extend last keyframe's value 
				 */
				cvalue= lastbezt->vec[1][1];
			}
		}
		else {
			/* evaltime occurs somewhere in the middle of the curve */
			// TODO: chould be optimised by using a binary search instead???
			for (a=0; prevbezt && bezt && (a < icu->totvert-1); a++, prevbezt=bezt, bezt++) {  
				/* evaltime occurs within the interval defined by these two keyframes */
				if ((prevbezt->vec[1][0] <= evaltime) && (bezt->vec[1][0] >= evaltime)) {
					/* value depends on interpolation mode */
					if (icu->ipo == IPO_CONST) {
						/* constant (evaltime not relevant, so no interpolation needed) */
						cvalue= prevbezt->vec[1][1];
					}
					else if (icu->ipo == IPO_LIN) {
						/* linear - interpolate between values of the two keyframes */
						fac= bezt->vec[1][0] - prevbezt->vec[1][0];
						
						/* prevent division by zero */
						if (fac) {
							fac= (evaltime - prevbezt->vec[1][0]) / fac;
							cvalue= prevbezt->vec[1][1] + (fac * (bezt->vec[1][1] - prevbezt->vec[1][1]));
						}
						else
							cvalue= prevbezt->vec[1][1];
					}
					else {
						/* bezier interpolation */
							/* v1,v2 are the first keyframe and its 2nd handle */
						v1[0]= prevbezt->vec[1][0];
						v1[1]= prevbezt->vec[1][1];
						v2[0]= prevbezt->vec[2][0];
						v2[1]= prevbezt->vec[2][1];
							/* v3,v4 are the last keyframe's 1st handle + the last keyframe */
						v3[0]= bezt->vec[0][0];
						v3[1]= bezt->vec[0][1];
						v4[0]= bezt->vec[1][0];
						v4[1]= bezt->vec[1][1];
						
						/* adjust handles so that they don't overlap (forming a loop) */
						correct_bezpart(v1, v2, v3, v4);
						
						/* try to get a value for this position - if failure, try another set of points */
						b= findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl);
						if (b) {
							berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1);
							cvalue= opl[0];
							break;
						}
					}
				}
			}
		}
		
		/* apply y-offset (for 'cyclic extrapolation') to calculated value */
		cvalue+= cycyofs;
	}
	
	/* clamp evaluated value to lie within allowable value range for this channel */
	if (icu->ymin < icu->ymax) {
		CLAMP(cvalue, icu->ymin, icu->ymax);
	}
	
	/* return evaluated value */
	return cvalue;
}

/* ------------------- IPO-Block/Curve Calculation - General API ----------------------- */

/* calculate the value of the given IPO-curve at the current frame, and set its curval */
void calc_icu (IpoCurve *icu, float ctime)
{
	/* calculate and set curval (evaluates driver too) */
	icu->curval= eval_icu(icu, ctime);
}

/* calculate for the current frame, all IPO-curves in IPO-block that can be evaluated 
 *	- icu->curval is set for all IPO-curves which are evaluated!
 */
void calc_ipo (Ipo *ipo, float ctime)
{
	IpoCurve *icu;
	
	/* if there is no IPO block to evaluate, or whole block is "muted" */
	if (ipo == NULL) return;
	if (ipo->muteipo) return;
	
	/* loop over all curves */
	for (icu= ipo->curve.first; icu; icu= icu->next) {
		/* only evaluated curve if allowed to:
		 * 	- Muted channels should not be evaluated as they shouldn't have any effect 
		 *		--> user explictly turned them off!
		 *	- Drivers should be evaluated at all updates
		 *		--> TODO Note: drivers should be separated from standard channels
		 *	- IPO_LOCK is not set, as it is set by some internal mechanisms to prevent
		 *		IPO-curve from overwriting data (currently only used for IPO-Record). 
		 */
		if ((icu->driver) || (icu->flag & IPO_LOCK)==0) { 
			if ((icu->flag & IPO_MUTE)==0)
				calc_icu(icu, ctime);
		}
	}
}

/* ------------------- IPO-Block/Curve Calculation - Special Hacks ----------------------- */

/* Calculate and return the value of the 'Time' Ipo-Curve from an Object,
 * OR return the current time if not found
 * 	- used in object.c -> bsystem_time() 
 */
float calc_ipo_time (Ipo *ipo, float ctime)
{
	/* only Time IPO from Object IPO-blocks are relevant */
	if ((ipo) && (ipo->blocktype == ID_OB)) {
		IpoCurve *icu= find_ipocurve(ipo, OB_TIME);
		
		/* only calculate (and set icu->curval) for time curve */
		if (icu) {
			calc_icu(icu, ctime);
			return (10.0f * icu->curval);
		}
	}
	
	/* no appropriate time-curve found */
	return ctime;
}

/* Evaluate the specified channel in the given IPO block on the specified frame (ctime),
 * writing the value into that channel's icu->curval, but ALSO dumping it in ctime.
 * 	- Returns success and modifies ctime! 
 */
short calc_ipo_spec (Ipo *ipo, int adrcode, float *ctime)
{
	IpoCurve *icu= find_ipocurve(ipo, adrcode);
	
	/* only evaluate if found */
	if (icu) {
		/* only calculate if allowed to (not locked and not muted) 
		 *	- drivers not taken into account, because this may be called when calculating a driver
		 */
		if ((icu->flag & (IPO_LOCK|IPO_MUTE))==0) 
			calc_icu(icu, *ctime);
		
		/* value resulting from calculations is written into ctime! */
		*ctime= icu->curval;
		return 1;
	}
	
	/* couldn't evaluate */
	return 0;
}

/* ***************************** IPO - DataAPI ********************************* */

/* --------------------- Flush/Execute IPO Values ----------------------------- */

/* Flush IpoCurve->curvals to the data they affect (defined by ID)
 *	 - not for Actions or Constraints!  (those have their own special handling)
 */
void execute_ipo (ID *id, Ipo *ipo)
{
	IpoCurve *icu;
	void *poin;
	int type;
	
	/* don't do anything without an IPO block */
	if (ipo == NULL) 
		return;
	
	/* loop over IPO Curves, getting pointer to var to affect, and write into that pointer */
	for (icu= ipo->curve.first; icu; icu= icu->next) {
		poin= get_ipo_poin(id, icu, &type);
		if (poin) write_ipo_poin(poin, type, icu->curval);
	}
}

/* Flush Action-Channel IPO data to Pose Channel */
void execute_action_ipo (bActionChannel *achan, bPoseChannel *pchan)
{
	/* only do this if there's an Action Channel and Pose Channel to use */
	if (achan && achan->ipo && pchan) {
		IpoCurve *icu;
		
		/* loop over IPO-curves, getting a pointer to pchan var to write to
		 *	- assume for now that only 'float' channels will ever get written into
		 */
		for (icu= achan->ipo->curve.first; icu; icu= icu->next) {
			void *poin= get_pchan_ipo_poin(pchan, icu->adrcode);
			if (poin) write_ipo_poin(poin, IPO_FLOAT, icu->curval);
		}
	}
}


/* --------------------- Force Calculation + Flush IPO Values ----------------------------- */

/* Calculate values for given IPO block, then flush to all of block's users
 *	 - for general usage 
 */
void do_ipo (Ipo *ipo)
{
	if (ipo) {
		float ctime= frame_to_float(G.scene->r.cfra);
		
		/* calculate values, then flush to all users of this IPO block */
		calc_ipo(ipo, ctime);
		do_ipo_nocalc(ipo);
	}
}

/* Calculate values for given Material's IPO block, then flush to given Material only */
void do_mat_ipo (Material *ma)
{
	float ctime;
	
	if (ELEM(NULL, ma, ma->ipo)) 
		return;
	
	ctime= frame_to_float(G.scene->r.cfra);
	/* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */
	
	/* calculate values for current time, then flush values to given material only */
	calc_ipo(ma->ipo, ctime);
	execute_ipo((ID *)ma, ma->ipo);
}

/* Calculate values for given Object's IPO block, then flush to given Object only
 *	- there's also some funky stuff that looks like it's for scene layers
 */
void do_ob_ipo (Object *ob)
{
	float ctime;
	unsigned int lay;
	
	if (ob->ipo == NULL) 
		return;
	
	/* do not set ob->ctime here: for example when parent in invisible layer */
	ctime= bsystem_time(ob, (float) G.scene->r.cfra, 0.0);
	
	/* calculate values of */
	calc_ipo(ob->ipo, ctime);
	
	/* Patch: remember localview */
	lay= ob->lay & 0xFF000000;
	
	/* flush IPO values to this object only */
	execute_ipo((ID *)ob, ob->ipo);
	
	/* hack: for layer animation??? - is this what this is? (Aligorith, 28Sep2008) */
	ob->lay |= lay;
	if ((ob->id.name[2]=='S') && (ob->id.name[3]=='C') && (ob->id.name[4]=='E')) {
		if (strcmp(G.scene->id.name+2, ob->id.name+6)==0) {
			G.scene->lay= ob->lay;
			//XXX copy_view3d_lock(0);
			/* no redraw here! creates too many calls */
		}
	}
}

/* Only execute those IPO-Curves with drivers, on the current frame, for the given Object
 * 	- TODO: Drivers should really be made separate from standard anim channels
 */
void do_ob_ipodrivers (Object *ob, Ipo *ipo, float ctime)
{
	IpoCurve *icu;
	void *poin;
	int type;
	
	for (icu= ipo->curve.first; icu; icu= icu->next) {
		if (icu->driver) {
			icu->curval= eval_icu(icu, ctime);
			
			poin= get_ipo_poin((ID *)ob, icu, &type);
			if (poin) write_ipo_poin(poin, type, icu->curval);
		}
	}
}

/* Special variation to calculate IPO values for Sequence + perform other stuff */
void do_seq_ipo (Sequence *seq, int cfra)
{
	float ctime, div;
	
	/* seq_ipo has an exception: calc both fields immediately */
	if (seq->ipo) {
		if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
			ctime = frame_to_float(cfra);
			div = 1.0;
		} 
		else {
			ctime= frame_to_float(cfra - seq->startdisp);
			div= (seq->enddisp - seq->startdisp) / 100.0f;
			if (div == 0.0) return;
		}
		
		/* 2nd field */
		calc_ipo(seq->ipo, (ctime+0.5f)/div);
		execute_ipo((ID *)seq, seq->ipo);
		seq->facf1= seq->facf0;
		
		/* 1st field */
		calc_ipo(seq->ipo, ctime/div);
		execute_ipo((ID *)seq, seq->ipo);
	}
	else 
		seq->facf1= seq->facf0= 1.0f;
}

/* --------- */


/* exception: it does calc for objects...
 * now find out why this routine was used anyway!
 */
void do_ipo_nocalc (Ipo *ipo)
{
	Object *ob;
	Material *ma;
	Tex *tex;
	World *wo;
	Lamp *la;
	Camera *ca;
	bSound *snd;
	
	if (ipo == NULL) 
		return;
	
	/* only flush IPO values (without calculating first/again) on 
	 * to the datablocks that use the given IPO block 
	 */
	switch (ipo->blocktype) {
	case ID_OB:
		for (ob= G.main->object.first; ob; ob= ob->id.next) {
			if (ob->ipo == ipo) do_ob_ipo(ob);
		}
		break;
	case ID_MA:
		for (ma= G.main->mat.first; ma; ma= ma->id.next) {
			if (ma->ipo == ipo) execute_ipo((ID *)ma, ipo);
		}
		break;
	case ID_TE:
		for (tex= G.main->tex.first; tex; tex= tex->id.next) {
			if (tex->ipo == ipo) execute_ipo((ID *)tex, ipo);
		}
		break;
	case ID_WO:
		for (wo= G.main->world.first; wo; wo= wo->id.next) {
			if (wo->ipo == ipo) execute_ipo((ID *)wo, ipo);
		}
		break;
	case ID_LA:
		for (la= G.main->lamp.first; la; la= la->id.next) {
			if (la->ipo == ipo) execute_ipo((ID *)la, ipo);
		}
		break;
	case ID_CA:
		for (ca= G.main->camera.first; ca; ca= ca->id.next) {
			if (ca->ipo == ipo) execute_ipo((ID *)ca, ipo);
		}
		break;
	case ID_SO:
		for (snd= G.main->sound.first; snd; snd= snd->id.next) {
			if (snd->ipo == ipo) execute_ipo((ID *)snd, ipo);
		}
		break;
	}
}

/* Executes IPO's for whole database on frame change, in a specified order,
 * with datablocks being calculated in alphabetical order
 * 	- called on scene_update_for_newframe() only 
 */
void do_all_data_ipos ()
{
	Material *ma;
	Tex *tex;
	World *wo;
	Ipo *ipo;
	Lamp *la;
	Key *key;
	Camera *ca;
	bSound *snd;
	Sequence *seq;
	Editing *ed;
	Base *base;
	float ctime;

	ctime= frame_to_float(G.scene->r.cfra);
	
	/* this exception cannot be depgraphed yet... what todo with objects in other layers?... */
	for (base= G.scene->base.first; base; base= base->next) {
		Object *ob= base->object;
		
		/* only update layer when an ipo */
		if (has_ipo_code(ob->ipo, OB_LAY)) {
			do_ob_ipo(ob);
			base->lay= ob->lay;
		}
	}
	
	/* layers for the set...*/
	if (G.scene->set) {
		for (base= G.scene->set->base.first; base; base= base->next) {
			Object *ob= base->object;
			
			if (has_ipo_code(ob->ipo, OB_LAY)) {
				do_ob_ipo(ob);
				base->lay= ob->lay;
			}
		}
	}
	
	/* Calculate all IPO blocks in use, execept those for Objects */
	for (ipo= G.main->ipo.first; ipo; ipo= ipo->id.next) {
		if ((ipo->id.us) && (ipo->blocktype != ID_OB)) {
			calc_ipo(ipo, ctime);
		}
	}

	/* Texture Blocks */
	for (tex= G.main->tex.first; tex; tex= tex->id.next) {
		if (tex->ipo) execute_ipo((ID *)tex, tex->ipo);
	}
	
	/* Material Blocks */
	for (ma= G.main->mat.first; ma; ma= ma->id.next) {
		if (ma->ipo) execute_ipo((ID *)ma, ma->ipo);
	}
	
	/* World Blocks */
	for (wo= G.main->world.first; wo; wo= wo->id.next) {
		if (wo->ipo) execute_ipo((ID *)wo, wo->ipo);
	}
	
	/* ShapeKey Blocks */
	for (key= G.main->key.first; key; key= key->id.next) {
		if (key->ipo) execute_ipo((ID *)key, key->ipo);
	}
	
	/* Lamp Blocks */
	for (la= G.main->lamp.first; la; la= la->id.next) {
		if (la->ipo) execute_ipo((ID *)la, la->ipo);
	}
	
	/* Camera Blocks */
	for (ca= G.main->camera.first; ca; ca= ca->id.next) {
		if (ca->ipo) execute_ipo((ID *)ca, ca->ipo);
	}
	
	/* Sound Blocks (Old + Unused) */
	for (snd= G.main->sound.first; snd; snd= snd->id.next) {
		if (snd->ipo) execute_ipo((ID *)snd, snd->ipo);
	}

	/* Sequencer: process FAC Ipos used as volume envelopes */
	ed= G.scene->ed;
	if (ed) {
		for (seq= ed->seqbasep->first; seq; seq= seq->next) {
			if ( ((seq->type == SEQ_RAM_SOUND) || (seq->type == SEQ_HD_SOUND)) &&
				 (seq->startdisp <= G.scene->r.cfra+2) && 
			     (seq->enddisp>G.scene->r.cfra) &&
				 (seq->ipo) ) 
			{
					do_seq_ipo(seq, G.scene->r.cfra);
			}
		}
	}
}


/* --------------------- Assorted ----------------------------- */ 

/* clear delta-transforms on all Objects which use the given IPO block */
void clear_delta_obipo(Ipo *ipo)
{
	Object *ob;
	
	/* only search if there's an IPO */
	if (ipo == NULL) 
		return;
	
	/* search through all objects in database */
	for (ob= G.main->object.first; ob; ob= ob->id.next) {
		/* can only update if not a library */
		if (ob->id.lib == NULL) {
			if (ob->ipo == ipo)  {
				memset(&ob->dloc, 0, 12);
				memset(&ob->drot, 0, 12);
				memset(&ob->dsize, 0, 12);
			}
		}
	}
}

/* ***************************** IPO - DataAPI ********************************* */

// !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!

/* These functions here should be replaced eventually by the Data API, as this is 
 * inflexible duplication...
 */

/* --------------------- Get Pointer API ----------------------------- */ 

/* get pointer to pose-channel's channel, but set appropriate flags first */
void *get_pchan_ipo_poin (bPoseChannel *pchan, int adrcode)
{
	void *poin= NULL;
	
	switch (adrcode) {
		case AC_QUAT_W:
			poin= &(pchan->quat[0]); 
			pchan->flag |= POSE_ROT;
			break;
		case AC_QUAT_X:
			poin= &(pchan->quat[1]); 
			pchan->flag |= POSE_ROT;
			break;
		case AC_QUAT_Y:
			poin= &(pchan->quat[2]); 
			pchan->flag |= POSE_ROT;
			break;
		case AC_QUAT_Z:
			poin= &(pchan->quat[3]); 
			pchan->flag |= POSE_ROT;
			break;
			
		case AC_LOC_X:
			poin= &(pchan->loc[0]); 
			pchan->flag |= POSE_LOC;
			break;
		case AC_LOC_Y:
			poin= &(pchan->loc[1]); 
			pchan->flag |= POSE_LOC;
			break;
		case AC_LOC_Z:
			poin= &(pchan->loc[2]); 
			pchan->flag |= POSE_LOC;
			break;
		
		case AC_SIZE_X:
			poin= &(pchan->size[0]); 
			pchan->flag |= POSE_SIZE;
			break;
		case AC_SIZE_Y:
			poin= &(pchan->size[1]); 
			pchan->flag |= POSE_SIZE;
			break;
		case AC_SIZE_Z:
			poin= &(pchan->size[2]); 
			pchan->flag |= POSE_SIZE;
			break;
	}
	
	/* return pointer */
	return poin;
}

/* get texture channel */
static void *give_tex_poin (Tex *tex, int adrcode, int *type )
{
	void *poin= NULL;

	switch (adrcode) {
	case TE_NSIZE:
		poin= &(tex->noisesize); break;
	case TE_TURB:
		poin= &(tex->turbul); break;
	case TE_NDEPTH:
		poin= &(tex->noisedepth); *type= IPO_SHORT; break;
	case TE_NTYPE:
		poin= &(tex->noisetype); *type= IPO_SHORT; break;
	case TE_VNW1:
		poin= &(tex->vn_w1); break;
	case TE_VNW2:
		poin= &(tex->vn_w2); break;
	case TE_VNW3:
		poin= &(tex->vn_w3); break;
	case TE_VNW4:
		poin= &(tex->vn_w4); break;
	case TE_VNMEXP:
		poin= &(tex->vn_mexp); break;
	case TE_ISCA:
		poin= &(tex->ns_outscale); break;
	case TE_DISTA:
		poin= &(tex->dist_amount); break;
	case TE_VN_COLT:
		poin= &(tex->vn_coltype); *type= IPO_SHORT; break;
	case TE_VN_DISTM:
		poin= &(tex->vn_distm); *type= IPO_SHORT; break;
	case TE_MG_TYP:
		poin= &(tex->stype); *type= IPO_SHORT; break;
	case TE_MGH:
		poin= &(tex->mg_H); break;
	case TE_MG_LAC:
		poin= &(tex->mg_lacunarity); break;
	case TE_MG_OCT:
		poin= &(tex->mg_octaves); break;
	case TE_MG_OFF:
		poin= &(tex->mg_offset); break;
	case TE_MG_GAIN:
		poin= &(tex->mg_gain); break;
	case TE_N_BAS1:
		poin= &(tex->noisebasis); *type= IPO_SHORT; break;
	case TE_N_BAS2:
		poin= &(tex->noisebasis2); *type= IPO_SHORT; break;
	case TE_COL_R:
		poin= &(tex->rfac); break;
	case TE_COL_G:
		poin= &(tex->gfac); break;
	case TE_COL_B:
		poin= &(tex->bfac); break;
	case TE_BRIGHT:
		poin= &(tex->bright); break;
	case TE_CONTRA:
		poin= &(tex->contrast); break;
	}
	
	/* return pointer */
	return poin;
}

/* get texture-slot/mapping channel */
void *give_mtex_poin (MTex *mtex, int adrcode)
{
	void *poin= NULL;
	
	switch (adrcode) {
	case MAP_OFS_X:
		poin= &(mtex->ofs[0]); break;
	case MAP_OFS_Y:
		poin= &(mtex->ofs[1]); break;
	case MAP_OFS_Z:
		poin= &(mtex->ofs[2]); break;
	case MAP_SIZE_X:
		poin= &(mtex->size[0]); break;
	case MAP_SIZE_Y:
		poin= &(mtex->size[1]); break;
	case MAP_SIZE_Z:
		poin= &(mtex->size[2]); break;
	case MAP_R:
		poin= &(mtex->r); break;
	case MAP_G:
		poin= &(mtex->g); break;
	case MAP_B:
		poin= &(mtex->b); break;
	case MAP_DVAR:
		poin= &(mtex->def_var); break;
	case MAP_COLF:
		poin= &(mtex->colfac); break;
	case MAP_NORF:
		poin= &(mtex->norfac); break;
	case MAP_VARF:
		poin= &(mtex->varfac); break;
	case MAP_DISP:
		poin= &(mtex->dispfac); break;
	}
	
	/* return pointer */
	return poin;
}

/* GS reads the memory pointed at in a specific ordering. There are,
 * however two definitions for it. I have jotted them down here, both,
 * but I think the first one is actually used. The thing is that
 * big-endian systems might read this the wrong way round. OTOH, we
 * constructed the IDs that are read out with this macro explicitly as
 * well. I expect we'll sort it out soon... */

/* from blendef: */
#define GS(a)	(*((short *)(a)))

/* from misc_util: flip the bytes from x  */
/*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */


/* general function to get pointer to source/destination data  */
void *get_ipo_poin (ID *id, IpoCurve *icu, int *type)
{
	void *poin= NULL;
	MTex *mtex= NULL;

	/* most channels will have float data, but those with other types will override this */
	*type= IPO_FLOAT;

	/* data is divided into 'blocktypes' based on ID-codes */
	switch (GS(id->name)) {
		case ID_OB: /* object channels -----------------------------  */
		{
			Object *ob= (Object *)id;
			
			switch (icu->adrcode) {
			case OB_LOC_X:
				poin= &(ob->loc[0]); break;
			case OB_LOC_Y:
				poin= &(ob->loc[1]); break;
			case OB_LOC_Z:
				poin= &(ob->loc[2]); break;
			case OB_DLOC_X:
				poin= &(ob->dloc[0]); break;
			case OB_DLOC_Y:
				poin= &(ob->dloc[1]); break;
			case OB_DLOC_Z:
				poin= &(ob->dloc[2]); break;
			
			case OB_ROT_X:
				poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break;
			case OB_ROT_Y:
				poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break;
			case OB_ROT_Z:
				poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break;
			case OB_DROT_X:
				poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break;
			case OB_DROT_Y:
				poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break;
			case OB_DROT_Z:
				poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break;
				
			case OB_SIZE_X:
				poin= &(ob->size[0]); break;
			case OB_SIZE_Y:
				poin= &(ob->size[1]); break;
			case OB_SIZE_Z:
				poin= &(ob->size[2]); break;
			case OB_DSIZE_X:
				poin= &(ob->dsize[0]); break;
			case OB_DSIZE_Y:
				poin= &(ob->dsize[1]); break;
			case OB_DSIZE_Z:
				poin= &(ob->dsize[2]); break;
			
			case OB_LAY:
				poin= &(ob->lay); *type= IPO_INT_BIT; break;
				
			case OB_COL_R:	
				poin= &(ob->col[0]); break;
			case OB_COL_G:
				poin= &(ob->col[1]); break;
			case OB_COL_B:
				poin= &(ob->col[2]); break;
			case OB_COL_A:
				poin= &(ob->col[3]); break;
				
			case OB_PD_FSTR:
				if (ob->pd) poin= &(ob->pd->f_strength);
				break;
			case OB_PD_FFALL:
				if (ob->pd) poin= &(ob->pd->f_power);
				break;
			case OB_PD_SDAMP:
				if (ob->pd) poin= &(ob->pd->pdef_damp);
				break;
			case OB_PD_RDAMP:
				if (ob->pd) poin= &(ob->pd->pdef_rdamp);
				break;
			case OB_PD_PERM:
				if (ob->pd) poin= &(ob->pd->pdef_perm);
				break;
			case OB_PD_FMAXD:
				if (ob->pd) poin= &(ob->pd->maxdist);
				break;
			}
		}
			break;
		case ID_MA: /* material channels -----------------------------  */
		{
			Material *ma= (Material *)id;
			
			switch (icu->adrcode) {
			case MA_COL_R:
				poin= &(ma->r); break;
			case MA_COL_G:
				poin= &(ma->g); break;
			case MA_COL_B:
				poin= &(ma->b); break;
			case MA_SPEC_R:
				poin= &(ma->specr); break;
			case MA_SPEC_G:
				poin= &(ma->specg); break;
			case MA_SPEC_B:
				poin= &(ma->specb); break;
			case MA_MIR_R:
				poin= &(ma->mirr); break;
			case MA_MIR_G:
				poin= &(ma->mirg); break;
			case MA_MIR_B:
				poin= &(ma->mirb); break;
			case MA_REF:
				poin= &(ma->ref); break;
			case MA_ALPHA:
				poin= &(ma->alpha); break;
			case MA_EMIT:
				poin= &(ma->emit); break;
			case MA_AMB:
				poin= &(ma->amb); break;
			case MA_SPEC:
				poin= &(ma->spec); break;
			case MA_HARD:
				poin= &(ma->har); *type= IPO_SHORT; break;
			case MA_SPTR:
				poin= &(ma->spectra); break;
			case MA_IOR:
				poin= &(ma->ang); break;
			case MA_MODE:
				poin= &(ma->mode); *type= IPO_INT_BIT; break; // evil... dumping bitflags directly to user!
			case MA_HASIZE:
				poin= &(ma->hasize); break;
			case MA_TRANSLU:
				poin= &(ma->translucency); break;
			case MA_RAYM:
				poin= &(ma->ray_mirror); break;
			case MA_FRESMIR:
				poin= &(ma->fresnel_mir); break;
			case MA_FRESMIRI:
				poin= &(ma->fresnel_mir_i); break;
			case MA_FRESTRA:
				poin= &(ma->fresnel_tra); break;
			case MA_FRESTRAI:
				poin= &(ma->fresnel_tra_i); break;
			case MA_ADD:
				poin= &(ma->add); break;
			}
			
			if (poin == NULL) {
				if (icu->adrcode & MA_MAP1) mtex= ma->mtex[0];
				else if (icu->adrcode & MA_MAP2) mtex= ma->mtex[1];
				else if (icu->adrcode & MA_MAP3) mtex= ma->mtex[2];
				else if (icu->adrcode & MA_MAP4) mtex= ma->mtex[3];
				else if (icu->adrcode & MA_MAP5) mtex= ma->mtex[4];
				else if (icu->adrcode & MA_MAP6) mtex= ma->mtex[5];
				else if (icu->adrcode & MA_MAP7) mtex= ma->mtex[6];
				else if (icu->adrcode & MA_MAP8) mtex= ma->mtex[7];
				else if (icu->adrcode & MA_MAP9) mtex= ma->mtex[8];
				else if (icu->adrcode & MA_MAP10) mtex= ma->mtex[9];
				else if (icu->adrcode & MA_MAP12) mtex= ma->mtex[11];
				else if (icu->adrcode & MA_MAP11) mtex= ma->mtex[10];
				else if (icu->adrcode & MA_MAP13) mtex= ma->mtex[12];
				else if (icu->adrcode & MA_MAP14) mtex= ma->mtex[13];
				else if (icu->adrcode & MA_MAP15) mtex= ma->mtex[14];
				else if (icu->adrcode & MA_MAP16) mtex= ma->mtex[15];
				else if (icu->adrcode & MA_MAP17) mtex= ma->mtex[16];
				else if (icu->adrcode & MA_MAP18) mtex= ma->mtex[17];
				
				if (mtex)
					poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
			}
		}
			break;
		case ID_TE: /* texture channels -----------------------------  */
		{
			Tex *tex= (Tex *)id;
			
			if (tex) 
				poin= give_tex_poin(tex, icu->adrcode, type);
		}
			break;
		case ID_SEQ: /* sequence channels -----------------------------  */
		{
			Sequence *seq= (Sequence *)id;
			
			switch (icu->adrcode) {
			case SEQ_FAC1:
				poin= &(seq->facf0); break;
			}
		}
			break;
		case ID_CU: /* curve channels -----------------------------  */
		{
			poin= &(icu->curval);
		}
			break;
		case ID_KE: /* shapekey channels -----------------------------  */
		{
			Key *key= (Key *)id;
			KeyBlock *kb;
			
			for(kb= key->block.first; kb; kb= kb->next) {
				if (kb->adrcode == icu->adrcode)
					break;
			}
			
			if (kb)
				poin= &(kb->curval);
		}
			break;
		case ID_WO: /* world channels -----------------------------  */
		{
			World *wo= (World *)id;
			
			switch (icu->adrcode) {
			case WO_HOR_R:
				poin= &(wo->horr); break;
			case WO_HOR_G:
				poin= &(wo->horg); break;
			case WO_HOR_B:
				poin= &(wo->horb); break;
			case WO_ZEN_R:
				poin= &(wo->zenr); break;
			case WO_ZEN_G:
				poin= &(wo->zeng); break;
			case WO_ZEN_B:
				poin= &(wo->zenb); break;
			
			case WO_EXPOS:
				poin= &(wo->exposure); break;
			
			case WO_MISI:
				poin= &(wo->misi); break;
			case WO_MISTDI:
				poin= &(wo->mistdist); break;
			case WO_MISTSTA:
				poin= &(wo->miststa); break;
			case WO_MISTHI:
				poin= &(wo->misthi); break;
			
			case WO_STAR_R:
				poin= &(wo->starr); break;
			case WO_STAR_G:
				poin= &(wo->starg); break;
			case WO_STAR_B:
				poin= &(wo->starb); break;
			
			case WO_STARDIST:
				poin= &(wo->stardist); break;
			case WO_STARSIZE:
				poin= &(wo->starsize); break;
			}
			
			if (poin == NULL) {
				if (icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
				else if (icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
				else if (icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
				else if (icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
				else if (icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
				else if (icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
				else if (icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
				else if (icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
				else if (icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
				else if (icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
				else if (icu->adrcode & MA_MAP11) mtex= wo->mtex[10];
				else if (icu->adrcode & MA_MAP12) mtex= wo->mtex[11];
				else if (icu->adrcode & MA_MAP13) mtex= wo->mtex[12];
				else if (icu->adrcode & MA_MAP14) mtex= wo->mtex[13];
				else if (icu->adrcode & MA_MAP15) mtex= wo->mtex[14];
				else if (icu->adrcode & MA_MAP16) mtex= wo->mtex[15];
				else if (icu->adrcode & MA_MAP17) mtex= wo->mtex[16];
				else if (icu->adrcode & MA_MAP18) mtex= wo->mtex[17];
				
				if (mtex)
					poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
			}
		}
			break;
		case ID_LA: /* lamp channels -----------------------------  */
		{
			Lamp *la= (Lamp *)id;
			
			switch (icu->adrcode) {
			case LA_ENERGY:
				poin= &(la->energy); break;		
			case LA_COL_R:
				poin= &(la->r); break;
			case LA_COL_G:
				poin= &(la->g); break;
			case LA_COL_B:
				poin= &(la->b); break;
			case LA_DIST:
				poin= &(la->dist); break;		
			case LA_SPOTSI:
				poin= &(la->spotsize); break;
			case LA_SPOTBL:
				poin= &(la->spotblend); break;
			case LA_QUAD1:
				poin= &(la->att1); break;
			case LA_QUAD2:
				poin= &(la->att2); break;
			case LA_HALOINT:
				poin= &(la->haint); break;
			}
			
			if (poin == NULL) {
				if (icu->adrcode & MA_MAP1) mtex= la->mtex[0];
				else if (icu->adrcode & MA_MAP2) mtex= la->mtex[1];
				else if (icu->adrcode & MA_MAP3) mtex= la->mtex[2];
				else if (icu->adrcode & MA_MAP4) mtex= la->mtex[3];
				else if (icu->adrcode & MA_MAP5) mtex= la->mtex[4];
				else if (icu->adrcode & MA_MAP6) mtex= la->mtex[5];
				else if (icu->adrcode & MA_MAP7) mtex= la->mtex[6];
				else if (icu->adrcode & MA_MAP8) mtex= la->mtex[7];
				else if (icu->adrcode & MA_MAP9) mtex= la->mtex[8];
				else if (icu->adrcode & MA_MAP10) mtex= la->mtex[9];
				else if (icu->adrcode & MA_MAP11) mtex= la->mtex[10];
				else if (icu->adrcode & MA_MAP12) mtex= la->mtex[11];
				else if (icu->adrcode & MA_MAP13) mtex= la->mtex[12];
				else if (icu->adrcode & MA_MAP14) mtex= la->mtex[13];
				else if (icu->adrcode & MA_MAP15) mtex= la->mtex[14];
				else if (icu->adrcode & MA_MAP16) mtex= la->mtex[15];
				else if (icu->adrcode & MA_MAP17) mtex= la->mtex[16];
				else if (icu->adrcode & MA_MAP18) mtex= la->mtex[17];
				
				if (mtex)
					poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
			}
		}
			break;
		case ID_CA: /* camera channels -----------------------------  */
		{
			Camera *ca= (Camera *)id;
			
			switch (icu->adrcode) {
			case CAM_LENS:
				if (ca->type == CAM_ORTHO)
					poin= &(ca->ortho_scale);
				else
					poin= &(ca->lens); 
				break;
			case CAM_STA:
				poin= &(ca->clipsta); break;
			case CAM_END:
				poin= &(ca->clipend); break;
				
			case CAM_YF_APERT:
				poin= &(ca->YF_aperture); break;
			case CAM_YF_FDIST:
				poin= &(ca->YF_dofdist); break;
				
			case CAM_SHIFT_X:
				poin= &(ca->shiftx); break;
			case CAM_SHIFT_Y:
				poin= &(ca->shifty); break;
			}
		}
			break;
		case ID_SO: /* sound channels -----------------------------  */
		{
			bSound *snd= (bSound *)id;
			
			switch (icu->adrcode) {
			case SND_VOLUME:
				poin= &(snd->volume); break;
			case SND_PITCH:
				poin= &(snd->pitch); break;
			case SND_PANNING:
				poin= &(snd->panning); break;
			case SND_ATTEN:
				poin= &(snd->attenuation); break;
			}
		}
			break;
		case ID_PA: /* particle channels -----------------------------  */
		{
			ParticleSettings *part= (ParticleSettings *)id;
			
			switch (icu->adrcode) {
			case PART_EMIT_FREQ:
			case PART_EMIT_LIFE:
			case PART_EMIT_VEL:
			case PART_EMIT_AVE:
			case PART_EMIT_SIZE:
				poin= NULL; 
				break;
			
			case PART_CLUMP:
				poin= &(part->clumpfac); break;
			case PART_AVE:
				poin= &(part->avefac); break;
			case PART_SIZE:
				poin= &(part->size); break;
			case PART_DRAG:
				poin= &(part->dragfac); break;
			case PART_BROWN:
				poin= &(part->brownfac); break;
			case PART_DAMP:
				poin= &(part->dampfac); break;
			case PART_LENGTH:
				poin= &(part->length); break;
			case PART_GRAV_X:
				poin= &(part->acc[0]); break;
			case PART_GRAV_Y:
				poin= &(part->acc[1]); break;
			case PART_GRAV_Z:
				poin= &(part->acc[2]); break;
			case PART_KINK_AMP:
				poin= &(part->kink_amp); break;
			case PART_KINK_FREQ:
				poin= &(part->kink_freq); break;
			case PART_KINK_SHAPE:
				poin= &(part->kink_shape); break;
			case PART_BB_TILT:
				poin= &(part->bb_tilt); break;
				
			case PART_PD_FSTR:
				if (part->pd) poin= &(part->pd->f_strength);
				break;
			case PART_PD_FFALL:
				if (part->pd) poin= &(part->pd->f_power);
				break;
			case PART_PD_FMAXD:
				if (part->pd) poin= &(part->pd->maxdist);
				break;
			case PART_PD2_FSTR:
				if (part->pd2) poin= &(part->pd2->f_strength);
				break;
			case PART_PD2_FFALL:
				if (part->pd2) poin= &(part->pd2->f_power);
				break;
			case PART_PD2_FMAXD:
				if (part->pd2) poin= &(part->pd2->maxdist);
				break;
			}
		}
			break;
	}

	/* return pointer */
	return poin;
}

/* --------------------- IPO-Curve Limits ----------------------------- */

/* set limits for IPO-curve 
 * Note: must be synced with UI and PyAPI
 */
void set_icu_vars (IpoCurve *icu)
{
	/* defaults. 0.0 for y-extents makes these ignored */
	icu->ymin= icu->ymax= 0.0;
	icu->ipo= IPO_BEZ;
	
	switch (icu->blocktype) {
		case ID_OB: /* object channels -----------------------------  */
		{
			if (icu->adrcode == OB_LAY) {
				icu->ipo= IPO_CONST;
				icu->vartype= IPO_BITS;
			}
		}
			break;
		case ID_MA: /* material channels -----------------------------  */
		{
			if (icu->adrcode < MA_MAP1) {
				switch (icu->adrcode) {
				case MA_HASIZE:
					icu->ymax= 10000.0; break;
				case MA_HARD:
					icu->ymax= 511.0; break;
				case MA_SPEC:
					icu->ymax= 2.0; break;
				case MA_MODE:
					icu->ipo= IPO_CONST;
					icu->vartype= IPO_BITS; break;
				case MA_RAYM:
					icu->ymax= 1.0; break;
				case MA_TRANSLU:
					icu->ymax= 1.0; break;
				case MA_IOR:
					icu->ymin= 1.0;
					icu->ymax= 3.0; break;
				case MA_FRESMIR:
					icu->ymax= 5.0; break;
				case MA_FRESMIRI:
					icu->ymin= 1.0;
					icu->ymax= 5.0; break;
				case MA_FRESTRA:
					icu->ymax= 5.0; break;
				case MA_FRESTRAI:
					icu->ymin= 1.0;
					icu->ymax= 5.0; break;
				case MA_ADD:
					icu->ymax= 1.0; break;
				case MA_EMIT:
					icu->ymax= 2.0; break;
				default:
					icu->ymax= 1.0; break;
				}
			}
			else {
				switch (icu->adrcode & (MA_MAP1-1)) {
				case MAP_OFS_X:
				case MAP_OFS_Y:
				case MAP_OFS_Z:
				case MAP_SIZE_X:
				case MAP_SIZE_Y:
				case MAP_SIZE_Z:
					icu->ymax= 1000.0;
					icu->ymin= -1000.0;
					break;
				case MAP_R:
				case MAP_G:
				case MAP_B:
				case MAP_DVAR:
				case MAP_COLF:
				case MAP_VARF:
				case MAP_DISP:
					icu->ymax= 1.0;
					break;
				case MAP_NORF:
					icu->ymax= 25.0;
					break;
				}
			}
		}
			break;
		case ID_TE: /* texture channels -----------------------------  */
		{
			switch (icu->adrcode & (MA_MAP1-1)) {
				case TE_NSIZE:
					icu->ymin= 0.0001f;
					icu->ymax= 2.0f; 
					break;
				case TE_NDEPTH:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 6.0f; 
					break;
				case TE_NTYPE:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 1.0f; 
					break;
				case TE_TURB:
					icu->ymax= 200.0f; 
					break;
				case TE_VNW1:
				case TE_VNW2:
				case TE_VNW3:
				case TE_VNW4:
					icu->ymax= 2.0f;
					icu->ymin= -2.0f; 
					break;
				case TE_VNMEXP:
					icu->ymax= 10.0f;
					icu->ymin= 0.01f; 
					break;
				case TE_VN_DISTM:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 6.0f; 
					break;
				case TE_VN_COLT:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 3.0f; 
					break;
				case TE_ISCA:
					icu->ymax= 10.0f;
					icu->ymin= 0.01f; 
					break;
				case TE_DISTA:
					icu->ymax= 10.0f; 
					break;
				case TE_MG_TYP:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 6.0f; 
					break;
				case TE_MGH:
					icu->ymin= 0.0001f;
					icu->ymax= 2.0f; 
					break;
				case TE_MG_LAC:
				case TE_MG_OFF:
				case TE_MG_GAIN:
					icu->ymax= 6.0f; break;
				case TE_MG_OCT:
					icu->ymax= 8.0f; break;
				case TE_N_BAS1:
				case TE_N_BAS2:
					icu->vartype= IPO_SHORT;
					icu->ipo= IPO_CONST;
					icu->ymax= 8.0f; 
					break;
				case TE_COL_R:
					icu->ymax= 0.0f; break;
				case TE_COL_G:
					icu->ymax= 2.0f; break;
				case TE_COL_B:
					icu->ymax= 2.0f; break;
				case TE_BRIGHT:
					icu->ymax= 2.0f; break;
				case TE_CONTRA:
					icu->ymax= 5.0f; break;	
			}
		}
			break;
		case ID_SEQ: /* sequence channels -----------------------------  */
		{
			icu->ymax= 1.0f;
		}
			break;
		case ID_CU: /* curve channels -----------------------------  */
		{
			icu->ymax= 1.0f;
		}
			break;
		case ID_WO: /* world channels -----------------------------  */
		{
			if (icu->adrcode < MA_MAP1) {
				switch (icu->adrcode) {
				case WO_EXPOS:
					icu->ymax= 5.0f; break;
				
				case WO_MISTDI:
				case WO_MISTSTA:
				case WO_MISTHI:
				case WO_STARDIST:
				case WO_STARSIZE:
					break;
					
				default:
					icu->ymax= 1.0f;
					break;
				}
			}
			else {
				switch (icu->adrcode & (MA_MAP1-1)) {
				case MAP_OFS_X:
				case MAP_OFS_Y:
				case MAP_OFS_Z:
				case MAP_SIZE_X:
				case MAP_SIZE_Y:
				case MAP_SIZE_Z:
					icu->ymax= 100.0f;
					icu->ymin= -100.0f;
					break;
				case MAP_R:
				case MAP_G:
				case MAP_B:
				case MAP_DVAR:
				case MAP_COLF:
				case MAP_NORF:
				case MAP_VARF:
				case MAP_DISP:
					icu->ymax= 1.0f;
				}
			}
		}
			break;
		case ID_LA: /* lamp channels -----------------------------  */
		{
			if (icu->adrcode < MA_MAP1) {
				switch (icu->adrcode) {
				case LA_ENERGY:
				case LA_DIST:
					break;		
				
				case LA_COL_R:
				case LA_COL_G:
				case LA_COL_B:
				case LA_SPOTBL:
				case LA_QUAD1:
				case LA_QUAD2:
					icu->ymax= 1.0f; break;
					
				case LA_SPOTSI:
					icu->ymax= 180.0f; break;
				
				case LA_HALOINT:
					icu->ymax= 5.0f; break;
				}
			}
			else {
				switch (icu->adrcode & (MA_MAP1-1)) {
				case MAP_OFS_X:
				case MAP_OFS_Y:
				case MAP_OFS_Z:
				case MAP_SIZE_X:
				case MAP_SIZE_Y:
				case MAP_SIZE_Z:
					icu->ymax= 100.0f;
					icu->ymin= -100.0f;
					break;
				case MAP_R:
				case MAP_G:
				case MAP_B:
				case MAP_DVAR:
				case MAP_COLF:
				case MAP_NORF:
				case MAP_VARF:
				case MAP_DISP:
					icu->ymax= 1.0f;
				}
			}
		}	
			break;
		case ID_CA: /* camera channels -----------------------------  */
		{
			switch (icu->adrcode) {
			case CAM_LENS:
				icu->ymin= 1.0f;
				icu->ymax= 1000.0f;
				break;
			case CAM_STA:
				icu->ymin= 0.001f;
				break;
			case CAM_END:
				icu->ymin= 0.1f;
				break;
				
			case CAM_YF_APERT:
				icu->ymin = 0.0f;
				icu->ymax = 2.0f;
				break;
			case CAM_YF_FDIST:
				icu->ymin = 0.0f;
				icu->ymax = 5000.0f;
				break;
				
			case CAM_SHIFT_X:
			case CAM_SHIFT_Y:
				icu->ymin= -2.0f;
				icu->ymax= 2.0f;
				break;
			}
		}
			break;
		case ID_SO: /* sound channels -----------------------------  */
		{
			switch (icu->adrcode) {
			case SND_VOLUME:
				icu->ymin= 0.0f;
				icu->ymax= 1.0f;
				break;
			case SND_PITCH:
				icu->ymin= -12.0f;
				icu->ymin= 12.0f;
				break;
			case SND_PANNING:
				icu->ymin= 0.0f;
				icu->ymax= 1.0f;
				break;
			case SND_ATTEN:
				icu->ymin= 0.0f;
				icu->ymin= 1.0f;
				break;
			}
		}
			break;
		case ID_PA: /* particle channels -----------------------------  */
		{
			switch (icu->adrcode) {
			case PART_EMIT_LIFE:
			case PART_SIZE:
			case PART_KINK_FREQ:
			case PART_EMIT_VEL:
			case PART_EMIT_AVE:
			case PART_EMIT_SIZE:
				icu->ymin= 0.0f;
				break;
			case PART_CLUMP:
				icu->ymin= -1.0f;
				icu->ymax= 1.0f;
				break;
			case PART_DRAG:
			case PART_DAMP:
			case PART_LENGTH:
				icu->ymin= 0.0f;
				icu->ymax= 1.0f;
				break;
			case PART_KINK_SHAPE:
				icu->ymin= -0.999f;
				icu->ymax= 0.999f;
				break;
			}
		}
			break;
		case ID_CO: /* constraint channels -----------------------------  */
		{
			icu->ymin= 0.0f;
			icu->ymax= 1.0f;
		}
			break;
	}
	
	/* by default, slider limits will be icu->ymin and icu->ymax */
	icu->slide_min= icu->ymin;
	icu->slide_max= icu->ymax;
}

/* --------------------- Pointer I/O API ----------------------------- */
 
/* write the given value directly into the given pointer */
void write_ipo_poin (void *poin, int type, float val)
{
	/* Note: we only support a limited number of types, with the value
	 * to set needing to be cast to the appropriate type first
	 * 	-> (float to integer conversions could be slow)
	 */
	switch(type) {
	case IPO_FLOAT:
		*((float *)poin)= val;
		break;
		
	case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */
		*((float *)poin)= (float)(val * M_PI_2 / 9.0);
		break;
		
	case IPO_INT:
	case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil!
	case IPO_LONG:
		*((int *)poin)= (int)val;
		break;
		
	case IPO_SHORT:
	case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil!
		*((short *)poin)= (short)val;
		break;
		
	case IPO_CHAR:
	case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil!
		*((char *)poin)= (char)val;
		break;
	}
}

/* read the value from the pointer that was obtained */
float read_ipo_poin (void *poin, int type)
{
	float val = 0.0;
	
	/* Note: we only support a limited number of types, with the value
	 * to set needing to be cast to the appropriate type first
	 *	-> (int to float conversions may loose accuracy in rare cases)
	 */
	switch (type) {
	case IPO_FLOAT:
		val= *((float *)poin);
		break;
		
	case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */
		val= *( (float *)poin);
		val = (float)(val / (M_PI_2/9.0));
		break;
	
	case IPO_INT:
	case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil!
	case IPO_LONG:
		val= (float)( *((int *)poin) );
		break;
		
	case IPO_SHORT:
	case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil!
		val= *((short *)poin);
		break;
	
	case IPO_CHAR:
	case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil
		val= *((char *)poin);
		break;
	}
	
	/* return value */
	return val;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!


/* ***************************** IPO <--> GameEngine Interface ********************************* */

/* channels is max 32 items, allocated by calling function */
short IPO_GetChannels (Ipo *ipo, IPO_Channel *channels)
{
	IpoCurve *icu;
	int total = 0;
	
	/* don't do anything with no IPO-block */
	if (ipo == NULL) 
		return 0;
	
	/* store the IPO-curve's adrcode in the relevant channel slot */
	for (icu=ipo->curve.first; (icu) && (total < 31); icu=icu->next, total++)
		channels[total]= icu->adrcode;
	
	/* return the number of channels stored */
	return total;
}

/* Get the float value for channel 'channel' at time 'ctime' */
float IPO_GetFloatValue (Ipo *ipo, IPO_Channel channel, float ctime)
{
	/* don't evaluate if no IPO to use */
	if (ipo == NULL) 
		return 0;
	
	/* only calculate the specified channel */
	calc_ipo_spec(ipo, channel, &ctime);
	
	/* unapply rotation hack, as gameengine doesn't use it */
	if ((OB_ROT_X <= channel) && (channel <= OB_DROT_Z))
		ctime *= (float)(M_PI_2/9.0); 

	/* return the value of this channel */
	return ctime;
}