IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 5, 2009, 1:41:10 PM (17 years ago)
Author:
bills
Message:

Changes from Paul Sydney to fix problems with mask like computation at the edges of skycells.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/magic/remove/src/Line.c

    r21156 r24344  
    1616{
    1717    double temp = *first;
     18    *first = *second;
     19    *second = temp;
     20}
     21
     22/** Internal routine to swap integer values */
     23
     24void SwapInt (int* first, int* second)
     25{
     26    int temp = *first;
    1827    *first = *second;
    1928    *second = temp;
     
    255264}
    256265
    257 /** Map a line to an image for its specified width and store as a list
    258     of pixel positions
     266/** Move a line by the specified X and Y offsets
     267    @param[in,out] line Line to move by X and Y offsets
     268    @param[in] xOffset X shift applied to both endpoints
     269    @param[in] yOffset Y shift applied to both endpoints        */
     270
     271void LineMove (Line *line, double xOffset, double yOffset)
     272{
     273    line->begin.x += xOffset;
     274    line->begin.y += yOffset;
     275    line->end.x   += xOffset;
     276    line->end.y   += yOffset;
     277}
     278
     279/** Return the maximum bounds between the line endpoints and
     280    current bounds
     281    @param[in] line Line endpoints to compare
     282    @param[in,out] xMin minimum X value to update
     283    @param[in,out] xMax maximum X value to update
     284    @param[in,out] yMin minimum Y value to update
     285    @param[in,out] yMax maximum Y value to update               */
     286
     287void MaxBounds (Line *line, int *xMin, int *xMax,  int *yMin, int *yMax)
     288{
     289    if (line->begin.x < *xMin) *xMin = (int) floor (line->begin.x);
     290    if (line->end.x   < *xMin) *xMin = (int) floor (line->end.x);
     291    if (line->begin.y < *yMin) *yMin = (int) floor (line->begin.y);
     292    if (line->end.y   < *yMin) *yMin = (int) floor (line->end.y);
     293
     294    if (line->begin.x > *xMax) *xMax = (int) ceil (line->begin.x);
     295    if (line->end.x   > *xMax) *xMax = (int) ceil (line->end.x);
     296    if (line->begin.y > *yMax) *yMax = (int) ceil (line->begin.y);
     297    if (line->end.y   > *yMax) *yMax = (int) ceil (line->end.y);
     298}
     299
     300/** Map a line to an image for its specified width and store as
     301    a list of pixel positions
    259302
    260303    @param[out] pixels list of PixelPos pointers corresponding
    261304                       based on the line settings
    262     @param[in] line Line to map to pixels                       */
    263 
    264 void PixelsFromLine (StreakPixels* pixels, Line *line)
    265 {
     305    @param[in] line Line to map to pixels   
     306    @param[in] numCols maximum X (columns) for the line
     307    @param[in] numRows maximum Y (rows) for the line            */
     308
     309void PixelsFromLine (StreakPixels* pixels, Line *line, int numCols, int numRows)
     310{
     311    Line offsetLine;
    266312    PixelPos *pixel;
    267     double slope, xOffset, yOffset, xMid, yMid, xBegin, yBegin, xEnd, yEnd, x, y;
     313    double slope, xOffset, yOffset, xMid, yMid;
     314    int x, y, xBegin = numCols, yBegin = numRows, xEnd = 0, yEnd = 0;
    268315
    269316    // Extract the endpoints
     
    280327    double dr = sqrt (dx * dx + dy * dy);
    281328    double halfWidth  = line->width / 2.0;
    282     double halfWidth2 = halfWidth * halfWidth;
    283329    if (!dr) return;
    284    
     330
     331    // Compute the intercepts of line width bounds and determine maximum
     332    // bounds in each axis
     333
     334    xOffset = -halfWidth * dy / dr;
     335    yOffset =  halfWidth * dx / dr;
     336
     337    offsetLine = *line;
     338    LineMove (&offsetLine, xOffset, yOffset);
     339    if (LineClip (&offsetLine, numCols, numRows))
     340        MaxBounds (&offsetLine, &xBegin, &xEnd, &yBegin, &yEnd);
     341
     342    offsetLine = *line;
     343    LineMove (&offsetLine, -xOffset, -yOffset);
     344    if (LineClip (&offsetLine, numCols, numRows))
     345        MaxBounds (&offsetLine, &xBegin, &xEnd, &yBegin, &yEnd);
     346
     347    if (xBegin > xEnd)
     348        SwapInt (&xBegin, &xEnd);
     349    else
     350        ++xEnd;
     351    if (yBegin > yEnd)
     352        SwapInt (&yBegin, &yEnd);
     353    else
     354        ++yEnd;
     355
    285356    // Step point by point based on the dominate axis
    286357   
     
    307378        // Compute the x and y offsets for the line width extent
    308379
    309         xOffset = halfWidth * dy / dr;
    310         yOffset = halfWidth * dr / dx;
    311         yMid   = y1 + slope * (floor (x1 - xOffset) - x1);
    312         xBegin = floor (x1 - xOffset);
    313         xEnd   = ceil  (x2 + xOffset) + 1.0;
    314 
    315         for (x = xBegin; x < xEnd; ++x)
    316         {
    317             yBegin = floor (yMid - yOffset);
    318             yEnd   = ceil  (yMid + yOffset) + 1.0;
    319             for (y = yBegin; y < yEnd; ++y)
     380        yMid = y1 + slope * (xBegin - x1);
     381        yOffset = fabs (halfWidth * dr / dx);
     382
     383        for (x = xBegin; x != xEnd; ++x)
     384        {
     385            yBegin = (int) floor (yMid - yOffset);
     386            yEnd   = (int) ceil  (yMid + yOffset) + 1;
     387            for (y = yBegin; y != yEnd; ++y)
    320388            {
    321                 if (DistanceSquared (line, x, y) <= halfWidth2)
     389                if (x >=0 && x < numCols && y >= 0 && y < numRows)
    322390                {
    323                     if (x >=0 && y >= 0) {
    324                         pixel = psAlloc (sizeof(PixelPos));
    325                         pixel->x = (unsigned int) x;
    326                         pixel->y = (unsigned int) y;
    327                         psArrayAdd (pixels, 1024, pixel);
    328                         psFree (pixel);
    329                     }
     391                    pixel = psAlloc (sizeof(PixelPos));
     392                    pixel->x = (unsigned int) x;
     393                    pixel->y = (unsigned int) y;
     394                    psArrayAdd (pixels, 1024, pixel);
     395                    psFree (pixel);
    330396                }
    331397            }
     
    354420       
    355421        // Compute the x and y offsets for the line width extent
    356        
    357         xOffset = halfWidth * dr / dy;
    358         yOffset = halfWidth * dx / dr;
    359 
    360         xMid   = x1 + slope * (floor (y1 - yOffset) - y1);
    361         yBegin = floor (y1 - yOffset);
    362         yEnd   = ceil  (y2 + yOffset) + 1.0;
    363        
    364         for (y = yBegin; y < yEnd; ++y)
    365         {
    366             xBegin = floor (xMid - xOffset);
    367             xEnd   = ceil  (xMid + xOffset) + 1.0;
    368             for (x = xBegin; x < xEnd; ++x)
     422
     423        xMid = x1 + slope * (yBegin - y1);
     424        xOffset = fabs (halfWidth * dr / dy);
     425       
     426        for (y = yBegin; y != yEnd; ++y)
     427        {
     428            xBegin = (int) floor (xMid - xOffset);
     429            xEnd   = (int) ceil  (xMid + xOffset) + 1;
     430            for (x = xBegin; x != xEnd; ++x)
    369431            {
    370                 if (DistanceSquared (line, x, y) <= halfWidth2)
     432                if (x >=0 && x < numCols && y >= 0 && y < numRows)
    371433                {
    372                     if (x >=0 && y >= 0) {
    373                         pixel = psAlloc (sizeof(PixelPos));
    374                         pixel->x = (unsigned int) x;
    375                         pixel->y = (unsigned int) y;
    376                         psArrayAdd (pixels, 1024, pixel);
    377                         psFree (pixel);
    378                     }
     434                    pixel = psAlloc (sizeof(PixelPos));
     435                    pixel->x = (unsigned int) x;
     436                    pixel->y = (unsigned int) y;
     437                    psArrayAdd (pixels, 1024, pixel);
     438                    psFree (pixel);
    379439                }
    380440            }
Note: See TracChangeset for help on using the changeset viewer.