MUSE Pipeline Reference Manual  2.1.1
muse_pixgrid.h
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2005-2017 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef MUSE_PIXGRID_H
23 #define MUSE_PIXGRID_H
24 
25 /*----------------------------------------------------------------------------*
26  * Includes *
27  *----------------------------------------------------------------------------*/
28 #include "muse_pixtable.h"
29 
30 /*----------------------------------------------------------------------------*
31  * Defines *
32  *----------------------------------------------------------------------------*/
33 
34 /* use bits 0-52 for the value (the pixel table row), this *
35  * allows to convert pixel tables with up to 9e15 pixels *
36  * (about 25 million MUSE exposures!) into a pixel grid */
37 #define PT_IDX_MASK 0x1FFFFFFFFFFFFFll
38 /* use bits 53-62 to store the thread ID, this *
39  * allows parallelization with up to 1024 cores */
40 #define XMAP_BITMASK 0x3FFll /* 1023 */
41 #define XMAP_LSHIFT 53ll
42 
43 /*----------------------------------------------------------------------------*
44  * Special variable types *
45  *----------------------------------------------------------------------------*/
46 
50 /*----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------*/
55 typedef struct {
56  unsigned int npix; /*<< number of pixels in this grid point */
57  cpl_size *pix; /*<< the row number(s) in the pixel table */
59 
60 /*----------------------------------------------------------------------------*/
64 /*----------------------------------------------------------------------------*/
65 typedef struct {
66  cpl_size *pix; /*<< The pixel grid array, elements can be *
67  * 0: empty *
68  * positive: row_number in the pixel table *
69  * negative: -(i_ext+1) in the extension array, *
70  * bits 53-62 contain the map index */
71  cpl_size nx; /*<< horizontal spatial size */
72  cpl_size ny; /*<< vertical spatial size */
73  cpl_size nz; /*<< size in dispersion direction */
74  unsigned short nmaps; /*<< number of extension maps */
75  cpl_size *nxmap; /*<< number of filled pixels in the extension maps */
76  cpl_size *nxalloc; /*<< number of allocated pixels in the extension maps */ // XXX needed?!?
77  muse_pixels_ext **xmaps; /*<< the extension maps */
78 } muse_pixgrid;
79 
80 /*----------------------------------------------------------------------------*
81  * Inline functions *
82  *----------------------------------------------------------------------------*/
83 
84 /*---------------------------------------------------------------------------*/
98 /*---------------------------------------------------------------------------*/
99 static inline cpl_size
100 muse_pixgrid_get_index(muse_pixgrid *aGrid, cpl_size aX, cpl_size aY,
101  cpl_size aZ, cpl_boolean aAllowOutside)
102 {
103  if (!aAllowOutside &&
104  (aX < 0 || aX >= aGrid->nx || aY < 0 || aY >= aGrid->ny ||
105  aZ < 0 || aZ >= aGrid->nz)) {
106  return -1;
107  }
108  if (aX < 0) {
109  aX = 0;
110  }
111  if (aX >= aGrid->nx) {
112  aX = aGrid->nx - 1;
113  }
114  if (aY < 0) {
115  aY = 0;
116  }
117  if (aY >= aGrid->ny) {
118  aY = aGrid->ny - 1;
119  }
120  if (aZ < 0) {
121  aZ = 0;
122  }
123  if (aZ >= aGrid->nz) {
124  aZ = aGrid->nz - 1;
125  }
126  return aX + aGrid->nx * (aY + aGrid->ny * aZ);
127 } /* muse_pixgrid_get_index() */
128 
129 /*---------------------------------------------------------------------------*/
138 /*---------------------------------------------------------------------------*/
139 static inline cpl_size
140 muse_pixgrid_get_count(muse_pixgrid *aGrid, cpl_size aIndex)
141 {
142  if (aIndex < 0) {
143  return 0;
144  }
145  /* get entry in pixel grid */
146  cpl_size p = aGrid->pix[aIndex];
147  if (p == 0) { /* points nowhere --> no pixels */
148  return 0;
149  }
150  if (p > 0) { /* points to pixel table --> 1 pixel */
151  return 1;
152  }
153  /* p is negative, so points to an extension map, get its index */
154  unsigned short ix = (-p >> XMAP_LSHIFT) & XMAP_BITMASK;
155  p = (-p - 1) & PT_IDX_MASK;
156  /* the npix component now gives the number of pixels in this grid index */
157  return aGrid->xmaps[ix][p].npix;
158 } /* muse_pixgrid_get_count() */
159 
160 /*---------------------------------------------------------------------------*/
169 /*---------------------------------------------------------------------------*/
170 static inline const cpl_size *
171 muse_pixgrid_get_rows(muse_pixgrid *aGrid, cpl_size aIndex)
172 {
173  if (aIndex < 0) {
174  return 0;
175  }
176  /* get entry in pixel grid */
177  cpl_size p = aGrid->pix[aIndex];
178  if (p == 0) { /* points nowhere --> no array */
179  return NULL;
180  }
181  if (p > 0) { /* points to pixel table */
182  return aGrid->pix + aIndex;
183  }
184  /* p is negative, so points to an extension map, get its array */
185  unsigned short ix = (-p >> XMAP_LSHIFT) & XMAP_BITMASK;
186  p = (-p - 1) & PT_IDX_MASK;
187  /* the pix component provides the array of pixel table rows in this index */
188  return aGrid->xmaps[ix][p].pix;
189 } /* muse_pixgrid_get_rows() */
190 
193 /*----------------------------------------------------------------------------*
194  * Function prototypes *
195  *----------------------------------------------------------------------------*/
196 muse_pixgrid *muse_pixgrid_create(muse_pixtable *, cpl_propertylist *, cpl_size, cpl_size, cpl_size);
197 muse_pixgrid *muse_pixgrid_2d_create(cpl_table *, double, double, double, double, float *);
199 
200 #endif /* MUSE_PIXGRID_H */
muse_pixgrid * muse_pixgrid_2d_create(cpl_table *, double, double, double, double, float *)
Convert selected rows of a pixel table into 2D pixel grid, linking the grid points to entries (=rows)...
Definition: muse_pixgrid.c:507
muse_pixgrid * muse_pixgrid_create(muse_pixtable *, cpl_propertylist *, cpl_size, cpl_size, cpl_size)
Convert selected rows of a pixel table into pixel grid, linking the grid points to entries (=rows) in...
Definition: muse_pixgrid.c:237
The pixel extension map.
Definition: muse_pixgrid.h:55
The pixel grid.
Definition: muse_pixgrid.h:65
void muse_pixgrid_delete(muse_pixgrid *)
Delete a pixel grid and remove its memory.
Definition: muse_pixgrid.c:575
Structure definition of MUSE pixel table.
static const cpl_size * muse_pixgrid_get_rows(muse_pixgrid *aGrid, cpl_size aIndex)
Return a pointer to the rows stored in one pixel.
Definition: muse_pixgrid.h:171
static cpl_size muse_pixgrid_get_index(muse_pixgrid *aGrid, cpl_size aX, cpl_size aY, cpl_size aZ, cpl_boolean aAllowOutside)
Get the grid index determined from all three coordinates.
Definition: muse_pixgrid.h:100
static cpl_size muse_pixgrid_get_count(muse_pixgrid *aGrid, cpl_size aIndex)
Return the number of rows stored in one pixel.
Definition: muse_pixgrid.h:140