-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vec3.h
278 lines (237 loc) · 6.52 KB
/
Vec3.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
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
// ========================================================================= //
// Authors: Matthias Borner //
// mailto:matthias.borner@igd.fraunhofer.de //
// //
// GRIS - Graphisch Interaktive Systeme //
// Technische Universität Darmstadt //
// Fraunhoferstrasse 5 //
// D-64283 Darmstadt, Germany //
// //
// Creation Date: 04.11.2009 //
// ========================================================================= //
// ========================================================================= //
// Simple Vector Class consisting of 3 primitives added to std library //
// Constructors: Vec3(), Vec3(T v1, T v2, T v3) //
// Index: [i] for i = 0,1,2 //
// Two sided Vec3: +, -, * (dot product), ^ (cross product), ==, != //
// Left sided Vec3: +=, -=, *= (scalar mul), /= (scalar div) //
// Left or right sided float: * //
// Right sided float: / //
// Functions: length(), sqlength(), distance(Vec3 v), normalize(), //
// normalized(), clear(), set(f,f,f), rotations //
// ========================================================================= //
#ifndef VEC3_H
#define VEC3_H
#include <math.h>
#include <string>
#define M_RadToDeg 0.0174532925f
// embed in namespace std to not mix up with OpenSG Vec3 or any others
//namespace std {
template<class T>
class Vec3
{
public:
// values
T x,y,z;
// empty constructor. sets values to 0
Vec3() : x(0), y(0), z(0)
{
}
// constructor with 3 values (v1,v2,v3)
Vec3(const T v1, const T v2, const T v3) : x(v1), y(v2), z(v3)
{
}
// copy constuctor
Vec3(const Vec3 & other) : x(other.x), y(other.y), z(other.z)
{
}
// returns i-th komponent (i=0,1,2) (RHS array operator)
const T operator[] (unsigned int i) const
{
return *(&x+i);
}
// LHS array operator
T & operator [] (unsigned int i)
{
return *(&x+i);
}
// Vec3 = Vec3 + Vec3 (vector addition)
Vec3 operator+ (const Vec3 &v) const
{
Vec3 result;
result.x = x + v.x;
result.y = y + v.y;
result.z = z + v.z;
return result;
}
// Vec3 = Vec3 - Vec3 (normal vector subtraction)
Vec3 operator- (const Vec3 &v) const
{
Vec3 result;
result.x = x - v.x;
result.y = y - v.y;
result.z = z - v.z;
return result;
}
// T = Vec3 * Vec3 (dot product)
T operator* (const Vec3 &v) const
{
return x*v.x + y*v.y + z*v.z;
}
// Vec3 = Vec3 ^ Vec3 (cross product)
Vec3 operator^ (const Vec3 &v) const
{
Vec3 result;
result.x = y*v.z - z*v.y;
result.y = z*v.x - x*v.z;
result.z = x*v.y - y*v.x;
return result;
}
// Vec3 += Vec3 (vector addition)
Vec3 operator+= (const Vec3 &v)
{
*this = *this + v;
return *this;
}
// Vec3 -= Vec3 (vector subtraction)
Vec3 operator-= (const Vec3 &v)
{
*this = *this - v;
return *this;
}
// Vec3 *= T (scalar multiplication)
Vec3 operator*= (const T f)
{
*this = *this * f;
return *this;
}
// Vec3 /= T (scalar division)
Vec3 operator/= (const T f)
{
*this = *this / f;
return *this;
}
// Vec3 == Vec3 (equals)
bool operator== (const Vec3 &v) const
{
if (v.x == x && v.y == y && v.z == z) return true;
else return false;
}
// Vec3 != Vec3 (not equal)
bool operator!= (const Vec3 &v) const
{
return !(*this == v);
}
// Vec3 = Vec3 * T (scalar multiplication)
Vec3 operator* (const T &f) const
{
Vec3 result;
result.x = x * f;
result.y = y * f;
result.z = z * f;
return result;
}
// Vec3 = Vec3 / T (scalar division)
Vec3 operator/ (const T &f) const
{
Vec3 result;
result.x = x / f;
result.y = y / f;
result.z = z / f;
return result;
}
// returns euclidic length (sqrt(x*x + y*y + z*z))
float length() const
{
return sqrt(x*x + y*y + z*z);
}
// returns squared euclidic length (x*x + y*y + z*z)
T sqlength() const
{
return x*x + y*y + z*z;
}
// returns distance to v
float distance(const Vec3 &v) const
{
return (v - *this).length();
}
// normalizes this vector (division by length). returns false when length is < 0.00001
bool normalize()
{
T l = this->length();
if (fabs(l) < 0.00001) return false;
*this /= l;
return true;
}
// returns normalized vector but does not change this Vec3. returns unnormalized vector if its length is < 0.00001
Vec3 normalized() const
{
T l = this->length();
if (fabs(l) < 0.00001) return *this;
return *this / l;
}
// sets values to 0
void clear()
{
x = 0;
y = 0;
z = 0;
}
// rotates the vector around x (angle in degree)
void rotX(float angle)
{
float y_new = cos(angle*M_RadToDeg)*y - sin(angle*M_RadToDeg)*z;
float z_new = sin(angle*M_RadToDeg)*y + cos(angle*M_RadToDeg)*z;
y = y_new;
z = z_new;
}
// rotates the vector around y (angle in degree)
void rotY(float angle)
{
float x_new = cos(angle*M_RadToDeg)*x + sin(angle*M_RadToDeg)*z;
float z_new = -sin(angle*M_RadToDeg)*x + cos(angle*M_RadToDeg)*z;
x = x_new;
z = z_new;
}
// rotates the vector around z (angle in degree)
void rotZ(float angle)
{
float x_new = cos(angle*M_RadToDeg)*x - sin(angle*M_RadToDeg)*y;
float y_new = sin(angle*M_RadToDeg)*x + cos(angle*M_RadToDeg)*y;
x = x_new;
y = y_new;
}
// set values
void set(T _x, T _y, T _z)
{
x = _x;
y = _y;
z = _z;
}
void set(const Vec3 &v)
{
x = v.x;
y = v.y;
z = v.z;
}
}; // class Vec3
// Vec3 = T * Vec3 (scalar multiplication)
template <class T>
Vec3<T> operator* (const T &f, const Vec3<T> &v)
{
return v * f;
}
// ostream << operator
template< class T>
std::ostream& operator<< (std::ostream& os, const Vec3<T> & v)
{
os << v.x << ", " << v.y << ", " << v.z;
return os;
}
// predifined typedefs
typedef Vec3<float> Vec3f;
typedef Vec3<int> Vec3i;
typedef Vec3<unsigned int> Vec3ui;
typedef Vec3<double> Vec3d;
//} // namespace std
#endif