-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector3.h
157 lines (126 loc) · 2.25 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
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
#ifndef OC_VECTOR3_H
#define OC_VECTOR3_H
#include <iosfwd>
#include "math.h"
struct Vector3
{
float x;
float y;
float z;
Vector3(){}
Vector3(const Vector3 & rhs) : x(rhs.x), y(rhs.y), z(rhs.z){}
Vector3(float inX, float inY, float inZ) : x(inX), y(inY), z(inZ){}
float Dot(const Vector3 & rhs) const
{
return x*rhs.x + y*rhs.y + z*rhs.z;
}
Vector3 Cross(const Vector3 & rhs) const
{
const Vector3 & u = *this;
const Vector3 & v = rhs;
float i = u.y*v.z - u.z*v.y;
float j = u.z*v.x - u.x*v.z;
float k = u.x*v.y - u.y*v.x;
return Vector3(i,j,k);
}
bool IsZero() const
{
return ::IsZero(x) && ::IsZero(y) && ::IsZero(z);
}
float Length2() const
{
return this->Dot(*this);
}
float Length() const
{
return sqrt(Length2());
}
const Vector3 & SafeNormalize()
{
float l = Length();
if(!::IsZero(l))
{
x /= l;
y /= l;
z /= l;
}
return *this;
}
const Vector3 & Normalize()
{
float l = Length();
x /= l;
y /= l;
z /= l;
return *this;
}
Vector3 operator*(const Vector3 & rhs) const
{
return Vector3(x*rhs.x, y*rhs.y, z*rhs.z);
}
const Vector3 & operator*=(const Vector3 & rhs)
{
x *= rhs.x;
y *= rhs.y;
z *= rhs.z;
return *this;
}
Vector3 operator+(const Vector3 & rhs) const
{
return Vector3(x+rhs.x, y+rhs.y, z+rhs.z);
}
const Vector3 & operator+=(const Vector3 & rhs)
{
x+= rhs.x;
y+= rhs.y;
z+= rhs.z;
return *this;
}
Vector3 operator-(const Vector3 & rhs) const
{
return Vector3(x-rhs.x, y-rhs.y, z-rhs.z);
}
const Vector3 & operator-=(const Vector3 & rhs)
{
x-= rhs.x;
y-= rhs.y;
z-= rhs.z;
return *this;
}
const Vector3 & operator=(const Vector3 & rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
return *this;
}
Vector3 operator*(float k) const
{
return Vector3(x*k, y*k, z*k);
}
const Vector3 & operator*=(float k)
{
x *= k;
y *= k;
z *= k;
return *this;
}
const Vector3 operator/(float k)
{
return Vector3(x/k, y/k, z/k);
}
const Vector3 & operator/=(float k)
{
x /= k;
y /= k;
z /= k;
return *this;
}
static Vector3 Zero;
};
inline Vector3 operator*(float k, const Vector3 & rhs)
{
return rhs.operator*(k);
}
std::ostream & operator<<(std::ostream & o, const Vector3 & rhs);
#endif