-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector3.h
127 lines (107 loc) · 2.54 KB
/
vector3.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
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
#ifndef _VECTOR3F_H_
#define _VECTOR3F_H_
#include <cmath>
struct Vector3f
{
static const Vector3f UnitX;
static const Vector3f UnitY;
static const Vector3f UnitZ;
float x, y, z;
inline Vector3f()
{
x = y = z = 0.0f;
}
inline Vector3f(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}
inline Vector3f(float xyz)
{
x = y = z = xyz;
}
inline Vector3f(const float *xyzArr)
{
x = xyzArr[0];
y = xyzArr[1];
z = xyzArr[2];
}
inline operator const float *() const
{
return ((const float *)&x);
}
inline float &operator[](unsigned int idx)
{
return (*(((float *)&x) + idx));
}
inline void operator +=(float s)
{
x += s;
y += s;
z += s;
}
inline void operator +=(const Vector3f &v)
{
x += v.x;
y += v.y;
z += v.z;
}
inline void operator -=(float s)
{
x -= s;
y -= s;
z -= s;
}
inline void operator -=(const Vector3f &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
}
inline void operator *=(float s)
{
x *= s;
y *= s;
z *= s;
}
inline void operator *=(const Vector3f &v)
{
x *= v.x;
y *= v.y;
z *= v.z;
}
inline void operator /=(float s)
{
float inv = 1.0f / s;
x *= inv;
y *= inv;
z *= inv;
}
inline void operator /=(const Vector3f &v)
{
x /= v.x;
y /= v.y;
z /= v.z;
}
};
Vector3f cross(const Vector3f &u, const Vector3f &v);
Vector3f normalize(const Vector3f &v);
Vector3f operator *(const Vector3f &u, const Vector3f &v);
Vector3f operator *(const Vector3f &v, float s);
Vector3f operator *(float s, const Vector3f &v);
Vector3f operator +(const Vector3f &u, const Vector3f &v);
Vector3f operator +(const Vector3f &v, float s);
Vector3f operator +(float s, const Vector3f &v);
Vector3f operator -(const Vector3f &u, const Vector3f &v);
Vector3f operator -(const Vector3f &v);
Vector3f operator -(const Vector3f &v, float s);
Vector3f operator -(float s, const Vector3f &v);
Vector3f operator /(const Vector3f &u, const Vector3f &v);
Vector3f operator /(const Vector3f &v, float s);
Vector3f operator /(float s, const Vector3f &v);
bool isZeroLength(const Vector3f &v);
float dot(const Vector3f &u, const Vector3f &v);
float length(const Vector3f &v);
#endif /* _VECTOR3F_H_ */