Attachment 'a3d04.c'
Download 1 // radio array, 5x5x5, rotating
2 //
3 // compile with cc -o a3d04 a3d04.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 a3d04dir ; mkdir a3d04dir"
20 #define PNG2SWF "png2swf -o a3d04.swf -r 5 a3d04dir/*.png"
21 #define PNGFMT "a3d04dir/a%04d.png"
22
23 #define YCENT 130
24 #define WAVELENGTH 10.0 // radio wavelength in pixel units
25 #define SPACE 30.0 // array spacing in pixel units
26 #define NPICS 100 // number of frames
27
28 #define TANGX 0.0 // target X angle in degrees
29 #define TANGY 0.0 // target Y angle in degrees
30 #define DANGX 0.0 // display center X angle in degrees
31 #define DANGY 0.0 // display center Y angle in degrees
32
33 #define DANGW 150.0 // display angle width in degrees
34 #define DANGS 10.0 // display angle step
35
36 #define ZSCALE 400.0 // depth scaling
37 // #define TILT 0.04 // array tilt around X axis, radians
38 #define TILT 0.00 // array tilt around X axis, radians
39 #define SPOT 10 // size of drawn spot
40 #define ASIZE 5 // number of elements per side
41 #define XLEFT 40 // left side pixel count
42 #define XSIZE 1000 // display window in pixels
43 #define YSIZE 750 // display window in pixels
44 #define FNT "DejaVuMonoSans"
45 #define FSZ 10
46
47 // ==========================================================================
48
49 int main() {
50
51 gdImagePtr im ;
52 double space = SPACE ;
53 double xleft = XLEFT ;
54 double yacent = YCENT ;
55 double xacent = 0.5*(XSIZE+XLEFT) ;
56 int ybot = 2* (int)(yacent) ;
57 double angH = DANGW*(YSIZE-ybot)/(XSIZE-XLEFT);
58 double pi2 = 8.0 * atan( 1.0 );
59 double deg2rad = pi2/360.0 ;
60
61 int oxc = (XSIZE+XLEFT)/2 ;
62 int oyc = (YSIZE+ybot)/2 ;
63
64 double dangx ; // pixel X angle radians
65 double dangx0 = deg2rad*(DANGX-0.5*DANGW) ; // left pixel value, radians
66 double dangx1 = deg2rad*(DANGX+0.5*DANGW) ; // right pixel value, radians
67 double dangxs = deg2rad*DANGW/(XSIZE-XLEFT); // pixel X slope radians
68 double dangxf = dangx0-dangxs*XLEFT ; // pixel X offset radians
69 double dangy ; // pixel Y angle
70 double dangy0 = deg2rad*(DANGY+0.5*angH) ; // top pixel value, radians
71 double dangy1 = deg2rad*(DANGY-0.5*angH) ; // bottom pixel value, radians
72 double dangys = deg2rad*angH/(YSIZE-ybot) ; // pixel Y slope radians
73 double dangyf = dangy0-dangys*YSIZE ; // pixel Y offset radians
74 double k = pi2/WAVELENGTH ; // wavenumber
75 double tilt = TILT ;
76 FILE *pngout ;
77 char dirname[80] ;
78 char framename[80] ;
79 char labstring[80] ;
80 int black, sun1, white, red, green, blue, gray, dgray, trans ;
81 int gcolor[256];
82 double ax[ASIZE][ASIZE][ASIZE]; // x position of element
83 double ay[ASIZE][ASIZE][ASIZE]; // y position of element
84 double az[ASIZE][ASIZE][ASIZE]; // z position of element
85 double ap[ASIZE][ASIZE][ASIZE]; // phase of element
86 double xx, yy, zz ;
87 double xr, yr, zr ;
88 double bx, by, bz ; // coordinates rotated on y axis
89 double cx, cy, cz ; // coordinates rotated on x axis
90 int ix, iy, iz ;
91 int ox, oy ; // output grid
92 double zscale ;
93 double offset=0.5*(1.0-ASIZE) ;
94 int xdraw, ydraw ;
95 int icolor ;
96 int cir ;
97 double stilt ;
98 double ctilt ;
99 int mag ; // pixel magnitude, 0-255 ;
100 double ss, cc ; //summed sin and cos components
101 double stx = sin(TANGX ) ;
102 double ctx = cos(TANGX ) ;
103 double sty = sin(TANGY ) ;
104 double cty = cos(TANGY ) ;
105 double spx ; // pixel angle sin and cos
106 double cpx ;
107 double spy ;
108 double cpy ;
109 double asize3 = ASIZE*ASIZE*ASIZE ;
110 double norm = 255.0 /asize3 ;
111 double phase ;
112 int xm ; // tickmark max
113 int xs ; // tickmark scale
114 int frame ;
115 int pct=0 ; // percentage of complete frame
116 int pct0=0 ;
117
118 #ifdef DEBUG
119 printf( "%12.6f%12.6f%12.6f%15.9f\n", dangx0, dangx1, dangxf, dangxs );
120 printf( "%12.6f%12.6f%12.6f%15.9f\n", dangy0, dangy1, dangyf, dangys );
121 #endif
122
123 // set up target directory
124
125 system( RMMKDIR );
126
127 // outer loop, rotate tilt 1/4 circle --------------------------------
128 // swf will continue looping, making apparent full circle
129
130 for( frame=0 ; frame<NPICS ; frame++ ) {
131 tilt = 0.25 * pi2 * ((double) frame)/((double) NPICS ) ;
132 stilt = sin( tilt );
133 ctilt = cos( tilt );
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.