Commit 321ff17a4937ffb44195bfd15444c3956ba80f4b

Authored by David Mayerich
1 parent 5eeaf941

Optimized the mathvec.h class to remove excessive push operations

stim/math/mathvec.h
... ... @@ -23,37 +23,34 @@ struct vec : public std::vector<T>
23 23  
24 24 }
25 25  
26   -// //efficiency constructors, makes construction easier for 1D-4D vectors
27   - vec(T rhs)
  26 + /// Create a vector with a set dimension d
  27 + vec(int d)
28 28 {
29   - resize(1, rhs);
30   -// cerr << " Created a vector " << endl;
31   -// //for(int i=0; i<N; i++)
32   -// // v[i] = rhs;
  29 + resize(d,0);
33 30 }
34 31  
35   -// vec(int s)
36   -// {
37   -// resize(s,0);
38   -// }
39 32  
  33 +// //efficiency constructors, makes construction easier for 1D-4D vectors
40 34 vec(T x, T y)
41 35 {
42   - push_back(x);
43   - push_back(y);
  36 + resize(2, 0);
  37 + at(0) = x;
  38 + at(1) = y;
44 39 }
45 40 vec(T x, T y, T z)
46 41 {
47   - push_back(x);
48   - push_back(y);
49   - push_back(z);
  42 + resize(3, 0);
  43 + at(0) = x;
  44 + at(1) = y;
  45 + at(2) = z;
50 46 }
51 47 vec(T x, T y, T z, T w)
52 48 {
53   - push_back(x);
54   - push_back(y);
55   - push_back(z);
56   - push_back(w);
  49 + resize(4, 0);
  50 + at(0) = x;
  51 + at(1) = y;
  52 + at(2) = z;
  53 + at(3) = w;
57 54 }
58 55  
59 56  
... ... @@ -65,6 +62,8 @@ struct vec : public std::vector&lt;T&gt;
65 62 push_back(other[i]);
66 63 }
67 64  
  65 + //I'm not sure what these were doing here.
  66 + //Keep them now, we'll worry about it later.
68 67 vec<T> push(T x)
69 68 {
70 69 push_back(x);
... ... @@ -92,6 +91,8 @@ struct vec : public std::vector&lt;T&gt;
92 91 push_back(w);
93 92 return *this;
94 93 }
  94 +
  95 + /// Casting operator. Creates a new vector with a new type U.
95 96 template< typename U >
96 97 operator vec<U>(){
97 98 unsigned int N = size();
... ... @@ -102,9 +103,8 @@ struct vec : public std::vector&lt;T&gt;
102 103 return result;
103 104 }
104 105  
105   - //template<class U>
106   - //friend vec<U, N>::operator vec<T, N>();
107 106  
  107 + /// computes the Euclidean length of the vector
108 108 T len() const
109 109 {
110 110 unsigned int N = size();
... ... @@ -119,10 +119,10 @@ struct vec : public std::vector&lt;T&gt;
119 119  
120 120 }
121 121  
  122 + /// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi])
122 123 vec<T> cart2sph() const
123 124 {
124   - //convert the vector from cartesian to spherical coordinates
125   - //x, y, z -> r, theta, phi (where theta = 0 to 2*pi)
  125 +
126 126  
127 127 vec<T> sph;
128 128 sph.push_back(std::sqrt(at(0)*at(0) + at(1)*at(1) + at(2)*at(2)));
... ... @@ -136,11 +136,9 @@ struct vec : public std::vector&lt;T&gt;
136 136 return sph;
137 137 }
138 138  
  139 + /// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi])
139 140 vec<T> sph2cart() const
140 141 {
141   - //convert the vector from cartesian to spherical coordinates
142   - //r, theta, phi -> x, y, z (where theta = 0 to 2*pi)
143   -
144 142 vec<T> cart;
145 143 cart.push_back(at(0) * std::cos(at(1)) * std::sin(at(2)));
146 144 cart.push_back(at(0) * std::sin(at(1)) * std::sin(at(2)));
... ... @@ -149,6 +147,7 @@ struct vec : public std::vector&lt;T&gt;
149 147 return cart;
150 148 }
151 149  
  150 + /// Computes the normalized vector (where each coordinate is divided by the L2 norm)
