Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

starplot.C

Go to the documentation of this file.
00001 
00008 
00009 //.............................................................................
00010 //    version 1:  May 1989   Piet Hut               email: piet@iassns.bitnet
00011 //                           Institute for Advanced Study, Princeton, NJ, USA
00012 //    version 2:  Dec 1992   Piet Hut  --  adopted to the new C++-based starlab
00013 //.............................................................................
00014 //  non-local functions: 
00015 //    starplot
00016 //.............................................................................
00017 //  Plot positions of all bodies in an N-body system, in a primitive way:
00018 //  the screen is divided in character positions, and the representation is:
00019 //  every  x  stands for one body; every  *  stands for more than one body.
00020 //  This is very primitive, but independent of terminal or plotting software
00021 //.............................................................................
00022 
00023 #include "dyn.h"
00024 
00025 #ifdef TOOLBOX
00026 
00027 #define  HBINS  56              // number of horizontal bins
00028 #define  VBINS  28              // number of vertical bins
00029 
00030 //-----------------------------------------------------------------------------
00031 //  starplot.c  --  projects the positions of all particles on the screen
00032 //                  accepts:  pn: a pointer to a nbody system,
00033 //                             k: the number of the coordinate axis along which
00034 //                                the projection is directed, with {k,x,y}
00035 //                                having a right-handend orientation,
00036 //                          lmax: maximum lengths alond the remaining axes.
00037 //-----------------------------------------------------------------------------
00038 
00039 void  starplot(dyn * b, int k, real lmax, int nlines)
00040     {
00041     int  n;
00042     int  kx, ky;         // the coordinates projected onto the x- and y-axes
00043     register int  i, j;
00044     real lvmax;
00045     char  screen[VBINS][HBINS];                      // pixels on the screen
00046     dyn *bi;
00047     char  *str;
00048     char  plot_symbol = 'o';
00049 
00050     switch (k)
00051         {
00052         case 1: 
00053             kx = 2; ky = 1; break;
00054         case 2:
00055             kx = 0; ky = 2; break;
00056         case 3:
00057             kx = 1; ky = 0; break;
00058         default: 
00059             cerr << "starplot: k = " << k
00060                  << " illegal value; choose from {1, 2, 3}\n";
00061             exit(0);
00062         }
00063 
00064     //  quick fix to determine  n  for a flat tree and get true maximum:
00065 
00066     real temp, true_max = 0;
00067     for (n = 0, bi = b->get_oldest_daughter(); bi != NULL;
00068          bi = bi->get_younger_sister()) {
00069         n++;
00070 
00071         temp = bi->get_pos()[kx];
00072         if (temp < 0) temp = -temp;
00073         if (temp > true_max) true_max = temp;
00074         temp = bi->get_pos()[ky];
00075         if (temp < 0) temp = -temp;
00076         if (temp > true_max) true_max = temp;
00077     }
00078     if (lmax <= 0) {
00079         temp = 1.0/128;
00080         while (temp < true_max) temp *= 2;
00081         lmax = temp;
00082     }   
00083 
00084     if (nlines > VBINS)
00085         nlines = VBINS;
00086 
00087     for (i = 0; i < nlines; i++)
00088         for (j = 0; j < HBINS; j++)
00089             screen[i][j] = ' ';         // no particles within this pixel
00090 
00091     lvmax = lmax * ((real)nlines / (real)VBINS);
00092 
00093     for (bi=b->get_oldest_daughter(); bi != NULL; bi=bi->get_younger_sister())
00094         {
00095         str = &plot_symbol;             // default
00096 
00097         i = (int)(nlines * 
00098                  (lvmax + bi->get_pos()[kx]) / (2.0*lvmax));
00099         j = (int)(HBINS *
00100                  (lmax + bi->get_pos()[ky]) / (2.0*lmax));
00101 
00102         if ( i >=0 && i < nlines && j >=0 && j < HBINS)
00103             {
00104             if (screen[i][j] == ' ')
00105                 screen[i][j] = *str;
00106             else if (screen[i][j] == *str || screen[i][j] == (*str + 'A'-'a'))
00107                 screen[i][j] = *str + 'A'-'a';
00108             else
00109                 screen[i][j] = '*';
00110             }
00111         }
00112 
00113     putchar(' ');                               // top line
00114     putchar(' ');
00115     putchar('+');
00116     for (j = 0; j < HBINS; j++)
00117         putchar('-');
00118     putchar('+');
00119     putchar('\n');
00120 
00121     putchar('^');                               // second line: N
00122     putchar(' ');
00123     putchar('|');
00124     for (j = 0; j < HBINS; j++)
00125         putchar(screen[nlines - 1][j]);
00126     putchar('|');
00127     //    cout << "   N = " << n;
00128     printf("   N = %d", n);
00129 
00130     printf("\n");
00131 
00132     putchar('|');                               // third line
00133     putchar(' ');
00134     putchar('|');
00135     for (j = 0; j < HBINS; j++)
00136         putchar(screen[nlines - 2][j]);
00137     putchar('|');
00138     putchar('\n');
00139 
00140     putchar('|');                               // fourth line: range
00141     putchar(' ');
00142     putchar('|');
00143     for (j = 0; j < HBINS; j++)
00144         putchar(screen[nlines - 3][j]);
00145     putchar('|');
00146 //    cout << "   max = " << lmax;
00147     printf("   max = %.2f", lmax);
00148     printf("\n");
00149 
00150     switch (k)
00151         {
00152         case 1: putchar('z'); break;
00153         case 2: putchar('x'); break;
00154         case 3: putchar('y'); break;
00155         }
00156     putchar(' ');                               // fifth line: y-axis label
00157     putchar('|');
00158     for (j = 0; j < HBINS; j++)
00159         putchar(screen[nlines - 4][j]);
00160     putchar('|');
00161     putchar('\n');
00162 
00163     for (i = nlines - 5; i >= 0; i--)
00164         {
00165         putchar(' ');
00166         putchar(' ');
00167         putchar('|');
00168         for (j = 0; j < HBINS; j++)
00169             putchar(screen[i][j]);
00170         putchar('|');
00171         putchar('\n');
00172         }
00173 
00174     putchar(' ');
00175     putchar(' ');
00176     putchar('+');
00177     for (j = 0; j < HBINS; j++)
00178         putchar('-');
00179     putchar('+');
00180     putchar('\n');
00181 
00182     for (j = 0; j < HBINS - 3; j++)
00183         putchar(' ');
00184     switch (k)
00185         {
00186         case 1: putchar('y'); break;
00187         case 2: putchar('z'); break;
00188         case 3: putchar('x'); break;
00189         }
00190     printf(" --->\n");
00191     }
00192 
00193 #define  BIG_NUMBER  100
00194 
00195 //-----------------------------------------------------------------------------
00196 //  main  --  driver to use  starplot()  as a tool. 
00197 //               The argument -a is interpreted as the axis along which to
00198 //            view the N-body system: the number of the coordinate axis along
00199 //            which the projection is directed, with {k,x,y} having a
00200 //            right-handend orientation.
00201 //               If an argument -l is provided, it defines the maximum
00202 //            lengths alond the remaining axes.
00203 //               If an argument -n is provided, it defines the maximum
00204 //            number of lines on the screen.
00205 //-----------------------------------------------------------------------------
00206 
00207 main(int argc, char ** argv)
00208 {
00209     int  k;
00210     int  nlines;
00211     real lmax;
00212     dyn *b;
00213     bool  a_flag = FALSE;       // if TRUE: a projection axis was prescribed
00214     bool  c_flag = FALSE;       // if TRUE: clear screen before each plot
00215     bool  l_flag = FALSE;       // if TRUE: a maximum length was prescribed
00216     bool  n_flag = FALSE;       // if TRUE: number of lines was prescribed
00217 
00218     check_help();
00219 
00220     extern char *poptarg;
00221     int c;
00222     char* param_string = "a:cl:n:";
00223 
00224     while ((c = pgetopt(argc, argv, param_string)) != -1)
00225         switch(c)
00226             {
00227             case 'a': a_flag = TRUE;
00228                       k = atoi(poptarg);
00229                       break;
00230             case 'c': c_flag = TRUE;
00231                       break;
00232             case 'l': l_flag = TRUE;
00233                       lmax = atof(poptarg);
00234                       break;
00235             case 'n': n_flag = TRUE;
00236                       nlines = atoi(poptarg);
00237                       break;
00238             case '?': params_to_usage(cerr, argv[0], param_string);
00239                       get_help();
00240                       exit(1);
00241             }            
00242 
00243     if (a_flag == FALSE)
00244         k = 3;                             // default
00245 
00246     if (l_flag == FALSE)
00247         lmax = 0;                          // default
00248     else
00249         if (lmax < 0)
00250            cerr << "starplot: a maximum length of " << lmax
00251                 << " < 0 not allowed\n";
00252 
00253     if (c_flag) cout << "\33[H\33[J\33[H";
00254 
00255     if (n_flag == FALSE)
00256         nlines = BIG_NUMBER;               // default
00257 
00258     while (b = get_dyn(cin))
00259         {
00260         if (c_flag) cout << "\33[H";
00261         starplot(b, k, lmax, nlines);
00262 
00263         dyn* bi = b->get_oldest_daughter();
00264         while (bi)
00265             {
00266             dyn * tmp = bi->get_younger_sister();
00267             delete bi;
00268             bi = tmp;
00269             }
00270         delete b;
00271         }
00272 }
00273 
00274 #endif
00275 
00276 // endof: starplot.C
00277 

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