IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 6, 2007, 5:47:41 PM (19 years ago)
Author:
eugene
Message:

improvements to skycells

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/addstar/src/sky_tessalation.c

    r12773 r12775  
    11# include "skycells.h"
    22# include "assert.h"
     3# define iSWAP(X,Y) {int tmp=(X); (X) = (Y); (Y) = tmp;}
    34
    45static Coords *refcoords = NULL;
    5 
    6 Image *sky_tessalation (int level, int *nimages, int mode) {
     6static int warned = FALSE;
     7
     8Image *sky_tessalation (int level, int *nimages, int mode, double scale) {
    79
    810  Image *images;
    911
    1012  if (mode == SQUARES) {
    11     images = sky_tessalation_squares (level, nimages);
     13    images = sky_tessalation_squares (level, nimages, scale);
    1214    return images;
    1315  }
    1416
    1517  if (mode == TRIANGLES) {
    16     images = sky_tessalation_triangles (level, nimages);
     18    images = sky_tessalation_triangles (level, nimages, scale);
    1719    return images;
    1820  }
     
    2123}
    2224
    23 Image *sky_tessalation_triangles (int level, int *nimages) {
     25Image *sky_tessalation_triangles (int level, int *nimages, double scale) {
    2426
    2527  int i, Ndigit, Ntriangles;
     
    3133  snprintf (format, 16, "skytri.%%0%dd", Ndigit);
    3234
    33   sky_tessalation_init ();
     35  sky_tessalation_init (scale);
    3436
    3537  tri = sky_base_triangles (&Ntriangles);
     
    5153}
    5254
    53 Image *sky_tessalation_squares (int level, int *nimages) {
     55Image *sky_tessalation_squares (int level, int *nimages, double scale) {
    5456
    5557  int i, N, Ndigit, Ntriangles;
     
    6264  snprintf (format, 16, "skycell.%%0%dd", Ndigit);
    6365
    64   sky_tessalation_init ();
     66  sky_tessalation_init (scale);
    6567
    6668  tri = sky_base_triangles (&Ntriangles);
     
    7779  } 
    7880
     81  // FILE *f1 = fopen ("center.keep.dat", "w");
     82  // FILE *f2 = fopen ("center.drop.dat", "w");
     83
    7984  ALLOCATE (outimage, Image, Ntriangles);
    8085  for (i = N = 0; i < Ntriangles; i++) {
    81     if (!strcmp(image[i].coords.ctype, "DROP")) continue;
     86    if (!strcmp(image[i].coords.ctype, "DROP")) {
     87      // fprintf (f2, "%f %f\n", tri[i].r, tri[i].d);
     88      continue;
     89    } else {
     90      // fprintf (f1, "%f %f\n", tri[i].r, tri[i].d);
     91    }
    8292    memcpy (&outimage[N], &image[i], sizeof(Image));
    8393    snprintf (outimage[N].name, 32, format, i);
     
    8595  } 
    8696  free (image);
     97
     98  // fclose (f1);
     99  // fclose (f2);
    87100
    88101  *nimages = N;
     
    159172int sky_triangle_to_rectangle (Image *image, SkyTriangle *triangle) {
    160173
    161   int i, n, parity, peak, b1, b2, NX, NY;
     174  int i, n, parity, peak, b1, b2, NX, NY, right;
    162175  double xv[3], yv[3];        // coordinates of the vertex in the reference projection
    163176  double xo, yo, xc, yc, angle, scale;
    164177  double Xmin, Xmax, Ymin, Ymax;
    165178  double dB, dP, s1, s2, r1, r2, dr, dx, dy;
     179  double angle_b1, angle_b2, slope;
    166180
    167181  // calculate the triangle coordinates in r,d
     
    195209  b1 = (peak + 1) % 3;
    196210  b2 = (peak + 2) % 3;
     211
     212  // angle is from the center to the peak corner
     213  angle_b1 = DEG_RAD*atan(yv[b1] / xv[b1]);
     214  angle_b2 = DEG_RAD*atan(yv[b2] / xv[b2]);
     215
     216  // if one of the base-center angles is very small, the parity is marginal.  Use additional
     217  // information to choose the parity.  note that both angle_b1 and angle_b2 cannot be close to
     218  // zero.
     219  if (fabs(angle_b1) < 10.0) {
     220    right = (xv[b1] > 0);     // pointing left or right?
     221    slope = (xv[peak] -  xv[b2]) / (yv[peak] - yv[b2]);
     222    if ( right && (slope >= 0.0)) parity = +1;
     223    if ( right && (slope <  0.0)) parity = -1;
     224    if (!right && (slope <= 0.0)) parity = +1;
     225    if (!right && (slope >  0.0)) parity = -1;
     226    if (parity > 0) {
     227      if (yv[peak] < yv[b2]) iSWAP(peak, b2); // require peak to be top (bottom) point
     228    } else {
     229      if (yv[peak] > yv[b2]) iSWAP(peak, b2); // require peak to be top (bottom) point
     230    }
     231    angle = atan2(parity*xv[peak], parity*yv[peak]); // note that this is x/y not y/x (and in radians)
     232  }
     233  if (fabs(angle_b2) < 10.0) {
     234    right = (xv[b2] > 0);     // pointing left or right?
     235    slope = (xv[peak] - xv[b1]) / (yv[peak] - yv[b1]); // tilt of opposite line
     236    if ( right && (slope >= 0.0)) parity = +1;
     237    if ( right && (slope <  0.0)) parity = -1;
     238    if (!right && (slope <= 0.0)) parity = +1;
     239    if (!right && (slope >  0.0)) parity = -1;
     240    if (parity > 0) {
     241      if (yv[peak] < yv[b1]) iSWAP(peak, b1); // require peak to be top (bottom) point
     242    } else {
     243      if (yv[peak] > yv[b1]) iSWAP(peak, b1); // require peak to be top (bottom) point
     244    }
     245    angle = atan2(parity*xv[peak], parity*yv[peak]); // note that this is x/y not y/x (and in radians)
     246  }
    197247
    198248  // xo, yo is the center of the baseline
     
    229279  NY = hypot((xv[peak]-xo),(yv[peak]-yo));
    230280
    231 
    232281  memset (image, 0, sizeof(Image));
    233282  image[0].coords = *refcoords;
     
    237286  image[0].coords.pc2_2 = +cos(angle);
    238287
    239   // XXX crpix1,2 should be one of the corners for RA---TAN!!!
    240   // 0.5NX + xc, 0.5NY + yc or something like that
    241   // is this the right sign?
     288  // crpix1,crpix2 is the projection center
    242289  image[0].coords.crpix1 = 0.5*NX - xc;
    243290  image[0].coords.crpix2 = 0.5*NY - yc;
    244291
    245292  // only keep one of the parity images
    246   if (parity == +1) {
     293  if (((triangle[0].d >= 0) && (parity == +1)) || ((triangle[0].d < 0) && (parity == -1))) {
    247294    strcpy (image[0].coords.ctype, "RA---TAN");
    248295  } else {
     
    250297  }
    251298  if ((NX > 60000) || (NY > 60000)) {
     299    if (!warned) {
     300      fprintf (stderr, "warning: NX,NY too big for DVO limits; modifying pixel scale\n");
     301      warned = TRUE;
     302    }
    252303    scale = NX / 60000.0;
    253304    scale = MAX(scale, NY / 60000.0);
     
    317368      out[4*i + 3].vertex[j] = out[4*i + j].vertex[1];
    318369    }
    319 # if (0)
    320     fprintf (stdout, "%f %f %f  %f %f %f  %f %f %f\n",
    321              in[i].vertex[0].x, in[i].vertex[0].y, in[i].vertex[0].z,
    322              in[i].vertex[1].x, in[i].vertex[1].y, in[i].vertex[1].z,
    323              in[i].vertex[2].x, in[i].vertex[2].y, in[i].vertex[2].z);
    324     for (j = 0; j < 4; j++) {
    325       fprintf (stdout, "%f %f %f  %f %f %f  %f %f %f\n",
    326                out[4*i+j].vertex[0].x, out[4*i+j].vertex[0].y, out[4*i+j].vertex[0].z,
    327                out[4*i+j].vertex[1].x, out[4*i+j].vertex[1].y, out[4*i+j].vertex[1].z,
    328                out[4*i+j].vertex[2].x, out[4*i+j].vertex[2].y, out[4*i+j].vertex[2].z);
    329     }
    330 # endif 
    331370    Nt += 4;
    332371  }
     
    440479}
    441480
    442 int sky_tessalation_init () {
     481int sky_tessalation_init (double scale) {
    443482
    444483  ALLOCATE (refcoords, Coords, 1);
    445484  refcoords[0].crval1 = refcoords[0].crval2 = 0.0;
    446485  refcoords[0].crpix1 = refcoords[0].crpix2 = 0.0;
    447   refcoords[0].cdelt1 = refcoords[0].cdelt2 = 1.0 / 3600;
     486  refcoords[0].cdelt1 = refcoords[0].cdelt2 = scale / 3600;
    448487  refcoords[0].pc1_1 = refcoords[0].pc2_2 = 1.0;
    449488  refcoords[0].pc1_2 = refcoords[0].pc2_1 = 0.0;
     
    453492  return (TRUE);
    454493}
    455 
    456 
    457 
    458 /*** this code was an attempt to fit the triangle boundaries into NX, NY ***/
    459 # if (0)
    460   for (n = 0; n < 3; n++) {
    461     // we will project to the triangle center position
    462     refcoords[0].crval1 = triangle[0].r;
    463     refcoords[0].crval2 = triangle[0].d;
    464 
    465     // find the size, rotation, and parity of the image
    466     // project the vertices and find the image parity
    467     parity = 1;
    468     for (i = 0; i < 3; i++) {
    469       RD_to_XY (&xv[i], &yv[i], triangle[0].rv[i], triangle[0].dv[i], refcoords);
    470       parity *= SIGN(yv[i]);
    471     }
    472 
    473     // choose the peak vertex
    474     peak = -1;
    475     for (i = 0; (peak == -1) && (i < 3); i++) {
    476       if (parity == SIGN(yv[i])) {
    477         peak = i;
    478       }
    479     }
    480     assert (peak != -1);
    481 
    482     // find the base and height
    483     b1 = (peak + 1) % 3;
    484     b2 = (peak + 2) % 3;
    485 
    486     // xo, yo is the center of the baseline
    487     xo = 0.5*(xv[b2] + xv[b1]);
    488     yo = 0.5*(yv[b2] + yv[b1]);
    489 
    490     if (n != 2) {
    491       // xc, yc is the true image center
    492       xc = 0.5*(xv[peak] + xo);
    493       yc = 0.5*(yv[peak] + yo);
    494       XY_to_RD (&triangle[0].r, &triangle[0].d, xc, yc, refcoords);
    495     }
    496   } 
    497 
    498   NX = hypot((xv[b2]-xv[b1]),(yv[b2]-yv[b1]));
    499   NY = hypot((xv[peak]-xo),(yv[peak]-yo));
    500   angle = atan2(parity*xv[peak], parity*yv[peak]); // note that this is x/y not y/x (and in radians)
    501 
    502   memset (image, 0, sizeof(Image));
    503   image[0].coords = *refcoords;
    504   image[0].coords.pc1_1 = +cos(angle);
    505   image[0].coords.pc1_2 = +sin(angle);
    506   image[0].coords.pc2_1 = -sin(angle);
    507   image[0].coords.pc2_2 = +cos(angle);
    508 
    509   if (parity == 1) {
    510     strcpy (image[0].coords.ctype, "TRP--TAN");
    511   } else {
    512     strcpy (image[0].coords.ctype, "TRM--TAN");
    513   }
    514   sprintf (image[0].name, "test.%03d", i);
    515   if ((NX > 60000) || (NY > 60000)) {
    516     scale = NX / 60000.0;
    517     scale = MAX(scale, NY / 60000.0);
    518     NX /= scale;
    519     NY /= scale;
    520     image[0].coords.cdelt1 *= scale;
    521     image[0].coords.cdelt2 *= scale;
    522   }
    523   image[0].NX = NX;
    524   image[0].NY = NY;
    525   image[0].code = 1;
    526 # endif
Note: See TracChangeset for help on using the changeset viewer.