152 151 vec<T> norm() const
153 152 {
154 153 unsigned int N = size();
... ... @@ -168,22 +167,27 @@ struct vec : public std::vector&lt;T&gt;
168 167 return result;
169 168 }
170 169  
  170 + /// Computes the cross product of a 3-dimensional vector
171 171 vec<T> cross(const vec<T> rhs) const
172 172 {
173   - vec<T> result;
  173 + if(size() != 3)
  174 + std::cout<<"Error in stim::mathvec::cross() - the vector must have 3 components"<<std::endl;
  175 +
  176 + vec<T> result(3);
174 177  
175 178 //compute the cross product (only valid for 3D vectors)
176   - result.push_back(at(1) * rhs[2] - at(2) * rhs[1]);
177   - result.push_back(at(2) * rhs[0] - at(0) * rhs[2]);
178   - result.push_back(at(0) * rhs[1] - at(1) * rhs[0]);
  179 + result[0] = (at(1) * rhs[2] - at(2) * rhs[1]);
  180 + result[1] = (at(2) * rhs[0] - at(0) * rhs[2]);
  181 + result[2] = (at(0) * rhs[1] - at(1) * rhs[0]);
179 182  
180 183 return result;
181 184 }
182 185  
  186 + /// Compute the Euclidean inner (dot) product
183 187 T dot(vec<T> rhs) const
184 188 {
185 189 T result = (T)0;
186   - unsigned int N = size();
  190 + unsigned int N = size();
187 191 for(int i=0; i<N; i++)
188 192 result += at(i) * rhs[i];
189 193  
... ... @@ -191,56 +195,80 @@ struct vec : public std::vector&lt;T&gt;
191 195  
192 196 }
193 197  
194   - //arithmetic
  198 + /// Arithmetic addition operator
  199 +
  200 + /// @param rhs is the right-hand-side operator for the addition
195 201 vec<T> operator+(vec<T> rhs) const
196 202 {
197   - vec<T> result;
198   -
199 203 unsigned int N = size();
  204 + vec<T> result(N);
  205 +
200 206 for(int i=0; i<N; i++)
201   - result.push_back(at(i) + rhs[i]);
  207 + result[i] = at(i) + rhs[i];
202 208  
203 209 return result;
204 210 }
  211 +
  212 + /// Arithmetic addition to a scalar
  213 +
  214 + /// @param rhs is the right-hand-side operator for the addition
205 215 vec<T> operator+(T rhs) const
206 216 {
207   - vec<T> result;
208 217 unsigned int N = size();
  218 +
  219 + vec<T> result(N);
209 220 for(int i=0; i<N; i++)
210   - result.push_back(at(i) + rhs);
  221 + result[i] = at(i) + rhs;
211 222  
212 223 return result;
213 224 }
  225 +
  226 + /// Arithmetic subtraction operator
  227 +
  228 + /// @param rhs is the right-hand-side operator for the subtraction
214 229 vec<T> operator-(vec<T> rhs) const
215 230 {
216   - vec<T> result;
217   -
218 231 unsigned int N = size();
  232 +
  233 + vec<T> result(N);
  234 +
219 235 for(int i=0; i<N; i++)
220   - result.push_back(at(i) - rhs[i]);
  236 + result[i] = at(i) - rhs[i];
221 237  
222 238 return result;
223 239 }
  240 +
  241 + /// Arithmetic scalar multiplication operator
  242 +
  243 + /// @param rhs is the right-hand-side operator for the subtraction
224 244 vec<T> operator*(T rhs) const
225 245 {
226   - vec<T> result;
227   -
228 246 unsigned int N = size();
  247 +
  248 + vec<T> result(N);
  249 +
229 250 for(int i=0; i<N; i++)
230   - result.push_back(at(i) * rhs);
  251 + result[i] = at(i) * rhs;
231 252  
232 253 return result;
233 254 }
  255 +
  256 + /// Arithmetic scalar division operator
  257 +
  258 + /// @param rhs is the right-hand-side operator for the subtraction
