IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 29, 2010, 3:55:49 PM (16 years ago)
Author:
eugene
Message:

update merges from trunk

Location:
branches/eam_branches/20100225
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20100225

  • branches/eam_branches/20100225/magic/remove/src/diffedpixels.c

    r26890 r27517  
    22#include "pmAstrometryWCS.h"
    33
    4 // #define DEBUG_PRINT 1
     4#define DEBUG_PRINT 1
    55
    66static void addTouchedPixels(streakFiles *sf, psString fileName);
     
    99static int xRight(psPlane *pt, int y);
    1010
     11#define  BL 0
     12#define  BR 1
     13#define  TL 2
     14#define  TR 3
     15
    1116// Examine the set of diff skycells and compute the pixels that were
    1217// included in difference processing
     
    3742        psString filename = psArrayGet(skycells, i);
    3843        psString resolved_name = pmConfigConvertFilename(filename, config, false, false);
    39 
     44        if (resolved_name == NULL) {
     45            psError(PS_ERR_IO, false, "failed to resolve skycell file: %s", filename);
     46            streaksExit("", PS_EXIT_DATA_ERROR);
     47        }
    4048        addTouchedPixels(sf, resolved_name);
    4149        psFree(resolved_name);
     
    8896        sf->diffedPixels->numCols, sf->diffedPixels->numRows);
    8997
    90     // convert corners of skycell to sky coordinates
     98    // convert corners of skycell to chip coordinates
    9199    // compute overlap with the chip using astrometry
    92100    psPlane pt[4];
     
    117125            streaksExit("", PS_EXIT_DATA_ERROR);
    118126        }
    119         pt[i].x = p.x;
    120         pt[i].y = p.y;
     127        pt[i].x = round(p.x);
     128        pt[i].y = round(p.y);
    121129    }
    122130
    123131    psFree(wcs);
    124132
    125     // put the corners in the desired order (see comments below)
    126 
     133    // Identify the bottom left, bottom right, top left, and top right corners
    127134    nameCorners(pt);
    128     psString type;
    129     if (pt[0].y < pt[2].y ) {
    130         type = "1";
    131     } else {
    132         type = "2";
    133     }
     135
    134136#ifdef DEBUG_PRINT
    135     printf("\nSKYCELL %s Type: %s\n", fileName, type);
    136     for (int i=0; i<4; i++) {
    137         printf("%f %f\n", pt[i].x, pt[i].y);
    138     }
     137    fprintf(stderr, "\nSKYCELL %s\n", fileName);
     138    fprintf(stderr, "BL %f %f\n", pt[BL].x, pt[BL].y);
     139    fprintf(stderr, "BR %f %f\n", pt[BR].x, pt[BR].y);
     140    fprintf(stderr, "TL %f %f\n", pt[TL].x, pt[TL].y);
     141    fprintf(stderr, "TR %f %f\n", pt[TR].x, pt[TR].y);
    139142#endif
     143
     144    int xmax = sf->diffedPixels->numCols - 1;
     145    int ymin;
     146    int ymax;
     147   
     148    if (pt[BL].y <= pt[BR].y) {
     149        ymin = fmax(0, pt[BL].y);
     150    } else {
     151        ymin = fmax(0, pt[BR].y);
     152    }
     153    if (pt[TR].y >= pt[TL].y) {
     154        ymax = fmin(pt[TR].y, sf->diffedPixels->numRows - 1);
     155    } else {
     156        ymax = fmin(pt[TL].y, sf->diffedPixels->numRows - 1);
     157    }
     158       
     159#if (DEBUG_PRINT > 1)
     160    fprintf(stderr, "\nxmin: %d xmax: %d\n", 0, xmax);
     161    fprintf(stderr, "\nymin: %d ymax: %d\n", ymin, ymax);
     162#endif
     163
    140164    // Now set the touched pixels
    141     int ymin = fmax(0, pt[1].y );
    142     int ymax = fmin(pt[3].y + 0.5, sf->diffedPixels->numRows - 1);
    143 #if (DEBUG_PRINT > 1)
    144     printf("\nymin: %d ymax: %d\n", ymin, ymax);
    145 #endif
    146165    for (int y = ymin ; y <= ymax; y++) {
    147         int xleft  = xLeft(pt, y);
    148         int xright = xRight(pt, y);
    149 
     166
     167        int xleft  = xLeft(pt, y) - 1;
     168        int xright = xRight(pt, y) + 1;
     169
     170        if (xright < 0) {
     171            continue;
     172        }
     173        if (xleft > xmax) {
     174            continue;
     175        }
    150176        if (xleft < 0) {
    151177            xleft = 0;
    152178        }
    153         if (xleft > sf->diffedPixels->numCols) {
    154             continue;
    155         }
    156         if (xright < 0) {
    157             continue;
    158         }
    159         if (xright >= sf->diffedPixels->numCols) {
    160             xright = sf->diffedPixels->numCols - 1;
     179        if (xright > xmax) {
     180            xright = xmax;
    161181        }
    162182#if (DEBUG_PRINT > 1)
    163         printf("  y: %d xleft: %d xright: %d\n", y, xleft, xright);
     183        fprintf(stderr, "  y: %d xleft: %d xright: %d\n", y, xleft, xright);
    164184#endif
    165185
     
    187207{
    188208    double x_d;
    189     if (y < pt[0].y) {
    190         // left boundary is the line from pt 0 to pt 1
    191         x_d = xOfY(&pt[0], &pt[1], y);
    192     } else {
    193         // left boundary is the line from pt 0 to pt 3
    194         x_d = xOfY(&pt[0], &pt[3], y);
    195     }
    196     return (int) (x_d + 0.5);
     209
     210    if ((y <= pt[BL].y ) && ((pt[BL].y >= pt[BR].y))) {
     211        // left boundary is the line from bottom left to bottom right
     212        x_d = xOfY(&pt[BL], &pt[BR], y);
     213    } else if ( y <= pt[TL].y ) {
     214        // left boundary is the line from Bottom left to top left
     215        x_d = xOfY(&pt[BL], &pt[TL], y);
     216    } else {
     217        // left boundary is the line from top left to top right
     218        x_d= xOfY(&pt[TL], &pt[TR], y);
     219    }
     220
     221    return (int) floor(x_d);
    197222}
    198223
     
    200225{
    201226    double x_d;
    202     if (y < pt[2].y) {
    203         // right boundary is the line from pt 1 to pt 2
    204         x_d = xOfY(&pt[1], &pt[2], y);
    205 
    206     } else {
    207         // right boundary is the line from pt 2 to pt 3
    208         x_d = xOfY(&pt[2], &pt[3], y);
    209     }
    210     return (int) x_d;
    211 }
    212 
    213 /*
    214  * To compute the overlap of a quadrilateral with a set of
    215  * points we transform the 4 corners to image coordinates
    216  * and name the 4 points of the quad
    217             pt 0 is left most (bottom most corner)
    218             pt 1 is bottom most (right most corner)
    219             pt 2 is right most (top most corner)
    220             pt 3 is top most (left most corner)
    221 
    222 * Then we can divide the quad into 3 regions A B and C based
    223 * on the y coordinate
    224 
    225 * (in the degenerate case of a rectangle aligned to the axes
    226 * we only have 1 region)
    227 * In each of the three regions A, B, and C the test for whether a
    228 * point is contained in the quad on scanline y is simply
    229 
    230         x >= xleft(y) x < xright(y)
    231 
    232 * where xleft(y) and xright(y) are the bounding lines at the given
    233 * y coordinate.
    234 
    235 The following diagrams show some examples. The 4 points are labeled 0 to 3
    236 The three regions are labeld A B and C, the boundaries between the regions
    237 ocur at pt0.y and pt2.y
    238 
    239 Example 1
    240 
    241                 3
    242                  C
    243          ---------------2           left boundary:  line 0_1 y < pt0.y
    244                  B                                  line 0_3 y >= pt0.y
    245           0--------------
    246                  A                  right boundary: line 1_2 y < pt2.y
    247                    1                                line 2_3  y >= pt.y
    248 **************************
    249 Example 2
    250                 3
    251               C
    252         0----------------
    253               B
    254       ----------------2
    255               A
    256             1
    257 
    258 **************************
    259 Example 3
    260 
    261         3       2
    262 
    263             B
    264 
    265         0       1
    266 
    267 (region A and C are empty in the case where the points form a rectangle)
    268 */
    269 
    270 /*
    271     Name the corners
    272     The following algorithm works for points that form a quadrilateral
    273     I think it also works for the situation where 3 points are co-linear
    274     and we have a triangle, but that isn't important for our purposes
    275 
    276 */
     227    if ((y <= pt[BR].y) && (pt[BR].y > pt[BL].y)) {
     228        // right boundary is the line from Bottom right to Top right
     229        x_d = xOfY(&pt[BL], &pt[BR], y);
     230    } else if ( y<= pt[TR].y) {
     231        // right boundary is the line from Top left to top right
     232        x_d = xOfY(&pt[BR], &pt[TR], y);
     233    } else {
     234        x_d = xOfY(&pt[TL], &pt[TR], y);
     235    }
     236    return (int) ceil(x_d);
     237}
    277238
    278239static void
    279240nameCorners(psPlane pt[4])
    280241{
    281     psPlane   pt0, pt1, pt2, pt3;
    282 
    283     /* find pt0 the left most (bottom most) corner */
    284     int imin = 0;
    285     int  min = (int) pt[0].x;
     242    // sort points top to bottom (in case of ties take favor rightmost)
    286243    int i;
    287     for (i=1; i<4; i++) {
    288         int x = pt[i].x;
    289         if ((x < min) ||
    290             ((x == min) && (pt[i].y < pt[imin].y))) {
    291             imin = i;
    292             min = x;
    293         }
    294     }
    295     // remve pt0 from the list
    296     pt0 = pt[imin];
    297     for (i=imin; i<3; i++) {
    298         pt[i] = pt[i+1];
    299     }
    300 
    301     // find the bottom most (right most) corner from the remaining 3
    302     imin = 0;
    303     min = pt[0].y;
    304     for (i=1; i<3; i++) {
    305         int y = pt[i].y;
    306         if ((y < min) ||
    307             ((y == min) && (pt[i].x > pt[imin].x)) ) {
    308             imin = i;
    309             min = y;
    310         }
    311     }
    312     // remve pt1 from the list
    313     pt1 = pt[imin];
    314     for (i=imin; i<3; i++) {
    315         pt[i] = pt[i+1];
    316     }
    317 
    318     // now find the right most (top most) of the remaining 2 points
    319     if ((pt[0].x > pt[1].x) ||
    320         ((pt[0].x == pt[1].x) && (pt[0].y > pt[1].y))) {
    321         pt2 = pt[0];
    322         pt3 = pt[1];
    323     } else {
    324         pt2 = pt[1];
    325         pt3 = pt[0];
    326     }
    327     /* write the outputs */
    328     pt[0] = pt0;
    329     pt[1] = pt1;
    330     pt[2] = pt2;
    331     pt[3] = pt3;
    332 }
     244    int j;
     245    for (i=0; i<4; i++) {
     246        for (j = i + 1; j < 4; j++) {
     247            if ((pt[j].y > pt[i].y) ||
     248                ((pt[j].y == pt[i].y) && (pt[j].x > pt[i].x))) {
     249                psPlane tmpPt = pt[j];
     250                pt[j] = pt[i];
     251                pt[i] = tmpPt;
     252            }
     253        }
     254    }
     255    psPlane bl, br;
     256    // sort bottom two points in x
     257    if (pt[3].x < pt[2].x) {
     258        bl = pt[3];
     259        br = pt[2];
     260    } else {
     261        bl = pt[2];
     262        br = pt[3];
     263    }
     264    // sort the top two points in x
     265    psPlane tl, tr;
     266    if (pt[0].x < pt[1].x) {
     267        tl = pt[0];
     268        tr = pt[1];
     269    } else {
     270        tl = pt[1];
     271        tr = pt[0];
     272    }
     273    pt[BL] = bl;
     274    pt[BR] = br;
     275    pt[TL] = tl;
     276    pt[TR] = tr;
     277}
Note: See TracChangeset for help on using the changeset viewer.