Starshatter_Open
Open source Starshatter engine
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Geometry.h
Go to the documentation of this file.
1
/* Project nGenEx
2
Destroyer Studios LLC
3
Copyright © 1997-2004. All Rights Reserved.
4
5
SUBSYSTEM: nGenEx.lib
6
FILE: Geometry.h
7
AUTHOR: John DiCamillo
8
9
10
OVERVIEW
11
========
12
Geometric classes: Rect, Vec3, Point, Matrix, Plane
13
*/
14
15
#ifndef Geometry_h
16
#define Geometry_h
17
18
#include "
Types.h
"
19
20
// +--------------------------------------------------------------------+
21
22
struct
Rect
;
23
struct
Insets
;
24
struct
Matrix
;
25
struct
Vec3
;
26
struct
Point
;
27
struct
Quaternion
;
28
struct
Plane
;
29
30
const
double
PI
= 3.14159265358979323846;
31
const
double
DEGREES
= (
PI
/180);
32
33
// +--------------------------------------------------------------------+
34
35
struct
Rect
36
{
37
static
const
char
*
TYPENAME
() {
return
"Rect"
; }
38
39
Rect
() :
x
(0),
y
(0),
w
(0),
h
(0) { }
40
Rect
(
int
ix,
int
iy,
int
iw,
int
ih) :
x
(ix),
y
(iy),
w
(iw),
h
(ih) { }
41
42
int
operator==
(
const
Rect
& r)
const
{
return
x
==r.
x
&&
y
==r.
y
&&
w
==r.
w
&&
h
==r.
h
; }
43
int
operator!=
(
const
Rect
& r)
const
{
return
x
!=r.
x
||
y
!=r.
y
||
w
!=r.
w
||
h
!=r.
h
; }
44
45
void
Inflate
(
int
dw,
int
dh);
46
void
Deflate
(
int
dw,
int
dh);
47
void
Inset
(
int
left,
int
right,
int
top,
int
bottom);
48
int
Contains
(
int
x
,
int
y
)
const
;
49
50
int
x
,
y
,
w
,
h
;
51
};
52
53
// +--------------------------------------------------------------------+
54
55
struct
Insets
56
{
57
Insets
() :
left
(0),
right
(0),
top
(0),
bottom
(0) { }
58
Insets
(WORD l, WORD r, WORD t, WORD b) :
left
(l),
right
(r),
top
(t),
bottom
(b) { }
59
60
WORD
left
;
61
WORD
right
;
62
WORD
top
;
63
WORD
bottom
;
64
};
65
66
// +--------------------------------------------------------------------+
67
68
struct
Matrix
69
{
70
static
const
char
*
TYPENAME
() {
return
"Matrix"
; }
71
72
Matrix
();
73
Matrix
(
const
Matrix
& m);
74
Matrix
(
const
Point
& vrt,
const
Point
& vup,
const
Point
& vpn);
75
76
Matrix
&
operator =
(
const
Matrix
& m);
77
Matrix
&
operator *=
(
const
Matrix
& m);
78
79
double
operator()
(
int
i,
int
j)
const
{
return
elem
[i][j]; }
80
double
&
operator()
(
int
i,
int
j) {
return
elem
[i][j]; }
81
82
void
Identity
();
83
void
Transpose
();
84
void
Rotate
(
double
roll,
double
pitch,
double
yaw);
85
void
Roll
(
double
roll);
86
void
Pitch
(
double
pitch);
87
void
Yaw
(
double
yaw);
88
void
ComputeEulerAngles
(
double
& roll,
double
& pitch,
double
& yaw)
const
;
89
90
double
Cofactor
(
int
i,
int
j)
const
;
91
void
Invert
();
92
93
Matrix
Inverse
()
const
{
94
Matrix
result(*
this
);
95
result.
Invert
();
96
return
result;
97
}
98
99
Matrix
operator*
(
const
Matrix
& m)
const
;
100
Point
operator*
(
const
Point
& p)
const
;
101
Vec3
operator*
(
const
Vec3
& v)
const
;
102
103
double
elem
[3][3];
104
105
private
:
106
Matrix
(
int
no_init) { }
107
};
108
109
// +--------------------------------------------------------------------+
110
111
struct
Vec2
112
{
113
static
const
char
*
TYPENAME
() {
return
"Vec2"
; }
114
115
Vec2
() { }
116
Vec2
(
int
ix,
int
iy) :
x
((float) ix),
y
((float) iy) { }
117
Vec2
(
float
ix,
float
iy) :
x
(ix),
y
(iy) { }
118
Vec2
(
double
ix,
double
iy) :
x
((float) ix),
y
((float) iy) { }
119
120
operator
void
*()
const
{
return
(
void
*) (
x
||
y
); }
121
int
operator==
(
const
Vec2
& p)
const
{
return
x
==p.
x
&&
y
==p.
y
; }
122
int
operator!=
(
const
Vec2
& p)
const
{
return
x
!=p.
x
||
y
!=p.
y
; }
123
Vec2
operator+
(
const
Vec2
& p)
const
{
return
Vec2
(
x
+p.
x
,
y
+p.
y
); }
124
Vec2
operator-
(
const
Vec2
& p)
const
{
return
Vec2
(
x
-p.
x
,
y
-p.
y
); }
125
Vec2
operator-
()
const
{
return
Vec2
(-
x
, -
y
); }
126
Vec2
operator*
(
float
s)
const
{
return
Vec2
(
x
*s,
y
*s); }
127
Vec2
operator/
(
float
s)
const
{
return
Vec2
(
x
/s,
y
/s); }
128
float
operator*
(
const
Vec2
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
); }
129
130
Vec2
&
operator=
(
const
Vec2
& p) {
x
=p.
x
;
y
=p.
y
;
return
*
this
; }
131
Vec2
&
operator+=
(
const
Vec2
& p) {
x
+=p.
x
;
y
+=p.
y
;
return
*
this
; }
132
Vec2
&
operator-=
(
const
Vec2
& p) {
x
-=p.
x
;
y
-=p.
y
;
return
*
this
; }
133
Vec2
&
operator*=
(
float
s) {
x
*=s;
y
*=s;
return
*
this
; }
134
Vec2
&
operator/=
(
float
s) {
x
/=s;
y
/=s;
return
*
this
; }
135
136
float
length
()
const
{
return
(
float
) sqrt(
x
*
x
+
y
*
y
); }
137
float
Normalize
();
138
float
dot
(
const
Vec2
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
); }
139
Vec2
normal
()
const
{
return
Vec2
(-
y
,
x
); }
140
141
float
x
,
y
;
142
};
143
144
// +--------------------------------------------------------------------+
145
146
struct
Vec3
147
{
148
static
const
char
*
TYPENAME
() {
return
"Vec3"
; }
149
150
Vec3
() { }
151
Vec3
(
int
ix,
int
iy,
int
iz) :
x
((float) ix),
y
((float) iy),
z
((float) iz) { }
152
Vec3
(
float
ix,
float
iy,
float
iz) :
x
(ix),
y
(iy),
z
(iz) { }
153
Vec3
(
double
ix,
double
iy,
double
iz) :
x
((float) ix),
y
((float) iy),
z
((float) iz) { }
154
155
operator
void
*()
const
{
return
(
void
*) (
x
||
y
||
z
); }
156
int
operator==
(
const
Vec3
& p)
const
{
return
x
==p.
x
&&
y
==p.
y
&&
z
==p.
z
; }
157
int
operator!=
(
const
Vec3
& p)
const
{
return
x
!=p.
x
||
y
!=p.
y
||
z
!=p.
z
; }
158
Vec3
operator+
(
const
Vec3
& p)
const
{
return
Vec3
(
x
+p.
x
,
y
+p.
y
,
z
+p.
z
); }
159
Vec3
operator-
(
const
Vec3
& p)
const
{
return
Vec3
(
x
-p.
x
,
y
-p.
y
,
z
-p.
z
); }
160
Vec3
operator-
()
const
{
return
Vec3
(-
x
, -
y
, -
z
); }
161
Vec3
operator*
(
float
s)
const
{
return
Vec3
(
x
*s,
y
*s,
z
*s); }
162
Vec3
operator/
(
float
s)
const
{
return
Vec3
(
x
/s,
y
/s,
z
/s); }
163
float
operator*
(
const
Vec3
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
+
z
*p.
z
); }
164
Vec3
operator*
(
const
Matrix
&)
const
;
165
166
Vec3
&
operator=
(
const
Vec3
& p) {
x
=p.
x
;
y
=p.
y
;
z
=p.
z
;
return
*
this
; }
167
Vec3
&
operator+=
(
const
Vec3
& p) {
x
+=p.
x
;
y
+=p.
y
;
z
+=p.
z
;
return
*
this
; }
168
Vec3
&
operator-=
(
const
Vec3
& p) {
x
-=p.
x
;
y
-=p.
y
;
z
-=p.
z
;
return
*
this
; }
169
Vec3
&
operator*=
(
float
s) {
x
*=s;
y
*=s;
z
*=s;
return
*
this
; }
170
Vec3
&
operator/=
(
float
s) {
x
/=s;
y
/=s;
z
/=s;
return
*
this
; }
171
172
void
SwapYZ
() {
float
t =
y
;
y
=
z
;
z
= t; }
173
float
length
()
const
{
return
(
float
) sqrt(
x
*
x
+
y
*
y
+
z
*
z
); }
174
float
Normalize
();
175
176
float
dot
(
const
Vec3
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
+
z
*p.
z
); }
177
Vec3
cross
(
const
Vec3
& v)
const
{
return
Vec3
((
y
*v.
z
) - (
z
*v.
y
),
178
(
z
*v.
x
) - (
x
*v.
z
),
179
(
x
*v.
y
) - (
y
*v.
x
)); }
180
181
float
x
,
y
,
z
;
182
};
183
184
double
ClosestApproachTime
(
const
Vec3
& loc1,
const
Vec3
& vel1,
185
const
Vec3
& loc2,
const
Vec3
& vel2);
186
187
// +--------------------------------------------------------------------+
188
189
struct
Point
190
{
191
static
const
char
*
TYPENAME
() {
return
"Point"
; }
192
193
Point
() :
x
(0),
y
(0),
z
(0) { }
194
Point
(
double
ix,
double
iy,
double
iz) :
x
(ix),
y
(iy),
z
(iz) { }
195
Point
(
const
Point
& p) :
x
(p.
x
),
y
(p.
y
),
z
(p.
z
) { }
196
Point
(
const
Vec3
& v) :
x
(v.
x
),
y
(v.
y
),
z
(v.
z
) { }
197
198
operator
Vec3
()
const
{
return
Vec3
((
float
)
x
, (
float
)
y
, (
float
)
z
); }
199
200
operator
void
*()
const
{
return
(
void
*) (
x
||
y
||
z
); }
201
int
operator==
(
const
Point
& p)
const
{
return
x
==p.
x
&&
y
==p.
y
&&
z
==p.
z
; }
202
int
operator!=
(
const
Point
& p)
const
{
return
x
!=p.
x
||
y
!=p.
y
||
z
!=p.
z
; }
203
Point
operator+
(
const
Point
& p)
const
{
return
Point
(
x
+p.
x
,
y
+p.
y
,
z
+p.
z
); }
204
Point
operator-
(
const
Point
& p)
const
{
return
Point
(
x
-p.
x
,
y
-p.
y
,
z
-p.
z
); }
205
Point
operator-
()
const
{
return
Point
(-
x
, -
y
, -
z
); }
206
Point
operator*
(
double
s)
const
{
return
Point
(
x
*s,
y
*s,
z
*s); }
207
Point
operator/
(
double
s)
const
{
return
Point
(
x
/s,
y
/s,
z
/s); }
208
double
operator*
(
const
Point
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
+
z
*p.
z
); }
209
Point
operator*
(
const
Matrix
& m)
const
;
210
211
Point
&
operator=
(
const
Point
& p) {
x
=p.
x
;
y
=p.
y
;
z
=p.
z
;
return
*
this
; }
212
Point
&
operator+=
(
const
Point
& p) {
x
+=p.
x
;
y
+=p.
y
;
z
+=p.
z
;
return
*
this
; }
213
Point
&
operator-=
(
const
Point
& p) {
x
-=p.
x
;
y
-=p.
y
;
z
-=p.
z
;
return
*
this
; }
214
Point
&
operator*=
(
double
s) {
x
*=s;
y
*=s;
z
*=s;
return
*
this
; }
215
Point
&
operator/=
(
double
s) {
x
/=s;
y
/=s;
z
/=s;
return
*
this
; }
216
217
double
length
()
const
{
return
sqrt(
x
*
x
+
y
*
y
+
z
*
z
); }
218
double
Normalize
();
219
void
SwapYZ
() {
double
t =
y
;
y
=
z
;
z
= t; }
220
Point
OtherHand
()
const
{
return
Point
(-
x
,
z
,
y
); }
221
222
void
SetElement
(
int
i,
double
v);
223
224
double
dot
(
const
Point
& p)
const
{
return
(
x
*p.
x
+
y
*p.
y
+
z
*p.
z
); }
225
Point
cross
(
const
Point
& p)
const
{
return
Point
((
y
*p.
z
) - (
z
*p.
y
),
226
(
z
*p.
x
) - (
x
*p.
z
),
227
(
x
*p.
y
) - (
y
*p.
x
)); }
228
229
double
x
,
y
,
z
;
230
};
231
232
double
ClosestApproachTime
(
const
Point
& loc1,
const
Point
& vel1,
233
const
Point
& loc2,
const
Point
& vel2);
234
235
// +--------------------------------------------------------------------+
236
237
struct
Quaternion
238
{
239
static
const
char
*
TYPENAME
() {
return
"Quaternion"
; }
240
241
Quaternion
() :
x
(0),
y
(0),
z
(0),
w
(0) { }
242
Quaternion
(
double
ix,
243
double
iy,
244
double
iz,
245
double
iw) :
x
(ix),
y
(iy),
z
(iz),
w
(iw) { }
246
Quaternion
(
const
Quaternion
& q) :
x
(q.
x
),
y
(q.
y
),
z
(q.
z
),
w
(q.
w
) { }
247
248
int
operator==
(
const
Quaternion
& q)
const
{
return
x
==q.
x
&&
y
==q.
y
&&
z
==q.
z
&&
w
==q.
w
; }
249
int
operator!=
(
const
Quaternion
& q)
const
{
return
x
!=q.
x
||
y
!=q.
y
||
z
!=q.
z
||
w
!=q.
w
; }
250
251
Quaternion
operator+
(
const
Quaternion
& q)
const
{
return
Quaternion
(
x
+q.
x
,
y
+q.
y
,
z
+q.
z
,
w
+q.
w
); }
252
Quaternion
operator-
(
const
Quaternion
& q)
const
{
return
Quaternion
(
x
-q.
x
,
y
-q.
y
,
z
-q.
z
,
w
-q.
w
); }
253
Quaternion
operator-
()
const
{
return
Quaternion
(-
x
, -
y
, -
z
, -
w
); }
254
Quaternion
operator*
(
double
s)
const
{
return
Quaternion
(
x
*s,
y
*s,
z
*s,
w
*s); }
255
Quaternion
operator/
(
double
s)
const
{
return
Quaternion
(
x
/s,
y
/s,
z
/s,
w
/s); }
256
257
Quaternion
&
operator=
(
const
Quaternion
& q) {
x
=q.
x
;
y
=q.
y
;
z
=q.
z
;
w
=q.
w
;
return
*
this
; }
258
Quaternion
&
operator+=
(
const
Quaternion
& q) {
x
+=q.
x
;
y
+=q.
y
;
z
+=q.
z
;
w
+=q.
w
;
return
*
this
; }
259
Quaternion
&
operator-=
(
const
Quaternion
& q) {
x
-=q.
x
;
y
-=q.
y
;
z
-=q.
z
;
w
-=q.
w
;
return
*
this
; }
260
Quaternion
&
operator*=
(
double
s) {
x
*=s;
y
*=s;
z
*=s;
w
*=s;
return
*
this
; }
261
Quaternion
&
operator/=
(
double
s) {
x
/=s;
y
/=s;
z
/=s;
w
/=s;
return
*
this
; }
262
263
double
length
()
const
{
return
sqrt(
x
*
x
+
y
*
y
+
z
*
z
+
w
*
w
); }
264
double
Normalize
();
265
266
double
x
,
y
,
z
,
w
;
267
};
268
269
// +--------------------------------------------------------------------+
270
271
struct
Plane
272
{
273
static
const
char
*
TYPENAME
() {
return
"Plane"
; }
274
275
Plane
();
276
Plane
(
const
Point
& p0,
const
Point
& p1,
const
Point
& p2);
277
Plane
(
const
Vec3
& v0,
const
Vec3
& v1,
const
Vec3
& v2);
278
279
void
Rotate
(
const
Vec3
& v0,
const
Matrix
& m);
280
void
Translate
(
const
Vec3
& v0);
281
282
float
distance
;
283
Vec3
normal
;
284
};
285
286
// +--------------------------------------------------------------------+
287
288
double
DotProduct
(
const
Point
& a,
const
Point
& b);
289
void
CrossProduct
(
const
Point
& a,
const
Point
& b,
Point
& out);
290
void
MConcat
(
double
in1[3][3],
double
in2[3][3],
double
out[3][3]);
291
292
// +--------------------------------------------------------------------+
293
294
int
lines_intersect
(
295
/* 1st line segment */
double
x1,
double
y1,
double
x2,
double
y2,
296
/* 2nd line segment */
double
x3,
double
y3,
double
x4,
double
y4,
297
/* intersect point */
double
& x,
double
& y);
298
299
// +--------------------------------------------------------------------+
300
301
#endif Geometry_h
302
nGenEx
Geometry.h
Generated on Tue Jun 5 2012 20:46:23 for Starshatter_Open by
1.8.1