Attachment 'adish03.c'

Download

   1 // uniformly illuminated parabolic dish, beam
   2 //
   3 // compile with:       cc -o adish03 adish03.c -lgd -lpng -lm
   4 #  define NAME      "adish03"
   5 //
   6 // Uses the libgd library.  For documentation see the file:
   7 //      /usr/share/doc/gd-*/index.html  
   8 //   or the website:  http://www.libgd.org/Reference
   9 //  
  10 // You will need truetype fonts, "apt search dejavu" returns
  11 // fonts-dejavu-core/focal,focal,now 2.37-1 all [installed,automatic]
  12 //
  13 // Uses /usr/local/bin/apngasm to make APNG animation from frames
  14 // /home/keithl/src/apngasm/apngasm-2.91-src.zip
  15 //
  16 // now uses j1 function and math.h
  17 //
  18 // This constructs 30 images then dups them forward and reverse.
  19 
  20 #include "gd.h"
  21 #include "math.h"
  22 #include <stdio.h>
  23 #include <stdlib.h>
  24 #include <unistd.h>
  25 
  26 #define  RMMKDIR     "rm -rf adish03dir ; mkdir adish03dir"
  27 #define  PNG2APNG    "/usr/local/bin/apngasm adish03.png adish03dir/* 1 10"
  28 #define  PNGFMT      "adish03dir/a%04d.png"
  29 
  30 #define  YCENT       130
  31 #define  INTPTS      100          // integration points 
  32 #define  WAVELENGTH  86.0         // radio wavelength in pixel units
  33 #define  SCALE       200.0        // disk scaling in pixel units
  34 #define  NPICS       40           // number of frames
  35 
  36 #define  XSIZE       1000         // display window in pixels
  37 #define  YSIZE       750          // display window in pixels
  38 #define  FNT         "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
  39 #define  FSZ         10
  40 
  41 double   amult[XSIZE][YSIZE]     ; //
  42 double   aphase[XSIZE][YSIZE]    ; //
  43 double   behind[XSIZE][YSIZE]    ; //
  44 
  45 // ==========================================================================
  46 
  47 int main() {
  48 
  49    gdImagePtr im                    ; // pixel image map used by gd
  50    gdPoint  beam[4]                 ; // trapezoidal output beam
  51    double   lambda =     WAVELENGTH ; //
  52    double   scale  = (double) SCALE ; // dish scale
  53    double   yacent = (double) YCENT ; // center of upper drawing
  54    double   xsize  = (double) XSIZE ; // width of pixels
  55    double   ysize  = (double) YSIZE ; // height of pixels
  56    int      ybot   = 2*YCENT        ; // top of output pixel array
  57    double   dybot  = (double) ybot  ; // real ""
  58    double   pi2    = 8.0*atan(1.0)  ; // two times PI
  59    double   d2r    = pi2/360.0      ; // degrees to radians conversion factor
  60    int      oxc    = XSIZE/2        ; // center of output pixel array
  61    int      oyc    = YSIZE/2        ; // center of output pixel array
  62    double   xacent = (double) oxc   ; // real ""
  63    int      brect[8]                ; // this returns an (ignored) bounding box
  64    FILE     *pngout                 ; // file handle for PNG output frame
  65    char     dirname[200]            ; // frame output directory
  66    char     framename[200]          ; // directory/name of frame
  67    char     labstring[200]          ; // used for labellin
  68    char     rmmkdir[200]            ; // remove and make frame directory
  69    char     link0[200]              ; // directory/name of source 
  70    char     link1[200]              ; // directory/name of target hardlink
  71    int      black, sun1, white, red ; // color map numbers
  72    int      green, blue, gray       ; // color map numbers
  73    int      dgray, trans            ; // color map numbers
  74    int      bcolor[256]             ; // color map numbers
  75    int      gcolor[256]             ; // color map numbers
  76    int      ox, oy                  ; // output pixel position
  77    int      oxm                     ; //
  78    int      i                       ; // general counter
  79    int      fs                      ; // big label font size
  80    int      cir                     ; //
  81    double   dcir                    ; //
  82    int      icolor                  ; //
  83    int      jcolor                  ; //
  84    int      mag                     ; // pixel magnitude, 0-255
  85    int      frame                   ; // animation frame count
  86    int      pct=0                   ; // percentage of complete frame
  87    int      pct0=0                  ; // previous percentage
  88    int      ya= YCENT               ; // antenna height
  89    double   bx                      ; // bessel argument
  90    double   j                       ; // 
  91    double   j0                      ; // 
  92    double   ffrac                   ; // frame scaling of variable, 0 to 1
  93    double   phase                   ; // phase
  94    double   k=pi2/lambda            ; // wavenumber
  95    double   oxd                     ; // 
  96    double   oxd2                    ; // 
  97    double   oyd                     ; //
  98    double   oyd2                    ; //
  99    double   oym                     ; //
 100    double   dis                     ; // distance from focus spot
 101    double   dis2                    ; // 
 102    int      rate = 20               ; // swf rate
 103    double   xm                      ; //
 104    double   max                     ; // normalize beam amplitude
 105    double   norm                    ; // normalize beam amplitude
 106    double   delta                   ; //
 107    double   sumsin                  ; //
 108    double   sumcos                  ; //
 109    double   sum                     ; //
 110    double   adis                    ; //
 111    double   amp[INTPTS]             ; //
 112    double   dn, da, di              ; //
 113 
 114 //  set up target directory
 115 
 116    system( RMMKDIR );
 117 
 118 //  make array amplitude values ------------------------------------------
 119    
 120    for( i=0 ; i<INTPTS ; i++ ) {
 121       dn = ( (double)INTPTS ) ;
 122       di = (double) i      ;
 123       da = (2.0*di+1.0-dn)/dn;
 124       amp[i]=sqrt(1.0-da*da );
 125       // DEBUG printf( "%3d%12.6f\n", i, amp[i] );
 126    }
 127 
 128 //  make phase and magnitude array ---------------------------------------
 129 
 130    max   = 0.0 ;
 131    cir   = (int) (0.39*scale)      ;
 132    dcir  = (double) cir            ;
 133    xm    = 2.0*dcir                ;
 134    delta = 2.0*xm/((double)INTPTS) ;
 135    xm   -= 0.5*delta-xacent        ;
 136    dcir *= 0.99*dcir               ;
 137 
 138    for( oy=0 ; oy<YSIZE ; oy++ ) {
 139       oyd   = (double)(oy-ya) ;
 140       oyd2  = oyd*oyd ;
 141       oym   = 0.5/(0.5+exp(-0.1*oyd ) ) ; // kludge!!
 142 
 143       printf( "setup %04d/%04d\r", oy, YSIZE ) ;
 144       fflush(stdout);
 145 
 146       // do half the array, then mirror
 147       for( ox=0 ; ox < oxc ; ox++ ) {
 148          oxd = xm - (double)ox ;
 149          sumsin = 0.0 ;
 150          sumcos = 0.0 ;
 151          for( i=0 ; i<INTPTS ; i++ ) {
 152             oxd2    = oxd*oxd ;
 153             dis2    = oxd2 + oyd2 ;
 154             dis     = sqrt( dis2 );
 155             sumsin += amp[i]*sin( k*dis ) / dis;
 156             sumcos += amp[i]*cos( k*dis ) / dis;
 157             oxd    -= delta ;
 158          }
 159 
 160          // is it behind the antenna?
 161          oxd  = (double) ( ox-oxc );
 162          oyd  = (double) ( oy-ya  );
 163          adis = 0.25*oxd*oxd + 4.0*oyd*oyd ;
 164          if( adis > dcir ) {
 165             sum            = sqrt(  sumsin*sumsin + sumcos*sumcos ) ;
 166 // normalization kludge !!!
 167             sum           *= oym*sqrt(oxd*oxd + oyd*oyd);
 168 
 169             amult[ox][oy]  = sum ;
 170             aphase[ox][oy] = atan2( sumsin, sumcos );
 171             if( max < sum ) { max = sum ; }
 172           }
 173           else {
 174              amult[ox][oy]  = 0.0 ;
 175              aphase[ox][oy] = 0.0 ;
 176           }
 177    }  }
 178 
 179    // normalize and mirror
 180 
 181    norm = 1.0 / max ;
 182 
 183    for( oy=0 ; oy<YSIZE ; oy++ ) {
 184       for( ox=0 ; ox<oxc ; ox++ ) {
 185          amult[ox][oy] *= norm ;
 186          oxm = XSIZE-(ox+1);
 187          amult[ oxm][oy] = amult[ ox][oy] ;
 188          aphase[oxm][oy] = aphase[ox][oy] ;
 189    }  }
 190          
 191    printf("                  \r");
 192 
 193 //  outer loop, sinusodial random factor change --------------------------
 194 //  from 0 to 1 to 0
 195   
 196    for( frame=0 ; frame<NPICS ; frame++ ) {
 197       phase = pi2*((double)frame)/((double)(NPICS-1));
 198 
 199 // set up array and define colors -----------------------------------------
 200 
 201       im = gdImageCreateTrueColor(XSIZE, YSIZE );
 202    
 203       // Allocate standard colors
 204       black = gdImageColorAllocate(im,   0,   0,   0);
 205       white = gdImageColorAllocate(im, 255, 255, 255);
 206       sun1  = gdImageColorAllocate(im,  51,  51, 102);
 207       red   = gdImageColorAllocate(im, 255,   0,   0);
 208       green = gdImageColorAllocate(im,   0, 255,   0);
 209       blue  = gdImageColorAllocate(im,   0,   0, 255);
 210       gray  = gdImageColorAllocate(im, 160, 160, 160);
 211       dgray = gdImageColorAllocate(im,  48,  48,  48);
 212       trans = gdImageColorAllocate(im, 1, 1, 1);
 213    
 214       // allocate gray scale array 
 215       for( icolor=0 ; icolor<256 ; icolor++ ) {
 216         gcolor[icolor] = gdImageColorAllocate(im, icolor,   icolor,   icolor);
 217         bcolor[icolor] = gdImageColorAllocate(im, icolor/3, icolor, icolor/3);
 218       }
 219 
 220       printf( "%04d/%04d\r", frame, NPICS ) ;
 221       fflush(stdout);
 222 
 223 // compute pixels ---------------------------------------------------------
 224    
 225       for( ox=0 ; ox<XSIZE ; ox++ ) {
 226          for( oy=0 ; oy<YSIZE ; oy++ ) {
 227             j  = amult[ox][oy]*sin( phase-aphase[ox][oy] ) ;
 228             if( j >  1.0 ) { j= 1.0; }
 229             if( j < -1.0 ) { j=-1.0; }
 230             mag = (int) ( 127.5*(1.0 + j ));
 231             gdImageSetPixel( im, ox,             oy, gcolor[mag] ) ;
 232       }  } 
 233   
 234  // labels -----------------------------------------------------------------
 235    
 236       fs= ybot/6 ;
 237    
 238       gdImageStringTTF( im, brect,             // imagespace, bounding box
 239                        white , FNT, fs, 0.0,   // color, font, fontsize, angle
 240                        15     , fs+15  ,       //  x, y
 241                        "Dish Antenna" );       //  text
 242    
 243 // draw dish --------------------------------------------------------------
 244    
 245       jcolor    = bcolor[255] ;
 246    
 247       // disk
 248       gdImageFilledEllipse( im, oxc, ya,     4*cir,  cir,   jcolor );
 249    
 250       //  square feed 
 251       beam[0].x = oxc+6     ;
 252       beam[0].y = ya+cir-1  ;
 253       beam[1].x = oxc-6     ;
 254       beam[1].y = ya+cir-1  ;
 255       beam[2].x = oxc-6     ;
 256       beam[2].y = ya+cir+15 ;
 257       beam[3].x = oxc+6     ;
 258       beam[3].y = ya+cir+15 ;
 259    
 260       gdImageFilledPolygon( im, beam, 4,             jcolor );
 261    
 262       // feed struts
 263       gdImageLine( im, oxc-2*cir, ya, oxc,  ya+cir,  jcolor );
 264       gdImageLine( im, oxc+2*cir, ya, oxc,  ya+cir,  jcolor );
 265       gdImageLine( im, oxc,       ya, oxc,  ya+cir,  jcolor );
 266 
 267    
 268       // labels on array -------------------------------------------------------
 269       fs= ybot/8 ;
 270    
 271       gdImageStringTTF( im, brect,             // imagespace, bounding box
 272                        white , FNT, fs, 0.0,   // color, font, fontsize, angle
 273                        15, YSIZE-15,           //  x, y
 274 		       "Ground Pattern" );     //  text
 275    
 276       // output the frame ------------------------------------------------------
 277    
 278       sprintf( framename, PNGFMT , frame );
 279       pngout = fopen( framename, "wb");
 280       gdImagePngEx( im, pngout, 1 );
 281 
 282       gdImageDestroy(im);
 283       fclose(pngout);
 284    
 285    }  // end of single frame loop
 286 
 287    printf( "frames complete, now run\n");
 288    printf( "%s\n", PNG2APNG );
 289 }
 290    
 291    

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] (2021-06-08 23:29:30, 10.6 KB) [[attachment:adish03.c]]
  • [get | view] (2021-06-08 22:56:00, 3166.4 KB) [[attachment:dish_beam.png]]
  • [get | view] (2021-06-08 23:29:59, 9.2 KB) [[attachment:pa03.c]]
  • [get | view] (2021-06-08 23:29:49, 9.2 KB) [[attachment:pa04.c]]
  • [get | view] (2021-06-08 23:24:45, 2567.7 KB) [[attachment:phased00.png]]
  • [get | view] (2021-06-08 23:24:33, 3261.8 KB) [[attachment:phased30.png]]
 All files | Selected Files: delete move to page

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