Blame view

stim/math/vec3.h 8.1 KB
b28f312d   David Mayerich   added code for ev...
1
2
  #ifndef TIRA_VEC3_H
  #define TIRA_VEC3_H
8e4f8364   David Mayerich   started a new opt...
3
4
5
  
  
  #include <stim/cuda/cudatools/callable.h>
14634fca   David Mayerich   edits to update s...
6
  #include <complex>
2e0f052a   Pavel Govyadinov   minor changes to ...
7
  #include <cmath>
8e4f8364   David Mayerich   started a new opt...
8
  
a2811d32   David Mayerich   updated vec3 str(...
9
  #include <sstream>
a2811d32   David Mayerich   updated vec3 str(...
10
  
8e4f8364   David Mayerich   started a new opt...
11
  
b28f312d   David Mayerich   added code for ev...
12
  namespace tira{
8e4f8364   David Mayerich   started a new opt...
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
  
  
  /// A class designed to act as a 3D vector with CUDA compatibility
  template<typename T>
  class vec3{
  
  protected:
  	T ptr[3];
  
  public:
  
  	CUDA_CALLABLE vec3(){}
  
  	CUDA_CALLABLE vec3(T v){
  		ptr[0] = ptr[1] = ptr[2] = v;
  	}
  
  	CUDA_CALLABLE vec3(T x, T y, T z){
  		ptr[0] = x;
  		ptr[1] = y;
  		ptr[2] = z;
  	}
  
  	//copy constructor
  	CUDA_CALLABLE vec3( const vec3<T>& other){
  		ptr[0] = other.ptr[0];
  		ptr[1] = other.ptr[1];
  		ptr[2] = other.ptr[2];
  	}
  
  	//access an element using an index
7d3162a2   Pavel Govyadinov   fixed majority of...
44
  	CUDA_CALLABLE T& operator[](size_t idx){
8e4f8364   David Mayerich   started a new opt...
45
46
  		return ptr[idx];
  	}
14634fca   David Mayerich   edits to update s...
47
48
49
50
  	//read only accessor method
  	CUDA_CALLABLE T get(size_t idx) const {
  		return ptr[idx];
  	}
8e4f8364   David Mayerich   started a new opt...
51
  
7d3162a2   Pavel Govyadinov   fixed majority of...
52
53
54
55
  	CUDA_CALLABLE T* data(){
  		return ptr;
  	}
  
8e4f8364   David Mayerich   started a new opt...
56
  /// Casting operator. Creates a new vector with a new type U.
14634fca   David Mayerich   edits to update s...
57
  /*	template< typename U >
8e4f8364   David Mayerich   started a new opt...
58
  	CUDA_CALLABLE operator vec3<U>(){
14634fca   David Mayerich   edits to update s...
59
60
61
62
  		vec3<U> result((U)ptr[0], (U)ptr[1], (U)ptr[2]);
  		//result.ptr[0] = (U)ptr[0];
  		//result.ptr[1] = (U)ptr[1];
  		//result.ptr[2] = (U)ptr[2];
8e4f8364   David Mayerich   started a new opt...
63
64
  
  		return result;
14634fca   David Mayerich   edits to update s...
65
  	}*/
8e4f8364   David Mayerich   started a new opt...
66
67
68
69
70
71
72
73
74
75
  
  	// computes the squared Euclidean length (useful for several operations where only >, =, or < matter)
  	CUDA_CALLABLE T len_sq() const{
  		return ptr[0] * ptr[0] + ptr[1] * ptr[1] + ptr[2] * ptr[2];
  	}
  
  	/// computes the Euclidean length of the vector
  	CUDA_CALLABLE T len() const{
  		return sqrt(len_sq());
  	}
14634fca   David Mayerich   edits to update s...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  
  	/// Calculate the L2 norm of the vector, accounting for the possibility that it is complex
  	CUDA_CALLABLE T norm2() const{
  		T conj_dot = std::real(ptr[0] * std::conj(ptr[0]) + ptr[1] * std::conj(ptr[1]) + ptr[2] * std::conj(ptr[2]));
  		return sqrt(conj_dot);
  	}
  
  	/// Calculate the normalized direction vector
  	CUDA_CALLABLE vec3<T> direction() const {
  		vec3<T> result;
  		T length = norm2();
  		result[0] = ptr[0] / length;
  		result[1] = ptr[1] / length;
  		result[2] = ptr[2] / length;
  		return result;
  	}
8e4f8364   David Mayerich   started a new opt...
92
  
8d55ae06   Pavel Govyadinov   Edited vec3 to ad...
93
  	/// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [-PI, PI])
8e4f8364   David Mayerich   started a new opt...
94
95
96
97
98
99
100
101
102
103
104
  	CUDA_CALLABLE vec3<T> cart2sph() const{
  		vec3<T> sph;
  		sph.ptr[0] = len();
  		sph.ptr[1] = std::atan2(ptr[1], ptr[0]);
  		if(sph.ptr[0] == 0)
  			sph.ptr[2] = 0;
  		else
  			sph.ptr[2] = std::acos(ptr[2] / sph.ptr[0]);
  		return sph;
  	}
  
fac43319   Pavel Govyadinov   Changed the posit...
105
106
107
108
109
110
111
112
113
  	CUDA_CALLABLE vec3<T> cart2cyl() const{
  		vec3<T> cyl;
  		cyl.ptr[0] = sqrt(pow(ptr[0],2) + pow(ptr[1],2));
  		cyl.ptr[1] = atan(ptr[1]/ptr[0]);
  		cyl.ptr[2] = ptr[2];
  
  		return cyl;
  	}
  
8e4f8364   David Mayerich   started a new opt...
114
115
116
117
118
119
120
121
122
123
  	/// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi])
  	CUDA_CALLABLE vec3<T> sph2cart() const{
  		vec3<T> cart;
  		cart.ptr[0] = ptr[0] * std::cos(ptr[1]) * std::sin(ptr[2]);
  		cart.ptr[1] = ptr[0] * std::sin(ptr[1]) * std::sin(ptr[2]);
  		cart.ptr[2] = ptr[0] * std::cos(ptr[2]);
  
  		return cart;
  	}
  
fac43319   Pavel Govyadinov   Changed the posit...
124
125
126
127
128
129
130
131
132
133
  	/// Convert the vector from cylindrical to cart coordinates (r, theta, z -> x, y, z where theta = [0, 2*pi])
  	CUDA_CALLABLE vec3<T> cyl2cart() const{
  		vec3<T> cart;
  		cart.ptr[0] = ptr[0] * std::cos(ptr[1]);
  		cart.ptr[1] = ptr[0] * std::sin(ptr[1]);
  		cart.ptr[2] = ptr[2];
  
  		return cart;
  	}
  
8e4f8364   David Mayerich   started a new opt...
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
  	/// Computes the normalized vector (where each coordinate is divided by the L2 norm)
  	CUDA_CALLABLE vec3<T> norm() const{
          vec3<T> result;
          T l = len();						//compute the vector length
          return (*this) / l;
  	}
  
  	/// Computes the cross product of a 3-dimensional vector
  	CUDA_CALLABLE vec3<T> cross(const vec3<T> rhs) const{
  
  		vec3<T> result;
  
  		result[0] = (ptr[1] * rhs.ptr[2] - ptr[2] * rhs.ptr[1]);
  		result[1] = (ptr[2] * rhs.ptr[0] - ptr[0] * rhs.ptr[2]);
  		result[2] = (ptr[0] * rhs.ptr[1] - ptr[1] * rhs.ptr[0]);
  
  		return result;
  	}
  
  	/// Compute the Euclidean inner (dot) product
      CUDA_CALLABLE T dot(vec3<T> rhs) const{
          return ptr[0] * rhs.ptr[0] + ptr[1] * rhs.ptr[1] + ptr[2] * rhs.ptr[2];
      }
  
  	/// Arithmetic addition operator
  
      /// @param rhs is the right-hand-side operator for the addition
  	CUDA_CALLABLE vec3<T> operator+(vec3<T> rhs) const{
  		vec3<T> result;
  		result.ptr[0] = ptr[0] + rhs[0];
  		result.ptr[1] = ptr[1] + rhs[1];
  		result.ptr[2] = ptr[2] + rhs[2];
  		return result;
  	}
  
  	/// Arithmetic addition to a scalar
  
  	/// @param rhs is the right-hand-side operator for the addition
  	CUDA_CALLABLE vec3<T> operator+(T rhs) const{
  		vec3<T> result;
  		result.ptr[0] = ptr[0] + rhs;
  		result.ptr[1] = ptr[1] + rhs;
  		result.ptr[2] = ptr[2] + rhs;
  		return result;
  	}
  
  	/// Arithmetic subtraction operator
  
  	/// @param rhs is the right-hand-side operator for the subtraction
  	CUDA_CALLABLE vec3<T> operator-(vec3<T> rhs) const{
  		vec3<T> result;
  		result.ptr[0] = ptr[0] - rhs[0];
  		result.ptr[1] = ptr[1] - rhs[1];
  		result.ptr[2] = ptr[2] - rhs[2];
  		return result;
  	}
  	/// Arithmetic subtraction to a scalar
  
  	/// @param rhs is the right-hand-side operator for the addition
  	CUDA_CALLABLE vec3<T> operator-(T rhs) const{
  		vec3<T> result;
  		result.ptr[0] = ptr[0] - rhs;
  		result.ptr[1] = ptr[1] - rhs;
  		result.ptr[2] = ptr[2] - rhs;
  		return result;
  	}
  
  	/// Arithmetic scalar multiplication operator
  
  	/// @param rhs is the right-hand-side operator for the subtraction
  	CUDA_CALLABLE vec3<T> operator*(T rhs) const{
  		vec3<T> result;
  		result.ptr[0] = ptr[0] * rhs;
  		result.ptr[1] = ptr[1] * rhs;
  		result.ptr[2] = ptr[2] * rhs;
  		return result;
  	}
  
  	/// Arithmetic scalar division operator
  
  	/// @param rhs is the right-hand-side operator for the subtraction
  	CUDA_CALLABLE vec3<T> operator/(T rhs) const{
  		return (*this) * ((T)1.0/rhs);
  	}
  
  	/// Multiplication by a scalar, followed by assignment
  	CUDA_CALLABLE vec3<T> operator*=(T rhs){
  		ptr[0] = ptr[0] * rhs;
  		ptr[1] = ptr[1] * rhs;
  		ptr[2] = ptr[2] * rhs;
  		return *this;
  	}
  
  	/// Addition and assignment
  	CUDA_CALLABLE vec3<T> operator+=(vec3<T> rhs){
  		ptr[0] = ptr[0] + rhs;
  		ptr[1] = ptr[1] + rhs;
  		ptr[2] = ptr[2] + rhs;
  		return *this;
  	}
  
  	/// Assign a scalar to all values
  	CUDA_CALLABLE vec3<T> & operator=(T rhs){
  		ptr[0] = ptr[0] = rhs;
  		ptr[1] = ptr[1] = rhs;
  		ptr[2] = ptr[2] = rhs;
  		return *this;
  	}
  
  	/// Casting and assignment
  	template<typename Y>
  	CUDA_CALLABLE vec3<T> & operator=(vec3<Y> rhs){
  		ptr[0] = (T)rhs.ptr[0];
  		ptr[1] = (T)rhs.ptr[1];
  		ptr[2] = (T)rhs.ptr[2];
  		return *this;
  	}
  
  	/// Unary minus (returns the negative of the vector)
  	CUDA_CALLABLE vec3<T> operator-() const{
  		vec3<T> result;
  		result.ptr[0] = -ptr[0];
  		result.ptr[1] = -ptr[1];
  		result.ptr[2] = -ptr[2];
  		return result;
  	}
  
8d55ae06   Pavel Govyadinov   Edited vec3 to ad...
261
262
263
264
265
266
267
  	CUDA_CALLABLE bool operator==(vec3<T> rhs) const{
  		if(rhs[0] == ptr[0] && rhs[1] == ptr[1] && rhs[2] == ptr[2])
  			return true;
  		else
  			return false;	
  	}
  
7bca968e   David Mayerich   fixed nvcc str() ...
268
  //#ifndef __CUDACC__
8e4f8364   David Mayerich   started a new opt...
269
  	/// Outputs the vector as a string
a2811d32   David Mayerich   updated vec3 str(...
270
  std::string str() const{
8e4f8364   David Mayerich   started a new opt...
271
272
  		std::stringstream ss;
  
308a743c   David Mayerich   fixed class compa...
273
  		const size_t N = 3;
8e4f8364   David Mayerich   started a new opt...
274
275
276
277
  
  		ss<<"[";
  		for(size_t i=0; i<N; i++)
  		{
308a743c   David Mayerich   fixed class compa...
278
  			ss<<ptr[i];
8e4f8364   David Mayerich   started a new opt...
279
280
281
282
283
284
285
  			if(i != N-1)
  				ss<<", ";
  		}
  		ss<<"]";
  
  		return ss.str();
  	}
7bca968e   David Mayerich   fixed nvcc str() ...
286
  //#endif
308a743c   David Mayerich   fixed class compa...
287
288
289
290
  
  	size_t size(){ return 3; }
  
  	};						//end class vec3
14634fca   David Mayerich   edits to update s...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  
  	/// Start Class cvec3 (complex vector class)
  	template<typename T>
  	class cvec3 : public vec3< std::complex<T> > {
  
  	public:
  		CUDA_CALLABLE cvec3() : vec3< std::complex<T> >() {}
  		CUDA_CALLABLE cvec3(T x, T y, T z) : vec3< std::complex<T> >(std::complex<T>(x), std::complex<T>(y), std::complex<T>(z)) {}
  		CUDA_CALLABLE cvec3(std::complex<T> x, std::complex<T> y, std::complex<T> z) : vec3< std::complex<T> >(x, y, z) {}
  
  		CUDA_CALLABLE cvec3& operator=(const vec3< std::complex<T> > rhs) {
  			vec3< std::complex<T> >::ptr[0] = rhs.get(0);
  			vec3< std::complex<T> >::ptr[1] = rhs.get(1);
  			vec3< std::complex<T> >::ptr[2] = rhs.get(2);
  			return *this;
  		}
  		CUDA_CALLABLE std::complex<T> dot(const vec3<T> rhs) {
  			std::complex<T> result =
  				vec3< std::complex<T> >::ptr[0] * rhs.get(0) +
  				vec3< std::complex<T> >::ptr[1] * rhs.get(1) +
  				vec3< std::complex<T> >::ptr[2] * rhs.get(2);
  
  			return result;
  		}
  	};						//end class cvec3
b28f312d   David Mayerich   added code for ev...
316
  }							//end namespace tira
8e4f8364   David Mayerich   started a new opt...
317
  
14634fca   David Mayerich   edits to update s...
318
319
320
  
  
  
8e4f8364   David Mayerich   started a new opt...
321
322
  /// Multiply a vector by a constant when the vector is on the right hand side
  template <typename T>
b28f312d   David Mayerich   added code for ev...
323
  tira::vec3<T> operator*(T lhs, tira::vec3<T> rhs){
8e4f8364   David Mayerich   started a new opt...
324
325
326
      return rhs * lhs;
  }
  
8309b07a   David Mayerich   fixed some vec3 e...
327
328
  //stream operator
  template<typename T>
b28f312d   David Mayerich   added code for ev...
329
  std::ostream& operator<<(std::ostream& os, tira::vec3<T> const& rhs){
8309b07a   David Mayerich   fixed some vec3 e...
330
331
332
333
  	os<<rhs.str();
  	return os;
  }
  
7d3162a2   Pavel Govyadinov   fixed majority of...
334
  #endif