-
Notifications
You must be signed in to change notification settings - Fork 0
/
em_waves.js
280 lines (233 loc) · 7.04 KB
/
em_waves.js
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
$(function(){
gPolar3d = new Polarize3d($('#viewport'));
$('#run_checkbox').change(doTimer);
$(".polslider").slider({
min: 0,
max: 360,
step: 1,
value: 90,
slide:ChangeSliders
});
$(".polbox").change(ChangeSliders);
ChangeSliders();
doTimer();
});
var gTimer;
var gTimerOn = true;
var gLastTimerTime = 0;
function doTimer()
{
if($('#run_checkbox').is(':checked')) {
gPolar3d.Rebuild();
gTimer = setTimeout("doTimer()",100);
var d = new Date();
var t= d.getTime();
// $('#fps').html(1000.0/(t-gLastTimerTime))
gLastTimerTime = t;
} else {
if(gTimer) clearTimeout(gTimer);
}
}
var polarizers=[
{z: -200, angle: 0},
{z:000 , angle: 1.2},
{z:200 , angle: 1.2}
];
var polarizer_positions = [-200,0,200];
function ChangeSliders()
{
gPolar3d.gSetupDirty = true;
polarizers = []
var sliders = $(".polslider").each(function(){
var v = $(this).slider('value');
var p = this.getAttribute('myPol');
var angle = v/180*Math.PI;
if($('input[myPol='+p+']').is(':checked')) polarizers.push({z:polarizer_positions[p],angle:angle});
});
console.log(polarizers);
}
// Subclass of Pad3d.
Polarize3d.prototype = new Pad3d;
Polarize3d.prototype.constructor = Polarize3d;
function Polarize3d( element, options ){
// console.log('TriDView ctor');
if(!element) {
// console.log("TriDView: NULL element supplied.");
return;
}
if($(element).length<1) {
// console.log()
return;
}
var settings = {
default_look_at: [0,0,0],
default_camera_distance: 800,
camera_distance_max: 8000,
camera_distance_min: 50,
default_theta: -0.1,
default_phi: 0.5,
}
$.extend(true,settings,options); // Change default settings by provided qualities.
Pad3d.call(this, element, settings); // Give settings to Pad contructor.
this.ResetView();
this.gSetupDirty = true;
}
var twopi = 2*Math.PI;
Polarize3d.prototype.Rebuild = function()
{
if(this.gSetupDirty) {
// Draw the scenery elements.
this.objects = [];
// for(var ip=0;ip<polarizers.length;ip++) {
// this.AddPolarizer(polarizers[ip].z, polarizers[ip].angle);
// }
this.scenery_objects = this.objects.slice(0); // make a copy
} else {
this.objects = this.scenery_objects.slice(0); // copy back.
}
var d = new Date();
var t = d.getTime();
// Draw polarizers into base
var wavelength = 100;
var period = 5000; // ms
var phase = twopi*((t/period)%1.0);
var amp = 100;
// this.AddCircularWave(wavelength,phase,1.0,-500,polarizers[0].z);
this.AddLinearWave(amp,wavelength,phase,0,-500,500,true,true);
// this.CreateFrame();
// if ($('#ctl-show-hitmap-tracks').is(':checked')) { this.CreateTracks(); }
// if ($('#ctl-show-hitmap-hits').is(':checked')) { this.CreateStrips(); }
// if ($('#ctl-show-hitmap-vertices').is(':checked')) { this.CreateVertices(); }
// console.log("built ",this.objects.length," objects");
this.Draw();
}
function NormalizeVector(v)
{
var dd = (v.x*v.x + v.y*v.y + v.z*v.z);
if(dd<=0) return;
var d = Math.sqrt(dd);
v.x /= d;
v.y /= d;
v.z /= d;
}
Polarize3d.prototype.AddArrow = function(x,y,z,x2,y2,z2,head_size,width,color,obj)
{
var n = {x: x2-x, y: y2-y, z: z2-z};
NormalizeVector(n);
this.AddLine(x,y,z,x2,y2,z2,width,color,obj);
// Vector direction
// find a perpendicular vector. This is a cheat.
// var u = {x: n.y, y:n.x, z:0};
// NormalizeVector(u);
// var v = {x: (n.y*u.z - n.z*u.y),
// y: (n.z*u.x - n.x*u.z),
// z: (n.x*u.y - n.y*u.x)
// };
// NormalizeVector(v);
var v = {x:0, y:0, z:1};
// this.AddLine(x2,y2,z2,
// x2 - n.x*head_size + u.x*head_size,
// y2 - n.y*head_size + u.y*head_size,
// z2 - n.z*head_size + u.z*head_size,
// width, color, obj);
// this.AddLine(x2,y2,z2,
// x2 - n.x*head_size - u.x*head_size,
// y2 - n.y*head_size - u.y*head_size,
// z2 - n.z*head_size - u.z*head_size,
// width, color, obj);
this.AddLine(x2,y2,z2,
x2 - 2*n.x*head_size + v.x*head_size,
y2 - 2*n.y*head_size + v.y*head_size,
z2 - 2*n.z*head_size + v.z*head_size,
width, color, obj);
this.AddLine(x2,y2,z2,
x2 - 2*n.x*head_size - v.x*head_size,
y2 - 2*n.y*head_size - v.y*head_size,
z2 - 2*n.z*head_size - v.z*head_size,
width, color, obj);
}
Polarize3d.prototype.AddLinearWave = function(amp,wavelength,phase,polarization,from_z,to_z,show_vectors,show_bfield)
{
// console.log(wavelength,phase,polarization,from_z,to_z);
var n = 0;
var Ex_last;
var Ey_last;
var Bx_last;
var By_last;
var z_last;
var i = 0;
this.AddLine(0,0,from_z,0,0,to_z,1,'black',null);
for(var z=from_z; z<=to_z; z+=5) {
var E = amp*Math.sin(phase - twopi*z/wavelength );
var Ex = Math.cos(polarization)*E;
var Ey = Math.sin(polarization)*E;
var Bx = Math.cos(polarization+twopi/4)*E;
var By = Math.sin(polarization+twopi/4)*E;
// console.debug(E,Ex,Ey);
if(n>0) {
this.AddLine(Ex_last,Ey_last,z_last,Ex,Ey,z,2,'blue',null);
if(show_bfield )this.AddLine(Bx_last,By_last,z_last,Bx,By,z,2,'red',null);
} else n++;
if(show_vectors && (i%4==0)) {
this.AddArrow(0,0,z,Ex,Ey,z,Math.abs(amp*0.05),1,'blue',null);
if(show_bfield )
this.AddArrow(0,0,z,Bx,By,z,Math.abs(amp*0.05),1,'red',null);
}
Ex_last = Ex;
Ey_last = Ey;
Bx_last = Bx;
By_last = By;
z_last = z;
i++;
}
}
Polarize3d.prototype.AddCircularWave = function(wavelength,phase,polarization,from_z,to_z)
{
var n = 0;
var Ex_last;
var Ey_last;
var z_last;
for(var z=from_z; z<=to_z; z+=5) {
var E = 100;
var Ex = Math.cos(polarization*(phase - twopi*z/wavelength))*E;
var Ey = Math.sin(polarization*(phase - twopi*z/wavelength))*E;
if(n>0) {
this.AddLine(Ex_last,Ey_last,z_last,Ex,Ey,z,1,'black',null);
} else n++;
Ex_last = Ex;
Ey_last = Ey;
z_last = z;
}
}
Polarize3d.prototype.AddPolarizer = function(z,angle)
{
this.AddCircle(z,200);
// this.AddCircle(z+10,200);
var hw = 150; // halfwidth
var hh = 15; // halfheight
var p=[
{x: -hw, y:-hh},
{x: hw, y:-hh},
{x: hw, y: hh},
{x: -hw, y: hh},
{x: -hw, y:-hh},
];
for(var i=0;i<4;i++) {
var j = i+1;
this.AddLine(Math.cos(angle)*p[i].x - Math.sin(angle)*p[i].y, Math.sin(angle)*p[i].x + Math.cos(angle)*p[i].y , z,
Math.cos(angle)*p[j].x - Math.sin(angle)*p[j].y, Math.sin(angle)*p[j].x + Math.cos(angle)*p[j].y , z,
1,'blue',null);
}
}
Polarize3d.prototype.AddCircle = function(z,r)
{
var nsteps = 18;
var dtheta = Math.PI*2/(nsteps);
for(var i=0;i<nsteps;i++) {
var x1 = r * Math.cos(i*dtheta);
var x2 = r * Math.cos((i+1)*dtheta);
var y1 = r * Math.sin(i*dtheta);
var y2 = r * Math.sin((i+1)*dtheta);
this.AddLine( x1, y1, z, x2, y2, z, 2, "blue");
}
}