Blame view

sphere.cpp 8.5 KB
3f56f1f9   dmayerich   initial commit
1
  #include "sphere.h"
51b6469a   dmayerich   added look-up tables
2
  #include "defaults.h"
3f56f1f9   dmayerich   initial commit
3
  
d6f53e68   dmayerich   rts organization
4
  #include "rts/math/complex.h"
3f56f1f9   dmayerich   initial commit
5
6
  #include <complex>
  #include <stdlib.h>
51b6469a   dmayerich   added look-up tables
7
  #include <fstream>
3f56f1f9   dmayerich   initial commit
8
  
3f36b18e   David Mayerich   Adding planewave ...
9
  //using namespace rts;
3f56f1f9   dmayerich   initial commit
10
11
12
13
14
15
16
17
  using namespace std;
  
  int cbessjyva(double v,complex<double> z,double &vm,complex<double>*cjv,

      complex<double>*cyv,complex<double>*cjvp,complex<double>*cyvp);
  
  int cbessjyva_sph(int v,complex<double> z,double &vm,complex<double>*cjv,

      complex<double>*cyv,complex<double>*cjvp,complex<double>*cyvp);
  
51b6469a   dmayerich   added look-up tables
18
19
20
  int bessjyv_sph(int v, double z, double &vm, double* cjv,

      double* cyv, double* cjvp, double* cyvp);
  
3f36b18e   David Mayerich   Adding planewave ...
21
  void sphere::calcCoeff(ptype lambda, bsComplex ri)
