-
Notifications
You must be signed in to change notification settings - Fork 74
/
Vec3.h
119 lines (94 loc) · 1.73 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
#ifndef Vec3_h_
#define Vec3_h_
#include <cmath>
struct Vec3;
Vec3 operator*(float r, const Vec3& v);
struct Vec3 {
union {
struct {
float x,y,z;
};
float D[3];
};
Vec3() { }
Vec3(float _x, float _y, float _z)
:x(_x), y(_y), z(_z)
{ }
float& operator[](unsigned int i) {
return D[i];
}
const float& operator[](unsigned int i) const {
return D[i];
}
float maxComponent() const {
float r = x;
if(y>r) r = y;
if(z>r) r = z;
return r;
}
float minComponent() const {
float r = x;
if(y<r) r = y;
if(z<r) r = z;
return r;
}
Vec3 operator+(const Vec3& r) const {
return Vec3(x+r.x, y+r.y, z+r.z);
}
Vec3 operator-(const Vec3& r) const {
return Vec3(x-r.x, y-r.y, z-r.z);
}
Vec3 cmul(const Vec3& r) const {
return Vec3(x*r.x, y*r.y, z*r.z);
}
Vec3 cdiv(const Vec3& r) const {
return Vec3(x/r.x, y/r.y, z/r.z);
}
Vec3 operator*(float r) const {
return Vec3(x*r,y*r,z*r);
}
Vec3 operator/(float r) const {
return Vec3(x/r, y/r, z/r);
}
Vec3& operator+=(const Vec3& r) {
x+=r.x;
y+=r.y;
z+=r.z;
return *this;
}
Vec3& operator-=(const Vec3& r) {
x-=r.x;
y-=r.y;
z-=r.z;
return *this;
}
Vec3& operator*=(float r) {
x*=r; y*=r; z*=r;
return *this;
}
// Inner/dot product
float operator*(const Vec3& r) const {
return x*r.x + y*r.y + z*r.z;
}
float norm() const {
return sqrtf(x*x+y*y+z*z);
}
float normSquared() const {
return x*x + y*y + z*z;
}
// Cross product
Vec3 operator^(const Vec3& r) const {
return Vec3(
y * r.z - z * r.y,
z * r.x - x * r.z,
x * r.y - y * r.x
);
}
Vec3 normalized() const {
return *this / norm();
}
};
inline Vec3 operator*(float r, const Vec3& v) {
return Vec3(v.x*r, v.y*r, v.z*r);
}
#endif