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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd. All rights reserved.
//
// File: geometry.h
//
// Description: Some linear algebra/geometry stuff mostly used in traffic
// Also contains some useful structures.
//
// History: 09/09/2002 + Created -- Dusit Eakkachaichanvet
//
//=============================================================================
#ifndef GEOMETRY_H
#define GEOMETRY_H
// *************************
// 2D & 3D GEOMETRY HELPERS
// *************************
#include <radmath/radmath.hpp>
#include <raddebug.hpp> // for rAssert & other debug print outs
/*
//////////////////////////////////////////////////////////////////////////////
// OLD OLD OLD STUFF
//////////////////////////////////////////////////////////////////////////////
#ifndef PI_F
#define PI_F 3.1415926535897932384626433832795f
#endif
#define MYEPSILON 0.001
#ifndef NULL
#define NULL 0
#endif
struct Line
{
float x1;
float y1;
float x2;
float y2;
float slope;
float b;
bool isVertical;
bool isInfinite;
bool isFinishLine;
};
struct Point
{
float x;
float y;
int id;
};
bool fequals(float a, float b);
bool fequals(float a, float b, float epsilon);
bool isVerticalLine( Line line );
Line getLine( float x1, float y1, float x2, float y2, bool isInfinite );
bool isPointOnLine( Line line, Point p );
bool IntersectLines2D( rmt::Vector p1,
rmt::Vector dir1,
rmt::Vector p2,
rmt::Vector dir2,
rmt::Vector& p );
*/
//////////////////////////////////////////////////////////////////////////////
// CUBIC BEZIER SHEEYATSU
//////////////////////////////////////////////////////////////////////////////
class CubicBezier
{
public:
enum
{
MAX_CONTROL_POINTS = 4, // total control points (including start & end points)
MAX_CURVE_POINTS = 30 // total curve points including start & end points
};
static void InitOnceLUTs();
static bool sIsInitialized;
static float B0[MAX_CURVE_POINTS];
static float B1[MAX_CURVE_POINTS];
static float B2[MAX_CURVE_POINTS];
static float B3[MAX_CURVE_POINTS];
CubicBezier();
~CubicBezier();
void GetCubicBezierCurve(rmt::Vector*& pts, int& nCurvePts);
void GetCubicBezierCurve2D(rmt::Vector*& pts, int& nCurvePts);
void AddControlPoint(const rmt::Vector& cp);
void SetControlPoint(const rmt::Vector& cp, int index);
protected:
void CreateCubicBezierCurve();
void CreateCubicBezierCurve2D();
rmt::Vector mCurve[MAX_CURVE_POINTS];
rmt::Vector mCurve2D[MAX_CURVE_POINTS];
rmt::Vector mControlPoints[MAX_CONTROL_POINTS];
int mNumControlPointsAdded;
bool mCurveIsCreated;
bool mCurveIsCreated2D;
//Prevent wasteful constructor creation.
CubicBezier( const CubicBezier& CubicBezier );
CubicBezier& operator=( const CubicBezier& CubicBezier );
};
//////////////////////////////////////////////////////////////////////////////
// DListArray
//////////////////////////////////////////////////////////////////////////////
class DListArray
{
public:
enum
{
MAX_ELEMS = 20
};
DListArray();
void Clear();
// returns index value of found element, -1 on error
int Find( void* data );
// returns the index value of the newly added element
// or -1 on error
int AddLast( void* data );
// returns the index value of the newly added element
// or -1 on error
int AddFirst( void* data );
// returns index value of the newly inserted element
// or -1 on error
int InsertAfter( void* data, int i );
// Note: this incurs a linear search
bool Remove( void* data );
bool Remove( int i );
int GetNumElems() const;
int GetFree() const;
void* GetDataAt( int i ) const;
int GetNextOf( int i ) const;
int GetPrevOf( int i ) const;
void* GetFirst() const;
void* GetLast() const;
int GetHead() const;
int GetTail() const;
private:
struct DLAElem
{
void* data;
int next;
int prev;
};
DLAElem mElems[MAX_ELEMS];
int mnElems;
int mHead;
int mTail;
int mFree;
};
inline int DListArray::GetNumElems() const
{
return mnElems;
}
inline int DListArray::GetFree() const
{
return mFree;
}
inline void* DListArray::GetDataAt(int i) const
{
return mElems[i].data;
}
inline void* DListArray::GetFirst() const
{
if( mHead != -1 )
{
return mElems[mHead].data;
}
return NULL;
}
inline void* DListArray::GetLast() const
{
if( mTail != -1 )
{
return mElems[mTail].data;
}
return NULL;
}
inline int DListArray::GetHead() const
{
return mHead;
}
inline int DListArray::GetTail() const
{
return mTail;
}
inline int DListArray::GetNextOf( int i ) const
{
rAssert( 0 <= i && i < MAX_ELEMS );
return mElems[i].next;
}
inline int DListArray::GetPrevOf( int i ) const
{
rAssert( 0 <= i && i < MAX_ELEMS );
return mElems[i].prev;
}
// history tracking
template <class T, int HISTORY_SIZE> class History
{
public:
History() : mNextSpot(0) {}
~History() {}
void Init( const T& t )
{
mNextSpot = 0;
for( int i=0; i< HISTORY_SIZE; i++ )
{
mHistory[i] = t;
}
mAverage = t;
}
void UpdateHistory( const T& t)
{
// first get the old value & recalculate our average
mAverage -= (mHistory[mNextSpot] - t) / (float)(HISTORY_SIZE);
mHistory[mNextSpot] = t;
mNextSpot = (mNextSpot + 1) % HISTORY_SIZE;
}
void GetAverage( T& t )
{
t = mAverage;
}
T GetEntry( int i )
{
rAssert( 0 <= i && i < HISTORY_SIZE );
return mHistory[i];
}
T GetLastEntry()
{
int i = mNextSpot - 1;
if( i == -1 )
{
i = HISTORY_SIZE - 1;
}
rAssert( 0 <= i && i < HISTORY_SIZE );
return mHistory[i];
}
int GetSize()
{
return HISTORY_SIZE;
}
private:
int mNextSpot;
T mHistory[HISTORY_SIZE];
T mAverage;
};
template <int HISTORY_SIZE> class VectorHistory : public History<rmt::Vector, HISTORY_SIZE>
{
public:
VectorHistory() {}
~VectorHistory() {}
void Init( const rmt::Vector& t )
{
History<rmt::Vector,HISTORY_SIZE>::Init(t);
GetAverage(mNormalizedAverage);
}
void GetNormalizedAverage( rmt::Vector& vec )
{
if( rmt::Epsilon( mNormalizedAverage.MagnitudeSqr(), 1.0f, 0.0005f ) )
{
vec = mNormalizedAverage;
}
else
{
mNormalizedAverage.NormalizeSafe();
vec = mNormalizedAverage;
}
}
void UpdateHistory( const rmt::Vector& vec )
{
History<rmt::Vector,HISTORY_SIZE>::UpdateHistory(vec);
GetAverage(mNormalizedAverage);
}
protected:
rmt::Vector mNormalizedAverage;
};
//////////////////////////////////////////////////////////////////////////////
// MISC
//////////////////////////////////////////////////////////////////////////////
const float KPH_2_MPS = 1.0f/3.60f;
// returns true if projection point is on line segment
bool PointToLineProjection2D( const rmt::Vector& in,
const rmt::Vector& linePt1,
const rmt::Vector& linePt2,
rmt::Vector& out );
rmt::Vector GetProjectionVector( const rmt::Vector& source,
const rmt::Vector& target );
// MAYA: GAME:
// Right-hand coord Left-hand coord
//
// +y (forefinger) +y (forefinger)
// | |
// | |
// | |
// /\ /\
// / \ / \
// / \ / \
// +z (middle) +x (thumb) +x(thumb) +z(middle)
//
// In Lefthand coordinate system, turn vector to
// the left (counter-clockwise) 90 degrees & return new vector
rmt::Vector Get90DegreeLeftTurn( const rmt::Vector& orig );
// In Lefthand coordinate system, turn vector to
// the right (clockwise) 90 degrees & return new vector
rmt::Vector Get90DegreeRightTurn( const rmt::Vector& orig );
float GetRotationAboutY( float x, float z );
// returns the number of intersections and po ints q1 & q2
int IntersectLineSphere( const rmt::Vector& p1,
const rmt::Vector& p2,
const rmt::Sphere& s,
rmt::Vector* intPts);
// just test if line segment intersects sphere... don't bother
// finding the intersection point(s)
bool TestIntersectLineSphere( const rmt::Vector& lOrig,
const rmt::Vector& lDir,
const rmt::Sphere& s );
// Test using (normalized) myHeading DOT vectorFromMyHeadingToTarget
bool WillCollide( const rmt::Vector& myPos,
const rmt::Vector& myHeading, // Must be normalized
const rmt::Vector& mySide, // Must be normalized
float myRadius,
float myLookAheadDist,
const rmt::Vector& targetPos,
bool& targetOnMyRightSide );
rmt::Vector UpdateVUP( const rmt::Vector& position, const rmt::Vector& target );
// Given points P1 and P2, and two points that define a line, A and B
// P1 and P2 are on the same side of the line, if the Normals for BAxP1A
// and BAxP2A are pointing on the same side of the plane (i.e.
// N1-dot-N2 >= 0)
bool PointsOnSameSideOfLine( const rmt::Vector& P1,
const rmt::Vector& P2,
const rmt::Vector& A,
const rmt::Vector& B );
// Given triangle with vertices v1, v2, v3 and a point p
// p is inside triangle if it is on the same side of line v1v2 as v3
// and on the same side of line v2v3 as v1,
// and on the same side of line v1v3 as v2
//
bool PointLiesInTriangle ( const rmt::Vector& p,
const rmt::Vector& v1,
const rmt::Vector& v2,
const rmt::Vector& v3 );
// Given a point "p", and a line starting at point "start" and ending
// at point "end", determine if p lies on the left side of the line
// (assuming that the line is looking in the direction of start-to-end
//
bool PointOnLeftSideOfLine( const rmt::Vector& p,
const rmt::Vector& start,
const rmt::Vector& end );
// Ditto.. but for the right side
//
bool PointOnRightSideOfLine( const rmt::Vector& p,
const rmt::Vector& start,
const rmt::Vector& end );
// Given a line segment described by vector from start to end,
// and an arbitrary point... return the point on the line segment
// closest to this arbitrary point and the float parameter along
// the line segment at which this closest point occurs
float FindClosestPointOnLine( const rmt::Vector& start,
const rmt::Vector& end,
const rmt::Vector& p,
rmt::Vector& closestPt );
float GetLineSegmentT( const rmt::Vector& segStart,
const rmt::Vector& segEnd,
const rmt::Vector& pt );
#endif // GEOMETRY_H
|