1 | #ifndef _MATH_CLASS_
|
---|
2 | #define _MATH_CLASS_
|
---|
3 |
|
---|
4 | #include <math.h>
|
---|
5 |
|
---|
6 | class vector3
|
---|
7 | {
|
---|
8 | public:
|
---|
9 | vector3() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {};
|
---|
10 | vector3( float a_X, float a_Y, float a_Z ) : x( a_X ), y( a_Y ), z( a_Z ) {};
|
---|
11 | vector3(int a_X, int a_Y, int a_Z) : xint(a_X), yint(a_Y), zint(a_Z) {};
|
---|
12 | void Set( float a_X, float a_Y, float a_Z ) { x = a_X; y = a_Y; z = a_Z; }
|
---|
13 | void Normalize() { float l = 1.0f / Length(); x *= l; y *= l; z *= l; }
|
---|
14 | float Length() { return (float)sqrt( x * x + y * y + z * z ); }
|
---|
15 | float SqrLength() { return x * x + y * y + z * z; }
|
---|
16 | float Dot( vector3 a_V ) { return x * a_V.x + y * a_V.y + z * a_V.z; }
|
---|
17 | vector3 Cross( vector3 b ) { return vector3( y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x ); }
|
---|
18 | void operator += ( vector3& a_V ) { x += a_V.x; y += a_V.y; z += a_V.z; }
|
---|
19 | void operator += ( vector3* a_V ) { x += a_V->x; y += a_V->y; z += a_V->z; }
|
---|
20 | void operator -= ( vector3& a_V ) { x -= a_V.x; y -= a_V.y; z -= a_V.z; }
|
---|
21 | void operator -= ( vector3* a_V ) { x -= a_V->x; y -= a_V->y; z -= a_V->z; }
|
---|
22 | void operator *= ( float f ) { x *= f; y *= f; z *= f; }
|
---|
23 | void operator *= ( vector3& a_V ) { x *= a_V.x; y *= a_V.y; z *= a_V.z; }
|
---|
24 | void operator *= ( vector3* a_V ) { x *= a_V->x; y *= a_V->y; z *= a_V->z; }
|
---|
25 | vector3 operator- () const { return vector3( -x, -y, -z ); }
|
---|
26 | friend vector3 operator + ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); }
|
---|
27 | friend vector3 operator - ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); }
|
---|
28 | friend vector3 operator + ( const vector3& v1, vector3* v2 ) { return vector3( v1.x + v2->x, v1.y + v2->y, v1.z + v2->z ); }
|
---|
29 | friend vector3 operator - ( const vector3& v1, vector3* v2 ) { return vector3( v1.x - v2->x, v1.y - v2->y, v1.z - v2->z ); }
|
---|
30 | friend vector3 operator * ( const vector3& v, float f ) { return vector3( v.x * f, v.y * f, v.z * f ); }
|
---|
31 | friend vector3 operator * ( const vector3& v1, vector3& v2 ) { return vector3( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); }
|
---|
32 | friend vector3 operator * ( float f, const vector3& v ) { return vector3( v.x * f, v.y * f, v.z * f ); }
|
---|
33 | //Added for texture atlas purposes
|
---|
34 | void Barycenter(vector3 v1, vector3 v2, vector3 v3)
|
---|
35 | {
|
---|
36 | x = (v1.x + v2.x + v3.x) / 3;
|
---|
37 | y = (v1.y + v2.y + v3.y) / 3;
|
---|
38 | z = (v1.z + v2.z + v3.z) / 3;
|
---|
39 |
|
---|
40 | }
|
---|
41 | //Triangle Centroid calculation
|
---|
42 | void Centroid(vector3 v1, vector3 v2, vector3 v3)
|
---|
43 | {
|
---|
44 | x = (v1.x/(v1.x + v2.x + v3.x)) + (v1.y/(v1.y + v2.y + v3.y)) + (v1.z/(v1.z + v2.z + v3.z));
|
---|
45 | y = (v2.x/(v1.x + v2.x + v3.x)) + (v2.y/(v1.y + v2.y + v3.y)) + (v2.z/(v1.z + v2.z + v3.z));
|
---|
46 | z = (v3.x/(v1.x + v2.x + v3.x)) + (v3.y/(v1.y + v2.y + v3.y)) + (v3.z/(v1.z + v2.z + v3.z));
|
---|
47 |
|
---|
48 | }
|
---|
49 | //Triangle Area calculation using Heron's Formula
|
---|
50 | double TriangleArea(vector3 v1, vector3 v2, vector3 v3)
|
---|
51 | {
|
---|
52 | float a, b, c, p;
|
---|
53 | double TriArea;
|
---|
54 |
|
---|
55 | a = v1.Length();
|
---|
56 | b = v2.Length();
|
---|
57 | c = v3.Length();
|
---|
58 | p = 0.5 * (a + b + c);
|
---|
59 |
|
---|
60 | TriArea = sqrt(p * (p - a) * (p - b) * (p - c));
|
---|
61 |
|
---|
62 | return TriArea;
|
---|
63 |
|
---|
64 | }
|
---|
65 | union
|
---|
66 | {
|
---|
67 | struct { float x, y, z; };
|
---|
68 | struct { float r, g, b; };
|
---|
69 | struct { float cell[3]; };
|
---|
70 | struct { int xint, yint, zint;};
|
---|
71 | };
|
---|
72 | };
|
---|
73 |
|
---|
74 | #endif |
---|