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
|
/*
*class++
* Name:
* Ellipse
* Purpose:
* An elliptical region within a 2-dimensional Frame.
* Constructor Function:
c astEllipse
f AST_ELLIPSE
* Description:
* The Ellipse class implements a Region which represents a ellipse
* within a 2-dimensional Frame.
* Inheritance:
* The Ellipse class inherits from the Region class.
* Attributes:
* The Ellipse class does not define any new attributes beyond
* those which are applicable to all Regions.
* Functions:
c In addition to those functions applicable to all Regions, the
c following functions may also be applied to all Ellipses:
f In addition to those routines applicable to all Regions, the
f following routines may also be applied to all Ellipses:
*
c - astEllipsePars: Get the geometric parameters of the Ellipse
f - AST_ELLIPSEPARS: Get the geometric parameters of the Ellipse
* Copyright:
* Copyright (C) 1997-2006 Council for the Central Laboratory of the
* Research Councils
* Copyright (C) 2009 Science & Technology Facilities Council.
* All Rights Reserved.
* Licence:
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
* Authors:
* DSB: David S. Berry (Starlink)
* History:
* 7-SEP-2004 (DSB):
* Original version.
* 4-NOV-2013 (DSB):
* Modify RegPins so that it can handle uncertainty regions that straddle
* a discontinuity. Previously, such uncertainty Regions could have a huge
* bounding box resulting in matching region being far too big.
* 6-JAN-2014 (DSB):
* Ensure cached information is available in RegCentre even if no new
* centre is supplied.
*class--
*/
/* Module Macros. */
/* ============== */
/* Set the name of the class we are implementing. This indicates to
the header files that define class interfaces that they should make
"protected" symbols available. */
#define astCLASS Ellipse
/* Include files. */
/* ============== */
/* Interface definitions. */
/* ---------------------- */
#include "globals.h" /* Thread-safe global data access */
#include "error.h" /* Error reporting facilities */
#include "memory.h" /* Memory allocation facilities */
#include "object.h" /* Base Object class */
#include "pointset.h" /* Sets of points/coordinates */
#include "region.h" /* Coordinate regions (parent class) */
#include "channel.h" /* I/O channels */
#include "box.h" /* Box Regions */
#include "wcsmap.h" /* Definitons of AST__DPI etc */
#include "circle.h" /* Interface definition for circle class */
#include "ellipse.h" /* Interface definition for this class */
#include "mapping.h" /* Position mappings */
#include "unitmap.h" /* Unit Mapping */
#include "pal.h" /* Positional astronomy library */
/* Error code definitions. */
/* ----------------------- */
#include "ast_err.h" /* AST error codes */
/* C header files. */
/* --------------- */
#include <float.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/* Module Variables. */
/* ================= */
/* Address of this static variable is used as a unique identifier for
member of this class. */
static int class_check;
/* Pointers to parent class methods which are extended by this class. */
static AstPointSet *(* parent_transform)( AstMapping *, AstPointSet *, int, AstPointSet *, int * );
static AstMapping *(* parent_simplify)( AstMapping *, int * );
static void (* parent_setregfs)( AstRegion *, AstFrame *, int * );
static void (* parent_resetcache)( AstRegion *, int * );
#ifdef THREAD_SAFE
/* Define how to initialise thread-specific globals. */
#define GLOBAL_inits \
globals->Class_Init = 0;
/* Create the function that initialises global data for this module. */
astMAKE_INITGLOBALS(Ellipse)
/* Define macros for accessing each item of thread specific global data. */
#define class_init astGLOBAL(Ellipse,Class_Init)
#define class_vtab astGLOBAL(Ellipse,Class_Vtab)
#include <pthread.h>
#else
/* Define the class virtual function table and its initialisation flag
as static variables. */
static AstEllipseVtab class_vtab; /* Virtual function table */
static int class_init = 0; /* Virtual function table initialised? */
#endif
/* External Interface Function Prototypes. */
/* ======================================= */
/* The following functions have public prototypes only (i.e. no
protected prototypes), so we must provide local prototypes for use
within this module. */
AstEllipse *astEllipseId_( void *, int, const double[2], const double[2], const double[2], void *, const char *, ... );
/* Prototypes for Private Member Functions. */
/* ======================================== */
static AstMapping *Simplify( AstMapping *, int * );
static AstPointSet *RegBaseMesh( AstRegion *, int * );
static AstPointSet *Transform( AstMapping *, AstPointSet *, int, AstPointSet *, int * );
static double *RegCentre( AstRegion *this, double *, double **, int, int, int * );
static int RegPins( AstRegion *, AstPointSet *, AstRegion *, int **, int * );
static int RegTrace( AstRegion *, int, double *, double **, int * );
static void Cache( AstEllipse *, int * );
static void CalcPars( AstFrame *, double[2], double[2], double[2], double *, double *, double *, int * );
static void Copy( const AstObject *, AstObject *, int * );
static void Delete( AstObject *, int * );
static void Dump( AstObject *, AstChannel *, int * );
static void EllipsePars( AstEllipse *, double[2], double *, double *, double *, double[2], double[2], int * );
static void RegBaseBox( AstRegion *this, double *, double *, int * );
static void ResetCache( AstRegion *this, int * );
static void SetRegFS( AstRegion *, AstFrame *, int * );
/* Member functions. */
/* ================= */
AstRegion *astBestEllipse_( AstPointSet *mesh, double *cen, AstRegion *unc, int *status ){
/*
*+
* Name:
* astBestEllipse
* Purpose:
* Find the best fitting Ellipse through a given mesh of points.
* Type:
* Protected function.
* Synopsis:
* #include "ellipse.h"
* AstRegion *astBestEllipse( AstPointSet *mesh, double *cen, AstRegion *unc )
* Class Membership:
* Ellipse member function
* Description:
* This function finds the best fitting Ellipse through a given mesh of
* points. Ellispes are always 2-dimensional.
* Parameters:
* mesh
* Pointer to a PointSet holding the mesh of points. They are
* assumed to be in the Frame represented by "unc".
* cen
* Pointer to an array holding the coordinates of the new Ellipse
* centre.
* unc
* A Region representing the uncertainty associated with each point
* on the mesh.
* Returned Value:
* Pointer to the best fitting Ellipse. It will inherit the positional
* uncertainty and Frame represented by "unc".
* Implementation Deficiencies:
* - The method used by this function is not very accurate, and assumes
* that the supplied mesh provides uniform coverage of the entire ellipse.
* Notes:
* - A NULL pointer is returned if an error has already occurred, or if
* this function should fail for any reason.
*-
*/
/* Local Variables: */
AstFrame *frm;
AstPointSet *ps2;
AstRegion *result;
double **ptr2;
double **ptr;
double *ang;
double *dist;
double *px;
double *py;
double a0;
double a;
double aa[2];
double at;
double b;
double c0;
double c1;
double c2;
double c;
double d;
double den;
double e;
double f;
double mn;
double mx;
double p[2];
double pa[2];
double pb[2];
double r1;
double r2;
double r3;
double smn;
double t1;
double t2;
double t3;
int ip;
int maxat;
int np;
double sw;
/* Initialise */
result = NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* Get no. of points in the mesh. */
np = astGetNpoint( mesh );
/* Get pointers to the axis values. */
ptr = astGetPoints( mesh );
/* Allocate work space */
dist = astMalloc( sizeof( double )*(size_t) np );
ang = astMalloc( sizeof( double )*(size_t) np );
/* Get a pointer to the Frame represented by "unc". This is the Frame to
which the supplied mesh points refer. */
frm = astGetFrame( unc->frameset, AST__CURRENT );
/* Check pointers can be used safely */
if( astOK ) {
/* Find the first mesh point which is at a non-zero distance from the
centre. */
px = ptr[ 0 ];
py = ptr[ 1 ];
for( ip = 0; ip < np; ip++, px++, py++ ) {
p[ 0 ] = *px;
p[ 1 ] = *py;
dist[ ip ] = astDistance( frm, cen, p );
if( dist[ ip ] != AST__BAD && dist[ ip ] != 0.0 ) {
break;
} else {
ang[ ip ] = AST__BAD;
dist[ ip ] = AST__BAD;
}
}
/* Find a point which is this distance away from the centre along the second
axis. This point is used to define zero angle when calling astAngle
below. */
astOffset2( frm, cen, 0.0, dist[ ip ], aa );
ang[ ip ] = astAngle( frm, aa, cen, p );
/* Get the distance from the centre to each of the remaining mesh points. Also
find the orientation of the radial lines through the centre to each mesh
point. At the same time, find the index of the point with the largest
radial distance. */
maxat = ip;
r2 = dist[ maxat ];
ip++;
px++;
py++;
for( ; ip < np; ip++, px++, py++ ) {
p[ 0 ] = *px;
p[ 1 ] = *py;
dist[ ip ] = astDistance( frm, cen, p );
ang[ ip ] = astAngle( frm, aa, cen, p );
if( dist[ ip ] != AST__BAD && dist[ ip ] > r2 ) {
r2 = dist[ ip ];
maxat = ip;
}
}
/* Find the higher index neighbouring point, wrapping back to the start
of the list when the end is reached. Note the radius and position angle
at this neighbouring point. */
t2 = 0.0;
r3 = AST__BAD;
t3 = AST__BAD;
a0 = ang[ maxat ];
for( ip = maxat + 1; ip < np; ip++ ) {
if( dist[ ip ] != AST__BAD ) {
r3 = dist[ ip ];
t3 = palDrange( ang[ ip ] - a0 );
break;
}
}
if( r3 == AST__BAD ) {
for( ip = 0; ip < maxat; ip++ ) {
if( dist[ ip ] != AST__BAD ) {
r3 = dist[ ip ];
t3 = palDrange( ang[ ip ] - a0 );
break;
}
}
}
/* Find the lower index neighbouring point, wrapping back to the end
of the list when the start is reached. Note the radius and position angle
at this neighbouring point. */
r1 = AST__BAD;
t1 = AST__BAD;
for( ip = maxat - 1; ip > -1; ip-- ) {
if( dist[ ip ] != AST__BAD ) {
r1 = dist[ ip ];
t1 = palDrange( ang[ ip ] - a0 );
break;
}
}
if( r1 == AST__BAD ) {
for( ip = np - 1; ip > maxat; ip-- ) {
if( dist[ ip ] != AST__BAD ) {
r1 = dist[ ip ];
t1 = palDrange( ang[ ip ] - a0 );
break;
}
}
}
/* Fit a quadratic through the three pairs of (radius,angle) values. The
centre point (r2,t2) is the point which is furthest from the centre,
and the other two are the neighbouring points found above. */
a = r2 - r1;
b = t2 - t1;
c = t2*t2 - t1*t1;
d = r3 - r2;
e = t3 - t2;
f = t3*t3 - t2*t2;
den = c*e - b*f;
if( den != 0.0 ) {
/* The co-efficients of the interpolating polynomial... */
c1 = ( d*c - a*f )/den;
c2 = ( a*e - d*b )/den;
c0 = r1 - c1*t1 - c2*t1*t1;
/* Find the largest radius (the turning point of the quadratic), and the
angle at which it occurs. */
if( c2 < 0.0 ) {
mx = ( 4*c0*c2 - c1*c1 )/( 4*c2 );
at = a0 - c1/( 2*c2 );
} else {
mx = r2;
at = a0 - t2;
}
/* This point is the end of the ellipse primary axis. Find its (x,y)
coords, and store in "pa". */
astOffset2( frm, cen, at, mx, pa );
/* Resolve all the supplied points into components parallel and
perpendicular to the line joining the centre and "pa". */
ps2 = astResolvePoints( frm, cen, pa, mesh, NULL );
ptr2 = astGetPoints( ps2 );
if( astOK ) {
/* For each other mesh point, work out the length of the secondary
axis which would result if we used that point to define the ellipse.
Find the mean of these secondary axis lengths, weighted by the length
of the y component to reduce influence of poor conditioning at very
low y. */
smn = 0.0;
sw = 0.0;
px = ptr2[ 0 ];
py = ptr2[ 1 ];
for( ip = 0; ip < np; ip++, px++, py++ ) {
if( *px != AST__BAD && *py != AST__BAD ) {
den = mx*mx - (*px)*(*px);
if( den > 0.0 ) {
smn += fabs( mx*(*py)*(*py) )/sqrt( den );
sw += fabs( *py );
}
}
}
if( sw > 0 ) {
mn = smn/sw;
/* Find the coords at the end of the mean secondary axis. */
astOffset2( frm, cen, at + AST__DPIBY2, mn, pb );
/* Create the Ellipse to return. */
result = (AstRegion *) astEllipse( frm, 0, cen, pa, pb, unc, "", status );
}
}
/* Free resources. */
ps2 = astAnnul( ps2 );
}
}
dist = astFree( dist );
ang = astFree( ang );
frm = astAnnul( frm );
/* Return NULL if anything went wrong. */
if( !astOK ) result = astAnnul( result );
/* Return the result.*/
return result;
}
void astInitEllipseVtab_( AstEllipseVtab *vtab, const char *name, int *status ) {
/*
*+
* Name:
* astInitEllipseVtab
* Purpose:
* Initialise a virtual function table for a Ellipse.
* Type:
* Protected function.
* Synopsis:
* #include "ellipse.h"
* void astInitEllipseVtab( AstEllipseVtab *vtab, const char *name )
* Class Membership:
* Ellipse vtab initialiser.
* Description:
* This function initialises the component of a virtual function
* table which is used by the Ellipse class.
* Parameters:
* vtab
* Pointer to the virtual function table. The components used by
* all ancestral classes will be initialised if they have not already
* been initialised.
* name
* Pointer to a constant null-terminated character string which contains
* the name of the class to which the virtual function table belongs (it
* is this pointer value that will subsequently be returned by the Object
* astClass function).
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Pointer to thread-specific global data */
AstMappingVtab *mapping; /* Pointer to Mapping component of Vtab */
AstRegionVtab *region; /* Pointer to Region component of Vtab */
/* Check the local error status. */
if ( !astOK ) return;
/* Get a pointer to the thread specific global data structure. */
astGET_GLOBALS(NULL);
/* Initialize the component of the virtual function table used by the
parent class. */
astInitRegionVtab( (AstRegionVtab *) vtab, name );
/* Store a unique "magic" value in the virtual function table. This
will be used (by astIsAEllipse) to determine if an object belongs
to this class. We can conveniently use the address of the (static)
class_check variable to generate this unique value. */
vtab->id.check = &class_check;
vtab->id.parent = &(((AstRegionVtab *) vtab)->id);
/* Initialise member function pointers. */
/* ------------------------------------ */
/* Store pointers to the member functions (implemented here) that provide
virtual methods for this class. */
vtab->EllipsePars = EllipsePars;
/* Save the inherited pointers to methods that will be extended, and
replace them with pointers to the new member functions. */
mapping = (AstMappingVtab *) vtab;
region = (AstRegionVtab *) vtab;
parent_transform = mapping->Transform;
mapping->Transform = Transform;
parent_simplify = mapping->Simplify;
mapping->Simplify = Simplify;
parent_setregfs = region->SetRegFS;
region->SetRegFS = SetRegFS;
parent_resetcache = region->ResetCache;
region->ResetCache = ResetCache;
region->RegPins = RegPins;
region->RegBaseMesh = RegBaseMesh;
region->RegBaseBox = RegBaseBox;
region->RegCentre = RegCentre;
region->RegTrace = RegTrace;
/* Store replacement pointers for methods which will be over-ridden by
new member functions implemented here. */
/* Declare the copy constructor, destructor and class dump
functions. */
astSetDelete( vtab, Delete );
astSetCopy( vtab, Copy );
astSetDump( vtab, Dump, "Ellipse", "Elliptical region" );
/* If we have just initialised the vtab for the current class, indicate
that the vtab is now initialised, and store a pointer to the class
identifier in the base "object" level of the vtab. */
if( vtab == &class_vtab ) {
class_init = 1;
astSetVtabClassIdentifier( vtab, &(vtab->id) );
}
}
static void Cache( AstEllipse *this, int *status ){
/*
* Name:
* Cache
* Purpose:
* Calculate intermediate values and cache them in the Ellipse structure.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* void Cache( AstEllipse *this, int *status )
* Class Membership:
* Ellipse member function
* Description:
* This function uses the PointSet stored in the parent Region to calculate
* some intermediate values which are useful in other methods. These
* values are stored within the Ellipse structure.
* Parameters:
* this
* Pointer to the Ellipse.
* status
* Pointer to the inherited status variable.
*/
/* Local Variables: */
AstFrame *frm; /* Pointer to base Frame in Ellipse */
double **ptr; /* Pointer to data in the encapsulated PointSet */
double *centre; /* Array holding centre coords */
double *point1; /* Array holding coords at end of primary axis */
double *point2; /* Array holding coords at another point on ellipse */
double a; /* The half-length of the primary axis */
double angle; /* Orientation of primary axis */
double b; /* The half-length of the secondary axis */
int i; /* Axis index */
/* Check the global error status. */
if ( !astOK ) return;
/* Do Nothing if the cached information is up to date. */
if( this->stale ) {
/* Get a pointer to the base Frame. */
frm = astGetFrame( ((AstRegion *) this)->frameset, AST__BASE );
/* Allocate memory. */
centre = (double *) astMalloc( sizeof( double )*2 );
point1 = (double *) astMalloc( sizeof( double )*2 );
point2 = (double *) astMalloc( sizeof( double )*2 );
/* Get pointers to the coordinate data in the parent Region structure. */
ptr = astGetPoints( ((AstRegion *) this)->points );
/* Check pointers can be used safely. */
if( astOK ) {
/* Copy the points in to the allocated memory. */
for( i = 0; i < 2; i++ ) {
centre[ i ] = ptr[ i ][ 0 ];
point1[ i ] = ptr[ i ][ 1 ];
point2[ i ] = ptr[ i ][ 2 ];
}
/* Calculate the geometric parameters of the ellipse. */
CalcPars( frm, centre, point1, point2, &a, &b, &angle, status );
/* Check the returned values. */
if( a <= 0.0 || a == AST__BAD || b <= 0.0 || b == AST__BAD ) {
if( astOK ) astError( AST__BADIN, "astInitEllipse(%s): The "
"supplied points do not determine an "
"ellipse.", status, astGetClass( this ) );
}
/* Store useful things in the Ellipse structure. */
if( astOK ) {
astFree( this->centre );
this->centre = centre;
centre = NULL;
astFree( this->point1 );
this->point1 = point1;
point1 = NULL;
this->a = a;
this->b = b;
this->angle = angle;
}
}
/* Initialise the bounding box. This is set properly when the astRegBaseMesh
function is called. These variables should not be used unless the
"basemesh" component of the parent Region structure is set to a non-null
value. */
this->lbx = -DBL_MAX;
this->ubx = DBL_MAX;
this->lby = -DBL_MAX;
this->uby = DBL_MAX;
/* Free resources */
frm = astAnnul( frm );
if( centre ) centre = astFree( centre );
if( point1 ) point1 = astFree( point1 );
point2 = astFree( point2 );
/* Indicate cached information is up to date. */
this->stale = 0;
}
}
static void CalcPars( AstFrame *frm, double centre[2], double point1[2],
double point2[2], double *a, double *b,
double *angle, int *status ){
/*
* Name:
* CalcPars
* Purpose:
* Calculate ellipse parameters.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* void CalcPars( AstFrame *frm, double centre[2], double point1[2],
* double point2[2], double *a, double *b, double *angle,
* int *status )
* Class Membership:
* Ellipse member function
* Description:
* This function uses the supplied positions to calculate the
* geometric parameters of an ellipse.
* Parameters:
* frm
* Pointer to the Frame in which the positions are defined.
* centre;
* Array holding centre coords.
* point1
* Array holding coords at end of primary axis
* point2
* Array holding coords at another point on ellipse. On exit it
* holds the coords at the end of the secondary axis.
* a
* Pointer to location at which to store the half-length of the
* primary axis.
* b
* Pointer to location at which to store the half-length of the
* secondary axis.
* angle
* Pointer to location at which to store the angle from the
* positive direction of the second Frame axis to the primary
* ellipse axis, in radians. Rotation from the second to the first
* Frame axis is positive.
* status
* Pointer to the inherited status variable.
*/
/* Local Variables: */
double point3[ 2 ]; /* Array holding a point on the primary axis */
double x; /* The offset parallel to the primary axis */
double y; /* The offset perpendicular to the primary axis */
/* Check the global error status. */
if ( !astOK ) return;
/* Get the geodesic distance between the centre and point 1 (the end of
the primary axis of the ellipse). This is the half length of the
primary axis of the ellipse (the axis which joins the centre position to
point 1). */
*a = astDistance( frm, centre, point1 );
/* Find the point (point3) on the primary axis which is closest to point 2,
and thus get the geodesic offsets (resolved parallel and perpendicular to
the primary axis) between the centre and point 2. */
if( *a > 0.0 ) {
astResolve( frm, centre, point1, point2, point3, &x, &y );
/* Find the half-length of the secondary ellipse axis. */
if( astOK ) {
*b = (*a)*(*a) - x*x;
if( *b > 0.0 ) *b = (*a)*y/sqrt( *b );
} else {
*b = *a;
}
/* Find the angle from the positive direction of the second axis to the
primary ellipse axis. */
point3[ 0 ] = centre[ 0 ];
point3[ 1 ] = centre[ 1 ] + fabs( 0.1*(*a) );
*angle = astAngle( frm, point3, centre, point1 );
/* Find the end point of the secondary axis. */
(void) astOffset2( frm, centre, *angle + AST__DPIBY2, *b, point2 );
}
}
static void EllipsePars( AstEllipse *this, double centre[2], double *a,
double *b, double *angle, double p1[2],
double p2[2], int *status ){
/*
*++
* Name:
c astEllipsePars
f AST_ELLIPSEPARS
* Purpose:
* Returns the geometric parameters of an Ellipse.
* Type:
* Public virtual function.
* Synopsis:
c #include "ellipse.h"
c void astEllipsePars( AstEllipse *this, double centre[2], double *a,
c double *b, double *angle, double p1[2], double p2[2] )
f CALL AST_ELLIPSEPARS( THIS, CENTRE, A, B, ANGLE, P1, P2, STATUS )
* Class Membership:
* Region method.
* Description:
c This function
f This routine
* returns the geometric parameters describing the supplied ellipse.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Region.
c centre
f CENTRE( 2 ) = DOUBLE PRECISION (Returned)
* The coordinates of the Ellipse centre are returned in this arrays.
c a
f A = DOUBLE PRECISION (Returned)
* Returned holding the half-length of the first axis of the
* ellipse.
c b
f B = DOUBLE PRECISION (Returned)
* Returned holding the half-length of the second axis of the
* ellipse.
c angle
f ANGLE = DOUBLE PRECISION (Returned)
* If the coordinate system in which the Ellipse is defined has
* axes (X,Y), then
c "*angle"
f ANGLE
* is returned holding the angle from the positive direction of
* the Y axis to the first axis of the ellipse, in radians.
* Positive rotation is in the same sense as rotation from the
* positive direction of Y to the positive direction of X.
c p1
f P1( 2 ) = DOUBLE PRECISION (Returned)
* An array in which to return the coordinates at one of the two ends
* of the first axis of the ellipse.
c A NULL pointer can be supplied if these coordinates are not needed.
c p2
f P2( 2 ) = DOUBLE PRECISION (Returned)
* An array in which to return the coordinates at one of the two ends
* of the second axis of the ellipse.
c A NULL pointer can be supplied if these coordinates are not needed.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Notes:
* - If the coordinate system represented by the Ellipse has been
* changed since it was first created, the returned parameters refer
* to the new (changed) coordinate system, rather than the original
* coordinate system. Note however that if the transformation from
* original to new coordinate system is non-linear, the shape
* represented by the supplied Ellipse object may not be an accurate
* ellipse.
* - Values of AST__BAD are returned for the parameters without error
* if the ellipse is degenerate or undefined.
*--
*/
/* Local Variables: */
AstFrame *frm; /* Current Frame represented by the Ellipse */
AstPointSet *pset; /* PointSet holding PointList axis values */
AstRegion *this_region; /* Parent Region pointer */
double **ptr; /* Pointer to axes values in the PointList */
double *point1; /* Pointer to "p1" or "buf1" */
double *point2; /* Pointer to "p2" or "buf2" */
double buf1[2]; /* Local substitute array for "p1" */
double buf2[2]; /* Local substitute array for "p2" */
int i; /* Axis index */
/* Check the inherited status. */
if( !astOK ) return;
/* Store a pointer to the parent region structure. */
this_region = (AstRegion *) this;
/* Transform the base Frame axis values into the current Frame. */
pset = astTransform( this_region->frameset, this_region->points, 1, NULL );
/* Get pointers to the coordinate data. */
ptr = astGetPoints( pset );
/* Choose the arrays to use - supplied arrays if possible, local arrays
otherwise. */
if( p1 ) {
point1 = p1;
} else {
point1 = buf1;
}
if( p2 ) {
point2 = p2;
} else {
point2 = buf2;
}
/* Check pointers can be used safely. */
if( astOK ) {
/* Copy the points in to separate arrays. */
for( i = 0; i < 2; i++ ) {
centre[ i ] = ptr[ i ][ 0 ];
point1[ i ] = ptr[ i ][ 1 ];
point2[ i ] = ptr[ i ][ 2 ];
}
/* Get the Ellipse frame. */
frm = astGetFrame( this_region->frameset, AST__CURRENT );
/* Calculate the geometric parameters of the ellipse. */
CalcPars( frm, centre, point1, point2, a, b, angle, status );
/* Ensure no zero values are returned. */
if( *a <= 0.0 || *b <= 0.0 ) {
*a = AST__BAD;
*b = AST__BAD;
*angle = AST__BAD;
}
/* Free resources */
frm = astAnnul( frm );
}
pset = astAnnul( pset );
}
static void RegBaseBox( AstRegion *this_region, double *lbnd, double *ubnd, int *status ){
/*
* Name:
* RegBaseBox
* Purpose:
* Returns the bounding box of an un-negated Region in the base Frame of
* the encapsulated FrameSet.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* void RegBaseBox( AstRegion *this, double *lbnd, double *ubnd, int *status )
* Class Membership:
* Ellipse member function (over-rides the astRegBaseBox protected
* method inherited from the Region class).
* Description:
* This function returns the upper and lower axis bounds of a Region in
* the base Frame of the encapsulated FrameSet, assuming the Region
* has not been negated. That is, the value of the Negated attribute
* is ignored.
* Parameters:
* this
* Pointer to the Region.
* lbnd
* Pointer to an array in which to return the lower axis bounds
* covered by the Region in the base Frame of the encapsulated
* FrameSet. It should have at least as many elements as there are
* axes in the base Frame.
* ubnd
* Pointer to an array in which to return the upper axis bounds
* covered by the Region in the base Frame of the encapsulated
* FrameSet. It should have at least as many elements as there are
* axes in the base Frame.
* status
* Pointer to the inherited status variable.
*/
/* Local Variables: */
AstEllipse *this; /* Pointer to Ellipse structure */
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to the Ellipse structure */
this = (AstEllipse *) this_region;
/* The bounding box of the mesh returned by astRegBaseMesh is used as the
bounding box of the Ellipse. These bounds are cached in the Ellipse
structure by astRegBaseMesh. Ensure astRegBaseMesh has been invoked,
so that it is safe to use the cached bounding box. */
if( !this_region->basemesh ) (void) astAnnul( astRegBaseMesh( this ) );
/* Store the bounding box. */
lbnd[ 0 ] = this->lbx;
ubnd[ 0 ] = this->ubx;
lbnd[ 1 ] = this->lby;
ubnd[ 1 ] = this->uby;
}
static AstPointSet *RegBaseMesh( AstRegion *this_region, int *status ){
/*
* Name:
* RegBaseMesh
* Purpose:
* Return a PointSet containing a mesh of points on the boundary of a
* Region in its base Frame.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* AstPointSet *astRegBaseMesh( AstRegion *this, int *status )
* Class Membership:
* Ellipse member function (over-rides the astRegBaseMesh protected
* method inherited from the Region class).
* Description:
* This function returns a PointSet containing a mesh of points on the
* boundary of the Region. The points refer to the base Frame of
* the encapsulated FrameSet.
* Parameters:
* this
* Pointer to the Region.
* status
* Pointer to the inherited status variable.
* Returned Value:
* Pointer to the PointSet. Annul the pointer using astAnnul when it
* is no longer needed.
* Notes:
* - A NULL pointer is returned if an error has already occurred, or if
* this function should fail for any reason.
*/
/* Local Constants: */
#define NP_EDGE 50 /* No. of points for determining geodesic */
/* Local Variables: */
AstEllipse *this; /* The Ellipse structure */
AstFrame *frm; /* Base Frame in encapsulated FrameSet */
AstPointSet *result; /* Returned pointer */
AstRegion *reg; /* Copy of supplied Ellipse */
double **ptr; /* Pointers to data */
double ang; /* Position angular of primary axis at "dx" */
double angle; /* Ellipse parametric angle at point */
double delta; /* Angular separation of points */
double dist; /* Offset along an axis */
double dx; /* Primary axis offset */
double dy; /* Secondary axis offset */
double lbnd[2]; /* Lower bounding box bounds */
double lbx; /* Lower x bound of mesh bounding box */
double lby; /* Lower y bound of mesh bounding box */
double p2[ 2 ]; /* Position in 2D Frame */
double p[ 2 ]; /* Position in 2D Frame */
double ubnd[2]; /* Upper bounding box bounds */
double ubx; /* Upper x bound of mesh bounding box */
double uby; /* Upper y bound of mesh bounding box */
int i; /* Point index */
int np; /* No. of points in returned PointSet */
/* Initialise */
result= NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* If the Region structure contains a pointer to a PointSet holding
a previously created mesh, return it. */
if( this_region->basemesh ) {
result = astClone( this_region->basemesh );
/* Otherwise, create a new mesh. */
} else {
/* Initialise the bounding box of the mesh points. */
lbx = DBL_MAX;
ubx = -DBL_MAX;
lby = DBL_MAX;
uby = -DBL_MAX;
/* Get a pointer to the Ellipse structure. */
this = (AstEllipse *) this_region;
/* Ensure cached information is available. */
Cache( this, status );
/* Get a pointer to the base Frame in the encapsulated FrameSet. */
frm = astGetFrame( this_region->frameset, AST__BASE );
/* Get the requested number of points to put on the mesh. */
np = astGetMeshSize( this );
/* Store the angular increment between points. */
delta = 2*AST__DPI/np;
/* Create a suitable PointSet to hold the returned positions. */
result = astPointSet( np, 2, "", status );
ptr = astGetPoints( result );
if( astOK ) {
/* Loop round each point. The angle is the parametric angle, phi, where
the ellipse is defined by:
dx = a.cos( phi )
dy = a.sin( phi )
measured from the primary ellipse. Positive in the sense of rotation from
axis 2 to axis 1. */
angle = 0.0;
for( i = 0; i < np; i++ ) {
/* Find the offsets from the centre. "dx" is geodesic distance along the
primary axis, and dy is geodesic distance along the secondary axis. */
dx = this->a*cos( angle );
dy = this->b*sin( angle );
/* Now find the point which corresponds to this dx and dy, taking account
of the potential spherical geometry of hte coordinate system. First
move a distance "dx" from the centre along the primary axis. The
function value returned is the direction of the geodesic curve at the
end point. That is, the angle (in radians) between the positive direction
of the second axis and the continuation of the geodesic curve at the
requested end point. */
ang = astOffset2( frm, this->centre, this->angle, dx, p );
/* Now move a distance "dy" from the point found above at right angles to
the primary axis. */
astOffset2( frm, p, ang + AST__DPIBY2, dy, p2 );
/* Store the resulting axis values. */
ptr[ 0 ][ i ] = p2[ 0 ];
ptr[ 1 ][ i ] = p2[ 1 ];
/* Update the bounds of the mesh bounding box. The box is expressed in
terms of axis offsets from the centre, in order to avoid problems with
boxes that cross RA=0 or RA=12h */
if( p2[ 0 ] != AST__BAD && p2[ 1 ] != AST__BAD ){
dist = astAxDistance( frm, 1, this->centre[ 0 ], p2[ 0 ] );
if( dist < lbx ) {
lbx = dist;
} else if( dist > ubx ) {
ubx = dist;
}
dist = astAxDistance( frm, 1, this->centre[ 1 ], p2[ 1 ] );
if( dist < lby ) {
lby = dist;
} else if( dist > uby ) {
uby = dist;
}
}
/* Increment the angular position of the next mesh point. */
angle += delta;
}
}
/* Save the returned pointer in the Region structure so that it does not
need to be created again next time this function is called. Also cache
the bounding box in the Ellipse structure. */
if( astOK && result ) {
this_region->basemesh = astClone( result );
/* Extend the bounding box if it contains any singularies. The astNormBox
requires a Mapping which can be used to test points in the base Frame.
Create a copy of the Circle and then set its FrameSet so that the current
Frame in the copy is the same as the base Frame in the original. */
reg = astCopy( this );
astSetRegFS( reg, frm );
astSetNegated( reg, 0 );
/* Normalise this box. */
lbnd[ 0 ] = this->centre[ 0 ] + lbx;
lbnd[ 1 ] = this->centre[ 1 ] + lby;
ubnd[ 0 ] = this->centre[ 0 ] + ubx;
ubnd[ 1 ] = this->centre[ 1 ] + uby;
astNormBox( frm, lbnd, ubnd, reg );
/* Save this box */
this->lbx = lbnd[ 0 ];
this->ubx = ubnd[ 0 ];
this->lby = lbnd[ 1 ];
this->uby = ubnd[ 1 ];
/* Free resources. */
reg = astAnnul( reg );
}
frm = astAnnul( frm );
}
/* Annul the result if an error has occurred. */
if( !astOK ) result = astAnnul( result );
/* Return a pointer to the output PointSet. */
return result;
}
static double *RegCentre( AstRegion *this_region, double *cen, double **ptr,
int index, int ifrm, int *status ){
/*
* Name:
* RegCentre
* Purpose:
* Re-centre a Region.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* double *RegCentre( AstRegion *this, double *cen, double **ptr,
* int index, int ifrm, int *status )
* Class Membership:
* Ellipse member function (over-rides the astRegCentre protected
* method inherited from the Region class).
* Description:
* This function shifts the centre of the supplied Region to a
* specified position, or returns the current centre of the Region.
* Parameters:
* this
* Pointer to the Region.
* cen
* Pointer to an array of axis values, giving the new centre.
* Supply a NULL value for this in order to use "ptr" and "index" to
* specify the new centre.
* ptr
* Pointer to an array of points, one for each axis in the Region.
* Each pointer locates an array of axis values. This is the format
* returned by the PointSet method astGetPoints. Only used if "cen"
* is NULL.
* index
* The index of the point within the arrays identified by "ptr" at
* which is stored the coords for the new centre position. Only used
* if "cen" is NULL.
* ifrm
* Should be AST__BASE or AST__CURRENT. Indicates whether the centre
* position is supplied and returned in the base or current Frame of
* the FrameSet encapsulated within "this".
* status
* Pointer to the inherited status variable.
* Returned Value:
* If both "cen" and "ptr" are NULL then a pointer to a newly
* allocated dynamic array is returned which contains the centre
* coords of the Region. This array should be freed using astFree when
* no longer needed. If either of "ptr" or "cen" is not NULL, then a
* NULL pointer is returned.
* Notes:
* - Some Region sub-classes do not have a centre. Such classes will report
* an AST__INTER error code if this method is called.
*/
/* Local Variables: */
AstEllipse *this; /* Pointer to Ellipse structure */
AstFrame *frm; /* Base Frame */
double **rptr; /* Data pointers for Region PointSet */
double *bc; /* Base Frame centre position */
double *result; /* Returned pointer */
double *tmp; /* Temporary array pointer */
double a[ 2 ]; /* Original position */
double angle; /* Orietentation of offset from old to new centre */
double axval; /* Axis value */
double b[ 2 ]; /* New position */
double dist; /* Distance from old to new centre */
double newcen[ 2 ]; /* New centre */
int ic; /* Coordinate index */
int ip; /* Position index */
int ncb; /* Number of base frame coordinate values per point */
int ncc; /* Number of current frame coordinate values per point */
/* Initialise */
result = NULL;
/* Check the local error status. */
if ( !astOK ) return result;
/* Get a pointer to the Ellipse structure. */
this = (AstEllipse *) this_region;
/* Ensure cached information is available. */
Cache( this, status );
/* Get the number of axis values per point in the current Frame. */
ncc = astGetNout( this_region->frameset );
/* An ellipse always has 2 base frame axes. */
ncb = 2;
/* If the centre coords are to be returned, return either a copy of the
base Frame centre coords, or transform the base Frame centre coords
into the current Frame. */
if( !ptr && !cen ) {
if( ifrm == AST__CURRENT ) {
result = astRegTranPoint( this_region, this->centre, 1, 1 );
} else {
result = astStore( NULL, this->centre, sizeof( double )*ncb );
}
/* Otherwise, we store the supplied new centre coords and return a NULL
pointer. */
} else {
/* Get a pointer to the axis values stored in the Region structure. */
rptr = astGetPoints( this_region->points );
/* Check pointers can be used safely */
if( astOK ) {
/* If the centre position was supplied in the current Frame, find the
corresponding base Frame position... */
if( ifrm == AST__CURRENT ) {
if( cen ) {
bc = astRegTranPoint( this_region, cen, 1, 0 );
} else {
tmp = astMalloc( sizeof( double)*(size_t)ncc );
if( astOK ) {
for( ic = 0; ic < ncc; ic++ ) tmp[ ic ] = ptr[ ic ][ index ];
}
bc = astRegTranPoint( this_region, tmp, 1, 0 );
tmp = astFree( tmp );
}
/* Replace any bad centre values with their current values. */
for( ic = 0; ic < ncb; ic++ ) {
if( bc[ ic ] == AST__BAD ) bc[ ic ] = this->centre[ ic ];
}
/* If the centre position was supplied in the base Frame, use the
supplied "cen" or "ptr" pointer directly to change the coords in the
parent Region structure and the cached coords in the Ellipse structure. */
} else {
bc = newcen;
for( ic = 0; ic < ncb; ic++ ) {
axval = cen ? cen[ ic ] : ptr[ ic ][ index ];
newcen[ ic ] = ( axval != AST__BAD ) ? axval : this->centre[ ic ];
}
}
/* Find the direction and length of the offset between the old and new
centre. */
frm = astGetFrame( this_region->frameset, AST__BASE );
angle = astAxAngle( frm, this->centre, bc, 2 );
dist = astDistance( frm, this->centre, bc );
/* Shift each point in the parent Region structure by the same length and
direction. */
for( ip = 0; ip < 3; ip++ ) {
a[ 0 ] = rptr[ ip ][ 0 ];
a[ 1 ] = rptr[ ip ][ 1 ];
astOffset2( frm, a, angle, dist, b );
rptr[ ip ][ 0 ] = b[ 0 ];
rptr[ ip ][ 1 ] = b[ 1 ];
}
/* Indicate that the cache is stale. */
astResetCache( this );
/* Free resources */
frm = astAnnul( frm );
if( bc != newcen ) bc = astFree( bc );
}
}
/* Return the result. */
return result;
}
static int RegPins( AstRegion *this_region, AstPointSet *pset, AstRegion *unc,
int **mask, int *status ){
/*
* Name:
* RegPins
* Purpose:
* Check if a set of points fall on the boundary of a given Ellipse.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* int RegPins( AstRegion *this, AstPointSet *pset, AstRegion *unc,
* int **mask, int *status )
* Class Membership:
* Ellipse member function (over-rides the astRegPins protected
* method inherited from the Region class).
* Description:
* This function returns a flag indicating if the supplied set of
* points all fall on the boundary of the given Ellipse.
*
* Some tolerance is allowed, as specified by the uncertainty Region
* stored in the supplied Ellipse "this", and the supplied uncertainty
* Region "unc" which describes the uncertainty of the supplied points.
* Parameters:
* this
* Pointer to the Ellipse.
* pset
* Pointer to the PointSet. The points are assumed to refer to the
* base Frame of the FrameSet encapsulated by "this".
* unc
* Pointer to a Region representing the uncertainties in the points
* given by "pset". The Region is assumed to represent the base Frame
* of the FrameSet encapsulated by "this". Zero uncertainity is assumed
* if NULL is supplied.
* mask
* Pointer to location at which to return a pointer to a newly
* allocated dynamic array of ints. The number of elements in this
* array is equal to the value of the Npoint attribute of "pset".
* Each element in the returned array is set to 1 if the
* corresponding position in "pset" is on the boundary of the Region
* and is set to zero otherwise. A NULL value may be supplied
* in which case no array is created. If created, the array should
* be freed using astFree when no longer needed.
* status
* Pointer to the inherited status variable.
* Returned Value:
* Non-zero if the points all fall on the boundary of the given
* Region, to within the tolerance specified. Zero otherwise.
*/
/* Local variables: */
AstEllipse *large_ellipse; /* Ellipse slightly larger than "this" */
AstEllipse *small_ellipse; /* Ellipse slightly smaller than "this" */
AstEllipse *this; /* Pointer to the Ellipse structure. */
AstFrame *frm; /* Base Frame in supplied Ellipse */
AstPointSet *ps1; /* Points masked by larger Ellipse */
AstPointSet *ps2; /* Points masked by larger and smaller Ellipsees */
AstRegion *tunc; /* Uncertainity Region from "this" */
double **ptr; /* Pointer to axis values in "ps2" */
double *p; /* Pointer to next axis value */
double *safe; /* An interior point in "this" */
double drad; /* Radius increment corresponding to border width */
double l1; /* Length of bounding box diagonal */
double l2; /* Length of bounding box diagonal */
double lbnd_tunc[2]; /* Lower bounds of "this" uncertainty Region */
double lbnd_unc[2]; /* Lower bounds of supplied uncertainty Region */
double lim; /* Smallest semi-minor/major axis length */
double p1[2]; /* New ellipse axis lengths */
double ubnd_tunc[2]; /* Upper bounds of "this" uncertainty Region */
double ubnd_unc[2]; /* Upper bounds of supplied uncertainty Region */
int i; /* Axis index */
int j; /* Point index */
int np; /* No. of supplied points */
int result; /* Returned flag */
/* Initialise */
result = 0;
if( mask ) *mask = NULL;
/* Check the inherited status. */
if( !astOK ) return result;
/* Get a pointer to the Ellipse structure. */
this = (AstEllipse *) this_region;
/* Check the supplied PointSet has 2 axis values per point. */
if( astGetNcoord( pset ) != 2 && astOK ) {
astError( AST__INTER, "astRegPins(%s): Illegal number of axis "
"values per point (%d) in the supplied PointSet - should be "
"2 (internal AST programming error).", status, astGetClass( this ),
astGetNcoord( pset ) );
}
/* Get the number of axes in the uncertainty Region and check it is also 2. */
if( unc && astGetNaxes( unc ) != 2 && astOK ) {
astError( AST__INTER, "astRegPins(%s): Illegal number of axes (%d) "
"in the supplied uncertainty Region - should be 2 "
"(internal AST programming error).", status, astGetClass( this ),
astGetNaxes( unc ) );
}
/* Get the centre of the region in the base Frame. We use this as a "safe"
interior point within the region. */
safe = astRegCentre( this, NULL, NULL, 0, AST__BASE );
/* We now find the maximum distance on each axis that a point can be from the
boundary of the Ellipse for it still to be considered to be on the boundary.
First get the Region which defines the uncertainty within the Ellipse being
checked (in its base Frame), re-centre it on the interior point found
above (to avoid problems if the uncertainty region straddles a
discontinuity), and get its bounding box. */
tunc = astGetUncFrm( this, AST__BASE );
if( safe ) astRegCentre( tunc, safe, NULL, 0, AST__CURRENT );
astGetRegionBounds( tunc, lbnd_tunc, ubnd_tunc );
/* Find the geodesic length within the base Frame of "this" of the diagonal of
the bounding box. */
frm = astGetFrame( this_region->frameset, AST__BASE );
l1 = astDistance( frm, lbnd_tunc, ubnd_tunc );
/* Also get the Region which defines the uncertainty of the supplied
points and get its bounding box. First re-centre the uncertainty at the
interior position to avoid problems from uncertainties that straddle a
discontinuity. */
if( unc ) {
if( safe ) astRegCentre( unc, safe, NULL, 0, AST__CURRENT );
astGetRegionBounds( unc, lbnd_unc, ubnd_unc );
/* Find the geodesic length of the diagonal of this bounding box. */
l2 = astDistance( frm, lbnd_unc, ubnd_unc );
/* Assume zero uncertainty if no "unc" Region was supplied. */
} else {
l2 = 0.0;
}
/* Ensure cached information is available. */
Cache( this, status );
/* The required border width is half of the total diagonal of the two bounding
boxes. */
if( astOK ) {
drad = 0.5*( l1 + l2 );
/* Create two new Ellipse, one of which is larger than "this" by the amount
found above, and the other of which is smaller than "this" by the amount
found above. */
p1[ 0 ] = this->a + 0.5*drad;
p1[ 1 ] = this->b + 0.5*drad;
large_ellipse = astEllipse( frm, 1, this->centre, p1, &(this->angle),
NULL, " ", status );
p1[ 0 ] = this->a - 0.5*drad;
p1[ 1 ] = this->b - 0.5*drad;
lim = 1.0E-6*drad;
if( p1[ 0 ] < lim ) p1[ 0 ] = lim;
if( p1[ 1 ] < lim ) p1[ 1 ] = lim;
small_ellipse = astEllipse( frm, 1, this->centre, p1, &(this->angle),
NULL, " ", status );
/* Negate the smaller region.*/
astNegate( small_ellipse );
/* Points are on the boundary of "this" if they are inside both the large
Ellipse and the negated small Ellipse. First transform the supplied PointSet
using the large Ellipse, then transform them using the negated smaller
Ellipse. */
ps1 = astTransform( large_ellipse, pset, 1, NULL );
ps2 = astTransform( small_ellipse, ps1, 1, NULL );
/* Get a point to the resulting axis values, and the number of axis
values per axis. */
ptr = astGetPoints( ps2 );
np = astGetNpoint( ps2 );
/* If a mask array is to be returned, create one. */
if( mask ) {
*mask = astMalloc( sizeof(int)*(size_t) np );
/* Check all the resulting points, setting mask values for all of them. */
if( astOK ) {
/* Initialise the mask elements on the basis of the first axis values */
result = 1;
p = ptr[ 0 ];
for( j = 0; j < np; j++ ) {
if( *(p++) == AST__BAD ) {
result = 0;
(*mask)[ j ] = 0;
} else {
(*mask)[ j ] = 1;
}
}
/* Now check for bad values on other axes. */
for( i = 1; i < 2; i++ ) {
p = ptr[ i ];
for( j = 0; j < np; j++ ) {
if( *(p++) == AST__BAD ) {
result = 0;
(*mask)[ j ] = 0;
}
}
}
}
/* If no output mask is to be made, we can break out of the check as soon
as the first bad value is found. */
} else if( astOK ) {
result = 1;
for( i = 0; i < 2 && result; i++ ) {
p = ptr[ i ];
for( j = 0; j < np; j++ ) {
if( *(p++) == AST__BAD ) {
result = 0;
break;
}
}
}
}
/* Free resources. */
large_ellipse = astAnnul( large_ellipse );
small_ellipse = astAnnul( small_ellipse );
ps1 = astAnnul( ps1 );
ps2 = astAnnul( ps2 );
}
tunc = astAnnul( tunc );
frm = astAnnul( frm );
safe = astFree( safe );
/* If an error has occurred, return zero. */
if( !astOK ) {
result = 0;
if( mask ) *mask = astAnnul( *mask );
}
/* Return the result. */
return result;
}
static int RegTrace( AstRegion *this_region, int n, double *dist, double **ptr,
int *status ){
/*
*+
* Name:
* RegTrace
* Purpose:
* Return requested positions on the boundary of a 2D Region.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* int astTraceRegion( AstRegion *this, int n, double *dist, double **ptr );
* Class Membership:
* Ellipse member function (overrides the astTraceRegion method
* inherited from the parent Region class).
* Description:
* This function returns positions on the boundary of the supplied
* Region, if possible. The required positions are indicated by a
* supplied list of scalar parameter values in the range zero to one.
* Zero corresponds to some arbitrary starting point on the boundary,
* and one corresponds to the end (which for a closed region will be
* the same place as the start).
* Parameters:
* this
* Pointer to the Region.
* n
* The number of positions to return. If this is zero, the function
* returns without action (but the returned function value still
* indicates if the method is supported or not).
* dist
* Pointer to an array of "n" scalar parameter values in the range
* 0 to 1.0.
* ptr
* A pointer to an array of pointers. The number of elements in
* this array should equal tthe number of axes in the Frame spanned
* by the Region. Each element of the array should be a pointer to
* an array of "n" doubles, in which to return the "n" values for
* the corresponding axis. The contents of the arrays are unchanged
* if the supplied Region belongs to a class that does not
* implement this method.
* Returned Value:
* Non-zero if the astTraceRegion method is implemented by the class
* of Region supplied, and zero if not.
*-
*/
/* Local Variables; */
AstEllipse *this;
AstFrame *frm;
AstMapping *map;
AstPointSet *bpset;
AstPointSet *cpset;
double **bptr;
double ang;
double angle;
double dx;
double dy;
double p2[ 2 ];
double p[ 2 ];
int i;
int ncur;
/* Check inherited status, and the number of points to return, returning
a non-zero value to indicate that this class supports the astRegTrace
method. */
if( ! astOK || n == 0 ) return 1;
/* Get a pointer to the Ellipse structure. */
this = (AstEllipse *) this_region;
/* Ensure cached information is available. */
Cache( this, status );
/* Get a pointer to the base Frame in the encapsulated FrameSet. */
frm = astGetFrame( this_region->frameset, AST__BASE );
/* We first determine the required positions in the base Frame of the
Region, and then transform them into the current Frame. Get the
base->current Mapping, and the number of current Frame axes. */
map = astGetMapping( this_region->frameset, AST__BASE, AST__CURRENT );
/* If it's a UnitMap we do not need to do the transformation, so put the
base Frame positions directly into the supplied arrays. */
if( astIsAUnitMap( map ) ) {
bpset = NULL;
bptr = ptr;
ncur = 2;
/* Otherwise, create a PointSet to hold the base Frame positions (known
to be 2D since this is an ellipse). */
} else {
bpset = astPointSet( n, 2, " ", status );
bptr = astGetPoints( bpset );
ncur = astGetNout( map );
}
/* Check the pointers can be used safely. */
if( astOK ) {
/* Loop round each point. */
for( i = 0; i < n; i++ ) {
/* The supplied scalar parameter values are the parametric angles, phi,
where the ellipse is defined by:
dx = a.cos( phi )
dy = a.sin( phi )
measured from the primary ellipse. Positive in the sense of rotation from
axis 2 to axis 1. */
angle = dist[ i ]*2*AST__DPI;
/* Find the offsets from the centre. "dx" is geodesic distance along the
primary axis, and dy is geodesic distance along the secondary axis. */
dx = this->a*cos( angle );
dy = this->b*sin( angle );
/* Now find the point which corresponds to this dx and dy, taking account
of the potential spherical geometry of hte coordinate system. First
move a distance "dx" from the centre along the primary axis. The
function value returned is the direction of the geodesic curve at the
end point. That is, the angle (in radians) between the positive direction
of the second axis and the continuation of the geodesic curve at the
requested end point. */
ang = astOffset2( frm, this->centre, this->angle, dx, p );
/* Now move a distance "dy" from the point found above at right angles to
the primary axis. */
astOffset2( frm, p, ang + AST__DPIBY2, dy, p2 );
/* Store the resulting axis values. */
bptr[ 0 ][ i ] = p2[ 0 ];
bptr[ 1 ][ i ] = p2[ 1 ];
}
}
/* If required, transform the base frame positions into the current
Frame, storing them in the supplied array. Then free resources. */
if( bpset ) {
cpset = astPointSet( n, ncur, " ", status );
astSetPoints( cpset, ptr );
(void) astTransform( map, bpset, 1, cpset );
cpset = astAnnul( cpset );
bpset = astAnnul( bpset );
}
/* Free remaining resources. */
map = astAnnul( map );
frm = astAnnul( frm );
/* Return a non-zero value to indicate that this class supports the
astRegTrace method. */
return 1;
}
static void ResetCache( AstRegion *this, int *status ){
/*
* Name:
* ResetCache
* Purpose:
* Clear cached information within the supplied Region.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* void ResetCache( AstRegion *this, int *status )
* Class Membership:
* Region member function (overrides the astResetCache method
* inherited from the parent Region class).
* Description:
* This function clears cached information from the supplied Region
* structure.
* Parameters:
* this
* Pointer to the Region.
* status
* Pointer to the inherited status variable.
*/
if( this ) {
( (AstEllipse *) this )->stale = 1;
(*parent_resetcache)( this, status );
}
}
static void SetRegFS( AstRegion *this_region, AstFrame *frm, int *status ) {
/*
* Name:
* SetRegFS
* Purpose:
* Stores a new FrameSet in a Region
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* void SetRegFS( AstRegion *this_region, AstFrame *frm, int *status )
* Class Membership:
* Ellipse method (over-rides the astSetRegFS method inherited from
* the Region class).
* Description:
* This function creates a new FrameSet and stores it in the supplied
* Region. The new FrameSet contains two copies of the supplied
* Frame, connected by a UnitMap.
* Parameters:
* this
* Pointer to the Region.
* frm
* The Frame to use.
* status
* Pointer to the inherited status variable.
*/
/* Check the global error status. */
if ( !astOK ) return;
/* Invoke the parent method to store the FrameSet in the parent Region
structure. */
(* parent_setregfs)( this_region, frm, status );
/* Indicate that cached information will need to be re-calculated before
it is next used. */
astResetCache( this_region );
}
static AstMapping *Simplify( AstMapping *this_mapping, int *status ) {
/*
* Name:
* Simplify
* Purpose:
* Simplify the Mapping represented by a Region.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* AstMapping *Simplify( AstMapping *this, int *status )
* Class Membership:
* Ellipse method (over-rides the astSimplify method inherited
* from the Region class).
* Description:
* This function invokes the parent Region Simplify method, and then
* performs any further region-specific simplification.
*
* If the Mapping from base to current Frame is not a UnitMap, this
* will include attempting to fit a new Region to the boundary defined
* in the current Frame.
* Parameters:
* this
* Pointer to the original Region.
* status
* Pointer to the inherited status variable.
* Returned Value:
* A pointer to the simplified Region. A cloned pointer to the
* supplied Region will be returned if no simplication could be
* performed.
* Notes:
* - A NULL pointer value will be returned if this function is
* invoked with the AST error status set, or if it should fail for
* any reason.
*/
/* Local Variables: */
AstMapping *map; /* Base -> current Mapping */
AstMapping *result; /* Result pointer to return */
AstPointSet *mesh; /* Mesh of current Frame positions */
AstPointSet *ps2; /* Ellipse PointSet in current Frame */
AstRegion *new; /* Pointer to simplified Region */
AstRegion *newreg; /* Equivalent circle or ellipse */
AstRegion *this; /* Pointer to supplied Region structure */
AstRegion *unc; /* Pointer to uncertainty Region */
double **ptr2; /* Pointer axis values in "ps2" */
double *cen; /* Pointer to array holding new centre coords */
int ic; /* Axis index */
int nc; /* No. of axis values per point */
int ok; /* Was the new centre found OK? */
int simpler; /* Has some simplication taken place? */
/* Initialise. */
result = NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* Get a pointer to the supplied Region structure. */
this = (AstRegion *) this_mapping;
/* Invoke the parent Simplify method inherited from the Region class. This
will simplify the encapsulated FrameSet and uncertainty Region. */
new = (AstRegion *) (*parent_simplify)( this_mapping, status );
/* Note if any simplification took place. This is assumed to be the case
if the pointer returned by the above call is different to the supplied
pointer. */
simpler = ( new != this );
/* We attempt to simplify the Ellipse by re-defining it within its current
Frame. Transforming the Ellipse from its base to its current Frame may
result in the region no longer being an ellipse. We test this by
transforming a set of bounds on the Ellipse boundary. */
map = astGetMapping( new->frameset, AST__BASE, AST__CURRENT );
/* Get a mesh of points covering the Ellipse in its current Frame. */
mesh = astRegMesh( new );
/* Get the Region describing the positional uncertainty within the Ellipse in
its current Frame. */
unc = astGetUncFrm( new, AST__CURRENT );
/* Transform the PointSet holding the ellipse centre into the current
Frame, and copy the axis values into a new array. */
ps2 = astRegTransform( this, this->points, 1, NULL, NULL );
nc = astGetNcoord( ps2 );
cen = astMalloc( sizeof( double )*(size_t) nc );
ptr2 = astGetPoints( ps2 );
if( astOK ) {
ok = 1;
for( ic = 0; ic < nc; ic++ ) {
cen[ ic ] = ptr2[ ic ][ 0 ];
if( cen[ ic ] == AST__BAD ) ok = 0;
}
/* Find the best fitting Circle (defined in the current Frame) through these
points */
newreg = ok ? astBestCircle( mesh, cen, unc ) : NULL;
/* See if all points within this mesh fall on the boundary of the best
fitting Circle, to within the uncertainty of the Region. */
if( newreg && astRegPins( newreg, mesh, NULL, NULL ) ) {
/* If so, use the new Circle in place of the original Region. */
(void) astAnnul( new );
new = astClone( newreg );
simpler =1;
/* Otherwise, if the region is 2-d we see if an Ellipse can represent the
mesh. */
} else if( ok && nc == 2 ){
/* Find the best fitting Ellipse (defined in the current Frame) through these
points */
if( newreg ) (void) astAnnul( newreg );
newreg = astBestEllipse( mesh, cen, unc );
/* See if all points within this mesh fall on the boundary of the best
fitting Ellipse, to within the uncertainty of the Region. */
if( newreg && astRegPins( newreg, mesh, NULL, NULL ) ) {
/* If so, use the new Ellipse in place of the original Region. */
(void) astAnnul( new );
new = astClone( newreg );
simpler = 1;
}
}
/* Free resources. */
if( newreg ) newreg = astAnnul( newreg );
}
ps2 = astAnnul( ps2 );
cen = astFree( cen );
mesh = astAnnul( mesh );
unc = astAnnul( unc );
map = astAnnul( map );
/* If any simplification could be performed, copy Region attributes from
the supplied Region to the returned Region, and return a pointer to it.
If the supplied Region had no uncertainty, ensure the returned Region
has no uncertainty. Otherwise, return a clone of the supplied pointer. */
if( simpler ){
astRegOverlay( new, this, 1 );
result = (AstMapping *) new;
} else {
new = astAnnul( new );
result = astClone( this );
}
/* If an error occurred, annul the returned pointer. */
if ( !astOK ) result = astAnnul( result );
/* Return the result. */
return result;
}
static AstPointSet *Transform( AstMapping *this_mapping, AstPointSet *in,
int forward, AstPointSet *out, int *status ) {
/*
* Name:
* Transform
* Purpose:
* Apply a Ellipse to transform a set of points.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* AstPointSet *Transform( AstMapping *this, AstPointSet *in,
* int forward, AstPointSet *out, int *status )
* Class Membership:
* Ellipse member function (over-rides the astTransform protected
* method inherited from the Mapping class).
* Description:
* This function takes a Ellipse and a set of points encapsulated in a
* PointSet and transforms the points by setting axis values to
* AST__BAD for all points which are outside the region. Points inside
* the region are copied unchanged from input to output.
* Parameters:
* this
* Pointer to the Ellipse.
* in
* Pointer to the PointSet holding the input coordinate data.
* forward
* A non-zero value indicates that the forward coordinate transformation
* should be applied, while a zero value requests the inverse
* transformation.
* out
* Pointer to a PointSet which will hold the transformed (output)
* coordinate values. A NULL value may also be given, in which case a
* new PointSet will be created by this function.
* status
* Pointer to the inherited status variable.
* Returned Value:
* Pointer to the output (possibly new) PointSet.
* Notes:
* - The forward and inverse transformations are identical for a
* Region.
* - A null pointer will be returned if this function is invoked with the
* global error status set, or if it should fail for any reason.
* - The number of coordinate values per point in the input PointSet must
* match the number of axes in the Frame represented by the Ellipse.
* - If an output PointSet is supplied, it must have space for sufficient
* number of points and coordinate values per point to accommodate the
* result. Any excess space will be ignored.
*/
/* Local Variables: */
AstEllipse *this; /* Pointer to Ellipse */
AstFrame *frm; /* Pointer to base Frame in FrameSet */
AstPointSet *pset_res; /* Pointer to PointSet holding resolved components */
AstPointSet *pset_tmp; /* Pointer to PointSet holding base Frame positions*/
AstPointSet *result; /* Pointer to output PointSet */
double **ptr_out; /* Pointer to output coordinate data */
double **ptr_res; /* Pointer to resolved components coordinate data */
double *px; /* Pointer to array of primary axis components */
double *py; /* Pointer to array of secondary axis components */
double c1; /* Constant */
double c2; /* Constant */
double d; /* Elliptical distance to current point */
int closed; /* Is the boundary part of the Region? */
int coord; /* Zero-based index for coordinates */
int inside; /* Is the point inside the Region? */
int ncoord_out; /* No. of coordinates per output point */
int neg; /* Has the Region been negated? */
int npoint; /* No. of points */
int point; /* Loop counter for points */
/* Check the global error status. */
if ( !astOK ) return NULL;
/* Obtain a pointer to the Ellipse structure. */
this = (AstEllipse *) this_mapping;
/* Apply the parent mapping using the stored pointer to the Transform member
function inherited from the parent Region class. This function validates
all arguments and generates an output PointSet if necessary,
containing a copy of the input PointSet. */
result = (*parent_transform)( this_mapping, in, forward, out, status );
/* Ensure cached information is available. */
Cache( this, status );
/* We will now extend the parent astTransform method by performing the
calculations needed to generate the output coordinate values. */
/* First use the encapsulated FrameSet to transform the supplied positions
from the current Frame in the encapsulated FrameSet (the Frame
represented by the Region), to the base Frame (the Frame in which the
Region is defined). This call also returns a pointer to the base Frame
of the encapsulated FrameSet. Note, the returned pointer may be a
clone of the "in" pointer, and so we must be carefull not to modify the
contents of the returned PointSet. */
pset_tmp = astRegTransform( this, in, 0, NULL, &frm );
/* Resolve all the base Frame positions into components parallel to and
perpendicular to the primary axis, relative to the ellipse centre. The
components are returned in a new PointSet. */
pset_res = astResolvePoints( frm, this->centre, this->point1, pset_tmp, NULL );
/* Determine the numbers of points from the component PointSet and obtain
pointers for accessing the component and output coordinate values. */
npoint = astGetNpoint( pset_res );
ptr_res = astGetPoints( pset_res );
ncoord_out = astGetNcoord( result );
ptr_out = astGetPoints( result );
/* See if the boundary is part of the Region. */
closed = astGetClosed( this );
/* See if the Region has been negated. */
neg = astGetNegated( this );
/* Form some frequently needed constants. */
c1 = 1.0/(this->a*this->a);
c2 = 1.0/(this->b*this->b);
/* Perform coordinate arithmetic. */
/* ------------------------------ */
if ( astOK ) {
px = ptr_res[ 0 ];
py = ptr_res[ 1 ];
/* Loop round each point */
for ( point = 0; point < npoint; point++, px++, py++ ) {
/* Bad input points result in bad output points */
if( *px == AST__BAD || *py == AST__BAD ) {
inside = 0;
/* If the input points are good... */
} else {
/* Find the elliptical distance from the centre to the supplied point (the
ellipse circumference has an "elliptical distance" of 1.0 at all points).*/
d = c1*(*px)*(*px) + c2*(*py)*(*py);
/* Now consider whether this radius value puts the point in or out of the
Ellipse. */
if( d != AST__BAD ){
if( neg ) {
if( closed ) {
inside = ( d >= 1.0 );
} else {
inside = ( d > 1.0 );
}
} else {
if( closed ) {
inside = ( d <= 1.0 );
} else {
inside = ( d < 1.0 );
}
}
} else {
inside = 0;
}
}
/* If the point is outside, store bad output values. */
if( !inside ) {
for ( coord = 0; coord < ncoord_out; coord++ ) {
ptr_out[ coord ][ point ] = AST__BAD;
}
}
}
}
/* Free resources */
pset_tmp = astAnnul( pset_tmp );
pset_res = astAnnul( pset_res );
frm = astAnnul( frm );
/* Annul the result if an error has occurred. */
if( !astOK ) result = astAnnul( result );
/* Return a pointer to the output PointSet. */
return result;
}
/* Functions which access class attributes. */
/* ---------------------------------------- */
/* Implement member functions to access the attributes associated with
this class using the macros defined for this purpose in the
"object.h" file. For a description of each attribute, see the class
interface (in the associated .h file). */
/* Copy constructor. */
/* ----------------- */
static void Copy( const AstObject *objin, AstObject *objout, int *status ) {
/*
* Name:
* Copy
* Purpose:
* Copy constructor for Ellipse objects.
* Type:
* Private function.
* Synopsis:
* void Copy( const AstObject *objin, AstObject *objout, int *status )
* Description:
* This function implements the copy constructor for Ellipse objects.
* Parameters:
* objin
* Pointer to the object to be copied.
* objout
* Pointer to the object being constructed.
* status
* Pointer to the inherited status variable.
* Notes:
* - This constructor makes a deep copy.
*/
/* Local Variables: */
AstEllipse *in; /* Pointer to input Ellipse */
AstEllipse *out; /* Pointer to output Ellipse */
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain pointers to the input and output Ellipses. */
in = (AstEllipse *) objin;
out = (AstEllipse *) objout;
/* For safety, first clear any references to the input memory from
the output Ellipse. */
out->centre = NULL;
out->point1 = NULL;
/* Copy dynamic memory contents */
out->centre = astStore( NULL, in->centre, sizeof( double )*2 );
out->point1 = astStore( NULL, in->point1, sizeof( double )*2 );
}
/* Destructor. */
/* ----------- */
static void Delete( AstObject *obj, int *status ) {
/*
* Name:
* Delete
* Purpose:
* Destructor for Ellipse objects.
* Type:
* Private function.
* Synopsis:
* void Delete( AstObject *obj, int *status )
* Description:
* This function implements the destructor for Ellipse objects.
* Parameters:
* obj
* Pointer to the object to be deleted.
* status
* Pointer to the inherited status variable.
* Notes:
* This function attempts to execute even if the global error status is
* set.
*/
/* Local Variables: */
AstEllipse *this; /* Pointer to Ellipse */
/* Obtain a pointer to the Ellipse structure. */
this = (AstEllipse *) obj;
/* Annul all resources. */
this->centre = astFree( this->centre );
this->point1 = astFree( this->point1 );
}
/* Dump function. */
/* -------------- */
static void Dump( AstObject *this_object, AstChannel *channel, int *status ) {
/*
* Name:
* Dump
* Purpose:
* Dump function for Ellipse objects.
* Type:
* Private function.
* Synopsis:
* void Dump( AstObject *this, AstChannel *channel, int *status )
* Description:
* This function implements the Dump function which writes out data
* for the Ellipse class to an output Channel.
* Parameters:
* this
* Pointer to the Ellipse whose data are being written.
* channel
* Pointer to the Channel to which the data are being written.
* status
* Pointer to the inherited status variable.
*/
/* Local Variables: */
AstEllipse *this; /* Pointer to the Ellipse structure */
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain a pointer to the Ellipse structure. */
this = (AstEllipse *) this_object;
/* Write out values representing the instance variables for the
Ellipse class. Accompany these with appropriate comment strings,
possibly depending on the values being written.*/
/* In the case of attributes, we first use the appropriate (private)
Test... member function to see if they are set. If so, we then use
the (private) Get... function to obtain the value to be written
out.
For attributes which are not set, we use the astGet... method to
obtain the value instead. This will supply a default value
(possibly provided by a derived class which over-rides this method)
which is more useful to a human reader as it corresponds to the
actual default attribute value. Since "set" will be zero, these
values are for information only and will not be read back. */
/* There are no values to write, so return without further action. */
}
/* Standard class functions. */
/* ========================= */
/* Implement the astIsAEllipse and astCheckEllipse functions using the macros
defined for this purpose in the "object.h" header file. */
astMAKE_ISA(Ellipse,Region)
astMAKE_CHECK(Ellipse)
AstEllipse *astEllipse_( void *frame_void, int form, const double centre[2],
const double point1[2], const double point2[2],
AstRegion *unc, const char *options, int *status, ...) {
/*
*++
* Name:
c astEllipse
f AST_ELLIPSE
* Purpose:
* Create a Ellipse.
* Type:
* Public function.
* Synopsis:
c #include "ellipse.h"
c AstEllipse *astEllipse( AstFrame *frame, int form, const double centre[2],
c const double point1[2], const double point2[2],
c AstRegion *unc, const char *options, ... )
f RESULT = AST_ELLIPSE( FRAME, FORM, CENTRE, POINT1, POINT2, UNC, OPTIONS,
f STATUS )
* Class Membership:
* Ellipse constructor.
* Description:
* This function creates a new Ellipse and optionally initialises its
* attributes.
*
* A Ellipse is a Region which represents a elliptical area within the
* supplied 2-dimensional Frame.
* Parameters:
c frame
f FRAME = INTEGER (Given)
* A pointer to the Frame in which the region is defined. It must
* have exactly 2 axes. A deep copy is taken of the supplied Frame.
* This means that any subsequent changes made to the Frame using the
* supplied pointer will have no effect the Region.
c form
f FORM = INTEGER (Given)
* Indicates how the ellipse is described by the remaining parameters.
* A value of zero indicates that the ellipse is specified by a
* centre position and two positions on the circumference. A value of
* one indicates that the ellipse is specified by its centre position,
* the half-lengths of its two axes, and the orientation of its first
* axis.
c centre
f CENTRE( 2 ) = DOUBLE PRECISION (Given)
c An array of 2 doubles,
f An array
* containing the coordinates at the centre of
* the ellipse.
c point1
f POINT1( 2 ) = DOUBLE PRECISION (Given)
c An array of 2 doubles. If "form"
f If FORM
* is zero, this array should contain the coordinates of one of the four
* points where an axis of the ellipse crosses the circumference of the
* ellipse.
c If "form"
f If FORM
* is one, it should contain the lengths of semi-major and
* semi-minor axes of the ellipse, given as geodesic distances
* within the Frame.
c point2
f POINT2( 2 ) = DOUBLE PRECISION (Given)
c An array of 2 doubles. If "form"
f If FORM
* is zero, this array should containing the coordinates at some other
* point on the circumference of the ellipse, distinct from
c "point1". If "form"
f POINT1. If FORM
* is one, the first element of this array should hold the angle
* between the second axis of the Frame and the first ellipse axis
* (i.e. the ellipse axis which is specified first in the
c "point1"
f POINT1
* array), and the second element will be ignored. The angle should be
* given in radians, measured positive in the same sense as rotation
* from the positive direction of the second Frame axis to the positive
* direction of the first Frame axis.
c unc
f UNC = INTEGER (Given)
* An optional pointer to an existing Region which specifies the
* uncertainties associated with the boundary of the Ellipse being created.
* The uncertainty in any point on the boundary of the Ellipse is found by
* shifting the supplied "uncertainty" Region so that it is centred at
* the boundary point being considered. The area covered by the
* shifted uncertainty Region then represents the uncertainty in the
* boundary position. The uncertainty is assumed to be the same for
* all points.
*
* If supplied, the uncertainty Region must be of a class for which
* all instances are centro-symetric (e.g. Box, Circle, Ellipse, etc.)
* or be a Prism containing centro-symetric component Regions. A deep
* copy of the supplied Region will be taken, so subsequent changes to
* the uncertainty Region using the supplied pointer will have no
* effect on the created Ellipse. Alternatively,
f a null Object pointer (AST__NULL)
c a NULL Object pointer
* may be supplied, in which case a default uncertainty is used
* equivalent to a box 1.0E-6 of the size of the Ellipse being created.
*
* The uncertainty Region has two uses: 1) when the
c astOverlap
f AST_OVERLAP
* function compares two Regions for equality the uncertainty
* Region is used to determine the tolerance on the comparison, and 2)
* when a Region is mapped into a different coordinate system and
* subsequently simplified (using
c astSimplify),
f AST_SIMPLIFY),
* the uncertainties are used to determine if the transformed boundary
* can be accurately represented by a specific shape of Region.
c options
f OPTIONS = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated string containing an optional
c comma-separated list of attribute assignments to be used for
c initialising the new Ellipse. The syntax used is identical to
c that for the astSet function and may include "printf" format
c specifiers identified by "%" symbols in the normal way.
f A character string containing an optional comma-separated
f list of attribute assignments to be used for initialising the
f new Ellipse. The syntax used is identical to that for the
f AST_SET routine.
c ...
c If the "options" string contains "%" format specifiers, then
c an optional list of additional arguments may follow it in
c order to supply values to be substituted for these
c specifiers. The rules for supplying these are identical to
c those for the astSet function (and for the C "printf"
c function).
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astEllipse()
f AST_ELLIPSE = INTEGER
* A pointer to the new Ellipse.
* Notes:
* - A null Object pointer (AST__NULL) will be returned if this
c function is invoked with the AST error status set, or if it
f function is invoked with STATUS set to an error value, or if it
* should fail for any reason.
*--
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Pointer to thread-specific global data */
AstFrame *frame; /* Pointer to Frame structure */
AstEllipse *new; /* Pointer to new Ellipse */
va_list args; /* Variable argument list */
/* Get a pointer to the thread specific global data structure. */
astGET_GLOBALS(NULL);
/* Check the global status. */
if ( !astOK ) return NULL;
/* Obtain and validate a pointer to the supplied Frame structure. */
frame = astCheckFrame( frame_void );
/* Initialise the Ellipse, allocating memory and initialising the
virtual function table as well if necessary. */
new = astInitEllipse( NULL, sizeof( AstEllipse ), !class_init, &class_vtab,
"Ellipse", frame, form, centre, point1, point2, unc );
/* If successful, note that the virtual function table has been
initialised. */
if ( astOK ) {
class_init = 1;
/* Obtain the variable argument list and pass it along with the options string
to the astVSet method to initialise the new Ellipse's attributes. */
va_start( args, status );
astVSet( new, options, NULL, args );
va_end( args );
/* If an error occurred, clean up by deleting the new object. */
if ( !astOK ) new = astDelete( new );
}
/* Return a pointer to the new Ellipse. */
return new;
}
AstEllipse *astEllipseId_( void *frame_void, int form, const double centre[2],
const double point1[2], const double point2[2],
void *unc_void, const char *options, ... ) {
/*
* Name:
* astEllipseId_
* Purpose:
* Create a Ellipse.
* Type:
* Private function.
* Synopsis:
* #include "ellipse.h"
* AstEllipse *astEllipseId( void *frame_void, int form, const double centre[2],
* const double point1[2], const double point2[2],
* void *unc_void, const char *options, ..., int *status )
* Class Membership:
* Ellipse constructor.
* Description:
* This function implements the external (public) interface to the
* astEllipse constructor function. It returns an ID value (instead
* of a true C pointer) to external users, and must be provided
* because astEllipse_ has a variable argument list which cannot be
* encapsulated in a macro (where this conversion would otherwise
* occur).
*
* The variable argument list also prevents this function from
* invoking astEllipse_ directly, so it must be a re-implementation
* of it in all respects, except for the final conversion of the
* result to an ID value.
* Parameters:
* As for astEllipse_.
* status
* Pointer to the inherited status variable.
* Returned Value:
* The ID value associated with the new Ellipse.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Pointer to thread-specific global data */
AstFrame *frame; /* Pointer to Frame structure */
AstEllipse *new; /* Pointer to new Ellipse */
AstRegion *unc; /* Pointer to Region structure */
va_list args; /* Variable argument list */
int *status; /* Get a pointer to the thread specific global data structure. */
astGET_GLOBALS(NULL);
/* Pointer to inherited status value */
/* Get a pointer to the inherited status value. */
status = astGetStatusPtr;
/* Check the global status. */
if ( !astOK ) return NULL;
/* Obtain a Frame pointer from the supplied ID and validate the
pointer to ensure it identifies a valid Frame. */
frame = astVerifyFrame( astMakePointer( frame_void ) );
/* Obtain a Region pointer from the supplied "unc" ID and validate the
pointer to ensure it identifies a valid Region . */
unc = unc_void ? astCheckRegion( astMakePointer( unc_void ) ) : NULL;
/* Initialise the Ellipse, allocating memory and initialising the
virtual function table as well if necessary. */
new = astInitEllipse( NULL, sizeof( AstEllipse ), !class_init, &class_vtab,
"Ellipse", frame, form, centre, point1, point2, unc );
/* If successful, note that the virtual function table has been
initialised. */
if ( astOK ) {
class_init = 1;
/* Obtain the variable argument list and pass it along with the options string
to the astVSet method to initialise the new Ellipse's attributes. */
va_start( args, options );
astVSet( new, options, NULL, args );
va_end( args );
/* If an error occurred, clean up by deleting the new object. */
if ( !astOK ) new = astDelete( new );
}
/* Return an ID value for the new Ellipse. */
return astMakeId( new );
}
AstEllipse *astInitEllipse_( void *mem, size_t size, int init, AstEllipseVtab *vtab,
const char *name, AstFrame *frame, int form,
const double centre[2], const double point1[2],
const double point2[2], AstRegion *unc, int *status ){
/*
*+
* Name:
* astInitEllipse
* Purpose:
* Initialise a Ellipse.
* Type:
* Protected function.
* Synopsis:
* #include "ellipse.h"
* AstEllipse *astInitEllipse( void *mem, size_t size, int init,
* AstEllipseVtab *vtab, const char *name,
* AstFrame *frame, const double centre[2],
* const double point1[2], const double point2[2],
* AstRegion *unc )
* Class Membership:
* Ellipse initialiser.
* Description:
* This function is provided for use by class implementations to initialise
* a new Ellipse object. It allocates memory (if necessary) to accommodate
* the Ellipse plus any additional data associated with the derived class.
* It then initialises a Ellipse structure at the start of this memory. If
* the "init" flag is set, it also initialises the contents of a virtual
* function table for a Ellipse at the start of the memory passed via the
* "vtab" parameter.
* Parameters:
* mem
* A pointer to the memory in which the Ellipse is to be initialised.
* This must be of sufficient size to accommodate the Ellipse data
* (sizeof(Ellipse)) plus any data used by the derived class. If a value
* of NULL is given, this function will allocate the memory itself using
* the "size" parameter to determine its size.
* size
* The amount of memory used by the Ellipse (plus derived class data).
* This will be used to allocate memory if a value of NULL is given for
* the "mem" parameter. This value is also stored in the Ellipse
* structure, so a valid value must be supplied even if not required for
* allocating memory.
* init
* A logical flag indicating if the Ellipse's virtual function table is
* to be initialised. If this value is non-zero, the virtual function
* table will be initialised by this function.
* vtab
* Pointer to the start of the virtual function table to be associated
* with the new Ellipse.
* name
* Pointer to a constant null-terminated character string which contains
* the name of the class to which the new object belongs (it is this
* pointer value that will subsequently be returned by the astGetClass
* method).
* frame
* A pointer to the Frame in which the region is defined.
* form
* Indicates how the "point" parameter should be interpreted.
* Should be either 0 or 1.
* centre
* An array of double, with one element for each Frame axis (Naxes
* attribute) containing the coordinates of the ellipse centre.
* point1
* An array of double, with one element for each Frame axis (Naxes
* attribute). If "form" is zero, it should contain the coordinates at
* the end of one of the axes of the ellipse. If "form" is one, it
* should contain the semi-major and semi-minor axes of the ellipse.
* point2
* An array of double, with one element for each Frame axis (Naxes
* attribute). If "form" is zero, it should contain the coordinates at
* some other point on the circumference of the ellipse. If "form" is
* one, element [1] is ignored and element [0] should contain the
* angle from the second frame axis to the first ellipse axis, given in
* radians, measured positive in the same sense as rotation from the
* positive direction of the second Frame axis to the positive
* direction of the first Frame axis. The "first" ellipse axis is
* whichever of the semi-major or semi-minor axis is specified first in
* the "point1" array.
* unc
* A pointer to a Region which specifies the uncertainty in the
* supplied positions (all points on the boundary of the new Ellipse
* being initialised are assumed to have the same uncertainty). A NULL
* pointer can be supplied, in which case default uncertainties equal to
* 1.0E-6 of the dimensions of the new Ellipse's bounding box are used.
* If an uncertainty Region is supplied, it must be either a Box, a
* Circle or an Ellipse, and its encapsulated Frame must be related
* to the Frame supplied for parameter "frame" (i.e. astConvert
* should be able to find a Mapping between them). Two positions
* the "frame" Frame are considered to be co-incident if their
* uncertainty Regions overlap. The centre of the supplied
* uncertainty Region is immaterial since it will be re-centred on the
* point being tested before use. A deep copy is taken of the supplied
* Region.
* Returned Value:
* A pointer to the new Ellipse.
* Notes:
* - A null pointer will be returned if this function is invoked with the
* global error status set, or if it should fail for any reason.
*-
*/
/* Local Variables: */
AstEllipse *new; /* Pointer to new Ellipse */
AstPointSet *pset; /* PointSet to pass to Region initialiser */
double **ptr; /* Pointer to coords data in pset */
const double *p1; /* Pointer to circumference point 1 */
const double *p2; /* Pointer to circumference point 2 */
int i; /* axis index */
int nc; /* No. of axes */
/* Check the global status. */
if ( !astOK ) return NULL;
/* If necessary, initialise the virtual function table. */
if ( init ) astInitEllipseVtab( vtab, name );
/* Initialise. */
new = NULL;
/* Check the supplied value for "form" is legal. */
if( form != 0 && form != 1 && astOK ) {
astError( AST__BADIN, "astInitEllipse(%s): The value supplied for "
"parameter \"form\" (%d) is illegal - it should be 0 or 1 "
"(programming error).", status, name, form );
}
/* Get the number of axis values required for each position. */
nc = astGetNaxes( frame );
/* Report an error if the Frame is not 2-dimensional. */
if( nc != 2 ) {
astError( AST__BADIN, "astInitEllipse(%s): The supplied %s has %d "
"axes - ellipses must have exactly 2 axes.", status, name,
astGetClass( frame ), nc );
}
/* If the ellipse is specified by axis lengths and orientation, find two
points on the circumference (ends of the two ellipse axes). */
if( form == 1 ) {
p1 = astMalloc( sizeof( double )*2 );
p2 = astMalloc( sizeof( double )*2 );
if( astOK ) {
astOffset2( frame, centre, *point2, point1[ 0 ], (double *) p1 );
astOffset2( frame, centre, *point2 + AST__DPIBY2, point1[ 1 ],
(double *) p2 );
}
/* If the ellipse is specified by two points on the circumference, use
them. */
} else {
p1 = point1;
p2 = point2;
}
/* Create a PointSet to hold the supplied values, and get points to the
data arrays. */
pset = astPointSet( 3, nc, " ", status );
ptr = astGetPoints( pset );
/* Copy the supplied coordinates into the PointSet, checking that no bad
values have been supplied. */
for( i = 0; astOK && i < nc; i++ ) {
if( centre[ i ] == AST__BAD ) {
astError( AST__BADIN, "astInitEllipse(%s): The value of axis %d is "
"undefined at the ellipse centre.", status, name, i + 1 );
}
if( astOK && p1[ i ] == AST__BAD ) {
astError( AST__BADIN, "astInitEllipse(%s): The value of axis %d is "
"undefined at point 1 on the circumference of "
"the ellipse.", status, name, i + 1 );
}
if( astOK && p2[ i ] == AST__BAD ) {
astError( AST__BADIN, "astInitEllipse(%s): The value of axis %d is "
"undefined at point 2 on the circumference of "
"the ellipse.", status, name, i + 1 );
}
ptr[ i ][ 0 ] = centre[ i ];
ptr[ i ][ 1 ] = p1[ i ];
ptr[ i ][ 2 ] = p2[ i ];
}
/* Check pointers can be used safely. */
if( astOK ) {
/* Initialise a Region structure (the parent class) as the first component
within the Ellipse structure, allocating memory if necessary. */
new = (AstEllipse *) astInitRegion( mem, size, 0, (AstRegionVtab *) vtab,
name, frame, pset, unc );
if ( astOK ) {
/* Initialise the Ellipse data. */
/* ------------------------ */
new->stale = 1;
/* If an error occurred, clean up by deleting the new Ellipse. */
if ( !astOK ) new = astDelete( new );
}
}
/* Free resources. */
pset = astAnnul( pset );
if( form == 1 ) {
p1 = astFree( (void *) p1 );
p2 = astFree( (void *) p2 );
}
/* Return a pointer to the new Ellipse. */
return new;
}
AstEllipse *astLoadEllipse_( void *mem, size_t size, AstEllipseVtab *vtab,
const char *name, AstChannel *channel, int *status ) {
/*
*+
* Name:
* astLoadEllipse
* Purpose:
* Load a Ellipse.
* Type:
* Protected function.
* Synopsis:
* #include "ellipse.h"
* AstEllipse *astLoadEllipse( void *mem, size_t size, AstEllipseVtab *vtab,
* const char *name, AstChannel *channel )
* Class Membership:
* Ellipse loader.
* Description:
* This function is provided to load a new Ellipse using data read
* from a Channel. It first loads the data used by the parent class
* (which allocates memory if necessary) and then initialises a
* Ellipse structure in this memory, using data read from the input
* Channel.
*
* If the "init" flag is set, it also initialises the contents of a
* virtual function table for a Ellipse at the start of the memory
* passed via the "vtab" parameter.
* Parameters:
* mem
* A pointer to the memory into which the Ellipse is to be
* loaded. This must be of sufficient size to accommodate the
* Ellipse data (sizeof(Ellipse)) plus any data used by derived
* classes. If a value of NULL is given, this function will
* allocate the memory itself using the "size" parameter to
* determine its size.
* size
* The amount of memory used by the Ellipse (plus derived class
* data). This will be used to allocate memory if a value of
* NULL is given for the "mem" parameter. This value is also
* stored in the Ellipse structure, so a valid value must be
* supplied even if not required for allocating memory.
*
* If the "vtab" parameter is NULL, the "size" value is ignored
* and sizeof(AstEllipse) is used instead.
* vtab
* Pointer to the start of the virtual function table to be
* associated with the new Ellipse. If this is NULL, a pointer
* to the (static) virtual function table for the Ellipse class
* is used instead.
* name
* Pointer to a constant null-terminated character string which
* contains the name of the class to which the new object
* belongs (it is this pointer value that will subsequently be
* returned by the astGetClass method).
*
* If the "vtab" parameter is NULL, the "name" value is ignored
* and a pointer to the string "Ellipse" is used instead.
* Returned Value:
* A pointer to the new Ellipse.
* Notes:
* - A null pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Pointer to thread-specific global data */
AstEllipse *new; /* Pointer to the new Ellipse */
/* Initialise. */
new = NULL;
/* Check the global error status. */
if ( !astOK ) return new;
/* Get a pointer to the thread specific global data structure. */
astGET_GLOBALS(channel);
/* If a NULL virtual function table has been supplied, then this is
the first loader to be invoked for this Ellipse. In this case the
Ellipse belongs to this class, so supply appropriate values to be
passed to the parent class loader (and its parent, etc.). */
if ( !vtab ) {
size = sizeof( AstEllipse );
vtab = &class_vtab;
name = "Ellipse";
/* If required, initialise the virtual function table for this class. */
if ( !class_init ) {
astInitEllipseVtab( vtab, name );
class_init = 1;
}
}
/* Invoke the parent class loader to load data for all the ancestral
classes of the current one, returning a pointer to the resulting
partly-built Ellipse. */
new = astLoadRegion( mem, size, (AstRegionVtab *) vtab, name,
channel );
if ( astOK ) {
/* Read input data. */
/* ================ */
/* Request the input Channel to read all the input data appropriate to
this class into the internal "values list". */
astReadClassData( channel, "Ellipse" );
/* Now read each individual data item from this list and use it to
initialise the appropriate instance variable(s) for this class. */
/* In the case of attributes, we first read the "raw" input value,
supplying the "unset" value as the default. If a "set" value is
obtained, we then use the appropriate (private) Set... member
function to validate and set the value properly. */
/* There are no values to read. */
/* ---------------------------- */
/* Indicate that no cache intermediate results are yet available in the
Ellipse structure */
new->stale = 1;
/* If an error occurred, clean up by deleting the new Ellipse. */
if ( !astOK ) new = astDelete( new );
}
/* Return the new Ellipse pointer. */
return new;
}
/* Virtual function interfaces. */
/* ============================ */
/* These provide the external interface to the virtual functions defined by
this class. Each simply checks the global error status and then locates and
executes the appropriate member function, using the function pointer stored
in the object's virtual function table (this pointer is located using the
astMEMBER macro defined in "object.h").
Note that the member function may not be the one defined here, as it may
have been over-ridden by a derived class. However, it should still have the
same interface. */
void astEllipsePars_( AstEllipse *this, double centre[2], double *a,
double *b, double *angle, double p1[2], double p2[2],
int *status ){
if ( !astOK ) return;
(**astMEMBER(this,Ellipse,EllipsePars))( this, centre, a, b,
angle, p1, p2, status );
}
|