-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvec.hpp
193 lines (155 loc) · 3.6 KB
/
vec.hpp
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
#ifndef INCLUDED_HPP_VEC
#define INCLUDED_HPP_VEC
#include <cl/cl.h>
#include <math.h>
#include <algorithm>
inline cl_float4 neg(cl_float4 v)
{
cl_float4 nv;
nv.x = -v.x;
nv.y = -v.y;
nv.z = -v.z;
nv.w = -v.w;
return nv;
}
inline cl_float4 add(cl_float4 v1, cl_float4 v2)
{
cl_float4 nv;
nv.x = v1.x + v2.x;
nv.y = v1.y + v2.y;
nv.z = v1.z + v2.z;
nv.w = v1.w + v2.w;
return nv;
}
inline cl_float4 add(cl_float4 v1, float v)
{
cl_float4 nv;
nv.x = v1.x + v;
nv.y = v1.y + v;
nv.z = v1.z + v;
nv.w = v1.w + v;
return nv;
}
inline cl_float4 sub(cl_float4 v1, cl_float4 v2)
{
cl_float4 nv;
nv.x = v1.x - v2.x;
nv.y = v1.y - v2.y;
nv.z = v1.z - v2.z;
nv.w = v1.w - v2.w;
return nv;
}
inline cl_float4 sub(cl_float4 v1, float v)
{
cl_float4 nv;
nv.x = v1.x - v;
nv.y = v1.y - v;
nv.z = v1.z - v;
nv.w = v1.w - v;
return nv;
}
inline cl_float4 mult(cl_float4 v1, float v)
{
cl_float4 nv;
nv.x = v1.x * v;
nv.y = v1.y * v;
nv.z = v1.z * v;
nv.w = v1.w * v;
return nv;
}
inline cl_float4 mult(cl_float4 v1, cl_float4 v)
{
cl_float4 nv;
nv.x = v1.x * v.x;
nv.y = v1.y * v.y;
nv.z = v1.z * v.z;
nv.w = v1.w * v.w;
return nv;
}
inline float dist(cl_float4 v1, cl_float4 v2) ///w discarded because hmm
{
cl_float4 nv;
nv = sub(v1, v2);
return sqrtf(nv.x*nv.x + nv.y*nv.y + nv.z*nv.z); ///????
}
inline cl_float4 avg(cl_float4 v1, cl_float4 v2)
{
cl_float4 nv;
nv = add(v1, v2);
nv.x /= 2.0f;
nv.y /= 2.0f;
nv.z /= 2.0f;
nv.w /= 2.0f;
return nv;
}
inline float dot(cl_float4 v1, cl_float4 v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; ///ignore w?
}
inline cl_float4 cross(cl_float4 v1, cl_float4 v2)
{
cl_float4 nv = {v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x, 0.0f};
return nv;
}
inline cl_float4 div(cl_float4 v1, float v)
{
cl_float4 nv;
nv.x = v1.x / v;
nv.y = v1.y / v;
nv.z = v1.z / v;
nv.w = v1.w / v;
return nv;
}
inline cl_float4 div(cl_float4 v1, cl_float4 v2)
{
cl_float4 nv;
nv.x = v1.x / v2.x;
nv.y = v1.y / v2.y;
nv.z = v1.z / v2.z;
///ignore w, will probably divide by 0
return nv;
}
inline cl_float4 normalise(cl_float4 v1)
{
float length = sqrtf(v1.x*v1.x + v1.y*v1.y + v1.z*v1.z); ///w? What am i doing?
return div(v1, length);
}
inline cl_float2 normalise(cl_float2 v1)
{
float length = sqrtf(v1.x * v1.x + v1.y * v1.y);
return {v1.x / length, v1.y / length};
}
inline float length(cl_float4 v1)
{
return sqrtf(v1.x*v1.x + v1.y*v1.y + v1.z*v1.z);
}
/*inline cl_float4 clamp(cl_float4 v1, float _min, float _max)
{
v1.x = std::max(v1.x, _min);
v1.y = std::max(v1.y, _min);
v1.z = std::max(v1.z, _min);
v1.x = std::min(v1.x, _max);
v1.y = std::min(v1.y, _max);
v1.z = std::min(v1.z, _max);
return v1;
}*/
inline float clamp(float v, float _min, float _max)
{
v = std::max(v, _min);
v = std::min(v, _max);
return v;
}
inline cl_float4 clamp(cl_float4 v, float _min, float _max)
{
cl_float4 nv;
nv.x = clamp(v.x, _min, _max);
nv.y = clamp(v.y, _min, _max);
nv.z = clamp(v.z, _min, _max);
nv.w = clamp(v.w, _min, _max);
return nv;
}
inline bool is_equal(cl_float4 v1, cl_float4 v2)
{
return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z);
}
#endif // INCLUDED_HPP_VEC