f1402849
dmayerich
renewed commit
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#ifndef RTS_VECTOR_H
#define RTS_VECTOR_H
#include <iostream>
//#include "rts/point.h"
namespace rts
{
template <class T, int N>
|
a47a23a9
dmayerich
added ENVI functions
|
13
|
struct rtsVector
|
f1402849
dmayerich
renewed commit
|
14
15
16
|
{
T v[N];
|
a47a23a9
dmayerich
added ENVI functions
|
17
|
CUDA_CALLABLE rtsVector()
|
f1402849
dmayerich
renewed commit
|
18
19
|
{
//memset(v, 0, sizeof(T) * N);
|
a47a23a9
dmayerich
added ENVI functions
|
20
21
|
for(int i=0; i<N; i++)
v[i] = 0;
|
f1402849
dmayerich
renewed commit
|
22
23
24
|
}
//efficiency constructor, makes construction easier for 1D-4D vectors
|
a47a23a9
dmayerich
added ENVI functions
|
25
|
CUDA_CALLABLE rtsVector(T x, T y = (T)0.0, T z = (T)0.0, T w = (T)0.0)
|
f1402849
dmayerich
renewed commit
|
26
27
28
29
30
31
32
33
34
35
36
|
{
if(N >= 1)
v[0] = x;
if(N >= 2)
v[1] = y;
if(N >= 3)
v[2] = z;
if(N >= 4)
v[3] = w;
}
|
a47a23a9
dmayerich
added ENVI functions
|
37
|
CUDA_CALLABLE rtsVector(const T(&data)[N])
|
f1402849
dmayerich
renewed commit
|
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
{
memcpy(v, data, sizeof(T) * N);
}
CUDA_CALLABLE T len()
{
//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 std::sqrt(sum_sq);
}
|
a47a23a9
dmayerich
added ENVI functions
|
54
|
CUDA_CALLABLE rtsVector<T, N> cart2sph()
|
f1402849
dmayerich
renewed commit
|
55
56
|
{
//convert the vector from cartesian to spherical coordinates
|
a47a23a9
dmayerich
added ENVI functions
|
57
|
//x, y, z -> r, theta, phi (where theta = 0 to 2*pi)
|
f1402849
dmayerich
renewed commit
|
58
|
|
a47a23a9
dmayerich
added ENVI functions
|
59
60
61
|
rtsVector<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]);
|
f1402849
dmayerich
renewed commit
|
62
63
64
65
66
|
sph[2] = std::acos(v[2] / sph[0]);
return sph;
}
|
a47a23a9
dmayerich
added ENVI functions
|
67
|
CUDA_CALLABLE rtsVector<T, N> sph2cart()
|
f1402849
dmayerich
renewed commit
|
68
69
|
{
//convert the vector from cartesian to spherical coordinates
|
a47a23a9
dmayerich
added ENVI functions
|
70
|
//r, theta, phi -> x, y, z (where theta = 0 to 2*pi)
|
f1402849
dmayerich
renewed commit
|
71
|
|
a47a23a9
dmayerich
added ENVI functions
|
72
|
rtsVector<T, N> cart;
|
f1402849
dmayerich
renewed commit
|
73
74
75
76
77
78
79
|
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;
}
|
a47a23a9
dmayerich
added ENVI functions
|
80
|
CUDA_CALLABLE rtsVector<T, N> norm()
|
f1402849
dmayerich
renewed commit
|
81
82
|
{
//compute and return the vector norm
|
a47a23a9
dmayerich
added ENVI functions
|
83
|
rtsVector<T, N> result;
|
f1402849
dmayerich
renewed commit
|
84
85
86
87
88
89
90
91
92
93
94
95
96
|
//compute the vector length
T l = len();
//normalize
for(int i=0; i<N; i++)
{
result.v[i] = v[i] / l;
}
return result;
}
|
a47a23a9
dmayerich
added ENVI functions
|
97
|
CUDA_CALLABLE rtsVector<T, 3> cross(rtsVector<T, 3> rhs)
|
f1402849
dmayerich
renewed commit
|
98
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
99
|
rtsVector<T, 3> result;
|
f1402849
dmayerich
renewed commit
|
100
101
102
103
104
105
106
107
|
//compute the cross product (only valid for 3D vectors)
result[0] = v[1] * rhs[2] - v[2] * rhs[1];
result[1] = v[2] * rhs[0] - v[0] * rhs[2];
result[2] = v[0] * rhs[1] - v[1] * rhs[0];
return result;
}
|
a47a23a9
dmayerich
added ENVI functions
|
108
109
|
CUDA_CALLABLE T dot(rtsVector<T, N> rhs)
|
f1402849
dmayerich
renewed commit
|
110
111
112
113
114
115
116
|
{
T result = (T)0;
for(int i=0; i<N; i++)
result += v[i] * rhs.v[i];
return result;
|
a47a23a9
dmayerich
added ENVI functions
|
117
|
|
f1402849
dmayerich
renewed commit
|
118
119
120
|
}
//arithmetic
|
a47a23a9
dmayerich
added ENVI functions
|
121
|
CUDA_CALLABLE rtsVector<T, N> operator+(rtsVector<T, N> rhs)
|
f1402849
dmayerich
renewed commit
|
122
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
123
|
rtsVector<T, N> result;
|
f1402849
dmayerich
renewed commit
|
124
125
126
127
128
129
|
for(int i=0; i<N; i++)
result.v[i] = v[i] + rhs.v[i];
return result;
}
|
a47a23a9
dmayerich
added ENVI functions
|
130
|
CUDA_CALLABLE rtsVector<T, N> operator-(rtsVector<T, N> rhs)
|
f1402849
dmayerich
renewed commit
|
131
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
132
|
rtsVector<T, N> result;
|
f1402849
dmayerich
renewed commit
|
133
134
135
136
137
138
|
for(int i=0; i<N; i++)
result.v[i] = v[i] - rhs.v[i];
return result;
}
|
a47a23a9
dmayerich
added ENVI functions
|
139
|
CUDA_CALLABLE rtsVector<T, N> operator*(T rhs)
|
f1402849
dmayerich
renewed commit
|
140
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
141
|
rtsVector<T, N> result;
|
f1402849
dmayerich
renewed commit
|
142
143
144
145
146
147
|
for(int i=0; i<N; i++)
result.v[i] = v[i] * rhs;
return result;
}
|
a47a23a9
dmayerich
added ENVI functions
|
148
|
CUDA_CALLABLE rtsVector<T, N> operator/(T rhs)
|
f1402849
dmayerich
renewed commit
|
149
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
150
|
rtsVector<T, N> result;
|
f1402849
dmayerich
renewed commit
|
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
|
for(int i=0; i<N; i++)
result.v[i] = v[i] / rhs;
return result;
}
std::string toStr()
{
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
CUDA_CALLABLE T& operator[](int i)
{
return v[i];
}
};
} //end namespace rts
template <typename T, int N>
|
a47a23a9
dmayerich
added ENVI functions
|
186
|
std::ostream& operator<<(std::ostream& os, rts::rtsVector<T, N> v)
|
f1402849
dmayerich
renewed commit
|
187
188
189
190
191
192
193
|
{
os<<v.toStr();
return os;
}
//arithmetic operators
template <typename T, int N>
|
a47a23a9
dmayerich
added ENVI functions
|
194
|
CUDA_CALLABLE rts::rtsVector<T, N> operator-(rts::rtsVector<T, N> v)
|
f1402849
dmayerich
renewed commit
|
195
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
196
|
rts::rtsVector<T, N> r;
|
f1402849
dmayerich
renewed commit
|
197
198
199
200
201
202
203
204
205
|
//negate the vector
for(int i=0; i<N; i++)
r.v[i] = -v.v[i];
return r;
}
template <typename T, int N>
|
a47a23a9
dmayerich
added ENVI functions
|
206
|
CUDA_CALLABLE rts::rtsVector<T, N> operator*(T lhs, rts::rtsVector<T, N> rhs)
|
f1402849
dmayerich
renewed commit
|
207
|
{
|
a47a23a9
dmayerich
added ENVI functions
|
208
|
rts::rtsVector<T, N> r;
|
f1402849
dmayerich
renewed commit
|
209
210
211
212
213
|
return rhs * lhs;
}
#endif
|