-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatyp_Vector4.h
213 lines (171 loc) · 3.87 KB
/
atyp_Vector4.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#pragma once
#include "atyp_Vector2.h"
#include "atyp_Vector4.h"
#include <memory>
class Vector4
{
public:
enum class position{
x,
y,
z,
w
};
union{
struct {
float x, y, z, w;
};
float data[4];
};
static Vector4 one() {
return Vector4(1.0f, 1.0f, 1.0f, 1.0f);
}
static Vector4 zero() {
return Vector4(0.0, 0.0, 0.0, 0.0f);
}
static Vector4 right() {
return Vector4(1.0f, 0.0, 0.0, 0.0f);
}
static Vector4 up() {
return Vector4(0.0f, 1.0f, 0.0f, 0.0f);
}
static Vector4 forward() {
return Vector4(0.0, 0.0, 1.0f, 0.0f);
}
static float dot(const Vector4& a, const Vector4& b) {
return (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w);
}
static Vector4 clamp(const Vector4& value, const Vector4& min, const Vector4& max){
Vector4 result;
result.x = (value.x < min.x ? min.x : (value.x > max.x ? max.x : value.x));
result.y = (value.y < min.y ? min.y : (value.y > max.y ? max.y : value.y));
result.z = (value.z < min.z ? min.z : (value.z > max.z ? max.z : value.z));
result.w = (value.w < min.w ? min.w : (value.w > max.w ? max.w : value.w));
return result;
}
static Vector4 Abs(const Vector4& value){
return Vector4(abs(value.x), abs(value.y), abs(value.z), abs(value.w));
}
Vector4() {
x = y = z = w = 0;
}
Vector4(const Vector2& v, float z, float w) {
x = v.x;
y = v.y;
this->z = z;
this->w = w;
}
Vector4(const Vector3& v, float w) {
x = v.x;
y = v.y;
z = v.z;
this->w = w;
}
Vector4(float a_x, float a_y, float a_z, float a_w) {
x = a_x;
y = a_y;
z = a_z;
w = a_w;
}
#ifdef _CSTDIO_
void Print() const{
printf("%.2f, %.2f, %.2f, %.2f\n", x, y, z, w);
}
#endif
Vector4 copy() const {
return Vector4(x, y, z, w);
}
float magnitude() const {
return sqrtf(x * x + y * y + z * z + w + w);
}
float magnitudeSqr() const {
return x * x + y * y + z * z + w + w;
}
Vector4& normalise() {
float mod = magnitude();
if(mod == 0.0)return *this;
x /= mod;
y /= mod;
z /= mod;
w /= mod;
return *this;
}
Vector4 normalise() const {
float mod = magnitude();
if(mod == 0.0)return *this;
return Vector4(x / mod, y / mod, z / mod, w / mod);
}
float dot(const Vector4& rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w;
}
Vector4 operator+(const Vector4& rhs) const {
return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
}
Vector4 operator-(const Vector4& rhs) const {
return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
}
Vector4 operator*(float rhs) const {
return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
}
Vector4 operator/(float rhs) const {
return Vector4(x / rhs, y / rhs, z / rhs, w / rhs);
}
Vector4& operator+=(const Vector4& rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
w += rhs.w;
return *this;
}
Vector4& operator-=(const Vector4& rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
w -= rhs.w;
return *this;
}
Vector4& operator*=(float rhs) {
x *= rhs;
y *= rhs;
z *= rhs;
w *= rhs;
return *this;
}
Vector4& operator/=(float rhs) {
x /= rhs;
y /= rhs;
z /= rhs;
w /= rhs;
return *this;
}
bool operator==(const Vector4& rhs) const{
return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
}
bool operator!=(const Vector4& rhs) const{
return !(*this == rhs);
}
float& operator[](int index) const {
return *((float*)this + index);
}
operator float* () const {
return (float*)this;
}
operator Vector3() const {
return Vector3(x, y, z);
}
operator Vector2() const {
return Vector2(x, y);
}
Vector4 cross(const Vector4& rhs) const {
Vector4 result(*this);
*((Vector4*)& result) = (*((Vector4*)& result)).cross(*((Vector4*)& rhs));
result.w = 0;
return result;
}
Vector4 operator-() const {
return Vector4(-x, -y, -z, -w);
}
};
inline Vector4 operator*(float lhs, Vector4 rhs) {
return Vector4(rhs.x * lhs, rhs.y * lhs, rhs.z * lhs, rhs.w * lhs);
}