WCSLIB  4.25.1
dis.h
Go to the documentation of this file.
1 /*============================================================================
2 
3  WCSLIB 5.0 - an implementation of the FITS WCS standard.
4  Copyright (C) 1995-2015, Mark Calabretta
5 
6  This file is part of WCSLIB.
7 
8  WCSLIB is free software: you can redistribute it and/or modify it under the
9  terms of the GNU Lesser General Public License as published by the Free
10  Software Foundation, either version 3 of the License, or (at your option)
11  any later version.
12 
13  WCSLIB is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16  more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with WCSLIB. If not, see http://www.gnu.org/licenses.
20 
21  Direct correspondence concerning WCSLIB to mark@calabretta.id.au
22 
23  Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
24  http://www.atnf.csiro.au/people/Mark.Calabretta
25  $Id: dis.h,v 5.0 2015/04/05 12:24:59 mcalabre Exp $
26 *=============================================================================
27 *
28 * WCSLIB 5.0 - experimental C routines that implement proposed extensions to
29 * the FITS World Coordinate System (WCS) standard. Refer to
30 *
31 * "Representations of distortions in FITS world coordinate systems",
32 * Calabretta, M.R. et al. (Paper IV), draft dated 2004/04/22 available from
33 * http://www.atnf.csiro.au/people/Mark.Calabretta
34 *
35 * Refer to the README file provided with WCSLIB for an overview of the
36 * library.
37 *
38 *
39 * Summary of the dis routines
40 * ---------------------------
41 * These routines apply the distortion functions defined by the extension to
42 * the FITS WCS standard proposed in Paper IV. They are based on the disprm
43 * struct which contains all information needed for the computations. The
44 * struct contains some members that must be set by the user, and others that
45 * are maintained by these routines, somewhat like a C++ class but with no
46 * encapsulation.
47 *
48 * Four routines, disini(), disparms(), discpy(), and disfree() are provided to
49 * manage the disprm struct, and another, disprt(), prints its contents.
50 *
51 * A setup routine, disset(), computes intermediate values in the disprm struct
52 * from parameters in it that were supplied by the user. The struct always
53 * needs to be set up by disset(), though disset() need not be called
54 * explicitly - refer to the explanation of disprm::flag.
55 *
56 * disp2x() and disx2p() implement the WCS distortion functions, disp2x() using
57 * separate functions, such as tpv1(), to do the computation.
58 *
59 *
60 * disini() - Default constructor for the disprm struct
61 * ----------------------------------------------------
62 * disini() allocates memory for arrays in a disprm struct and sets all members
63 * of the struct to default values.
64 *
65 * In general, different distortion functions may be associated with each axis,
66 * and at the point where disini() is invoked, it is usually not known how many
67 * distortion parameters may be required for each. Hence a separate routine,
68 * disparms(), is provided to allocate memory for the parameter arrays if
69 * desired. One way or the other, after invoking disini(), it is the caller's
70 * responsibility to allocate memory (if necessary) for the distortion
71 * parameters. If disparms() is used for this purpose, then disfree() will
72 * free it and subsequent calls to disini() will manage it, e.g. if naxis
73 * changes. Of course, if there is no distortion function on an axis then
74 * there is no need to allocate memory for any parameters.
75 *
76 * PLEASE NOTE: every disprm struct must be initialized by disini(), possibly
77 * repeatedly. On the first invokation, and only the first invokation,
78 * disprm::flag must be set to -1 to initialize memory management, regardless
79 * of whether disini() will actually be used to allocate memory.
80 *
81 * Given:
82 * alloc int If true, allocate memory unconditionally for arrays in
83 * the disprm struct.
84 *
85 * If false, it is assumed that pointers to these arrays
86 * have been set by the user except if they are null
87 * pointers in which case memory will be allocated for
88 * them regardless. (In other words, setting alloc true
89 * saves having to initalize these pointers to zero.)
90 *
91 * naxis int The number of world coordinate axes, used to determine
92 * array sizes.
93 *
94 * Given and returned:
95 * dis struct disprm*
96 * Distortion function parameters. Note that, in order
97 * to initialize memory management disprm::flag must be
98 * set to -1 when dis is initialized for the first time
99 * (memory leaks may result if it had already been
100 * initialized).
101 *
102 * Function return value:
103 * int Status return value:
104 * 0: Success.
105 * 1: Null disprm pointer passed.
106 * 2: Memory allocation failed.
107 *
108 * For returns > 1, a detailed error message is set in
109 * disprm::err if enabled, see wcserr_enable().
110 *
111 *
112 * disparms() - Allocate memory for parameters in a disprm struct
113 * --------------------------------------------------------------
114 * disparms() allocates memory for the arrays of distortion function parameters
115 * in a disprm struct. It relies on disprm::nparm[] having been set to
116 * determine the number of parameters for each distortion function. The
117 * allocated memory will be freed by disfree().
118 *
119 * Given and returned:
120 * dis struct disprm*
121 * disprm::nparm[] is used to determine the number of
122 * parameters in each distortion function. Memory is
123 * allocated for the distortion function parameters,
124 * disprm::parms[][], and it is initialized to zero.
125 *
126 * Function return value:
127 * int Status return value:
128 * 0: Success.
129 * 1: Null disprm pointer passed.
130 * 2: Memory allocation failed.
131 *
132 * For returns > 1, a detailed error message is set in
133 * disprm::err if enabled, see wcserr_enable().
134 *
135 *
136 * discpy() - Copy routine for the disprm struct
137 * ---------------------------------------------
138 * discpy() does a deep copy of one disprm struct to another, using disini() to
139 * allocate memory unconditionally for its arrays if required. Only the
140 * "information to be provided" part of the struct is copied; a call to
141 * disset() is required to initialize the remainder.
142 *
143 * Given:
144 * alloc int If true, allocate memory unconditionally for arrays in
145 * the destination, including the parameter array via a
146 * call to disparms(). Otherwise, it is assumed that
147 * pointers to these arrays have been set by the user
148 * except if they are null pointers in which case memory
149 * will be allocated for them regardless.
150 *
151 * dissrc const struct disprm*
152 * Struct to copy from.
153 *
154 * Given and returned:
155 * disdst struct disprm*
156 * Struct to copy to. disprm::flag should be set to -1
157 * if disdst was not previously initialized (memory leaks
158 * may result if it was previously initialized).
159 *
160 * Function return value:
161 * int Status return value:
162 * 0: Success.
163 * 1: Null disprm pointer passed.
164 * 2: Memory allocation failed.
165 *
166 * For returns > 1, a detailed error message is set in
167 * disprm::err if enabled, see wcserr_enable().
168 *
169 *
170 * disfree() - Destructor for the disprm struct
171 * --------------------------------------------
172 * disfree() frees memory allocated for the disprm arrays by disini() and/or
173 * disparms(). disini() and disparms() keep a record of the memory they
174 * allocate and disfree() will only attempt to free this.
175 *
176 * PLEASE NOTE: disfree() must not be invoked on a disprm struct that was not
177 * initialized by disini().
178 *
179 * Given:
180 * dis struct disprm*
181 * Distortion function parameters.
182 *
183 * Function return value:
184 * int Status return value:
185 * 0: Success.
186 * 1: Null disprm pointer passed.
187 *
188 *
189 * disprt() - Print routine for the disprm struct
190 * ----------------------------------------------
191 * disprt() prints the contents of a disprm struct using wcsprintf(). Mainly
192 * intended for diagnostic purposes.
193 *
194 * Given:
195 * dis const struct disprm*
196 * Distortion function parameters.
197 *
198 * Function return value:
199 * int Status return value:
200 * 0: Success.
201 * 1: Null disprm pointer passed.
202 *
203 *
204 * disset() - Setup routine for the disprm struct
205 * ----------------------------------------------
206 * disset(), sets up the disprm struct according to information supplied within
207 * it - refer to the explanation of disprm::flag.
208 *
209 * Note that this routine need not be called directly; it will be invoked by
210 * disp2x() and disx2p() if the disprm::flag is anything other than a
211 * predefined magic value.
212 *
213 * Given and returned:
214 * dis struct disprm*
215 * Distortion function parameters.
216 *
217 * Function return value:
218 * int Status return value:
219 * 0: Success.
220 * 1: Null disprm pointer passed.
221 * 2: Memory allocation failed.
222 * 3: Invalid parameter.
223 *
224 * For returns > 1, a detailed error message is set in
225 * disprm::err if enabled, see wcserr_enable().
226 *
227 *
228 * disp2x() - Apply distortion function
229 * ------------------------------------
230 * disp2x() applies the distortion functions. By definition, the distortion
231 * is in the pixel-to-world direction.
232 *
233 * Depending on the point in the algorithm chain at which it is invoked,
234 * disp2x() may transform pixel coordinates to corrected pixel coordinates, or
235 * intermediate pixel coordinates to corrected intermediate pixel coordinates,
236 * or image coordinates to corrected image coordinates.
237 *
238 *
239 * Given and returned:
240 * dis struct disprm*
241 * Distortion function parameters.
242 *
243 * Given:
244 * rawcrd const double[]
245 * Array of coordinates.
246 *
247 * Returned:
248 * discrd double[] Array of coordinates to which the distortion functions
249 * have been applied.
250 *
251 * Function return value:
252 * int Status return value:
253 * 0: Success.
254 * 1: Null disprm pointer passed.
255 * 2: Memory allocation failed.
256 * 3: Invalid parameter.
257 * 4: Distort error.
258 *
259 * For returns > 1, a detailed error message is set in
260 * disprm::err if enabled, see wcserr_enable().
261 *
262 *
263 * disx2p() - Apply de-distortion function
264 * ---------------------------------------
265 * disx2p() applies the inverse of the distortion functions. By definition,
266 * the de-distortion is in the world-to-pixel direction.
267 *
268 * Depending on the point in the algorithm chain at which it is invoked,
269 * disx2p() may transform corrected pixel coordinates to pixel coordinates, or
270 * corrected intermediate pixel coordinates to intermediate pixel coordinates,
271 * or corrected image coordinates to image coordinates.
272 *
273 * disx2p() iteratively solves for the inverse using disp2x(). It assumes
274 * that the distortion is small and the functions are well-behaved, being
275 * continuous and with continuous derivatives. Also that, to first order
276 * in the neighbourhood of the solution, discrd[j] ~= a + b*rawcrd[j], i.e.
277 * independent of rawcrd[i], where i != j. This is effectively equivalent to
278 * assuming that the distortion functions are separable to first order.
279 * Furthermore, a is assumed to be small, and b close to unity.
280 *
281 * Given and returned:
282 * dis struct disprm*
283 * Distortion function parameters.
284 *
285 * Given:
286 * discrd const double[]
287 * Array of coordinates.
288 *
289 * Returned:
290 * rawcrd double[] Array of coordinates to which the inverse distortion
291 * functions have been applied.
292 *
293 * Function return value:
294 * int Status return value:
295 * 0: Success.
296 * 1: Null disprm pointer passed.
297 * 2: Memory allocation failed.
298 * 3: Invalid parameter.
299 * 5: De-distort error.
300 *
301 * For returns > 1, a detailed error message is set in
302 * disprm::err if enabled, see wcserr_enable().
303 *
304 *
305 * disprm struct - Distortion parameters
306 * -------------------------------------
307 * The disprm struct contains all of the information required to apply a set of
308 * distortion functions. It consists of certain members that must be set by
309 * the user ("given") and others that are set by the WCSLIB routines
310 * ("returned").
311 *
312 * int flag
313 * (Given and returned) This flag must be set to zero whenever any of the
314 * following members of the disprm struct are set or modified:
315 *
316 * - disprm::naxis,
317 * - disprm::dtype,
318 * - disprm::axmap,
319 * - disprm::nparm,
320 * - disprm::parms.
321 *
322 * This signals the initialization routine, disset(), to recompute the
323 * returned members of the disprm struct. disset() will reset flag to
324 * indicate that this has been done.
325 *
326 * PLEASE NOTE: flag must be set to -1 when disini() is called for the
327 * first time for a particular disprm struct in order to initialize memory
328 * management. It must ONLY be used on the first initialization otherwise
329 * memory leaks may result.
330 *
331 * int naxis
332 * (Given or returned) Number of pixel and world coordinate elements.
333 *
334 * If disini() is used to initialize the disprm struct (as would normally
335 * be the case) then it will set naxis from the value passed to it as a
336 * function argument. The user should not subsequently modify it.
337 *
338 * char (*dtype)[16]
339 * (Given) Pointer to the first element of an array of char[16] containing
340 * the name of the distortion function for each axis.
341 *
342 * int **axmap
343 * (Given) Pointer to the first element of an array of int* containing
344 * pointers to the first elements of the axis mapping arrays for each axis.
345 *
346 * An axis mapping associates the independent variables of a distortion
347 * function with the 1-relative image axis number. For example, consider
348 * an image with a spectrum on the first axis, followed by RA, Dec, and
349 * time axes. For a distortion in (RA,Dec) and no distortion on the
350 * spectral or time axes, the axis mapping arrays, axmap[j][], would be
351 *
352 = j=0: [0, 0, 0, 0] ...no distortion on spectral axis,
353 = 1: [2, 3, 0, 0] ...RA distortion depends on RA and Dec,
354 = 2: [3, 2, 0, 0] ...Dec distortion depends on Dec and RA,
355 = 3: [0, 0, 0, 0] ...no distortion on time axis,
356 *
357 * where zero indicates that there is no corresponding independent
358 * variable.
359 *
360 * double **offset
361 * (Given) Pointer to the first element of an array of double* containing
362 * an offset used to renormalize the independent variables of the
363 * distortion function for each axis.
364 *
365 * The offsets are subtracted from the independent variables before
366 * scaling.
367 *
368 * It is not necessary to reset the disprm struct (via disset()) when
369 * disprm::offset is changed.
370 *
371 * double **scale
372 * (Given) Pointer to the first element of an array of double* containing
373 * a scale used to renormalize the independent variables of the distortion
374 * function for each axis.
375 *
376 * The scale is applied to the independent variables after the offsets are
377 * subtracted.
378 *
379 * It is not necessary to reset the disprm struct (via disset()) when
380 * disprm::scale is changed.
381 *
382 * int *nparm
383 * (Given) Pointer to the first element of an array of int containing the
384 * number of distortion parameters for each axis.
385 *
386 * The number of parameters specified must satisfy the requirements of the
387 * particular distortion function. For example, the polynomial distortion
388 * associated with the TPV "projection" must have a number that corresponds
389 * to the degree of the polynomial:
390 *
391 = Degree 1: 4
392 = 2: 7
393 = 3: 12
394 = 4: 17
395 = 5: 24
396 = 6: 31
397 = 7: 40
398 *
399 * double **parms
400 * (Given) Pointer to the first element of an array of double* containing
401 * pointers to the first elements of the arrays of distortion parameters
402 * for each axis.
403 *
404 * disparms() may be used to allocate memory for these arrays.
405 *
406 * double *maxdis
407 * (Given) Pointer to the first element of an array of double specifying
408 * the maximum absolute value of the distortion for each axis computed over
409 * the whole image.
410 *
411 * It is not necessary to reset the disprm struct (via disset()) when
412 * disprm::maxdis is changed.
413 *
414 * double totdis
415 * (Given) The maximum absolute value of the combination of all distortion
416 * functions specified as an offset in pixel coordinates computed over the
417 * whole image.
418 *
419 * It is not necessary to reset the disprm struct (via disset()) when
420 * disprm::totdis is changed.
421 *
422 * struct wcserr *err
423 * (Returned) If enabled, when an error status is returned, this struct
424 * contains detailed information about the error, see wcserr_enable().
425 *
426 * int (**disp2x)(DISP2X_ARGS)
427 * (For internal use only.)
428 * int (**disx2p)(DISX2P_ARGS)
429 * (For internal use only.)
430 * double *tmpmem
431 * (For internal use only.)
432 * int **iwrk
433 * (For internal use only.)
434 * double **dwrk
435 * (For internal use only.)
436 * int m_flag
437 * (For internal use only.)
438 * int m_naxis
439 * (For internal use only.)
440 * char (*m_dtype)[16]
441 * (For internal use only.)
442 * int **m_axmap
443 * (For internal use only.)
444 * double **m_offset
445 * (For internal use only.)
446 * double **m_scale
447 * (For internal use only.)
448 * int *m_nparm
449 * (For internal use only.)
450 * double **m_parms
451 * (For internal use only.)
452 * double *m_maxdis
453 * (For internal use only.)
454 * int m_alloc
455 * (For internal use only.)
456 * int m_padding
457 * (For internal use only.)
458 *
459 *
460 * Global variable: const char *dis_errmsg[] - Status return messages
461 * ------------------------------------------------------------------
462 * Error messages to match the status value returned from each function.
463 *
464 *===========================================================================*/
465 
466 #ifndef WCSLIB_DIS
467 #define WCSLIB_DIS
468 
469 #ifdef __cplusplus
470 extern "C" {
471 #endif
472 
473 
474 extern const char *dis_errmsg[];
475 
477  DISERR_SUCCESS = 0, /* Success. */
478  DISERR_NULL_POINTER = 1, /* Null disprm pointer passed. */
479  DISERR_MEMORY = 2, /* Memory allocation failed. */
480  DISERR_BAD_PARAM = 3, /* Invalid parameter value. */
481  DISERR_DISTORT = 4, /* Distortion error. */
482  DISERR_DEDISTORT = 5 /* De-distortion error. */
483 };
484 
485 /* For use in declaring distortion function prototypes. */
486 #define DISP2X_ARGS int nparm, const double parms[], int ncrd, \
487 const double rawcrd[], double *discrd
488 
489 /* For use in declaring de-distortion function prototypes. */
490 #define DISX2P_ARGS int nparm, const double parms[], int ncrd, \
491 const double discrd[], double rawcrd[]
492 
493 
494 struct disprm {
495  /* Initialization flag (see the prologue above). */
496  /*------------------------------------------------------------------------*/
497  int flag; /* Set to zero to force initialization. */
498 
499  /* Parameters to be provided (see the prologue above). */
500  /*------------------------------------------------------------------------*/
501  int naxis; /* The number of pixel coordinate elements, */
502  /* given by NAXIS. */
503  char (*dtype)[16]; /* For each axis, the distortion type. */
504  int **axmap; /* For each axis, the axis mapping array. */
505  double **offset; /* For each axis, renormalization offset. */
506  double **scale; /* For each axis, renormalization scale. */
507  int *nparm; /* For each axis, the number of distortion */
508  /* parameters. */
509  double **parms; /* For each axis, the array of distortion */
510  /* parameters. */
511  double *maxdis; /* For each axis, the maximum distortion. */
512  double totdis; /* The maximum combined distortion. */
513 
514  /* Error handling, if enabled. */
515  /*------------------------------------------------------------------------*/
516  struct wcserr *err;
517 
518  /* Private - the remainder are for internal use. */
519  /*------------------------------------------------------------------------*/
520  int (**disp2x)(DISP2X_ARGS); /* For each axis, pointers to the */
521  int (**disx2p)(DISX2P_ARGS); /* distortion function and its inverse. */
522 
523  double *tmpmem;
524  double **iwrk;
525  double **dwrk;
526 
527  int m_flag, m_naxis; /* The remainder are for memory management. */
528  char (*m_dtype)[16];
529  int **m_axmap;
530  double **m_offset, **m_scale;
531  int *m_nparm;
532  double **m_parms;
533  double *m_maxdis;
535 };
536 
537 /* Size of the disprm struct in int units, used by the Fortran wrappers. */
538 #define DISLEN (sizeof(struct disprm)/sizeof(int))
539 
540 
541 int disini(int alloc, int naxis, struct disprm *dis);
542 
543 int disparms(struct disprm *dis);
544 
545 int discpy(int alloc, const struct disprm *dissrc, struct disprm *disdst);
546 
547 int disfree(struct disprm *dis);
548 
549 int disprt(const struct disprm *dis);
550 
551 int disset(struct disprm *dis);
552 
553 int disp2x(struct disprm *dis, const double rawcrd[], double discrd[]);
554 
555 int disx2p(struct disprm *dis, const double discrd[], double rawcrd[]);
556 
557 
558 /* Specialist distortion functions (internal use only). */
559 int tpvset(int j, struct disprm *dis);
560 int tpv1(DISP2X_ARGS);
561 int tpv2(DISP2X_ARGS);
562 int tpv3(DISP2X_ARGS);
563 int tpv4(DISP2X_ARGS);
564 int tpv5(DISP2X_ARGS);
565 int tpv6(DISP2X_ARGS);
566 int tpv7(DISP2X_ARGS);
567 
568 #ifdef __cplusplus
569 }
570 #endif
571 
572 #endif /* WCSLIB_DIS */
int flag
Definition: dis.h:497
int discpy(int alloc, const struct disprm *dissrc, struct disprm *disdst)
Copy routine for the disprm struct.
Definition: dis.h:481
const char * dis_errmsg[]
Status return messages.
int ** axmap
Definition: dis.h:504
int disp2x(struct disprm *dis, const double rawcrd[], double discrd[])
Apply distortion function.
double ** scale
Definition: dis.h:506
int ** m_axmap
Definition: dis.h:529
double ** m_scale
Definition: dis.h:530
Error message handling.
Definition: wcserr.h:221
int disx2p(struct disprm *dis, const double discrd[], double rawcrd[])
Apply de-distortion function.
Definition: dis.h:480
int disset(struct disprm *dis)
Setup routine for the disprm struct.
int naxis
Definition: dis.h:501
double ** m_offset
Definition: dis.h:530
int tpv5(DISP2X_ARGS)
(Internal use only.)
int tpv1(DISP2X_ARGS)
(Internal use only.)
int m_flag
Definition: dis.h:527
Definition: dis.h:482
int * nparm
Definition: dis.h:507
Distortion parameters.
Definition: dis.h:494
int tpv3(DISP2X_ARGS)
(Internal use only.)
char(* dtype)[16]
Definition: dis.h:503
Definition: dis.h:478
int disfree(struct disprm *dis)
Destructor for the disprm struct.
double ** m_parms
Definition: dis.h:532
int tpv4(DISP2X_ARGS)
(Internal use only.)
dis_errmsg_enum
Definition: dis.h:476
int m_naxis
Definition: dis.h:527
int disprt(const struct disprm *dis)
Print routine for the disprm struct.
double * tmpmem
Definition: dis.h:523
int m_padding
Definition: dis.h:534
double * maxdis
Definition: dis.h:511
int m_alloc
Definition: dis.h:534
double ** dwrk
Definition: dis.h:525
double totdis
Definition: dis.h:512
int(** disx2p)(DISX2P_ARGS)
Definition: dis.h:521
Definition: dis.h:477
int tpvset(int j, struct disprm *dis)
(Internal use only.)
struct wcserr * err
Definition: dis.h:516
int disini(int alloc, int naxis, struct disprm *dis)
Default constructor for the disprm struct.
int tpv6(DISP2X_ARGS)
(Internal use only.)
double ** parms
Definition: dis.h:509
Definition: dis.h:479
int * m_nparm
Definition: dis.h:531
int tpv2(DISP2X_ARGS)
(Internal use only.)
double * m_maxdis
Definition: dis.h:533
double ** offset
Definition: dis.h:505
#define DISX2P_ARGS
Definition: dis.h:490
int(** disp2x)(DISP2X_ARGS)
Definition: dis.h:520
double ** iwrk
Definition: dis.h:524
char(* m_dtype)[16]
Definition: dis.h:528
int tpv7(DISP2X_ARGS)
(Internal use only.)
int disparms(struct disprm *dis)
Allocate memory for parameters in a disprm struct.
#define DISP2X_ARGS
Definition: dis.h:486