1 /*
2 *class++
3 * Name:
4 * CmpMap
5
6 * Purpose:
7 * Compound Mapping.
8
9 * Constructor Function:
10 c astCmpMap
11 f AST_CMPMAP
12
13 * Description:
14 * A CmpMap is a compound Mapping which allows two component
15 * Mappings (of any class) to be connected together to form a more
16 * complex Mapping. This connection may either be "in series"
17 * (where the first Mapping is used to transform the coordinates of
18 * each point and the second mapping is then applied to the
19 * result), or "in parallel" (where one Mapping transforms the
20 * earlier coordinates for each point and the second Mapping
21 * simultaneously transforms the later coordinates).
22 *
23 * Since a CmpMap is itself a Mapping, it can be used as a
24 * component in forming further CmpMaps. Mappings of arbitrary
25 * complexity may be built from simple individual Mappings in this
26 * way.
27
28 * Inheritance:
29 * The CmpMap class inherits from the Mapping class.
30
31 * Attributes:
32 * The CmpMap class does not define any new attributes beyond those
33 * which are applicable to all Mappings.
34
35 * Functions:
36 c The CmpMap class does not define any new functions beyond those
37 f The CmpMap class does not define any new routines beyond those
38 * which are applicable to all Mappings.
39
40 * Copyright:
41 * Copyright (C) 1997-2006 Council for the Central Laboratory of the
42 * Research Councils
43
44 * Licence:
45 * This program is free software: you can redistribute it and/or
46 * modify it under the terms of the GNU Lesser General Public
47 * License as published by the Free Software Foundation, either
48 * version 3 of the License, or (at your option) any later
49 * version.
50 *
51 * This program is distributed in the hope that it will be useful,
52 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 * GNU Lesser General Public License for more details.
55 *
56 * You should have received a copy of the GNU Lesser General
57 * License along with this program. If not, see
58 * <http://www.gnu.org/licenses/>.
59
60 * Authors:
61 * RFWS: R.F. Warren-Smith (Starlink)
62
63 * History:
64 * 1-FEB-1996 (RFWS):
65 * Original version.
66 * 25-SEP-1996 (RFWS):
67 * Implemented external interface and I/O facilities.
68 * 12-DEC-1996 (RFWS):
69 * Over-ride the astMapList method.
70 * 13-DEC-1996 (RFWS):
71 * Over-ride the astSimplify method.
72 * 4-JUN-1997 (RFWS):
73 * Eliminate any simplification when MapList is used. Instead,
74 * over-ride the MapMerge method and implement all
75 * simplification in this.
76 * 24-MAR-1998 (RFWS):
77 * Fixed bug in testing of simplified invert flag in Simplify.
78 * 15-APR-1998 (RFWS):
79 * Improved the MapMerge method to allow parallel combinations
80 * of series CmpMaps to be replaced by series combinations of
81 * parallel CmpMaps, and vice versa.
82 * 26-SEP-2001 (DSB):
83 * Over-ride the astDecompose method.
84 * 8-JAN-2003 (DSB):
85 * - Changed private InitVtab method to protected astInitCmpMapVtab
86 * method.
87 * 8-JAN-2003 (DSB):
88 * - Modified MapMerge so that a parallel CmpMap can swap with a
89 * suitable PermMap lower neighbour.
90 * 23-APR-2004 (DSB):
91 * - Modified Simplify to avoid infinite loops.
92 * 27-APR-2004 (DSB):
93 * - Correction to MapMerge to prevent segvio if CmpMap and PermMap
94 * cannot be swapped.
95 * 4-OCT-2004 (DSB):
96 * Modify astMapList to return flag indicating presence of inverted
97 * CmpMaps in supplied Mapping.
98 * 20-APR-2005 (DSB):
99 * Modify MapMerge so that it will attempt to merge the first
100 * and second CmpMaps in a list of series CmpMaps.
101 * 8-FEB-2006 (DSB):
102 * Corrected logic within MapMerge for cases where a PermMap is
103 * followed by a parallel CmpMap.
104 * 14-FEB-2006 (DSB):
105 * Override astGetObjSize.
106 * 14-MAR-2006 (DSB):
107 * - When checking for patterns in the simplification process,
108 * require at least 30 samples in the waveform for evidence of a
109 * pattern.
110 * - Override astEqual.
111 * - The constructor no longer reports an error if the resulting
112 * CmpMap cannot transform points in either direction. This is
113 * because it may be possible to simplify such a CmpMap and the
114 * simplified Mapping may have defined transformations. E.g. if a
115 * Mapping which has only a forward transformation is combined in
116 * series with its own inverse, the combination will simplify to a
117 * UnitMap (usually).
118 * 9-MAY-2006 (DSB):
119 * - In Simplify, remove checks for patterns in the number of atomic
120 * mappings when calling astSimplify recursively.
121 * 23-AUG-2006 (DSB):
122 * - In Simplify, add checks for re-appearance of a Mapping that is
123 * already being simplified at a higher levelin the call stack.
124 * 18-APR-2007 (DSB):
125 * In Simplify: if the returned Mapping is not a CmpMap, always copy
126 * the returned component Mapping (rather than cloning it) so that
127 * the returned Mapping is not affected if user code subsequently
128 * inverts the component Mapping via some other pointer.
129 * 12-MAR-2008 (DSB):
130 * Modify MapSplit so that attempts to split the inverse
131 * transformation if it cannot split the forward transformation.
132 * 30-JUL-2009 (DSB):
133 * Ensure the PermMap has equal number of inputs and outputs when
134 * swapping a PermMap and a CmpMap in astMapMerge.
135 * 3-JAN-2011 (DSB):
136 * In MapSplit, certain classes of Mapping (e.g. PermMaps) can
137 * produce a returned Mapping with zero outputs. Consider such
138 * Mappings to be unsplitable.
139 * 11-JAN-2011 (DSB):
140 * Improve simplification of serial combinations of parellel CmpMaps.
141 * 25-JAN-2011 (DSB):
142 * Big improvement to the efficiency of the astMapSplit method.
143 * 24-JAN-2012 (DSB):
144 * If efficient MapSplit fails to split (e.g. due to the presence
145 * of PermMaps), then revert to the older slower method.
146 * 5-FEB-2013 (DSB):
147 * Take account of Invert flags when combining parallel CmpMaps in
148 * series.
149 * 29-APR-2013 (DSB):
150 * In MapList, use the astDoNotSimplify method to check that it is
151 * OK to expand the CmpMap.
152 *class--
153 */
154
155 /* Module Macros. */
156 /* ============== */
157 /* Set the name of the class we are implementing. This indicates to
158 the header files that define class interfaces that they should make
159 "protected" symbols available. */
160 #define astCLASS CmpMap
161
162 /* Include files. */
163 /* ============== */
164 /* Interface definitions. */
165 /* ---------------------- */
166
167 #include "error.h" /* Error reporting facilities */
168 #include "memory.h" /* Memory allocation facilities */
169 #include "object.h" /* Base Object class */
170 #include "pointset.h" /* Sets of points/coordinates */
171 #include "mapping.h" /* Coordinate Mappings (parent class) */
172 #include "channel.h" /* I/O channels */
173 #include "permmap.h" /* Coordinate permutation Mappings */
174 #include "unitmap.h" /* Unit transformations */
175 #include "cmpmap.h" /* Interface definition for this class */
176 #include "frameset.h" /* Interface definition for FrameSets */
177 #include "globals.h" /* Thread-safe global data access */
178
179 /* Error code definitions. */
180 /* ----------------------- */
181 #include "ast_err.h" /* AST error codes */
182
183 /* C header files. */
184 /* --------------- */
185 #include <stdarg.h>
186 #include <stddef.h>
187 #include <string.h>
188 #include <stdio.h>
189
190 /* Module Variables. */
191 /* ================= */
192
193 /* Address of this static variable is used as a unique identifier for
194 member of this class. */
195 static int class_check;
196
197 /* Pointers to parent class methods which are extended by this class. */
198 static int (* parent_getobjsize)( AstObject *, int * );
199 static AstPointSet *(* parent_transform)( AstMapping *, AstPointSet *, int, AstPointSet *, int * );
200 static int (* parent_maplist)( AstMapping *, int, int, int *, AstMapping ***, int **, int * );
201 static int *(* parent_mapsplit)( AstMapping *, int, const int *, AstMapping **, int * );
202
203 #if defined(THREAD_SAFE)
204 static int (* parent_managelock)( AstObject *, int, int, AstObject **, int * );
205 #endif
206
207
208 /* Define macros for accessing each item of thread specific global data. */
209 #ifdef THREAD_SAFE
210
211 /* Define how to initialise thread-specific globals. */
212 #define GLOBAL_inits \
213 globals->Class_Init = 0; \
214 globals->Simplify_Depth = 0; \
215 globals->Simplify_Stackmaps = NULL;
216
217 /* Create the function that initialises global data for this module. */
218 astMAKE_INITGLOBALS(CmpMap)
219
220 #define class_init astGLOBAL(CmpMap,Class_Init)
221 #define class_vtab astGLOBAL(CmpMap,Class_Vtab)
222 #define simplify_depth astGLOBAL(CmpMap,Simplify_Depth)
223 #define simplify_stackmaps astGLOBAL(CmpMap,Simplify_Stackmaps)
224
225
226
227 /* If thread safety is not needed, declare and initialise globals at static
228 variables. */
229 #else
230
231 static int simplify_depth = 0;
232 static AstMapping **simplify_stackmaps = NULL;
233
234
235 /* Define the class virtual function table and its initialisation flag
236 as static variables. */
237 static AstCmpMapVtab class_vtab; /* Virtual function table */
238 static int class_init = 0; /* Virtual function table initialised? */
239
240 #endif
241
242 /* External Interface Function Prototypes. */
243 /* ======================================= */
244 /* The following functions have public prototypes only (i.e. no
245 protected prototypes), so we must provide local prototypes for use
246 within this module. */
247 AstCmpMap *astCmpMapId_( void *, void *, int, const char *, ... );
248
249 /* Prototypes for Private Member Functions. */
250 /* ======================================== */
251 static AstMapping *CombineMaps( AstMapping *, int, AstMapping *, int, int, int * );
252 static AstMapping *RemoveRegions( AstMapping *, int * );
253 static AstMapping *Simplify( AstMapping *, int * );
254 static AstPointSet *Transform( AstMapping *, AstPointSet *, int, AstPointSet *, int * );
255 static double Rate( AstMapping *, double *, int, int, int * );
256 static int *MapSplit( AstMapping *, int, const int *, AstMapping **, int * );
257 static int *MapSplit0( AstMapping *, int, const int *, AstMapping **, int, int * );
258 static int *MapSplit1( AstMapping *, int, const int *, AstMapping **, int * );
259 static int *MapSplit2( AstMapping *, int, const int *, AstMapping **, int * );
260 static int Equal( AstObject *, AstObject *, int * );
261 static int GetIsLinear( AstMapping *, int * );
262 static int MapList( AstMapping *, int, int, int *, AstMapping ***, int **, int * );
263 static int MapMerge( AstMapping *, int, int, int *, AstMapping ***, int **, int * );
264 static int PatternCheck( int, int, int **, int *, int * );
265 static void Copy( const AstObject *, AstObject *, int * );
266 static void Decompose( AstMapping *, AstMapping **, AstMapping **, int *, int *, int *, int * );
267 static void Delete( AstObject *, int * );
268 static void Dump( AstObject *, AstChannel *, int * );
269 static int GetObjSize( AstObject *, int * );
270
271 #if defined(THREAD_SAFE)
272 static int ManageLock( AstObject *, int, int, AstObject **, int * );
273 #endif
274
275
276 /* Member functions. */
277 /* ================= */
Equal(AstObject * this_object,AstObject * that_object,int * status)278 static int Equal( AstObject *this_object, AstObject *that_object, int *status ) {
279 /*
280 * Name:
281 * Equal
282
283 * Purpose:
284 * Test if two CmpMaps are equivalent.
285
286 * Type:
287 * Private function.
288
289 * Synopsis:
290 * #include "cmpmap.h"
291 * int Equal( AstObject *this, AstObject *that, int *status )
292
293 * Class Membership:
294 * CmpMap member function (over-rides the astEqual protected
295 * method inherited from the astMapping class).
296
297 * Description:
298 * This function returns a boolean result (0 or 1) to indicate whether
299 * two CmpMaps are equivalent.
300
301 * Parameters:
302 * this
303 * Pointer to the first Object (a CmpMap).
304 * that
305 * Pointer to the second Object.
306 * status
307 * Pointer to the inherited status variable.
308
309 * Returned Value:
310 * One if the CmpMaps are equivalent, zero otherwise.
311
312 * Notes:
313 * - A value of zero will be returned if this function is invoked
314 * with the global status set, or if it should fail for any reason.
315 */
316
317 /* Local Variables: */
318 AstCmpMap *that;
319 AstCmpMap *this;
320 AstMapping **that_map_list;
321 AstMapping **this_map_list;
322 int *that_invert_list;
323 int *this_invert_list;
324 int i;
325 int result;
326 int that_inv;
327 int that_nmap;
328 int this_inv;
329 int this_nmap;
330
331 /* Initialise. */
332 result = 0;
333
334 /* Check the global error status. */
335 if ( !astOK ) return result;
336
337 /* Obtain pointers to the two CmpMap structures. */
338 this = (AstCmpMap *) this_object;
339 that = (AstCmpMap *) that_object;
340
341 /* Check the second object is a CmpMap. We know the first is a
342 CmpMap since we have arrived at this implementation of the virtual
343 function. */
344 if( astIsACmpMap( that ) ) {
345
346 /* Check they are both either parallel or series. */
347 if( that->series == that->series ) {
348
349 /* Decompose the first CmpMap into a sequence of Mappings to be applied in
350 series or parallel, as appropriate, and an associated list of
351 Invert flags. */
352 this_nmap = 0;
353 this_map_list = NULL;
354 this_invert_list = NULL;
355 astMapList( (AstMapping *) this, this->series, astGetInvert( this ),
356 &this_nmap, &this_map_list, &this_invert_list );
357
358 /* Similarly decompose the second CmpMap. */
359 that_nmap = 0;
360 that_map_list = NULL;
361 that_invert_list = NULL;
362 astMapList( (AstMapping *) that, that->series, astGetInvert( that ),
363 &that_nmap, &that_map_list, &that_invert_list );
364
365 /* Check the decompositions yielded the same number of component
366 Mappings. */
367 if( that_nmap == this_nmap ) {
368
369 /* Check equality of every component. */
370 for( i = 0; i < this_nmap; i++ ) {
371
372 /* Temporarily set the Mapping Invert flags to the required values,
373 saving the original values so that they can be re-instated later.*/
374 this_inv = astGetInvert( this_map_list[ i ] );
375 astSetInvert( this_map_list[ i ], this_invert_list[ i ] );
376 that_inv = astGetInvert( that_map_list[ i ] );
377 astSetInvert( that_map_list[ i ], that_invert_list[ i ] );
378
379 /* Compare the two component Mappings for equality. */
380 result = astEqual( this_map_list[ i ], that_map_list[ i ] );
381
382 /* Re-instate the original Invert flags. */
383 astSetInvert( this_map_list[ i ], this_inv );
384 astSetInvert( that_map_list[ i ], that_inv );
385
386 /* Leave the loop if the Mappings are not equal. */
387 if( !result ) break;
388 }
389 }
390
391 /* Free resources */
392 for( i = 0; i < this_nmap; i++ ) {
393 this_map_list[ i ] = astAnnul( this_map_list[ i ] );
394 }
395 this_map_list = astFree( this_map_list );
396 this_invert_list = astFree( this_invert_list );
397
398 for( i = 0; i < that_nmap; i++ ) {
399 that_map_list[ i ] = astAnnul( that_map_list[ i ] );
400 }
401 that_map_list = astFree( that_map_list );
402 that_invert_list = astFree( that_invert_list );
403
404 }
405 }
406
407 /* If an error occurred, clear the result value. */
408 if ( !astOK ) result = 0;
409
410 /* Return the result, */
411 return result;
412 }
413
GetIsLinear(AstMapping * this_mapping,int * status)414 static int GetIsLinear( AstMapping *this_mapping, int *status ){
415 /*
416 * Name:
417 * GetIsLinear
418
419 * Purpose:
420 * Return the value of the IsLinear attribute for a CmpMap.
421
422 * Type:
423 * Private function.
424
425 * Synopsis:
426 * #include "mapping.h"
427 * void GetIsLinear( AstMapping *this, int *status )
428
429 * Class Membership:
430 * CmpMap member function (over-rides the protected astGetIsLinear
431 * method inherited from the Mapping class).
432
433 * Description:
434 * This function returns the value of the IsLinear attribute for a
435 * Frame, which is one if both component Mappings have a value of 1
436 * for the IsLinear attribute.
437
438 * Parameters:
439 * this
440 * Pointer to the CmpqMap.
441 * status
442 * Pointer to the inherited status variable.
443 */
444 AstCmpMap *this;
445 this = (AstCmpMap *) this_mapping;
446 return astGetIsLinear( this->map1 ) && astGetIsLinear( this->map2 );
447 }
448
GetObjSize(AstObject * this_object,int * status)449 static int GetObjSize( AstObject *this_object, int *status ) {
450 /*
451 * Name:
452 * GetObjSize
453
454 * Purpose:
455 * Return the in-memory size of an Object.
456
457 * Type:
458 * Private function.
459
460 * Synopsis:
461 * #include "cmpmap.h"
462 * int GetObjSize( AstObject *this, int *status )
463
464 * Class Membership:
465 * CmpMap member function (over-rides the astGetObjSize protected
466 * method inherited from the parent class).
467
468 * Description:
469 * This function returns the in-memory size of the supplied CmpMap,
470 * in bytes.
471
472 * Parameters:
473 * this
474 * Pointer to the CmpMap.
475 * status
476 * Pointer to the inherited status variable.
477
478 * Returned Value:
479 * The Object size, in bytes.
480
481 * Notes:
482 * - A value of zero will be returned if this function is invoked
483 * with the global status set, or if it should fail for any reason.
484 */
485
486 /* Local Variables: */
487 AstCmpMap *this; /* Pointer to CmpMap structure */
488 int result; /* Result value to return */
489
490 /* Initialise. */
491 result = 0;
492
493 /* Check the global error status. */
494 if ( !astOK ) return result;
495
496 /* Obtain a pointers to the CmpMap structure. */
497 this = (AstCmpMap *) this_object;
498
499 /* Invoke the GetObjSize method inherited from the parent class, and then
500 add on any components of the class structure defined by thsi class
501 which are stored in dynamically allocated memory. */
502 result = (*parent_getobjsize)( this_object, status );
503
504 result += astGetObjSize( this->map1 );
505 result += astGetObjSize( this->map2 );
506
507 /* If an error occurred, clear the result value. */
508 if ( !astOK ) result = 0;
509
510 /* Return the result, */
511 return result;
512 }
513
CombineMaps(AstMapping * mapping1,int invert1,AstMapping * mapping2,int invert2,int series,int * status)514 static AstMapping *CombineMaps( AstMapping *mapping1, int invert1,
515 AstMapping *mapping2, int invert2,
516 int series, int *status ) {
517 /*
518 * Name:
519 * CombineMaps
520
521 * Purpose:
522 * Combine two Mappings with specified Invert flags into a CmpMap.
523
524 * Type:
525 * Private function.
526
527 * Synopsis:
528 * #include "cmpmap.h"
529 * AstMapping *CombineMaps( AstMapping *mapping1, int invert1,
530 * AstMapping *mapping2, int invert2,
531 * int series, int *status )
532
533 * Class Membership:
534 * CmpMap member function.
535
536 * Description:
537 * This function combines two Mappings into a CmpMap (compound
538 * Mapping) as if their Invert flags were set to specified values
539 * when the CmpMap is created. However, the individual Mappings are
540 * returned with their Invert flag values unchanged from their
541 * original state.
542
543 * Parameters:
544 * mapping1
545 * Pointer to the first Mapping.
546 * invert1
547 * The (boolean) Invert flag value required for the first Mapping.
548 * mapping2
549 * Pointer to the second Mapping.
550 * invert2
551 * The (boolean) Invert flag value required for the second Mapping.
552 * series
553 * Whether the Mappings are to be combined in series (as opposed to
554 * in parallel).
555 * status
556 * Pointer to the inherited status variable.
557
558 * Returned Value:
559 * A pointer to the resulting compound Mapping (a CmpMap).
560
561 * Notes:
562 * - This function is a wrap-up for the astCmpMap constructor and
563 * temporarily assigns the required Invert flag values while
564 * creating the required CmpMap. However, it also takes account of
565 * the possibility that the two Mapping pointers supplied may point
566 * at the same Mapping.
567 * - A null Object pointer (AST__NULL) will be returned if this
568 * function is invoked with the AST error status set, or if it
569 * should fail for any reason.
570 */
571
572 /* Local Variables: */
573 AstMapping *map1; /* First temporary Mapping pointer */
574 AstMapping *map2; /* Second temporary Mapping pointer */
575 AstMapping *result; /* Pointer to result Mapping */
576 int copy; /* Copy needed? */
577 int inv1; /* First original Invert flag value */
578 int inv2; /* Second original Invert flag value */
579 int set1; /* First Invert flag originally set? */
580 int set2; /* Second Invert flag originally set? */
581
582 /* Initialise */
583 result = NULL;
584
585 /* Check the global error status. */
586 if ( !astOK ) return result;
587
588 /* Limit incoming values to 0 or 1. */
589 invert1 = ( invert1 != 0 );
590 invert2 = ( invert2 != 0 );
591
592 /* Obtain the Invert flag values for each Mapping. */
593 inv1 = astGetInvert( mapping1 );
594 inv2 = astGetInvert( mapping2 );
595
596 /* Also determine if these values are explicitly set. */
597 set1 = astTestInvert( mapping1 );
598 set2 = astTestInvert( mapping2 );
599
600 /* If both Mappings are actually the same but we need different Invert
601 flag values to be set, then this can only be achieved by making a
602 copy. Note if this is necessary. */
603 copy = ( ( mapping1 == mapping2 ) && ( invert1 != invert2 ) );
604
605 /* Clone the first Mapping pointer. Do likewise for the second but
606 make a copy instead if necessary. */
607 map1 = astClone( mapping1 );
608 map2 = copy ? astCopy( mapping2 ) : astClone( mapping2 );
609
610 /* If the Invert value for the first Mapping needs changing, make the
611 change. */
612 if ( invert1 != inv1 ) {
613 if ( invert1 ) {
614 astSetInvert( map1, 1 );
615 } else {
616 astClearInvert( map1 );
617 }
618 }
619
620 /* Similarly, change the Invert flag for the second Mapping if
621 necessary. */
622 if ( invert2 != inv2 ) {
623 if ( invert2 ) {
624 astSetInvert( map2, 1 );
625 } else {
626 astClearInvert( map2 );
627 }
628 }
629
630 /* Combine the two Mappings into a CmpMap. */
631 result = (AstMapping *) astCmpMap( map1, map2, series, "", status );
632
633 /* If the first Mapping's Invert value was changed, restore it to its
634 original state. */
635 if ( invert1 != inv1 ) {
636 if ( set1 ) {
637 astSetInvert( map1, inv1 );
638 } else {
639 astClearInvert( map1 );
640 }
641 }
642
643 /* Similarly, restore the second Mapping's Invert value if
644 necessary. This step is not needed, however, if a copy was made. */
645 if ( ( invert2 != inv2 ) && !copy ) {
646 if ( set2 ) {
647 astSetInvert( map2, inv2 );
648 } else {
649 astClearInvert( map2 );
650 }
651 }
652
653 /* Annul the temporary Mapping pointers. */
654 map1 = astAnnul( map1 );
655 map2 = astAnnul( map2 );
656
657 /* If an error occurred, then annul the result pointer. */
658 if ( !astOK ) result = astAnnul( result );
659
660 /* Return the result. */
661 return result;
662 }
663
Decompose(AstMapping * this_mapping,AstMapping ** map1,AstMapping ** map2,int * series,int * invert1,int * invert2,int * status)664 static void Decompose( AstMapping *this_mapping, AstMapping **map1,
665 AstMapping **map2, int *series, int *invert1,
666 int *invert2, int *status ) {
667 /*
668 *
669 * Name:
670 * Decompose
671
672 * Purpose:
673 * Decompose a Mapping into two component Mappings.
674
675 * Type:
676 * Private function.
677
678 * Synopsis:
679 * #include "mapping.h"
680 * void Decompose( AstMapping *this, AstMapping **map1,
681 * AstMapping **map2, int *series,
682 * int *invert1, int *invert2, int *status )
683
684 * Class Membership:
685 * CmpMap member function (over-rides the protected astDecompose
686 * method inherited from the Mapping class).
687
688 * Description:
689 * This function returns pointers to two Mappings which, when applied
690 * either in series or parallel, are equivalent to the supplied Mapping.
691 *
692 * Since the Frame class inherits from the Mapping class, Frames can
693 * be considered as special types of Mappings and so this method can
694 * be used to decompose either CmpMaps or CmpFrames.
695
696 * Parameters:
697 * this
698 * Pointer to the Mapping.
699 * map1
700 * Address of a location to receive a pointer to first component
701 * Mapping.
702 * map2
703 * Address of a location to receive a pointer to second component
704 * Mapping.
705 * series
706 * Address of a location to receive a value indicating if the
707 * component Mappings are applied in series or parallel. A non-zero
708 * value means that the supplied Mapping is equivalent to applying map1
709 * followed by map2 in series. A zero value means that the supplied
710 * Mapping is equivalent to applying map1 to the lower numbered axes
711 * and map2 to the higher numbered axes, in parallel.
712 * invert1
713 * The value of the Invert attribute to be used with map1.
714 * invert2
715 * The value of the Invert attribute to be used with map2.
716 * status
717 * Pointer to the inherited status variable.
718
719 * Notes:
720 * - Any changes made to the component Mappings using the returned
721 * pointers will be reflected in the supplied Mapping.
722
723 *-
724 */
725
726
727 /* Local Variables: */
728 AstCmpMap *this; /* Pointer to CmpMap structure */
729
730 /* Check the global error status. */
731 if ( !astOK ) return;
732
733 /* Obtain a pointer to the CmpMap structure. */
734 this = (AstCmpMap *) this_mapping;
735
736 /* First deal with series mappings. */
737 if( this->series ) {
738 if( series ) *series = 1;
739
740 /* If the CmpMap has been inverted, return the Mappings in reverse
741 order with inverted Invert falgs. */
742 if( astGetInvert( this ) ) {
743 if( map1 ) *map1 = astClone( this->map2 );
744 if( map2 ) *map2 = astClone( this->map1 );
745 if( invert1 ) *invert1 = this->invert2 ? 0 : 1;
746 if( invert2 ) *invert2 = this->invert1 ? 0 : 1;
747
748 /* If the CmpMap has not been inverted, return the Mappings in their
749 original order with their original Invert flags. */
750 } else {
751 if( map1 ) *map1 = astClone( this->map1 );
752 if( map2 ) *map2 = astClone( this->map2 );
753 if( invert1 ) *invert1 = this->invert1;
754 if( invert2 ) *invert2 = this->invert2;
755 }
756
757 /* Now deal with parallel mappings. */
758 } else {
759 if( series ) *series = 0;
760
761 /* The mappings are returned in their original order whether or not the
762 CmpMap has been inverted. */
763 if( map1 ) *map1 = astClone( this->map1 );
764 if( map2 ) *map2 = astClone( this->map2 );
765
766 /* If the CmpMap has been inverted, return inverted Invert flags. */
767 if( astGetInvert( this ) ) {
768 if( invert1 ) *invert1 = this->invert1 ? 0 : 1;
769 if( invert2 ) *invert2 = this->invert2 ? 0 : 1;
770
771 /* If the CmpMap has not been inverted, return the original Invert flags. */
772 } else {
773 if( invert1 ) *invert1 = this->invert1;
774 if( invert2 ) *invert2 = this->invert2;
775 }
776
777 }
778 }
779
astInitCmpMapVtab_(AstCmpMapVtab * vtab,const char * name,int * status)780 void astInitCmpMapVtab_( AstCmpMapVtab *vtab, const char *name, int *status ) {
781 /*
782 *+
783 * Name:
784 * astInitCmpMapVtab
785
786 * Purpose:
787 * Initialise a virtual function table for a CmpMap.
788
789 * Type:
790 * Protected function.
791
792 * Synopsis:
793 * #include "cmpmap.h"
794 * void astInitCmpMapVtab( AstCmpMapVtab *vtab, const char *name )
795
796 * Class Membership:
797 * CmpMap vtab initialiser.
798
799 * Description:
800 * This function initialises the component of a virtual function
801 * table which is used by the CmpMap class.
802
803 * Parameters:
804 * vtab
805 * Pointer to the virtual function table. The components used by
806 * all ancestral classes will be initialised if they have not already
807 * been initialised.
808 * name
809 * Pointer to a constant null-terminated character string which contains
810 * the name of the class to which the virtual function table belongs (it
811 * is this pointer value that will subsequently be returned by the Object
812 * astClass function).
813 *-
814 */
815
816 /* Local Variables: */
817 astDECLARE_GLOBALS /* Pointer to thread-specific global data */
818 AstMappingVtab *mapping; /* Pointer to Mapping component of Vtab */
819 AstObjectVtab *object; /* Pointer to Object component of Vtab */
820
821 /* Check the local error status. */
822 if ( !astOK ) return;
823
824 /* Get a pointer to the thread specific global data structure. */
825 astGET_GLOBALS(NULL);
826
827 /* Initialize the component of the virtual function table used by the
828 parent class. */
829 astInitMappingVtab( (AstMappingVtab *) vtab, name );
830
831 /* Store a unique "magic" value in the virtual function table. This
832 will be used (by astIsACmpMap) to determine if an object belongs to
833 this class. We can conveniently use the address of the (static)
834 class_check variable to generate this unique value. */
835 vtab->id.check = &class_check;
836 vtab->id.parent = &(((AstMappingVtab *) vtab)->id);
837
838 /* Initialise member function pointers. */
839 /* ------------------------------------ */
840 /* Store pointers to the member functions (implemented here) that
841 provide virtual methods for this class. */
842
843 /* None. */
844
845 /* Save the inherited pointers to methods that will be extended, and
846 replace them with pointers to the new member functions. */
847 object = (AstObjectVtab *) vtab;
848 mapping = (AstMappingVtab *) vtab;
849 parent_getobjsize = object->GetObjSize;
850 object->GetObjSize = GetObjSize;
851
852 #if defined(THREAD_SAFE)
853 parent_managelock = object->ManageLock;
854 object->ManageLock = ManageLock;
855 #endif
856
857 parent_maplist = mapping->MapList;
858 mapping->MapList = MapList;
859
860 parent_transform = mapping->Transform;
861 mapping->Transform = Transform;
862
863 parent_mapsplit = mapping->MapSplit;
864 mapping->MapSplit = MapSplit;
865
866 /* Store replacement pointers for methods which will be over-ridden by
867 new member functions implemented here. */
868 object->Equal = Equal;
869 mapping->Decompose = Decompose;
870 mapping->MapMerge = MapMerge;
871 mapping->Simplify = Simplify;
872 mapping->RemoveRegions = RemoveRegions;
873 mapping->GetIsLinear = GetIsLinear;
874
875 /* For some reason the CmpMap implementation of astRate can be immensely
876 slow for complex Mapping, so it's currently disable until such time as
877 I have time to sort it out.
878
879 mapping->Rate = Rate;
880 */
881
882 /* Declare the copy constructor, destructor and class dump function. */
883 astSetCopy( vtab, Copy );
884 astSetDelete( vtab, Delete );
885 astSetDump( vtab, Dump, "CmpMap", "Compound Mapping" );
886
887 /* If we have just initialised the vtab for the current class, indicate
888 that the vtab is now initialised, and store a pointer to the class
889 identifier in the base "object" level of the vtab. */
890 if( vtab == &class_vtab ) {
891 class_init = 1;
892 astSetVtabClassIdentifier( vtab, &(vtab->id) );
893 }
894 }
895
896 #if defined(THREAD_SAFE)
ManageLock(AstObject * this_object,int mode,int extra,AstObject ** fail,int * status)897 static int ManageLock( AstObject *this_object, int mode, int extra,
898 AstObject **fail, int *status ) {
899 /*
900 * Name:
901 * ManageLock
902
903 * Purpose:
904 * Manage the thread lock on an Object.
905
906 * Type:
907 * Private function.
908
909 * Synopsis:
910 * #include "object.h"
911 * AstObject *ManageLock( AstObject *this, int mode, int extra,
912 * AstObject **fail, int *status )
913
914 * Class Membership:
915 * CmpMap member function (over-rides the astManageLock protected
916 * method inherited from the parent class).
917
918 * Description:
919 * This function manages the thread lock on the supplied Object. The
920 * lock can be locked, unlocked or checked by this function as
921 * deteremined by parameter "mode". See astLock for details of the way
922 * these locks are used.
923
924 * Parameters:
925 * this
926 * Pointer to the Object.
927 * mode
928 * An integer flag indicating what the function should do:
929 *
930 * AST__LOCK: Lock the Object for exclusive use by the calling
931 * thread. The "extra" value indicates what should be done if the
932 * Object is already locked (wait or report an error - see astLock).
933 *
934 * AST__UNLOCK: Unlock the Object for use by other threads.
935 *
936 * AST__CHECKLOCK: Check that the object is locked for use by the
937 * calling thread (report an error if not).
938 * extra
939 * Extra mode-specific information.
940 * fail
941 * If a non-zero function value is returned, a pointer to the
942 * Object that caused the failure is returned at "*fail". This may
943 * be "this" or it may be an Object contained within "this". Note,
944 * the Object's reference count is not incremented, and so the
945 * returned pointer should not be annulled. A NULL pointer is
946 * returned if this function returns a value of zero.
947 * status
948 * Pointer to the inherited status variable.
949
950 * Returned Value:
951 * A local status value:
952 * 0 - Success
953 * 1 - Could not lock or unlock the object because it was already
954 * locked by another thread.
955 * 2 - Failed to lock a POSIX mutex
956 * 3 - Failed to unlock a POSIX mutex
957 * 4 - Bad "mode" value supplied.
958
959 * Notes:
960 * - This function attempts to execute even if an error has already
961 * occurred.
962 */
963
964 /* Local Variables: */
965 AstCmpMap *this; /* Pointer to CmpMap structure */
966 int result; /* Returned status value */
967
968 /* Initialise */
969 result = 0;
970
971 /* Check the supplied pointer is not NULL. */
972 if( !this_object ) return result;
973
974 /* Obtain a pointers to the CmpMap structure. */
975 this = (AstCmpMap *) this_object;
976
977 /* Invoke the ManageLock method inherited from the parent class. */
978 if( !result ) result = (*parent_managelock)( this_object, mode, extra,
979 fail, status );
980
981 /* Invoke the astManageLock method on any Objects contained within
982 the supplied Object. */
983 if( !result ) result = astManageLock( this->map1, mode, extra, fail );
984 if( !result ) result = astManageLock( this->map2, mode, extra, fail );
985
986 return result;
987
988 }
989 #endif
990
MapList(AstMapping * this_mapping,int series,int invert,int * nmap,AstMapping *** map_list,int ** invert_list,int * status)991 static int MapList( AstMapping *this_mapping, int series, int invert,
992 int *nmap, AstMapping ***map_list, int **invert_list, int *status ) {
993 /*
994 * Name:
995 * MapList
996
997 * Purpose:
998 * Decompose a CmpMap into a sequence of simpler Mappings.
999
1000 * Type:
1001 * Private function.
1002
1003 * Synopsis:
1004 * #include "mapping.h"
1005 * int MapList( AstMapping *this, int series, int invert, int *nmap,
1006 * AstMapping ***map_list, int **invert_list )
1007
1008 * Class Membership:
1009 * CmpMap member function (over-rides the protected astMapList
1010 * method inherited from the Maping class).
1011
1012 * Description:
1013 * This function decomposes a CmpMap into a sequence of simpler
1014 * Mappings which may be applied in sequence to achieve the same
1015 * effect. The CmpMap is decomposed as far as possible, but it is
1016 * not guaranteed that this will necessarily yield any more than
1017 * one Mapping, which may actually be the original CmpMap supplied.
1018 *
1019 * This function is provided to support both the simplification of
1020 * CmpMaps, and the analysis of CmpMap structure so that particular
1021 * forms can be recognised.
1022
1023 * Parameters:
1024 * this
1025 * Pointer to the CmpMap to be decomposed (the CmpMap is not
1026 * actually modified by this function).
1027 * series
1028 * If this value is non-zero, an attempt will be made to
1029 * decompose the CmpMap into a sequence of equivalent Mappings
1030 * which can be applied in series (i.e. one after the other). If
1031 * it is zero, the decomposition will instead yield Mappings
1032 * which can be applied in parallel (i.e. on successive sub-sets
1033 * of the input/output coordinates).
1034 * invert
1035 * The value to which the CmpMap's Invert attribute is to be
1036 * (notionally) set before performing the
1037 * decomposition. Normally, the value supplied here will be the
1038 * actual Invert value obtained from the CmpMap (e.g. using
1039 * astGetInvert). Sometimes, however, when a CmpMap is
1040 * encapsulated within another structure, that structure may
1041 * retain an Invert value (in order to prevent external
1042 * interference) which should be used instead.
1043 *
1044 * Note that the actual Invert value of the CmpMap supplied is
1045 * not used (or modified) by this function.
1046 * nmap
1047 * The address of an int which holds a count of the number of
1048 * individual Mappings in the decomposition. On entry, this
1049 * should count the number of Mappings already in the
1050 * "*map_list" array (below). On exit, it is updated to include
1051 * any new Mappings appended by this function.
1052 * map_list
1053 * Address of a pointer to an array of Mapping pointers. On
1054 * entry, this array pointer should either be NULL (if no
1055 * Mappings have yet been obtained) or should point at a
1056 * dynamically allocated array containing Mapping pointers
1057 * ("*nmap" in number) which have been obtained from a previous
1058 * invocation of this function.
1059 *
1060 * On exit, the dynamic array will be enlarged to contain any
1061 * new Mapping pointers that result from the decomposition
1062 * requested. These pointers will be appended to any previously
1063 * present, and the array pointer will be updated as necessary
1064 * to refer to the enlarged array (any space released by the
1065 * original array will be freed automatically).
1066 *
1067 * The new Mapping pointers returned will identify a sequence of
1068 * Mappings which, when applied in order, will perform a forward
1069 * transformation equivalent to that of the original CmpMap
1070 * (after its Invert flag has first been set to the value
1071 * requested above). The Mappings should be applied in series or
1072 * in parallel according to the type of decomposition requested.
1073 *
1074 * All the Mapping pointers returned by this function should be
1075 * annulled by the caller, using astAnnul, when no longer
1076 * required. The dynamic array holding these pointers should
1077 * also be freed, using astFree.
1078 * invert_list
1079 * Address of a pointer to an array of int. On entry, this array
1080 * pointer should either be NULL (if no Mappings have yet been
1081 * obtained) or should point at a dynamically allocated array
1082 * containing Invert attribute values ("*nmap" in number) which
1083 * have been obtained from a previous invocation of this
1084 * function.
1085 *
1086 * On exit, the dynamic array will be enlarged to contain any
1087 * new Invert attribute values that result from the
1088 * decomposition requested. These values will be appended to any
1089 * previously present, and the array pointer will be updated as
1090 * necessary to refer to the enlarged array (any space released
1091 * by the original array will be freed automatically).
1092 *
1093 * The new Invert values returned identify the values which must
1094 * be assigned to the Invert attributes of the corresponding
1095 * Mappings (whose pointers are in the "*map_list" array) before
1096 * they are applied. Note that these values may differ from the
1097 * actual Invert attribute values of these Mappings, which are
1098 * not relevant.
1099 *
1100 * The dynamic array holding these values should be freed by the
1101 * caller, using astFree, when no longer required.
1102
1103 * Returned Value:
1104 * A non-zero value is returned if the supplied Mapping contained any
1105 * inverted CmpMaps.
1106
1107 * Notes:
1108 * - It is unspecified to what extent the original CmpMap and the
1109 * individual (decomposed) Mappings are
1110 * inter-dependent. Consequently, the individual Mappings cannot be
1111 * modified without risking modification of the original CmpMap.
1112 * - If this function is invoked with the global error status set,
1113 * or if it should fail for any reason, then the *nmap value, the
1114 * list of Mapping pointers and the list of Invert values will all
1115 * be returned unchanged.
1116 */
1117
1118 /* Local Variables: */
1119 AstCmpMap *this; /* Pointer to CmpMap structure */
1120 int invert1; /* Invert flag for first component Mapping */
1121 int invert2; /* Invert flag for second component Mapping */
1122 int r1; /* Value returned from first map list */
1123 int r2; /* Value returned from second map list */
1124 int result; /* Returned value */
1125
1126 /* Check the global error status. */
1127 if ( !astOK ) return 0;
1128
1129 /* Obtain a pointer to the CmpMap structure. */
1130 this = (AstCmpMap *) this_mapping;
1131
1132 /* Check if the CmpMap combines its component Mappings in the same way
1133 (series or parallel) as the decomposition requires. Also, do not
1134 expand CmpMaps that are not appropriate for simplification. */
1135 if ( this->series == series && !astDoNotSimplify( this ) ) {
1136
1137 /* If so, obtain the Invert attribute values to be applied to each
1138 component Mapping. */
1139 invert1 = this->invert1;
1140 invert2 = this->invert2;
1141
1142 /* If the CmpMap itself is inverted, also invert the Invert values to be
1143 applied to its components. */
1144 if ( invert ) {
1145 invert1 = !invert1;
1146 invert2 = !invert2;
1147 }
1148
1149 /* If the component Mappings are applied in series, then concatenate
1150 the Mapping lists obtained from each of them. Do this in reverse
1151 order if the CmpMap is inverted, since the second Mapping would be
1152 applied first in this case. */
1153 if ( series ) {
1154 if ( !invert ) {
1155 r1 = astMapList( this->map1, series, invert1,
1156 nmap, map_list, invert_list );
1157 r2 = astMapList( this->map2, series, invert2,
1158 nmap, map_list, invert_list );
1159 } else {
1160 r1 = astMapList( this->map2, series, invert2,
1161 nmap, map_list, invert_list );
1162 r2 = astMapList( this->map1, series, invert1,
1163 nmap, map_list, invert_list );
1164 }
1165
1166 /* If the component Mappings are applied in parallel, then concatenate
1167 the Mapping lists obtained from each of them. In this case,
1168 inverting the CmpMap has no effect on the order in which they are
1169 applied. */
1170 } else {
1171 r1 = astMapList( this->map1, series, invert1,
1172 nmap, map_list, invert_list );
1173 r2 = astMapList( this->map2, series, invert2,
1174 nmap, map_list, invert_list );
1175 }
1176
1177 /* Did we find any inverted CmpMaps? */
1178 result = invert || r1 || r2;
1179
1180 /* If the CmpMap does not combine its components in the way required
1181 by the decomposition (series or parallel), then we cannot decompose
1182 it. In this case it must be appended to the Mapping list as a
1183 single entity. We can use the parent class method to do this. */
1184 } else {
1185 result = ( *parent_maplist )( this_mapping, series, invert, nmap,
1186 map_list, invert_list, status );
1187 }
1188
1189 return result;
1190 }
1191
MapMerge(AstMapping * this,int where,int series,int * nmap,AstMapping *** map_list,int ** invert_list,int * status)1192 static int MapMerge( AstMapping *this, int where, int series, int *nmap,
1193 AstMapping ***map_list, int **invert_list, int *status ) {
1194 /*
1195 * Name:
1196 * MapMerge
1197
1198 * Purpose:
1199 * Simplify a sequence of Mappings containing a CmpMap.
1200
1201 * Type:
1202 * Private function.
1203
1204 * Synopsis:
1205 * #include "mapping.h"
1206 * int MapMerge( AstMapping *this, int where, int series, int *nmap,
1207 * AstMapping ***map_list, int **invert_list )
1208
1209 * Class Membership:
1210 * CmpMap method (over-rides the protected astMapMerge method
1211 * inherited from the Mapping class).
1212
1213 * Description:
1214 * This function attempts to simplify a sequence of Mappings by
1215 * merging a nominated CmpMap in the sequence with its neighbours,
1216 * so as to shorten the sequence if possible.
1217 *
1218 * In many cases, simplification will not be possible and the
1219 * function will return -1 to indicate this, without further
1220 * action.
1221 *
1222 * In most cases of interest, however, this function will either
1223 * attempt to replace the nominated CmpMap with one which it
1224 * considers simpler, or to merge it with the Mappings which
1225 * immediately precede it or follow it in the sequence (both will
1226 * normally be considered). This is sufficient to ensure the
1227 * eventual simplification of most Mapping sequences by repeated
1228 * application of this function.
1229 *
1230 * In some cases, the function may attempt more elaborate
1231 * simplification, involving any number of other Mappings in the
1232 * sequence. It is not restricted in the type or scope of
1233 * simplification it may perform, but will normally only attempt
1234 * elaborate simplification in cases where a more straightforward
1235 * approach is not adequate.
1236
1237 * Parameters:
1238 * this
1239 * Pointer to the nominated CmpMap which is to be merged with
1240 * its neighbours. This should be a cloned copy of the CmpMap
1241 * pointer contained in the array element "(*map_list)[where]"
1242 * (see below). This pointer will not be annulled, and the
1243 * CmpMap it identifies will not be modified by this function.
1244 * where
1245 * Index in the "*map_list" array (below) at which the pointer
1246 * to the nominated CmpMap resides.
1247 * series
1248 * A non-zero value indicates that the sequence of Mappings to
1249 * be simplified will be applied in series (i.e. one after the
1250 * other), whereas a zero value indicates that they will be
1251 * applied in parallel (i.e. on successive sub-sets of the
1252 * input/output coordinates).
1253 * nmap
1254 * Address of an int which counts the number of Mappings in the
1255 * sequence. On entry this should be set to the initial number
1256 * of Mappings. On exit it will be updated to record the number
1257 * of Mappings remaining after simplification.
1258 * map_list
1259 * Address of a pointer to a dynamically allocated array of
1260 * Mapping pointers (produced, for example, by the astMapList
1261 * method) which identifies the sequence of Mappings. On entry,
1262 * the initial sequence of Mappings to be simplified should be
1263 * supplied.
1264 *
1265 * On exit, the contents of this array will be modified to
1266 * reflect any simplification carried out. Any form of
1267 * simplification may be performed. This may involve any of: (a)
1268 * removing Mappings by annulling any of the pointers supplied,
1269 * (b) replacing them with pointers to new Mappings, (c)
1270 * inserting additional Mappings and (d) changing their order.
1271 *
1272 * The intention is to reduce the number of Mappings in the
1273 * sequence, if possible, and any reduction will be reflected in
1274 * the value of "*nmap" returned. However, simplifications which
1275 * do not reduce the length of the sequence (but improve its
1276 * execution time, for example) may also be performed, and the
1277 * sequence might conceivably increase in length (but normally
1278 * only in order to split up a Mapping into pieces that can be
1279 * more easily merged with their neighbours on subsequent
1280 * invocations of this function).
1281 *
1282 * If Mappings are removed from the sequence, any gaps that
1283 * remain will be closed up, by moving subsequent Mapping
1284 * pointers along in the array, so that vacated elements occur
1285 * at the end. If the sequence increases in length, the array
1286 * will be extended (and its pointer updated) if necessary to
1287 * accommodate any new elements.
1288 *
1289 * Note that any (or all) of the Mapping pointers supplied in
1290 * this array may be annulled by this function, but the Mappings
1291 * to which they refer are not modified in any way (although
1292 * they may, of course, be deleted if the annulled pointer is
1293 * the final one).
1294 * invert_list
1295 * Address of a pointer to a dynamically allocated array which,
1296 * on entry, should contain values to be assigned to the Invert
1297 * attributes of the Mappings identified in the "*map_list"
1298 * array before they are applied (this array might have been
1299 * produced, for example, by the astMapList method). These
1300 * values will be used by this function instead of the actual
1301 * Invert attributes of the Mappings supplied, which are
1302 * ignored.
1303 *
1304 * On exit, the contents of this array will be updated to
1305 * correspond with the possibly modified contents of the
1306 * "*map_list" array. If the Mapping sequence increases in
1307 * length, the "*invert_list" array will be extended (and its
1308 * pointer updated) if necessary to accommodate any new
1309 * elements.
1310
1311 * Returned Value:
1312 * If simplification was possible, the function returns the index
1313 * in the "map_list" array of the first element which was
1314 * modified. Otherwise, it returns -1 (and makes no changes to the
1315 * arrays supplied).
1316
1317 * Notes:
1318 * - A value of -1 will be returned if this function is invoked
1319 * with the global error status set, or if it should fail for any
1320 * reason.
1321 */
1322
1323 /* Local Variables: */
1324 AstCmpMap *cmpmap1; /* Pointer to first CmpMap */
1325 AstCmpMap *cmpmap2; /* Pointer to second CmpMap */
1326 AstCmpMap *cmpmap; /* Pointer to nominated CmpMap */
1327 AstCmpMap *new_cm; /* Pointer to new CmpMap */
1328 AstMapping **map_list1; /* Pointer to list of cmpmap1 component Mappings */
1329 AstMapping **map_list2; /* Pointer to list of cmpmap2 component Mappings */
1330 AstMapping **new_map_list; /* Extended Mapping list */
1331 AstMapping *map; /* Pointer to nominated CmpMap */
1332 AstMapping *new1; /* Pointer to new CmpMap */
1333 AstMapping *new2; /* Pointer to new CmpMap */
1334 AstMapping *new; /* Pointer to replacement Mapping */
1335 AstMapping *simp1; /* Pointer to simplified Mapping */
1336 AstMapping *simp2; /* Pointer to simplified Mapping */
1337 AstMapping *submap1; /* A subset of mappings from cmpmap1 */
1338 AstMapping *submap2; /* A subset of mappings from cmpmap2 */
1339 AstMapping *tmap2; /* Temporary Mapping */
1340 AstMapping *tmap; /* Temporary Mapping */
1341 AstPermMap *new_pm; /* Pointer to new PermMap */
1342 AstPermMap *permmap1; /* Pointer to first PermMap */
1343 AstUnitMap *unit; /* UnitMap that feeds const PermMap i/p's */
1344 const char *class; /* Pointer to Mapping class string */
1345 double *conperm; /* Pointer to PermMap constants array */
1346 double *const_new; /* Pointer to new PermMap constants array */
1347 double *p; /* Pointer to PermMap input position */
1348 double *q; /* Pointer to PermMap output position */
1349 double *qa; /* Pointer to 1st component output position */
1350 double *qb; /* Pointer to 2nd component output position */
1351 int *inperm; /* Pointer to copy of PermMap inperm array */
1352 int *inperm_new; /* Pointer to new PermMap inperm array */
1353 int *invert_list1; /* Pointer to list of cmpmap1 invert values */
1354 int *invert_list2; /* Pointer to list of cmpmap2 invert values */
1355 int *new_invert_list; /* Extended Invert flag list */
1356 int *outperm; /* Pointer to copy of PermMap outperm array */
1357 int *outperm_new; /* Pointer to new PermMap outperm array */
1358 int aconstants; /* Are all 1st component outputs constant? */
1359 int bconstants; /* Are all 2nd component outputs constant? */
1360 int canswap; /* Can nominated Mapping swap with lower neighbour? */
1361 int i; /* Coordinate index */
1362 int iconid; /* Constant identifier in supplied PermMap */
1363 int imap1; /* Index of first Mapping */
1364 int imap2; /* Index of second Mapping */
1365 int imap; /* Loop counter for Mappings */
1366 int invert1; /* Invert flag for first CmpMap */
1367 int invert1a; /* Invert flag for sub-Mapping */
1368 int invert1b; /* Invert flag for sub-Mapping */
1369 int invert2; /* Invert flag for second CmpMap */
1370 int invert2a; /* Invert flag for sub-Mapping */
1371 int invert2b; /* Invert flag for sub-Mapping */
1372 int invert; /* Invert attribute value */
1373 int j; /* Coordinate index */
1374 int jmap1; /* Index of next component Mapping in cmpmap1 */
1375 int jmap2; /* Index of next component Mapping in cmpmap2 */
1376 int new_invert; /* New Invert attribute value */
1377 int nin2a; /* No. input coordinates for sub-Mapping */
1378 int nin2b; /* No. input coordinates for sub-Mapping */
1379 int nmap1; /* Number of Mappings in cmpmap1 */
1380 int nmap2; /* Number of Mappings in cmpmap2 */
1381 int nout2a; /* No. of outputs for 1st component Mapping */
1382 int nout2b; /* No. of outputs for 2nd component Mapping */
1383 int npin; /* No. of inputs for original PermMap */
1384 int npin_new; /* No. of inputs for new PermMap */
1385 int npout; /* No. of outputs for original PermMap */
1386 int npout_new; /* No. of outputs for new PermMap */
1387 int nunit; /* No. of PermMap i/p's fed by UnitMap */
1388 int oconid; /* Constant identifier in returned PermMap */
1389 int result; /* Result value to return */
1390 int set; /* Invert attribute set? */
1391 int simpler; /* Simplification possible? */
1392 int subin2; /* Number of inputs of submap2 */
1393 int subinv1; /* Invert attribute to use with submap1 */
1394 int subinv2; /* Invert attribute to use with submap2 */
1395 int subout1; /* Number of outputs of submap1 */
1396
1397 /* Initialise.*/
1398 result = -1;
1399
1400 /* Check the inherited status. */
1401 if ( !astOK ) return result;
1402
1403 /* Simplify the CmpMap on its own. */
1404 /* =============================== */
1405 /* Obtain a pointer to the nominated Mapping (which is a CmpMap). */
1406 map = ( *map_list )[ where ];
1407 cmpmap = (AstCmpMap *) map;
1408
1409 /* Determine if the Mapping's Invert attribute is set and obtain its
1410 value. */
1411 set = astTestInvert( map );
1412 invert = astGetInvert( map );
1413
1414 /* If necessary, change the Invert attribute to the value we want. We
1415 do this so that simplification (below) has a chance to absorb a
1416 non-zero Invert value into the implementation of the simplified
1417 Mapping (the preference being to have an Invert value of zero after
1418 simplification, if possible). */
1419 if ( invert != ( *invert_list )[ where ] ) {
1420 astSetInvert( map, ( *invert_list )[ where ] );
1421 }
1422
1423 /* Simplify the Mapping and obtain the new Invert value. */
1424 new = astSimplify( map );
1425 new_invert = astGetInvert( new );
1426
1427 /* If necessary, restore the original Mapping's Invert attribute to
1428 its initial state. */
1429 if ( invert != ( *invert_list )[ where ] ) {
1430 if ( set ) {
1431 astSetInvert( map, invert );
1432 } else {
1433 astClearInvert( map );
1434 }
1435 }
1436
1437 /* We must now determine if simplification has occurred. Since this is
1438 internal code, we can compare the two Mapping pointers directly to
1439 see whether "astSimplify" just cloned the pointer we gave it. If it
1440 did, then simplification was probably not possible, but check to
1441 see if the Invert attribute has changed to be sure. */
1442 if ( astOK ) {
1443 simpler = ( new != map ) || ( new_invert != ( *invert_list )[ where ] );
1444
1445 /* If simplification was successful, annul the original pointer in the
1446 Mapping list and replace it with the new one, together with its
1447 invert flag. */
1448 if ( simpler ) {
1449 (void) astAnnul( ( *map_list )[ where ] );
1450 ( *map_list )[ where ] = new;
1451 ( *invert_list )[ where ] = new_invert;
1452
1453 /* Return the result. */
1454 result = where;
1455
1456 /* Otherwise, annul the new Mapping pointer. */
1457 } else {
1458 new = astAnnul( new );
1459
1460 /* If the nominated CmpMap is a series CmpMap and the sequence of
1461 Mappings are being combined in series, or if the nominated CmpMap is
1462 a parallel CmpMap and the sequence of Mappings are being combined in
1463 parallel, replace the single CmpMap with the two component Mappings. */
1464 if( ( series && cmpmap->series ) ||
1465 ( !series && !cmpmap->series ) ) {
1466
1467 /* We are increasing the number of Mappings in the list, so we need to create
1468 new, larger, arrays to hold the list of Mapping pointers and invert flags. */
1469 new_map_list = astMalloc( ( *nmap + 1 )*sizeof( AstMapping * ) );
1470 new_invert_list = astMalloc( ( *nmap + 1 )*sizeof( int ) );
1471 if( astOK ) {
1472
1473 /* Copy the values prior to the nominated CmpMap. */
1474 for( i = 0; i < where; i++ ) {
1475 new_map_list[ i ] = astClone( ( *map_list )[ i ] );
1476 new_invert_list[ i ] = ( *invert_list )[ i ];
1477 }
1478
1479 /* Next insert the two components of the nominated CmpMap */
1480 new_map_list[ where ] = astClone( cmpmap->map1 );
1481 new_invert_list[ where ] = cmpmap->invert1;
1482 new_map_list[ where + 1 ] = astClone( cmpmap->map2 );
1483 new_invert_list[ where + 1 ] = cmpmap->invert2;
1484
1485 /* Now copy any values after the nominated CmpMap. */
1486 for( i = where + 1; i < *nmap; i++ ) {
1487 new_map_list[ i + 1 ] = astClone( ( *map_list )[ i ] );
1488 new_invert_list[ i + 1 ] = ( *invert_list )[ i ];
1489 }
1490
1491 /* Now annul the Object pointers in the supplied map list. */
1492 for( i = 0; i < *nmap; i++ ) {
1493 (* map_list )[ i ] = astAnnul( ( *map_list )[ i ] );
1494 }
1495
1496 /* Free the memory holding the supplied Mapping and invert flag lists. */
1497 astFree( *map_list );
1498 astFree( *invert_list );
1499
1500 /* Return pointers to the new extended lists. */
1501 *map_list = new_map_list;
1502 *invert_list = new_invert_list;
1503
1504 /* Increase the number of Mappings in the list, and the index of
1505 the first modified Mapping. */
1506 (*nmap)++;
1507 result = where;
1508
1509 /* Indicate some simplification has taken place */
1510 simpler = 1;
1511 }
1512 }
1513 }
1514
1515 /* If no simplification has been done, merge adjacent CmpMaps. */
1516 /* ========================================================== */
1517 /* If the CmpMap would not simplify on its own, we now look for a
1518 neighbouring CmpMap with which it might merge. We use the previous
1519 Mapping, if suitable, since this will normally also have been fully
1520 simplified on its own. Check if a previous Mapping exists. */
1521 if( !simpler ) {
1522 if ( astOK && *nmap > 1 ) {
1523
1524 /* Obtain the indices of the two potential Mappings to be merged. imap1
1525 is the first Mapping, imap2 is the second. imapc is the CmpMap, imapn is
1526 the neighbouring Mapping. */
1527 if( where == 0 ) {
1528 imap1 = 0;
1529 imap2 = 1;
1530 } else {
1531 imap1 = where - 1;
1532 imap2 = where;
1533 }
1534
1535 /* Obtain the Class string of the neighbouring Mapping and determine if it
1536 is a CmpMap. */
1537 class = astGetClass( ( *map_list )[ (where>0)?where-1:1 ] );
1538 if ( astOK && !strcmp( class, "CmpMap" ) ) {
1539
1540 /* If suitable, obtain pointers to the two CmpMaps. */
1541 cmpmap1 = (AstCmpMap *) ( *map_list )[ imap1 ];
1542 cmpmap2 = (AstCmpMap *) ( *map_list )[ imap2 ];
1543
1544 /* Obtain the associated invert flag values. */
1545 invert1 = ( *invert_list )[ imap1 ];
1546 invert2 = ( *invert_list )[ imap2 ];
1547
1548 /* Extract the invert flags associated with each CmpMap sub-Mapping
1549 and combine these with the flag values obtained above so as to give
1550 the invert flag to be used with each individual sub-Mapping. */
1551 invert1a = cmpmap1->invert1;
1552 invert1b = cmpmap1->invert2;
1553 if ( invert1 ) {
1554 invert1a = !invert1a;
1555 invert1b = !invert1b;
1556 }
1557 invert2a = cmpmap2->invert1;
1558 invert2b = cmpmap2->invert2;
1559 if ( invert2 ) {
1560 invert2a = !invert2a;
1561 invert2b = !invert2b;
1562 }
1563
1564 /* Series CmpMaps in parallel. */
1565 /* =========================== */
1566 /* Now check if the CmpMaps can be merged. This may be possible if we
1567 are examining a list of Mappings combined in parallel and the two
1568 adjacent CmpMaps both combine their sub-Mappings in series. */
1569 if ( !series && cmpmap1->series && cmpmap2->series ) {
1570
1571 /* Form two new parallel CmpMaps with the sub-Mappings re-arranged so
1572 that when combined in series these new CmpMaps are equivalent to
1573 the original ones. In doing this, we must take account of the
1574 invert flags which apply to each sub-Mapping and also of the fact
1575 that the order in which the sub-Mappings are applied depends on the
1576 invert flags of the original CmpMaps. */
1577 new1 = CombineMaps( invert1 ? cmpmap1->map2 : cmpmap1->map1,
1578 invert1 ? invert1b : invert1a,
1579 invert2 ? cmpmap2->map2 : cmpmap2->map1,
1580 invert2 ? invert2b : invert2a, 0, status );
1581 new2 = CombineMaps( invert1 ? cmpmap1->map1 : cmpmap1->map2,
1582 invert1 ? invert1a : invert1b,
1583 invert2 ? cmpmap2->map1 : cmpmap2->map2,
1584 invert2 ? invert2a : invert2b, 0, status );
1585
1586 /* Having converted the parallel combination of series CmpMaps into a
1587 pair of equivalent parallel CmpMaps that can be combined in series,
1588 try and simplify each of these new CmpMaps. */
1589 simp1 = astSimplify( new1 );
1590 simp2 = astSimplify( new2 );
1591
1592 /* Test if either could be simplified by checking if its pointer value
1593 has changed. Also check if the Invert attribute has changed (not
1594 strictly necessary, but a useful safety feature in case of any
1595 rogue code which just changes this attribute instead of issuing a
1596 new pointer). */
1597 simpler = ( simp1 != new1 ) || ( simp2 != new2 ) ||
1598 astGetInvert( simp1 ) || astGetInvert( simp2 );
1599
1600 /* If either CmpMap was simplified, then combine the resulting
1601 Mappings in series to give the replacement CmpMap. */
1602 if ( simpler ) new =
1603 (AstMapping *) astCmpMap( simp1, simp2, 1, "", status );
1604
1605 /* Annul the temporary Mapping pointers. */
1606 new1 = astAnnul( new1 );
1607 new2 = astAnnul( new2 );
1608 simp1 = astAnnul( simp1 );
1609 simp2 = astAnnul( simp2 );
1610
1611 /* Parallel CmpMaps in series. */
1612 /* =========================== */
1613 /* A pair of adjacent CmpMaps can also potentially be merged if we are
1614 examining a list of Mappings combined in series and the two
1615 adjacent CmpMaps both combine their sub-Mappings in parallel. */
1616 } else if ( series && !cmpmap1->series && !cmpmap2->series ) {
1617
1618 /* Expand each of the two adjacent CmpMaps into a list of Mappings to be
1619 combined in parallel. */
1620 map_list1 = map_list2 = NULL;
1621 invert_list1 = invert_list2 = NULL;
1622 nmap1 = nmap2 = 0;
1623 (void) astMapList( (AstMapping *) cmpmap1, 0, invert1,
1624 &nmap1, &map_list1, &invert_list1 );
1625 (void) astMapList( (AstMapping *) cmpmap2, 0, invert2,
1626 &nmap2, &map_list2, &invert_list2 );
1627
1628 /* We want to divide each of these lists into N sub-lists so that the
1629 outputs of the Mappings in the i'th sub-list from cmpmap1 can feed
1630 (i.e. equal in number) the inputs of the Mappings in the i'th sub-list
1631 from cmpmap2. If such a sub-list contains more than one Mapping we
1632 combine them together into a parallel CmpMap. Initialise a flag to
1633 indicate that we have not yet found any genuine simplification. */
1634 simpler = 0;
1635
1636 /* Initialise the index of the next Mapping to be added into each
1637 sublist. */
1638 jmap1 = jmap2 = 0;
1639
1640 /* Indicate both sublists are currently empty. */
1641 subout1 = subin2 = 0;
1642 new = submap1 = submap2 = NULL;
1643 subinv1 = subinv2 = 0;
1644
1645 /* Loop round untill all Mappings have been used. */
1646 while( jmap1 <= nmap1 && jmap2 <= nmap2 && astOK ) {
1647
1648 /* Note the number of outputs from submap1 and the number of inputs to
1649 submap2. If the Invert flag is not set to the required value for
1650 either Mapping, then inputs become outputs and vice-versa, so swap Nin
1651 and Nout. */
1652 if( !submap1 ) {
1653 subout1 = 0;
1654 } else if( subinv1 == astGetInvert( submap1 ) ) {
1655 subout1 = astGetNout( submap1 );
1656 } else {
1657 subout1 = astGetNin( submap1 );
1658 }
1659
1660 if( !submap2 ) {
1661 subin2 = 0;
1662 } else if( subinv2 == astGetInvert( submap2 ) ) {
1663 subin2 = astGetNin( submap2 );
1664 } else {
1665 subin2 = astGetNout( submap2 );
1666 }
1667
1668 /* If sublist for cmpmap1 has too few outputs, add the next Mapping from
1669 the cmpmap1 list into the submap1 sublist. */
1670 if( subout1 < subin2 ) {
1671 tmap = CombineMaps( submap1, subinv1,
1672 map_list1[ jmap1 ],
1673 invert_list1[ jmap1 ], 0, status );
1674 (void) astAnnul( submap1 );
1675 submap1 = tmap;
1676 subinv1 = 0;
1677 jmap1++;
1678
1679 /* If sublist for cmpmap2 has too few inputs, add the next Mapping from
1680 the cmpmap2 list into the submap2 sublist. */
1681 } else if( subin2 < subout1 ) {
1682 tmap = CombineMaps( submap2, subinv2,
1683 map_list2[ jmap2 ],
1684 invert_list2[ jmap2 ], 0, status );
1685 (void) astAnnul( submap2 );
1686 submap2 = tmap;
1687 subinv2 = 0;
1688 jmap2++;
1689
1690 /* If submap1 can now feed submap2, combine them in series, and attempt to
1691 simplify it. */
1692 } else {
1693
1694 /* Check this is not the first pass (when we do not have a submap1 or
1695 submap2). */
1696 if( submap1 && submap2 ) {
1697
1698 /* Combine the Mappings in series and simplify. */
1699 tmap = CombineMaps( submap1, subinv1, submap2,
1700 subinv2, 1, status );
1701 submap1 = astAnnul( submap1 );
1702 submap2 = astAnnul( submap2 );
1703 tmap2 = astSimplify( tmap );
1704 tmap = astAnnul( tmap );
1705
1706 /* Note if any simplification took place. */
1707 if( tmap != tmap2 ||
1708 astGetInvert( tmap ) != astGetInvert( tmap2 ) )
1709 simpler = 1;
1710
1711 /* Add the simplifed Mapping into the total merged Mapping (a parallel
1712 CmpMap). */
1713 if( !new ) {
1714 new = tmap2;
1715 } else {
1716 tmap = (AstMapping *) astCmpMap( new, tmap2, 0,
1717 " ", status );
1718 tmap2 = astAnnul( tmap2 );
1719 (void) astAnnul( new );
1720 new = tmap;
1721 }
1722 }
1723
1724 /* Reset submap1 to be the next Mapping from the cmpmap1 map list. First,
1725 save its old Invert flag and set it to the required value. */
1726 if( jmap1 < nmap1 ) {
1727 submap1 = astClone( map_list1[ jmap1 ] );
1728 subinv1 = invert_list1[ jmap1 ];
1729 jmap1++;
1730 } else {
1731 break;
1732 }
1733
1734 /* Do the same for the second list. */
1735 if( jmap2 < nmap2 ) {
1736 submap2 = astClone( map_list2[ jmap2 ] );
1737 subinv2 = invert_list2[ jmap2 ];
1738 jmap2++;
1739 } else {
1740 break;
1741 }
1742 }
1743 }
1744
1745 /* Free the lists of Mapping pointers and invert flags. */
1746 if( map_list1 ) {
1747 for( jmap1 = 0; jmap1 < nmap1; jmap1++ ) {
1748 map_list1[ jmap1 ] = astAnnul( map_list1[ jmap1 ] );
1749 }
1750 map_list1 = astFree( map_list1 );
1751 }
1752 invert_list1 = astFree( invert_list1 );
1753
1754 if( map_list2 ) {
1755 for( jmap2 = 0; jmap2 < nmap2; jmap2++ ) {
1756 map_list2[ jmap2 ] = astAnnul( map_list2[ jmap2 ] );
1757 }
1758 map_list2 = astFree( map_list2 );
1759 }
1760 invert_list2 = astFree( invert_list2 );
1761
1762 }
1763 }
1764
1765 /* Update Mapping list. */
1766 /* ==================== */
1767 /* If adjacent CmpMaps can be combined, then annul the original pointers. */
1768 if ( astOK && simpler ) {
1769 ( *map_list )[ imap1 ] = astAnnul( ( *map_list )[ imap1 ] );
1770 ( *map_list )[ imap2 ] = astAnnul( ( *map_list )[ imap2 ] );
1771
1772 /* Insert the pointer to the replacement CmpMap and initialise its
1773 invert flag. */
1774 ( *map_list )[ imap1 ] = new;
1775 ( *invert_list )[ imap1 ] = 0;
1776
1777 /* Loop to close the resulting gap by moving subsequent elements down
1778 in the arrays. */
1779 for ( imap = imap2 + 1; imap < *nmap; imap++ ) {
1780 ( *map_list )[ imap - 1 ] = ( *map_list )[ imap ];
1781 ( *invert_list )[ imap - 1 ] = ( *invert_list )[ imap ];
1782 }
1783
1784 /* Clear the vacated elements at the end. */
1785 ( *map_list )[ *nmap - 1 ] = NULL;
1786 ( *invert_list )[ *nmap - 1 ] = 0;
1787
1788 /* Decrement the Mapping count and return the index of the first
1789 modified element. */
1790 ( *nmap )--;
1791 result = imap1;
1792 }
1793 }
1794 }
1795 }
1796
1797 /* If we are merging the Mappings in series, and if the nominated CmpMap
1798 is a parallel CmpMap, and if the lower neighbour is a PermMap, it may
1799 be possible to swap the PermMap and the CmpMap. This may allow one of
1800 the two swapped Mappings to merge with its new neighbour.
1801 ==================================================================== */
1802
1803 /* Only do this if no simplification occurred above, and if the Mappings
1804 are being merged in series, and if the nominated Mapping is not the
1805 first in the list. */
1806 if( result == -1 && where > 0 ){
1807
1808 /* Obtain the indices of the two potential Mappings to be swapped. */
1809 imap1 = where - 1;
1810 imap2 = where;
1811
1812 /* Obtain a pointer to the CmpMap. */
1813 cmpmap2 = (AstCmpMap *) ( *map_list )[ imap2 ];
1814
1815 /* Obtain the Class string of the first (previous) Mapping and
1816 determine if it is a PermMap. Also check that the nominated Mapping is
1817 a parallel CmpMap. */
1818 class = astGetClass( ( *map_list )[ imap1 ] );
1819 if ( astOK && !strcmp( class, "PermMap" ) && !cmpmap2->series) {
1820
1821 /* Indicate we have no new Mapping to store. */
1822 new = NULL;
1823
1824 /* If suitable, obtain a pointer to the PermMap. */
1825 permmap1 = (AstPermMap *) ( *map_list )[ imap1 ];
1826
1827 /* Obtain the current values of the Invert attribute in the Mappings. */
1828 invert1 = astGetInvert( permmap1 );
1829 invert2 = astGetInvert( cmpmap2 );
1830
1831 /* Temporarily set the Invert attributes of both Mappings to the values
1832 supplied in the "invert_list" parameter. */
1833 astSetInvert( permmap1, ( *invert_list )[ imap1 ] );
1834 astSetInvert( cmpmap2, ( *invert_list )[ imap2 ] );
1835
1836 /* Get the number of inputs and outputs for the PermMap.*/
1837 npout = astGetNout( permmap1 );
1838 npin = astGetNin( permmap1 );
1839
1840 /* Get the number of inputs and outputs for the two components of the
1841 nominated parallel CmpMap. */
1842 nin2a = astGetNin( cmpmap2->map1 );
1843 nin2b = astGetNin( cmpmap2->map2 );
1844 nout2a = astGetNout( cmpmap2->map1 );
1845 nout2b = astGetNout( cmpmap2->map2 );
1846
1847 /* Get the input and output axis permutation arrays and the constants
1848 array from the PermMap */
1849 inperm =astGetInPerm( permmap1 );
1850 outperm =astGetOutPerm( permmap1 );
1851 conperm = astGetConstants( permmap1 );
1852
1853 /* In order to swap the Mappings, the PermMap outputs which feed the
1854 inputs of the first component of the parallel CmpMap must be copied
1855 from a contiguous block at the end of the list of PermMap inputs, or
1856 must all be assigned constant values. Likewise, the PermMap outputs which
1857 feed the inputs of the second component of the parallel CmpMap must be
1858 copied from a contiguous block at the beggining of the list of PermMap
1859 inputs or must be assigned constant values. Also, there must be a
1860 one-to-one correspondance between inputs and outputs in the PermMap.
1861 Check that the first block of nin2a PermMap outputs are copied from
1862 the last block of nin2a PermMap inputs (and vica-versa) or are constant. */
1863 canswap = ( npin == npout );
1864 aconstants = ( outperm[ 0 ] < 0 );
1865
1866 for( i = 0, j = npin - nin2a; i < nin2a; i++, j++ ) {
1867 if( aconstants ) {
1868 if( outperm[ i ] >= 0 ) {
1869 canswap = 0;
1870 break;
1871 }
1872
1873 } else if( outperm[ i ] != j || inperm[ j ] != i ) {
1874 canswap = 0;
1875 break;
1876 }
1877 }
1878
1879 /* Check that the first block of nin2b PermMap inputs are copied from
1880 the last block of nin2b PermMap outputs, and vica-versa. */
1881 bconstants = ( outperm[ nin2a ] < 0 );
1882 for( i = 0, j = npout - nin2b; i < nin2b; i++, j++ ) {
1883 if( bconstants ) {
1884 if( outperm[ j ] >= 0 ) {
1885 canswap = 0;
1886 break;
1887 }
1888 } else if( inperm[ i ] != j || outperm[ j ] != i ) {
1889 canswap = 0;
1890 break;
1891 }
1892 }
1893
1894 /* If the Mappings can be swapped.. */
1895 new_pm = NULL;
1896 new_cm = NULL;
1897 qa = NULL;
1898 qb = NULL;
1899 if( canswap ) {
1900
1901 /* Temporarily set the Invert attributes of the component Mappings to the
1902 values they had when the CmpMap was created. */
1903 invert2a = astGetInvert( cmpmap2->map1 );
1904 invert2b = astGetInvert( cmpmap2->map2 );
1905 astSetInvert( cmpmap2->map1, cmpmap2->invert1 );
1906 astSetInvert( cmpmap2->map2, cmpmap2->invert2 );
1907
1908 /* If any PermMap outputs are constant, we will need the results of
1909 transforming these constants using the CmpMap which follows. */
1910 if( aconstants || bconstants ) {
1911
1912 /* Transform a set of bad inputs using the PermMap. This will assign the
1913 PermMap constant to any fixed outputs. */
1914 p = astMalloc( sizeof( double )*(size_t) npin );
1915 q = astMalloc( sizeof( double )*(size_t) npout );
1916 qa = astMalloc( sizeof( double )*(size_t) nout2a );
1917 qb = astMalloc( sizeof( double )*(size_t) nout2b );
1918 if( astOK ) {
1919 for( i = 0; i < npin; i++ ) p[ i ] = AST__BAD;
1920 astTranN( permmap1, 1, npin, 1, p, 1, npout, 1, q );
1921
1922 /* Transform the PermMap outputs using the two component Mappings in the
1923 CmpMap. */
1924 astTranN( cmpmap2->map1, 1, nin2a, 1, q, 1, nout2a, 1, qa );
1925 astTranN( cmpmap2->map2, 1, nin2b, 1, q + nin2a, 1, nout2b, 1, qb );
1926
1927 }
1928 p = astFree( p );
1929 q = astFree( q );
1930 }
1931
1932 /* If necessary, create a UnitMap to replace a Mapping which has constant
1933 outputs. The number of axes for the UnitMap is chosen to give the
1934 correct total number of inputs for the final parallel CmpMap. At the
1935 same time determine the number of inputs needed by the final PermMap. */
1936 if( aconstants ) {
1937 nunit = npin - nin2b;
1938 npin_new = nout2b + nunit;
1939 } else if( bconstants ) {
1940 nunit = npin - nin2a;
1941 npin_new = nout2a + nunit;
1942 } else {
1943 nunit = 0;
1944 npin_new = nout2a + nout2b;
1945 }
1946 unit = nunit ? astUnitMap( nunit, "", status ) : NULL;
1947
1948 /* Determine the number of outputs for the final PermMap and allocate memory
1949 for its permutation arrays. */
1950 npout_new = nout2a + nout2b;
1951 outperm_new = astMalloc( sizeof( int )*(size_t) npout_new );
1952 inperm_new = astMalloc( sizeof( int )*(size_t) npin_new );
1953 const_new = astMalloc( sizeof( double )*(size_t) ( npout_new + npin_new ) );
1954 if( astOK ) {
1955 oconid = 0;
1956
1957 /* First assign permutations for the second component Mapping, if used. */
1958 if( !bconstants ) {
1959 for( i = 0, j = npout_new - nout2b; i < nout2b; i++,j++ ) {
1960 inperm_new[ i ] = j;
1961 outperm_new[ j ] = i;
1962 }
1963
1964 /* Otherwise, store constants */
1965 } else {
1966
1967 for( i = 0; i < nunit; i++ ){
1968 iconid = inperm[ i ];
1969 if( iconid >= npout ) {
1970 inperm_new[ i ] = npout_new;
1971
1972 } else if( iconid >= 0 ) {
1973 astError( AST__INTER, "astMapMerge(CmpMap): Swapped PermMap "
1974 "input is not constant (internal AST programming "
1975 "error)." , status);
1976 break;
1977
1978 } else {
1979 inperm_new[ i ] = --oconid;
1980 const_new[ -( oconid + 1 ) ] = conperm[ -( iconid + 1 ) ];
1981 }
1982 }
1983
1984 for( i = 0, j = npout_new - nout2b; i < nout2b; i++,j++ ) {
1985 outperm_new[ j ] = --oconid;
1986 const_new[ -( oconid + 1 ) ] = qb[ i ];
1987 }
1988
1989 }
1990
1991 /* Now assign permutations for the first component Mapping, if used. */
1992 if( !aconstants ) {
1993 for( i = 0, j = npin_new - nout2a; i < nout2a; i++,j++ ) {
1994 inperm_new[ j ] = i;
1995 outperm_new[ i ] = j;
1996 }
1997
1998 /* Otherwise, store constants */
1999 } else {
2000
2001 for( i = nout2b; i < npin_new; i++ ){
2002 iconid = inperm[ i - nout2b + nin2b ];
2003 if( iconid >= npout ) {
2004 inperm_new[ i ] = npout_new;
2005
2006 } else if( iconid >= 0 ) {
2007 astError( AST__INTER, "astMapMerge(CmpMap): Swapped PermMap "
2008 "input is not constant (internal AST programming "
2009 "error)." , status);
2010 break;
2011
2012 } else {
2013 inperm_new[ i ] = --oconid;
2014 const_new[ -( oconid + 1 ) ] = conperm[ -( iconid + 1 ) ];
2015 }
2016 }
2017
2018 for( i = 0; i < nout2a; i++ ) {
2019 outperm_new[ i ] = --oconid;
2020 const_new[ -( oconid + 1 ) ] = qa[ i ];
2021 }
2022
2023 }
2024
2025 /* Create the new PermMap */
2026 new_pm = astPermMap( npin_new, inperm_new, npout_new,
2027 outperm_new, const_new, "", status );
2028
2029 /* Create the new CmpMap.*/
2030 if( aconstants ) {
2031 if( unit ) {
2032 new_cm = astCmpMap( cmpmap2->map2, unit, 0, "", status );
2033 } else {
2034 new_cm = astCopy( cmpmap2->map2 );
2035 }
2036
2037 } else if( bconstants ) {
2038 if( unit ) {
2039 new_cm = astCmpMap( unit, cmpmap2->map1, 0, "", status );
2040 } else {
2041 new_cm = astCopy( cmpmap2->map1 );
2042 }
2043
2044 } else{
2045 new_cm = astCmpMap( cmpmap2->map2, cmpmap2->map1, 0, "", status );
2046 }
2047
2048 }
2049
2050 /* Free Memory. */
2051 if( unit ) unit = astAnnul( unit );
2052 outperm_new = astFree( outperm_new );
2053 inperm_new = astFree( inperm_new );
2054 const_new = astFree( const_new );
2055 if( aconstants || bconstants ) {
2056 qa = astFree( qa );
2057 qb = astFree( qb );
2058 }
2059
2060 /* Re-instate the original Invert attributes in the component Mappings. */
2061 astSetInvert( cmpmap2->map1, invert2a );
2062 astSetInvert( cmpmap2->map2, invert2b );
2063
2064 }
2065
2066 /* Release the arrays holding the input and output permutation arrays
2067 and constants copied from the PermMap. */
2068 inperm = astFree( inperm );
2069 outperm = astFree( outperm );
2070 conperm = astFree( conperm );
2071
2072 /* Re-instate the original values of the Invert attributes of both
2073 Mappings. */
2074 astSetInvert( permmap1, invert1 );
2075 astSetInvert( cmpmap2, invert2 );
2076
2077 /* If the Mappings can be swapped... */
2078 if( astOK && canswap ) {
2079
2080 /* Annul the supplied pointer to the two Mappings. */
2081 ( *map_list )[ imap1 ] = astAnnul( ( *map_list )[ imap1 ] );
2082 ( *map_list )[ imap2 ] = astAnnul( ( *map_list )[ imap2 ] );
2083
2084 /* Store the new PermMap pointer in the slot previously occupied by the
2085 nominated CmpMap pointer. Likewise, store the invert flag. */
2086 ( *map_list )[ imap2 ] = (AstMapping *) new_pm;
2087 ( *invert_list )[ imap2 ] = astGetInvert( new_pm );
2088
2089 /* Store the new PermMap pointer in the slot previously occupied by the
2090 nominated CmpMap pointer. Likewise, store the invert flag. */
2091 ( *map_list )[ imap1 ] = (AstMapping *) new_cm;
2092 ( *invert_list )[ imap1 ] = astGetInvert( new_cm );
2093
2094 /* Return the index of the first modified element. */
2095 result = imap1;
2096
2097 }
2098 }
2099 }
2100
2101 /* If an error occurred, clear the result value. */
2102 if ( !astOK ) result = -1;
2103
2104 /* Return the result. */
2105 return result;
2106 }
2107
MapSplit1(AstMapping * this,int nin,const int * in,AstMapping ** map,int * status)2108 static int *MapSplit1( AstMapping *this, int nin, const int *in, AstMapping **map, int *status ){
2109 /*
2110 * Name:
2111 * MapSplit1
2112
2113 * Purpose:
2114 * Create a Mapping representing a subset of the inputs of an existing
2115 * Mapping.
2116
2117 * Type:
2118 * Private function.
2119
2120 * Synopsis:
2121 * #include "cmpmap.h"
2122 * int *MapSplit1( AstMapping *this, int nin, const int *in, AstMapping **map )
2123
2124 * Class Membership:
2125 * CmpMap method
2126
2127 * Description:
2128 * This function performs the work for the astMapSplit method. It
2129 * first invokes the astMapSplit method to see if the forward
2130 * transformation of the supplied Mapping (not necessarily a CmpMap)
2131 * can be split as requested. If this is not possible it invokes MapSplit2
2132 * which attempts an inverse approach to the problem. For each possible
2133 * sub-sets of the Mapping outputs it call astMapSplit to see if the
2134 * sub-set of outputs are generated from the selected inputs.
2135
2136 * Parameters:
2137 * this
2138 * Pointer to the Mapping to be split. It is not assumed to be a CmpMap.
2139 * nin
2140 * The number of inputs to pick from "this".
2141 * in
2142 * Pointer to an array of indices (zero based) for the inputs which
2143 * are to be picked. This array should have "nin" elements. If "Nin"
2144 * is the number of inputs of the supplied Mapping, then each element
2145 * should have a value in the range zero to Nin-1.
2146 * map
2147 * Address of a location at which to return a pointer to the new
2148 * Mapping. This Mapping will have "nin" inputs (the number of
2149 * outputs may be different to "nin"). A NULL pointer will be
2150 * returned if the supplied Mapping has no subset of outputs which
2151 * depend only on the selected inputs.
2152
2153 * Returned Value:
2154 * A pointer to a dynamically allocated array of ints. The number of
2155 * elements in this array will equal the number of outputs for the
2156 * returned Mapping. Each element will hold the index of the
2157 * corresponding output in the supplied Mapping. The array should be
2158 * freed using astFree when no longer needed. A NULL pointer will
2159 * be returned if no output Mapping can be created.
2160
2161 * Notes:
2162 * - If this function is invoked with the global error status set,
2163 * or if it should fail for any reason, then NULL values will be
2164 * returned as the function value and for the "map" pointer.
2165 */
2166
2167 /* Local Variables: */
2168 int *result; /* Axis order to return */
2169
2170 /* Initialise */
2171 result = NULL;
2172 *map = NULL;
2173
2174 /* Check the global error status. */
2175 if ( !astOK ) return result;
2176
2177 /* First see if the forward transformation can be split as requested. */
2178 result = astMapSplit( this, nin, in, map );
2179
2180 /* If forward transformation could not be split, we attempt to split the
2181 inverse transformation by selecting every possible sub-set of Mapping
2182 outputs until one is found which is fed by the requested mapping inputs. */
2183 if( !result ) result = MapSplit2( this, nin, in, map, status );
2184
2185 /* Free returned resources if an error has occurred. */
2186 if( !astOK ) {
2187 result = astFree( result );
2188 *map = astAnnul( *map );
2189 }
2190
2191 /* Return the list of output indices. */
2192 return result;
2193 }
2194
MapSplit2(AstMapping * this,int nin,const int * in,AstMapping ** map,int * status)2195 static int *MapSplit2( AstMapping *this, int nin, const int *in, AstMapping **map, int *status ){
2196 /*
2197 * Name:
2198 * MapSplit2
2199
2200 * Purpose:
2201 * Create a Mapping representing a subset of the inputs of an existing
2202 * Mapping.
2203
2204 * Type:
2205 * Private function.
2206
2207 * Synopsis:
2208 * #include "cmpmap.h"
2209 * int *MapSplit2( AstMapping *this, int nin, const int *in, AstMapping **map )
2210
2211 * Class Membership:
2212 * CmpMap method
2213
2214 * Description:
2215 * This function attempts to split the supplied Mapping using an
2216 * inverse approach to the problem. For each possible sub-sets of the
2217 * Mapping outputs it call astMapSplit to see if the sub-set of outputs
2218 * are generated from the selected inputs.
2219
2220 * Parameters:
2221 * this
2222 * Pointer to the Mapping to be split. It is not assumed to be a CmpMap.
2223 * nin
2224 * The number of inputs to pick from "this".
2225 * in
2226 * Pointer to an array of indices (zero based) for the inputs which
2227 * are to be picked. This array should have "nin" elements. If "Nin"
2228 * is the number of inputs of the supplied Mapping, then each element
2229 * should have a value in the range zero to Nin-1.
2230 * map
2231 * Address of a location at which to return a pointer to the new
2232 * Mapping. This Mapping will have "nin" inputs (the number of
2233 * outputs may be different to "nin"). A NULL pointer will be
2234 * returned if the supplied Mapping has no subset of outputs which
2235 * depend only on the selected inputs.
2236
2237 * Returned Value:
2238 * A pointer to a dynamically allocated array of ints. The number of
2239 * elements in this array will equal the number of outputs for the
2240 * returned Mapping. Each element will hold the index of the
2241 * corresponding output in the supplied Mapping. The array should be
2242 * freed using astFree when no longer needed. A NULL pointer will
2243 * be returned if no output Mapping can be created.
2244
2245 * Notes:
2246 * - If this function is invoked with the global error status set,
2247 * or if it should fail for any reason, then NULL values will be
2248 * returned as the function value and for the "map" pointer.
2249 */
2250
2251 /* Local Variables: */
2252 AstMapping *map2; /* Subset Mapping */
2253 AstMapping *this2; /* Inverted copy of the supplied Mapping */
2254 int *out; /* Selected output indices */
2255 int *result; /* Axis order to return */
2256 int *result2; /* Axis order for current output subset */
2257 int i; /* Loop count */
2258 int iscmp; /* Is "this" a CmpMap? */
2259 int j; /* Loop count */
2260 int mout; /* Number of selected outputs */
2261 int nin2; /* Number of inputs fed by current outputs */
2262 int nout; /* The number of outputs from the supplied Mapping */
2263 int ok; /* Are all required inputs fed by current outputs? */
2264
2265 /* Initialise */
2266 result = NULL;
2267 *map = NULL;
2268
2269 /* Check the global error status. */
2270 if ( !astOK ) return result;
2271
2272 /* Get the number of Mapping outputs. */
2273 nout = astGetNout( this );
2274
2275 /* Get an inverted copy of the Mapping. We do this rather than inverting
2276 the supplied Maping in case an error occurs which may leave the
2277 supplied Mapping inverted. */
2278 this2 = astCopy( this );
2279 astInvert( this2 );
2280
2281 /* Note if the Mapping is a CmpMap. */
2282 iscmp = astIsACmpMap( this );
2283
2284 /* Allocate memory to hold the selected output indices. */
2285 out = astMalloc( nout*sizeof( int ) );
2286
2287 /* Loop round all useful subset sizes. */
2288 if( out ) {
2289 for( mout = 1; mout < nout && !result; mout++ ) {
2290
2291 /* Initialise the first subset of outputs to check at the current subset
2292 size. */
2293 for( i = 0; i < mout; i++ ) out[ i ] = 0;
2294
2295 /* Loop round all ways of picking a subset of "mout" outputs from the total
2296 available "nout" outputs. */
2297 while( ! result ) {
2298
2299 /* Skip this subset if it refers to any axis index more than once. */
2300 ok = 1;
2301 for( i = 1; i < mout && ok; i++ ) {
2302 for( j = 0; j < i; j++ ) {
2303 if( out[ i ] == out[ j ] ) {
2304 ok = 0;
2305 break;
2306 }
2307 }
2308 }
2309 if( ok ) {
2310
2311 /* Attempt to split the inverted Mapping using the current subset of
2312 outputs. Take care to avoid an infinite loop if "this" is a CmpMap. */
2313 if( iscmp ) {
2314 result2 = MapSplit0( this2, mout, out, &map2, 1, status );
2315 } else {
2316 result2 = astMapSplit( this2, mout, out, &map2 );
2317 }
2318
2319 /* If succesful... */
2320 if( result2 ) {
2321
2322 /* See if the inputs that feed the current subset of outputs are the same
2323 as the inputs specified by the caller (and in the same order). */
2324 nin2 = astGetNout( map2 );
2325 ok = ( nin2 == nin );
2326 if( ok ) {
2327 for( i = 0; i < nin; i++ ) {
2328 if( in[ i ] != result2[ i ] ) {
2329 ok = 0;
2330 break;
2331 }
2332 }
2333 }
2334
2335 /* If so, set up the values returned to the caller. */
2336 if( ok ) {
2337 result = astStore( result, out, mout*sizeof(int) );
2338 astInvert( map2 );
2339 *map = astClone( map2 );
2340 }
2341
2342 /* Free resources. */
2343 result2 = astFree( result2 );
2344 map2 = astAnnul( map2 );
2345 }
2346 }
2347
2348 /* Increment the first axis index. */
2349 i = 0;
2350 out[ i ]++;
2351
2352 /* If the incremented axis index is now too high, reset it to zero and
2353 increment the next higher axis index. Do this until an incremented axis
2354 index is not too high. */
2355 while( out[ i ] == nout ) {
2356 out[ i++ ] = 0;
2357
2358 if( i < mout ) {
2359 out[ i ]++;
2360 } else {
2361 break;
2362 }
2363 }
2364
2365 /* If all subsets have been checked break out of the loop. */
2366 if( i == mout ) break;
2367
2368 }
2369 }
2370 }
2371
2372 /* Free resources. */
2373 out = astFree( out );
2374 this2 = astAnnul( this2 );
2375
2376 /* Free returned resources if an error has occurred. */
2377 if( !astOK ) {
2378 result = astFree( result );
2379 *map = astAnnul( *map );
2380 }
2381
2382 /* Return the list of output indices. */
2383 return result;
2384 }
2385
MapSplit0(AstMapping * this_mapping,int nin,const int * in,AstMapping ** map,int reentry,int * status)2386 static int *MapSplit0( AstMapping *this_mapping, int nin, const int *in,
2387 AstMapping **map, int reentry, int *status ){
2388 /*
2389 * Name:
2390 * MapSplit0
2391
2392 * Purpose:
2393 * Create a Mapping representing a subset of the inputs of an existing
2394 * CmpMap.
2395
2396 * Type:
2397 * Private function.
2398
2399 * Synopsis:
2400 * #include "cmpmap.h"
2401 * int *MapSplit0( AstMapping *this, int nin, const int *in,
2402 * AstMapping **map, int reentry, int *status )
2403
2404 * Class Membership:
2405 * CmpMap method
2406
2407 * Description:
2408 * This function creates a new Mapping by picking specified inputs from
2409 * an existing CmpMap. This is only possible if the specified inputs
2410 * correspond to some subset of the CmpMap outputs. That is, there
2411 * must exist a subset of the CmpMap outputs for which each output
2412 * depends only on the selected CmpMap inputs, and not on any of the
2413 * inputs which have not been selected. If this condition is not met
2414 * by the supplied CmpMap, then a NULL Mapping is returned.
2415
2416 * Parameters:
2417 * this
2418 * Pointer to the CmpMap to be split (the CmpMap is not actually
2419 * modified by this function).
2420 * nin
2421 * The number of inputs to pick from "this".
2422 * in
2423 * Pointer to an array of indices (zero based) for the inputs which
2424 * are to be picked. This array should have "nin" elements. If "Nin"
2425 * is the number of inputs of the supplied CmpMap, then each element
2426 * should have a value in the range zero to Nin-1.
2427 * map
2428 * Address of a location at which to return a pointer to the new
2429 * Mapping. This Mapping will have "nin" inputs (the number of
2430 * outputs may be different to "nin"). A NULL pointer will be
2431 * returned if the supplied CmpMap has no subset of outputs which
2432 * depend only on the selected inputs.
2433 * reentry
2434 * Set to zero if this is a top level entry, and non-zero if it is
2435 * a recursive entry.
2436 * status
2437 * Pointer to the inherited status variable.
2438
2439 * Returned Value:
2440 * A pointer to a dynamically allocated array of ints. The number of
2441 * elements in this array will equal the number of outputs for the
2442 * returned Mapping. Each element will hold the index of the
2443 * corresponding output in the supplied CmpMap. The array should be
2444 * freed using astFree when no longer needed. A NULL pointer will
2445 * be returned if no output Mapping can be created.
2446
2447 * Notes:
2448 * - If this function is invoked with the global error status set,
2449 * or if it should fail for any reason, then NULL values will be
2450 * returned as the function value and for the "map" pointer.
2451 */
2452
2453 /* Local Variables: */
2454 AstCmpMap *this;
2455 AstMapping **map_list;
2456 AstMapping *amap;
2457 AstMapping *bmap;
2458 AstPermMap *pmap;
2459 int *aout;
2460 int *cin;
2461 int *cout;
2462 int *inp;
2463 int *invert_list;
2464 int *outp;
2465 int *p;
2466 int *result;
2467 int doperm;
2468 int i;
2469 int ibot;
2470 int ibotout;
2471 int iin;
2472 int imap;
2473 int iout;
2474 int itop;
2475 int j;
2476 int naout;
2477 int ncin;
2478 int ncout;
2479 int nmap;
2480 int npin;
2481 int npout;
2482 int ok;
2483 int old_inv;
2484 int t;
2485
2486
2487 /* Initialise */
2488 result = NULL;
2489 *map = NULL;
2490
2491 /* Check the global error status. */
2492 if ( !astOK ) return result;
2493
2494 /* Get a pointer to the CmpMap structure. */
2495 this = (AstCmpMap *) this_mapping;
2496
2497 /* Get the number of inputs and outputs in the supplied CmpMap. */
2498 npin = astGetNin( this );
2499 npout = astGetNout( this );
2500
2501 /* Check all input axis indices are valid. */
2502 ok = 1;
2503 for( i = 0; i < nin; i++ ) {
2504 if( in[ i ] < 0 || in[ i ] >= npin ) {
2505 ok = 0;
2506 break;
2507 }
2508 }
2509
2510 /* If OK, proceed. */
2511 if( ok ) {
2512
2513 /* Initialise dynamic arrays of Mapping pointers and associated Invert
2514 flags. */
2515 nmap = 0;
2516 map_list = NULL;
2517 invert_list = NULL;
2518
2519 /* Decompose the CmpMap into a sequence of Mappings to be applied in
2520 series or parallel, as appropriate, and an associated list of
2521 Invert flags. */
2522 (void) astMapList( this_mapping, this->series, astGetInvert( this ),
2523 &nmap, &map_list, &invert_list );
2524
2525 /* First handle lists of Mapping in series. */
2526 if( this->series ) {
2527
2528 /* Initialise the array of inputs to be split from the next component
2529 Mapping. */
2530 ncin = nin;
2531 cin = astStore( NULL, in, sizeof( int )*nin );
2532
2533 /* Loop round all the component Mappings that are combined in series to form
2534 the supplied CmpMap. */
2535 for( imap = 0; imap < nmap && cin; imap++ ) {
2536
2537 /* Temporarily reset the Invert attribute within the commponent Mapping back
2538 to the value it had when the CmpMap was created. */
2539 old_inv = astGetInvert( map_list[ imap ] );
2540 astSetInvert( map_list[ imap ], invert_list[ imap ] );
2541
2542 /* Attempt to split the component Mapping using the current list of
2543 inputs. */
2544 cout = MapSplit1( map_list[ imap ], ncin, cin, &amap, status );
2545
2546 /* If the split could be done... */
2547 if( amap ) {
2548
2549 /* The outputs that correspond to the picked inputs become the inputs to
2550 be picked from the next component Mapping. */
2551 (void) astFree( cin );
2552 cin = cout;
2553 ncin = astGetNout( amap );
2554
2555 /* Combine the split Mapping in series with the earlier split Mappings. */
2556 if( *map ) {
2557 bmap = (AstMapping *) astCmpMap( *map, amap, 1, " ", status );
2558 amap = astAnnul( amap );
2559 (void) astAnnul( *map );
2560 *map = bmap;
2561 } else {
2562 *map = amap;
2563 }
2564
2565 /* If the split could not be done, free the array of Mapping inputs to
2566 indicate that no more component Mappings need be checked. */
2567 } else {
2568 cin = astFree( cin );
2569 cout = astFree( cout );
2570 }
2571
2572 /* Re-instate the original value of the Invert attribute within the
2573 commponent Mapping. */
2574 astSetInvert( map_list[ imap ], old_inv );
2575 }
2576
2577 /* Return the final array of output indices. */
2578 result = cin;
2579
2580 /* Now handle lists of Mapping in parallel. */
2581 } else {
2582
2583 /* Allocate work space. */
2584 outp = astMalloc( sizeof(int)*(size_t)nin );
2585 inp = astMalloc( sizeof(int)*(size_t)nin );
2586 cin = astMalloc( sizeof(int)*(size_t)npin );
2587 cout = astMalloc( sizeof(int)*(size_t)npout );
2588 if( astOK ) {
2589
2590 /* The caller may have selected the Mapping inputs in any order, so we
2591 need to create a PermMap which will permute the inputs from the
2592 requested order to the order used by the CmpMap. First fill the outperm
2593 work array with its own indices. */
2594 for( i = 0; i < nin; i++ ) outp[ i ] = i;
2595
2596 /* Sort the outperm work array so that it accesses the array of input indices
2597 in ascending order */
2598 for( j = nin - 1; j > 0; j-- ) {
2599 p = outp;
2600 for( i = 0; i < j; i++,p++ ) {
2601 if( in[ p[0] ] > in[ p[1] ] ) {
2602 t = p[0];
2603 p[0] = p[1];
2604 p[1] = t;
2605 }
2606 }
2607 }
2608
2609 /* Create the inperm array which is the inverse of the above outperm
2610 array. Note if the permutation is necessary. */
2611 doperm = 0;
2612 for( i = 0; i < nin; i++ ) {
2613 if( outp[ i ] != i ) doperm = 1;
2614 inp[ outp[ i ] ] = i;
2615 }
2616
2617 /* Create a PermMap which reorders the inputs into ascending order. */
2618 pmap = doperm ? astPermMap( nin, inp, nin, outp, NULL, "", status ) : NULL;
2619
2620 /* Store the sorted input indices in the inp work array. */
2621 for( i = 0; i < nin; i++ ) {
2622 inp[ i ] = in[ outp[ i ] ];
2623 }
2624
2625 /* Initialise the index within the supplied CmpMap of the last (highest)
2626 input in the current component Mapping. */
2627 itop = -1;
2628
2629 /* Initialise the index within the supplied CmpMap of the first (lowest)
2630 output for the current component Mapping. */
2631 ibotout = 0;
2632
2633 /* Initialise the index within the supplied CmpMap of the current picked input. */
2634 iin = 0;
2635
2636 /* Initialise the index of the next returned output index. */
2637 ncout = 0;
2638
2639 /* Loop round all the component Mappings that are combined in series to form
2640 the supplied CmpMap. */
2641 for( imap = 0; imap < nmap && cout; imap++ ) {
2642
2643 /* Temporarily reset the Invert attribute within the component Mapping back
2644 to the value it had when the CmpMap was created. */
2645 old_inv = astGetInvert( map_list[ imap ] );
2646 astSetInvert( map_list[ imap ], invert_list[ imap ] );
2647
2648 /* Get the index within the supplied CmpMap of the first (lowest) input in
2649 the current component Mapping. */
2650 ibot = itop + 1;
2651
2652 /* Get the index within the supplied CmpMap of the last (highest) input in
2653 the current component Mapping. */
2654 itop += astGetNin( map_list[ imap ] );
2655
2656 /* Get the zero-based indicies of the required inputs that feed the current
2657 component Mapping. */
2658 ncin = 0;
2659 while( iin < nin && inp[ iin ] <= itop ) {
2660 cin[ ncin++ ] = inp[ iin++ ] - ibot;
2661 }
2662
2663 /* Skip components from which no inputs are being picked. */
2664 if( ncin > 0 ) {
2665
2666 /* Attempt to split the component Mapping using the current list of inputs. */
2667 aout = MapSplit1( map_list[ imap ], ncin, cin, &amap,
2668 status );
2669
2670 /* If successful... */
2671 if( amap ) {
2672
2673 /* Correct the output indices so that they refer to the numbering scheme
2674 of the total CmpMap, and append to the total list of output indices. */
2675 naout = astGetNout( amap );
2676 for( iout = 0; iout < naout; iout++ ) {
2677 cout[ ncout++ ] = aout[ iout ] + ibotout;
2678 }
2679
2680 /* Combine the split Mapping in parallel with the earlier split Mappings. */
2681 if( *map ) {
2682 bmap = (AstMapping *) astCmpMap( *map, amap, 0, " ",
2683 status );
2684 amap = astAnnul( amap );
2685 (void) astAnnul( *map );
2686 *map = bmap;
2687 } else {
2688 *map = amap;
2689 }
2690
2691 /* If the component Mapping could not be split, free the cout array to
2692 indicate that no more component Mappings need be considered. */
2693 } else {
2694 cout = astFree( cout );
2695 }
2696
2697 /* Free remaining resources. */
2698 aout = astFree( aout );
2699 }
2700
2701 /* Update the index within the supplied CmpMap of the first (lowest) output in
2702 the next component Mapping. */
2703 ibotout += astGetNout( map_list[ imap ] );
2704
2705 /* Re-instate the original value of the Invert attribute within the
2706 commponent Mapping. */
2707 astSetInvert( map_list[ imap ], old_inv );
2708 }
2709
2710 /* If the requested inputs could be split from the total CmpMap, add in any
2711 PermMap needed to re-order the inputs. */
2712 if( cout && ncout ){
2713 if( doperm ) {
2714 bmap = (AstMapping *) astCmpMap( pmap, *map, 1, "", status );
2715 (void) astAnnul( *map );
2716 *map = bmap;
2717 }
2718
2719 /* Also return the list of output indices. */
2720 result = cout;
2721 cout = NULL;
2722 }
2723
2724 /* Free remaining resources. */
2725 if( pmap ) pmap = astAnnul( pmap );
2726 }
2727 outp = astFree( outp );
2728 inp = astFree( inp );
2729 cin = astFree( cin );
2730 cout = astFree( cout );
2731 }
2732
2733 /* Loop to annul all the Mapping pointers in the list. */
2734 for ( i = 0; i < nmap; i++ ) map_list[ i ] = astAnnul( map_list[ i ] );
2735
2736 /* Free the dynamic arrays. */
2737 map_list = astFree( map_list );
2738 invert_list = astFree( invert_list );
2739
2740 }
2741
2742 /* Mappings that have no outputs cannot be used. */
2743 if( !result && *map ) *map = astAnnul( *map );
2744
2745 /* If the above method failed to split the CmpMap, we attempt to split the
2746 inverse transformation by selecting every possible sub-set of Mapping
2747 outputs until one is found which is fed by the requested mapping inputs. */
2748 if( !result && !reentry ) result = MapSplit2( this_mapping, nin, in, map,
2749 status );
2750
2751 /* Free returned resources if an error has occurred. */
2752 if( !astOK ) {
2753 result = astFree( result );
2754 *map = astAnnul( *map );
2755 }
2756
2757 /* Return the list of output indices. */
2758 return result;
2759 }
2760
MapSplit(AstMapping * this,int nin,const int * in,AstMapping ** map,int * status)2761 static int *MapSplit( AstMapping *this, int nin, const int *in,
2762 AstMapping **map, int *status ){
2763 /*
2764 * Name:
2765 * MapSplit
2766
2767 * Purpose:
2768 * Create a Mapping representing a subset of the inputs of an existing
2769 * CmpMap.
2770
2771 * Type:
2772 * Private function.
2773
2774 * Synopsis:
2775 * #include "cmpmap.h"
2776 * int *MapSplit( AstMapping *this, int nin, const int *in,
2777 * AstMapping **map, int *status )
2778
2779 * Class Membership:
2780 * CmpMap method (over-rides the protected astMapSplit method
2781 * inherited from the Mapping class).
2782
2783 * Description:
2784 * This function is the main entry point for the astMapSplit method.
2785 * It is a simple wrapper for MapSplit0 which calls MapSplit0
2786 * indicating that this is a top-level entry.
2787
2788 * Parameters:
2789 * this
2790 * Pointer to the CmpMap to be split (the CmpMap is not actually
2791 * modified by this function).
2792 * nin
2793 * The number of inputs to pick from "this".
2794 * in
2795 * Pointer to an array of indices (zero based) for the inputs which
2796 * are to be picked. This array should have "nin" elements. If "Nin"
2797 * is the number of inputs of the supplied CmpMap, then each element
2798 * should have a value in the range zero to Nin-1.
2799 * map
2800 * Address of a location at which to return a pointer to the new
2801 * Mapping. This Mapping will have "nin" inputs (the number of
2802 * outputs may be different to "nin"). A NULL pointer will be
2803 * returned if the supplied CmpMap has no subset of outputs which
2804 * depend only on the selected inputs.
2805 * status
2806 * Pointer to the inherited status variable.
2807
2808 * Returned Value:
2809 * A pointer to a dynamically allocated array of ints. The number of
2810 * elements in this array will equal the number of outputs for the
2811 * returned Mapping. Each element will hold the index of the
2812 * corresponding output in the supplied CmpMap. The array should be
2813 * freed using astFree when no longer needed. A NULL pointer will
2814 * be returned if no output Mapping can be created.
2815
2816 * Notes:
2817 * - If this function is invoked with the global error status set,
2818 * or if it should fail for any reason, then NULL values will be
2819 * returned as the function value and for the "map" pointer.
2820 */
2821 return MapSplit0( this, nin, in, map, 0, status );
2822 }
2823
PatternCheck(int val,int check,int ** list,int * list_len,int * status)2824 static int PatternCheck( int val, int check, int **list, int *list_len, int *status ){
2825 /*
2826 * Name:
2827 * Looping
2828
2829 * Purpose:
2830 * Check for repeating patterns in a set of integer values.
2831
2832 * Type:
2833 * Private function.
2834
2835 * Synopsis:
2836 * #include "cmpmap.h"
2837 * int PatternCheck( int val, int nmap, int **mlist, int **nlist, int *list_len )
2838
2839 * Class Membership:
2840 * CmpMap member function.
2841
2842 * Description:
2843 * This function appends a supplied integer to a dynamic list, creating
2844 * or expanding the list if necessary.It then optionally, check the
2845 * list for evidence of repeating patterns. If such a pattern is
2846 * found, its wavelength is returned.
2847
2848 * Parameters:
2849 * val
2850 * The integer value to add to the list.
2851 * check
2852 * Should a check for reating patterns be performed?
2853 * list
2854 * Address of a location at which is stored a pointer to an array
2855 * holding the values supplied on previous invocations of this
2856 * function. If a NULL pointer is supplied a new array is allocated.
2857 * On exit, the supplied value is appended to the end of the array. The
2858 * array is extended as necessary. The returned pointer should be
2859 * freed using astFree when no longer needed.
2860 * list_len
2861 * Address of a location at which is stored the number of elements
2862 * in the "list" array.
2863
2864 * Returned Value:
2865 * A non-zero "wavelength" value is returned if there is a repeating
2866 * pattern is found in the "list" array. Otherwise, zero is returned.
2867 * The "wavelength" is the number of integer values which constitute a
2868 * single instance of the pattern.
2869
2870 * Notes:
2871 * - A value of 1 is returned if this function is invoked with the AST
2872 * error status set, or if it should fail for any reason.
2873 */
2874
2875 /* Local Variables: */
2876 int *wave[ 30 ]; /* Pointers to start of waves */
2877 int iat; /* Index of elements added by this invocation */
2878 int jat; /* Index of element condiered next */
2879 int jlo; /* Earliest "mlist" entry to consider */
2880 int k; /* Index of element within pattern */
2881 int mxwave; /* Max pattern length to consider */
2882 int iwave; /* Index of current wave */
2883 int nwave; /* Number of waves required to mark a pattern */
2884 int result; /* Returned flag */
2885 int wavelen; /* Current pattern length */
2886
2887 /* Check the global status. */
2888 if ( !astOK ) return 1;
2889
2890 /* Initialise */
2891 result = 0;
2892
2893 /* If no array has been supplied, create a new array. */
2894 if( !(*list) ) {
2895 *list = astMalloc( 100*sizeof( int ) );
2896 *list_len = 0;
2897 }
2898
2899 /* Store the new value in the array, extending it if necessary. */
2900 iat = (*list_len)++;
2901 *list = astGrow( *list, *list_len, sizeof( int ) );
2902 if( astOK ) {
2903 (*list)[ iat ] = val;
2904
2905 /* If required, determine the maximum "wavelength" for looping patterns to be
2906 checked, and store the earliest list entry to consider. We take 3 complete
2907 patterns as evidence of looping, but we only do the check when the
2908 list length is at least 30. */
2909 if( check && *list_len > 29 ){
2910 mxwave = iat/3;
2911 if( mxwave > 50 ) mxwave = 50;
2912 jlo = iat - 3*mxwave;
2913
2914 /* Search backwards from the end of "list" looking for the most recent
2915 occurence of the supplied "val" value. Limit the search to
2916 wavelengths of no more than the above limit. */
2917 jat = iat - 1;
2918 while( jat >= jlo ) {
2919 if( (*list)[ jat ] == val ) {
2920
2921 /* When an earlier occurrence of "val" is found, see if the values
2922 which precede it are the same as the values which precede the new
2923 element if "list" added by this invocation. We use 3 complete
2924 patterns as evidence of looping, unless the wavelength is 1 in which
2925 case we use 30 patterns (this is because wavelengths of 1 can occur
2926 in short sequences legitamately). */
2927 wavelen = iat - jat;
2928
2929 if( wavelen == 1 ) {
2930 nwave = 30;
2931 if( nwave > iat ) nwave = iat;
2932 } else {
2933 nwave = 3;
2934 }
2935
2936 if( nwave*wavelen <= *list_len ) {
2937 result = wavelen;
2938 wave[ 0 ] = *list + *list_len - wavelen;
2939 for( iwave = 1; iwave < nwave; iwave++ ) {
2940 wave[ iwave ] = wave[ iwave - 1 ] - wavelen;
2941 }
2942
2943 for( k = 0; k < wavelen; k++ ) {
2944 for( iwave = 1; iwave < nwave; iwave++ ) {
2945 if( *wave[ iwave ] != *wave[ 0 ] ) {
2946 result = 0;
2947 break;
2948 }
2949 wave[ iwave ]++;
2950 }
2951 wave[ 0 ]++;
2952 }
2953 }
2954
2955 /* Break if we have found a repeating pattern. */
2956 if( result ) break;
2957
2958 }
2959 jat--;
2960 }
2961 }
2962 }
2963
2964 if( !astOK ) result= 1;
2965
2966 /* Return the result.*/
2967 return result;
2968 }
2969
Rate(AstMapping * this,double * at,int ax1,int ax2,int * status)2970 static double Rate( AstMapping *this, double *at, int ax1, int ax2, int *status ){
2971 /*
2972 * Name:
2973 * Rate
2974
2975 * Purpose:
2976 * Calculate the rate of change of a Mapping output.
2977
2978 * Type:
2979 * Private function.
2980
2981 * Synopsis:
2982 * #include "cmpmap.h"
2983 * result = Rate( AstMapping *this, double *at, int ax1, int ax2, int *status )
2984
2985 * Class Membership:
2986 * CmpMap member function (overrides the astRate method inherited
2987 * from the Mapping class ).
2988
2989 * Description:
2990 * This function returns the rate of change of a specified output of
2991 * the supplied Mapping with respect to a specified input, at a
2992 * specified input position.
2993
2994 * Parameters:
2995 * this
2996 * Pointer to the Mapping to be applied.
2997 * at
2998 * The address of an array holding the axis values at the position
2999 * at which the rate of change is to be evaluated. The number of
3000 * elements in this array should equal the number of inputs to the
3001 * Mapping.
3002 * ax1
3003 * The index of the Mapping output for which the rate of change is to
3004 * be found (output numbering starts at 0 for the first output).
3005 * ax2
3006 * The index of the Mapping input which is to be varied in order to
3007 * find the rate of change (input numbering starts at 0 for the first
3008 * input).
3009 * status
3010 * Pointer to the inherited status variable.
3011
3012 * Returned Value:
3013 * The rate of change of Mapping output "ax1" with respect to input
3014 * "ax2", evaluated at "at", or AST__BAD if the value cannot be
3015 * calculated.
3016
3017 */
3018
3019 /* Local Variables: */
3020 AstMapping *c1;
3021 AstMapping *c2;
3022 AstCmpMap *map;
3023 double result;
3024 int old_inv1;
3025 int old_inv2;
3026 int nin1;
3027 int nin2;
3028 double *at2;
3029 double r1;
3030 double r2;
3031 int nout1;
3032 int i;
3033
3034 /* Check inherited status */
3035 if( !astOK ) return AST__BAD;
3036
3037 /* Get a pointer to the CmpMap structure. */
3038 map = (AstCmpMap *) this;
3039
3040 /* Note the current Invert flags of the two component Mappings. */
3041 old_inv1 = astGetInvert( map->map1 );
3042 old_inv2 = astGetInvert( map->map2 );
3043
3044 /* Temporarily reset them to the values they had when the CmpMap was
3045 created. */
3046 astSetInvert( map->map1, map->invert1 );
3047 astSetInvert( map->map2, map->invert2 );
3048
3049 /* If the CmpMap itself has been inverted, invert the component Mappings.
3050 Also note the order in which the Mappings should be applied if in series. */
3051 if( !astGetInvert( this ) ) {
3052 c1 = map->map1;
3053 c2 = map->map2;
3054 } else {
3055 c1 = map->map2;
3056 c2 = map->map1;
3057 astInvert( c1 );
3058 astInvert( c2 );
3059 }
3060
3061 /* First deal with Mappings in series. */
3062 if( map->series ) {
3063
3064 /* Get the number of inputs to the two component Mappings. */
3065 nin1 = astGetNin( c1 );
3066 nin2 = astGetNin( c2 );
3067
3068 /* Allocate workspace to hold the result of transforming the supplied "at"
3069 position using the first component. */
3070 at2 = astMalloc( sizeof( double )*(size_t) nin2 );
3071
3072 /* Transform the supplied "at" position using the first component. */
3073 astTranN( c1, 1, nin1, 1, at, 1, nin2, 1, at2 );
3074
3075 /* The required rate of change is the sum of the products of the rate of
3076 changes of the two component mappings, summed over all the output axes
3077 of the first componment. */
3078 result = 0.0;
3079 for( i = 0; i < nin2; i++ ) {
3080
3081 /* Find the rate of change of output "i" of the first component with
3082 respect to input "ax2" at the supplied "at" position. */
3083 r1 = astRate( c1, at, i, ax2 );
3084
3085 /* Find the rate of change of output "ax1" of the second component with
3086 respect to input "i" at the transformed "at2" position. */
3087 r2 = astRate( c2, at2, ax1, i );
3088
3089 /* If both are good, increment the ryunning total by the product of the
3090 two rates. Otherwise, break. */
3091 if( r1 != AST__BAD && r2 != AST__BAD ) {
3092 result += r1*r2;
3093 } else {
3094 result = AST__BAD;
3095 break;
3096 }
3097 }
3098
3099 /* Free the workspace. */
3100 at2 = astFree( at2 );
3101
3102 /* Now deal with Mappings in parallel. */
3103 } else {
3104
3105 /* Get the number of inputs and outputs for the lower component Mappings. */
3106 nin1 = astGetNin( map->map1 );
3107 nout1 = astGetNout( map->map1 );
3108
3109 /* If both input and output relate to the lower component Mappings, use its
3110 astRate method. */
3111 if( ax1 < nout1 && ax2 < nin1 ) {
3112 result = astRate( map->map1, at, ax1, ax2 );
3113
3114 /* If both input and output relate to the upper component Mappings, use its
3115 astRate method. */
3116 } else if( ax1 >= nout1 && ax2 >= nin1 ) {
3117 result = astRate( map->map2, at + nin1, ax1 - nout1, ax2 - nin1 );
3118
3119 /* If input and output relate to different component Mappings, return
3120 zero. */
3121 } else {
3122 result = 0.0;
3123 }
3124 }
3125
3126 /* Reinstate the original Invert flags of the component Mappings .*/
3127 astSetInvert( map->map1, old_inv1 );
3128 astSetInvert( map->map2, old_inv2 );
3129
3130 /* Return the result. */
3131 return result;
3132 }
3133
RemoveRegions(AstMapping * this_mapping,int * status)3134 static AstMapping *RemoveRegions( AstMapping *this_mapping, int *status ) {
3135 /*
3136 * Name:
3137 * RemoveRegions
3138
3139 * Purpose:
3140 * Remove any Regions from a Mapping.
3141
3142 * Type:
3143 * Private function.
3144
3145 * Synopsis:
3146 * #include "cmpmap.h"
3147 * AstMapping *RemoveRegions( AstMapping *this, int *status )
3148
3149 * Class Membership:
3150 * CmpMap method (over-rides the astRemoveRegions method inherited
3151 * from the Mapping class).
3152
3153 * Description:
3154 * This function searches the supplied Mapping (which may be a
3155 * compound Mapping such as a CmpMap) for any component Mappings
3156 * that are instances of the AST Region class. It then creates a new
3157 * Mapping from which all Regions have been removed. If a Region
3158 * cannot simply be removed (for instance, if it is a component of a
3159 * parallel CmpMap), then it is replaced with an equivalent UnitMap
3160 * in the returned Mapping.
3161 *
3162 * The implementation provided by the CmpMap class invokes the
3163 * astRemoveRegions method on the two component Mappings, and joins
3164 * the results together into a new CmpMap.
3165
3166 * Parameters:
3167 * this
3168 * Pointer to the original Region.
3169 * status
3170 * Pointer to the inherited status variable.
3171
3172 * Returned Value:
3173 * A pointer to the modified mapping.
3174
3175 * Notes:
3176 * - A NULL pointer value will be returned if this function is
3177 * invoked with the AST error status set, or if it should fail for
3178 * any reason.
3179 */
3180
3181 /* Local Variables: */
3182 AstCmpMap *new; /* Pointer to new CmpMap */
3183 AstCmpMap *this; /* Pointer to CmpMap structure */
3184 AstMapping *newmap1; /* New first component Mapping */
3185 AstMapping *newmap2; /* New second component Mapping */
3186 AstMapping *result; /* Result pointer to return */
3187 int nax; /* Number of Frame axes */
3188 int unit1; /* Is new first Mapping a UnitMap? */
3189 int unit2; /* Is new second Mapping a UnitMap? */
3190
3191 /* Initialise. */
3192 result = NULL;
3193
3194 /* Check the global error status. */
3195 if ( !astOK ) return result;
3196
3197 /* Get a pointer to the CmpMap. */
3198 this = (AstCmpMap *) this_mapping;
3199
3200 /* Invoke the astRemoveRegions method on the two component Mappings. */
3201 newmap1 = astRemoveRegions( this->map1 );
3202 newmap2 = astRemoveRegions( this->map2 );
3203
3204 /* If neither component was modified, just return a clone of the supplied
3205 pointer. */
3206 if( this->map1 == newmap1 && this->map2 == newmap2 ) {
3207 result = astClone( this );
3208
3209 /* Otherwise, we need to create a new Mapping to return. */
3210 } else {
3211
3212 /* The implementation of the astRemoveRegions method provided by the
3213 Region class returns a Frame rather than a UnitMap. But we need
3214 Mappings here, not Frames. So if either of these new Mappings is
3215 a Frame, replace it with an equivalent UnitMap. Also, get flags
3216 indicating if either Mapping is a UnitMap.*/
3217 if( astIsAFrame( newmap1 ) ) {
3218 nax = astGetNin( newmap1 );
3219 (void) astAnnul( newmap1 );
3220 newmap1 = (AstMapping *) astUnitMap( nax, " ", status );
3221 unit1 = 1;
3222 } else {
3223 unit1 = astIsAUnitMap( newmap1 );
3224 }
3225
3226 if( astIsAFrame( newmap2 ) ) {
3227 nax = astGetNin( newmap2 );
3228 (void) astAnnul( newmap2 );
3229 newmap2 = (AstMapping *) astUnitMap( nax, " ", status );
3230 unit2 = 1;
3231 } else {
3232 unit2 = astIsAUnitMap( newmap2 );
3233 }
3234
3235 /* First handle series CmpMaps. */
3236 if( this->series ) {
3237
3238 /* Otherwise, if the second new Mapping is a UnitMap, return a copy of the
3239 first new Mapping (with the original Invert attribute) since the second
3240 one will have no effect. */
3241 if( unit1 ) {
3242 result = astCopy( newmap2 );
3243 astSetInvert( result, this->invert2 );
3244 if( astGetInvert( this ) ) astInvert( result );
3245
3246 /* Otherwise, if the second new Mapping is a UnitMap, return a copy of the
3247 first new Mapping (with the original Invert attribute) since the second
3248 one will have no effect. */
3249 } else if( unit2 ) {
3250 result = astCopy( newmap1 );
3251 astSetInvert( result, this->invert1 );
3252 if( astGetInvert( this ) ) astInvert( result );
3253
3254 /* If neither of the new Mappings is a UnitMap, return a new CmpMap
3255 containing the two new Mappings. We take a deep copy of the supplied
3256 CmpMap and then modify the Mappings os that we retain any extra
3257 information (such as invert flags) in the supplied CmpMap. */
3258 } else {
3259 new = astCopy( this );
3260 (void) astAnnul( new->map1 );
3261 (void) astAnnul( new->map2 );
3262 new->map1 = astClone( newmap1 );
3263 new->map2 = astClone( newmap2 );
3264 result = (AstMapping *) new;
3265 }
3266
3267 /* Now handle parallel CmpMaps. */
3268 } else {
3269
3270 /* If both new Mappings are UnitMaps, return an equivalent UnitMap. */
3271 if( unit1 && unit2 ) {
3272 result = (AstMapping *) astUnitMap( astGetNin( newmap1 ) +
3273 astGetNin( newmap2 ), " ",
3274 status );
3275
3276 /* Otherwise, return a new CmpMap containing the two new Mappings. */
3277 } else {
3278 new = astCopy( this );
3279 (void) astAnnul( new->map1 );
3280 (void) astAnnul( new->map2 );
3281 new->map1 = astClone( newmap1 );
3282 new->map2 = astClone( newmap2 );
3283 result = (AstMapping *) new;
3284 }
3285 }
3286 }
3287
3288 /* Free resources. */
3289 newmap1 = astAnnul( newmap1 );
3290 newmap2 = astAnnul( newmap2 );
3291
3292 /* Annul the returned Mapping if an error has occurred. */
3293 if( !astOK ) result = astAnnul( result );
3294
3295 /* Return the result. */
3296 return result;
3297 }
3298
Simplify(AstMapping * this_mapping,int * status)3299 static AstMapping *Simplify( AstMapping *this_mapping, int *status ) {
3300 /*
3301 * Name:
3302 * Simplify
3303
3304 * Purpose:
3305 * Simplify a Mapping.
3306
3307 * Type:
3308 * Private function.
3309
3310 * Synopsis:
3311 * #include "mapping.h"
3312 * AstMapping *Simplify( AstMapping *this, int *status )
3313
3314 * Class Membership:
3315 * CmpMap method (over-rides the astSimplify method inherited from
3316 * the Mapping class).
3317
3318 * Description:
3319 * This function simplifies a CmpMap to eliminate redundant
3320 * computational steps, or to merge separate steps which can be
3321 * performed more efficiently in a single operation.
3322
3323 * Parameters:
3324 * this
3325 * Pointer to the original Mapping.
3326 * status
3327 * Pointer to the inherited status variable.
3328
3329 * Returned Value:
3330 * A new pointer to the (possibly simplified) Mapping.
3331
3332 * Notes:
3333 * - A NULL pointer value will be returned if this function is
3334 * invoked with the AST error status set, or if it should fail for
3335 * any reason.
3336 */
3337
3338 /* Local Variables: */
3339 astDECLARE_GLOBALS /* Pointer to thread-specific global data */
3340 AstCmpMap *this; /* Pointer to CmpMap structure */
3341 AstMapping **map_list; /* Mapping array pointer */
3342 AstMapping *map; /* Pointer to cloned Mapping pointer */
3343 AstMapping *result; /* Result pointer to return */
3344 AstMapping *tmp; /* Temporary Mapping pointer */
3345 int *invert_list; /* Invert array pointer */
3346 int *mlist; /* Point to list of modified Mapping indices */
3347 int *nlist; /* Point to list of Mapping counts */
3348 int i; /* Loop counter for Mappings */
3349 int improved; /* Simplification achieved? */
3350 int invert; /* Invert attribute value */
3351 int invert_n; /* Invert value for final Mapping */
3352 int mlist_len; /* No. of entries in mlist */
3353 int nlist_len; /* No. of entries in nlist */
3354 int modified; /* Index of first modified Mapping */
3355 int nmap; /* Mapping count */
3356 int nominated; /* Index of nominated Mapping */
3357 int set; /* Invert attribute set? */
3358 int set_n; /* Invert set for final Mapping? */
3359 int simpler; /* Simplification possible? */
3360 int t; /* Temporary storage */
3361 int wlen1; /* Pattern wavelength for "modified" values */
3362 int wlen2; /* Pattern wavelength for "nmap" values */
3363
3364 /* Initialise. */
3365 result = NULL;
3366
3367 /* Check the global error status. */
3368 if ( !astOK ) return result;
3369
3370 /* Get a pointer to the thread specific global data structure. */
3371 astGET_GLOBALS(this_mapping);
3372
3373 /* It is possible for the astSimplify method to be called recursively from
3374 within astSimplify. It is also possible that the Mapping being
3375 simplified by the current invocation is the same as the Mapping being
3376 simplified by some recursive invocation higher up the call stack. If
3377 this happens we will get into an infinite loop, since we already know
3378 that simplifying the supplied Mapping will involve (eventually) a
3379 recursive call to astSimplify with the same Mapping. To avoid this
3380 looping, we note the Mappings supplied at each depth and first compare
3381 the supplied Mapping with the Mappings which are currently being
3382 simplified higher up the call stack. If the supplied Mapping is
3383 already being simplified at a higher level, then we return immediately
3384 without doing any simplification. Otherwise, we record the supplied
3385 Mapping pointer in a static list so that it is available to subsequent
3386 recursive invocations of this function. First compare the supplied
3387 Mapping with the Mappingsbeing simpliied higher up. Return without
3388 action if a match is found. */
3389 for( i = 0; i < simplify_depth; i++ ) {
3390 if( astEqual( this_mapping, simplify_stackmaps[ i ] ) ) {
3391 return astClone( this_mapping );
3392 }
3393 }
3394
3395 /* We have further work to do, so increment the recursion depth, extend
3396 the simplify_stackmaps array, and store the new Mapping in it for future use. */
3397 simplify_depth++;
3398 simplify_stackmaps = astGrow( simplify_stackmaps, simplify_depth, sizeof( AstMapping * ) );
3399 if( astOK ) {
3400 simplify_stackmaps[ simplify_depth - 1 ] = astClone( this_mapping );
3401 }
3402
3403 /* Obtain a pointer to the CmpMap structure. */
3404 this = (AstCmpMap *) this_mapping;
3405
3406 /* Initialise dynamic arrays of Mapping pointers and associated Invert
3407 flags. */
3408 nmap = 0;
3409 map_list = NULL;
3410 invert_list = NULL;
3411
3412 /* Decompose the CmpMap into a sequence of Mappings to be applied in
3413 series or parallel, as appropriate, and an associated list of
3414 Invert flags. If any inverted CmpMaps are found in the Mapping, then
3415 we can at least simplify the returned Mapping by swapping and
3416 inverting the components. Set "simpler" to indicate this. */
3417 simpler = astMapList( this_mapping, this->series, astGetInvert( this ), &nmap,
3418 &map_list, &invert_list );
3419
3420 /* Initialise pointers to memory used to hold lists of the modified
3421 Mapping index and the number of mappings after each call of
3422 astMapMerge. */
3423 mlist = NULL;
3424 nlist = NULL;
3425
3426 /* Loop to simplify the sequence until a complete pass through it has
3427 been made without producing any improvement. */
3428 improved = 1;
3429 while ( astOK && improved ) {
3430 improved = 0;
3431
3432 /* Loop to nominate each Mapping in the sequence in turn. */
3433 nominated = 0;
3434 while ( astOK && ( nominated < nmap ) ) {
3435
3436
3437 /* Clone a pointer to the nominated Mapping and attempt to merge it
3438 with its neighbours. Annul the cloned pointer afterwards. */
3439 map = astClone( map_list[ nominated ] );
3440 modified = astMapMerge( map, nominated, this->series,
3441 &nmap, &map_list, &invert_list );
3442 map = astAnnul( map );
3443
3444 /* Move on to nominate the next Mapping in the sequence. */
3445 nominated++;
3446
3447 /* Note if any simplification occurred above. */
3448 if( modified >= 0 ) {
3449
3450 /* Append the index of the first modified Mapping in the list and and check
3451 that there is no repreating pattern in the list. If there is, we are
3452 probably in a loop where one mapping class is making a change, and another
3453 is undoing the change. The Looping function returns the "wavelength"
3454 of any pattern found. If a pattern was discovered, we ignore it unless
3455 there is also a pattern in the "nmap" values - the wavelengths of the
3456 two patterns must be related by a integer factor. */
3457 wlen1 = PatternCheck( modified, 1, &mlist, &mlist_len, status );
3458 wlen2 = PatternCheck( nmap, wlen1, &nlist, &nlist_len, status );
3459 if( wlen1 && wlen2 ) {
3460
3461 /* Ensure wlen2 is larger than or equal to wlen1. */
3462 if( wlen1 > wlen2 ) {
3463 t = wlen1;
3464 wlen1 = wlen2;
3465 wlen2 = t;
3466 }
3467
3468 /* See if wlen2 is an integer multiple of wlen1. If not, ignore the
3469 patterns. */
3470 if( ( wlen2 % wlen1 ) != 0 ) wlen1 = 0;
3471 }
3472
3473 /* Ignore the simplication if a repeating pattern is occurring. */
3474 if( wlen1 == 0 ) {
3475 improved = 1;
3476 simpler = 1;
3477
3478 /* If the simplification resulted in modification of an earlier
3479 Mapping than would normally be considered next, then go back to
3480 consider the modified one first. */
3481 if ( modified < nominated ) nominated = modified;
3482 }
3483 }
3484 }
3485 }
3486
3487 /* Free resources */
3488 if( mlist ) mlist = astFree( mlist );
3489 if( nlist ) nlist = astFree( nlist );
3490
3491 /* Construct the output Mapping. */
3492 /* ============================= */
3493 /* If no simplification occurred above, then simply clone a pointer to
3494 the original Mapping. */
3495 if ( astOK ) {
3496 if ( !simpler ) {
3497 result = astClone( this );
3498
3499 /* Otherwise, we must construct the result from the contents of the
3500 Mapping list. */
3501 } else {
3502
3503 /* If the simplified Mapping list has only a single element, then the
3504 output Mapping will not be a CmpMap. In this case, we cannot
3505 necessarily set the Invert flag of the Mapping to the value we want
3506 (because we must not modify the Mapping itself. */
3507 if ( nmap == 1 ) {
3508
3509 /* We must make a copy. Cloning is no good (even if the Mapping already
3510 has the Invert attribute value we want), since we want the returned
3511 Mapping to be independent of the original component Mappings, so that
3512 if user code inverts a component Mapping (via some other pre-existing
3513 pointer), the returned simplified Mapping is not affected. */
3514 result = astCopy( map_list[ 0 ] );
3515
3516 /* Either clear the copy's Invert attribute, or set it to 1, as
3517 required. */
3518 if ( invert_list[ 0 ] ) {
3519 astSetInvert( result, 1 );
3520 } else {
3521 astClearInvert( result );
3522 }
3523
3524 /* If the simplified Mapping sequence has more than one element, the
3525 output Mapping will be a CmpMap. In this case, we can set each
3526 individual Mapping element to have the Invert attribute value we
3527 want, so long as we return these attribute values to their original
3528 state again afterwards (once a Mapping is encapsulated inside a
3529 CmpMap, further external changes to its Invert attribute do not
3530 affect the behaviour of the CmpMap). */
3531 } else {
3532
3533 /* Determine if the Invert attribute for the last Mapping is set, and
3534 obtain its value. */
3535 set_n = astTestInvert( map_list[ nmap - 1 ] );
3536 invert_n = astGetInvert( map_list[ nmap - 1 ] );
3537
3538 /* Set this attribute to the value we want. */
3539 astSetInvert( map_list[ nmap - 1 ], invert_list[ nmap - 1 ] );
3540
3541 /* Loop through the Mapping sequence in reverse to merge it into an
3542 equivalent CmpMap. */
3543 for ( i = nmap - 1; i >= 0; i-- ) {
3544
3545 /* Simply clone the pointer to the last Mapping in the sequence (which
3546 will be encountered first). */
3547 if ( !result ) {
3548 result = astClone( map_list[ i ] );
3549
3550 /* For subsequent Mappings, test if the Invert attribute is set and
3551 save its value. */
3552 } else {
3553 set = astTestInvert( map_list[ i ] );
3554 invert = astGetInvert( map_list[ i ] );
3555
3556 /* Set this attribute to the value required. */
3557 astSetInvert( map_list[ i ], invert_list[ i ] );
3558
3559 /* Combine the Mapping with the CmpMap formed so far and replace the
3560 result pointer with the new pointer this produces, annulling the
3561 previous pointer. */
3562 tmp = (AstMapping *) astCmpMap( map_list[ i ], result,
3563 this->series, "", status );
3564 (void) astAnnul( result );
3565 result = tmp;
3566
3567 /* Restore the Invert attribute of the Mapping to its original
3568 state. */
3569 if ( !set ) {
3570 astClearInvert( map_list[ i ] );
3571 } else {
3572 astSetInvert( map_list[ i ], invert );
3573 }
3574 }
3575 }
3576
3577 /* When all the Mappings have been merged into the CmpMap, restore the
3578 state of the Invert attribute for the final Mapping in the
3579 sequence. */
3580 if ( !set_n ) {
3581 astClearInvert( map_list[ nmap - 1 ] );
3582 } else {
3583 astSetInvert( map_list[ nmap - 1 ], invert_n );
3584 }
3585 }
3586 }
3587 }
3588
3589 /* Clean up. */
3590 /* ========= */
3591 /* Loop to annul all the Mapping pointers in the simplified list. */
3592 for ( i = 0; i < nmap; i++ ) map_list[ i ] = astAnnul( map_list[ i ] );
3593
3594 /* Free the dynamic arrays. */
3595 map_list = astFree( map_list );
3596 invert_list = astFree( invert_list );
3597
3598 /* Decrement the recursion depth and free the pointer to the supplied
3599 Mapping currently stored at the end of the simplify_stackmaps array. */
3600 simplify_depth--;
3601 if( astOK ) {
3602 simplify_stackmaps[ simplify_depth ] = astAnnul( simplify_stackmaps[ simplify_depth ] );
3603 }
3604
3605 /* If we are now at depth zero, free the simplify_stackmaps array. */
3606 if( simplify_depth == 0 ) simplify_stackmaps = astFree( simplify_stackmaps );
3607
3608 /* If an error occurred, annul the returned Mapping. */
3609 if ( !astOK ) result = astAnnul( result );
3610
3611 /* Return the result. */
3612 return result;
3613 }
3614
Transform(AstMapping * this,AstPointSet * in,int forward,AstPointSet * out,int * status)3615 static AstPointSet *Transform( AstMapping *this, AstPointSet *in,
3616 int forward, AstPointSet *out, int *status ) {
3617 /*
3618 * Name:
3619 * Transform
3620
3621 * Purpose:
3622 * Apply a CmpMap to transform a set of points.
3623
3624 * Type:
3625 * Private function.
3626
3627 * Synopsis:
3628 * #include "cmpmap.h"
3629 * AstPointSet *Transform( AstMapping *this, AstPointSet *in,
3630 * int forward, AstPointSet *out, int *status )
3631
3632 * Class Membership:
3633 * CmpMap member function (over-rides the astTransform method inherited
3634 * from the Mapping class).
3635
3636 * Description:
3637 * This function takes a CmpMap and a set of points encapsulated in a
3638 * PointSet and transforms the points so as to apply the required Mapping.
3639 * This implies applying each of the CmpMap's component Mappings in turn,
3640 * either in series or in parallel.
3641
3642 * Parameters:
3643 * this
3644 * Pointer to the CmpMap.
3645 * in
3646 * Pointer to the PointSet associated with the input coordinate values.
3647 * forward
3648 * A non-zero value indicates that the forward coordinate transformation
3649 * should be applied, while a zero value requests the inverse
3650 * transformation.
3651 * out
3652 * Pointer to a PointSet which will hold the transformed (output)
3653 * coordinate values. A NULL value may also be given, in which case a
3654 * new PointSet will be created by this function.
3655 * status
3656 * Pointer to the inherited status variable.
3657
3658 * Returned Value:
3659 * Pointer to the output (possibly new) PointSet.
3660
3661 * Notes:
3662 * - A null pointer will be returned if this function is invoked with the
3663 * global error status set, or if it should fail for any reason.
3664 * - The number of coordinate values per point in the input PointSet must
3665 * match the number of coordinates for the CmpMap being applied.
3666 * - If an output PointSet is supplied, it must have space for sufficient
3667 * number of points and coordinate values per point to accommodate the
3668 * result. Any excess space will be ignored.
3669 */
3670
3671 /* Local Variables: */
3672 AstCmpMap *map; /* Pointer to CmpMap to be applied */
3673 AstPointSet *result; /* Pointer to output PointSet */
3674 AstPointSet *temp1; /* Pointer to temporary PointSet */
3675 AstPointSet *temp2; /* Pointer to temporary PointSet */
3676 AstPointSet *temp; /* Pointer to temporary PointSet */
3677 int forward1; /* Use forward direction for Mapping 1? */
3678 int forward2; /* Use forward direction for Mapping 2? */
3679 int ipoint1; /* Index of first point in batch */
3680 int ipoint2; /* Index of last point in batch */
3681 int nin1; /* No. input coordinates for Mapping 1 */
3682 int nin2; /* No. input coordinates for Mapping 2 */
3683 int nin; /* No. input coordinates supplied */
3684 int nout1; /* No. output coordinates for Mapping 1 */
3685 int nout2; /* No. output coordinates for Mapping 2 */
3686 int nout; /* No. output coordinates supplied */
3687 int np; /* Number of points in batch */
3688 int npoint; /* Number of points to be transformed */
3689
3690 /* Local Constants: */
3691 const int nbatch = 2048; /* Maximum points in a batch */
3692
3693 /* Check the global error status. */
3694 if ( !astOK ) return NULL;
3695
3696 /* Obtain a pointer to the CmpMap. */
3697 map = (AstCmpMap *) this;
3698
3699 /* Apply the parent Mapping using the stored pointer to the Transform member
3700 function inherited from the parent Mapping class. This function validates
3701 all arguments and generates an output PointSet if necessary, but does not
3702 actually transform any coordinate values. */
3703 result = (*parent_transform)( this, in, forward, out, status );
3704
3705 /* We now extend the parent astTransform method by applying the component
3706 Mappings of the CmpMap to generate the output coordinate values. */
3707
3708 /* Determine whether to apply the forward or inverse Mapping, according to the
3709 direction specified and whether the Mapping has been inverted. */
3710 if ( astGetInvert( map ) ) forward = !forward;
3711
3712 /* Check if either component Mapping's inversion flag has changed since it was
3713 used to construct the CmpMap. Set a "forward" flag for each Mapping to
3714 change the direction we will use, to compensate if necessary. (Such changes
3715 may have occurred if other pointers to the component Mappings are in
3716 circulation). */
3717 forward1 = forward;
3718 forward2 = forward;
3719 if ( map->invert1 != astGetInvert( map->map1 ) ) forward1 = !forward1;
3720 if ( map->invert2 != astGetInvert( map->map2 ) ) forward2 = !forward2;
3721
3722 /* Determine the number of points being transformed. */
3723 npoint = astGetNpoint( in );
3724
3725 /* Mappings in series. */
3726 /* ------------------- */
3727 /* If required, use the two component Mappings in series. To do this, we must
3728 apply one Mapping followed by the other, which means storing an intermediate
3729 result. Since this function may be invoked recursively and have to store an
3730 intermediate result on each occasion, the memory required may become
3731 excessive when transforming large numbers of points. To overcome this, we
3732 split the points up into smaller batches. */
3733 if ( astOK ) {
3734 if ( map->series ) {
3735
3736 /* Obtain the numbers of input and output coordinates. */
3737 nin = astGetNcoord( in );
3738 nout = astGetNcoord( result );
3739
3740 /* Loop to process all the points in batches, of maximum size nbatch points. */
3741 for ( ipoint1 = 0; ipoint1 < npoint; ipoint1 += nbatch ) {
3742
3743 /* Calculate the index of the final point in the batch and deduce the number of
3744 points (np) to be processed in this batch. */
3745 ipoint2 = ipoint1 + nbatch - 1;
3746 if ( ipoint2 > npoint - 1 ) ipoint2 = npoint - 1;
3747 np = ipoint2 - ipoint1 + 1;
3748
3749 /* Create temporary PointSets to describe the input and output points for this
3750 batch. */
3751 temp1 = astPointSet( np, nin, "", status );
3752 temp2 = astPointSet( np, nout, "", status );
3753
3754 /* Associate the required subsets of the input and output coordinates with the
3755 two PointSets. */
3756 astSetSubPoints( in, ipoint1, 0, temp1 );
3757 astSetSubPoints( result, ipoint1, 0, temp2 );
3758
3759 /* Apply the two Mappings in sequence and in the required order and direction.
3760 Store the intermediate result in a temporary PointSet (temp) which is
3761 created by the first Mapping applied. */
3762 if ( forward ) {
3763 temp = astTransform( map->map1, temp1, forward1, NULL );
3764 (void) astTransform( map->map2, temp, forward2, temp2 );
3765 } else {
3766 temp = astTransform( map->map2, temp1, forward2, NULL );
3767 (void) astTransform( map->map1, temp, forward1, temp2 );
3768 }
3769
3770 /* Delete the temporary PointSets after processing each batch of points. */
3771 temp = astDelete( temp );
3772 temp1 = astDelete( temp1 );
3773 temp2 = astDelete( temp2 );
3774
3775 /* Quit processing batches if an error occurs. */
3776 if ( !astOK ) break;
3777 }
3778
3779 /* Mappings in parallel. */
3780 /* --------------------- */
3781 /* If required, use the two component Mappings in parallel. Since we do not
3782 need to allocate any memory to hold intermediate coordinate values here,
3783 there is no need to process the points in batches. */
3784 } else {
3785
3786 /* Get the effective number of input and output coordinates per point for each
3787 Mapping (taking account of the direction in which each will be used to
3788 transform points). */
3789 nin1 = forward1 ? astGetNin( map->map1 ) : astGetNout( map->map1 );
3790 nout1 = forward1 ? astGetNout( map->map1 ) : astGetNin( map->map1 );
3791 nin2 = forward2 ? astGetNin( map->map2 ) : astGetNout( map->map2 );
3792 nout2 = forward2 ? astGetNout( map->map2 ) : astGetNin( map->map2 );
3793
3794 /* Create temporary PointSets to describe the input and output coordinates for
3795 the first Mapping. */
3796 temp1 = astPointSet( npoint, nin1, "", status );
3797 temp2 = astPointSet( npoint, nout1, "", status );
3798
3799 /* Associate the required subsets of the input and output coordinates with
3800 these PointSets. */
3801 astSetSubPoints( in, 0, 0, temp1 );
3802 astSetSubPoints( result, 0, 0, temp2 );
3803
3804 /* Use the astTransform method to apply the coordinate transformation described
3805 by the first Mapping. */
3806 (void) astTransform( map->map1, temp1, forward1, temp2 );
3807
3808 /* Delete the temporary PointSets. */
3809 temp1 = astDelete( temp1 );
3810 temp2 = astDelete( temp2 );
3811
3812 /* Create a new pair of temporary PointSets to describe the input and output
3813 coordinates for the second Mapping, and associate the required subsets of
3814 the input and output coordinates with these PointSets. */
3815 temp1 = astPointSet( npoint, nin2, "", status );
3816 temp2 = astPointSet( npoint, nout2, "", status );
3817 astSetSubPoints( in, 0, nin1, temp1 );
3818 astSetSubPoints( result, 0, nout1, temp2 );
3819
3820 /* Apply the coordinate transformation described by the second Mapping. */
3821 (void) astTransform( map->map2, temp1, forward2, temp2 );
3822
3823 /* Delete the two temporary PointSets. */
3824 temp1 = astDelete( temp1 );
3825 temp2 = astDelete( temp2 );
3826 }
3827 }
3828
3829 /* If an error occurred, clean up by deleting the output PointSet (if
3830 allocated by this function) and setting a NULL result pointer. */
3831 if ( !astOK ) {
3832 if ( !out ) result = astDelete( result );
3833 result = NULL;
3834 }
3835
3836 /* Return a pointer to the output PointSet. */
3837 return result;
3838 }
3839
3840 /* Copy constructor. */
3841 /* ----------------- */
Copy(const AstObject * objin,AstObject * objout,int * status)3842 static void Copy( const AstObject *objin, AstObject *objout, int *status ) {
3843 /*
3844 * Name:
3845 * Copy
3846
3847 * Purpose:
3848 * Copy constructor for CmpMap objects.
3849
3850 * Type:
3851 * Private function.
3852
3853 * Synopsis:
3854 * void Copy( const AstObject *objin, AstObject *objout, int *status )
3855
3856 * Description:
3857 * This function implements the copy constructor for CmpMap objects.
3858
3859 * Parameters:
3860 * objin
3861 * Pointer to the object to be copied.
3862 * objout
3863 * Pointer to the object being constructed.
3864 * status
3865 * Pointer to the inherited status variable.
3866
3867 * Returned Value:
3868 * void
3869
3870 * Notes:
3871 * - This constructor makes a deep copy, including a copy of the component
3872 * Mappings within the CmpMap.
3873 */
3874
3875 /* Local Variables: */
3876 AstCmpMap *in; /* Pointer to input CmpMap */
3877 AstCmpMap *out; /* Pointer to output CmpMap */
3878
3879 /* Check the global error status. */
3880 if ( !astOK ) return;
3881
3882 /* Obtain pointers to the input and output CmpMaps. */
3883 in = (AstCmpMap *) objin;
3884 out = (AstCmpMap *) objout;
3885
3886 /* For safety, start by clearing any references to the input component
3887 Mappings from the output CmpMap. */
3888 out->map1 = NULL;
3889 out->map2 = NULL;
3890
3891 /* Make copies of these Mappings and store pointers to them in the output
3892 CmpMap structure. */
3893 out->map1 = astCopy( in->map1 );
3894 out->map2 = astCopy( in->map2 );
3895 }
3896
3897 /* Destructor. */
3898 /* ----------- */
Delete(AstObject * obj,int * status)3899 static void Delete( AstObject *obj, int *status ) {
3900 /*
3901 * Name:
3902 * Delete
3903
3904 * Purpose:
3905 * Destructor for CmpMap objects.
3906
3907 * Type:
3908 * Private function.
3909
3910 * Synopsis:
3911 * void Delete( AstObject *obj, int *status )
3912
3913 * Description:
3914 * This function implements the destructor for CmpMap objects.
3915
3916 * Parameters:
3917 * obj
3918 * Pointer to the object to be deleted.
3919 * status
3920 * Pointer to the inherited status variable.
3921
3922 * Returned Value:
3923 * void
3924
3925 * Notes:
3926 * This function attempts to execute even if the global error status is
3927 * set.
3928 */
3929
3930 /* Local Variables: */
3931 AstCmpMap *this; /* Pointer to CmpMap */
3932
3933 /* Obtain a pointer to the CmpMap structure. */
3934 this = (AstCmpMap *) obj;
3935
3936 /* Annul the pointers to the component Mappings. */
3937 this->map1 = astAnnul( this->map1 );
3938 this->map2 = astAnnul( this->map2 );
3939
3940 /* Clear the remaining CmpMap variables. */
3941 this->invert1 = 0;
3942 this->invert2 = 0;
3943 this->series = 0;
3944 }
3945
3946 /* Dump function. */
3947 /* -------------- */
Dump(AstObject * this_object,AstChannel * channel,int * status)3948 static void Dump( AstObject *this_object, AstChannel *channel, int *status ) {
3949 /*
3950 * Name:
3951 * Dump
3952
3953 * Purpose:
3954 * Dump function for CmpMap objects.
3955
3956 * Type:
3957 * Private function.
3958
3959 * Synopsis:
3960 * void Dump( AstObject *this, AstChannel *channel, int *status )
3961
3962 * Description:
3963 * This function implements the Dump function which writes out data
3964 * for the CmpMap class to an output Channel.
3965
3966 * Parameters:
3967 * this
3968 * Pointer to the CmpMap whose data are being written.
3969 * channel
3970 * Pointer to the Channel to which the data are being written.
3971 * status
3972 * Pointer to the inherited status variable.
3973 */
3974
3975 /* Local Variables: */
3976 AstCmpMap *this; /* Pointer to the CmpMap structure */
3977 int ival; /* Integer value */
3978 int set; /* Attribute value set? */
3979
3980 /* Check the global error status. */
3981 if ( !astOK ) return;
3982
3983 /* Obtain a pointer to the CmpMap structure. */
3984 this = (AstCmpMap *) this_object;
3985
3986 /* Write out values representing the instance variables for the CmpMap
3987 class. Accompany these with appropriate comment strings, possibly
3988 depending on the values being written.*/
3989
3990 /* In the case of attributes, we first use the appropriate (private)
3991 Test... member function to see if they are set. If so, we then use
3992 the (private) Get... function to obtain the value to be written
3993 out.
3994
3995 For attributes which are not set, we use the astGet... method to
3996 obtain the value instead. This will supply a default value
3997 (possibly provided by a derived class which over-rides this method)
3998 which is more useful to a human reader as it corresponds to the
3999 actual default attribute value. Since "set" will be zero, these
4000 values are for information only and will not be read back. */
4001
4002 /* Series. */
4003 /* ------- */
4004 ival = this->series;
4005 set = ( ival == 0 );
4006 astWriteInt( channel, "Series", set, 0, ival,
4007 ival ? "Component Mappings applied in series" :
4008 "Component Mappings applied in parallel" );
4009
4010 /* First Invert flag. */
4011 /* ------------------ */
4012 ival = this->invert1;
4013 set = ( ival != 0 );
4014 astWriteInt( channel, "InvA", set, 0, ival,
4015 ival ? "First Mapping used in inverse direction" :
4016 "First Mapping used in forward direction" );
4017
4018 /* Second Invert flag. */
4019 /* ------------------- */
4020 ival = this->invert2;
4021 set = ( ival != 0 );
4022 astWriteInt( channel, "InvB", set, 0, ival,
4023 ival ? "Second Mapping used in inverse direction" :
4024 "Second Mapping used in forward direction" );
4025
4026 /* First Mapping. */
4027 /* -------------- */
4028 astWriteObject( channel, "MapA", 1, 1, this->map1,
4029 "First component Mapping" );
4030
4031 /* Second Mapping. */
4032 /* --------------- */
4033 astWriteObject( channel, "MapB", 1, 1, this->map2,
4034 "Second component Mapping" );
4035 }
4036
4037 /* Standard class functions. */
4038 /* ========================= */
4039 /* Implement the astIsACmpMap and astCheckCmpMap functions using the
4040 macros defined for this purpose in the "object.h" header file. */
astMAKE_ISA(CmpMap,Mapping)4041 astMAKE_ISA(CmpMap,Mapping)
4042 astMAKE_CHECK(CmpMap)
4043
4044 AstCmpMap *astCmpMap_( void *map1_void, void *map2_void, int series,
4045 const char *options, int *status, ...) {
4046 /*
4047 *+
4048 * Name:
4049 * astCmpMap
4050
4051 * Purpose:
4052 * Create a CmpMap.
4053
4054 * Type:
4055 * Protected function.
4056
4057 * Synopsis:
4058 * #include "cmpmap.h"
4059 * AstCmpMap *astCmpMap( AstMapping *map1, AstMapping *map2, int series,
4060 * const char *options, ... )
4061
4062 * Class Membership:
4063 * CmpMap constructor.
4064
4065 * Description:
4066 * This function creates a new CmpMap and optionally initialises its
4067 * attributes.
4068
4069 * Parameters:
4070 * map1
4071 * Pointer to the first Mapping.
4072 * map2
4073 * Pointer to the second Mapping.
4074 * series
4075 * If a non-zero value is given, the two Mappings will be connected
4076 * together in series. A zero value requests that they be connected in
4077 * parallel.
4078 * options
4079 * Pointer to a null terminated string containing an optional
4080 * comma-separated list of attribute assignments to be used for
4081 * initialising the new CmpMap. The syntax used is the same as for the
4082 * astSet method and may include "printf" format specifiers identified
4083 * by "%" symbols in the normal way.
4084 * ...
4085 * If the "options" string contains "%" format specifiers, then an
4086 * optional list of arguments may follow it in order to supply values to
4087 * be substituted for these specifiers. The rules for supplying these
4088 * are identical to those for the astSet method (and for the C "printf"
4089 * function).
4090
4091 * Returned Value:
4092 * A pointer to the new CmpMap.
4093
4094 * Notes:
4095 * - A null pointer will be returned if this function is invoked
4096 * with the global error status set, or if it should fail for any
4097 * reason.
4098 *-
4099
4100 * Implementation Notes:
4101 * - This function implements the basic CmpMap constructor which is
4102 * available via the protected interface to the CmpMap class. A
4103 * public interface is provided by the astCmpMapId_ function.
4104 * - Because this function has a variable argument list, it is
4105 * invoked by a macro that evaluates to a function pointer (not a
4106 * function invocation) and no checking or casting of arguments is
4107 * performed before the function is invoked. Because of this, the
4108 * "map1" and "map2" parameters are of type (void *) and are
4109 * converted and validated within the function itself.
4110 */
4111
4112 /* Local Variables: */
4113 astDECLARE_GLOBALS /* Pointer to thread-specific global data */
4114 AstCmpMap *new; /* Pointer to new CmpMap */
4115 AstMapping *map1; /* Pointer to first Mapping structure */
4116 AstMapping *map2; /* Pointer to second Mapping structure */
4117 va_list args; /* Variable argument list */
4118
4119 /* Initialise. */
4120 new = NULL;
4121
4122 /* Get a pointer to the thread specific global data structure. */
4123 astGET_GLOBALS(NULL);
4124
4125 /* Check the global status. */
4126 if ( !astOK ) return new;
4127
4128 /* Obtain and validate pointers to the Mapping structures provided. */
4129 map1 = astCheckMapping( map1_void );
4130 map2 = astCheckMapping( map2_void );
4131 if ( astOK ) {
4132
4133 /* Initialise the CmpMap, allocating memory and initialising the
4134 virtual function table as well if necessary. */
4135 new = astInitCmpMap( NULL, sizeof( AstCmpMap ), !class_init, &class_vtab,
4136 "CmpMap", map1, map2, series );
4137
4138 /* If successful, note that the virtual function table has been
4139 initialised. */
4140 if ( astOK ) {
4141 class_init = 1;
4142
4143 /* Obtain the variable argument list and pass it along with the
4144 options string to the astVSet method to initialise the new CmpMap's
4145 attributes. */
4146 va_start( args, status );
4147 astVSet( new, options, NULL, args );
4148 va_end( args );
4149
4150 /* If an error occurred, clean up by deleting the new object. */
4151 if ( !astOK ) new = astDelete( new );
4152 }
4153 }
4154
4155 /* Return a pointer to the new CmpMap. */
4156 return new;
4157 }
4158
astCmpMapId_(void * map1_void,void * map2_void,int series,const char * options,...)4159 AstCmpMap *astCmpMapId_( void *map1_void, void *map2_void, int series,
4160 const char *options, ... ) {
4161 /*
4162 *++
4163 * Name:
4164 c astCmpMap
4165 f AST_CMPMAP
4166
4167 * Purpose:
4168 * Create a CmpMap.
4169
4170 * Type:
4171 * Public function.
4172
4173 * Synopsis:
4174 c #include "cmpmap.h"
4175 c AstCmpMap *astCmpMap( AstMapping *map1, AstMapping *map2, int series,
4176 c const char *options, ... )
4177 f RESULT = AST_CMPMAP( MAP1, MAP2, SERIES, OPTIONS, STATUS )
4178
4179 * Class Membership:
4180 * CmpMap constructor.
4181
4182 * Description:
4183 * This function creates a new CmpMap and optionally initialises
4184 * its attributes.
4185 *
4186 * A CmpMap is a compound Mapping which allows two component
4187 * Mappings (of any class) to be connected together to form a more
4188 * complex Mapping. This connection may either be "in series"
4189 * (where the first Mapping is used to transform the coordinates of
4190 * each point and the second mapping is then applied to the
4191 * result), or "in parallel" (where one Mapping transforms the
4192 * earlier coordinates for each point and the second Mapping
4193 * simultaneously transforms the later coordinates).
4194 *
4195 * Since a CmpMap is itself a Mapping, it can be used as a
4196 * component in forming further CmpMaps. Mappings of arbitrary
4197 * complexity may be built from simple individual Mappings in this
4198 * way.
4199
4200 * Parameters:
4201 c map1
4202 f MAP1 = INTEGER (Given)
4203 * Pointer to the first component Mapping.
4204 c map2
4205 f MAP2 = INTEGER (Given)
4206 * Pointer to the second component Mapping.
4207 c series
4208 f SERIES = LOGICAL (Given)
4209 c If a non-zero value is given for this parameter, the two
4210 c component Mappings will be connected in series. A zero
4211 c value requests that they are connected in parallel.
4212 f If a .TRUE. value is given for this argument, the two
4213 f component Mappings will be connected in series. A
4214 f .FALSE. value requests that they are connected in parallel.
4215 c options
4216 f OPTIONS = CHARACTER * ( * ) (Given)
4217 c Pointer to a null-terminated string containing an optional
4218 c comma-separated list of attribute assignments to be used for
4219 c initialising the new CmpMap. The syntax used is identical to
4220 c that for the astSet function and may include "printf" format
4221 c specifiers identified by "%" symbols in the normal way.
4222 f A character string containing an optional comma-separated
4223 f list of attribute assignments to be used for initialising the
4224 f new CmpMap. The syntax used is identical to that for the
4225 f AST_SET routine.
4226 c ...
4227 c If the "options" string contains "%" format specifiers, then
4228 c an optional list of additional arguments may follow it in
4229 c order to supply values to be substituted for these
4230 c specifiers. The rules for supplying these are identical to
4231 c those for the astSet function (and for the C "printf"
4232 c function).
4233 f STATUS = INTEGER (Given and Returned)
4234 f The global status.
4235
4236 * Returned Value:
4237 c astCmpMap()
4238 f AST_CMPMAP = INTEGER
4239 * A pointer to the new CmpMap.
4240
4241 * Notes:
4242 * - If the component Mappings are connected in series, then using
4243 * the resulting CmpMap to transform coordinates will cause the
4244 * first Mapping to be applied, followed by the second Mapping. If
4245 * the inverse CmpMap transformation is requested, the two
4246 * component Mappings will be applied in both the reverse order and
4247 * the reverse direction.
4248 * - When connecting two component Mappings in series, the number
4249 * of output coordinates generated by the first Mapping (its Nout
4250 * attribute) must equal the number of input coordinates accepted
4251 * by the second Mapping (its Nin attribute).
4252 * - If the component Mappings of a CmpMap are connected in
4253 * parallel, then the first Mapping will be used to transform the
4254 * earlier input coordinates for each point (and to produce the
4255 * earlier output coordinates) and the second Mapping will be used
4256 * simultaneously to transform the remaining input coordinates (to
4257 * produce the remaining output coordinates for each point). If the
4258 * inverse transformation is requested, each Mapping will still be
4259 * applied to the same coordinates, but in the reverse direction.
4260 * - When connecting two component Mappings in parallel, there is
4261 * no restriction on the number of input and output coordinates for
4262 * each Mapping.
4263 c - Note that the component Mappings supplied are not copied by
4264 c astCmpMap (the new CmpMap simply retains a reference to
4265 c them). They may continue to be used for other purposes, but
4266 c should not be deleted. If a CmpMap containing a copy of its
4267 c component Mappings is required, then a copy of the CmpMap should
4268 c be made using astCopy.
4269 f - Note that the component Mappings supplied are not copied by
4270 f AST_CMPMAP (the new CmpMap simply retains a reference to
4271 f them). They may continue to be used for other purposes, but
4272 f should not be deleted. If a CmpMap containing a copy of its
4273 f component Mappings is required, then a copy of the CmpMap should
4274 f be made using AST_COPY.
4275 * - A null Object pointer (AST__NULL) will be returned if this
4276 c function is invoked with the AST error status set, or if it
4277 f function is invoked with STATUS set to an error value, or if it
4278 * should fail for any reason.
4279 *--
4280
4281 * Implementation Notes:
4282 * - This function implements the external (public) interface to
4283 * the astCmpMap constructor function. It returns an ID value
4284 * (instead of a true C pointer) to external users, and must be
4285 * provided because astCmpMap_ has a variable argument list which
4286 * cannot be encapsulated in a macro (where this conversion would
4287 * otherwise occur).
4288 * - Because no checking or casting of arguments is performed
4289 * before the function is invoked, the "map1" and "map2" parameters
4290 * are of type (void *) and are converted from an ID value to a
4291 * pointer and validated within the function itself.
4292 * - The variable argument list also prevents this function from
4293 * invoking astCmpMap_ directly, so it must be a re-implementation
4294 * of it in all respects, except for the conversions between IDs
4295 * and pointers on input/output of Objects.
4296 */
4297
4298 /* Local Variables: */
4299 astDECLARE_GLOBALS /* Pointer to thread-specific global data */
4300 AstCmpMap *new; /* Pointer to new CmpMap */
4301 AstMapping *map1; /* Pointer to first Mapping structure */
4302 AstMapping *map2; /* Pointer to second Mapping structure */
4303 va_list args; /* Variable argument list */
4304
4305 int *status; /* Pointer to inherited status value */
4306
4307 /* Get a pointer to the thread specific global data structure. */
4308 astGET_GLOBALS(NULL);
4309
4310 /* Initialise. */
4311 new = NULL;
4312
4313 /* Get a pointer to the inherited status value. */
4314 status = astGetStatusPtr;
4315
4316 /* Check the global status. */
4317 if ( !astOK ) return new;
4318
4319 /* Obtain the Mapping pointers from the ID's supplied and validate the
4320 pointers to ensure they identify valid Mappings. */
4321 map1 = astVerifyMapping( astMakePointer( map1_void ) );
4322 map2 = astVerifyMapping( astMakePointer( map2_void ) );
4323 if ( astOK ) {
4324
4325 /* Initialise the CmpMap, allocating memory and initialising the
4326 virtual function table as well if necessary. */
4327 new = astInitCmpMap( NULL, sizeof( AstCmpMap ), !class_init, &class_vtab,
4328 "CmpMap", map1, map2, series );
4329
4330 /* If successful, note that the virtual function table has been initialised. */
4331 if ( astOK ) {
4332 class_init = 1;
4333
4334 /* Obtain the variable argument list and pass it along with the
4335 options string to the astVSet method to initialise the new CmpMap's
4336 attributes. */
4337 va_start( args, options );
4338 astVSet( new, options, NULL, args );
4339 va_end( args );
4340
4341 /* If an error occurred, clean up by deleting the new object. */
4342 if ( !astOK ) new = astDelete( new );
4343 }
4344 }
4345
4346 /* Return an ID value for the new CmpMap. */
4347 return astMakeId( new );
4348 }
4349
astInitCmpMap_(void * mem,size_t size,int init,AstCmpMapVtab * vtab,const char * name,AstMapping * map1,AstMapping * map2,int series,int * status)4350 AstCmpMap *astInitCmpMap_( void *mem, size_t size, int init,
4351 AstCmpMapVtab *vtab, const char *name,
4352 AstMapping *map1, AstMapping *map2, int series, int *status ) {
4353 /*
4354 *+
4355 * Name:
4356 * astInitCmpMap
4357
4358 * Purpose:
4359 * Initialise a CmpMap.
4360
4361 * Type:
4362 * Protected function.
4363
4364 * Synopsis:
4365 * #include "cmpmap.h"
4366 * AstCmpMap *astInitCmpMap( void *mem, size_t size, int init,
4367 * AstCmpMapVtab *vtab, const char *name,
4368 * AstMapping *map1, AstMapping *map2,
4369 * int series )
4370
4371 * Class Membership:
4372 * CmpMap initialiser.
4373
4374 * Description:
4375 * This function is provided for use by class implementations to initialise
4376 * a new CmpMap object. It allocates memory (if necessary) to
4377 * accommodate the CmpMap plus any additional data associated with the
4378 * derived class. It then initialises a CmpMap structure at the start
4379 * of this memory. If the "init" flag is set, it also initialises the
4380 * contents of a virtual function table for a CmpMap at the start of
4381 * the memory passed via the "vtab" parameter.
4382
4383 * Parameters:
4384 * mem
4385 * A pointer to the memory in which the CmpMap is to be initialised.
4386 * This must be of sufficient size to accommodate the CmpMap data
4387 * (sizeof(CmpMap)) plus any data used by the derived class. If a
4388 * value of NULL is given, this function will allocate the memory itself
4389 * using the "size" parameter to determine its size.
4390 * size
4391 * The amount of memory used by the CmpMap (plus derived class
4392 * data). This will be used to allocate memory if a value of NULL is
4393 * given for the "mem" parameter. This value is also stored in the
4394 * CmpMap structure, so a valid value must be supplied even if not
4395 * required for allocating memory.
4396 * init
4397 * A logical flag indicating if the CmpMap's virtual function table
4398 * is to be initialised. If this value is non-zero, the virtual function
4399 * table will be initialised by this function.
4400 * vtab
4401 * Pointer to the start of the virtual function table to be associated
4402 * with the new CmpMap.
4403 * name
4404 * Pointer to a constant null-terminated character string which contains
4405 * the name of the class to which the new object belongs (it is this
4406 * pointer value that will subsequently be returned by the Object
4407 * astClass function).
4408 * map1
4409 * Pointer to the first Mapping.
4410 * map2
4411 * Pointer to the second Mapping.
4412 * series
4413 * If a non-zero value is given, the two Mappings will be connected
4414 * together in series. A zero value requests that they be connected in
4415 * parallel.
4416
4417 * Returned Value:
4418 * A pointer to the new CmpMap.
4419
4420 * Notes:
4421 * - A null pointer will be returned if this function is invoked with the
4422 * global error status set, or if it should fail for any reason.
4423 *-
4424 */
4425
4426 /* Local Variables: */
4427 AstCmpMap *new; /* Pointer to new CmpMap */
4428 int map_f; /* Forward transformation defined? */
4429 int map_i; /* Inverse transformation defined? */
4430 int nin2; /* No. input coordinates for Mapping 2 */
4431 int nin; /* No. input coordinates for CmpMap */
4432 int nout1; /* No. output coordinates for Mapping 1 */
4433 int nout; /* No. output coordinates for CmpMap */
4434
4435 /* Check the global status. */
4436 if ( !astOK ) return NULL;
4437
4438 /* If necessary, initialise the virtual function table. */
4439 if ( init ) astInitCmpMapVtab( vtab, name );
4440
4441 /* Initialise. */
4442 new = NULL;
4443
4444 /* Determine in which directions each component Mapping is able to transform
4445 coordinates. Combine these results to obtain a result for the overall
4446 CmpMap. */
4447 map_f = astGetTranForward( map1 ) && astGetTranForward( map2 );
4448 map_i = astGetTranInverse( map1 ) && astGetTranInverse( map2 );
4449 if ( astOK ) {
4450
4451 /* If connecting the Mappings in series, check that the number of coordinates
4452 are compatible and report an error if they are not. */
4453 if ( series ) {
4454 nout1 = astGetNout( map1 );
4455 nin2 = astGetNin( map2 );
4456 if ( astOK && ( nout1 != nin2 ) ) {
4457 astError( AST__INNCO, "astInitCmpMap(%s): The number of output "
4458 "coordinates per point (%d) for the first Mapping "
4459 "supplied does not match the number of input "
4460 "coordinates (%d) for the second Mapping.", status, name, nout1,
4461 nin2 );
4462 }
4463 }
4464 }
4465
4466 /* If OK, determine the total number of input and output coordinates per point
4467 for the CmpMap. */
4468 if ( astOK ) {
4469 if ( series ) {
4470 nin = astGetNin( map1 );
4471 nout = astGetNout( map2 );
4472 } else {
4473 nin = astGetNin( map1 ) + astGetNin( map2 );
4474 nout = astGetNout( map1 ) + astGetNout( map2 );
4475 }
4476
4477 } else {
4478 nin = 0;
4479 nout = 0;
4480 }
4481
4482 /* Initialise a Mapping structure (the parent class) as the first component
4483 within the CmpMap structure, allocating memory if necessary. Specify
4484 the number of input and output coordinates and in which directions the
4485 Mapping should be defined. */
4486 if ( astOK ) {
4487 new = (AstCmpMap *) astInitMapping( mem, size, 0,
4488 (AstMappingVtab *) vtab, name,
4489 nin, nout, map_f, map_i );
4490
4491 if ( astOK ) {
4492
4493 /* Initialise the CmpMap data. */
4494 /* --------------------------- */
4495 /* Store pointers to the component Mappings. Extract Mappings if
4496 FrameSets are provided. */
4497 if( astIsAFrameSet( map1 ) ) {
4498 new->map1 = astGetMapping( (AstFrameSet *) map1, AST__BASE,
4499 AST__CURRENT );
4500 } else {
4501 new->map1 = astClone( map1 );
4502 }
4503
4504 if( astIsAFrameSet( map2 ) ) {
4505 new->map2 = astGetMapping( (AstFrameSet *) map2, AST__BASE,
4506 AST__CURRENT );
4507 } else {
4508 new->map2 = astClone( map2 );
4509 }
4510
4511
4512 /* Save the initial values of the inversion flags for these Mappings. */
4513 new->invert1 = astGetInvert( new->map1 );
4514 new->invert2 = astGetInvert( new->map2 );
4515
4516 /* Note whether the Mappings are joined in series (instead of in parallel),
4517 constraining this flag to be 0 or 1. */
4518 new->series = ( series != 0 );
4519
4520 /* If an error occurred, clean up by annulling the Mapping pointers and
4521 deleting the new object. */
4522 if ( !astOK ) {
4523 new->map1 = astAnnul( new->map1 );
4524 new->map2 = astAnnul( new->map2 );
4525 new = astDelete( new );
4526 }
4527 }
4528 }
4529
4530 /* Return a pointer to the new object. */
4531 return new;
4532 }
4533
astLoadCmpMap_(void * mem,size_t size,AstCmpMapVtab * vtab,const char * name,AstChannel * channel,int * status)4534 AstCmpMap *astLoadCmpMap_( void *mem, size_t size,
4535 AstCmpMapVtab *vtab, const char *name,
4536 AstChannel *channel, int *status ) {
4537 /*
4538 *+
4539 * Name:
4540 * astLoadCmpMap
4541
4542 * Purpose:
4543 * Load a CmpMap.
4544
4545 * Type:
4546 * Protected function.
4547
4548 * Synopsis:
4549 * #include "cmpmap.h"
4550 * AstCmpMap *astLoadCmpMap( void *mem, size_t size,
4551 * AstCmpMapVtab *vtab, const char *name,
4552 * AstChannel *channel )
4553
4554 * Class Membership:
4555 * CmpMap loader.
4556
4557 * Description:
4558 * This function is provided to load a new CmpMap using data read
4559 * from a Channel. It first loads the data used by the parent class
4560 * (which allocates memory if necessary) and then initialises a
4561 * CmpMap structure in this memory, using data read from the input
4562 * Channel.
4563 *
4564 * If the "init" flag is set, it also initialises the contents of a
4565 * virtual function table for a CmpMap at the start of the memory
4566 * passed via the "vtab" parameter.
4567
4568
4569 * Parameters:
4570 * mem
4571 * A pointer to the memory into which the CmpMap is to be
4572 * loaded. This must be of sufficient size to accommodate the
4573 * CmpMap data (sizeof(CmpMap)) plus any data used by derived
4574 * classes. If a value of NULL is given, this function will
4575 * allocate the memory itself using the "size" parameter to
4576 * determine its size.
4577 * size
4578 * The amount of memory used by the CmpMap (plus derived class
4579 * data). This will be used to allocate memory if a value of
4580 * NULL is given for the "mem" parameter. This value is also
4581 * stored in the CmpMap structure, so a valid value must be
4582 * supplied even if not required for allocating memory.
4583 *
4584 * If the "vtab" parameter is NULL, the "size" value is ignored
4585 * and sizeof(AstCmpMap) is used instead.
4586 * vtab
4587 * Pointer to the start of the virtual function table to be
4588 * associated with the new CmpMap. If this is NULL, a pointer to
4589 * the (static) virtual function table for the CmpMap class is
4590 * used instead.
4591 * name
4592 * Pointer to a constant null-terminated character string which
4593 * contains the name of the class to which the new object
4594 * belongs (it is this pointer value that will subsequently be
4595 * returned by the astGetClass method).
4596 *
4597 * If the "vtab" parameter is NULL, the "name" value is ignored
4598 * and a pointer to the string "CmpMap" is used instead.
4599
4600 * Returned Value:
4601 * A pointer to the new CmpMap.
4602
4603 * Notes:
4604 * - A null pointer will be returned if this function is invoked
4605 * with the global error status set, or if it should fail for any
4606 * reason.
4607 *-
4608 */
4609
4610 /* Local Variables: */
4611 astDECLARE_GLOBALS /* Pointer to thread-specific global data */
4612 AstCmpMap *new; /* Pointer to the new CmpMap */
4613
4614 /* Initialise. */
4615 new = NULL;
4616
4617 /* Check the global error status. */
4618 if ( !astOK ) return new;
4619
4620 /* Get a pointer to the thread specific global data structure. */
4621 astGET_GLOBALS(channel);
4622
4623 /* If a NULL virtual function table has been supplied, then this is
4624 the first loader to be invoked for this CmpMap. In this case the
4625 CmpMap belongs to this class, so supply appropriate values to be
4626 passed to the parent class loader (and its parent, etc.). */
4627 if ( !vtab ) {
4628 size = sizeof( AstCmpMap );
4629 vtab = &class_vtab;
4630 name = "CmpMap";
4631
4632 /* If required, initialise the virtual function table for this class. */
4633 if ( !class_init ) {
4634 astInitCmpMapVtab( vtab, name );
4635 class_init = 1;
4636 }
4637 }
4638
4639 /* Invoke the parent class loader to load data for all the ancestral
4640 classes of the current one, returning a pointer to the resulting
4641 partly-built CmpMap. */
4642 new = astLoadMapping( mem, size, (AstMappingVtab *) vtab, name,
4643 channel );
4644
4645 if ( astOK ) {
4646
4647 /* Read input data. */
4648 /* ================ */
4649 /* Request the input Channel to read all the input data appropriate to
4650 this class into the internal "values list". */
4651 astReadClassData( channel, "CmpMap" );
4652
4653 /* Now read each individual data item from this list and use it to
4654 initialise the appropriate instance variable(s) for this class. */
4655
4656 /* In the case of attributes, we first read the "raw" input value,
4657 supplying the "unset" value as the default. If a "set" value is
4658 obtained, we then use the appropriate (private) Set... member
4659 function to validate and set the value properly. */
4660
4661 /* Series. */
4662 /* ------- */
4663 new->series = astReadInt( channel, "series", 1 );
4664 new->series = ( new->series != 0 );
4665
4666 /* First Invert flag. */
4667 /* ------------------ */
4668 new->invert1 = astReadInt( channel, "inva", 0 );
4669 new->invert1 = ( new->invert1 != 0 );
4670
4671 /* Second Invert flag. */
4672 /* ------------------- */
4673 new->invert2 = astReadInt( channel, "invb", 0 );
4674 new->invert2 = ( new->invert2 != 0 );
4675
4676 /* First Mapping. */
4677 /* -------------- */
4678 new->map1 = astReadObject( channel, "mapa", NULL );
4679
4680 /* Second Mapping. */
4681 /* --------------- */
4682 new->map2 = astReadObject( channel, "mapb", NULL );
4683
4684 /* If an error occurred, clean up by deleting the new CmpMap. */
4685 if ( !astOK ) new = astDelete( new );
4686 }
4687
4688 /* Return the new CmpMap pointer. */
4689 return new;
4690 }
4691
4692 /* Virtual function interfaces. */
4693 /* ============================ */
4694 /* These provide the external interface to the virtual functions defined by
4695 this class. Each simply checks the global error status and then locates and
4696 executes the appropriate member function, using the function pointer stored
4697 in the object's virtual function table (this pointer is located using the
4698 astMEMBER macro defined in "object.h").
4699
4700 Note that the member function may not be the one defined here, as it may
4701 have been over-ridden by a derived class. However, it should still have the
4702 same interface. */
4703
4704 /* None. */
4705
4706
4707
4708
4709
4710
4711
4712
4713