Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

snap_to_image.C

Go to the documentation of this file.
00001 
00013 
00014 //.............................................................................
00015 //
00016 //    version 1:  Nov 1998   Steve McMillan      email: steve@zonker.drexel.edu
00017 //                           Drexel University, Philadelphia, PA, USA
00018 //.............................................................................
00019 
00020 #include "hdyn.h"
00021 
00022 void write_image(float* a, int m, int n, char* filename, int scale);
00023 
00024 #define IOFF    10
00025 #define CMAX    256
00026 
00027 #define L       2.0
00028 #define NX      256
00029 #define NY      256
00030 
00031 void add_point(float* a, int nx, int ny, int i, int j, real r, float color)
00032 {
00033     // Stars are square for r < 2, have corners removed for r = 2,
00034     // are approximate circles for r > 2.
00035 
00036     int ir = (int)r;
00037 
00038     for (int ii = max(-ir, -i); ii <= min(ir, nx-i); ii++)
00039         for (int jj = max(-ir, -j); jj <= min(ir, ny-j); jj++)
00040             if (ir < 2
00041 //              || (ir == 2 && (abs(ii) < ir || abs(jj) < ir))
00042                 || (ii*ii + jj*jj <= (ir+0.5)*(ir+0.5)))
00043                 *(a+(j+jj)*nx+(i+ii)) = color;
00044 }
00045 
00046 main(int argc, char** argv)
00047 {
00048     int count = 0, count1 = 0;
00049     char filename[64], command[64];
00050     char* fn;
00051 
00052     real l = L;
00053     int nx = NX, ny = NY;
00054     int n = 0, nskip = 0;
00055     int psize = 3;
00056     int axis = 3;               // 1 = x, 2 = y, 3 = z
00057 
00058     char file[64];
00059     strcpy(file, "snap");
00060 
00061     bool mass = false;
00062 
00063     check_help();
00064 
00065     extern char *poptarg;
00066     int c;
00067     char* param_string = "f:l:mn:p:P:s:S:";
00068 
00069     while ((c = pgetopt(argc, argv, param_string)) != -1) {
00070         switch (c) {
00071             case 'f':   strncpy(file, poptarg, 63);
00072                         file[63] = '\0';        // just in case
00073                         break;
00074             case 'l':   l = atof(poptarg);
00075                         break;
00076             case 'm':   mass = true;
00077                         break;
00078             case 'n':   n = atoi(poptarg);
00079                         break;
00080             case 'p':   psize = atoi(poptarg);
00081                         if (psize < 1) psize = 1;
00082                         break;
00083             case 'P':   if (poptarg[0] == 'x' || atoi(poptarg) == 1)
00084                             axis = 1;
00085                         else if (poptarg[0] == 'y' || atoi(poptarg) == 2)
00086                             axis = 2;
00087                         else if (poptarg[0] == 'z' || atoi(poptarg) == 3)
00088                             axis = 3;
00089                         break;
00090             case 's':   nx = ny = atoi(poptarg);
00091                         break;
00092             case 'S':   nskip = atoi(poptarg);
00093                         break;
00094             default:
00095             case '?':   params_to_usage(cerr, argv[0], param_string);
00096                         return false;
00097         }
00098     }
00099 
00100     float* a = new float[nx*ny];
00101     if (!a) exit(1);
00102 
00103     int iax, jax;
00104 
00105     if (axis == 1) {
00106         iax = 1;
00107         jax = 2;
00108     } else if (axis == 2) {
00109         iax = 2;
00110         jax = 0;
00111     } else {
00112         iax = 0;
00113         jax = 1;
00114     }
00115 
00116     real p2 = 0.5*psize;
00117 
00118     int cmin = 1000000, cmax = -1000000;
00119     real color_scale;
00120 
00121     real mmin = VERY_LARGE_NUMBER, mmax = -VERY_LARGE_NUMBER;
00122     real logfac = 1;
00123 
00124     // Loop over input snapshots.
00125 
00126     hdyn* b;
00127     while (b = get_hdyn(cin)) {
00128 
00129         if (count == 0) {
00130 
00131             // Determine overall color scaling (and mass range, if
00132             // relevant) from first snap.
00133 
00134             for_all_daughters(hdyn, b, bb) {
00135                 if (bb->get_index() >= 0) {
00136                     cmin = min(cmin, bb->get_index()+IOFF);
00137                     cmax = max(cmax, bb->get_index()+IOFF);
00138                 }
00139                 if (mass && bb->get_mass() > 0) {
00140                     mmin = min(mmin, bb->get_mass());
00141                     mmax = max(mmax, bb->get_mass());
00142                 }
00143             }
00144 
00145             if (cmax >= CMAX) cmax = CMAX - 1;
00146             color_scale = 1.0 / cmax;
00147 
00148             if (mass) logfac = 1.0/log10(mmax/mmin);
00149         }
00150 
00151         if (nskip <= 0 || count % (nskip+1) == 0) {
00152 
00153             // Make this snapshot into an image.
00154 
00155             for (int i = 0; i < nx*ny; i++) a[i] = 0;
00156 
00157             for_all_daughters(hdyn, b, bb) {
00158 
00159                 // Should probably sort stars by coordinate along the
00160                 // projection axis, as in xstarplot.  NOT done yet.
00161 
00162                 vector x = bb->get_pos();
00163 
00164                 if (x[iax] > -l && x[iax] < l
00165                     && x[jax] > -l && x[jax] < l) {
00166 
00167                     // Coordinates:
00168 
00169                     int i = (int) ((l+x[iax]) * 0.5 * nx / l);
00170                     int j = (int) ((l+x[jax]) * 0.5 * ny / l);
00171 
00172                     // Color (by index):
00173 
00174                     float color = 0.9999;               // default is white
00175 
00176                     if (bb->get_index() > 0)
00177                         color = (bb->get_index() % (cmax - cmin) + IOFF)
00178                                         * color_scale;
00179 
00180                     // Radius of point representing star:
00181 
00182                     real r = p2;
00183 
00184                     if (mass && bb->get_mass() > 0) {
00185 
00186                         // Star size depends on its mass.  Minimum size is
00187                         // 1 pixel, maximum is p2.  Scaling is logarithmic.
00188 
00189                         r *= log10(bb->get_mass()/mmin) * logfac;
00190                     }
00191 
00192                     add_point(a, nx, ny, i, j, r, color);
00193                 }
00194             }
00195 
00196             // Image file name counts output images, not input snaps.
00197 
00198             if (streq(file, "-"))
00199                 fn = NULL;
00200             else {
00201                 sprintf(filename, "%s.%3.3d.sun", file, count1++);
00202                 fn = filename;
00203             }
00204 
00205             write_image(a, nx, ny, fn, 0);      // 0 ==> don't scale
00206 
00207             if (fn) {
00208 
00209                 // Runtime compression:
00210 
00211                 sprintf(command, "gzip -f -q %s &", filename);
00212                 system(command);
00213             }
00214         }
00215 
00216         rmtree(b);
00217 
00218         count++;
00219         if (count%10 == 0) cerr << count1 << "/" << count << " ";
00220 
00221         if (n > 0 && count1 > n) break;
00222     }
00223     cerr << endl;
00224 }

Generated at Sun Feb 24 09:57:16 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001