-
Notifications
You must be signed in to change notification settings - Fork 9
/
math.h
243 lines (184 loc) · 4.39 KB
/
math.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include <iostream>
#include <cmath>
using namespace std;
////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////
const double epsilon = 1e-5; // Used to compensate for floating point inaccuracy.
const double scale = 128; // Scale
////////////////////////////////////////////////////////////////////
// Name: Vector3
// Description: 3D vector class with all operators implemented.
// "double" is used for greater accuracy.
////////////////////////////////////////////////////////////////////
class Vector3
{
public:
double x, y, z;
const bool operator == ( const Vector3 &arg_ ) const
{
if ( ( x == arg_.x ) && ( y == arg_.y ) && ( z == arg_.z ) )
{
return true;
}
return false;
}
const Vector3 operator - ( const Vector3 &arg_ ) const
{
Vector3 temp;
temp.x = x - arg_.x;
temp.y = y - arg_.y;
temp.z = z - arg_.z;
return temp;
}
const Vector3 operator + ( const Vector3 &arg_ ) const
{
Vector3 temp;
temp.x = x + arg_.x;
temp.y = y + arg_.y;
temp.z = z + arg_.z;
return temp;
}
const Vector3 operator * ( const double fArg_ ) const
{
Vector3 temp;
temp.x = x * fArg_;
temp.y = y * fArg_;
temp.z = z * fArg_;
return temp;
}
const Vector3 operator / ( const double fArg_ ) const
{
Vector3 temp;
temp.x = x / fArg_;
temp.y = y / fArg_;
temp.z = z / fArg_;
return temp;
}
const Vector3 operator - ( ) const
{
Vector3 temp;
temp.x = -x;
temp.y = -y;
temp.z = -z;
return temp;
}
const double Dot ( const Vector3 &arg_ ) const
{
return x * arg_.x + y * arg_.y + z * arg_.z;
}
const Vector3 Cross ( const Vector3 &arg_ ) const
{
Vector3 temp;
temp.x = y * arg_.z - z * arg_.y;
temp.y = z * arg_.x - x * arg_.z;
temp.z = x * arg_.y - y * arg_.x;
return temp;
}
const double Magnitude ( ) const
{
return sqrt ( x * x + y * y + z * z );
}
const double MagnitudeSquared ( ) const
{
return ( x * x + y * y + z * z );
}
void Normalize ( )
{
const double fLength = Magnitude ( );
x /= fLength;
y /= fLength;
z /= fLength;
return;
}
Vector3 ( )
{
x = 0;
y = 0;
z = 0;
}
Vector3 ( const double x, const double y, const double z )
{
this->x = x;
this->y = y;
this->z = z;
}
};
////////////////////////////////////////////////////////////////////
// Name: Plane
// Description: Plane class. "double" is used for greater accuracy.
// Follows N dot P + D = 0 equation.
////////////////////////////////////////////////////////////////////
class Plane
{
public:
Vector3 n; // Plane normal
double d; // D
enum eCP { FRONT = 0, BACK, ONPLANE };
Plane ( )
{
d = 0;
}
Plane ( const Vector3 n, const double d )
{
this->n = n;
this->d = d;
}
Plane ( const Vector3 &a, const Vector3 &b, const Vector3 &c )
{
n = ( c - b ).Cross ( a - b );
n.Normalize ( );
d = -n.Dot ( a );
}
void PointsToPlane ( const Vector3 &a, const Vector3 &b, const Vector3 &c )
{
n = ( c - b ).Cross ( a - b );
n.Normalize ( );
d = -n.Dot ( a );
}
double DistanceToPlane ( const Vector3 &v )
{
return ( n.Dot ( v ) + d );
}
eCP ClassifyPoint ( const Vector3 &v )
{
double Distance = DistanceToPlane ( v );
if ( Distance > epsilon )
{
return eCP::FRONT;
}
else if ( Distance < -epsilon )
{
return eCP::BACK;
}
return eCP::ONPLANE;
}
bool GetIntersection ( const Plane &a, const Plane &b, Vector3 &v )
{
double denom;
denom = n.Dot ( a.n.Cross ( b.n ) );
if ( fabs ( denom ) < epsilon )
{
return false;
}
v = ( ( a.n.Cross ( b.n ) ) * -d - ( b.n.Cross ( n ) ) * a.d - ( n.Cross ( a.n ) ) * b.d ) / denom;
return true;
}
bool GetIntersection ( const Vector3 &Start, const Vector3 &End, Vector3 &Intersection, double &Percentage )
{
Vector3 Direction = End - Start;
double Num, Denom;
Direction.Normalize ( );
Denom = n.Dot ( Direction );
if ( fabs ( Denom ) < epsilon )
{
return false;
}
Num = -DistanceToPlane ( Start );
Percentage = Num / Denom;
Intersection = Start + ( Direction * Percentage );
Percentage = Percentage / ( End - Start ).Magnitude ( );
return true;
}
};