Blame view

stim/math/spharmonics.h 4.93 KB
487a9b49   David Mayerich   added the ability...
1
2
3
  #ifndef STIM_SPH_HARMONICS

  #define STIM_SPH_HARMONICS

  

39d843e3   Pavel Govyadinov   fixed spharmonics...
4
  #include <complex>

cce7daf9   Pavel Govyadinov   added glObj.h to ...
5
  #include <stim/math/vector.h>

487a9b49   David Mayerich   added the ability...
6
  #include <boost/math/special_functions/spherical_harmonic.hpp>

39d843e3   Pavel Govyadinov   fixed spharmonics...
7
  #include <stim/math/constants.h>

487a9b49   David Mayerich   added the ability...
8
9
  #include <vector>

  

487a9b49   David Mayerich   added the ability...
10
11
12
13
14
15
  #define WIRE_SCALE 1.001

  namespace stim{

  

  template<class T>

  class spharmonics{

  

39d843e3   Pavel Govyadinov   fixed spharmonics...
16
17
18
  public:

  	std::vector<T> C;	//list of SH coefficients

  

487a9b49   David Mayerich   added the ability...
19
20
  protected:

  

487a9b49   David Mayerich   added the ability...
21
22
23
24
25
26
  

  	unsigned int mcN;	//number of Monte-Carlo samples

  

  	//calculate the value of the SH basis function (l, m) at (theta, phi)

  		//here, theta = [0, PI], phi = [0, 2*PI]

  	double SH(int l, int m, double theta, double phi){

39d843e3   Pavel Govyadinov   fixed spharmonics...
27
28
29
  		std::complex<T> result = boost::math::spherical_harmonic(l, m, phi, theta);

  		return result.imag() + result.real();

  //		return boost::math::spherical_harmonic_i(l, m, phi, theta);

487a9b49   David Mayerich   added the ability...
30
31
32
33
34
35
36
37
38
39
  	}

  

  	unsigned int coeff_1d(unsigned int l, int m){

  		return pow(l + 1, 2) - (l - m) - 1;

  	}

  

  	

  

  

  public:

761ebaa9   David Mayerich   spherical harmoni...
40
41
42
43
44
45
  	spharmonics() {

  		mcN = 0;

  	}

  	spharmonics(size_t c) : spharmonics() {

  		resize(c);

  	}

487a9b49   David Mayerich   added the ability...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  

  	void push(double c){

  		C.push_back(c);

  	}

  

  	void resize(unsigned int n){

  		C.resize(n);

  	}

  

  	void setc(unsigned int l, int m, T value){

  		unsigned int c = coeff_1d(l, m);

  		C[c] = value;

  	}

  

  	void setc(unsigned int c, T value){

  		C[c] = value;

  	}

  

39d843e3   Pavel Govyadinov   fixed spharmonics...
64
65
66
67
68
69
70
71
72
73
  	unsigned int getSize() const{

  		return C.size();

  	}

  

  	std::vector<T> getC() const{

  		return C;

  	}

  

  	

  

487a9b49   David Mayerich   added the ability...
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
  	/// Initialize Monte-Carlo sampling of a function using N spherical harmonics coefficients

  

  	/// @param N is the number of spherical harmonics coefficients used to represent the user function

  	void mcBegin(unsigned int coefficients){

  		C.resize(coefficients, 0);

  		mcN = 0;

  	}

  

  	void mcBegin(unsigned int l, int m){

  		unsigned int c = pow(l + 1, 2) - (l - m);

  		mcBegin(c);

  	}

  

  	void mcSample(double theta, double phi, double val){

  

  		int l, m;

  		double sh;

  

  		l = m = 0;

  		for(unsigned int i = 0; i < C.size(); i++){

  

  			sh = SH(l, m, theta, phi);

  			C[i] += sh * val;

  

  			m++;			//increment m

  

  			//if we're in a new tier, increment l and set m = -l

  			if(m > l){		

  				l++;

  				m = -l;

  			}

  		}	//end for all coefficients

  

  		//increment the number of samples

  		mcN++;

  

  	}	//end mcSample()

  

  	void mcEnd(){

  

  		//divide all coefficients by the number of samples

  		for(unsigned int i = 0; i < C.size(); i++)

  			C[i] /= mcN;

  	}

  

  	/// Generates a PDF describing the probability distribution of points on a spherical surface

  

  	/// @param sph_pts is a list of points in spherical coordinates (theta, phi) where theta = [0, 2pi] and phi = [0, pi]

  	/// @param l is the maximum degree of the spherical harmonic function

  	/// @param m is the maximum order

321ff17a   David Mayerich   Optimized the mat...
124
  	void pdf(std::vector<stim::vec<double> > sph_pts, unsigned int l, int m){

487a9b49   David Mayerich   added the ability...
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
  			

  		mcBegin( l, m );		//begin spherical harmonic sampling

  

  		unsigned int nP = sph_pts.size();

  

  		for(unsigned int p = 0; p < nP; p++){

  			mcSample(sph_pts[p][1], sph_pts[p][2], 1.0);

  		}

  

  		mcEnd();

  	}

  

  	std::string str(){

  

  		std::stringstream ss;

  

  		int l, m;

  		l = m = 0;

  		for(unsigned int i = 0; i < C.size(); i++){

  				

  			ss<<C[i]<<'\t';

  

  			m++;			//increment m

  

  			//if we're in a new tier, increment l and set m = -l

  			if(m > l){

  				l++;

  				m = -l;

  

  				ss<<std::endl;

  					

  			}

  		}

  

  		return ss.str();

  

  

  	}

  

  	/// Returns the value of the function at the coordinate (theta, phi)

  

  	/// @param theta = [0, 2pi]

  	/// @param phi = [0, pi]

  	double operator()(double theta, double phi){

  

  		double fx = 0;

  

  		int l = 0;

  		int m = 0;

  		for(unsigned int i = 0; i < C.size(); i++){

  			fx += C[i] * SH(l, m, theta, phi);

  			m++;

  			if(m > l){

  				l++;

  				m = -l;					

  			}

  

  		}

  

  		return fx;

  	}

39d843e3   Pavel Govyadinov   fixed spharmonics...
186
  /*

761ebaa9   David Mayerich   spherical harmoni...
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
  	//overload arithmetic operations

  	spharmonics<T> operator*(T rhs) const {

  		spharmonics<T> result(C.size());				//create a new spherical harmonics object

  		for (size_t c = 0; c < C.size(); c++)			//for each coefficient

  			result.C[c] = C[c] * rhs;					//calculate the factor and store the result in the new spharmonics object

  		return result;

  	}

  

  	spharmonics<T> operator+(spharmonics<T> rhs) {

  		size_t low = std::min(C.size(), rhs.C.size());				//store the number of coefficients in the lowest object

  		size_t high = std::max(C.size(), rhs.C.size());				//store the number of coefficients in the result

  		bool rhs_lowest = false;								//true if rhs has the lowest number of coefficients

  		if (rhs.C.size() < C.size()) rhs_lowest = true;			//if rhs has a lower number of coefficients, set the flag

  

  		spharmonics<T> result(high);								//create a new object

  		size_t c;

  		for (c = 0; c < low; c++)						//perform the first batch of additions

  			result.C[c] = C[c] + rhs.C[c];						//perform the addition

  

  		for (c = low; c < high; c++) {

  			if (rhs_lowest)

  				result.C[c] = C[c];

  			else

  				result.C[c] = rhs.C[c];

  		}

  		return result;

  	}

  

  	spharmonics<T> operator-(spharmonics<T> rhs) {

  		return (*this) + (rhs * (T)(-1));

  	}

39d843e3   Pavel Govyadinov   fixed spharmonics...
218
  */

487a9b49   David Mayerich   added the ability...
219
220
221
222
223
224
225
226
227
  };		//end class sph_harmonics

  

  

  

  

  }

  

  

  #endif