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
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
|
/********************************************************************
* Description: gomath.c
* Library file with various functions for working with matrices
*
* Derived from a work by Fred Proctor,
* changed to work with emc2 and HAL
*
* Adapting Author: Alex Joni
* License: LGPL Version 2
* System: Linux
*
*******************************************************************
Similar to posemath, but using different functions.
TODO:
* find the new functions, add them to posemath, convert the rest
*/
/* for debugging */
extern int printf(const char * fmt, ...);
#include <stddef.h> /* NULL */
#include "rtapi_math.h"
#include <float.h>
#include "sincos.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gotypes.h" /* GoResult ,Real, etc */
#include "gomath.h" /* these decls, GoCartesian, etc. */
go_real go_cbrt(go_real x)
{
if (x < 0.0) {
return (go_real) -pow((double) -x, 1.0/3.0);
}
return (go_real) pow((double) x, 1.0/3.0);
}
/* Translation rep conversion functions */
int go_cart_sph_convert(const go_cart * v, go_sph * s)
{
go_real r;
s->theta = atan2(v->y, v->x);
r = sqrt(go_sq(v->x) + go_sq(v->y) + go_sq(v->z));
s->r = r;
if (GO_TRAN_SMALL(r)) {
s->phi = 0;
} else {
s->phi = acos(v->z / r);
}
return GO_RESULT_OK;
}
int go_cart_cyl_convert(const go_cart * v, go_cyl * c)
{
c->theta = atan2(v->y, v->x);
c->r = sqrt(go_sq(v->x) + go_sq(v->y));
c->z = v->z;
return GO_RESULT_OK;
}
int go_sph_cart_convert(const go_sph * s, go_cart * v)
{
go_real sth, cth, sph, cph;
sincos(s->theta, &sth, &cth);
sincos(s->phi, &sph, &cph);
v->x = s->r * cth * sph;
v->y = s->r * sth * sph;
v->z = s->r * cph;
return GO_RESULT_OK;
}
int go_sph_cyl_convert(const go_sph * s, go_cyl * c)
{
go_real sph, cph;
sincos(s->phi, &sph, &cph);
c->theta = s->theta;
c->r = s->r * sph;
c->z = s->r * cph;
return GO_RESULT_OK;
}
int go_cyl_cart_convert(const go_cyl * c, go_cart * v)
{
v->x = c->r * cos(c->theta);
v->y = c->r * sin(c->theta);
v->z = c->z;
return GO_RESULT_OK;
}
int go_cyl_sph_convert(const go_cyl * c, go_sph * s)
{
s->theta = c->theta;
s->r = sqrt(go_sq(c->r) + go_sq(c->z));
if (GO_TRAN_SMALL(s->r)) {
s->phi = 0.0;
} else {
s->phi = acos(c->z / s->r);
}
return GO_RESULT_OK;
}
/* rotation rep conversion functions */
int go_rvec_quat_convert(const go_rvec * r, go_quat * q)
{
go_cart vec;
go_cart uvec;
go_real mag;
go_real sh;
vec.x = r->x;
vec.y = r->y;
vec.z = r->z;
if (GO_RESULT_OK != go_cart_unit(&vec, &uvec)) {
/* a zero vector */
q->s = 1;
q->x = q->y = q->z = 0;
return GO_RESULT_OK;
}
(void) go_cart_mag(&vec, &mag);
sincos(0.5 * mag, &sh, &(q->s));
if (q->s >= 0) {
q->x = uvec.x * sh;
q->y = uvec.y * sh;
q->z = uvec.z * sh;
} else {
q->s = -q->s;
q->x = -uvec.x * sh;
q->y = -uvec.y * sh;
q->z = -uvec.z * sh;
}
return GO_RESULT_OK;
}
int go_rvec_mat_convert(const go_rvec * r, go_mat * m)
{
go_cart vec;
go_cart uvec;
go_real s, c, omc;
go_real mag;
vec.x = r->x;
vec.y = r->y;
vec.z = r->z;
if (GO_RESULT_OK != go_cart_unit(&vec, &uvec)) {
/* a zero vector */
m->x.x = 1, m->y.x = 0, m->z.x = 0;
m->x.y = 0, m->y.y = 1, m->z.y = 0;
m->x.z = 0, m->y.z = 0, m->z.z = 1;
return GO_RESULT_OK;
}
(void) go_cart_mag(&vec, &mag);
sincos(mag, &s, &c);
omc = 1 - c;
m->x.x = c + go_sq(uvec.x) * omc;
m->y.x = -uvec.z * s + uvec.x * uvec.y * omc;
m->z.x = uvec.y * s + uvec.x * uvec.z * omc;
m->x.y = uvec.z * s + uvec.y * uvec.x * omc;
m->y.y = c + go_sq(uvec.y) * omc;
m->z.y = -uvec.x * s + uvec.y * uvec.z * omc;
m->x.z = -uvec.y * s + uvec.z * uvec.x * omc;
m->y.z = uvec.x * s + uvec.z * uvec.y * omc;
m->z.z = c + go_sq(uvec.z) * omc;
return GO_RESULT_OK;
}
int go_rvec_zyz_convert(const go_rvec * rvec, go_zyz * zyz)
{
go_mat mat;
int retval;
retval = go_rvec_mat_convert(rvec, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyz_convert(&mat, zyz);
}
int go_rvec_zyx_convert(const go_rvec * rvec, go_zyx * zyx)
{
go_mat mat;
int retval;
retval = go_rvec_mat_convert(rvec, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyx_convert(&mat, zyx);
}
int go_rvec_rpy_convert(const go_rvec * r, go_rpy * rpy)
{
go_quat q;
int retval;
retval = go_rvec_quat_convert(r, &q);
if (GO_RESULT_OK != retval) return retval;
return go_quat_rpy_convert(&q, rpy);
}
int go_quat_rvec_convert(const go_quat * q, go_rvec * r)
{
go_real sh;
go_real mag;
sh = sqrt(go_sq(q->x) + go_sq(q->y) + go_sq(q->z));
if (GO_ROT_SMALL(sh)) {
r->x = 0;
r->y = 0;
r->z = 0;
} else {
mag = 2 * atan2(sh, q->s) / sh;
r->x = mag * q->x;
r->y = mag * q->y;
r->z = mag * q->z;
}
return GO_RESULT_OK;
}
int go_quat_mat_convert(const go_quat * q, go_mat * m)
{
/* from space book where e1=q->x e2=q->y e3=q->z e4=q->s */
m->x.x = 1 - 2 * (go_sq(q->y) + go_sq(q->z));
m->y.x = 2 * (q->x * q->y - q->z * q->s);
m->z.x = 2 * (q->z * q->x + q->y * q->s);
m->x.y = 2 * (q->x * q->y + q->z * q->s);
m->y.y = 1 - 2 * (go_sq(q->z) + go_sq(q->x));
m->z.y = 2 * (q->y * q->z - q->x * q->s);
m->x.z = 2 * (q->z * q->x - q->y * q->s);
m->y.z = 2 * (q->y * q->z + q->x * q->s);
m->z.z = 1 - 2 * (go_sq(q->x) + go_sq(q->y));
return GO_RESULT_OK;
}
int go_quat_zyz_convert(const go_quat * q, go_zyz * zyz)
{
go_mat m;
int retval;
retval = go_quat_mat_convert(q, &m);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyz_convert(&m, zyz);
}
int go_quat_zyx_convert(const go_quat * q, go_zyx * zyx)
{
go_mat m;
int retval;
retval = go_quat_mat_convert(q, &m);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyx_convert(&m, zyx);
}
int go_quat_rpy_convert(const go_quat * q, go_rpy * rpy)
{
go_mat m;
int retval;
retval = go_quat_mat_convert(q, &m);
if (GO_RESULT_OK != retval) return retval;
return go_mat_rpy_convert(&m, rpy);
}
int go_mat_rvec_convert(const go_mat * m, go_rvec * r)
{
go_quat q;
int retval;
retval = go_mat_quat_convert(m, &q);
if (GO_RESULT_OK != retval) return retval;
return go_quat_rvec_convert(&q, r);
}
/*
from space book:
e1 = (c32 - c23) / 4*e4
e2 = (c13 - c31) / 4*e4
e3 = (c21 - c12) / 4*e4
e4 = sqrt(1 + c11 + c22 + c33) / 2
if e4 == 0
e1 = sqrt(1 + c11 - c33 - c22) / 2
e2 = sqrt(1 + c22 - c33 - c11) / 2
e3 = sqrt(1 + c33 - c11 - c22) / 2
to determine whether to take the positive or negative sqrt value
since e4 == 0 indicates a 180* rotation then (0 x y z) = (0 -x -y -z).
Thus some generalities can be used:
1) find which of e1, e2, or e3 has the largest magnitude and leave it pos
2) if e1 is largest then
if c21 < 0 then take the negative for e2
if c31 < 0 then take the negative for e3
3) else if e2 is largest then
if c21 < 0 then take the negative for e1
if c32 < 0 then take the negative for e3
4) else if e3 is larget then
if c31 < 0 then take the negative for e1
if c32 < 0 then take the negative for e2
Note: c21 in the space book is m.x.y in this C code
*/
int go_mat_quat_convert(const go_mat * m, go_quat * q)
{
go_real discr;
go_real a;
if (! go_mat_is_norm(m)) {
return GO_RESULT_NORM_ERROR;
}
discr = 1.0 + m->x.x + m->y.y + m->z.z;
if (discr < 0.0) discr = 0.0; /* give sqrt some slack for tiny negs */
q->s = 0.5 * sqrt(discr);
if (GO_ROT_SMALL(q->s)) {
q->s = 0;
discr = 1.0 + m->x.x - m->y.y - m->z.z;
if (discr < 0.0) discr = 0.0;
q->x = sqrt(discr) / 2.0;
discr = 1.0 + m->y.y - m->x.x - m->z.z;
if (discr < 0.0) discr = 0.0;
q->y = sqrt(discr) / 2.0;
discr = 1.0 + m->z.z - m->y.y - m->x.x;
if (discr < 0.0) discr = 0.0;
q->z = sqrt(discr) / 2.0;
if (q->x > q->y && q->x > q->z) {
if (m->x.y < 0.0) {
q->y *= -1;
}
if (m->x.z < 0.0) {
q->z *= -1;
}
} else if (q->y > q->z) {
if (m->x.y < 0.0) {
q->x *= -1;
}
if (m->y.z < 0.0) {
q->z *= -1;
}
} else {
if (m->x.z < 0.0) {
q->x *= -1;
}
if (m->y.z < 0.0) {
q->y *= -1;
}
}
} else {
q->x = (m->y.z - m->z.y) / (a = 4 * q->s);
q->y = (m->z.x - m->x.z) / a;
q->z = (m->x.y - m->y.x) / a;
}
return go_quat_norm(q, q);
}
int go_mat_zyz_convert(const go_mat * m, go_zyz * zyz)
{
zyz->y = atan2(sqrt(go_sq(m->x.z) + go_sq(m->y.z)), m->z.z);
if (GO_ROT_SMALL(zyz->y)) {
zyz->z = 0;
zyz->y = 0; /* force Y to 0 */
zyz->zp = atan2(-m->y.x, m->x.x);
} else if (GO_ROT_CLOSE(zyz->y, GO_PI)) {
zyz->z = 0;
zyz->y = GO_PI; /* force Y to 180 */
zyz->zp = atan2(m->y.x, -m->x.x);
} else {
zyz->z = atan2(m->z.y, m->z.x);
zyz->zp = atan2(m->y.z, -m->x.z);
}
return GO_RESULT_OK;
}
int go_mat_zyx_convert(const go_mat * m, go_zyx * zyx)
{
zyx->y = atan2(-m->x.z, sqrt(go_sq(m->x.x) + go_sq(m->x.y)));
if (GO_ROT_CLOSE(zyx->y, GO_PI_2)) {
zyx->z = 0;
zyx->y = GO_PI_2; /* force it */
zyx->x = atan2(m->y.x, m->y.y);
} else if (GO_ROT_CLOSE(zyx->y, GO_PI_2)) {
zyx->z = 0;
zyx->y = -GO_PI_2; /* force it */
zyx->x = -atan2(m->y.z, m->y.y);
} else {
zyx->z = atan2(m->x.y, m->x.x);
zyx->x = atan2(m->y.z, m->z.z);
}
return GO_RESULT_OK;
}
int go_mat_rpy_convert(const go_mat * m, go_rpy * rpy)
{
rpy->p = atan2(-m->x.z, sqrt(go_sq(m->x.x) + go_sq(m->x.y)));
if (GO_ROT_CLOSE(rpy->p, GO_PI_2)) {
rpy->r = atan2(m->y.x, m->y.y);
rpy->p = GO_PI_2; /* force it */
rpy->y = 0;
} else if (GO_ROT_CLOSE(rpy->p, GO_PI_2)) {
rpy->r = -atan2(m->y.z, m->y.y);
rpy->p = -GO_PI_2; /* force it */
rpy->y = 0;
} else {
rpy->r = atan2(m->y.z, m->z.z);
rpy->y = atan2(m->x.y, m->x.x);
}
return GO_RESULT_OK;
}
int go_zyz_rvec_convert(const go_zyz * zyz, go_rvec * r)
{
go_mat m;
int retval;
retval = go_zyz_mat_convert(zyz, &m);
if (GO_RESULT_OK != retval) return retval;
return go_mat_rvec_convert(&m, r);
}
int go_zyz_quat_convert(const go_zyz * zyz, go_quat * q)
{
go_mat m;
int retval;
retval = go_zyz_mat_convert(zyz, &m);
if (GO_RESULT_OK != retval) return retval;
return go_mat_quat_convert(&m, q);
}
int go_zyz_mat_convert(const go_zyz * zyz, go_mat * m)
{
go_real sa, sb, sg;
go_real ca, cb, cg;
sa = sin(zyz->z);
sb = sin(zyz->y);
sg = sin(zyz->zp);
ca = cos(zyz->z);
cb = cos(zyz->y);
cg = cos(zyz->zp);
m->x.x = ca * cb * cg - sa * sg;
m->y.x = -ca * cb * sg - sa * cg;
m->z.x = ca * sb;
m->x.y = sa * cb * cg + ca * sg;
m->y.y = -sa * cb * sg + ca * cg;
m->z.y = sa * sb;
m->x.z = -sb * cg;
m->y.z = sb * sg;
m->z.z = cb;
return GO_RESULT_OK;
}
int go_zyz_zyx_convert(const go_zyz * zyz, go_zyx * zyx)
{
go_mat mat;
int retval;
retval = go_zyz_mat_convert(zyz, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyx_convert(&mat, zyx);
}
int go_zyz_rpy_convert(const go_zyz * zyz, go_rpy * rpy)
{
go_mat mat;
int retval;
retval = go_zyz_mat_convert(zyz, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_rpy_convert(&mat, rpy);
}
int go_zyx_rvec_convert(const go_zyx * zyx, go_rvec * r)
{
go_mat mat;
int retval;
retval = go_zyx_mat_convert(zyx, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_rvec_convert(&mat, r);
}
int go_zyx_quat_convert(const go_zyx * zyx, go_quat * q)
{
go_mat mat;
int retval;
retval = go_zyx_mat_convert(zyx, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_quat_convert(&mat, q);
}
int go_zyx_mat_convert(const go_zyx * zyx, go_mat * m)
{
go_real sa, sb, sg;
go_real ca, cb, cg;
sa = sin(zyx->z);
sb = sin(zyx->y);
sg = sin(zyx->x);
ca = cos(zyx->z);
cb = cos(zyx->y);
cg = cos(zyx->x);
m->x.x = ca * cb;
m->y.x = ca * sb * sg - sa * cg;
m->z.x = ca * sb * cg + sa * sg;
m->x.y = sa * cb;
m->y.y = sa * sb * sg + ca * cg;
m->z.y = sa * sb * cg - ca * sg;
m->x.z = -sb;
m->y.z = cb * sg;
m->z.z = cb * cg;
return GO_RESULT_OK;
}
int go_zyx_zyz_convert(const go_zyx * zyx, go_zyz * zyz)
{
go_mat mat;
int retval;
retval = go_zyx_mat_convert(zyx, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyz_convert(&mat, zyz);
}
int go_zyx_rpy_convert(const go_zyx * zyx, go_rpy * rpy)
{
go_mat mat;
int retval;
retval = go_zyx_mat_convert(zyx, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_rpy_convert(&mat, rpy);
}
int go_rpy_rvec_convert(const go_rpy * rpy, go_rvec * rvec)
{
go_quat quat;
int retval;
retval = go_rpy_quat_convert(rpy, &quat);
if (GO_RESULT_OK != retval) return retval;
return go_quat_rvec_convert(&quat, rvec);
}
int go_rpy_quat_convert(const go_rpy * rpy, go_quat * quat)
{
go_mat mat;
int retval;
retval = go_rpy_mat_convert(rpy, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_quat_convert(&mat, quat);
}
int go_rpy_mat_convert(const go_rpy * rpy, go_mat * m)
{
go_real sa, sb, sg;
go_real ca, cb, cg;
sa = sin(rpy->y);
sb = sin(rpy->p);
sg = sin(rpy->r);
ca = cos(rpy->y);
cb = cos(rpy->p);
cg = cos(rpy->r);
m->x.x = ca * cb;
m->y.x = ca * sb * sg - sa * cg;
m->z.x = ca * sb * cg + sa * sg;
m->x.y = sa * cb;
m->y.y = sa * sb * sg + ca * cg;
m->z.y = sa * sb * cg - ca * sg;
m->x.z = -sb;
m->y.z = cb * sg;
m->z.z = cb * cg;
return GO_RESULT_OK;
}
int go_rpy_zyz_convert(const go_rpy * rpy, go_zyz * zyz)
{
go_mat mat;
int retval;
retval = go_rpy_mat_convert(rpy, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyz_convert(&mat, zyz);
}
int go_rpy_zyx_convert(const go_rpy * rpy, go_zyx * zyx)
{
go_mat mat;
int retval;
retval = go_rpy_mat_convert(rpy, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_zyx_convert(&mat, zyx);
}
go_pose go_pose_this(go_real x, go_real y, go_real z,
go_real rs, go_real rx, go_real ry, go_real rz)
{
go_pose pose;
pose.tran.x = x, pose.tran.y = y, pose.tran.z = z;
pose.rot.s = rs, pose.rot.x = rx, pose.rot.y = ry, pose.rot.z = rz;
return pose;
}
go_cart go_cart_zero(void)
{
go_cart cart;
cart.x = 0, cart.y = 0, cart.z = 0;
return cart;
}
go_quat go_quat_identity(void)
{
go_quat quat;
quat.s = 1, quat.x = 0, quat.y = 0, quat.z = 0;
return quat;
}
go_pose go_pose_identity(void)
{
go_pose pose;
pose.tran.x = 0, pose.tran.y = 0, pose.tran.z = 0;
pose.rot.s = 1, pose.rot.x = 0, pose.rot.y = 0, pose.rot.z = 0;
return pose;
}
int go_pose_hom_convert(const go_pose * p, go_hom * h)
{
h->tran = p->tran;
return go_quat_mat_convert(&p->rot, &h->rot);
}
int go_hom_pose_convert(const go_hom * h, go_pose * p)
{
p->tran = h->tran;
return go_mat_quat_convert(&h->rot, &p->rot);
}
int go_cart_rvec_convert(const go_cart * cart, go_rvec * rvec)
{
rvec->x = cart->x;
rvec->y = cart->y;
rvec->z = cart->z;
return GO_RESULT_OK;
}
int go_rvec_cart_convert(const go_rvec * rvec, go_cart * cart)
{
cart->x = rvec->x;
cart->y = rvec->y;
cart->z = rvec->z;
return GO_RESULT_OK;
}
/* go_cart functions */
go_flag go_cart_cart_compare(const go_cart * v1, const go_cart * v2)
{
if (GO_TRAN_CLOSE(v1->x, v2->x) &&
GO_TRAN_CLOSE(v1->y, v2->y) &&
GO_TRAN_CLOSE(v1->z, v2->z)) {
return 1;
}
return 0;
}
int go_cart_cart_dot(const go_cart * v1, const go_cart * v2,
go_real * d)
{
*d = v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
return GO_RESULT_OK;
}
int go_cart_cart_cross(const go_cart * v1, const go_cart * v2,
go_cart * vout)
{
go_cart cp1, cp2;
cp1 = *v1;
cp2 = *v2;
vout->x = cp1.y * cp2.z - cp1.z * cp2.y;
vout->y = cp1.z * cp2.x - cp1.x * cp2.z;
vout->z = cp1.x * cp2.y - cp1.y * cp2.x;
return GO_RESULT_OK;
}
int go_cart_mag(const go_cart * v, go_real * d)
{
*d = sqrt(go_sq(v->x) + go_sq(v->y) + go_sq(v->z));
return GO_RESULT_OK;
}
int go_cart_magsq(const go_cart * v, go_real * d)
{
*d = go_sq(v->x) + go_sq(v->y) + go_sq(v->z);
return GO_RESULT_OK;
}
go_flag go_cart_cart_par(const go_cart *v1, const go_cart *v2)
{
go_real dot;
go_real magsq1, magsq2;
/*
parallel if v1 dot v2 = mag v1 * mag v2, or more efficiently,
sq(v1 dot v2) = magsq v1 * magsq v2
*/
go_cart_cart_dot(v1, v2, &dot);
go_cart_magsq(v1, &magsq1);
go_cart_magsq(v2, &magsq2);
return GO_TRAN_CLOSE(dot*dot, magsq1*magsq2);
}
go_flag go_cart_cart_perp(const go_cart *v1, const go_cart *v2)
{
go_real dot;
/* perpendicular if v1 dot v2 is 0 */
go_cart_cart_dot(v1, v2, &dot);
return GO_TRAN_SMALL(dot);
}
int go_cart_cart_disp(const go_cart * v1, const go_cart * v2,
go_real * d)
{
*d = sqrt(go_sq(v2->x - v1->x) +
go_sq(v2->y - v1->y) +
go_sq(v2->z - v1->z));
return GO_RESULT_OK;
}
int go_cart_cart_add(const go_cart * v1, const go_cart * v2,
go_cart * vout)
{
vout->x = v1->x + v2->x;
vout->y = v1->y + v2->y;
vout->z = v1->z + v2->z;
return GO_RESULT_OK;
}
int go_cart_cart_sub(const go_cart * v1, const go_cart * v2,
go_cart * vout)
{
vout->x = v1->x - v2->x;
vout->y = v1->y - v2->y;
vout->z = v1->z - v2->z;
return GO_RESULT_OK;
}
int go_cart_scale_mult(const go_cart * v1, go_real d, go_cart * vout)
{
vout->x = v1->x * d;
vout->y = v1->y * d;
vout->z = v1->z * d;
return GO_RESULT_OK;
}
int go_cart_neg(const go_cart * v1, go_cart * vout)
{
vout->x = -v1->x;
vout->y = -v1->y;
vout->z = -v1->z;
return GO_RESULT_OK;
}
int go_cart_unit(const go_cart * v, go_cart * vout)
{
go_real size = sqrt(go_sq(v->x) + go_sq(v->y) + go_sq(v->z));
if (GO_TRAN_SMALL(size)) {
vout->x = GO_INF;
vout->y = GO_INF;
vout->z = GO_INF;
return GO_RESULT_NORM_ERROR;
}
size = 1.0 / size;
vout->x = v->x * size;
vout->y = v->y * size;
vout->z = v->z * size;
return GO_RESULT_OK;
}
int go_cart_is_norm(const go_cart * v)
{
return GO_TRAN_CLOSE(sqrt(go_sq(v->x) + go_sq(v->y) + go_sq(v->z)), 1);
}
int go_cart_cart_rot(const go_cart * v1,
const go_cart * v2,
go_quat * quat)
{
int retval;
go_cart u1;
go_cart u2;
go_cart cross;
go_real mag;
go_real dot;
go_real th;
go_rvec rvec;
/* unitize the input vectors */
retval = go_cart_unit(v1, &u1);
if (GO_RESULT_OK != retval) return retval;
retval = go_cart_unit(v2, &u2);
if (GO_RESULT_OK != retval) return retval;
/* and cross the unit vectors to get the mutual normal */
(void) go_cart_cart_cross(&u1, &u2, &cross);
/* the magnitude of the mutual normal is sin theta */
(void) go_cart_mag(&cross, &mag);
th = asin(mag);
/* dot them to get parallel (1) or antiparallel (-1) */
(void) go_cart_cart_dot(&u1, &u2, &dot);
/* make the cross a unit vector so we can theta-ize it */
retval = go_cart_unit(&cross, &cross);
/* handle aligned vectors */
if (GO_RESULT_OK != retval) {
/* v1 and v2 are aligned, so th will be zero */
if (dot > 0.0) { /* would be -1 or 1 */
/* parallel */
quat->s = 1.0;
quat->x = quat->y = quat->z = 0.0;
return GO_RESULT_OK;
}
/* else antiparallel - set u2 to be normal to u1,
rotate about it PI rads */
retval = go_cart_normal(&u1, &u2);
if (GO_RESULT_OK != retval) return retval;
rvec.x = GO_PI * u2.x;
rvec.y = GO_PI * u2.y;
rvec.z = GO_PI * u2.z;
return go_rvec_quat_convert(&rvec, quat);
}
/* else not aligned */
if (dot < 0.0) {
th = GO_PI - th;
}
rvec.x = th * cross.x;
rvec.y = th * cross.y;
rvec.z = th * cross.z;
return go_rvec_quat_convert(&rvec, quat);
}
int go_cart_cart_proj(const go_cart * v1, const go_cart * v2,
go_cart * vout)
{
go_cart v2u;
go_real d;
int retval;
retval = go_cart_unit(v2, &v2u);
if (GO_RESULT_OK != retval) return retval;
retval = go_cart_cart_dot(v1, &v2u, &d);
if (GO_RESULT_OK != retval) return retval;
return go_cart_scale_mult(&v2u, d, vout);
}
int go_cart_plane_proj(const go_cart * v, const go_cart * normal,
go_cart * vout)
{
go_cart par;
int retval;
retval = go_cart_cart_proj(v, normal, &par);
if (GO_RESULT_OK != retval) return retval;
return go_cart_cart_sub(v, &par, vout);
}
int go_cart_cart_angle(const go_cart * v1, const go_cart * v2,
go_real * a)
{
go_real dot, m1, m2;
go_cart_cart_dot(v1, v2, &dot);
go_cart_mag(v1, &m1);
go_cart_mag(v2, &m2);
if (m1 <= 0. || m2 <= 0.) {
return GO_RESULT_DIV_ERROR;
}
dot = dot / (m1 * m2);
if (dot > 1.)
dot = 1.;
else if (dot < -1.)
dot = -1.;
*a = acos(dot);
return GO_RESULT_OK;
}
int go_cart_normal(const go_cart * v, go_cart * vout)
{
go_cart cart;
go_real min, ymin;
/* pick the X, Y or Z vector that is most perpendicular to 'v' */
cart.x = 1, cart.y = 0, cart.z = 0; /* start with X */
min = fabs(v->x);
ymin = fabs(v->y);
if (ymin < min) {
min = ymin; /* Y is more perp */
cart.x = 0, cart.y = 1, cart.z = 0;
}
if (fabs(v->z) < min) {
cart.x = 0, cart.y = 0, cart.z = 1;
}
/* cross the most perp axis vector with 'v' to get a real perp */
go_cart_cart_cross(v, &cart, &cart);
/* unitize it into 'vout' and return */
return go_cart_unit(&cart, vout);
}
int go_cart_centroid(const go_cart * varray,
go_integer num,
go_cart * centroid)
{
go_integer i;
if (num < 1) return GO_RESULT_BAD_ARGS;
centroid->x = varray[0].x;
centroid->y = varray[0].y;
centroid->z = varray[0].z;
for (i = 1; i < num; i++) {
centroid->x += varray[i].x;
centroid->y += varray[i].y;
centroid->z += varray[i].z;
}
return go_cart_scale_mult(centroid, 1.0/((go_real) num), centroid);
}
int go_cart_centroidize(const go_cart * vinarray,
go_integer num,
go_cart * centroid,
go_cart * voutarray)
{
go_integer i;
int retval;
retval = go_cart_centroid(vinarray, num, centroid);
if (GO_RESULT_OK != retval) return retval;
for (i = 0; i < num; i++) {
(void) go_cart_cart_sub(&vinarray[i], centroid, &voutarray[i]);
}
return GO_RESULT_OK;
}
/*
Another Solution of the Cubic Equation Here is another method of
solving a cubic polynomial equation submitted independently by Paul
A. Torres and Robert A. Warren. It is based on the idea of "completing
the cube," by arranging matters so that three of the four terms are
three of the four terms of a perfect cube.
Start with the cubic equation
x3 + ax2 + bx + c = 0.
If a2 - 3b = 0, then the first three terms are the first three terms
of a perfect cube, namely (x+a/3)3. Then you can "complete the cube"
by subtracting c from both sides and adding the missing term of the
cube a3/27 to both sides. Recalling that b = a2/3, you get
x3 + ax2 + bx + c = 0,
x3 + ax2 + (a2/3)x + c = 0,
x3 + ax2 + (a2/3)x = -c,
x3 + ax2 + (a2/3)x + a3/27 = a3/27 - c,
(x+a/3)3 = a3/27 - c.
By taking the cube root of the left side and the three cube roots of
the right side, you get
x = -a/3 + (a3/27 - c)1/3,
x = -a/3 + (a3/27 - c)1/3(-1+sqrt[-3])/2,
x = -a/3 + (a3/27 - c)1/3(-1-sqrt[-3])/2.
These are the roots of the cubic equation that were sought.
If a2 - 3b is nonzero, then proceed as follows. Set x = y + z, where y
is an indeterminate and z is a function of a, b, and c, which will be
found below. Then
(y+z)3 + a(y+z)2 + b(y+z) + c = 0,
y3 + (3z+a)y2 + (3z2+2az+b)y + (z3+az2+bz+c) = 0,
y3 + dy2 + ey + f = 0,
where
d = 3z + a,
e = 3z2 + 2az + b,
f = z3 + az2 + bz + c.
The first three terms of this equation in y will be those of a perfect
cube if and only if d2 - 3e is zero, which happens if and only if a2 -
3b = 0, which cannot happen in this case, so we seemingly haven't
gained anything. However, the last three terms of this equation in y
will be those of a perfect cube if and only if e2 = 3df, that is if
and only if
(3z2+2az+b)2 = 3(3z+a)(z3+az2+bz+c),
(a2-3b)z2 + (ab-9c)z + (b2-3ac) = 0,
gz2 + hz + i = 0,
where
g = a2 - 3b,
h = ab - 9c,
i = b2 - 3ac.
Since a2 - 3b is nonzero, g is nonzero, and we have a true quadratic
equation, called the resolvent quadratic. Now we pick z to be a root
of this quadratic equation. If the GCD of z2 + (h/g)z + (i/g) and z3 +
az2 + bz + c as polynomials in z is not 1, then any root of the GCD is
also a root of the original cubic equation in x. Once you have at
least one root, the problem of finding the other roots is reduced to
solving a quadratic or linear equation. If the GCD is 1, then neither
value of z can make f = 0, so we can assume henceforth that f is
nonzero. Either root z of the quadratic will do, but we must choose
one of them. We arbitrarily pick the one with a plus sign in front of
the radical,
z = (-h+sqrt[h2-4gi])/(2g),
= (9c-ab+sqrt[81c2-54abc+12a3c+12b3-3a2b2])/(2[a2-3b]).
Set z equal to this value in the equation for y, and divide it by f on
both sides. Then the last three terms of the cubic in y are those of a
perfect cube, namely (ey/[3f]+1)3, so we can complete the cube to
solve it. We do this by subtracting y3/f from both sides, then adding
the missing term of the cubic, (ey/[3f])3 to both sides, obtaining
(ey/[3f]+1)3 = ([e/(3f)]3-1/f)y3,
ey/(3f) + 1 = y([e/(3f)]3-1/f)1/3,
ey/(3f) + 1 = y([e/(3f)]3-1/f)1/3(-1+sqrt[-3])/2,
ey/(3f) + 1 = y([e/(3f)]3-1/f)1/3(-1-sqrt[-3])/2,
y = 3f/[-e+(e3-27f2)1/3],
y = 3f/[-e+(e3-27f2)1/3(-1+sqrt[-3])/2],
y = 3f/[-e+(e3-27f2)1/3(-1-sqrt[-3])/2].
Now you have the values of y. Add z to each to get the values of x:
x = z + 3f/[(e3-27f2)1/3-e],
x = z + 3f/[(e3-27f2)1/3(-1+sqrt[-3])/2-e],
x = z + 3f/[(e3-27f2)1/3(-1-sqrt[-3])/2-e].
These are the roots of the cubic equation that were sought.
Example: Solve x3 + 6x2 + 9x + 6 = 0.
a = 6, b = 9, c = 6.
Then
d = 3z + 6, e = 3z2 + 12z + 9, f = z3 + 6z2 + 9z + 6.
Then
g = a2 - 3b = 36 - 27 = 9,
h = ab - 9c = 54 - 54 = 0,
i = b2 - 3ac = 81 - 108 = -27,
and the resolvent quadratic is
9z2 - 27 = 0,
z2 - 3 = 0,
z = sqrt(3).
Then
d = 6 + 3 sqrt(3),
e = 18 + 12 sqrt(3),
f = 24 + 12 sqrt(3),
and the cubic in y is
y3 + (6+3 sqrt[3])y2 + (18+12 sqrt[3])y + (24+12 sqrt[3]) = 0.
Then one root is
y = 3f/[(e3-27f2)1/3-e],
= 3(24+12 sqrt[3])/([18+12 sqrt(3)]3-27[24+12 sqrt(3)]2)1/3-18-12 sqrt[3]),
= (12+6 sqrt[3])/([9+6 sqrt(3)]1/3-3-2 sqrt[3]),
= (6+4 sqrt[3])/([2+sqrt(3)]1/3-2-sqrt[3]).
After a lot of simplification, you get
y = -2 - sqrt(3) - (2-sqrt[3])1/3 - (2+sqrt[3])1/3,
x = -2 - (2-sqrt[3])1/3 - (2+sqrt[3])1/3
(and two other roots).
*/
go_complex go_complex_add(go_complex z1, go_complex z2)
{
go_complex sum;
sum.re = z1.re + z2.re;
sum.im = z1.im + z2.im;
return sum;
}
go_complex go_complex_sub(go_complex z1, go_complex z2)
{
go_complex diff;
diff.re = z1.re - z2.re;
diff.im = z1.im - z2.im;
return diff;
}
go_complex go_complex_mult(go_complex z1, go_complex z2)
{
go_complex prod;
prod.re = (z1.re * z2.re) - (z1.im * z2.im);
prod.im = (z1.re * z2.im) + (z1.im * z2.re);
return prod;
}
go_complex go_complex_div(go_complex z1, go_complex z2, int * result)
{
go_complex z2c = {0, 0};
go_real denom;
denom = go_sq(z2.re) + go_sq(z2.im);
if (denom < GO_REAL_EPSILON) {
*result = GO_RESULT_DIV_ERROR;
return z2c;
}
z2c.re = z2.re, z2c.im = -z2.im; /* complex conjugate */
*result = GO_RESULT_OK;
return go_complex_scale(go_complex_mult(z1, z2c), 1.0 / denom);
}
go_complex go_complex_scale(go_complex z, go_real scale)
{
go_complex s;
s.re = scale * z.re;
s.im = scale * z.im;
return s;
}
go_real go_complex_mag(go_complex z)
{
return sqrt(go_sq(z.re) + go_sq(z.im));
}
go_real go_complex_arg(go_complex z)
{
return atan2(z.im, z.re);
}
/*
go_complex_sqrt() and go_complex_cbrt() are structured so that if
the roots of complex conjugates are taken, the first roots z1 will
be complex conjugates. This is due to atan2 returning args in the
range -PI to PI.
*/
void go_complex_sqrt(go_complex z, go_complex * z1, go_complex * z2)
{
go_real r, th;
r = sqrt(go_complex_mag(z));
th = 0.5 * go_complex_arg(z);
z1->re = r * cos(th);
z1->im = r * sin(th);
if (NULL != z2) {
z2->re = -z1->re;
z2->im = -z1->im;
}
return;
}
void go_complex_cbrt(go_complex z, go_complex * z1, go_complex * z2, go_complex * z3)
{
go_real r, th;
r = go_cbrt(go_complex_mag(z));
th = 1.0/3.0 * go_complex_arg(z);
z1->re = r * cos(th);
z1->im = r * sin(th);
if (NULL != z2) {
z2->re = r * cos(th + 2.0/3.0 * GO_PI);
z2->im = r * sin(th + 2.0/3.0 * GO_PI);
}
if (NULL != z3) {
z3->re = r * cos(th + 4.0/3.0 * GO_PI);
z3->im = r * sin(th + 4.0/3.0 * GO_PI);
}
return;
}
int go_quadratic_solve(const go_quadratic * quad,
go_complex * z1,
go_complex * z2)
{
go_real discr;
/* check if b is 0 and we can reduce order */
if (GO_SMALL(quad->b)) {
z2->re = z2->im = 0.0;
z1->re = -quad->a;
z1->im = 0.0;
return GO_RESULT_OK;
}
discr = go_sq(quad->a) - 4.0 * quad->b;
if (discr < 0.0) {
discr = sqrt(-discr);
z1->re = z2->re = -0.5 * quad->a;
z1->im = 0.5 * discr;
z2->im = -0.5 * discr;
} else {
discr = sqrt(discr);
z1->re = 0.5 * (-quad->a + discr);
z2->re = 0.5 * (-quad->a - discr);
z1->im = z2->im = 0.0;
}
return GO_RESULT_OK;
}
/*
References on solving the cubic:
"Solving Cubic and Quartic Polynomials,"
www.karlscalculus.org/pdf/cubicQuartic.pdf
"Cubic Equations in One Formula,"
http://mathforum.org/library/drmath/view/52668.html
*/
int go_cubic_solve(const go_cubic * cub,
go_complex * z1,
go_complex * z2,
go_complex * z3)
{
go_quadratic quad;
go_real a, b;
go_complex z, A, B, I = {0.0, 1.0};
go_complex u1, u2, u3;
/* check if c is 0, and we have a root of 0 and can reduce order
to a quadratic */
if (GO_SMALL(cub->c)) {
z3->re = z3->im = 0.0;
quad.a = cub->a;
quad.b = cub->b;
return go_quadratic_solve(&quad, z1, z2);
}
a = cub->b - 1.0/3.0 * go_sq(cub->a);
b = cub->c - 1.0/3.0 * cub->a * cub->b + 2.0/27.0 * go_cub(cub->a);
z.re = 0.25 * go_sq(b) + 1.0/27.0 * go_cub(a), z.im = 0.0;
go_complex_sqrt(z, &A, &B);
/* since z is real, the pair of sqrt(z) is either pure real or
pure complex */
A.re -= 0.5 * b;
B.re -= 0.5 * b;
/* subtracting b/2 keeps them pure real and in general unequal, or
makes them complex conjugates */
/* Now we need to take the cube root of each. If they're pure real,
we can take the scalar cube root. */
if (GO_SMALL(A.im)) {
A.re = go_cbrt(A.re), A.im = 0.0;
B.re = go_cbrt(B.re), B.im = 0.0;
} else {
go_complex_cbrt(A, &A, NULL, NULL);
B.re = A.re, B.im = -A.im; /* B and A must be complex conjugates */
}
u1 = go_complex_add(A, B);
u2 = u3 = go_complex_scale(u1, -0.5);
z = go_complex_scale(go_complex_sub(A, B), sqrt(0.75));
z = go_complex_mult(I, z);
u2 = go_complex_add(u2, z);
u3 = go_complex_sub(u3, z);
z1->re = u1.re - 1.0/3.0 * cub->a, z1->im = u1.im;
z2->re = u2.re - 1.0/3.0 * cub->a, z2->im = u2.im;
z3->re = u3.re - 1.0/3.0 * cub->a, z3->im = u3.im;
return GO_RESULT_OK;
}
/*
http://mathforum.org/dr.math/faq/faq.cubic.equations.html
Solving the quartic with no third-order term:
The quartic in y must factor into two quadratics with real
coefficients, since any complex roots must occur in conjugate
pairs. Write
y4 + e y2 + f y + g = (y2 + h y + j) (y2 - h y + g/j)
Since f and g aren't zero, neither j nor h is zero either. Without
loss of generality, we can assume h > 0 (otherwise swap the two
factors). Then, equating coefficients of y2 and y,
e = g/j + j - h2, f = h (g/j - j).
so
g/j + j = e + h2, g/j - j = f/h.
Adding and subtracting these two equations,
2 g/j = e + h2 + f/h, 2 j = e + h2 - f/h.
Multiplying these together,
4 g = e2 + 2 e h2 + h4 - f2/h2.
Rearranging,
h6 + 2 e h4 + (e2-4 g) h2 - f2 = 0.
This is a cubic equation in h2, with known coefficients, since we know
e, f, and g. We can use the solution for cubic equations shown above
to find a positive real value of h2, whose existence is guaranteed by
Descartes' Rule of Signs, and then take its positive square root. This
value of h will give a value of j from 2 j = e + h2 - f/h. Once you
know h and j, you know the quadratic factors of the quartic in
y. Notice that you do not need all the roots h2 of the cubic, and that
any positive real one will do. Further notice that h2 = 4 z from the
previous section.
Once you have factored the quartic into two quadratics, finishing the
finding of the roots is simple, using the Quadratic Formula. Once the
roots y are found, the corresponding x's are gotten from x = y - a/4.
*/
int go_quartic_solve(const go_quartic * quart,
go_complex * z1,
go_complex * z2,
go_complex * z3,
go_complex * z4)
{
go_cubic cub;
go_quadratic quad;
go_real a2, a3, a4;
go_real e, f, g;
go_complex p, q, r, fc, a4c;
int retval;
/*
There are several ways to solve quartic equations. They all start by
dividing the equation by the leading coefficient, to make it of the
form
x4 + a x3 + b x2 + c x + d = 0.
There is an easy special case if d = 0, in which case you can factor
the quartic into x times a cubic. The roots then are 0 and the roots
of that cubic. (To solve the cubic equation, see above.)
*/
if (GO_SMALL(quart->d)) {
/* set one root to be 0 and solve for reduced cubic */
z4->re = z4->im = 0.0;
cub.a = quart->a;
cub.b = quart->b;
cub.c = quart->c;
return go_cubic_solve(&cub, z1, z2, z3);
}
/*
First eliminate the third-order term. Substitute x = y - a/4,
expand, and simplify, to get
y4 + e y2 + f y + g = 0
where
e = b - 3 a2/8,
f = c + a3/8 - a b/2,
g = d - 3 a4/256 + a2 b/16 - a c/4.
*/
a2 = go_sq(quart->a);
e = quart->b - 3.0/8.0 * a2;
a3 = a2 * quart->a;
f = quart->c + 1.0/8.0 * a3 - 0.5 * (quart->a * quart->b);
a4 = a2 * a2;
g = quart->d - 3.0/256.0 * a4 + 1.0/16.0 * a2 * quart->b - 0.25 * (quart->a * quart->c);
/*
At this point, there are two useful special cases.
1. If g = 0, then, again, you can factor the quartic into y times
a cubic. The roots of the original equation are then x = -a/4
and the roots of that cubic with a/4 subtracted from each.
2. If f = 0, then the quartic in y is actually a quadratic
equation in the variable y2. Solve this using your favorite
method, and then take the two square roots of each of the
solutions for y2 to find the four values of y which
work. Subtract a/4 from each to get the four roots x.
Henceforth we can assume that d, f, and g are all nonzero.
*/
/* Case 1 */
if (GO_SMALL(g)) {
a4 = -0.25 * quart->a; /* reuse a4 as -a/4, save a var */
z4->re = a4;
z4->im = 0.0;
cub.a = 0.0;
cub.b = e;
cub.c = f;
retval = go_cubic_solve(&cub, z1, z2, z3);
if (GO_RESULT_OK != retval) return retval;
z1->re += a4;
z2->re += a4;
z3->re += a4;
return GO_RESULT_OK;
}
/* Case 2 */
if (GO_SMALL(f)) {
a4 = -0.25 * quart->a; /* reuse a4 as -a/4, save a var */
quad.a = e;
quad.b = g;
retval = go_quadratic_solve(&quad, z1, z3);
if (GO_RESULT_OK != retval) return retval;
go_complex_sqrt(*z1, z1, z2);
go_complex_sqrt(*z3, z3, z4);
z1->re += a4;
z2->re += a4;
z3->re += a4;
z4->re += a4;
return GO_RESULT_OK;
}
/*
Here is one way to proceed from this point. The related auxiliary
cubic equation
z3 + (e/2) z2 + ((e2-4 g)/16) z - f2/64 = 0
has three roots. Find those roots by solving this cubic (again,
see above). None of these is zero because f isn't zero. Let p and
q be the square roots of two of those roots (any choices of roots
and signs will work), and set
r = -f/(8 p q).
Then p2, q2, and r2 are the three roots of the above cubic. More
important is the fact that the four roots of the original quartic
are
x = p + q + r - a/4,
x = p - q - r - a/4,
x = -p + q - r - a/4, and
x = -p - q + r - a/4.
This solution was discovered by Leonhard Euler (1707-1783).
*/
cub.a = 0.5 * e;
cub.b = 1.0/16.0 * (go_sq(e) - 4.0 * g);
cub.c = -1.0/64.0 * go_sq(f);
retval = go_cubic_solve(&cub, z1, z2, z3);
if (GO_RESULT_OK != retval) return retval;
go_complex_sqrt(*z1, &p, NULL);
go_complex_sqrt(*z2, &q, NULL);
fc.re = f, fc.im = 0.0; /* fc is complex of scalar f */
r = go_complex_div(fc, go_complex_scale(go_complex_mult(p, q), -8.0), &retval);
if (GO_RESULT_OK != retval) return retval;
a4c.re = 0.25 * quart->a, a4c.im = 0.0; /* a/4 as a complex */
*z1 = go_complex_sub(go_complex_add(go_complex_add(p, q), r), a4c);
*z2 = go_complex_sub(go_complex_sub(go_complex_sub(p, q), r), a4c);
*z3 = go_complex_sub(go_complex_sub(go_complex_sub(q, p), r), a4c);
*z4 = go_complex_sub(go_complex_sub(go_complex_sub(r, p), q), a4c);
return GO_RESULT_OK;
}
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
#define MAG2(a,b) sqrt((a)*(a)+(b)*(b))
/*!
This computes the Householder reduction to tridiagonal form of a
real symmetric matrix \a a. \a a will contain the orthogonal matrix
that effects the transformation. \a d will contain the diagonal
elements, and \a e will contain the off-diagonal elements. This is
used by go_tridiag_ql to find the eigenvalues and eigenvectors of a
real symmetric matrix.
*/
int go_tridiag_reduce(go_real ** a, /*< the real symmetric matrix,
overwritten with the diagonalizing
matrix */
go_integer n, /*< how many rows and columns */
go_real * d, /*< the diagonal elements will be
stored here */
go_real * e) /*< the off-diagonal elements
will be stored here, starting
at index 1 */
{
go_integer l,k,j,i;
go_real scale,hh,h,g,f;
for (i=n-1;i>0;i--) {
l=i-1;
h=scale=0.0;
if (l > 0) {
for (k=0;k<=l;k++)
scale += fabs(a[i][k]);
if (scale == 0.0)
e[i]=a[i][l];
else {
for (k=0;k<=l;k++) {
a[i][k] /= scale;
h += a[i][k]*a[i][k];
}
f=a[i][l];
g=(f >= 0.0 ? -sqrt(h) : sqrt(h));
e[i]=scale*g;
h -= f*g;
a[i][l]=f-g;
f=0.0;
for (j=0;j<=l;j++) {
a[j][i]=a[i][j]/h;
g=0.0;
for (k=0;k<=j;k++)
g += a[j][k]*a[i][k];
for (k=j+1;k<=l;k++)
g += a[k][j]*a[i][k];
e[j]=g/h;
f += e[j]*a[i][j];
}
hh=f/(h+h);
for (j=0;j<=l;j++) {
f=a[i][j];
e[j]=g=e[j]-hh*f;
for (k=0;k<=j;k++)
a[j][k] -= (f*e[k]+g*a[i][k]);
}
}
} else
e[i]=a[i][l];
d[i]=h;
}
d[0]=0.0;
e[0]=0.0;
for (i=0;i<n;i++) {
l=i-1;
if (d[i]) {
for (j=0;j<=l;j++) {
g=0.0;
for (k=0;k<=l;k++)
g += a[i][k]*a[k][j];
for (k=0;k<=l;k++)
a[k][j] -= g*a[k][i];
}
}
d[i]=a[i][i];
a[i][i]=1.0;
for (j=0;j<=l;j++) a[j][i]=a[i][j]=0.0;
}
return GO_RESULT_OK;
}
/*!
This computes the eigenvalues and eigenvectors of the
tridiagonalized matrix with diagonal \a d and off-diagonal \a e,
using QL reduction with implicit shifts. \a d, \a e and \a z should
have been generated by a prior call to go_tridiag_reduce. \a d will
contain the eigenvalues, and \a z will contain the eigenvectors.
Calling example:
go_tridiag_reduce(matrix, 3, d, e);
go_tridiag_ql(d, e, 3, matrix);
\a d will contain the eigenvalues of the original \a matrix, and
the new \a matrix will contain the eigenvectors.
*/
int go_tridiag_ql(go_real * d,
go_real * e,
go_integer n,
go_real ** z)
{
go_integer m,l,iter,i,k;
go_real s,r,p,g,f,dd,c,b;
for (i=1;i<n;i++) e[i-1]=e[i];
e[n-1]=0.0;
for (l=0;l<n;l++) {
iter=0;
do {
for (m=l;m<n-1;m++) {
dd=fabs(d[m])+fabs(d[m+1]);
if ((go_real)(fabs(e[m])+dd) == dd) break;
}
if (m != l) {
if (iter++ == 30) return GO_RESULT_ERROR;
g=(d[l+1]-d[l])/(2.0*e[l]);
r=MAG2(g,1.0);
g=d[m]-d[l]+e[l]/(g+SIGN(r,g));
s=c=1.0;
p=0.0;
for (i=m-1;i>=l;i--) {
f=s*e[i];
b=c*e[i];
e[i+1]=(r=MAG2(f,g));
if (r == 0.0) {
d[i+1] -= p;
e[m]=0.0;
break;
}
s=f/r;
c=g/r;
g=d[i+1]-p;
r=(d[i]-g)*s+2.0*c*b;
d[i+1]=g+(p=s*r);
g=c*r-b;
for (k=0;k<n;k++) {
f=z[k][i+1];
z[k][i+1]=s*z[k][i]+c*f;
z[k][i]=c*z[k][i]-s*f;
}
}
if (r == 0.0 && i >= l) continue;
d[l] -= p;
e[l]=g;
e[m]=0.0;
}
} while (m != l);
}
return GO_RESULT_OK;
}
int go_cart_cart_pose(const go_cart * v1, const go_cart * v2,
go_cart * v1c, go_cart * v2c,
go_integer num, go_pose * p)
{
go_integer t;
go_cart c1, c2;
go_real Sxx, Sxy, Sxz;
go_real Syx, Syy, Syz;
go_real Szx, Szy, Szz;
GO_MATRIX_DECLARE(N, Nspace, 4, 4);
go_real d[4], e[4];
go_real eigenval;
int retval;
Sxx = Sxy = Sxz = 0.0;
Syx = Syy = Syz = 0.0;
Szx = Szy = Szz = 0.0;
go_matrix_init(N, Nspace, 4, 4);
retval = go_cart_centroidize(v1, num, &c1, v1c);
if (GO_RESULT_OK != retval) return retval;
retval = go_cart_centroidize(v2, num, &c2, v2c);
if (GO_RESULT_OK != retval) return retval;
for (t = 0; t < num; t++) {
Sxx += v1c[t].x * v2c[t].x;
Sxy += v1c[t].x * v2c[t].y;
Sxz += v1c[t].x * v2c[t].z;
Syx += v1c[t].y * v2c[t].x;
Syy += v1c[t].y * v2c[t].y;
Syz += v1c[t].y * v2c[t].z;
Szx += v1c[t].z * v2c[t].x;
Szy += v1c[t].z * v2c[t].y;
Szz += v1c[t].z * v2c[t].z;
}
N.el[0][0] = Sxx + Syy + Szz;
N.el[0][1] = N.el[1][0] = Syz - Szy;
N.el[0][2] = N.el[2][0] = Szx - Sxz;
N.el[0][3] = N.el[3][0] = Sxy - Syx;
N.el[1][1] = Sxx - Syy - Szz;
N.el[1][2] = N.el[2][1] = Sxy + Syx;
N.el[1][3] = N.el[3][1] = Szx + Sxz;
N.el[2][2] = -Sxx + Syy - Szz;
N.el[2][3] = N.el[3][2] = Syz + Szy;
N.el[3][3] = -Sxx -Syy + Szz;
/* compute eigenvectors */
retval = go_tridiag_reduce(N.el, 4, d, e);
if (GO_RESULT_OK != retval) return retval;
retval = go_tridiag_ql(d, e, 4, N.el);
if (GO_RESULT_OK != retval) return retval;
/* pick eigenvector associated with most positive eigenvalue */
eigenval = d[0], t = 0;
if (d[1] > eigenval) eigenval = d[1], t = 1;
if (d[2] > eigenval) eigenval = d[2], t = 2;
if (d[3] > eigenval) eigenval = d[3], t = 3;
/* now t is the column with the eigenvector we want as our quaternion */
p->rot.s = N.el[0][t];
p->rot.x = N.el[1][t];
p->rot.y = N.el[2][t];
p->rot.z = N.el[3][t];
retval = go_quat_norm(&p->rot, &p->rot);
if (GO_RESULT_OK != retval) return retval;
/* rotate left centroid, subtract right from left to get translation */
(void) go_quat_cart_mult(&p->rot, &c1, &c1);
(void) go_cart_cart_sub(&c2, &c1, &p->tran);
return GO_RESULT_OK;
}
static int trilaterate(go_real x2, go_real x3, go_real y3,
go_real l1, go_real l2, go_real l3,
go_cart * p)
{
go_real discr;
p->x = 0.5 * (x2 - (go_sq(l2) - go_sq(l1))/x2);
p->y =
(go_sq(l1) - 2.0 * p->x * x3 + go_sq(x3) +
go_sq(y3) - go_sq(l3)) / (2.0 * y3);
discr = go_sq(l1) - go_sq(p->x) - go_sq(p->y);
if (discr < -GO_REAL_EPSILON) {
return GO_RESULT_DOMAIN_ERROR;
}
if (discr < 0.0) discr = 0.0; /* make slightly negative numbers 0 */
/* return z positive, caller can make negative if they want the
point below the base */
p->z = sqrt(discr);
return GO_RESULT_OK;
}
int go_cart_trilaterate(const go_cart * c1,
const go_cart * c2,
const go_cart * c3,
go_real l1,
go_real l2,
go_real l3,
go_cart * out1,
go_cart * out2)
{
go_pose P_to_B, B_to_P;
go_mat mat;
go_cart diff, proj;
go_cart p1, p2, p3, out1_in_P, out2_in_P;
int retval;
/* transform the points from their original base frame {B} into an
easier frame {P}, with c1 at the {P}, origin, c2 along the {P} x
axis, and all three in the {P} xy plane */
/* first get the transform */
P_to_B.tran = *c1;
go_cart_cart_sub(c2, c1, &diff);
retval = go_cart_unit(&diff, &mat.x);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_sub(c3, c1, &diff);
retval = go_cart_cart_proj(&diff, &mat.x, &proj);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_sub(&diff, &proj, &diff);
retval = go_cart_unit(&diff, &mat.y);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_cross(&mat.x, &mat.y, &mat.z);
retval = go_mat_quat_convert(&mat, &P_to_B.rot);
if (GO_RESULT_OK != retval) return retval;
retval = go_pose_inv(&P_to_B, &B_to_P);
if (GO_RESULT_OK != retval) return retval;
/* now transform them */
go_pose_cart_mult(&B_to_P, c1, &p1);
go_pose_cart_mult(&B_to_P, c2, &p2);
go_pose_cart_mult(&B_to_P, c3, &p3);
/* trilaterate to get the point in {P} */
retval = trilaterate(p2.x, p3.x, p3.y, l1, l2, l3, &out1_in_P);
if (GO_RESULT_OK != retval) return retval;
out2_in_P.x = out1_in_P.x;
out2_in_P.y = out1_in_P.y;
out2_in_P.z = -out1_in_P.z; /* the other one has a minus z */
go_pose_cart_mult(&P_to_B, &out1_in_P, out1);
go_pose_cart_mult(&P_to_B, &out2_in_P, out2);
return GO_RESULT_OK;
}
/* go_rvec functions */
go_flag go_rvec_rvec_compare(const go_rvec * r1, const go_rvec * r2)
{
if (GO_ROT_CLOSE(r1->x, r2->x) &&
GO_ROT_CLOSE(r1->y, r2->y) &&
GO_ROT_CLOSE(r1->z, r2->z)) {
return 1;
}
return 0;
}
int go_rvec_scale_mult(const go_rvec * r, go_real s, go_rvec * rout)
{
rout->x = r->x * s;
rout->y = r->y * s;
rout->z = r->z * s;
return GO_RESULT_OK;
}
/* go_mat functions */
int go_mat_norm(const go_mat * mat, go_mat * mout)
{
go_cart yprojx;
int retval;
/* unitize the X vector, which we must be able to do */
retval = go_cart_unit(&mat->x, &mout->x);
if (GO_RESULT_OK != retval) return retval;
/* project Y onto X, subtract from Y to get Y perp to X, and unitize */
retval = go_cart_cart_proj(&mat->y, &mout->x, &yprojx);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_sub(&mat->y, &yprojx, &mout->y);
retval = go_cart_unit(&mout->y, &mout->y);
if (GO_RESULT_OK != retval) return retval;
/* cross the new X and Y to get Z */
return go_cart_cart_cross(&mout->x, &mout->y, &mout->z);
}
go_flag go_mat_is_norm(const go_mat * m)
{
go_cart u;
go_cart_cart_cross(&m->x, &m->y, &u);
#define COL_IS_UNIT(r) GO_TRAN_CLOSE(go_sq((r).x) + go_sq((r).y) + go_sq((r).z), 1)
return (COL_IS_UNIT(m->x) &&
COL_IS_UNIT(m->y) &&
COL_IS_UNIT(m->z) &&
go_cart_cart_compare(&u, &m->z));
#undef COL_IS_UNIT
}
int go_mat_inv(const go_mat * m, go_mat * mout)
{
go_real cp;
/* inverse of a rotation matrix is the transpose */
mout->x.x = m->x.x;
mout->y.y = m->y.y;
mout->z.z = m->z.z;
cp = m->x.y;
mout->x.y = m->y.x;
mout->y.x = cp;
cp = m->x.z;
mout->x.z = m->z.x;
mout->z.x = cp;
cp = m->y.z;
mout->y.z = m->z.y;
mout->z.y = cp;
return GO_RESULT_OK;
}
int go_mat_cart_mult(const go_mat * m, const go_cart * v,
go_cart * vout)
{
go_cart cp;
cp = *v;
vout->x = m->x.x * cp.x + m->y.x * cp.y + m->z.x * cp.z;
vout->y = m->x.y * cp.x + m->y.y * cp.y + m->z.y * cp.z;
vout->z = m->x.z * cp.x + m->y.z * cp.y + m->z.z * cp.z;
return GO_RESULT_OK;
}
int go_mat_mat_mult(const go_mat * m1, const go_mat * m2, go_mat * mout)
{
go_mat cp1, cp2;
cp1 = *m1;
cp2 = *m2;
mout->x.x = cp1.x.x * cp2.x.x + cp1.y.x * cp2.x.y + cp1.z.x * cp2.x.z;
mout->x.y = cp1.x.y * cp2.x.x + cp1.y.y * cp2.x.y + cp1.z.y * cp2.x.z;
mout->x.z = cp1.x.z * cp2.x.x + cp1.y.z * cp2.x.y + cp1.z.z * cp2.x.z;
mout->y.x = cp1.x.x * cp2.y.x + cp1.y.x * cp2.y.y + cp1.z.x * cp2.y.z;
mout->y.y = cp1.x.y * cp2.y.x + cp1.y.y * cp2.y.y + cp1.z.y * cp2.y.z;
mout->y.z = cp1.x.z * cp2.y.x + cp1.y.z * cp2.y.y + cp1.z.z * cp2.y.z;
mout->z.x = cp1.x.x * cp2.z.x + cp1.y.x * cp2.z.y + cp1.z.x * cp2.z.z;
mout->z.y = cp1.x.y * cp2.z.x + cp1.y.y * cp2.z.y + cp1.z.y * cp2.z.z;
mout->z.z = cp1.x.z * cp2.z.x + cp1.y.z * cp2.z.y + cp1.z.z * cp2.z.z;
return GO_RESULT_OK;
}
/* go_quat functions */
go_flag go_quat_quat_compare(const go_quat * q1, const go_quat * q2)
{
if (GO_ROT_CLOSE(q1->s, q2->s) &&
GO_TRAN_CLOSE(q1->x, q2->x) &&
GO_TRAN_CLOSE(q1->y, q2->y) &&
GO_TRAN_CLOSE(q1->z, q2->z)) {
return 1;
}
/* note (0, x, y, z) = (0, -x, -y, -z) */
if (! GO_ROT_SMALL(q1->s) ||
GO_TRAN_CLOSE(q1->x, -q2->x) ||
GO_TRAN_CLOSE(q1->y, -q2->y) ||
GO_TRAN_CLOSE(q1->z, -q2->z)) {
return 0;
}
return 1;
}
int go_quat_mag(const go_quat * quat, go_real * d)
{
go_real sh;
sh = sqrt(go_sq(quat->x) + go_sq(quat->y) + go_sq(quat->z));
*d = 2 * atan2(sh, quat->s);
return GO_RESULT_OK;
}
int go_quat_unit(const go_quat * q1, go_quat * qout)
{
go_real d;
(void) go_quat_mag(q1, &d);
if (GO_SMALL(d)) return GO_RESULT_ERROR;
d = 1.0 / d;
return go_quat_scale_mult(q1, d, qout);
}
int go_quat_norm(const go_quat * q1, go_quat * qout)
{
go_real size;
size = sqrt(go_sq(q1->s) + go_sq(q1->x) + go_sq(q1->y) + go_sq(q1->z));
if (GO_ROT_SMALL(size)) {
qout->s = 1;
qout->x = 0;
qout->y = 0;
qout->z = 0;
return GO_RESULT_NORM_ERROR;
}
size = 1.0 / size;
if (q1->s >= 0) {
qout->s = q1->s * size;
qout->x = q1->x * size;
qout->y = q1->y * size;
qout->z = q1->z * size;
return GO_RESULT_OK;
} else {
qout->s = -q1->s * size;
qout->x = -q1->x * size;
qout->y = -q1->y * size;
qout->z = -q1->z * size;
return GO_RESULT_OK;
}
}
int go_quat_inv(const go_quat * q1, go_quat * qout)
{
qout->s = q1->s;
qout->x = -q1->x;
qout->y = -q1->y;
qout->z = -q1->z;
if (!go_quat_is_norm(q1)) {
return GO_RESULT_NORM_ERROR;
}
return GO_RESULT_OK;
}
go_flag go_quat_is_norm(const go_quat * q1)
{
return GO_TRAN_CLOSE(go_sq(q1->s) +
go_sq(q1->x) +
go_sq(q1->y) +
go_sq(q1->z), 1);
}
int go_quat_scale_mult(const go_quat * q, go_real s, go_quat * qout)
{
go_real sh; /* sine of half angle */
go_real ha; /* half angle */
go_real scale; /* new sh / old sh */
sh = sqrt(go_sq(q->x) + go_sq(q->y) + go_sq(q->z));
if (GO_SMALL(sh)) {
/* zero rotation-- leave it alone */
*qout = *q;
return GO_RESULT_OK;
}
ha = atan2(sh, q->s);
ha *= s;
scale = sin(ha) / sh;
qout->s = cos(ha);
qout->x = scale * q->x;
qout->y = scale * q->y;
qout->z = scale * q->z;
return GO_RESULT_OK;
}
int go_quat_quat_mult(const go_quat * q1, const go_quat * q2,
go_quat * qout)
{
go_quat cp1, cp2;
if (!go_quat_is_norm(q1) || !go_quat_is_norm(q2)) {
return GO_RESULT_NORM_ERROR;
}
cp1 = *q1;
cp2 = *q2;
qout->s = cp1.s * cp2.s - cp1.x * cp2.x - cp1.y * cp2.y - cp1.z * cp2.z;
if (qout->s >= 0) {
qout->x = cp1.s * cp2.x + cp1.x * cp2.s + cp1.y * cp2.z - cp1.z * cp2.y;
qout->y = cp1.s * cp2.y - cp1.x * cp2.z + cp1.y * cp2.s + cp1.z * cp2.x;
qout->z = cp1.s * cp2.z + cp1.x * cp2.y - cp1.y * cp2.x + cp1.z * cp2.s;
} else {
qout->s = -qout->s;
qout->x = -cp1.s * cp2.x - cp1.x * cp2.s - cp1.y * cp2.z + cp1.z * cp2.y;
qout->y = -cp1.s * cp2.y + cp1.x * cp2.z - cp1.y * cp2.s - cp1.z * cp2.x;
qout->z = -cp1.s * cp2.z - cp1.x * cp2.y + cp1.y * cp2.x - cp1.z * cp2.s;
}
return GO_RESULT_OK;
}
int go_quat_cart_mult(const go_quat * q1, const go_cart * v2,
go_cart * vout)
{
go_cart c;
if (!go_quat_is_norm(q1)) {
return GO_RESULT_NORM_ERROR;
}
c.x = q1->y * v2->z - q1->z * v2->y;
c.y = q1->z * v2->x - q1->x * v2->z;
c.z = q1->x * v2->y - q1->y * v2->x;
vout->x = v2->x + 2 * (q1->s * c.x + q1->y * c.z - q1->z * c.y);
vout->y = v2->y + 2 * (q1->s * c.y + q1->z * c.x - q1->x * c.z);
vout->z = v2->z + 2 * (q1->s * c.z + q1->x * c.y - q1->y * c.x);
return GO_RESULT_OK;
}
/* go_pose functions*/
go_flag go_pose_pose_compare(const go_pose * p1, const go_pose * p2)
{
return (go_quat_quat_compare(&p1->rot, &p2->rot) &&
go_cart_cart_compare(&p1->tran, &p2->tran));
}
int go_pose_inv(const go_pose * p1, go_pose * p2)
{
int retval;
retval = go_quat_inv(&p1->rot, &p2->rot);
if (GO_RESULT_OK != retval) return retval;
retval = go_quat_cart_mult(&p2->rot, &p1->tran, &p2->tran);
if (GO_RESULT_OK != retval) return retval;
p2->tran.x = -p2->tran.x;
p2->tran.y = -p2->tran.y;
p2->tran.z = -p2->tran.z;
return GO_RESULT_OK;
}
int go_pose_cart_mult(const go_pose * p1, const go_cart * v2, go_cart * vout)
{
int retval;
/* first rotate the vector */
retval = go_quat_cart_mult(&p1->rot, v2, vout);
if (GO_RESULT_OK != retval) return retval;
/* then translate it */
return go_cart_cart_add(&p1->tran, vout, vout);
}
int go_pose_pose_mult(const go_pose * p1, const go_pose * p2, go_pose * pout)
{
go_pose out;
int retval;
retval = go_quat_cart_mult(&p1->rot, &p2->tran, &out.tran);
if (GO_RESULT_OK != retval) return retval;
retval = go_cart_cart_add(&p1->tran, &out.tran, &out.tran);
if (GO_RESULT_OK != retval) return retval;
retval = go_quat_quat_mult(&p1->rot, &p2->rot, &out.rot);
*pout = out;
return retval;
}
int go_pose_scale_mult(const go_pose * p1, go_real s, go_pose * pout)
{
(void) go_cart_scale_mult(&p1->tran, s, &pout->tran);
return go_quat_scale_mult(&p1->rot, s, &pout->rot);
}
int go_pose_pose_interp(go_real t1, const go_pose * p1, go_real t2, const go_pose * p2, go_real t3, go_pose * p3)
{
go_pose pdiff;
int retval;
if (GO_CLOSE(t1, t2)) return GO_RESULT_ERROR;
retval = go_pose_inv(p1, &pdiff);
if (GO_RESULT_OK != retval) return retval;
(void) go_pose_pose_mult(&pdiff, p2, &pdiff);
(void) go_pose_scale_mult(&pdiff, (t3 - t1) / (t2 - t1), &pdiff);
return go_pose_pose_mult(p1, &pdiff, p3);
}
/* homogeneous transform functions */
int go_hom_inv(const go_hom * h1, go_hom * h2)
{
int retval;
retval = go_mat_inv(&h1->rot, &h2->rot);
if (GO_RESULT_OK != retval) return retval;
retval = go_mat_cart_mult(&h2->rot, &h1->tran, &h2->tran);
if (GO_RESULT_OK != retval) return retval;
h2->tran.x = -h2->tran.x;
h2->tran.y = -h2->tran.y;
h2->tran.z = -h2->tran.z;
return GO_RESULT_OK;
}
int go_pose_screw_mult(const go_pose * pose, const go_screw * screw, go_screw * out)
{
go_pose poseinv;
go_cart wxp;
go_cart v;
int retval;
/*
B |B . B |
.T = | R . P |
A |A . A|
B B A A A
.v = R ( v + w X P )
. A B
B B A
.w = R w
. A
*/
retval = go_pose_inv(pose, &poseinv);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_cross(&screw->w, &poseinv.tran, &wxp);
go_cart_cart_add(&screw->v, &wxp, &v);
go_quat_cart_mult(&pose->rot, &v, &out->v);
go_quat_cart_mult(&pose->rot, &screw->w, &out->w);
return GO_RESULT_OK;
}
/* line and plane functions */
int go_line_from_poGO_RESULT_direction(const go_cart * point, const go_cart * direction, go_line * line)
{
int retval;
retval = go_cart_unit(direction, &line->direction);
if (GO_RESULT_OK != retval) return GO_RESULT_DIV_ERROR;
line->point = *point;
return GO_RESULT_OK;
}
int go_line_from_points(const go_cart * point1, const go_cart * point2, go_line * line)
{
go_cart direction;
int retval;
(void) go_cart_cart_sub(point2, point1, &direction);
retval = go_cart_unit(&direction, &line->direction);
if (GO_RESULT_OK != retval) return GO_RESULT_DIV_ERROR;
line->point = *point1;
return GO_RESULT_OK;
}
int go_line_from_planes(const go_plane * plane1, const go_plane * plane2, go_line * line)
{
enum {X, Y, Z} which;
go_real max, ymax, denominv;
(void) go_cart_cart_cross(&plane1->normal, &plane2->normal, &line->direction);
if (GO_RESULT_OK != go_cart_unit(&line->direction, &line->direction)) {
/* planes are parallel */
return GO_RESULT_ERROR;
}
/* pick the cross product component with the largest magnitude,
which will give the most robust calculations for the point
on the line */
max = fabs(line->direction.x);
which = X;
ymax = fabs(line->direction.y);
if (ymax > max) {
max = ymax;
which = Y;
}
if (fabs(line->direction.z) > max) {
which = Z;
}
switch (which) {
case X:
denominv = 1.0/(plane1->normal.y*plane2->normal.z -
plane2->normal.y*plane1->normal.z);
line->point.y = (plane1->normal.z*plane2->d -
plane2->normal.z*plane1->d)*denominv;
line->point.z = (plane2->normal.y*plane1->d -
plane1->normal.y*plane2->d)*denominv;
line->point.x = 0;
break;
case Y:
denominv = 1.0/(plane1->normal.z*plane2->normal.x -
plane2->normal.z*plane1->normal.x);
line->point.z = (plane1->normal.x*plane2->d -
plane2->normal.x*plane1->d)*denominv;
line->point.x = (plane2->normal.z*plane1->d -
plane1->normal.z*plane2->d)*denominv;
line->point.y = 0;
break;
default:
denominv = 1.0/(plane1->normal.x*plane2->normal.y -
plane2->normal.x*plane1->normal.y);
line->point.x = (plane1->normal.y*plane2->d -
plane2->normal.y*plane1->d)*denominv;
line->point.y = (plane2->normal.x*plane1->d -
plane1->normal.x*plane2->d)*denominv;
line->point.z = 0;
break;
}
return GO_RESULT_OK;
}
go_flag go_line_line_compare(const go_line * line1, const go_line * line2)
{
go_cart diff;
go_real dot;
/* check that directions are parallel */
go_cart_cart_dot(&line1->direction, &line2->direction, &dot);
if (! GO_CLOSE(dot, 1)) return 0;
/* check that points are along the direction */
(void) go_cart_cart_sub(&line1->point, &line2->point, &diff);
go_cart_cart_dot(&line1->direction, &diff, &dot);
if (! GO_CLOSE(dot, 1)) return 0;
return 1;
}
int go_line_evaluate(const go_line * line, go_real d, go_cart * point)
{
go_cart v;
(void) go_cart_scale_mult(&line->direction, d, &v);
(void) go_cart_cart_add(&line->point, &v, point);
return GO_RESULT_OK;
}
int go_poGO_RESULT_line_distance(const go_cart * point, const go_line * line, go_real * distance)
{
*distance = sqrt(
go_sq(line->direction.z * (point->y - line->point.y) -
line->direction.y * (point->z - line->point.z)) +
go_sq(line->direction.x * (point->z - line->point.z) -
line->direction.z * (point->x - line->point.x)) +
go_sq(line->direction.y * (point->x - line->point.x) -
line->direction.x * (point->y - line->point.y)));
return GO_RESULT_OK;
}
int go_poGO_RESULT_line_proj(const go_cart * point, const go_line * line, go_cart * pout)
{
go_cart vp;
int retval;
go_cart_cart_sub(point, &line->point, &vp);
retval = go_cart_cart_proj(&vp, &line->direction, &vp);
if (GO_RESULT_OK != retval) return retval;
go_cart_cart_add(&line->point, &vp, pout);
return GO_RESULT_OK;
}
int go_poGO_RESULT_plane_proj(const go_cart * point, const go_plane * plane, go_cart * proj)
{
go_real denom;
go_real k;
denom =
go_sq(plane->normal.x) +
go_sq(plane->normal.y) +
go_sq(plane->normal.z);
if (GO_TRAN_SMALL(denom)) return GO_RESULT_DIV_ERROR;
k = -(plane->normal.x * point->x +
plane->normal.y * point->y +
plane->normal.z * point->z +
plane->d)/denom;
proj->x = point->x + k*plane->normal.x;
proj->y = point->y + k*plane->normal.y;
proj->z = point->z + k*plane->normal.z;
return GO_RESULT_OK;
}
int go_line_plane_proj(const go_line * line, const go_plane * plane, go_line * proj)
{
int retval;
retval = go_cart_plane_proj(&line->direction, &plane->normal, &proj->direction);
if (GO_RESULT_OK != retval) return retval;
return go_poGO_RESULT_plane_proj(&line->point, plane, &proj->point);
}
int go_plane_from_poGO_RESULT_normal(const go_cart * point, const go_cart * normal, go_plane * plane)
{
if (GO_RESULT_OK != go_cart_unit(normal, &plane->normal)) return GO_RESULT_ERROR;
/* D = -(Ax + By + Cz) */
plane->d = -(plane->normal.x * point->x +
plane->normal.y * point->y +
plane->normal.z * point->z);
return GO_RESULT_OK;
}
int go_plane_from_abcd(go_real A, go_real B, go_real C, go_real D, go_plane * plane)
{
go_real mag;
/* get magnitude of normal vector, actually mag squared */
mag = go_sq(A) + go_sq(B) + go_sq(C);
if (GO_TRAN_SMALL(mag)) {
return GO_RESULT_DIV_ERROR;
}
mag = 1.0 / sqrt(mag); /* now it's the mag multiplier */
plane->normal.x = A * mag;
plane->normal.y = B * mag;
plane->normal.z = C * mag;
plane->d = D * mag;
return GO_RESULT_OK;
}
int go_plane_from_points(const go_cart * point1, const go_cart * point2, const go_cart * point3, go_plane * plane)
{
go_cart v12, v23;
/* cross vectors from 1-2, 2-3 to get normal */
go_cart_cart_sub(point2, point1, &v12);
go_cart_cart_sub(point3, point2, &v23);
go_cart_cart_cross(&v12, &v23, &plane->normal);
if (GO_RESULT_OK != go_cart_unit(&plane->normal, &plane->normal)) return GO_RESULT_ERROR;
/* D = -(Ax + By + Cz) */
plane->d = -(plane->normal.x * point1->x +
plane->normal.y * point1->y +
plane->normal.z * point1->z);
return GO_RESULT_OK;
}
int go_plane_from_poGO_RESULT_line(const go_cart * point, const go_line * line, go_plane * plane)
{
go_cart p0, p1;
(void) go_line_evaluate(line, 0, &p0);
(void) go_line_evaluate(line, 1, &p1);
return go_plane_from_points(&p0, &p1, point, plane);
}
/* for planes to be the same, they must have the same normal vector
and same 'd', so the comparison is simple */
go_flag go_plane_plane_compare(const go_plane * plane1, const go_plane * plane2)
{
if (! go_cart_cart_compare(&plane1->normal, &plane2->normal)) return 0;
return GO_CLOSE(plane1->d, plane2->d);
}
int go_poGO_RESULT_plane_distance(const go_cart * point, const go_plane * plane, go_real * distance)
{
*distance = plane->normal.x * point->x +
plane->normal.y * point->y +
plane->normal.z * point->z +
plane->d;
return GO_RESULT_OK;
}
int go_plane_evaluate(const go_plane * plane, go_real u, go_real v, go_cart * point)
{
go_cart v1, v2; /* othogonal vectors in plane */
go_cart p; /* point in plane closest to origin */
if (GO_RESULT_OK != go_cart_normal(&plane->normal, &v1)) return GO_RESULT_ERROR;
(void) go_cart_cart_cross(&plane->normal, &v1, &v2);
(void) go_cart_scale_mult(&v1, u, &v1);
(void) go_cart_scale_mult(&v2, v, &v2);
/* with point = k * normal,
a*ka * y*ky + z*kz + d = 0,
k*(1) + d = 0,
k = -d
*/
p.x = -plane->normal.x * plane->d;
p.y = -plane->normal.y * plane->d;
p.z = -plane->normal.z * plane->d;
(void) go_cart_cart_add(&p, &v1, &p);
(void) go_cart_cart_add(&p, &v2, point);
return GO_RESULT_OK;
}
/*
To get the intersect point of the line and plane, set them equal and
solve for the line parameter d, then plug into the parameterization
to get the point:
A(px + d*vx) + B(py + d*vy) + C(pz + d*vz) + D = 0
_ -(A*px + B*py + C*pz + D)
d ---------------------
_ A*vx + B*vy + C*vz
Then plug d into px + d*vx, ... to get intersect x, y and z.
*/
int go_line_plane_intersect(const go_line * line, const go_plane * plane, go_cart * point, go_real * distance)
{
go_real num, denom;
/* first check for line parallel to plane */
(void) go_cart_cart_dot(&plane->normal, &line->direction, &denom);
if (GO_SMALL(denom)) return GO_RESULT_ERROR;
/* check for point lying in plane */
(void) go_cart_cart_dot(&plane->normal, &line->point, &num);
num += plane->d;
if (GO_SMALL(num)) {
*point = line->point;
/* distance probably isn't exactly zero, so compute it */
return go_poGO_RESULT_plane_distance(&line->point, plane, distance);
}
/* else plug in d to get intersect point */
*distance = -num / denom;
return go_line_evaluate(line, *distance, point);
}
/* general vector and matrix functions */
/*
Adapted from ludcmp routine in Numerical Recipes in C, but with
indices starting at 0 and no heap allocation.
Given a matrix a[0..n-1][0..n-1], this routine replaces it by the LU
decomposition of a rowwise permutation of itself. a and n are
input. a is changed and output. indx[0..n-1] is an output vector
that records the row permutation effected by the partial pivoting; d
is output as +/-1 depending on whether the number of row
interchanges was even or odd, respectively. This routine is used in
combination with lubksb to solve linear equations or invert a
matrix.
Warning! The matrix 'a' can't be declared as go_real a[n][n]. It
needs to be an array of go_real pointers. See e.g. go_mat6_inv() for
how to set up the 'a' matrix.
*/
static go_real go_singular_epsilon = 1.0e-15;
go_real go_get_singular_epsilon(void)
{
return go_singular_epsilon;
}
int go_set_singular_epsilon(go_real epsilon)
{
if (epsilon <= 0.0) return GO_RESULT_ERROR;
go_singular_epsilon = epsilon;
return GO_RESULT_OK;
}
int ludcmp(go_real ** a,
go_real * scratchrow,
go_integer n,
go_integer * indx,
go_real * d)
{
go_integer i, imax, j, k;
go_real big, dum, sum, temp;
*d = 1.0;
for (i = 0; i < n; i++) {
big = 0.0;
for (j = 0; j < n; j++)
if ((temp = fabs(a[i][j])) > big)
big = temp;
if (big < go_singular_epsilon)
return GO_RESULT_SINGULAR;
scratchrow[i] = 1.0 / big;
}
for (j = 0; j < n; j++) {
for (i = 0; i < j; i++) {
sum = a[i][j];
for (k = 0; k < i; k++)
sum -= a[i][k] * a[k][j];
a[i][j] = sum;
}
big = 0.0;
imax = 0;
for (i = j; i < n; i++) {
sum = a[i][j];
for (k = 0; k < j; k++)
sum -= a[i][k] * a[k][j];
a[i][j] = sum;
if ((dum = scratchrow[i] * fabs(sum)) >= big) {
big = dum;
imax = i;
}
}
if (j != imax) {
for (k = 0; k < n; k++) {
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
*d = -(*d);
scratchrow[imax] = scratchrow[j];
}
indx[j] = imax;
if (fabs(a[j][j]) < go_singular_epsilon)
return GO_RESULT_SINGULAR;
if (j != n - 1) {
dum = 1.0 / (a[j][j]);
for (i = j + 1; i < n; i++)
a[i][j] *= dum;
}
}
return GO_RESULT_OK;
}
/*
Solves the set of n linear equations A·X = B. Here a[0..n-1][0..n-1]
is input, not as the matrix A but rather as its LU decomposition,
determined by the routine ludcmp. indx[0..n-1] is input as the
permutation vector returned by ludcmp. b[0..n-1] is input as the
right-hand side vector B, and returns with the solution vector X. a,
n, and indx are not modified by this routine and can be left in
place for successive calls with different right-hand sides b. This
routine takes into account the possibility that b will begin with
many zero elements, so it is efficient for use in matrix inversion.
Warning! The matrix 'a' can't be declared as go_real a[n][n]. It
needs to be an array of go_real pointers. See e.g. go_mat6_inv() for
how to set up the 'a' matrix.
*/
int lubksb(go_real ** a,
go_integer n,
go_integer * indx,
go_real * b)
{
go_integer i, ii = -1, ip, j;
go_real sum;
for (i = 0; i < n; i++) {
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii != -1)
for (j = ii; j <= i - 1; j++)
sum -= a[i][j] * b[j];
else if (sum)
ii = i;
b[i] = sum;
}
for (i = n - 1; i >= 0; i--) {
sum = b[i];
for (j = i + 1; j < n; j++)
sum -= a[i][j] * b[j];
if (fabs(a[i][i]) < go_singular_epsilon)
return GO_RESULT_SINGULAR;
b[i] = sum / a[i][i];
}
return GO_RESULT_OK;
}
int go_cart_vector_convert(const go_cart * c,
go_real * v)
{
v[0] = c->x, v[1] = c->y, v[2] = c->z;
return GO_RESULT_OK;
}
int go_vector_cart_convert(const go_real * v,
go_cart * c)
{
c->x = v[0], c->y = v[1], c->z = v[2];
return GO_RESULT_OK;
}
int go_quat_matrix_convert(const go_quat * quat,
go_matrix * matrix)
{
go_mat mat;
int retval;
/* check for an initialized matrix */
if (0 == matrix->el[0]) return GO_RESULT_ERROR;
/* check for a 3x3 matrix */
if (matrix->rows != 3 || matrix->cols != 3) return GO_RESULT_ERROR;
retval = go_quat_mat_convert(quat, &mat);
if (GO_RESULT_OK != retval) return retval;
return go_mat_matrix_convert(&mat, matrix);
}
/* | m.x.x m.y.x m.z.x | */
/* go_mat m = | m.x.y m.y.y m.z.y | */
/* | m.x.z m.y.z m.z.z | */
/* go_matrix mout[row][col] */
int go_mat_matrix_convert(const go_mat * mat,
go_matrix * matrix)
{
/* check for an initialized matrix */
if (0 == matrix->el[0]) return GO_RESULT_ERROR;
/* check for a 3x3 matrix */
if (matrix->rows != 3 || matrix->cols != 3) return GO_RESULT_ERROR;
matrix->el[0][0] = mat->x.x, matrix->el[0][1] = mat->y.x, matrix->el[0][2] = mat->z.x;
matrix->el[1][0] = mat->x.y, matrix->el[1][1] = mat->y.y, matrix->el[1][2] = mat->z.y;
matrix->el[2][0] = mat->x.z, matrix->el[2][1] = mat->y.z, matrix->el[2][2] = mat->z.z;
return GO_RESULT_OK;
}
int go_matrix_matrix_add(const go_matrix * a,
const go_matrix * b,
go_matrix * apb)
{
go_integer row, col;
/* check for an initialized matrix */
if (0 == a->el[0] || 0 == b->el[0] || 0 == apb->el[0]) return GO_RESULT_ERROR;
/* check for matching rows and cols */
if (a->rows != b->rows || a->cols != b->cols ||
b->rows != apb->rows || b->cols != apb->cols) return GO_RESULT_ERROR;
for (row = 0; row < a->rows; row++) {
for (col = 0; col < a->cols; col++) {
apb->el[row][col] = a->el[row][col] + b->el[row][col];
}
}
return GO_RESULT_OK;
}
int go_matrix_matrix_copy(const go_matrix * src,
go_matrix * dst)
{
go_integer row, col;
/* check for an initialized matrix */
if (0 == src->el[0] || 0 == dst->el[0]) return GO_RESULT_ERROR;
/* check for matching rows and cols */
if (src->rows != dst->rows || src->cols != dst->cols) return GO_RESULT_ERROR;
for (row = 0; row < src->rows; row++) {
for (col = 0; col < src->cols; col++) {
dst->el[row][col] = src->el[row][col];
}
}
return GO_RESULT_OK;
}
int go_matrix_matrix_mult(const go_matrix * a,
const go_matrix * b,
go_matrix * ab)
{
go_real ** ptrin;
go_real ** ptrout;
go_integer row, col, i;
/* check for an initialized matrix */
if (0 == a->el[0] || 0 == b->el[0] || 0 == ab->el[0]) return GO_RESULT_ERROR;
/* check for consistent rows and cols */
if (a->cols != b->rows ||
a->rows != ab->rows ||
b->cols != ab->cols) return GO_RESULT_ERROR;
if (ab == a) {
/* destructive multiply, use a's copy space and copy back */
ptrin = a->elcpy;
ptrout = a->el;
} else if (ab == b) {
ptrin = b->elcpy;
ptrout = b->el;
} else {
ptrin = ab->el;
ptrout = NULL;
}
for (row = 0; row < a->rows; row++) {
for (col = 0; col < b->cols; col++) {
ptrin[row][col] = 0;
for (i = 0; i < a->cols; i++) {
ptrin[row][col] += a->el[row][i] * b->el[i][col];
}
}
}
if (NULL != ptrout) {
for (row = 0; row < ab->rows; row++) {
for (col = 0; col < ab->cols; col++) {
ptrout[row][col] = ptrin[row][col];
}
}
}
return GO_RESULT_OK;
}
int go_matrix_vector_mult(const go_matrix * a,
const go_vector * v,
go_vector * axv)
{
go_vector * ptrin;
go_vector * ptrout;
go_integer row, i;
/* check for an initialized matrix */
if (0 == a->el[0]) return GO_RESULT_ERROR;
if (axv == v) {
ptrin = a->elcpy[0];
ptrout = axv;
} else {
ptrin = axv;
ptrout = NULL;
}
for (row = 0; row < a->rows; row++) {
ptrin[row] = 0;
for (i = 0; i < a->cols; i++) {
ptrin[row] += a->el[row][i] * v[i];
}
}
if (ptrout != NULL) {
for (row = 0; row < a->rows; row++) {
ptrout[row] = ptrin[row];
}
}
return GO_RESULT_OK;
}
/*
The matrix-vector cross product is a matrix of the same dimension,
whose columns are the column-wise cross products of the matrix
and the vector. The matrices must be 3xN, the vector 3x1.
*/
int go_matrix_vector_cross(const go_matrix * a,
const go_vector * v,
go_matrix * axv)
{
go_real ** ptrin;
go_real ** ptrout;
go_cart vc; /* 'v' */
go_cart ac; /* a column of the 'a' matrix */
go_cart cross;
go_integer row, col;
/* check for an initialized matrix */
if (0 == a->el[0] || 0 == axv->el[0]) return GO_RESULT_ERROR;
/* check for consistent rows and cols */
if (a->rows != 3 ||
axv->rows != 3 ||
a->cols != axv->cols) return GO_RESULT_ERROR;
if (axv == a) {
/* destructive multiply, use a's copy space and copy back */
ptrin = a->elcpy;
ptrout = a->el;
} else {
ptrin = axv->el;
ptrout = NULL;
}
/* get 'v' as a cartesian type */
vc.x = v[0], vc.y = v[1], vc.z = v[2];
for (col = 0; col < a->cols; col++) {
/* pick off the col'th column as a cartesian type */
ac.x = a->el[0][col], ac.y = a->el[1][col], ac.z = a->el[2][col];
/* cross it with v */
go_cart_cart_cross(&ac, &vc, &cross);
/* make it the col'th column of axv[] */
ptrin[0][col] = cross.x, ptrin[1][col] = cross.y, ptrin[2][col] = cross.z;
}
if (ptrout != NULL) {
for (row = 0; row < a->rows; row++) {
for (col = 0; col < a->cols; col++) {
ptrout[row][col] = ptrin[row][col];
}
}
}
return GO_RESULT_OK;
}
int go_matrix_transpose(const go_matrix * a,
go_matrix * at)
{
go_real ** ptrin;
go_real ** ptrout;
go_integer row, col;
/* check for fixed matrix */
if (0 == a->el[0] || 0 == at->el[0]) return GO_RESULT_ERROR;
if (at == a) {
ptrin = a->elcpy;
ptrout = a->el;
} else {
ptrin = at->el;
ptrout = NULL;
}
for (row = 0; row < a->rows; row++) {
for (col = 0; col < a->cols; col++) {
ptrin[col][row] = a->el[row][col];
}
}
if (ptrout != NULL) {
for (row = 0; row < a->rows; row++) {
for (col = 0; col < a->cols; col++) {
ptrout[row][col] = ptrin[row][col];
}
}
}
return GO_RESULT_OK;
}
int go_matrix_inv(const go_matrix * m, /* M x N */
go_matrix * minv) /* N x M */
{
go_real d;
go_integer N, row, col;
int retval;
/* check for fixed matrix */
if (0 == m->el[0] || 0 == minv->el[0]) return GO_RESULT_ERROR;
N = m->rows;
/* copy of m since ludcmp destroys input matrix */
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++) {
m->elcpy[row][col] = m->el[row][col];
}
}
/* convert the copy to its LU decomposition */
retval = ludcmp(m->elcpy, m->v, N, m->index, &d);
if (GO_RESULT_OK != retval) return retval;
/* backsubstitute a column with a 1 in it to get the inverse */
for (col = 0; col < N; col++) {
for (row = 0; row < N; row++) {
m->v[row] = 0.0;
}
m->v[col] = 1.0;
retval = lubksb(m->elcpy, N, m->index, m->v);
if (GO_RESULT_OK != retval) return retval;
for (row = 0; row < N; row++) {
minv->el[row][col] = m->v[row];
}
}
return GO_RESULT_OK;
}
extern int go_mat3_inv(const go_real a[3][3],
go_real ainv[3][3])
{
go_real a11a22;
go_real a11a23;
go_real a11a32;
go_real a11a33;
go_real a12a33;
go_real a12a23;
go_real a12a21;
go_real a12a31;
go_real a13a21;
go_real a13a22;
go_real a13a31;
go_real a13a32;
go_real a21a32;
go_real a21a33;
go_real a22a31;
go_real a22a33;
go_real a23a31;
go_real a23a32;
go_real denom;
a11a22 = a[0][0]*a[1][1];
a11a23 = a[0][0]*a[1][2];
a11a32 = a[0][0]*a[2][1];
a11a33 = a[0][0]*a[2][2];
a12a21 = a[0][1]*a[1][0];
a12a23 = a[0][1]*a[1][2];
a12a31 = a[0][1]*a[2][0];
a12a33 = a[0][1]*a[2][2];
a13a21 = a[0][2]*a[1][0];
a13a22 = a[0][2]*a[1][1];
a13a31 = a[0][2]*a[2][0];
a13a32 = a[0][2]*a[2][1];
a21a32 = a[1][0]*a[2][1];
a21a33 = a[1][0]*a[2][2];
a22a31 = a[1][1]*a[2][0];
a22a33 = a[1][1]*a[2][2];
a23a31 = a[1][2]*a[2][0];
a23a32 = a[1][2]*a[2][1];
denom =
+ a11a22*a[2][2]
- a11a23*a[2][1]
- a12a33*a[1][0]
+ a13a21*a[2][1]
- a13a22*a[2][0]
+ a12a23*a[2][0];
if (GO_SMALL(denom)) {
return GO_RESULT_SINGULAR;
}
denom = 1.0/denom;
ainv[0][0] = (a22a33 - a23a32)*denom;
ainv[0][1] = (a13a32 - a12a33)*denom;
ainv[0][2] = (a12a23 - a13a22)*denom;
ainv[1][0] = (a23a31 - a21a33)*denom;
ainv[1][1] = (a11a33 - a13a31)*denom;
ainv[1][2] = (a13a21 - a11a23)*denom;
ainv[2][0] = (a21a32 - a22a31)*denom;
ainv[2][1] = (a12a31 - a11a32)*denom;
ainv[2][2] = (a11a22 - a12a21)*denom;
return GO_RESULT_OK;
}
int go_mat3_mat3_mult(const go_real a[3][3],
const go_real b[3][3],
go_real axb[3][3])
{
go_real work[3][3];
go_integer i, j, k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
work[i][j] = 0;
for (k = 0; k < 3; k++) {
work[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
axb[i][j] = work[i][j];
return GO_RESULT_OK;
}
int go_mat3_vec3_mult(const go_real a[3][3],
const go_real v[3],
go_real axv[3])
{
go_real work[3];
go_integer i, j;
for (i = 0; i < 3; i++) {
work[i] = 0;
for (j = 0; j < 3; j++) {
work[i] += a[i][j] * v[j];
}
}
for (i = 0; i < 3; i++)
axv[i] = work[i];
return GO_RESULT_OK;
}
extern int go_mat4_inv(const go_real a[4][4],
go_real ainv[4][4])
{
go_real work[4][4];
go_real denom;
go_real a11a22;
go_real a11a23;
go_real a11a24;
go_real a11a33;
go_real a11a32;
go_real a11a34;
go_real a12a21;
go_real a12a23;
go_real a12a24;
go_real a12a31;
go_real a12a33;
go_real a12a34;
go_real a13a21;
go_real a13a22;
go_real a13a24;
go_real a13a31;
go_real a13a32;
go_real a13a34;
go_real a14a21;
go_real a14a22;
go_real a14a23;
go_real a14a31;
go_real a14a32;
go_real a14a33;
go_real a21a32;
go_real a21a33;
go_real a21a34;
go_real a22a31;
go_real a22a33;
go_real a22a34;
go_real a23a31;
go_real a23a32;
go_real a23a34;
go_real a24a31;
go_real a24a32;
go_real a24a33;
go_real a11a22a33;
go_real a11a22a34;
go_real a11a23a32;
go_real a11a23a34;
go_real a11a24a32;
go_real a11a24a33;
go_real a12a21a33;
go_real a12a21a34;
go_real a12a23a31;
go_real a12a23a34;
go_real a12a24a31;
go_real a12a24a33;
go_real a13a21a32;
go_real a13a21a34;
go_real a13a22a31;
go_real a13a22a34;
go_real a13a24a31;
go_real a13a24a32;
go_real a14a21a32;
go_real a14a21a33;
go_real a14a22a31;
go_real a14a22a33;
go_real a14a23a31;
go_real a14a23a32;
go_integer row, col;
a11a22 = a[0][0]*a[1][1];
a11a23 = a[0][0]*a[1][2];
a11a24 = a[0][0]*a[1][3];
a11a33 = a[0][0]*a[2][2];
a11a32 = a[0][0]*a[2][1];
a11a34 = a[0][0]*a[2][3];
a12a21 = a[0][1]*a[1][0];
a12a23 = a[0][1]*a[1][2];
a12a24 = a[0][1]*a[1][3];
a12a31 = a[0][1]*a[2][0];
a12a33 = a[0][1]*a[2][2];
a12a34 = a[0][1]*a[2][3];
a13a21 = a[0][2]*a[1][0];
a13a22 = a[0][2]*a[1][1];
a13a24 = a[0][2]*a[1][3];
a13a31 = a[0][2]*a[2][0];
a13a32 = a[0][2]*a[2][1];
a13a34 = a[0][2]*a[2][3];
a14a21 = a[0][3]*a[1][0];
a14a22 = a[0][3]*a[1][1];
a14a23 = a[0][3]*a[1][2];
a14a31 = a[0][3]*a[2][0];
a14a32 = a[0][3]*a[2][1];
a14a33 = a[0][3]*a[2][2];
a21a32 = a[1][0]*a[2][1];
a21a33 = a[1][0]*a[2][2];
a21a34 = a[1][0]*a[2][3];
a22a31 = a[1][1]*a[2][0];
a22a33 = a[1][1]*a[2][2];
a22a34 = a[1][1]*a[2][3];
a23a31 = a[1][2]*a[2][0];
a23a32 = a[1][2]*a[2][1];
a23a34 = a[1][2]*a[2][3];
a24a31 = a[1][3]*a[2][0];
a24a32 = a[1][3]*a[2][1];
a24a33 = a[1][3]*a[2][2];
a11a22a33 = a11a22*a[2][2];
a11a22a34 = a11a22*a[2][3];
a11a23a32 = a11a23*a[2][1];
a11a23a34 = a11a23*a[2][3];
a11a24a32 = a11a24*a[2][1];
a11a24a33 = a11a24*a[2][2];
a12a21a33 = a12a21*a[2][2];
a12a21a34 = a12a21*a[2][3];
a12a23a31 = a12a23*a[2][0];
a12a23a34 = a12a23*a[2][3];
a12a24a31 = a12a24*a[2][0];
a12a24a33 = a12a24*a[2][2];
a13a21a32 = a13a21*a[2][1];
a13a21a34 = a13a21*a[2][3];
a13a22a31 = a13a22*a[2][0];
a13a22a34 = a13a22*a[2][3];
a13a24a31 = a13a24*a[2][0];
a13a24a32 = a13a24*a[2][1];
a14a21a32 = a14a21*a[2][1];
a14a21a33 = a14a21*a[2][2];
a14a22a31 = a14a22*a[2][0];
a14a22a33 = a14a22*a[2][2];
a14a23a31 = a14a23*a[2][0];
a14a23a32 = a14a23*a[2][1];
denom =
+ a12a24a33*a[3][0]
+ a13a24a31*a[3][1]
- a12a21a33*a[3][3]
- a13a24a32*a[3][0]
- a14a21a32*a[3][2]
- a12a24a31*a[3][2]
+ a14a23a32*a[3][0]
+ a13a21a32*a[3][3]
+ a14a21a33*a[3][1]
+ a12a23a31*a[3][3]
- a14a23a31*a[3][1]
+ a12a21a34*a[3][2]
- a12a23a34*a[3][0]
- a13a21a34*a[3][1]
- a13a22a31*a[3][3]
+ a14a22a31*a[3][2]
+ a13a22a34*a[3][0]
- a14a22a33*a[3][0]
+ a11a22a33*a[3][3]
- a11a22a34*a[3][2]
+ a11a24a32*a[3][2]
- a11a23a32*a[3][3]
+ a11a23a34*a[3][1]
- a11a24a33*a[3][1];
if (GO_SMALL(denom)) {
return GO_RESULT_SINGULAR;
}
denom = 1.0/denom;
work[0][0] = (+ a22a33*a[3][3]
- a22a34*a[3][2]
+ a24a32*a[3][2]
- a23a32*a[3][3]
+ a23a34*a[3][1]
- a24a33*a[3][1])*denom;
work[0][1] = (- a12a33*a[3][3]
+ a12a34*a[3][2]
- a13a34*a[3][1]
+ a13a32*a[3][3]
- a14a32*a[3][2]
+ a14a33*a[3][1])*denom;
work[0][2] = (+ a12a23*a[3][3]
- a12a24*a[3][2]
- a13a22*a[3][3]
+ a13a24*a[3][1]
+ a14a22*a[3][2]
- a14a23*a[3][1])*denom;
work[0][3] = (- a12a23a34
+ a12a24a33
+ a13a22a34
- a13a24a32
- a14a22a33
+ a14a23a32)*denom;
work[1][0] = (+ a23a31*a[3][3]
- a23a34*a[3][0]
- a21a33*a[3][3]
+ a21a34*a[3][2]
+ a24a33*a[3][0]
- a24a31*a[3][2])*denom;
work[1][1] = (+ a11a33*a[3][3]
- a11a34*a[3][2]
- a13a31*a[3][3]
+ a14a31*a[3][2]
+ a13a34*a[3][0]
- a14a33*a[3][0])*denom;
work[1][2] = (- a11a23*a[3][3]
+ a11a24*a[3][2]
+ a13a21*a[3][3]
+ a14a23*a[3][0]
- a14a21*a[3][2]
- a13a24*a[3][0])*denom;
work[1][3] = (+ a11a23a34
- a11a24a33
- a13a21a34
+ a14a21a33
- a14a23a31
+ a13a24a31)*denom;
work[2][0] = (+ a22a34*a[3][0]
- a22a31*a[3][3]
- a24a32*a[3][0]
+ a21a32*a[3][3]
- a21a34*a[3][1]
+ a24a31*a[3][1])*denom;
work[2][1] = (+ a11a34*a[3][1]
- a11a32*a[3][3]
- a12a34*a[3][0]
- a14a31*a[3][1]
+ a12a31*a[3][3]
+ a14a32*a[3][0])*denom;
work[2][2] = (- a11a24*a[3][1]
- a14a22*a[3][0]
+ a11a22*a[3][3]
+ a12a24*a[3][0]
+ a14a21*a[3][1]
- a12a21*a[3][3])*denom;
work[2][3] = (- a11a22a34
+ a11a24a32
+ a14a22a31
- a12a24a31
+ a12a21a34
- a14a21a32)*denom;
work[3][0] = (+ a21a33*a[3][1]
+ a23a32*a[3][0]
- a22a33*a[3][0]
- a23a31*a[3][1]
+ a22a31*a[3][2]
- a21a32*a[3][2])*denom;
work[3][1] = (+ a12a33*a[3][0]
- a13a32*a[3][0]
- a12a31*a[3][2]
+ a13a31*a[3][1]
+ a11a32*a[3][2]
- a11a33*a[3][1])*denom;
work[3][2] = (- a12a23*a[3][0]
+ a12a21*a[3][2]
- a13a21*a[3][1]
+ a13a22*a[3][0]
- a11a22*a[3][2]
+ a11a23*a[3][1])*denom;
work[3][3] = (+ a13a21a32
+ a12a23a31
- a12a21a33
- a13a22a31
+ a11a22a33
- a11a23a32)*denom;
for (row = 0; row < 4; row++)
for (col = 0; col < 4; col++)
ainv[row][col] = work[row][col];
return GO_RESULT_OK;
}
int go_mat4_mat4_mult(const go_real a[4][4],
const go_real b[4][4],
go_real axb[4][4])
{
go_real work[4][4];
go_integer i, j, k;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
work[i][j] = 0;
for (k = 0; k < 4; k++) {
work[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
axb[i][j] = work[i][j];
return GO_RESULT_OK;
}
int go_mat4_vec4_mult(const go_real a[4][4],
const go_real v[4],
go_real axv[4])
{
go_real work[4];
go_integer i, j;
for (i = 0; i < 4; i++) {
work[i] = 0;
for (j = 0; j < 4; j++) {
work[i] += a[i][j] * v[j];
}
}
for (i = 0; i < 4; i++)
axv[i] = work[i];
return GO_RESULT_OK;
}
int go_mat6_inv(const go_real a[6][6],
go_real ainv[6][6])
{
go_real cpy[6][6];
go_real *cpyptr[6];
go_real scratchrow[6];
go_real v[6];
go_real d;
go_integer index[6];
go_integer row, col;
int retval;
/* create a copy of m[][] in cpy[][], since ludcmp destroys input matrix */
for (row = 0; row < 6; row++) {
for (col = 0; col < 6; col++) {
cpy[row][col] = a[row][col];
}
/* set up the go_real pointer array that ludcmp likes */
cpyptr[row] = cpy[row];
}
/* convert the copy to its LU decomposition */
retval = ludcmp(cpyptr, scratchrow, 6, index, &d);
if (GO_RESULT_OK != retval) return retval;
/* backsubstitute a column with a 1 in it to get the inverse */
for (col = 0; col < 6; col++) {
for (row = 0; row < 6; row++) {
v[row] = 0.0;
}
v[col] = 1.0;
retval = lubksb(cpyptr, 6, index, v);
if (GO_RESULT_OK != retval) return retval;
for (row = 0; row < 6; row++) {
ainv[row][col] = v[row];
}
}
return GO_RESULT_OK;
}
int go_mat6_mat6_mult(const go_real a[6][6],
const go_real b[6][6],
go_real axb[6][6])
{
go_real work[6][6];
go_integer i, j, k;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
work[i][j] = 0;
for (k = 0; k < 6; k++) {
work[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
axb[i][j] = work[i][j];
return GO_RESULT_OK;
}
int go_mat6_vec6_mult(const go_real a[6][6],
const go_real v[6],
go_real axv[6])
{
go_real work[6];
go_integer i, j;
for (i = 0; i < 6; i++) {
work[i] = 0;
for (j = 0; j < 6; j++) {
work[i] += a[i][j] * v[j];
}
}
for (i = 0; i < 6; i++)
axv[i] = work[i];
return GO_RESULT_OK;
}
/* recall: */
/* | m.x.x m.y.x m.z.x | */
/* M = | m.x.y m.y.y m.z.y | */
/* | m.x.z m.y.z m.z.z | */
int go_dh_pose_convert(const go_dh * dh, go_pose * p)
{
go_hom h;
go_real sth, cth; /* sin, cos theta[i] */
go_real sal, cal; /* sin, cos alpha[i-1] */
sincos(dh->theta, &sth, &cth);
sincos(dh->alpha, &sal, &cal);
h.rot.x.x = cth, h.rot.y.x = -sth, h.rot.z.x = 0.0;
h.rot.x.y = sth*cal, h.rot.y.y = cth*cal, h.rot.z.y = -sal;
h.rot.x.z = sth*sal, h.rot.y.z = cth*sal, h.rot.z.z = cal;
h.tran.x = dh->a;
h.tran.y = -sal*dh->d;
h.tran.z = cal*dh->d;
return go_hom_pose_convert(&h, p);
}
int go_pose_dh_convert(const go_pose * ph, go_dh * dh)
{
go_hom h;
go_pose_hom_convert(ph, &h);
dh->a = h.tran.x;
dh->alpha = -atan2(h.rot.z.y, h.rot.z.z);
dh->theta = -atan2(h.rot.y.x, h.rot.x.x);
if (GO_ROT_SMALL(dh->alpha)) {
dh->d = h.tran.z / cos(dh->alpha);
} else {
dh->d = -h.tran.y / sin(dh->alpha);
}
return GO_RESULT_OK;
}
int go_link_joint_set(const go_link * link, go_real joint, go_link * linkout)
{
go_pose pose;
go_rvec rvec;
int retval;
linkout->type = link->type;
linkout->quantity = link->quantity;
if (GO_LINK_DH == link->type) {
linkout->u.dh.a = link->u.dh.a;
linkout->u.dh.alpha = link->u.dh.alpha;
if (GO_QUANTITY_LENGTH == link->quantity) {
linkout->u.dh.d = joint;
linkout->u.dh.theta = link->u.dh.theta;
} else {
linkout->u.dh.d = link->u.dh.d;
linkout->u.dh.theta = joint;
}
return GO_RESULT_OK;
}
if (GO_LINK_PP == link->type) {
pose = go_pose_identity();
if (GO_QUANTITY_LENGTH == link->quantity) {
pose.tran.z = joint;
return go_pose_pose_mult(&link->u.pp.pose, &pose, &linkout->u.pp.pose);
}
/* else revolute */
rvec.x = 0, rvec.y = 0, rvec.z = joint; /* rot(Z,joint) */
retval = go_rvec_quat_convert(&rvec, &pose.rot);
if (GO_RESULT_OK != retval) return retval;
return go_pose_pose_mult(&link->u.pp.pose, &pose, &linkout->u.pp.pose);
}
if (GO_LINK_PK == link->type) {
/*
Our PK type is always a length joint, so link->quantity must
be GO_QUANTITY_LENGTH. One day we may be able to handle revolute
parallel joints, but we can't now. Let's fail if we ever get
a revolute joint.
FIXME-- add revolute joints to PKMs.
*/
if (GO_QUANTITY_LENGTH != link->quantity) {
return GO_RESULT_IMPL_ERROR;
}
/* else we're a prismatic joint */
linkout->u.pk.base = link->u.pk.base;
linkout->u.pk.platform = link->u.pk.platform;
linkout->u.pk.d = joint;
return GO_RESULT_OK;
}
/* else not a recognized link type */
return GO_RESULT_ERROR;
}
/* this only works for serial-link manipulators */
int go_link_pose_build(const go_link * link_params, go_integer num, go_pose * pose)
{
go_pose p;
go_integer link;
*pose = go_pose_identity();
for (link = 0; link < num; link++) {
if (GO_LINK_DH == link_params[link].type) {
go_dh_pose_convert(&link_params[link].u.dh, &p);
go_pose_pose_mult(pose, &p, pose);
} else if (GO_LINK_PP == link_params[link].type) {
go_pose_pose_mult(pose, &link_params[link].u.pp.pose, pose);
} else {
return GO_RESULT_ERROR;
}
}
return GO_RESULT_OK;
}
|