-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
209 lines (192 loc) · 4.55 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
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
#include <cmath>
#include <new>
#include <stdexcept>
#include <utility> // for std::hash<T>
static constexpr float eps = 1e-4;
//#define CAN_ACCESS_VECTOR_AS_ARRAY
struct VectorF
{
#ifdef CAN_ACCESS_VECTOR_AS_ARRAY
union
{
struct
{
float x, y, z;
};
float values[3];
};
#else
float x, y, z;
#endif
constexpr VectorF()
: x(0), y(0), z(0)
{
}
constexpr VectorF(float v)
: x(v), y(v), z(v)
{
}
constexpr VectorF(float x, float y, float z)
: x(x), y(y), z(z)
{
}
constexpr const VectorF & operator +() const
{
return *this;
}
constexpr VectorF operator -() const
{
return VectorF(-x, -y, -z);
}
constexpr VectorF operator +(const VectorF & rt) const
{
return VectorF(x + rt.x, y + rt.y, z + rt.z);
}
constexpr VectorF operator -(const VectorF & rt) const
{
return VectorF(x - rt.x, y - rt.y, z - rt.z);
}
constexpr VectorF operator *(const VectorF & rt) const
{
return VectorF(x * rt.x, y * rt.y, z * rt.z);
}
constexpr VectorF operator /(const VectorF & rt) const
{
return VectorF(x / rt.x, y / rt.y, z / rt.z);
}
friend constexpr float dot(const VectorF & a, const VectorF & b);
#ifdef CAN_ACCESS_VECTOR_AS_ARRAY
float * begin()
{
return &values[0];
}
float * end()
{
return &values[3];
}
constexpr const float * begin() const
{
return &values[0];
}
constexpr const float * end() const
{
return &values[3];
}
constexpr const float * cbegin() const
{
return &values[0];
}
constexpr const float * cend() const
{
return &values[3];
}
static constexpr size_t size()
{
return 3;
}
#endif
friend constexpr VectorF cross(const VectorF & a, const VectorF & b);
friend constexpr VectorF operator *(float a, const VectorF & b);
friend constexpr VectorF operator /(float a, const VectorF & b);
constexpr VectorF operator *(float rt) const
{
return VectorF(x * rt, y * rt, z * rt);
}
constexpr VectorF operator /(float rt) const
{
return VectorF(x / rt, y / rt, z / rt);
}
friend constexpr float absSquared(const VectorF & v);
friend float abs(const VectorF & v);
friend VectorF normalize(const VectorF & v);
friend VectorF normalize(const VectorF & v, std::nothrow_t);
const VectorF & operator +=(VectorF rt)
{
return operator =(operator +(rt));
}
const VectorF & operator -=(VectorF rt)
{
return operator =(operator -(rt));
}
const VectorF & operator *=(VectorF rt)
{
return operator =(operator *(rt));
}
const VectorF & operator /=(VectorF rt)
{
return operator =(operator /(rt));
}
const VectorF & operator *=(float rt)
{
return operator =(operator *(rt));
}
const VectorF & operator /=(float rt)
{
return operator =(operator /(rt));
}
};
namespace std
{
template <>
struct hash<VectorF>
{
hash<float> floatHasher;
size_t operator ()(VectorF v) const
{
return floatHasher(v.x) + 2 * floatHasher(v.y) + 3 * floatHasher(v.z);
}
};
}
constexpr float dot(const VectorF & a, const VectorF & b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
constexpr VectorF cross(const VectorF & a, const VectorF & b)
{
return VectorF(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
constexpr VectorF operator *(float a, const VectorF & b)
{
return VectorF(a * b.x, a * b.y, a * b.z);
}
constexpr VectorF operator /(float a, const VectorF & b)
{
return VectorF(a / b.x, a / b.y, a / b.z);
}
constexpr float absSquared(const VectorF & v)
{
return v.x * v.x + v.y * v.y + v.z * v.z;
}
inline float abs(const VectorF & v)
{
return std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
namespace
{
inline float normalizeNoThrowHelper(float v)
{
return (v == 0) ? 1 : v;
}
}
inline VectorF normalize(const VectorF & v)
{
float r = abs(v);
if(r == 0)
throw std::range_error("got <0, 0, 0> in normalize(VectorF)");
return v / r;
}
inline VectorF normalizeNoThrow(const VectorF & v)
{
return v / normalizeNoThrowHelper(abs(v));
}
constexpr bool operator ==(VectorF a, VectorF b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
constexpr bool operator !=(VectorF a, VectorF b)
{
return a.x != b.x || a.y != b.y || a.z != b.z;
}
#endif // VECTOR_H_INCLUDED