IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 4, 2005, 3:55:40 PM (22 years ago)
Author:
Paul Price
Message:

Updated to handle higher polynomial orders than linear, and to calculate the output size itself

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/stac/src/stacRead.c

    r2764 r2897  
    33#include "pslib.h"
    44#include "stac.h"
    5 
    6 #define ORDER 2
    75
    86psArray *stacReadImages(stacConfig *config)
     
    5452        // Read the file
    5553        psTrace("stac.read.maps",5,"Reading map file %s....\n",mapfile);
    56         psPlaneTransform *map = psPlaneTransformAlloc(ORDER,ORDER);
    5754
    58         // Format is as following:
    59         // A0  A1
    60         // B00 B01
    61         // B10 B11
    62         //
    63         // where:
    64         // x_sky = A0 + B00*x + B10*y
    65         // y_sky = A1 + B01*x + B11*y
     55        // Format is now:
     56        // order
     57        // x coefficients
     58        // y coefficients
     59        //
     60        // where the order is 1 for linear, 2 for quadratic, 3 for cubic.
     61        // and the coefficients are read by the following pseudo-code:
     62        //
     63        //      for (int k = 0; k < order + 1; k++)
     64        //          for (int j = 0; j < k; j++)
     65        //              int i = k - j;
     66        //              read coefficient of x^i y^j
     67        //
     68        // This produces the ordering:
     69        // x^0 y^0, x^1 y^0, x^0 y^1, x^2 y^0, x^1 y^1, x^0 y^2, x^3 y^0, x^2 y^1, x^1 y^2, x^0 y^3
     70        //
     71        // This is, of course, for third order polynomials.
     72        // For lower orders, the list is truncated at the appropriate level.
    6673
    67         double a0, a1, b00, b10, b01, b11;
    68 
    69         if (fscanf(mapfp,"%lf %lf %lf %lf %lf %lf", &a0, &a1, &b00, &b01, &b10, &b11) != 6) {
    70             psLogMsg("stac.read.maps", PS_LOG_ERROR, "Couldn't read all 6 transformation values from %s\n",
    71                      mapfile);
     74        int order = 0;                  // Polynomial order
     75        if (fscanf(mapfp, "%d", &order) != 1) {
     76            psLogMsg("stac.read.maps", PS_LOG_ERROR, "Unable to read map file %s\n", mapfile);
    7277            exit(EXIT_FAILURE);
    7378        }
    74         fclose(mapfp);
    75         map->x->coeff[0][0] = a0;
    76         map->x->coeff[1][0] = b00;
    77         map->x->coeff[0][1] = b10;
    78         map->y->coeff[0][0] = a1;
    79         map->y->coeff[1][0] = b01;
    80         map->y->coeff[0][1] = b11;
    8179
    82         // Mask out the cross-terms
    83         map->x->mask[1][1] = 1;
    84         map->y->mask[1][1] = 1;
    85         // Set the cross-terms to zero, just in case...
    86         map->x->coeff[1][1] = 0.0;
    87         map->y->coeff[1][1] = 0.0;
     80        psTrace("stac.read.maps", 5, "Polynomial order: %d\n", order);
     81
     82        psPlaneTransform *map = psPlaneTransformAlloc(order + 1, order + 1); // The transformation
     83        // Set coefficient masks
     84        for (int i = 0; i < order + 1; i++) {
     85            for (int j = 0; j < order + 1; j++) {
     86                if (i + j > order + 1) {
     87                    map->x->mask[i][j] = 1;
     88                    map->y->mask[i][j] = 1;
     89                } else {
     90                    map->x->mask[i][j] = 0;
     91                    map->y->mask[i][j] = 0;
     92                }
     93            }
     94        }
     95
     96        // Read x coefficients
     97        psTrace("stac.read.maps", 7, "x' = \n");
     98        for (int k = 0; k < order + 1; k++) {
     99            for (int j = 0; j < k + 1; j++) {
     100                int i = k - j;
     101                if (fscanf(mapfp, "%lf", &map->x->coeff[i][j]) != 1) {
     102                    psLogMsg("stac.read.maps", PS_LOG_ERROR, "Unable to read map file %s\n", mapfile);
     103                    exit(EXIT_FAILURE);
     104                }
     105                psTrace("stac.read.maps", 7, "      %f x^%d y^%d\n", map->x->coeff[i][j], i, j);
     106            }
     107        }
     108        // Read y coefficients
     109        psTrace("stac.read.maps", 7, "y' = \n");
     110        for (int k = 0; k < order + 1; k++) {
     111            for (int j = 0; j < k + 1; j++) {
     112                int i = k - j;
     113                if (fscanf(mapfp, "%lf", &map->y->coeff[i][j]) != 1) {
     114                    psLogMsg("stac.read.maps", PS_LOG_ERROR, "Unable to read map file %s\n", mapfile);
     115                    exit(EXIT_FAILURE);
     116                }
     117                psTrace("stac.read.maps", 7, "      %f x^%d y^%d\n", map->y->coeff[i][j], i, j);
     118            }
     119        }
     120
     121        fclose(mapfp);
    88122
    89123        // Plug it into the array
    90124        maps->data[i] = map;
    91 
    92 #ifdef TESTING
    93         // Check psLib's answer is the same as I would get with old version
    94         double detx = 256.0, dety = 256.0;
    95         // My code
    96         double myx = a0 + b00*detx + b10*dety;
    97         double myy = a1 + b01*detx + b11*dety;
    98         // psLib
    99         psPlane *det = psAlloc(sizeof(psPlane));
    100         det->x = 256.0;
    101         det->y = 256.0;
    102         psPlane *pslib = psPlaneTransformApply(NULL, map, det);
    103 
    104         psTrace("stac.read.maps.test",0,"me: %lf,%lf\n",myx,myy);
    105         psTrace("stac.read.maps.test",0,"ps: %lf,%lf\n",pslib->x,pslib->y);
    106         psFree(det);
    107         psFree(pslib);
    108 #endif
    109125
    110126    }
Note: See TracChangeset for help on using the changeset viewer.