CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/lin/vec3.h
Views: 1401
1
#ifndef _MATH_LIN_VEC3
2
#define _MATH_LIN_VEC3
3
4
#include <math.h>
5
#include <string.h> // memset
6
7
namespace Lin {
8
9
class Matrix4x4;
10
11
// Hm, doesn't belong in this file.
12
class Vec4 {
13
public:
14
float x,y,z,w;
15
Vec4(){}
16
Vec4(float a, float b, float c, float d) {x=a;y=b;z=c;w=d;}
17
};
18
19
class Vec3 {
20
public:
21
float x,y,z;
22
23
Vec3() { }
24
explicit Vec3(float f) {x=y=z=f;}
25
26
float operator [] (int i) const { return (&x)[i]; }
27
float &operator [] (int i) { return (&x)[i]; }
28
29
Vec3(const float _x, const float _y, const float _z) {
30
x=_x; y=_y; z=_z;
31
}
32
void Set(float _x, float _y, float _z) {
33
x=_x; y=_y; z=_z;
34
}
35
Vec3 operator + (const Vec3 &other) const {
36
return Vec3(x+other.x, y+other.y, z+other.z);
37
}
38
void operator += (const Vec3 &other) {
39
x+=other.x; y+=other.y; z+=other.z;
40
}
41
Vec3 operator -(const Vec3 &v) const {
42
return Vec3(x-v.x,y-v.y,z-v.z);
43
}
44
void operator -= (const Vec3 &other)
45
{
46
x-=other.x; y-=other.y; z-=other.z;
47
}
48
Vec3 operator -() const {
49
return Vec3(-x,-y,-z);
50
}
51
52
Vec3 operator * (const float f) const {
53
return Vec3(x*f,y*f,z*f);
54
}
55
Vec3 operator / (const float f) const {
56
float invf = (1.0f/f);
57
return Vec3(x*invf,y*invf,z*invf);
58
}
59
void operator /= (const float f)
60
{
61
*this = *this / f;
62
}
63
float operator * (const Vec3 &other) const {
64
return x*other.x + y*other.y + z*other.z;
65
}
66
void operator *= (const float f) {
67
*this = *this * f;
68
}
69
void scaleBy(const Vec3 &other) {
70
x *= other.x; y *= other.y; z *= other.z;
71
}
72
Vec3 scaledBy(const Vec3 &other) const {
73
return Vec3(x*other.x, y*other.y, z*other.z);
74
}
75
Vec3 scaledByInv(const Vec3 &other) const {
76
return Vec3(x/other.x, y/other.y, z/other.z);
77
}
78
Vec3 operator *(const Matrix4x4 &m) const;
79
void operator *=(const Matrix4x4 &m) {
80
*this = *this * m;
81
}
82
Vec3 rotatedBy(const Matrix4x4 &m) const;
83
Vec3 operator %(const Vec3 &v) const {
84
return Vec3(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
85
}
86
float length2() const {
87
return x*x + y*y + z*z;
88
}
89
float length() const {
90
return sqrtf(length2());
91
}
92
void setLength(const float l) {
93
(*this) *= l/length();
94
}
95
Vec3 withLength(const float l) const {
96
return (*this) * l / length();
97
}
98
float distance2To(const Vec3 &other) const {
99
return Vec3(other-(*this)).length2();
100
}
101
Vec3 normalized() const {
102
return (*this) / length();
103
}
104
float normalize() { //returns the previous length, is often useful
105
float len = length();
106
(*this) = (*this)/len;
107
return len;
108
}
109
bool operator == (const Vec3 &other) const {
110
if (x==other.x && y==other.y && z==other.z)
111
return true;
112
else
113
return false;
114
}
115
Vec3 lerp(const Vec3 &other, const float t) const {
116
return (*this)*(1-t) + other*t;
117
}
118
void setZero() {
119
memset((void *)this,0,sizeof(float)*3);
120
}
121
};
122
123
inline Vec3 operator * (const float f, const Vec3 &v) {return v * f;}
124
125
// In new code, prefer these to the operators.
126
127
inline float dot(const Vec3 &a, const Vec3 &b) {
128
return a.x * b.x + a.y * b.y + a.z * b.z;
129
}
130
131
inline Vec3 cross(const Vec3 &a, const Vec3 &b) {
132
return a % b;
133
}
134
135
class AABBox {
136
public:
137
Vec3 min;
138
Vec3 max;
139
};
140
141
} // namespace Lin
142
143
#endif // _MATH_LIN_VEC3
144
145