3f56f1f9   dmayerich   initial commit
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
  {
      /*  These calculations are done at high-precision on the CPU
          since they are only required once for each wavelength.
  
          Input:
  
          lambda  =   wavelength of the incident field
          n       =   complex refractive index of the sphere
      */
  
      //clear the previous coefficients
      A.clear();
      B.clear();
  
  	//convert to an std complex value
  	complex<double> nc(ri.real(), ri.imag());
  	n = ri;
  
      //compute the magnitude of the k vector
      double k = 2 * PI / lambda;
      complex<double> kna = nc * k * (double)a;
  
      //compute the arguments k*a and k*n*a
      complex<double> ka(k * a, 0.0);
  
      //allocate space for the Bessel functions of the first and second kind (and derivatives)
      int bytes = sizeof(complex<double>) * (Nl + 1);
      complex<double>* cjv_ka = (complex<double>*)malloc(bytes);
      complex<double>* cyv_ka = (complex<double>*)malloc(bytes);
      complex<double>* cjvp_ka = (complex<double>*)malloc(bytes);
      complex<double>* cyvp_ka = (complex<double>*)malloc(bytes);
      complex<double>* cjv_kna = (complex<double>*)malloc(bytes);
      complex<double>* cyv_kna = (complex<double>*)malloc(bytes);
      complex<double>* cjvp_kna = (complex<double>*)malloc(bytes);
      complex<double>* cyvp_kna = (complex<double>*)malloc(bytes);
  
      //allocate space for the spherical Hankel functions and derivative
      complex<double>* chv_ka = (complex<double>*)malloc(bytes);
      complex<double>* chvp_ka = (complex<double>*)malloc(bytes);
  
      //compute the bessel functions using the CPU-based algorithm
      double vm;
      cbessjyva_sph(Nl, ka, vm, cjv_ka, cyv_ka, cjvp_ka, cyvp_ka);
      cbessjyva_sph(Nl, kna, vm, cjv_kna, cyv_kna, cjvp_kna, cyvp_kna);
  
3f56f1f9   dmayerich   initial commit
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
      //compute A for each order
      complex<double> i(0, 1);
      complex<double> a, b, c, d;
      complex<double> An, Bn;
      for(int l=0; l<=Nl; l++)
      {
          //compute the Hankel functions from j and y
          chv_ka[l] = cjv_ka[l] + i * cyv_ka[l];
          chvp_ka[l] = cjvp_ka[l] + i * cyvp_ka[l];
  
          //Compute A (internal scattering coefficient)
          //compute the numerator and denominator for A
          a = cjv_ka[l] * chvp_ka[l] - cjvp_ka[l] * chv_ka[l];
          b = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc;
  
          //calculate A and add it to the list
          An = (2.0 * l + 1.0) * pow(i, l) * (a / b);
          A.push_back(bsComplex(An.real(), An.imag()));
51b6469a   dmayerich   added look-up tables
85
  
3f56f1f9   dmayerich   initial commit
86
87
88
89
90
91
92
93
  
          //Compute B (external scattering coefficient)
          c = cjv_ka[l] * cjvp_kna[l] * nc - cjv_kna[l] * cjvp_ka[l];
          d = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc;
  
          //calculate B and add it to the list
          Bn = (2.0 * l + 1.0) * pow(i, l) * (c / d);
          B.push_back(bsComplex(Bn.real(), Bn.imag()));
3f56f1f9   dmayerich   initial commit
94
  
51b6469a   dmayerich   added look-up tables
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
  
      }
  }
  
  void sphere::calcBesselLut(bsComplex* j, ptype k, bsComplex n, int aR)
  {
      /*Compute the look-up-table for spherical bessel functions used inside of the sphere
          j    =   (Nl + 1) x aR array of values
          aR      =   resolution of j
      */
  
      //allocate space for the Bessel functions of the first and second kind (and derivatives -- which will be ignored)
      int bytes = sizeof(complex<double>) * (Nl + 1);
      complex<double>* cjv_knr = (complex<double>*)malloc(bytes);
      complex<double>* cyv_knr = (complex<double>*)malloc(bytes);
      complex<double>* cjvp_knr = (complex<double>*)malloc(bytes);
      complex<double>* cyvp_knr = (complex<double>*)malloc(bytes);
  
      //compute the bessel functions using the CPU-based algorithm
      double vm;
  
      //for each sample along r
      ptype dr = a / (aR - 1);
      ptype r;
      for(int ir = 0; ir < aR; ir++)
      {
          r = ir * dr;
          complex<double> knr( (k*n*r).real(), (k*n*r).imag() );
          cbessjyva_sph(Nl, knr, vm, cjv_knr, cyv_knr, cjvp_knr, cyvp_knr);
  
          //copy the double data to the bsComplex array
          for(int l=0; l<=Nl; l++)
  		{
  			//deal with the NaN case at the origin
  			if(ir == 0)
  			{
  				if(l == 0)
  					j[ir * (Nl+1)] = 1;
  				else
  					j[ir * (Nl+1) + l] = 0;
  			}
  			else
  				j[ir * (Nl+1) + l] = bsComplex(cjv_knr[l].real(), cjv_knr[l].imag());
  		}
      }
  
  	/*ofstream outfile("besselout.txt");
      for(int ir = 0; ir < aR; ir++)
      {
          for(int l = 0; l<Nl+1; l++)
          {
              outfile<<j[ir * (Nl+1) + l].real()<<"     ";
          }
          outfile<<endl;
      }
  	outfile.close();*/
  
  }
  
  void sphere::calcHankelLut(bsComplex* h, ptype k, int rR)
  {
  	/*Compute the look-up-table for spherical bessel functions used inside of the sphere
          h_out   =   (Nl + 1) x aR array of values
  		rmin	=	minimum value of r
  		d_max	=	maximum value of r
          rR      =   resolution of h_out
      */
  
      //allocate space for the Bessel functions of the first and second kind (and derivatives -- which will be ignored)
      int bytes = sizeof(double) * (Nl + 1);
      double* cjv_kr = (double*)malloc(bytes);
      double* cyv_kr = (double*)malloc(bytes);
      double* cjvp_kr = (double*)malloc(bytes);
      double* cyvp_kr = (double*)malloc(bytes);
  
      //compute the bessel functions using the CPU-based algorithm
      double vm;
  
  
  
      //for each sample along r
      ptype dr = (d_max - max(a, d_min)) / (rR - 1);
      ptype r;
      for(int ir = 0; ir < rR; ir++)
      {
          r = ir * dr + max(a, d_min);
          double kr = k*r;
          bessjyv_sph(Nl, kr, vm, cjv_kr, cyv_kr, cjvp_kr, cyvp_kr);
  
          //copy the double data to the bsComplex array
          for(int l=0; l<=Nl; l++)
  		{
  			//h[ir * (Nl+1) + l] = bsComplex(cjv_kr[l].real(), cyv_kr[l].real());
  			h[ir * (Nl+1) + l] = bsComplex(cjv_kr[l], cyv_kr[l]);
  		}
3f56f1f9   dmayerich   initial commit
190
      }
51b6469a   dmayerich   added look-up tables
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
  
  	/*ofstream outfile("hankelout.txt");
      for(int ir = 0; ir < rR; ir++)
      {
  		outfile<<ir*dr + max(a, d_min)<<"     ";
          for(int l = 0; l<=0; l++)
          {
              outfile<<h[ir * (Nl+1) + l].real()<<"     "<<h[ir * (Nl+1) + l].imag()<<"     ";
          }
          outfile<<endl;
      }
  	outfile.close();*/
  }
  
  void sphere::calcLut(bsComplex* j, bsComplex* h, ptype lambda, bsComplex n, int aR, int rR)
  {
      /*Compute the look-up-tables for spherical bessel functions used both inside and outside of the sphere.
          j       =   (Nl + 1) x aR array of values
          j       =   (Nl + 1) x rR array of values
          d_max    =   maximum distance for the LUT
          aR      =   resolution of j_in
          rR      =   resolution of j_out
      */
  
      //compute the magnitude of the k vector
      double k = 2 * PI / lambda;
  
  	calcBesselLut(j, k, n, aR);
  	calcHankelLut(h, k, rR);
  }
  
