beam.h
4.22 KB
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
#ifndef RTS_BEAM
#define RTS_BEAM
#include "../math/vector.h"
#include "../math/function.h"
#include <vector>
namespace rts{
template<typename P>
class beam
{
public:
enum beam_type {Uniform, Bartlett, Hamming, Hanning};
private:
P na[2]; //numerical aperature of the focusing optics
vec<P> f; //focal point
vec<P> k; //direction vector
vec<P> E0; //polarization direction
P omega; //frequency
function<P, P> apod; //apodization function
unsigned int apod_res; //resolution of complex apodization filters
void apod_uniform()
{
apod = (P)1;
}
void apod_bartlett()
{
apod = (P)1;
apod.insert((P)1, (P)0);
}
void apod_hanning()
{
apod = (P)0;
P x, y;
for(unsigned int n=0; n<apod_res; n++)
{
x = (P)n/(P)apod_res;
y = pow( cos( ((P)3.14159 * x) / 2 ), 2);
apod.insert(x, y);
}
}
void apod_hamming()
{
apod = (P)0;
P x, y;
for(unsigned int n=0; n<apod_res; n++)
{
x = (P)n/(P)apod_res;
y = (P)27/(P)50 + ( (P)23/(P)50 ) * cos((P)3.14159 * x);
apod.insert(x, y);
}
}
void set_apod(beam_type type)
{
if(type == Uniform)
apod_uniform();
if(type == Bartlett)
apod_bartlett();
if(type == Hanning)
apod_hanning();
if(type == Hamming)
apod_hamming();
}
public:
///constructor: build a default beam (NA=1.0)
beam(beam_type _apod = Uniform)
{
na[0] = (P)0.0;
na[1] = (P)1.0;
f = vec<P>( (P)0.0, (P)0.0, (P)0.0 );
k = vec<P>( (P)0.0, (P)0.0, (P)1.0 );
E0 = vec<P>( (P)1.0, (P)0.0, (P)0.0 );
omega = (P)2 * (P)3.14159;
apod_res = 256; //set the default resolution for apodization filters
set_apod(_apod); //set the apodization function type
}
///Numerical Aperature functions
void NA(P _na)
{
na[0] = (P)0;
na[1] = _na;
}
void NA(P _na0, P _na1)
{
na[0] = _na0;
na[1] = _na1;
}
//Monte-Carlo decomposition into plane waves
std::vector< planewave<P> > mc(unsigned int N, unsigned int seed = 0)
{
/*Create Monte-Carlo samples of a cassegrain objective by performing uniform sampling
of a sphere and projecting these samples onto an inscribed sphere.
samples = rtsPointer to sample vectors specified as normalized cartesian coordinates
N = number of samples
kSph = incident light direction in spherical coordinates
NAin = internal obscuration NA
NAout = outer cassegrain NA
*/
srand(seed); //seed the random number generator
///compute the rotation operator to transform (0, 0, 1) to k
P cos_angle = k.dot(rts::vec<P>(0, 0, 1));
rts::matrix<P, 3> rotation;
if(cos_angle != 1.0)
{
rts::vec<P> r_axis = rts::vec<P>(0, 0, 1).cross(k).norm(); //compute the axis of rotation
P angle = acos(cos_angle); //compute the angle of rotation
rts::quaternion<P> quat; //create a quaternion describing the rotation
quat.CreateRotation(angle, r_axis);
rotation = quat.toMatrix3(); //compute the rotation matrix
}
//find the phi values associated with the cassegrain ring
P PHI[2];
PHI[0] = (P)asin(na[0]);
PHI[1] = (P)asin(na[1]);
//calculate the z-axis cylinder coordinates associated with these angles
P Z[2];
Z[0] = cos(PHI[0]);
Z[1] = cos(PHI[1]);
P range = Z[0] - Z[1];
std::vector< planewave<P> > samples; //create a vector of plane waves
planewave<P> beam_center(k, E0, omega); //create a plane wave representing the beam center
//draw a distribution of random phi, z values
P z, phi, theta;
for(int i=0; i<N; i++) //for each sample
{
z = ((P)rand() / (P)RAND_MAX) * range + Z[1]; //find a random position on the surface of a cylinder
theta = ((P)rand() / (P)RAND_MAX) * 2 * (P)3.14159;
phi = acos(z); //project onto the sphere, computing phi in spherical coordinates
//compute and store cartesian coordinates
rts::vec<P> spherical(1, theta, phi); //convert from spherical to cartesian coordinates
rts::vec<P> cart = spherical.sph2cart();
vec<P> k_prime = rotation * cart; //create a sample vector
//store a wave refracted along the given direction
samples.push_back(beam_center.refract(k_prime) * apod(phi/PHI[1]));
}
return samples;
}
};
}
#endif