summaryrefslogtreecommitdiff
path: root/common/algebra.h
blob: 76aaab283ede1040e6c515d0336c4dfa441c918d (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef _ALGEBRA_H_
#define _ALGEBRA_H_

#include <math.h>

//
// Simple math library and algebra functions.
//
// Everything is based on the Float4 class, so changing that class should be enough
// to add support for compiler specific math intrinsics. For now only the reference
// implementation is supported.
//
// TODO: Add SSE support.
//

// Classes defined in this file:
class Float4;
class Point3;
class Vector3;
class Matrix33;
class Matrix44;

// ============================================================================
// Float4 class.

class Float4
{
public:
	// Constructors.
	inline Float4() { };
	inline explicit Float4(const float _x, const float _y, const float _z)
		: x(_x), y(_y), z(_z) { };
	inline explicit Float4(const float _x, const float _y, const float _z, const float _w)
		: x(_x), y(_y), z(_z), w(_w) { };

	// Get/Set functions.
	inline float GetX() const { return x; };
	inline float GetY() const { return y; };
	inline float GetZ() const { return z; };
	inline float GetW() const { return w; };
	inline void SetX(const float _x) { x = _x; };
	inline void SetY(const float _y) { y = _y; };
	inline void SetZ(const float _z) { z = _z; };
	inline void SetW(const float _w) { w = _w; };

	// Comparison.
	friend inline bool operator==(const Float4& a, const Float4& b)
	{ return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w); };

	friend inline bool Compare3(const Float4& a, const Float4& b)
	{ return (a.x == b.x) && (a.y == b.y) && (a.z == b.z); };

	// Math operations.
	friend inline Float4 operator+(const Float4& a, const Float4& b)
	{ return Float4(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); };

	friend inline Float4 operator-(const Float4& a, const Float4& b)
	{ return Float4(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); };

	friend inline Float4 operator*(const Float4& a, float f)
	{ return Float4(a.x*f, a.y*f, a.z*f, a.w*f); };

	friend inline Float4 operator*(const Float4& a, const Float4& b)
	{ return Float4(a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w); };

	friend inline Float4 operator/(const Float4& a, float f)
	{ return Float4(a.x/f, a.y/f, a.z/f, a.w/f); };

	// Dot product.
	friend inline float Dot3(const Float4& a, const Float4& b)
	{ return a.x*b.x + a.y*b.y + a.z*b.z; };

	// Cross product.
	friend inline Float4 Cross3(const Float4& a, const Float4& b)
	{ return Float4(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); };

	// Other functions.
	inline float Length3() const
	{ return sqrtf(Dot3(*this, *this)); };

protected:
	float x, y, z, w;
};


// ============================================================================
// 3D Point class.

class Point3
{
public:
	// Constructors.
	inline Point3() { };
	inline explicit Point3(const Float4& _v)
		: m_Value(_v) { };
	inline explicit Point3(const float _x, const float _y, const float _z)
		: m_Value(_x, _y, _z) { };

	// Get/Set functions.
	inline const Float4& GetValue() const { return m_Value; };
	inline float GetX() const { return m_Value.GetX(); };
	inline float GetY() const { return m_Value.GetY(); };
	inline float GetZ() const { return m_Value.GetZ(); };
	inline void SetX(const float _x) { m_Value.SetX(_x); };
	inline void SetY(const float _y) { m_Value.SetY(_y); };
	inline void SetZ(const float _z) { m_Value.SetZ(_z); };

	// Math operations.
	template<typename T> inline Point3& operator+=(const T& a) { return *this = *this + a; }
	template<typename T> inline Point3& operator-=(const T& a) { return *this = *this - a; }
	template<typename T> inline Point3& operator/=(const T& a) { return *this = *this / a; }
	template<typename T> inline Point3& operator*=(const T& a) { return *this = *this * a; }

protected:
	Float4 m_Value;
};


// ============================================================================
// 3D Vector class.

class Vector3
{
public:
	inline Vector3()
		{ };
	inline explicit Vector3(const Float4& _v)
		: m_Value(_v) { };
	inline explicit Vector3(const float _x, const float _y, const float _z)
		: m_Value(_x, _y, _z) { };

