vector.h
5.18 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef RTS_VECTOR_H
#define RTS_VECTOR_H
#include <iostream>
#include <cmath>
#include <sstream>
//#include "rts/math/point.h"
#include "../cuda/callable.h"
namespace stim
{
template <class T, int N=3>
struct vec
{
T v[N];
CUDA_CALLABLE vec()
{
//memset(v, 0, sizeof(T) * N);
for(int i=0; i<N; i++)
v[i] = 0;
}
//efficiency constructor, makes construction easier for 1D-4D vectors
CUDA_CALLABLE vec(T rhs)
{
for(int i=0; i<N; i++)
v[i] = rhs;
}
CUDA_CALLABLE vec(T x, T y)
{
v[0] = x;
v[1] = y;
}
CUDA_CALLABLE vec(T x, T y, T z)
{
v[0] = x;
v[1] = y;
v[2] = z;
}
CUDA_CALLABLE vec(T x, T y, T z, T w)
{
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = w;
}
//copy constructor
CUDA_CALLABLE vec( const vec<T, N>& other){
for(int i=0; i<N; i++)
v[i] = other.v[i];
}
template< typename U >
CUDA_CALLABLE operator vec<U, N>(){
vec<U, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i];
return result;
}
//template<class U>
//friend vec<U, N>::operator vec<T, N>();
CUDA_CALLABLE T len() const
{
//compute and return the vector length
T sum_sq = (T)0;
for(int i=0; i<N; i++)
{
sum_sq += v[i] * v[i];
}
return sqrt(sum_sq);
}
CUDA_CALLABLE vec<T, N> cart2sph() const
{
//convert the vector from cartesian to spherical coordinates
//x, y, z -> r, theta, phi (where theta = 0 to 2*pi)
vec<T, N> sph;
sph[0] = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
sph[1] = std::atan2(v[1], v[0]);
sph[2] = std::acos(v[2] / sph[0]);
return sph;
}
CUDA_CALLABLE vec<T, N> sph2cart() const
{
//convert the vector from cartesian to spherical coordinates
//r, theta, phi -> x, y, z (where theta = 0 to 2*pi)
vec<T, N> cart;
cart[0] = v[0] * std::cos(v[1]) * std::sin(v[2]);
cart[1] = v[0] * std::sin(v[1]) * std::sin(v[2]);
cart[2] = v[0] * std::cos(v[2]);
return cart;
}
CUDA_CALLABLE vec<T, N> norm() const
{
//compute and return the vector norm
vec<T, N> result;
//compute the vector length
T l = len();
//normalize
for(int i=0; i<N; i++)
{
result.v[i] = v[i] / l;
}
return result;
}
CUDA_CALLABLE vec<T, 3> cross(const vec<T, 3> rhs) const
{
vec<T, 3> result;
//compute the cross product (only valid for 3D vectors)
result[0] = v[1] * rhs.v[2] - v[2] * rhs.v[1];
result[1] = v[2] * rhs.v[0] - v[0] * rhs.v[2];
result[2] = v[0] * rhs.v[1] - v[1] * rhs.v[0];
return result;
}
CUDA_CALLABLE T dot(vec<T, N> rhs) const
{
T result = (T)0;
for(int i=0; i<N; i++)
result += v[i] * rhs.v[i];
return result;
}
//arithmetic
CUDA_CALLABLE vec<T, N> operator+(vec<T, N> rhs) const
{
vec<T, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i] + rhs.v[i];
return result;
}
CUDA_CALLABLE vec<T, N> operator+(T rhs) const
{
vec<T, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i] + rhs;
return result;
}
CUDA_CALLABLE vec<T, N> operator-(vec<T, N> rhs) const
{
vec<T, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i] - rhs.v[i];
return result;
}
CUDA_CALLABLE vec<T, N> operator*(T rhs) const
{
vec<T, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i] * rhs;
return result;
}
CUDA_CALLABLE vec<T, N> operator/(T rhs) const
{
vec<T, N> result;
for(int i=0; i<N; i++)
result.v[i] = v[i] / rhs;
return result;
}
CUDA_CALLABLE vec<T, N> operator*=(T rhs){
for(int i=0; i<N; i++)
v[i] = v[i] * rhs;
return *this;
}
CUDA_CALLABLE vec<T, N> & operator=(T rhs){
for(int i=0; i<N; i++)
v[i] = rhs;
return *this;
}
template<typename Y>
CUDA_CALLABLE vec<T, N> & operator=(vec<Y, N> rhs){
for(int i=0; i<N; i++)
v[i] = rhs.v[i];
return *this;
}
//unary minus
CUDA_CALLABLE vec<T, N> operator-() const{
vec<T, N> r;
//negate the vector
for(int i=0; i<N; i++)
r.v[i] = -v[i];
return r;
}
CUDA_CALLABLE bool operator==(vec<T, N> rhs) const
{
if ( (rhs.v[0] == v[0]) && (rhs.v[1] == v[1]) && (rhs.v[2] == v[2]) )
return true;
return false;
}
std::string str() const
{
std::stringstream ss;
ss<<"[";
for(int i=0; i<N; i++)
{
ss<<v[i];
if(i != N-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
//bracket operator - allows assignment to the vector
CUDA_CALLABLE T& operator[](const unsigned int i)
{
return v[i];
}
};
} //end namespace rts
template <typename T, int N>
std::ostream& operator<<(std::ostream& os, stim::vec<T, N> v)
{
os<<v.str();
return os;
}
template <typename T, int N>
CUDA_CALLABLE stim::vec<T, N> operator*(T lhs, stim::vec<T, N> rhs)
{
stim::vec<T, N> r;
return rhs * lhs;
}
//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7
//template<class T, int N> using rtsVector = rts::vector<T, N>;
//#endif
#endif