Attachment 'adish01.c'

Download

   1 // uniformly illuminated parabolic dish
   2 //
   3 // compile with    cc -o adish01 adish01.c -lgd -lpng -lm -lgsl -lgslcblas
   4 //
   5 // Uses the libgd library.  For documentation see the file:
   6 //      /usr/share/doc/gd-*/index.html  
   7 //   or the website:  http://www.libgd.org/Reference
   8 //  
   9 // You will need truetype fonts.  If you don't have them, you can
  10 // copy ../fonts/truetype/.. from openoffice.org to /usr/share/
  11 //
  12 // Uses swftools to build a swf movie from individual png images
  13 //        for more information, see http://www.swftools.org/
  14 //
  15 // Uses the GNU Scientific Library to make the J1() bessel function
  16 // for more information, see:
  17 //  http://www.gnu.org/software/gsl/manual/html_node/Regular-Cylindrical-Bessel-Functions.html
  18 //
  19 // This constructs half the slides, then mirrors them
  20 
  21 #include "gd.h"
  22 #include "math.h"
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <unistd.h>
  26 #include <gsl/gsl_sf_bessel.h>
  27 
  28 #define  RMMKDIR     "rm -rf adish01dir ; mkdir adish01dir"
  29 #define  PNG2SWF     "png2swf -o adish01.swf -r 10 adish01dir/*.png" 
  30 #define  PNGFMT      "adish01dir/a%04d.png"
  31 
  32 #define  YCENT       130
  33 #define  WAVELENGTH  50.0         // radio wavelength in pixel units
  34 #define  SCALE       10.0         // disk scaling in pixel units
  35 #define  SCMIN       30.0         // minimum dish scale 
  36 #define  SCMAX       60.0         // minimum dish scale 
  37 #define  NPICS       30           // ( half the ) number of frames
  38 #define  RSPACE      30.0         // random variation
  39 
  40 #define  DANGW       150          // display angle width in degrees
  41 #define  DANGS       10           // display angle tic step in degrees
  42 
  43 #define  XLEFT       40           // left side pixel count
  44 #define  XSIZE       1000         // display window in pixels
  45 #define  YSIZE       750          // display window in pixels
  46 #define  FNT         "DejaVuMonoSans"
  47 #define  FSZ         10
  48 
  49 // ==========================================================================
  50 
  51 int main() {
  52 
  53    gdImagePtr im                    ; // pixel image map used by gd
  54    gdPoint  beam[4]                 ; // trapezoidal output beam
  55    double   lambda=(double)WAVELENGTH;//
  56    double   scale  = (double) SCALE ; // dish scale
  57    double   xleft  = (double) XLEFT ; // left of pixel array
  58    double   yacent = (double) YCENT ; // center of upper drawing
  59    double   xsize  = (double) XSIZE ; // width of pixels
  60    double   ysize  = (double) YSIZE ; // height of pixels
  61    double   dangw  = (double) DANGW ; // degree width of output pixel array
  62    double   dangs  = (double) DANGS ; // display angle tic step in degrees
  63    double   dangw2 = 0.5*dangw      ; // half width of output pixel array
  64    double   xwide  = xsize-xleft    ; // pixel width of output pixel array
  65    int      ybot   = 2*YCENT        ; // top of output pixel array
  66    double   dybot  = (double) ybot  ; // real ""
  67    double   ytall  = ysize-dybot    ; // height of output pixel array
  68    double   pi2    = 8.0*atan(1.0)  ; // two times PI
  69    double   d2r    = pi2/360.0      ; // degrees to radians conversion factor
  70    int      oxc    =(XSIZE+XLEFT)/2 ; // center of output pixel array
  71    int      oyc    =(YSIZE+ybot)/2  ; // center of output pixel array
  72    double   xacent = (double) oxc   ; // real ""
  73    double   aspect =ytall/xwide     ; // aspect ration of output pixel array
  74    double   angH   =dangw*aspect    ; // angular height of output pixel array
  75    double   angH2 = 0.5*angH        ; // half height of output pixel array
  76 
  77    double   angx                    ; // pixel X angle radians
  78    double   angx0=-d2r*dangw2       ; // left pixel value, radians
  79    double   angxs=d2r*dangw/xwide   ; // pixel X slope radians per pixel
  80    double   angxf=angx0-angxs*xleft ; // pixel X offset radians
  81 
  82    double   angy                    ; // pixel Y angle
  83    double   angy0=-d2r*angH2        ; // top pixel value, radians
  84    double   angys=d2r*angH/ytall    ; // pixel Y slope radians
  85    double   angyf=angy0-angys*dybot ; // pixel Y offset radians
  86 
  87    double   ang                     ; // angle from center
  88 
  89    FILE     *pngout                 ; // file handle for PNG output frame
  90    char     dirname[80]             ; // frame output directory
  91    char     framename[80]           ; // directory/name of frame
  92    char     labstring[80]           ; // used for labellin
  93    char     link0[80]               ; // directory/name of source 
  94    char     link1[80]               ; // directory/name of target hardlink
  95    int      black, sun1, white, red ; // color map numbers
  96    int      green, blue, gray       ; // color map numbers
  97    int      dgray, trans            ; // color map numbers
  98    int      gcolor[256]             ; // color map numbers
  99    int      ox, oy                  ; // output pixel position
 100    int      i                       ; // general counter
 101    double   zscale                  ; // scaling for perspective depth
 102    int      fs                      ; // big label font size
 103    int      icolor                  ; //
 104    int      cir                     ; //
 105    int      beamw                   ; //
 106    int      mag                     ; // pixel magnitude, 0-255
 107    int      m                       ; // tickmark max
 108    int      s                       ; // tickmark scale
 109    int      frame                   ; // animation frame count
 110    int      pct=0                   ; // percentage of complete frame
 111    int      pct0=0                  ; // previous percentage
 112    double   min = 1e-6              ; // min value for bessel function
 113    double   bx = min                ; // bessel argument
 114    double   j=gsl_sf_bessel_J1(bx)/bx;// bessel/x near zero value
 115    double   norm = 255.1/(j*j)      ; // normalizes amplitude
 116    double   j0=j                    ; //
 117    double   ffrac                   ; // frame scaling of variable, 0 to 1
 118 
 119 #ifdef DEBUG
 120    printf( "%12.6f\n", j0 );
 121    printf( "%12.6f%12.6f%15.9f\n",  angx0, angxf, angxs );
 122    printf( "%12.6f%12.6f%15.9f\n",  angy0, angyf, angys );
 123 #endif
 124 
 125 //  set up target directory
 126 
 127    system( RMMKDIR );
 128 
 129 //  outer loop, sinusodial random factor change --------------------------
 130 //  from 0 to 1 to 0
 131   
 132    for( frame=0 ; frame<NPICS ; frame++ ) {
 133 
 134       ffrac = 0.5*(1.0-cos(0.5*pi2*((double)frame)/((double)(NPICS-1))));
 135       scale = SCMIN+(SCMAX-SCMIN)*ffrac ;
 136 
 137 #ifndef SINGLE
 138       printf( "%04d/%04d\r", frame, NPICS ) ;
 139       fflush(stdout);
 140 #endif
 141 
 142 // set up array and define colors -----------------------------------------
 143 
 144    im = gdImageCreateTrueColor(XSIZE, YSIZE );
 145 
 146    // Allocate standard colors
 147    black = gdImageColorAllocate(im,   0,   0,   0);
 148    white = gdImageColorAllocate(im, 255, 255, 255);
 149    sun1  = gdImageColorAllocate(im,  51,  51, 102);
 150    red   = gdImageColorAllocate(im, 255,   0,   0);
 151    green = gdImageColorAllocate(im,   0, 255,   0);
 152    blue  = gdImageColorAllocate(im,   0,   0, 255);
 153    gray  = gdImageColorAllocate(im, 160, 160, 160);
 154    dgray = gdImageColorAllocate(im,  48,  48,  48);
 155    trans = gdImageColorAllocate(im, 1, 1, 1);
 156 
 157    // allocate gray scale array 
 158    for( icolor=0 ; icolor<256 ; icolor++ ) {
 159      gcolor[icolor] = gdImageColorAllocate(im, icolor, icolor, icolor);
 160    }
 161 
 162 #ifdef SINGLE
 163    printf( "color setup done\n");
 164 #endif
 165 
 166 //   labels -----------------------------------------------------------------
 167 
 168    fs= ybot/7 ;
 169 
 170    gdImageStringFT( im, NULL,               // imagespace, bounding box
 171                     white , FNT, fs, 0.0,   // color, font, fontsize, angle
 172                     6       , fs+6  ,       //  x, y
 173                     "Dish Antenna" );       //  text
 174 
 175    fs= 10*ybot/95 ;
 176    icolor = gcolor[ (int)( 255.1*(1.0-ffrac )) ] ;
 177 
 178    gdImageStringFT( im, NULL,               // imagespace, bounding box
 179                     icolor, FNT, fs, 0.0,   // color, font, fontsize, angle
 180                     xsize-10*fs, fs+6 ,     // x, y
 181                     "Smaller dish" );       //  text
 182 
 183    gdImageStringFT( im, NULL,               // imagespace, bounding box
 184                     icolor, FNT, fs, 0.0,   // color, font, fontsize, angle
 185                     xsize-15*fs, 3*fs,      // x, y
 186                     "Larger ground spot" ); //  text
 187 
 188    icolor = gcolor[ (int)( 255.1*ffrac ) ] ;
 189 
 190    gdImageStringFT( im, NULL,               // imagespace, bounding box
 191                     icolor, FNT, fs, 0.0,   // color, font, fontsize, angle
 192                     xsize-10*fs, 6*fs+6,    // x, y
 193                     "Bigger dish" );        //  text
 194 
 195    gdImageStringFT( im, NULL,               // imagespace, bounding box
 196                     icolor, FNT, fs, 0.0,   // color, font, fontsize, angle
 197                     xsize-15.5*fs, 8*fs,    // x, y
 198                     "Smaller ground spot"); //  text
 199 
 200 //   draw dish --------------------------------------------------------------
 201 
 202    cir = (int) (0.8*scale)  ;
 203 
 204    // beam 
 205    beamw     = 2*cir+(int)(0.3*yacent*lambda/scale) ;
 206    oy        = (int) (1.3*yacent) ;
 207    beam[2].x = oxc+beamw ;
 208    beam[2].y = oy        ;
 209    beam[3].x = oxc-beamw ;
 210    beam[3].y = oy        ;
 211 
 212    beamw     = 2*cir     ;
 213    oy        = (int) (0.5*yacent) ;
 214    beam[0].x = oxc-beamw ;
 215    beam[0].y = oy        ;
 216    beam[1].x = oxc+beamw ;
 217    beam[1].y = oy        ;
 218 
 219    gdImageFilledPolygon( im, beam, 4, dgray );
 220 
 221    // disk
 222    gdImageFilledEllipse( im, oxc, oy, 4*cir, cir, gray );
 223 
 224    // feed 
 225    gdImageFilledEllipse( im, oxc, 2*oy, 24,  6, gray );
 226    gdImageLine( im, oxc-2*cir, oy, oxc-12, 2*oy, gray );
 227 
 228 
 229 //   now compute pixels -----------------------------------------------------
 230 
 231    for( ox=XLEFT ; ox<XSIZE ; ox++ ) {
 232       angx =  angxf + angxs*((double)ox );
 233 
 234 #ifndef SINGLE
 235       if( pct != pct0 ) {
 236          printf( "%04d/%04d%4d\r", frame, NPICS, pct ) ;
 237          fflush(stdout);
 238       }
 239 #endif
 240 
 241       for( oy=ybot ; oy<YSIZE ; oy++ ) {
 242          angy = angyf + angys*((double)oy );
 243          ang  = sqrt( angx*angx + angy*angy) ;
 244 
 245          bx = pi2*scale*sin(ang)/lambda  ; // bessel argument
 246 
 247          if( fabs(bx) < min ) { j=j0 ; }
 248          else {
 249             j = gsl_sf_bessel_J1(bx)/bx  ; // bessel/x near zero value
 250          }
 251          mag = (int) (norm*j*j)          ; 
 252 
 253 #ifdef DEBUG
 254          if( (ox==oxc) && (oy==oyc) ) {
 255             printf( "%4d%4d%10.6f%10.6f%10.6f%10.3e%10.3e%4d\n",
 256                       ox, oy, angx, angy, ang, bx,  j,    mag );
 257          }
 258 #endif
 259          if( abs(mag-181) < 3 ) {
 260             gdImageSetPixel( im, ox, oy, red         ) ;
 261          }
 262          else {
 263             gdImageSetPixel( im, ox, oy, gcolor[mag] ) ;
 264          }
 265    }  }
 266 
 267 #ifdef SINGLE
 268    printf( "intensity points drawn\n");
 269 #endif
 270 
 271    // draw tickmarks on edges ----------------------------------------------
 272 
 273    // draw x tickmarks 
 274    m = (int) ( 0.001+ dangw2/dangs ); 
 275    s = (int) ( xwide*dangs/dangw   );
 276    for( i = -m ; i <= m ; i++ ) {
 277       ox = oxc + s*i ;
 278       gdImageLine( im, ox, ybot, ox, ybot-5, white );
 279 
 280       sprintf( labstring, "%3d", i*DANGS );
 281       gdImageStringFT( im, NULL,                   // imagespace, bounding
 282                        white, FNT, FSZ,            // color, font, fontsize
 283                        0.0, ox+FSZ/2, ybot-FSZ/2,  // angle, x, y
 284                        labstring );                // the text
 285 
 286    }
 287    ox = (int) ( xacent ) ;
 288    gdImageLine( im, xacent, ybot, ox, ybot-10, white );
 289 
 290    // draw y tickmarks
 291    m = (int) ( 0.001 + angH2/dangs ) ; 
 292 
 293    for( i = -m ; i <= m ; i++ ) {
 294       oy = oyc + s*i ;
 295       gdImageLine( im, xleft, oy,  xleft-5, oy, white );
 296 
 297       sprintf( labstring, "%3d", i*DANGS );
 298       gdImageStringFT( im, NULL,                   // imagespace, bounding
 299                        white, FNT, FSZ,            // color, font, fontsize
 300                        0.0, xleft-4*FSZ, oy+FSZ/2, // angle, x, y
 301                        labstring );                // the text
 302 
 303    }
 304    ox = (int) ( xacent ) ;
 305    gdImageLine( im, xleft, oyc, xleft-10, oyc, white );
 306 
 307    // output the frame ------------------------------------------------------
 308 
 309    sprintf( framename, PNGFMT , frame );
 310    pngout = fopen( framename, "wb");
 311    gdImagePngEx( im, pngout, 1 );
 312    gdImageDestroy(im);
 313    fclose(pngout);
 314 
 315 }  // end of single frame
 316 
 317    // link to the other half of the sequence
 318    for( i=0 ; i < NPICS ; i++ ) {
 319       sprintf( link0 , PNGFMT, i );
 320       sprintf( link1 , PNGFMT, 2*NPICS-i );
 321       link( link0, link1 ) ;
 322    }
 323 
 324    system( PNG2SWF );
 325 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-06-09 20:26:13, 12.2 KB) [[attachment:a3d02.c]]
  • [get | view] (2009-06-09 20:27:52, 12453.1 KB) [[attachment:a3d02.swf]]
  • [get | view] (2009-06-09 20:26:26, 13.5 KB) [[attachment:a3d03.c]]
  • [get | view] (2009-06-09 20:28:29, 3594.1 KB) [[attachment:a3d03.swf]]
  • [get | view] (2009-06-09 20:26:36, 12.1 KB) [[attachment:a3d04.c]]
  • [get | view] (2009-06-09 20:29:46, 14017.0 KB) [[attachment:a3d04.swf]]
  • [get | view] (2009-06-09 20:25:59, 15.1 KB) [[attachment:a3d05.c]]
  • [get | view] (2009-06-09 20:31:27, 20001.4 KB) [[attachment:a3d05.swf]]
  • [get | view] (2009-06-09 20:25:47, 12.1 KB) [[attachment:adish01.c]]
  • [get | view] (2009-06-09 20:25:34, 2843.1 KB) [[attachment:adish01.swf]]
  • [get | view] (2009-03-23 17:51:06, 228.2 KB) [[attachment:inter_array.png]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.