-
Notifications
You must be signed in to change notification settings - Fork 17
/
draw_radiation.c
1348 lines (1098 loc) · 40.6 KB
/
draw_radiation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The official website and doumentation for xnec2c is available here:
* https://www.xnec2c.org/
*/
#include "draw_radiation.h"
#include "shared.h"
/* Buffered points in 3d (xyz) space
* forming the radiation pattern */
point_3d_t *point_3d = NULL;
rgba_t *rdpat_colors = NULL;
GtkWidget *create_gl_window(GtkBuilder **builder);
color_point_t *rdpat_points = NULL;
color_triangle_t *rdpat_triangles = NULL;
#define phi_theta_step_to_idx(phi_step, theta_step) \
((phi_step%fpat.nph)*fpat.nth + (theta_step%fpat.nth))
#define phi_step_to_rad(phi_step) ((fpat.phis + (phi_step%fpat.nph)*fpat.dph)*TORAD)
#define theta_step_to_rad(theta_step) ((fpat.thets + (theta_step%fpat.nth)*fpat.dth)*TORAD)
/*-----------------------------------------------------------------------*/
/* Scale_Gain()
*
* Scales radiation pattern gain according to selected style
* ( ARRL style, logarithmic or linear voltage/power )
*/
double Scale_Gain( double gain, int fstep, int idx )
{
/* Scaled rad pattern gain and pol factor */
double scaled_rad = 0.0;
gain += Polarization_Factor( calc_data.pol_type, fstep, idx );
switch( rc_config.gain_style )
{
case GS_LINP:
scaled_rad = pow(10.0, (gain/10.0));
break;
case GS_LINV:
scaled_rad = pow(10.0, (gain/20.0));
break;
case GS_ARRL:
scaled_rad = exp( 0.058267 * gain );
break;
case GS_LOG:
scaled_rad = gain;
if( scaled_rad < -40 )
scaled_rad = 0.0;
else
scaled_rad = scaled_rad /40.0 + 1.0;
} /* switch( rc_config.gain_style ) */
return( scaled_rad );
} /* Scale_Gain() */
/*-----------------------------------------------------------------------*/
extern GtkWidget *gl_window;
/* Draw_Radiation_Pattern()
*
* Draws the radiation pattern as a frame of line
* segmants joining the points defined by spherical
* co-ordinates theta, phi and r = gain(theta, phi)
*/
static void
Draw_Radiation_Pattern( cairo_t *cr )
{
/* Line segments to draw on Screen */
Segment_t segm;
int
idx,
nth, /* Theta step count */
nph, /* Phi step count */
col_idx, /* Index to rad pattern color buffers */
pts_idx; /* Index to rad pattern 3d-points buffer */
/* Frequency step and polarization type */
int fstep, pol;
/* Theta and phi angles defining a rad pattern point
* and distance of its projection from xyz origin */
double theta, phi, r, r_min, r_range;
/* theta and phi step in rads */
double dth = (double)fpat.dth * (double)TORAD;
double dph = (double)fpat.dph * (double)TORAD;
/* Used to set text in labels */
gchar txt[8];
/* Abort if rad pattern cannot be drawn */
fstep = calc_data.freq_step;
if( isFlagClear(ENABLE_RDPAT) || (fstep < 0) )
return;
pol = calc_data.pol_type;
/* Change drawing if newer rad pattern data */
if( isFlagSet(DRAW_NEW_RDPAT) )
{
size_t mreq = ((size_t)(fpat.nth * fpat.nph)) * sizeof(point_3d_t);
mem_realloc( (void **)&point_3d, mreq, "in draw_radiation.c" );
mreq = ((size_t)(fpat.nth * fpat.nph)) * sizeof(color_point_t);
mem_realloc( (void **)&rdpat_points, mreq, "in draw_radiation.c" );
mreq = (size_t)((fpat.nth-1) * fpat.nph + (fpat.nph-1) * fpat.nth);
mreq *= sizeof(rgba_t);
mem_realloc( (void **)&rdpat_colors, mreq, "in draw_radiation.c" );
ClearFlag( DRAW_NEW_RDPAT );
/* Distance of rdpattern point furthest from xyz origin */
idx = rad_pattern[fstep].max_gain_idx[pol];
rdpattern_proj_params.r_max =
Scale_Gain( rad_pattern[fstep].gtot[idx], fstep, idx);
/* Distance of rdpattern point nearest to xyz origin */
idx = rad_pattern[fstep].min_gain_idx[pol];
r_min = Scale_Gain( rad_pattern[fstep].gtot[idx], fstep, idx);
/* Range of scaled rdpattern gain values */
r_range = rdpattern_proj_params.r_max - r_min;
/* Set radiation pattern projection parametrs */
New_Projection_Parameters(
rdpattern_width,
rdpattern_height,
&rdpattern_proj_params );
/*** Convert radiation pattern values
* to points in 3d space in x,y,z axis ***/
pts_idx = 0;
phi = (double)(fpat.phis * TORAD); /* In rads */
/* Step phi angle */
for( nph = 0; nph < fpat.nph; nph++ )
{
theta = (double)(fpat.thets * TORAD); /* In rads */
/* Step theta angle */
for( nth = 0; nth < fpat.nth; nth++ )
{
if (abs(theta - theta_step_to_rad(nth)) > 1e-12)
pr_err("theta != theta_step_to_rad(nth): %f != %f\n",
theta, theta_step_to_rad(nth));
if (abs(phi - phi_step_to_rad(nph)) > 1e-12)
pr_err("phi != phi_step_to_rad(nph): %f != %f\n",
phi, phi_step_to_rad(nph));
if (pts_idx != phi_theta_step_to_idx(nph, nth))
pr_err("pts_idx != phi_theta_step_to_idx(nph, nth): %d != (%d,%d)\n",
pts_idx, nph, nth);
/* Distance of pattern point from the xyz origin */
r = Scale_Gain( rad_pattern[fstep].gtot[pts_idx], fstep, pts_idx );
/* Distance of pattern point from xyz origin */
point_3d[pts_idx].r = r;
rdpat_points[pts_idx].point.r = r;
/* Distance of point's projection on xyz axis, from origin */
point_3d[pts_idx].z = r * cos(theta);
r *= sin(theta);
point_3d[pts_idx].x = r * cos(phi);
point_3d[pts_idx].y = r * sin(phi);
rdpat_points[pts_idx].point.x = point_3d[pts_idx].x;
rdpat_points[pts_idx].point.y = point_3d[pts_idx].y;
rdpat_points[pts_idx].point.z = point_3d[pts_idx].z;
if (pts_idx < 100)
printf("%4d(%4d): nph=%3d nth=%3d r=%f phi=%f theta=%f x=%+f y=%+f z=+%f\n",
pts_idx,
phi_theta_step_to_idx(nph, nth),
nph, nth,
r, phi, theta,
point_3d[pts_idx].x,
point_3d[pts_idx].y,
point_3d[pts_idx].z);
// Value_to_Color() takes doubles by reference, so need to make temp values
// to pass to the OpenGL fields below.
double red, green, blue;
Value_to_Color(
&red, &green, &blue,
point_3d[pts_idx].r - r_min,
r_range
);
rdpat_points[phi_theta_step_to_idx(nph, nth)].color.r = (float)red;
rdpat_points[phi_theta_step_to_idx(nph, nth)].color.g = (float)green;
rdpat_points[phi_theta_step_to_idx(nph, nth)].color.b = (float)blue;
rdpat_points[phi_theta_step_to_idx(nph, nth)].color.a = 0.5;
/* Step theta in rads */
theta += dth;
/* Step 3d points index */
pts_idx++;
} /* for( nth = 0; nth < fpat.nth; nth++ ) */
/* Step phi in rads */
phi += dph;
} /* for( nph = 0; nph < fpat.nph; nph++ ) */
// <= is not a typo, we want to step one more further in phi to
// close the loop. phi/theta_step_to_rad will take the modulus and
// return the correct index:
mreq = 2 * fpat.nph * (fpat.nth-1) * sizeof(color_triangle_t);
mem_alloc((void**)&rdpat_triangles, mreq, __LOCATION__);
int tri_idx = 0;
for( nth = 0; nth < fpat.nth-1; nth++ )
{
if (abs(0-theta_step_to_rad(nth)) < 1e-12)
continue;
if (abs(M_PI-theta_step_to_rad(nth)) < 1e-12)
continue;
// Step theta angle, there is 1 less set of triangle pairs than
// the number of theta points:
for( nph = 0; nph < fpat.nph; nph++ )
{
rdpat_triangles[tri_idx].cp[0] =
rdpat_points[phi_theta_step_to_idx(nph, nth)];
rdpat_triangles[tri_idx].cp[1] =
rdpat_points[phi_theta_step_to_idx(nph+1, nth)];
rdpat_triangles[tri_idx].cp[2] =
rdpat_points[phi_theta_step_to_idx(nph+1, nth+1)];
if (tri_idx < 10)
printf("A[%4d]. %d=(r=%f, %+f, %+f, %+f)\t%d=(r=%f, %+f, %+f, %+f)\t%d=(r=%f, %+f, %+f, %+f)\n",
tri_idx,
phi_theta_step_to_idx(nph, nth),
rdpat_triangles[tri_idx].cp[0].point.r,
rdpat_triangles[tri_idx].cp[0].point.x,
rdpat_triangles[tri_idx].cp[0].point.y,
rdpat_triangles[tri_idx].cp[0].point.z,
phi_theta_step_to_idx(nph+1, nth),
rdpat_triangles[tri_idx].cp[1].point.r,
rdpat_triangles[tri_idx].cp[1].point.x,
rdpat_triangles[tri_idx].cp[1].point.y,
rdpat_triangles[tri_idx].cp[1].point.z,
phi_theta_step_to_idx(nph+1, nth+1),
rdpat_triangles[tri_idx].cp[2].point.r,
rdpat_triangles[tri_idx].cp[2].point.x,
rdpat_triangles[tri_idx].cp[2].point.y,
rdpat_triangles[tri_idx].cp[2].point.z
);
tri_idx++;
rdpat_triangles[tri_idx].cp[0] =
rdpat_points[phi_theta_step_to_idx(nph, nth)];
rdpat_triangles[tri_idx].cp[1] =
rdpat_points[phi_theta_step_to_idx(nph, nth+1)];
rdpat_triangles[tri_idx].cp[2] =
rdpat_points[phi_theta_step_to_idx(nph+1, nth+1)];
if (tri_idx < 10)
printf("B[%4d]. %d=(r=%f, %+f, %+f, %+f)\t%d=(r=%f, %+f, %+f, %+f)\t%d=(r=%f, %+f, %+f, %+f)\n",
tri_idx,
phi_theta_step_to_idx(nph, nth),
rdpat_triangles[tri_idx].cp[0].point.r,
rdpat_triangles[tri_idx].cp[0].point.x,
rdpat_triangles[tri_idx].cp[0].point.y,
rdpat_triangles[tri_idx].cp[0].point.z,
phi_theta_step_to_idx(nph, nth+1),
rdpat_triangles[tri_idx].cp[1].point.r,
rdpat_triangles[tri_idx].cp[1].point.x,
rdpat_triangles[tri_idx].cp[1].point.y,
rdpat_triangles[tri_idx].cp[1].point.z,
phi_theta_step_to_idx(nph+1, nth+1),
rdpat_triangles[tri_idx].cp[2].point.r,
rdpat_triangles[tri_idx].cp[2].point.x,
rdpat_triangles[tri_idx].cp[2].point.y,
rdpat_triangles[tri_idx].cp[2].point.z
);
tri_idx++;
}
}
/* Calculate RGB value for rad pattern seg.
* The average gain value of the two points
* marking each line segment is used here */
/* Pattern segment color in theta direction */
col_idx = pts_idx = 0;
for( nph = 0; nph < fpat.nph; nph++ )
{
for( nth = 1; nth < fpat.nth; nth++ )
{
Value_to_Color(
&rdpat_colors[col_idx].r, &rdpat_colors[col_idx].g, &rdpat_colors[col_idx].b,
(point_3d[pts_idx].r+point_3d[pts_idx+1].r)/2.0-r_min,
r_range );
rdpat_colors[col_idx].a = 0.5;
col_idx++;
pts_idx++;
} /* for( nph = 0; nph < fpat.nph; nph++ ) */
/* Needed because of "index look-ahead" above */
pts_idx++;
} /* for( nth = 1; nth < fpat.nth; nth++ ) */
/* Pattern segment color in phi direction */
for( nth = 0; nth < fpat.nth; nth++ )
{
pts_idx = nth;
for( nph = 1; nph < fpat.nph; nph++ )
{
Value_to_Color(
&rdpat_colors[col_idx].r, &rdpat_colors[col_idx].g, &rdpat_colors[col_idx].b,
(point_3d[pts_idx].r +
point_3d[pts_idx+fpat.nth].r)/2.0-r_min,
r_range );
col_idx++;
/* Needed because of "index look-ahead" above */
pts_idx += fpat.nth;
} /* for( nth = 0; nth < fpat.nth; nth++ ) */
} /* for( nph = 1; nph < fpat.nph; nph++ ) */
/* Show max gain on color code bar */
snprintf( txt, 8, "%6.1f", rad_pattern[fstep].max_gain[pol] );
gtk_label_set_text( GTK_LABEL(Builder_Get_Object(
rdpattern_window_builder, "rdpattern_colorcode_maxlabel")),
txt );
/* Show min gain on color code bar */
snprintf( txt, 6, "%4.1f", rad_pattern[fstep].min_gain[pol] );
gtk_label_set_text(GTK_LABEL(Builder_Get_Object(
rdpattern_window_builder, "rdpattern_colorcode_minlabel")),
txt );
} /* if( isFlagSet(DRAW_NEWRDPAT) ) ) */
xnec2_widget_queue_draw(create_gl_window(NULL));
/* Draw xyz axes to Screen */
Draw_XYZ_Axes( cr, rdpattern_proj_params );
/* Overlay structure on Near Field pattern */
if( isFlagSet(OVERLAY_STRUCT) )
{
/* Save structure projection params pointers */
projection_parameters_t params = structure_proj_params;
/* Divert structure drawing to rad pattern area */
structure_proj_params = rdpattern_proj_params;
structure_proj_params.r_max = params.r_max;
structure_proj_params.xy_scale =
params.xy_scale1 * rdpattern_proj_params.xy_zoom;
/* Process and draw geometry if enabled */
Process_Wire_Segments();
Process_Surface_Patches();
Draw_Surface_Patches( cr, structure_segs+data.n, data.m );
Draw_Wire_Segments( cr, structure_segs, data.n );
/* Restore structure projection params */
structure_proj_params = params;
} /* if( isFlagSet(OVERLAY_STRUCT) ) */
/*** Draw rad pattern on screen ***/
/* Draw segments along theta direction */
col_idx = pts_idx = 0;
// printf("==== points start\n");
for( nph = 0; nph < fpat.nph; nph++ )
{
for( nth = 1; nth < fpat.nth; nth++ )
{
/* Project line segment to Screen */
Set_Gdk_Segment(
&segm,
&rdpattern_proj_params,
point_3d[pts_idx].x,
point_3d[pts_idx].y,
point_3d[pts_idx].z,
point_3d[pts_idx+1].x,
point_3d[pts_idx+1].y,
point_3d[pts_idx+1].z );
/*
printf(
"{ { %f, %f, %f }, { %f, %f, %f } }, // theta a\n"
"{ { %f, %f, %f }, { %f, %f, %f } }, // theta b\n",
point_3d[pts_idx].x, point_3d[pts_idx].y, point_3d[pts_idx].z,
red[col_idx], grn[col_idx], blu[col_idx],
point_3d[pts_idx+1].x, point_3d[pts_idx+1].y, point_3d[pts_idx+1].z,
red[col_idx], grn[col_idx], blu[col_idx]);
*/
pts_idx++;
/* Draw segment */
cairo_set_source_rgb( cr, rdpat_colors[col_idx].r, rdpat_colors[col_idx].g,
rdpat_colors[col_idx].b);
Cairo_Draw_Line( cr, segm.x1, segm.y1, segm.x2, segm.y2 );
col_idx++;
} /* for( nth = 1; nth < fpat.nth; nth++ ) */
pts_idx++;
} /* for( nph = 0; nph < fpat.nph; nph++ ) */
/* Draw segments along phi direction */
for( nth = 0; nth < fpat.nth; nth++ )
{
pts_idx = nth;
for( nph = 1; nph < fpat.nph; nph++ )
{
/* Project line segment to Screen */
Set_Gdk_Segment(
&segm,
&rdpattern_proj_params,
point_3d[pts_idx].x,
point_3d[pts_idx].y,
point_3d[pts_idx].z,
point_3d[pts_idx+fpat.nth].x,
point_3d[pts_idx+fpat.nth].y,
point_3d[pts_idx+fpat.nth].z );
/*
printf(
"{ { %f, %f, %f }, { %f, %f, %f } }, // phi a\n"
"{ { %f, %f, %f }, { %f, %f, %f } }, // phi b\n",
point_3d[pts_idx].x, point_3d[pts_idx].y, point_3d[pts_idx].z,
red[col_idx], grn[col_idx], blu[col_idx],
point_3d[pts_idx+fpat.nth].x, point_3d[pts_idx+fpat.nth].y, point_3d[pts_idx+fpat.nth].z,
red[col_idx], grn[col_idx], blu[col_idx]);
*/
/* Draw segment */
cairo_set_source_rgb( cr, rdpat_colors[col_idx].r, rdpat_colors[col_idx].g,
rdpat_colors[col_idx].b);
Cairo_Draw_Line( cr, segm.x1, segm.y1, segm.x2, segm.y2 );
col_idx++;
/* Needed because drawing segments "looks ahead"
* in the 3d points buffer in the above loop */
pts_idx += fpat.nth;
} /* for( nph = 1; nph < fpat.nph; nph++ ) */
} /* for( nth = 0; nth < fpat.nth; nth++ ) */
/* Show gain in direction of viewer */
Show_Viewer_Gain(
rdpattern_window_builder,
"rdpattern_viewer_gain",
rdpattern_proj_params );
} /* Draw_Radiation_Pattern() */
/*-----------------------------------------------------------------------*/
/* Draw_Near_Field()
*
* Draws near E/H fields and Poynting vector
*/
static void
Draw_Near_Field( cairo_t *cr )
{
int idx, npts; /* Number of points to plot */
double
fx, fy, fz, /* Co-ordinates of "free" end of field lines */
fscale; /* Scale factor for equalizing field line segments */
/* Scale factor ref, for normalizing field strength values */
static double dr;
/* Co-ordinates of Poynting vectors */
static double *pov_x = NULL, *pov_y = NULL;
static double *pov_z = NULL, *pov_r = NULL;
/* Range of Poynting vector values,
* its max and min and log of max/min */
static double pov_max = 0, max;
/* Used to set text in labels */
gchar txt[9];
/* Line segments to draw on Screen */
Segment_t segm;
/* For coloring field lines */
double xred = 0.0, xgrn = 0.0, xblu = 0.0;
/* Abort if drawing a near field pattern is not possible */
if( isFlagClear(ENABLE_NEAREH) || !near_field.valid )
return;
/* Initialize projection parameters */
if( isFlagSet(DRAW_NEW_EHFIELD) )
{
/* Reference for scale factor used in
* normalizing field strength values */
if( fpat.near ) /* Spherical co-ordinates */
dr = (double)fpat.dxnr;
else /* Rectangular co-ordinates */
dr = sqrt(
(double)fpat.dxnr * (double)fpat.dxnr +
(double)fpat.dynr * (double)fpat.dynr +
(double)fpat.dznr * (double)fpat.dznr )/1.75;
/* Set radiation pattern projection parametrs */
/* Distance of field point furthest from xyz origin */
rdpattern_proj_params.r_max = near_field.r_max + dr;
New_Projection_Parameters(
rdpattern_width,
rdpattern_height,
&rdpattern_proj_params );
ClearFlag( DRAW_NEW_EHFIELD );
} /* if( isFlagSet( DRAW_NEW_EHFIELD ) */
/* Draw xyz axes to Screen */
Draw_XYZ_Axes( cr, rdpattern_proj_params );
/* Overlay structure on Near Field pattern */
if( isFlagSet(OVERLAY_STRUCT) )
{
/* Save projection params pointers */
projection_parameters_t params = structure_proj_params;
/* Divert structure drawing to rad pattern area */
structure_proj_params = rdpattern_proj_params;
/* Process and draw geometry if enabled */
Process_Wire_Segments();
Process_Surface_Patches();
Draw_Surface_Patches( cr, structure_segs+data.n, data.m );
Draw_Wire_Segments( cr, structure_segs, data.n );
/* Restore structure params */
structure_proj_params = params;
} /* if( isFlagSet(OVERLAY_STRUCT) ) */
/* Step thru near field values */
npts = fpat.nrx * fpat.nry * fpat.nrz;
for( idx = 0; idx < npts; idx++ )
{
/*** Draw Near E Field ***/
if( isFlagSet(DRAW_EFIELD) && (fpat.nfeh & NEAR_EFIELD) )
{
/* Set gc attributes for segment */
Value_to_Color( &xred, &xgrn, &xblu,
near_field.er[idx], near_field.max_er );
/* Scale factor for each field point, to make
* near field direction lines equal-sized */
fscale = dr / near_field.er[idx];
/* Scaled field values are used to set one end of a
* line segment that represents direction of field.
* The other end is set by the field point co-ordinates */
fx = near_field.px[idx] + near_field.erx[idx] * fscale;
fy = near_field.py[idx] + near_field.ery[idx] * fscale;
fz = near_field.pz[idx] + near_field.erz[idx] * fscale;
/* Project new line segment of
* phi chain to the Screen */
Set_Gdk_Segment(
&segm, &rdpattern_proj_params,
near_field.px[idx], near_field.py[idx], near_field.pz[idx],
fx, fy, fz );
/* Draw segment */
cairo_set_source_rgb( cr, xred, xgrn, xblu );
Cairo_Draw_Line( cr, segm.x1, segm.y1, segm.x2, segm.y2 );
} /* if( isFlagSet(DRAW_EFIELD) && (fpat.nfeh & NEAR_EFIELD) ) */
/*** Draw Near H Field ***/
if( isFlagSet(DRAW_HFIELD) && (fpat.nfeh & NEAR_HFIELD) )
{
/* Set gc attributes for segment */
Value_to_Color( &xred, &xgrn, &xblu,
near_field.hr[idx], near_field.max_hr );
/* Scale factor for each field point, to make
* near field direction lines equal-sized */
fscale = dr / near_field.hr[idx];
/* Scaled field values are used to set one end of a
* line segment that represents direction of field.
* The other end is set by the field point co-ordinates */
fx = near_field.px[idx] + near_field.hrx[idx] * fscale;
fy = near_field.py[idx] + near_field.hry[idx] * fscale;
fz = near_field.pz[idx] + near_field.hrz[idx] * fscale;
/* Project new line segment of
* phi chain to the Screen */
Set_Gdk_Segment(
&segm, &rdpattern_proj_params,
near_field.px[idx], near_field.py[idx], near_field.pz[idx],
fx, fy, fz );
/* Draw segment */
cairo_set_source_rgb( cr, xred, xgrn, xblu );
Cairo_Draw_Line( cr, segm.x1, segm.y1, segm.x2, segm.y2 );
} /* if( isFlagSet(DRAW_HFIELD) && (fpat.nfeh & NEAR_HFIELD) ) */
/*** Draw Poynting Vector ***/
if( isFlagSet(DRAW_POYNTING) &&
(fpat.nfeh & NEAR_EFIELD) &&
(fpat.nfeh & NEAR_HFIELD) )
{
int ipv; /* Mem request and index */
static size_t mreq = 0;
/* Allocate on new near field matrix size */
if( !mreq || isFlagSet(ALLOC_PNTING_BUFF) )
{
mreq = (size_t)npts * sizeof( double );
mem_realloc( (void **)&pov_x, mreq, "in draw_radiation.c" );
mem_realloc( (void **)&pov_y, mreq, "in draw_radiation.c" );
mem_realloc( (void **)&pov_z, mreq, "in draw_radiation.c" );
mem_realloc( (void **)&pov_r, mreq, "in draw_radiation.c" );
ClearFlag( ALLOC_PNTING_BUFF );
}
/* Calculate Poynting vector and its max and min */
pov_max = 0;
for( ipv = 0; ipv < npts; ipv++ )
{
pov_x[ipv] =
near_field.ery[ipv] * near_field.hrz[ipv] -
near_field.hry[ipv] * near_field.erz[ipv];
pov_y[ipv] =
near_field.erz[ipv] * near_field.hrx[ipv] -
near_field.hrz[ipv] * near_field.erx[ipv];
pov_z[ipv] =
near_field.erx[ipv] * near_field.hry[ipv] -
near_field.hrx[ipv] * near_field.ery[ipv];
pov_r[ipv] = sqrt(
pov_x[ipv] * pov_x[ipv] +
pov_y[ipv] * pov_y[ipv] +
pov_z[ipv] * pov_z[ipv] );
if( pov_max < pov_r[ipv] )
pov_max = pov_r[ipv];
} /* for( ipv = 0; ipv < npts; ipv++ ) */
/* Set gc attributes for segment */
Value_to_Color( &xred, &xgrn, &xblu, pov_r[idx], pov_max );
/* Scale factor for each field point, to make
* near field direction lines equal-sized */
fscale = dr / pov_r[idx];
/* Scaled field values are used to set one end of a
* line segment that represents direction of field.
* The other end is set by the field point co-ordinates */
fx = near_field.px[idx] + pov_x[idx] * fscale;
fy = near_field.py[idx] + pov_y[idx] * fscale;
fz = near_field.pz[idx] + pov_z[idx] * fscale;
/* Project new line segment of
* Poynting vector to the Screen */
Set_Gdk_Segment(
&segm,
&rdpattern_proj_params,
near_field.px[idx], near_field.py[idx],
near_field.pz[idx], fx, fy, fz );
/* Draw segment */
cairo_set_source_rgb( cr, xred, xgrn, xblu );
Cairo_Draw_Line( cr, segm.x1, segm.y1, segm.x2, segm.y2 );
} /* if( isFlagSet(DRAW_POYNTING) ) */
} /* for( idx = 0; idx < npts; idx++ ) */
if( isFlagSet(NEAREH_ANIMATE) )
{
return;
}
/* Show max field strength on color code bar */
if( isFlagSet(DRAW_EFIELD) )
max = near_field.max_er;
else if( isFlagSet(DRAW_HFIELD) )
max = near_field.max_hr;
else if( isFlagSet(DRAW_POYNTING) )
max = pov_max;
snprintf( txt, sizeof(txt), "%8.2E", max );
gtk_label_set_text( GTK_LABEL(Builder_Get_Object(
rdpattern_window_builder, "rdpattern_colorcode_maxlabel")),
txt );
/* Show min field strength on color code bar */
gtk_label_set_text( GTK_LABEL(Builder_Get_Object(
rdpattern_window_builder, "rdpattern_colorcode_minlabel")),
"0" );
} /* Draw_Near_Field() */
/*-----------------------------------------------------------------------*/
/* Draw_Radiation()
*
* Draws the radiation pattern or near E/H fields
*/
int
_Draw_Radiation( cairo_t *cr )
{
/* Clear drawingarea */
cairo_set_source_rgb( cr, BLACK );
cairo_rectangle(
cr, 0.0, 0.0,
(double)rdpattern_proj_params.width,
(double)rdpattern_proj_params.height);
cairo_fill( cr );
/* Abort if xnec2c may be quit by user */
if( isFlagSet(MAIN_QUIT) || isFlagClear(ENABLE_EXCITN) )
return FALSE;
/* Don't draw radiation pattern when freq
* loop is running and optimizer enabled */
if( isFlagSet(OPTIMIZER_OUTPUT) && isFlagSet(FREQ_LOOP_RUNNING) )
return FALSE;
// Try to hold the lock to prevent drawing the radiation pattern
// since it could be drawing while inotify triggers a new freqloop:
int locked = g_mutex_trylock(&global_lock);
/* Draw rad pattern or E/H fields */
if( isFlagSet(DRAW_GAIN) )
Draw_Radiation_Pattern( cr );
else if( isFlagSet(DRAW_EHFIELD) )
Draw_Near_Field( cr );
/* Display frequency step */
if (calc_data.freq_step >= 0)
Display_Fstep( rdpattern_fstep_entry, calc_data.freq_step );
if (locked)
g_mutex_unlock(&global_lock);
return TRUE;
} /* Draw_Radiation() */
int Draw_Radiation( cairo_t *cr )
{
int ret;
g_mutex_lock(&freq_data_lock);
ret = _Draw_Radiation( cr );
g_mutex_unlock(&freq_data_lock);
return ret;
}
/*-----------------------------------------------------------------------*/
gboolean
Animate_Near_Field( gpointer udata )
{
/* omega*t = 2*pi*f*t = Time-varying phase of excitation */
static double wt = 0.0;
int idx, npts;
if( isFlagClear(NEAREH_ANIMATE) )
return( FALSE );
/* Number of points in near fields */
npts = fpat.nrx * fpat.nry * fpat.nrz;
for( idx = 0; idx < npts; idx++ )
{
if( isFlagSet(DRAW_EFIELD) || isFlagSet(DRAW_POYNTING) )
{
/* Real component of complex E field strength */
near_field.erx[idx] = near_field.ex[idx] *
cos( wt + near_field.fex[idx] );
near_field.ery[idx] = near_field.ey[idx] *
cos( wt + near_field.fey[idx] );
near_field.erz[idx] = near_field.ez[idx] *
cos( wt + near_field.fez[idx] );
/* Near total electric field vector */
near_field.er[idx] = sqrt(
near_field.erx[idx] * near_field.erx[idx] +
near_field.ery[idx] * near_field.ery[idx] +
near_field.erz[idx] * near_field.erz[idx] );
if( near_field.max_er < near_field.er[idx] )
near_field.max_er = near_field.er[idx];
}
if( isFlagSet(DRAW_HFIELD) || isFlagSet(DRAW_POYNTING) )
{
/* Real component of complex H field strength */
near_field.hrx[idx] = near_field.hx[idx] *
cos( wt + near_field.fhx[idx] );
near_field.hry[idx] = near_field.hy[idx] *
cos( wt + near_field.fhy[idx] );
near_field.hrz[idx] = near_field.hz[idx] *
cos( wt + near_field.fhz[idx] );
/* Near total magnetic field vector*/
near_field.hr[idx] = sqrt(
near_field.hrx[idx] * near_field.hrx[idx] +
near_field.hry[idx] * near_field.hry[idx] +
near_field.hrz[idx] * near_field.hrz[idx] );
if( near_field.max_hr < near_field.hr[idx] )
near_field.max_hr = near_field.hr[idx];
}
} /* for( idx = 0; idx < npts; idx++ ) */
/* Increment excitation phase, keep < 2pi */
wt += near_field.anim_step;
if( wt >= (double)M_2PI )
wt = 0.0;
xnec2_widget_queue_draw( rdpattern_drawingarea );
return( TRUE );
} /* Animate_Near_Field() */
/*-----------------------------------------------------------------------*/
/* Polarization_Factor()
*
* Calculates polarization factor from axial
* ratio and tilt of polarization ellipse
*/
double
Polarization_Factor( int pol_type, int fstep, int idx )
{
double axrt, axrt2, tilt2, polf = 1.0;
switch( pol_type )
{
case POL_TOTAL:
polf = 1.0;
break;
case POL_HORIZ:
axrt2 = rad_pattern[fstep].axrt[idx];
axrt2 *= axrt2;
tilt2 = sin( rad_pattern[fstep].tilt[idx] );
tilt2 *= tilt2;
polf = (axrt2 + (1.0 - axrt2) * tilt2) / (1.0 + axrt2);
break;
case POL_VERT:
axrt2 = rad_pattern[fstep].axrt[idx];
axrt2 *= axrt2;
tilt2 = cos( rad_pattern[fstep].tilt[idx] );
tilt2 *= tilt2;
polf = (axrt2 + (1.0 - axrt2) * tilt2) / (1.0 + axrt2);
break;
case POL_LHCP:
axrt = rad_pattern[fstep].axrt[idx];
axrt2 = axrt * axrt;
polf = (1.0 + 2.0 * axrt + axrt2) / 2.0 / (1.0 + axrt2);
break;
case POL_RHCP:
axrt = rad_pattern[fstep].axrt[idx];
axrt2 = axrt * axrt;
polf = (1.0 - 2.0 * axrt + axrt2) / 2.0 / (1.0 + axrt2);
}
if( polf < 1.0E-200 ) polf = 1.0E-200;
polf = 10.0 * log10( polf );
return( polf );
} /* Polarization_Factor() */
/*-----------------------------------------------------------------------*/
/* Set_Polarization()
*
* Sets the polarization type of gain to be plotted
*/
void
Set_Polarization( int pol )
{
calc_data.pol_type = pol;
Set_Window_Labels();
/* Show gain in direction of viewer */
if( isFlagSet(INPUT_OPENED) )
Show_Viewer_Gain( main_window_builder, "main_gain_entry", structure_proj_params );
/* Enable redraw of rad pattern */
SetFlag( DRAW_NEW_RDPAT );
/* Trigger a redraw of drawingareas */
if( isFlagSet(DRAW_ENABLED) )
{
xnec2_widget_queue_draw( rdpattern_drawingarea );
}
if( isFlagSet(PLOT_ENABLED) )
{
xnec2_widget_queue_draw( freqplots_drawingarea );
}
} /* Set_Polarization() */
/*-----------------------------------------------------------------------*/
/* Set_Gain_Style()
*
* Sets the radiation pattern Gain scaling style
*/
void
Set_Gain_Style( int gs )
{
static char *scale_widget_names[NUM_SCALES] = {
"rdpattern_linear_power",
"rdpattern_linear_voltage",
"rdpattern_arrl_style",
"rdpattern_logarithmic"
};
GtkWidget *widget;
// This should never happen:
if (gs >= NUM_SCALES)
return;
rc_config.gain_style = gs;
widget = Builder_Get_Object( rdpattern_window_builder, scale_widget_names[rc_config.gain_style] );
gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(widget), TRUE );
Set_Window_Labels();
/* Trigger a redraw of drawingarea */
if( isFlagSet(DRAW_ENABLED) )
{
/* Enable redraw of rad pattern */
SetFlag( DRAW_NEW_RDPAT );
xnec2_widget_queue_draw( rdpattern_drawingarea );
}
} /* Set_Gain_Style() */
/*-----------------------------------------------------------------------*/
/* New_Radiation_Projection_Angle()
*
* Calculates new projection parameters when a
* structure projection angle (Wr or Wi) changes
*/