forked from derandark/DungeonViewerAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathLib.h
229 lines (188 loc) · 4.76 KB
/
MathLib.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
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include <math.h>
#include "PackObj.h"
// #define F_EPSILON (0.0002f)
class Frame;
class Vec2D
{
public:
inline Vec2D() {
}
inline Vec2D(float _x, float _y) {
x = _x;
y = _y;
}
inline Vec2D& operator+=(const Vec2D& quant) {
x += quant.x;
y += quant.y;
return *this;
}
ULONG pack_size();
ULONG Pack(BYTE** ppData, ULONG iSize);
BOOL UnPack(BYTE** ppData, ULONG iSize);
float x, y;
};
class Vector
{
public:
inline Vector() {
}
inline Vector(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
}
inline Vector operator*(const float amount) const {
return Vector(x * amount, y * amount, z * amount);
}
inline Vector operator*(const Vector& quant) const {
return Vector(x * quant.x, y * quant.y, z * quant.z);
}
inline Vector& operator*=(const float amount) {
x *= amount;
y *= amount;
z *= amount;
return *this;
}
inline Vector& operator*=(const Vector& quant) {
x *= quant.x;
y *= quant.y;
z *= quant.z;
return *this;
}
inline Vector operator/(const float amount) const {
return Vector(x / amount, y / amount, z / amount);
}
inline Vector& operator/=(const float amount) {
x /= amount;
y /= amount;
z /= amount;
return *this;
}
inline Vector& operator/=(const Vector& quant) {
x /= quant.x;
y /= quant.y;
z /= quant.z;
return *this;
}
inline Vector operator-(const Vector& quant) const {
return Vector(x - quant.x, y - quant.y, z - quant.z);
}
inline Vector& operator-=(const Vector& quant) {
x -= quant.x;
y -= quant.y;
z -= quant.z;
return *this;
}
inline Vector operator+(const Vector& quant) const {
return Vector(x + quant.x, y + quant.y, z + quant.z);
}
inline Vector& operator+=(const Vector& quant) {
x += quant.x;
y += quant.y;
z += quant.z;
return *this;
}
inline operator const float *() const {
return &x;
}
inline operator float *() {
return &x;
}
float magnitude() const {
return (float)sqrt((x * x) + (y * y) + (z * z));
}
inline ULONG pack_size() {
return(sizeof(float) * 3);
}
inline ULONG Pack(BYTE** ppData, ULONG iSize) {
ULONG PackSize = pack_size();
if (iSize >= PackSize) {
PACK(float, x);
PACK(float, y);
PACK(float, z);
}
return PackSize;
}
inline BOOL UnPack(BYTE** ppData, ULONG iSize) {
if (iSize < pack_size())
return FALSE;
UNPACK(float, x);
UNPACK(float, y);
UNPACK(float, z);
return TRUE;
}
BOOL IsValid() const;
float dot_product(const Vector& v) const;
Vector& normalize();
BOOL normalize_check_small();
BOOL is_zero() const;
float x, y, z;
};
Vector cross_product(const Vector& v1, const Vector& v2);
// Imaginary class, either entirely inlined by Turbine or non-existant.
class Quaternion
{
public:
inline Quaternion() {
}
inline Quaternion(float _w, float _x, float _y, float _z) {
w = _w;
x = _x;
y = _y;
z = _z;
}
// Should probably be renamed. It's not the length of the quaternion!!
inline float magnitude() const {
return (float)sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
inline ULONG pack_size() {
return(sizeof(float) * 4);
}
inline ULONG Pack(BYTE** ppData, ULONG iSize) {
// The client performs no size check, but we will.
ULONG PackSize = pack_size();
if (iSize >= PackSize) {
PACK(float, w);
PACK(float, x);
PACK(float, y);
PACK(float, z);
}
return PackSize;
}
inline BOOL UnPack(BYTE** ppData, ULONG iSize) {
if (iSize < pack_size())
return FALSE;
UNPACK(float, w);
UNPACK(float, x);
UNPACK(float, y);
UNPACK(float, z);
return TRUE;
}
BOOL IsValid() const;
void normalize();
float dot_product(const Quaternion& q) const;
float w, x, y, z;
};
class Ray
{
public:
Vector m_origin;
Vector m_direction;
};
class Plane
{
public:
// Constructors
Plane();
Plane(Vector& Vect1, Vector& Vect2);
// Pack Functions
ULONG pack_size();
BOOL UnPack(BYTE** ppData, ULONG iSize);
// Math Functions
float dot_product(const Vector& point);
int which_side(const Vector& point, float near_dist);
BOOL compute_time_of_intersection(const Ray& ray, float *time);
Vector m_normal;
float m_dist;
};