Attachment 'adish01.c'
Download 1 // uniformly illuminated parabolic dish
2 //
3 // OLD compile with: cc -o adish01A adish01A.c -lgd -lpng -lm -lgsl -lgslcblas
4 // NEW compile with: cc -o adish01B adish01B.c -lgd -lpng -lm
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. If you don't have them, you can
11 // copy ../fonts/truetype/.. from openoffice.org to /usr/share/
12 //
13 // Uses apngasm to make APNG animation from frames
14 //
15 //OLD Uses the GNU Scientific Library to make the J1() bessel function
16 //OLD for more information, see:
17 //OLD http://www.gnu.org/software/gsl/manual/html_node/Regular-Cylindrical-Bessel-Functions.html
18 //
19 // now uses j1 function and math.h
20 //
21 // This constructs 30 images then dups them forward and reverse.
22
23 #include "gd.h"
24 #include "math.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 // #include <gsl/gsl_sf_bessel.h>
29
30 #define RMMKDIR "rm -rf adish01dir ; mkdir adish01dir"
31 #define PNG2APNG "/usr/local/bin/apngasm adish01B.png adish01dir/* 1 10"
32 #define PNGFMT "adish01dir/a%04d.png"
33
34 #define YCENT 130
35 #define WAVELENGTH 50.0 // radio wavelength in pixel units
36 #define SCALE 10.0 // disk scaling in pixel units
37 #define SCMIN 30.0 // minimum dish scale
38 #define SCMAX 60.0 // minimum dish scale
39 #define NPICS 30 // ( half the ) number of frames
40 #define RSPACE 30.0 // random variation
41
42 #define DANGW 150 // display angle width in degrees
43 #define DANGS 10 // display angle tic step in degrees
44
45 #define XLEFT 40 // left side pixel count
46 #define XSIZE 1000 // display window in pixels
47 #define YSIZE 750 // display window in pixels
48 #define FNT "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
49 #define FSZ 10
50
51 // #define SINGLE 1
52
53 // ==========================================================================
54
55 // int main( ch) {
56 int main(int argc, char *argv[], char * envp[] ) {
57
58 gdImagePtr im ; // pixel image map used by gd
59 gdPoint beam[4] ; // trapezoidal output beam
60 double lambda=(double)WAVELENGTH;//
61 double scale = (double) SCALE ; // dish scale
62 double xleft = (double) XLEFT ; // left of pixel array
63 double yacent = (double) YCENT ; // center of upper drawing
64 double xsize = (double) XSIZE ; // width of pixels
65 double ysize = (double) YSIZE ; // height of pixels
66 double dangw = (double) DANGW ; // degree width of output pixel array
67 double dangs = (double) DANGS ; // display angle tic step in degrees
68 double dangw2 = 0.5*dangw ; // half width of output pixel array
69 double xwide = xsize-xleft ; // pixel width of output pixel array
70 int ybot = 2*YCENT ; // top of output pixel array
71 double dybot = (double) ybot ; // real ""
72 double ytall = ysize-dybot ; // height of output pixel array
73 double pi2 = 8.0*atan(1.0) ; // two times PI
74 double d2r = pi2/360.0 ; // degrees to radians conversion factor
75 int oxc =(XSIZE+XLEFT)/2 ; // center of output pixel array
76 int oyc =(YSIZE+ybot)/2 ; // center of output pixel array
77 double xacent = (double) oxc ; // real ""
78 double aspect =ytall/xwide ; // aspect ration of output pixel array
79 double angH =dangw*aspect ; // angular height of output pixel array
80 double angH2 = 0.5*angH ; // half height of output pixel array
81
82 //// printf( "ybot%10d xwide%10d\n", ybot, dybot); /// temp
83
84 double angx ; // pixel X angle radians
85 double angx0=-d2r*dangw2 ; // left pixel value, radians
86 double angxs=d2r*dangw/xwide ; // pixel X slope radians per pixel
87 double angxf=angx0-angxs*xleft ; // pixel X offset radians
88
89 double angy ; // pixel Y angle
90 double angy0=-d2r*angH2 ; // top pixel value, radians
91 double angys=d2r*angH/ytall ; // pixel Y slope radians
92 double angyf=angy0-angys*dybot ; // pixel Y offset radians
93
94 double ang ; // angle from center
95
96 int brect[8] ; // this returns a bounding box, not needed, but ...
97
98 FILE *pngout ; // file handle for PNG output frame
99
100 char dirname[80] ; // frame output directory
101 char framename[80] ; // directory/name of frame
102 char labstring[80] ; // used for labellin
103 char link0[80] ; // directory/name of source
104 char link1[80] ; // directory/name of target hardlink
105 int black, sun1, white, red ; // color map numbers
106 int green, blue, gray ; // color map numbers
107 int dgray, trans ; // color map numbers
108 int bcolor[256] ; // color map numbers
109 int gcolor[256] ; // color map numbers
110 int ox, oy ; // output pixel position
111 int i ; // general counter
112 double factr ; // beamwidth factor
113 int fs ; // big label font size
114 int cir ; //
115 int icolor ; //
116 int jcolor ; //
117 int beamw ; //
118 int mag ; // pixel magnitude, 0-255
119 int m ; // tickmark max
120 int s ; // tickmark scale
121 int frame ; // animation frame count
122 int pct=0 ; // percentage of complete frame
123 int pct0=0 ; // previous percentage
124 double min = 1e-6 ; // min value for bessel function
125 double bx = min ; // bessel argument
126 double j=j1(bx)/bx ; // bessel/x near zero value
127 double norm = 255.1/(j*j) ; // normalizes amplitude
128 double j0=j ; //
129 double ffrac ; // frame scaling of variable, 0 to 1
130
131 #ifdef DEBUG
132 printf( "%12.6f\n", j0 );
133 printf( "%12.6f%12.6f%15.9f\n", angx0, angxf, angxs );
134 printf( "%12.6f%12.6f%15.9f\n", angy0, angyf, angys );
135
136 // print environment
137
138 for (i=0 ; envp[i] != NULL; i++) printf("\n%s", envp[i]);
139 getchar();
140 printf( "\n");
141 #endif
142
143 // set up target directory
144
145 system( RMMKDIR );
146
147 // outer loop, sinusodial random factor change --------------------------
148 // from 0 to 1 to 0
149
150 for( frame=0 ; frame<NPICS ; frame++ ) {
151
152 ffrac = 0.5*(1.0-cos(0.5*pi2*((double)frame)/((double)(NPICS-1))));
153 scale = SCMIN+(SCMAX-SCMIN)*ffrac ;
154
155 #ifndef SINGLE
156 printf( "%04d/%04d\r", frame, NPICS ) ;
157 fflush(stdout);
158 #endif
159
160 // set up array and define colors -----------------------------------------
161
162 im = gdImageCreateTrueColor(XSIZE, YSIZE );
163
164 // Allocate standard colors
165 black = gdImageColorAllocate(im, 0, 0, 0);
166 white = gdImageColorAllocate(im, 255, 255, 255);
167 sun1 = gdImageColorAllocate(im, 51, 51, 102);
168 red = gdImageColorAllocate(im, 255, 0, 0);
169 green = gdImageColorAllocate(im, 0, 255, 0);
170 blue = gdImageColorAllocate(im, 0, 0, 255);
171 gray = gdImageColorAllocate(im, 160, 160, 160);
172 dgray = gdImageColorAllocate(im, 48, 48, 48);
173 trans = gdImageColorAllocate(im, 1, 1, 1);
174
175 // allocate gray scale array
176 for( icolor=0 ; icolor<256 ; icolor++ ) {
177 gcolor[icolor] = gdImageColorAllocate(im, icolor, icolor, icolor);
178 bcolor[icolor] = gdImageColorAllocate(im, icolor/3, icolor, icolor/3);
179 }
180
181 #ifdef SINGLE
182 printf( "color setup done\n");
183 #endif
184
185 // labels -----------------------------------------------------------------
186 //ERROR labels not appearing ... why?
187 //
188 fs= ybot/6 ;
189
190 gdImageStringTTF( im, brect, // imagespace, bounding box
191 white , FNT, fs, 0.0, // color, font, fontsize, angle
192 15 , fs+15 , // x, y
193 "Dish Antenna" ); // text
194
195 ox = oxc + 2*cir +4 ;
196 oy = (int) (0.4*yacent) ;
197
198 //// printf( "ox%9d oy%9d fs%9d\n", ox, oy, fs ); /// TEMP
199
200 fs = 10*ybot/95 ;
201 icolor = gcolor[ (int)( 255.1*(1.0-ffrac )) ] ;
202 jcolor = bcolor[ (int)( 255.1*(1.0-ffrac )) ] ;
203
204 gdImageStringTTF( im, brect, // imagespace, bounding box
205 jcolor, FNT, fs, 0.0, // color, font, fontsize, angle
206 xsize-10*fs, 2*fs+6, // x, y
207 "Small dish" ); // text
208
209 gdImageStringTTF( im, brect, // imagespace, bounding box
210 icolor, FNT, fs, 0.0, // color, font, fontsize, angle
211 xsize-12*fs, 4*fs, // x, y
212 "Big beam angle" ); // text
213
214 icolor = gcolor[ (int)( 255.1*ffrac ) ] ;
215 jcolor = bcolor[ (int)( 255.1*ffrac ) ] ;
216
217 gdImageStringTTF( im, brect, // imagespace, bounding box
218 jcolor, FNT, fs, 0.0, // color, font, fontsize, angle
219 xsize-10*fs, 6*fs+6, // x, y
220 "Big dish" ); // text
221
222 //// gdImageLine( im, xsize-10*fs-4, 6*fs-4, ox, oy, jcolor ); // arrow
223
224 gdImageStringTTF( im, brect, // imagespace, bounding box
225 icolor, FNT, fs, 0.0, // color, font, fontsize, angle
226 xsize-(27*fs/2), 8*fs, // x, y
227 "Small beam angle"); // text
228
229 // draw beam --------------------------------------------------------------
230
231 // beam
232 for( i=0; i<256; i++) {
233
234 factr = sqrt( (255.0-(double)i)/255.0);
235
236 beamw = (int) (factr*(0.8*scale+0.5*yacent*lambda/scale)) ;
237 oy = (int) (1.7*yacent) ;
238 beam[2].x = oxc+beamw ;
239 beam[2].y = oy ;
240 beam[3].x = oxc-beamw ;
241 beam[3].y = oy ;
242
243 beamw = (int) ( 0.8 * factr * scale ) ;
244 oy = (int) (0.4*yacent) ;
245 beam[0].x = oxc-beamw ;
246 beam[0].y = oy ;
247 beam[1].x = oxc+beamw ;
248 beam[1].y = oy ;
249 gdImageFilledPolygon( im, beam, 4, gcolor[i] );
250 }
251
252 oy = (int) (0.4*yacent) ;
253 cir = (int) (0.39*scale) ;
254
255 // disk
256 gdImageFilledEllipse( im, oxc, oy, 4*cir, cir, green );
257
258 // square feed
259 beam[0].x = oxc+2 ;
260 beam[0].y = oy+cir-1 ;
261 beam[1].x = oxc-2 ;
262 beam[1].y = oy+cir-1 ;
263 beam[2].x = oxc-2 ;
264 beam[2].y = oy+cir+5 ;
265 beam[3].x = oxc+2 ;
266 beam[3].y = oy+cir+5 ;
267
268 gdImageFilledPolygon( im, beam, 4, green );
269
270 // feed struts
271 gdImageLine( im, oxc-2*cir, oy, oxc, oy+cir, green );
272 gdImageLine( im, oxc+2*cir, oy, oxc, oy+cir, green );
273 gdImageLine( im, oxc, oy, oxc, oy+cir, green );
274
275 // now compute pixels -----------------------------------------------------
276
277 for( ox=XLEFT ; ox<XSIZE ; ox++ ) {
278 angx = angxf + angxs*((double)ox );
279
280 #ifndef SINGLE
281 if( pct != pct0 ) {
282 printf( "%04d/%04d%4d\r", frame, NPICS, pct ) ;
283 fflush(stdout);
284 }
285 #endif
286
287 for( oy=ybot ; oy<YSIZE ; oy++ ) {
288 angy = angyf + angys*((double)oy );
289 ang = sqrt( angx*angx + angy*angy) ;
290
291 bx = pi2*scale*sin(ang)/lambda ; // bessel argument
292
293 if( fabs(bx) < min ) { j=j0 ; }
294 else {
295 // OLD j = gsl_sf_bessel_J1(bx)/bx ; // bessel/x near zero value
296 j = j1(bx)/bx ; // bessel/x near zero value
297 }
298 mag = (int) (norm*j*j) ;
299
300 #ifdef DEBUG
301 if( (ox==oxc) && (oy==oyc) ) {
302 printf( "%4d%4d%10.6f%10.6f%10.6f%10.3e%10.3e%4d\n",
303 ox, oy, angx, angy, ang, bx, j, mag );
304 }
305 #endif
306 gdImageSetPixel( im, ox, oy, gcolor[mag] ) ;
307 } }
308
309 #ifdef SINGLE
310 printf( "intensity points drawn\n");
311 #endif
312
313 // draw tickmarks on edges ----------------------------------------------
314 //
315
316 //// printf( "oxc%9d oyc%9d\n", oxc, oyc ); // TEMP
317
318 // draw x tickmarks
319 m = (int) ( 0.001+ dangw2/dangs );
320 s = (int) ( xwide*dangs/dangw );
321
322 //// printf( "ZSM s%9d m%9d\n" , s, m); // TEMP
323
324
325 for( i = -m ; i <= m ; i++ ) {
326 ox = oxc + s*i ;
327 gdImageLine( im, ox, ybot, ox, ybot-5, white );
328
329 sprintf( labstring, "%3d", i*DANGS );
330 gdImageStringTTF( im, brect, // imagespace, bounding
331 white, FNT, FSZ, // color, font, fontsize
332 0.0, ox+FSZ/2, ybot-FSZ/2, // angle, x, y
333 labstring ); // the text
334 //// printf( "AAA FSZ%4d ox%9d ybot%9d %s\n", FSZ, ox, ybot, labstring); // TEMP
335 }
336 ox = (int) ( xacent ) ;
337 gdImageLine( im, xacent, ybot, ox, ybot-10, white );
338
339 // draw y tickmarks
340 m = (int) ( 0.001 + angH2/dangs ) ;
341
342 for( i = -m ; i <= m ; i++ ) {
343 oy = oyc + s*i ;
344 gdImageLine( im, XLEFT, oy, XLEFT-5, oy, white );
345
346 sprintf( labstring, "%3d", i*DANGS );
347 gdImageStringTTF( im, brect, // imagespace, bounding
348 white, FNT, FSZ, // color, font, fontsize
349 0.0, XLEFT-4*FSZ, oy+FSZ/2, // angle, x, y
350 labstring ); // the text
351
352 //// printf( "BBB FSZ%4d XLEFT%9d oy %9d %s\n", FSZ, XLEFT, oy, labstring); // TEMP
353 }
354 ox = (int) ( xacent ) ;
355 gdImageLine( im, XLEFT, oyc, XLEFT-10, oyc, white );
356
357 // labels on array -------------------------------------------------------
358 fs= ybot/8 ;
359
360 gdImageStringTTF( im, brect, // imagespace, bounding box
361 white , FNT, fs, 0.0, // color, font, fontsize, angle
362 XLEFT+15, YSIZE-15, // x, y
363 "Ground Pattern" ); // text
364 ///// printf( "fs%4d XLEFT+15%9d YSIZE-15%9d\n", fs, XLEFT+15, YSIZE-15); // TEMP
365
366 // output the frame ------------------------------------------------------
367
368 sprintf( framename, PNGFMT , frame );
369 pngout = fopen( framename, "wb");
370 gdImagePngEx( im, pngout, 1 );
371
372 // output mirror frame ---------------------------------------------------
373
374 sprintf( framename, PNGFMT , (2*NPICS-frame-1) );
375 pngout = fopen( framename, "wb");
376 gdImagePngEx( im, pngout, 1 );
377
378 gdImageDestroy(im);
379 fclose(pngout);
380 }
381
382 printf( "frames complete\n");
383
384 // system( "ls -l adish01dir" );
385
386 printf( "\n\napng\n");
387 printf( "%s\n", PNG2APNG );
388 system( PNG2APNG );
389 }
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.You are not allowed to attach a file to this page.