	// Get/Set functions.
	inline const Float4& GetValue() const { return m_Value; };
	inline float GetX() const { return m_Value.GetX(); };
	inline float GetY() const { return m_Value.GetY(); };
	inline float GetZ() const { return m_Value.GetZ(); };
	inline void SetX(const float _x) { m_Value.SetX(_x); };
	inline void SetY(const float _y) { m_Value.SetY(_y); };
	inline void SetZ(const float _z) { m_Value.SetZ(_z); };

	// Math operations.
	template<typename T> inline Vector3& operator+=(const T& a) { return *this = *this + a; }
	template<typename T> inline Vector3& operator-=(const T& a) { return *this = *this - a; }
	template<typename T> inline Vector3& operator/=(const T& a) { return *this = *this / a; }
	template<typename T> inline Vector3& operator*=(const T& a) { return *this = *this * a; }

	// Other functions.
	inline float Length() const
	{ return m_Value.Length3(); };

	inline float LengthSquared() const
	{ return Dot3(m_Value, m_Value); };

protected:
	Float4 m_Value;
};


// ============================================================================
// 3x3 Matrix class.

class Matrix33
{
public:

protected:
};


// ============================================================================
// 4x4 Matrix class.

class Matrix44
{
public:

protected:
};


// ============================================================================
// Operators.

// Comparison.
inline bool operator==(const Point3& a, const Point3& b)
{ return Compare3(a.GetValue(),  b.GetValue()); };

inline bool operator==(const Vector3& a, const Vector3& b)
{ return Compare3(a.GetValue(),  b.GetValue()); };

// Multiply by a scalar.
inline Vector3 operator*(const Vector3& a, float f)
{ return Vector3(a.GetValue()*f); };

inline Vector3 operator*(float f, const Vector3& a)
{ return Vector3(a.GetValue()*f); };

inline Point3 operator*(const Point3& a, float f)
{ return Point3(a.GetValue()*f); };

inline Point3 operator*(float f, const Point3& a)
{ return Point3(a.GetValue()*f); };

// Divide by a scalar.
inline Vector3 operator/(const Vector3& a, float f)
{ return Vector3(a.GetValue()/f); };

inline Vector3 operator/(float f, const Vector3& a)
{ return Vector3(a.GetValue()/f); };

inline Point3 operator/(const Point3& a, float f)
{ return Point3(a.GetValue()/f); };

inline Point3 operator/(float f, const Point3& a)
{ return Point3(a.GetValue()/f); };

// Add vectors/points (return a point if either is a point).
inline Point3 operator+(const Point3& a, const Point3& b)
{ return Point3(a.GetValue()+b.GetValue()); };

inline Point3 operator+(const Point3& a, const Vector3& b)
{ return Point3(a.GetValue()+b.GetValue()); };

inline Point3 operator+(const Vector3& a, const Point3& b)
{ return Point3(a.GetValue()+b.GetValue()); };

inline Vector3 operator+(const Vector3& a, const Vector3& b)
{ return Vector3(a.GetValue()+b.GetValue()); };

// Subtract vectors/points (return a vector if both arguments are the same type).
inline Vector3 operator-(const Point3& a, const Point3& b)
{ return Vector3(a.GetValue()-b.GetValue()); };

inline Point3 operator-(const Point3& a, const Vector3& b)
{ return Point3(a.GetValue()-b.GetValue()); };

inline Point3 operator-(const Vector3& a, const Point3& b)
{ return Point3(a.GetValue()-b.GetValue()); };

inline Vector3 operator-(const Vector3& a, const Vector3& b)
{ return Vector3(a.GetValue()-b.GetValue()); };

// Dot product.
inline float Dot3(const Vector3& a, const Vector3& b)
{ return Dot3(a.GetValue(), b.GetValue()); };


// ============================================================================
// Linear Algebra functions.

float DistancePointSegmentSquared(const Point3& Pt, const Point3& SegStart, const Point3& SegEnd);

#endif