3f36b18e   David Mayerich   Adding planewave ...
222
  void sphere::calcUp(ptype lambda, bsComplex n, rts::quad<ptype, 3> nfPlane, unsigned int R)
51b6469a   dmayerich   added look-up tables
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
  {
      //calculate the parameters of the lookup table
  
      //first find the distance to the closest and furthest points on the nearfield plane
      d_min = nfPlane.dist(p);
      d_max = nfPlane.dist_max(p);
  
      //compute the radius of the cross-section of the sphere with the plane
      ptype a_inter = 0;
      if(d_min < a)
          a_inter = sqrt(a - d_min);
  
  
  	//calculate the resolution of the Usp and Uip lookup tables
  	int aR = 1 + 2 * R * a_inter / (nfPlane(0, 0) - nfPlane(1, 1)).len();
  	int dR = 2 * R;
  	int thetaR = DEFAULT_SPHERE_THETA_R;
  
  	//allocate space for the bessel function LUTs
  	bsComplex* j = (bsComplex*)malloc(sizeof(bsComplex) * (Nl + 1) * aR);
  	bsComplex* h = (bsComplex*)malloc(sizeof(bsComplex) * (Nl + 1) * dR);
  
  	calcLut(j, h, lambda, n, aR, dR);
  
  	//allocate space for the Usp lookup texture
  	Usp.R[0] = dR;
  	Usp.R[1] = thetaR;
  	Usp.init_gpu();
  
  	//allocate space for the Uip lookup texture
  	Uip.R[0] = aR;
  	Uip.R[1] = thetaR;
  	Uip.init_gpu();
  
  
  
  	scalarUsp(h, dR, thetaR);
  	scalarUip(j, aR, thetaR);
  
  	scalarslice UspMag = Usp.Mag();
  	UspMag.toImage("Usp.bmp", true);
  
  	scalarslice UipMag = Uip.Mag();
  	UipMag.toImage("Uip.bmp", true);
  
  	//free memory
  	free(j);
  	free(h);
  
  }
  
  sphere& sphere::operator=(const sphere &rhs)
  {
  	p = rhs.p;
  	a = rhs.a;
  	iMaterial = rhs.iMaterial;
  	Nl = rhs.Nl;
  	n = rhs.n;
  	B = rhs.B;
  	A = rhs.A;
  
  	return *this;
  }
  
  sphere::sphere(const sphere &rhs)
  {
  	p = rhs.p;
  	a = rhs.a;
  	iMaterial = rhs.iMaterial;
  	Nl = rhs.Nl;
  	n = rhs.n;
  	B = rhs.B;
  	A = rhs.A;
3f56f1f9   dmayerich   initial commit
296
  }