Attachment 'a3d02.c'
Download 1 // radio array, 5x5x5, dynamic spacing
2 //
3 // compile with cc -o a3d02 a3d02.c -lgd -lpng -lm
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 #include "gd.h"
16 #include "math.h"
17 #include <stdio.h>
18
19 #define RMMKDIR "rm -rf a3d02dir ; mkdir a3d02dir"
20 #define PNG2SWF "png2swf -o a3d02.swf -r 10 a3d02dir/*.png"
21 #define PNGFMT "a3d02dir/a%04d.png"
22
23 #define YCENT 130
24 #define WAVELENGTH 10.0 // radio wavelength in pixel units
25 #define SPACE 40.0 // array spacing in pixel units
26 #define SPACEMIN 12.0 // minimum array spacing
27 #define NPICS 100 // number of frames
28
29 #define TANGX 0.0 // target X angle in degrees
30 #define TANGY 0.0 // target Y angle in degrees
31 #define DANGX 0.0 // display center X angle in degrees
32 #define DANGY 0.0 // display center Y angle in degrees
33
34 #define DANGW 150.0 // display angle width in degrees
35 #define DANGS 10.0 // display angle step
36
37 #define ZSCALE 400.0 // depth scaling
38 // #define TILT 0.04 // array tilt around X axis, radians
39 #define TILT 0.00 // array tilt around X axis, radians
40 #define SPOT 10 // size of drawn spot
41 #define ASIZE 5 // number of elements per side
42 #define XLEFT 40 // left side pixel count
43 #define XSIZE 1000 // display window in pixels
44 #define YSIZE 750 // display window in pixels
45 #define FNT "DejaVuMonoSans"
46 #define FSZ 10
47
48 // ==========================================================================
49
50 int main() {
51
52 gdImagePtr im ;
53 double space = SPACE ;
54 double xleft = XLEFT ;
55 double yacent = YCENT ;
56 double xacent = 0.5*(XSIZE+XLEFT) ;
57 int ybot = 2* (int)(yacent) ;
58 double angH = DANGW*(YSIZE-ybot)/(XSIZE-XLEFT);
59 double pi2 = 8.0 * atan( 1.0 );
60 double deg2rad = pi2/360.0 ;
61
62 int oxc = (XSIZE+XLEFT)/2 ;
63 int oyc = (YSIZE+ybot)/2 ;
64
65 double dangx ; // pixel X angle radians
66 double dangx0 = deg2rad*(DANGX-0.5*DANGW) ; // left pixel value, radians
67 double dangx1 = deg2rad*(DANGX+0.5*DANGW) ; // right pixel value, radians
68 double dangxs = deg2rad*DANGW/(XSIZE-XLEFT); // pixel X slope radians
69 double dangxf = dangx0-dangxs*XLEFT ; // pixel X offset radians
70 double dangy ; // pixel Y angle
71 double dangy0 = deg2rad*(DANGY+0.5*angH) ; // top pixel value, radians
72 double dangy1 = deg2rad*(DANGY-0.5*angH) ; // bottom pixel value, radians
73 double dangys = deg2rad*angH/(YSIZE-ybot) ; // pixel Y slope radians
74 double dangyf = dangy0-dangys*YSIZE ; // pixel Y offset radians
75 double k = pi2/WAVELENGTH ; // wavenumber
76 double tilt = TILT ;
77 FILE *pngout ;
78 char dirname[80] ;
79 char framename[80] ;
80 char labstring[80] ;
81 int black, sun1, white, red, green, blue, gray, dgray, trans ;
82 int gcolor[256];
83 double ax[ASIZE][ASIZE][ASIZE]; // x position of element
84 double ay[ASIZE][ASIZE][ASIZE]; // y position of element
85 double az[ASIZE][ASIZE][ASIZE]; // z position of element
86 double ap[ASIZE][ASIZE][ASIZE]; // phase of element
87 double xx, yy, zz ;
88 double xr, yr, zr ;
89 double bx, by, bz ; // coordinates rotated on y axis
90 double cx, cy, cz ; // coordinates rotated on x axis
91 int ix, iy, iz ;
92 int ox, oy ; // output grid
93 double zscale ;
94 double offset=0.5*(1.0-ASIZE) ;
95 int xdraw, ydraw ;
96 int icolor ;
97 int cir ;
98 double stilt = sin( tilt );
99 double ctilt = cos( tilt );
100 int mag ; // pixel magnitude, 0-255 ;
101 double ss, cc ; //summed sin and cos components
102 double stx = sin(TANGX ) ;
103 double ctx = cos(TANGX ) ;
104 double sty = sin(TANGY ) ;
105 double cty = cos(TANGY ) ;
106 double spx ; // pixel angle sin and cos
107 double cpx ;
108 double spy ;
109 double cpy ;
110 double asize3 = ASIZE*ASIZE*ASIZE ;
111 double norm = 255.0 /asize3 ;
112 double phase ;
113 int xm ; // tickmark max
114 int xs ; // tickmark scale
115 int frame ;
116 int pct=0 ; // percentage of complete frame
117 int pct0=0 ;
118
119 #ifdef DEBUG
120 printf( "%12.6f%12.6f%12.6f%15.9f\n", dangx0, dangx1, dangxf, dangxs );
121 printf( "%12.6f%12.6f%12.6f%15.9f\n", dangy0, dangy1, dangyf, dangys );
122 #endif
123
124 // set up target directory
125
126 system( RMMKDIR );
127
128 // outer loop, sinusodial spacing change --------------------------------
129 // from SPACEMIN to SPACE
130
131 for( frame=0 ; frame<NPICS ; frame++ ) {
132 space =0.5*(SPACEMIN-SPACE)*cos( pi2*( ((double)frame)/((double)NPICS) ) );
133 space+=0.5*(SPACEMIN+SPACE) ;
134
135 #ifndef SINGLE
136 printf( "%04d/%04d\r", frame, NPICS ) ;
137 fflush(stdout);
138 #endif
139
140
141 // define array with even spacing ---------------------------------------
142
143 for( iz=0; iz<ASIZE ; iz++ ) {
144 zz = space*(((double)iz)+offset );
145 for( iy=0; iy<ASIZE ; iy++ ) {
146 yy = space*(((double)iy)+offset) ;
147 zr = zz*ctilt - yy*stilt;
148 yr = yy*ctilt + zz*stilt;
149 xr = -2.0*yr ;
150 for( ix=0; ix<ASIZE ; ix++ ) {
151 // compute array positions
152 xx = xr + space*(((double)ix)+offset) ;
153 ax[ix][iy][iz] = xx ;
154 ay[ix][iy][iz] = yr ;
155 az[ix][iy][iz] = zr ;
156
157 // compute target angle
158 // first rotation, Y axis, by TANGX
159 // bx = xx*ctx - zr*stx ;
160 // by = yr ;
161 // bz = zr*ctx + xx*stx ;
162
163 // second rotation, X axis, by TANGY
164
165 // cx = bx ;
166 // cy = by*cty - bz*sty ;
167 // cz = bz*cty + by*sty ;
168
169 // ap[ix][iy][iz] = -k*cz ;
170 ap[ix][iy][iz] = -k*( (zr*ctx+xx*stx)*cty + yr*sty ) ;
171
172 }
173 #ifdef DEBUG
174 printf( "%3d%3d%18.3f%18.3f%18.3f%18.3f\n", iy, iz,
175 ax[2][iy][iz], ay[2][iy][iz], az[2][iy][iz], ap[2][iy][iz] );
176 #endif
177 } }
178 #ifdef SINGLE
179 printf( "array setup done\n");
180 #endif
181
182 // set up array and define colors -----------------------------------------
183
184 im = gdImageCreateTrueColor(XSIZE, YSIZE );
185
186 // Allocate standard colors
187 black = gdImageColorAllocate(im, 0, 0, 0);
188 white = gdImageColorAllocate(im, 255, 255, 255);
189 sun1 = gdImageColorAllocate(im, 51, 51, 102);
190 red = gdImageColorAllocate(im, 255, 0, 0);
191 green = gdImageColorAllocate(im, 0, 255, 0);
192 blue = gdImageColorAllocate(im, 0, 0, 255);
193 gray = gdImageColorAllocate(im, 128, 128, 128);
194 dgray = gdImageColorAllocate(im, 48, 48, 48);
195 trans = gdImageColorAllocate(im, 1, 1, 1);
196
197 // allocate gray scale array
198 for( icolor=0 ; icolor<256 ; icolor++ ) {
199 gcolor[icolor] = gdImageColorAllocate(im, icolor, icolor, icolor);
200 }
201
202 #ifdef SINGLE
203 printf( "color setup done\n");
204 #endif
205
206 // draw array with perspective ---------------------------------------------
207
208 for( iz=0; iz<ASIZE ; iz++ ) {
209 for( iy=0; iy<ASIZE ; iy++ ) {
210 for( ix=0; ix<ASIZE ; ix++ ) {
211 zscale = 1.0 / ( 1.0- az[ix][iy][iz]/ZSCALE ) ;
212 cir = (int) ( zscale * SPOT );
213 xdraw = (int) ( xacent + zscale*ax[ix][iy][iz] ) ;
214 ydraw = (int) ( yacent + zscale*ay[ix][iy][iz] ) ;
215 icolor = 180 + (int)(30.0*az[ix][iy][iz]/space ) ;
216 if( icolor > 255 ) { icolor = 255 ; }
217 gdImageFilledEllipse(im, xdraw, ydraw, cir, cir, gcolor[icolor] );
218 } } }
219
220 #ifdef SINGLE
221 printf( "array drawn\n");
222 #endif
223
224 // now compute phases
225
226 for( ox=XLEFT ; ox<XSIZE ; ox++ ) {
227 dangx = dangxf + dangxs*((double)ox );
228 spx = sin( dangx );
229 cpx = cos( dangx );
230
231 pct0 = pct ;
232 pct = (100*(ox-XLEFT))/(XSIZE-XLEFT) ;
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 dangy = dangyf + dangys*((double)oy );
243 spy = sin( dangy );
244 cpy = cos( dangy );
245
246 // sum the components
247 ss = 0.0 ;
248 cc = 0.0 ;
249
250 #ifdef DEBUG
251 if( ( ox==oxc ) && ( oy==oyc ) ) {
252 printf( "%4d%5d%12.6f%12.6f%12.6f%12.6f\n\n", ox, oy,
253 cpx, spx, cpy, spy );
254 }
255 #endif
256
257 // compute z distance and phase of each source to plane
258 for( iz=0; iz<ASIZE ; iz++ ) {
259 for( iy=0; iy<ASIZE ; iy++ ) {
260 for( ix=0; ix<ASIZE ; ix++ ) {
261
262 // xr = ax[ix][iy][iz] ;
263 // yr = ax[ix][iy][iz] ;
264 // zr = az[ix][iy][iz] ;
265
266 // compute pixel angle
267 // first rotation, Y axis, by TANGX
268 // bx = xr*cpx - zr*spx ;
269 // by = yr ;
270 // bz = zr*cpx + xr*spx ;
271
272 // second rotation, X axis, by TANGY
273
274 // cx = bx ;
275 // cy = by*cpy - bz*spy ;
276 // cz = bz*cpy + by*spy ;
277
278 // ap[ix][iy][iz] = -k*cz ;
279 // ap[ix][iy][iz] = -k*( (zr*cpx+xr*spx)*cpy + yr*spy);
280
281 phase = k*( ay[ix][iy][iz]*spy +
282 ( az[ix][iy][iz]*cpx + ax[ix][iy][iz]*spx)*cpy ) ;
283 ss += sin( phase + ap[ix][iy][iz] );
284 cc += cos( phase + ap[ix][iy][iz] );
285
286 #ifdef DEBUG
287 if( ( ox==oxc ) && ( oy==oyc ) ) {
288 printf( "%3d%3d%3d%12.6f%12.6f%12.6f%12.6f\n", ix,iy,iz,
289 phase, ap[ix][iy][iz], ss, cc );
290 }
291 #endif
292 }
293 }
294 }
295
296 mag = (int) (norm*sqrt( ss*ss + cc*cc ));
297 if( abs(mag-181) < 10 ) {
298 gdImageSetPixel( im, ox, oy, red ) ;
299 }
300 else {
301 gdImageSetPixel( im, ox, oy, gcolor[mag] ) ;
302 }
303 } }
304
305 #ifdef SINGLE
306 printf( "intensity points drawn\n");
307 #endif
308
309 // draw tickmarks on edges ----------------------------------------------
310
311 // draw x tickmarks
312 xm = (int) (0.001+0.5*(DANGW/DANGS)) ;
313 xs = (XSIZE-XLEFT)*DANGS/DANGW;
314 for( ix = -xm ; ix <= xm ; ix++ ) {
315 ox = oxc + (int) ( xs*ix ) ;
316 gdImageLine( im, ox, ybot, ox, ybot-5, white );
317
318 sprintf( labstring, "%3d", ( (int)( ix*DANGS ) ) );
319 gdImageStringFT( im, NULL, // imagespace, bounding
320 white, FNT, FSZ, // color, font, fontsize
321 0.0, ox+FSZ/2, ybot-FSZ/2, // angle, x, y
322 labstring ); // the text
323
324 }
325 ox = (int) ( xacent ) ;
326 gdImageLine( im, xacent, ybot, ox, ybot-10, white );
327
328 // draw y tickmarks
329 xm = (int) (0.001+0.5*(angH/DANGS)) ;
330 for( ix = -xm ; ix <= xm ; ix++ ) {
331 oy = oyc + (int) ( xs*ix ) ;
332 gdImageLine( im, xleft, oy, xleft-5, oy, white );
333
334 sprintf( labstring, "%3d", ( (int)( ix*DANGS ) ) );
335 gdImageStringFT( im, NULL, // imagespace, bounding
336 white, FNT, FSZ, // color, font, fontsize
337 0.0, xleft-4*FSZ, oy+FSZ/2, // angle, x, y
338 labstring ); // the text
339
340 }
341 ox = (int) ( xacent ) ;
342 gdImageLine( im, xleft, oyc, xleft-10, oyc, white );
343
344 // output the frame ------------------------------------------------------
345
346 sprintf( framename, PNGFMT , frame );
347 pngout = fopen( framename, "wb");
348 gdImagePngEx( im, pngout, 1 );
349 gdImageDestroy(im);
350 fclose(pngout);
351
352 } // end of single frame
353
354 system( PNG2SWF );
355 }
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.