-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVec3.hpp
127 lines (96 loc) · 2.56 KB
/
Vec3.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
#ifndef VEC3_H
#define VEC3_H
struct Vec3
{
double x, y, z;
// Constructor
Vec3(double x_ = 0, double y_ = 0, double z_ = 0)
: x(x_), y(y_), z(z_) {
}
/**********************/
// Operator Overloads //
/**********************/
inline bool operator==(const Vec3& v) const {
return (x == v.x && y == v.y && z == v.z);
}
inline Vec3 operator+(const Vec3& v) const {
return Vec3(x + v.x, y + v.y, z + v.z);
}
inline Vec3 operator+(double S) const {
return Vec3(x + S, y + S, z + S);
}
inline void operator+=(const Vec3& v) {
x += v.x; y += v.y; z += v.z;
}
inline void operator+=(double S) {
x += S; y += S; z += S;
}
inline Vec3 operator-() const {
return Vec3(-x, -y, -z);
}
inline Vec3 operator-(const Vec3& v) const {
return Vec3(x - v.x, y - v.y, z - v.z);
}
inline Vec3 operator-(double S) const {
return Vec3(x - S, y - S, z - S);
}
inline void operator-=(const Vec3& v) {
x -= v.x; y -= v.y; z -= v.z;
}
inline void operator-=(double S) {
x -= S; y -= S; z -= S;
}
inline Vec3 operator/(const Vec3& v) const {
return Vec3(x / v.x, y / v.y, z / v.z);
}
inline Vec3 operator/(double S) const {
return Vec3(x / S, y / S, z / S);
}
inline void operator/=(const Vec3& v) {
x /= v.x; y /= v.y; z /= v.z;
}
inline void operator/=(double S) {
x /= S; y /= S; z /= S;
}
inline Vec3 operator*(const Vec3& v) const {
return Vec3(x * v.x, y * v.y, z * v.z);
}
inline Vec3 operator*(double S) const {
return Vec3(x * S, y * S, z * S);
}
inline void operator*=(const Vec3& v) {
x *= v.x; y *= v.y; z *= v.z;
}
inline void operator*=(double S) {
x *= S; y *= S; z *= S;
}
/*************/
// Functions //
/*************/
inline double dot(const Vec3 &v) const {
return x*v.x + y*v.y + z*v.z;
}
inline Vec3 cross(const Vec3 &v) const {
return Vec3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
}
inline double magSq() const {
return x*x + y*y + z*z;
}
inline double mag() const {
return sqrt(this->magSq());
}
inline double dist(const Vec3& v) const {
return (v - *this).mag();
}
inline void normalize() {
double mag = this->mag();
if(mag != 0) (*this) *= 1.0/mag;
}
/**********/
// Output //
/**********/
friend ostream& operator<<(ostream& os, const Vec3& v) {
return (os << "(" << v.x << "," << v.y << "," << v.z << ")");
}
};
#endif