-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvector.h
228 lines (195 loc) · 5.03 KB
/
vector.h
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
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_VECTOR_HEADER
#define CACC_VECTOR_HEADER
#include "defines.h"
#include "float.h"
CACC_NAMESPACE_BEGIN
template <typename T, int N> class Vector;
template <typename T, int N> __forceinline__ __host__ __device__
float norm(Vector<T, N> const &);
template <typename T, int N>
class Vector {
protected:
T v;
public:
__forceinline__ __host__ __device__
Vector() {}
__forceinline__ __host__ __device__
Vector(float value) {
#pragma unroll
for (int i = 0; i < N; ++i) {
v[i] = value;
}
}
__forceinline__ __host__ __device__
Vector(float x, float y) {
v[0] = x;
v[1] = y;
}
__forceinline__ __host__ __device__
Vector(float x, float y, float z) {
v[0] = x;
v[1] = y;
v[2] = z;
}
__forceinline__ __host__ __device__
Vector(float x, float y, float z, float w) {
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = w;
}
__forceinline__ __host__ __device__
Vector(float const * values) {
#pragma unroll
for (int i = 0; i < N; ++i) {
v[i] = values[i];
}
}
__forceinline__ __host__ __device__
Vector(Vector const & other) {
*this = other;
}
__forceinline__ __host__ __device__
float & operator[] (int i) {
return v[i];
}
__forceinline__ __host__ __device__
float const & operator[] (int i) const {
return v[i];
}
__forceinline__ __host__ __device__
Vector & operator= (Vector const & other) {
v.data = other.v.data;
return *this;
}
__forceinline__ __host__ __device__
Vector operator- (void) const {
Vector ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = -v[i];
}
return ret;
}
__forceinline__ __host__ __device__
Vector operator* (float const & rhs) const{
Vector ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = v[i] * rhs;
}
return ret;
}
__forceinline__ __host__ __device__
Vector operator/ (float const & rhs) const {
Vector ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = v[i] / rhs;
}
return ret;
}
__forceinline__ __host__ __device__
Vector & operator/= (float const & rhs) {
#pragma unroll
for (int i = 0; i < N; ++i) {
v[i] /= rhs;
}
return *this;
}
__forceinline__ __host__ __device__
Vector operator+ (Vector const & rhs) const {
Vector ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = v[i] + rhs[i];
}
return ret;
}
__forceinline__ __host__ __device__
Vector & operator+= (Vector const & rhs) {
#pragma unroll
for (int i = 0; i < N; ++i) {
v[i] += rhs[i];
}
return *this;
}
__forceinline__ __host__ __device__
Vector operator- (Vector const & rhs) const{
Vector ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = v[i] - rhs[i];
}
return ret;
}
__forceinline__ __host__ __device__
Vector normalize() {
return *this / norm(*this);
}
};
typedef Vector<Float4, 4> Vec4f;
typedef Vector<Float4, 3> Vec3f;
typedef Vector<Float2, 2> Vec2f;
template<typename T, int N>
__forceinline__ __host__ __device__
Vector<T, N> operator * (float lhs, Vector<T, N> const & rhs) {
Vector<T, N> ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = lhs * rhs[i];
}
return ret;
}
template<typename T, int N>
__forceinline__ __host__ __device__
Vector<T, N> operator / (float lhs, Vector<T, N> const & rhs) {
Vector<T, N> ret;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret[i] = lhs * rhs[i];
}
return ret;
}
template<typename T, int N>
__forceinline__ __host__ __device__
float dot(Vector<T, N> const & lhs, Vector<T, N> const & rhs) {
float ret = 0.0f;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret += lhs[i] * rhs[i];
}
return ret;
}
template<typename T, int N>
__forceinline__ __host__ __device__
float square_norm(Vector<T, N> const & vec) {
float ret = 0.0f;
#pragma unroll
for (int i = 0; i < N; ++i) {
ret += vec[i] * vec[i];
}
return ret;
}
template<typename T, int N>
__forceinline__ __host__ __device__
float norm(Vector<T, N> const & vec) {
return sqrt(square_norm(vec));
}
__forceinline__ __host__ __device__
Vec3f cross(Vec3f const & lhs, Vec3f const & rhs) {
Vec3f ret;
ret[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
ret[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
ret[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
return ret;
}
CACC_NAMESPACE_END
#endif /* CACC_VECTOR_HEADER */