234 259 vec<T> operator/(T rhs) const
235 260 {
236   - vec<T> result;
237   -
238 261 unsigned int N = size();
  262 +
  263 + vec<T> result(N);
  264 +
239 265 for(int i=0; i<N; i++)
240   - result.push_back(at(i) / rhs);
  266 + result[i] = at(i) / rhs;
241 267  
242 268 return result;
243 269 }
  270 +
  271 + /// Multiplication by a scalar, followed by assignment
244 272 vec<T> operator*=(T rhs){
245 273  
246 274 unsigned int N = size();
... ... @@ -248,12 +276,16 @@ struct vec : public std::vector&lt;T&gt;
248 276 at(i) = at(i) * rhs;
249 277 return *this;
250 278 }
  279 +
  280 + /// Addition and assignment
251 281 vec<T> operator+=(vec<T> rhs){
252 282 unsigned int N = size();
253 283 for(int i=0; i<N; i++)
254 284 at(i) += rhs[i];
255 285 return *this;
256 286 }
  287 +
  288 + /// Assign a scalar to all values
257 289 vec<T> & operator=(T rhs){
258 290  
259 291 unsigned int N = size();
... ... @@ -262,6 +294,7 @@ struct vec : public std::vector&lt;T&gt;
262 294 return *this;
263 295 }
264 296  
  297 + /// Casting and assignment
265 298 template<typename Y>
266 299 vec<T> & operator=(vec<Y> rhs){
267 300 unsigned int N = rhs.size();
... ... @@ -271,19 +304,23 @@ struct vec : public std::vector&lt;T&gt;
271 304 at(i) = rhs[i];
272 305 return *this;
273 306 }
274   - //unary minus
  307 +
  308 + /// Unary minus (returns the negative of the vector)
275 309 vec<T> operator-() const{
276   - vec<T> r;
277 310  
278   - //negate the vector
279 311 unsigned int N = size();
  312 +
  313 + vec<T> r(N);
  314 +
  315 + //negate the vector
280 316 for(int i=0; i<N; i++)
281   - r.push_back(-at(i));
  317 + r[i] = -at(i);
282 318  
283 319 return r;
284 320 }
285 321  
286 322  
  323 + /// Outputs the vector as a string
287 324 std::string str() const
288 325 {
289 326 std::stringstream ss;
... ... @@ -315,7 +352,7 @@ std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, stim::vec&lt;T&gt; v)
315 352 }
316 353  
317 354  
318   -
  355 +/// Multiply a vector by a constant when the vector is on the right hand side
319 356 template <typename T>
320 357 stim::vec<T> operator*(T lhs, stim::vec<T> rhs)
321 358 {
... ... @@ -324,8 +361,4 @@ stim::vec&lt;T&gt; operator*(T lhs, stim::vec&lt;T&gt; rhs)
324 361 return rhs * lhs;
325 362 }
326 363  
327   -//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7
328   -//template<class T, int N> using rtsVector = rts::vector<T, N>;
329   -//#endif
330   -
331 364 #endif
... ...
stim/math/spharmonics.h
... ... @@ -100,7 +100,7 @@ public:
100 100 /// @param sph_pts is a list of points in spherical coordinates (theta, phi) where theta = [0, 2pi] and phi = [0, pi]
101 101 /// @param l is the maximum degree of the spherical harmonic function
102 102 /// @param m is the maximum order
103   - void pdf(std::vector<stim::vec<double>> sph_pts, unsigned int l, int m){
  103 + void pdf(std::vector<stim::vec<double> > sph_pts, unsigned int l, int m){
104 104  
105 105 mcBegin( l, m ); //begin spherical harmonic sampling
106 106  
... ...
stim/visualization/gl_spharmonics.h
... ... @@ -14,6 +14,8 @@ template &lt;typename T&gt;
14 14 class gl_spharmonics : public spharmonics<T>{
15 15  
16 16 protected:
  17 + using spharmonics<T>::C;
  18 + using spharmonics<T>::SH;
17 19  
18 20 T* func; //stores the raw function data (samples at each point)
19 21  
... ... @@ -184,4 +186,4 @@ public:
184 186  
185 187  
186 188  
187   -#endif
188 189 \ No newline at end of file
  190 +#endif
... ...