This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bspline.h
633 lines (547 loc) · 14 KB
/
bspline.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//-----------------------------------------------------------------------------
//
// $Logfile:: /Quake 2 Engine/Sin/code/game/bspline.h $
// $Revision:: 20 $
// $Author:: Markd $
// $Date:: 5/19/99 11:30a $
//
// Copyright (C) 1998 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source is may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// $Log:: /Quake 2 Engine/Sin/code/game/bspline.h $
//
// 20 5/19/99 11:30a Markd
// Added new camera support
//
// 19 10/25/98 11:52p Jimdose
// added EXPORT_TEMPLATE
//
// 18 10/19/98 9:52p Jimdose
// fixed savegame bugs with bspline
//
// 17 10/11/98 2:03p Markd
// Inverted pitch on orientation
//
// 16 10/10/98 9:12p Markd
// Fixed angle errors with bsplines
//
// 15 9/24/98 1:19a Markd
// bullet proofed some equating bspline code
//
// 14 9/23/98 11:00p Markd
// put in some garbage collection on stuff that wasn't freed up
//
// 13 9/21/98 10:15p Markd
// Putting archiving and unarchiving functions in
//
// 12 8/15/98 2:40p Markd
// fixed bspline stuff
//
// 11 7/11/98 6:31p Markd
// removed valid orientation, simplified code
//
// 10 7/10/98 2:10p Markd
// Endpoint now returns number of controlpoints.
//
// 9 7/10/98 1:12p Markd
// Added additonal setup function which takes speed as well as position
//
// 8 7/09/98 11:54p Markd
// Put in default speed of 1
//
// 7 7/08/98 12:42p Markd
// Added quaternion support
//
// 6 7/02/98 9:48p Markd
// added orientation
//
// 5 5/26/98 7:56p Jimdose
// added scripted cameras
//
// 4 5/07/98 10:41p Jimdose
// Added spline type for selecting between looping and non-looping curves
//
// 3 5/05/98 2:37p Jimdose
// Added code to allow spline loop and clamping the spline start and end
//
// 2 5/03/98 4:42p Jimdose
// Added file to Sin
//
// DESCRIPTION:
// Uniform non-rational bspline class.
//
#ifndef __BSPLINE_H__
#define __BSPLINE_H__
#include "g_local.h"
#include "Vector.h"
typedef enum
{
SPLINE_NORMAL,
SPLINE_LOOP,
SPLINE_CLAMP
} splinetype_t;
class EXPORT_FROM_DLL BSplineControlPoint
{
private:
float roll;
Vector position;
Vector orientation;
float speed;
public:
BSplineControlPoint();
BSplineControlPoint( Vector pos, Vector orient, float speed );
BSplineControlPoint( Vector pos );
void Clear( void );
void Set( Vector pos );
void Set( Vector pos, float speed );
void Set( Vector pos, Vector orient, float speed );
void Get( Vector& pos, Vector& orient, float& speed );
void Get( Vector& pos );
Vector *GetPosition( void );
Vector *GetOrientation( void );
float *GetRoll( void );
float *GetSpeed( void );
void operator=( BSplineControlPoint &point );
virtual void Archive( Archiver &arc );
virtual void Unarchive( Archiver &arc );
};
inline EXPORT_FROM_DLL void BSplineControlPoint::Archive
(
Archiver &arc
)
{
arc.WriteVector( position );
arc.WriteVector( orientation );
arc.WriteFloat( speed );
arc.WriteFloat( roll );
}
inline EXPORT_FROM_DLL void BSplineControlPoint::Unarchive
(
Archiver &arc
)
{
arc.ReadVector( &position );
arc.ReadVector( &orientation );
arc.ReadFloat( &speed );
arc.ReadFloat( &roll );
}
inline void BSplineControlPoint::operator=
(
BSplineControlPoint &point
)
{
position = point.position;
orientation = point.orientation;
speed = point.speed;
roll = point.roll;
}
inline BSplineControlPoint::BSplineControlPoint()
{
roll = 0;
speed = 1;
}
inline BSplineControlPoint::BSplineControlPoint
(
Vector pos
)
{
speed = 1;
position = pos;
}
inline BSplineControlPoint::BSplineControlPoint
(
Vector pos,
Vector orient,
float speed
)
{
position = pos;
orient[ PITCH ] = -orient[ PITCH ];
orient.AngleVectors( &orientation, NULL, NULL );
roll = orient[ ROLL ];
if ( roll > 180 )
{
roll -= 360;
}
if ( roll < -180 )
{
roll += 360;
}
this->speed = speed;
}
inline void BSplineControlPoint::Clear
(
void
)
{
roll = 0;
position = "0 0 0";
vec_zero.AngleVectors( &orientation, NULL, NULL );
speed = 1.0f;
}
inline void BSplineControlPoint::Set
(
Vector pos
)
{
speed = 1;
position = pos;
}
inline void BSplineControlPoint::Set
(
Vector pos,
float pointspeed
)
{
speed = pointspeed;
position = pos;
}
inline void BSplineControlPoint::Set
(
Vector pos,
Vector orient,
float speed
)
{
position = pos;
orient[ PITCH ] = -orient[ PITCH ];
orient.AngleVectors( &orientation, NULL, NULL );
roll = orient[ ROLL ];
if ( roll > 180 )
{
roll -= 360;
}
if ( roll < -180 )
{
roll += 360;
}
this->speed = speed;
}
inline void BSplineControlPoint::Get
(
Vector& pos
)
{
pos = position;
}
inline Vector *BSplineControlPoint::GetPosition
(
void
)
{
return &position;
}
inline void BSplineControlPoint::Get
(
Vector& pos,
Vector& orient,
float& speed
)
{
pos = position;
orient = orientation;
speed = this->speed;
}
inline Vector *BSplineControlPoint::GetOrientation
(
void
)
{
return &orientation;
}
inline float *BSplineControlPoint::GetRoll
(
void
)
{
return &roll;
}
inline float *BSplineControlPoint::GetSpeed
(
void
)
{
return &speed;
}
class EXPORT_FROM_DLL BSpline
{
private:
BSplineControlPoint *control_points;
int num_control_points;
int loop_control_point;
splinetype_t curvetype;
qboolean has_orientation;
float EvalNormal( float u, Vector &pos, Vector& orient );
float EvalLoop( float u, Vector &pos, Vector& orient );
float EvalClamp( float u, Vector &pos, Vector& orient );
public:
BSpline();
~BSpline();
BSpline( Vector *control_points_, int num_control_points_, splinetype_t type );
BSpline( Vector *control_points_, Vector *control_orients_, float *control_speeds_, int num_control_points_, splinetype_t type );
void operator=( BSpline &spline );
void SetType( splinetype_t type );
int GetType( void );
void Clear( void );
void Set( Vector *control_points_, int num_control_points_, splinetype_t type );
void Set( Vector *control_points_, Vector *control_orients_, float *control_speeds_, int num_control_points_, splinetype_t type );
void AppendControlPoint( const Vector& new_control_point );
void AppendControlPoint( const Vector& new_control_point, const float& speed );
void AppendControlPoint( const Vector& new_control_point, const Vector& new_control_orient, const float& speed );
Vector Eval( float u );
float Eval( float u, Vector& pos, Vector& orient );
void DrawControlSegments( void );
void DrawCurve( int num_subdivisions );
void DrawCurve( Vector offset, int num_subdivisions );
void SetLoopPoint( const Vector& pos );
float EndPoint( void );
// return the index of the control point picked or -1 if none.
int PickControlPoint( const Vector& window_point, float pick_size );
Vector *GetControlPoint( int id );
void GetControlPoint( int id, Vector& pos, Vector& orient, float& speed );
void SetControlPoint( int id, const Vector& new_control_point );
void SetControlPoint( int id, const Vector& new_control_point, const Vector& new_control_orient, const float& speed );
virtual void Archive( Archiver &arc );
virtual void Unarchive( Archiver &arc );
};
inline BSpline::BSpline()
{
has_orientation = false;
control_points = NULL;
num_control_points = 0;
loop_control_point = 0;
curvetype = SPLINE_NORMAL;
}
inline BSpline::~BSpline()
{
if ( control_points )
{
delete [] control_points;
control_points = NULL;
}
}
inline BSpline::BSpline
(
Vector *control_points_,
int num_control_points_,
splinetype_t type
)
{
has_orientation = false;
control_points = NULL;
num_control_points = 0;
loop_control_point = 0;
curvetype = SPLINE_NORMAL;
Set( control_points_, num_control_points_, type );
}
inline BSpline::BSpline
(
Vector *control_points_,
Vector *control_orients_,
float *control_speeds_,
int num_control_points_,
splinetype_t type
)
{
has_orientation = false;
control_points = NULL;
num_control_points = 0;
loop_control_point = 0;
curvetype = SPLINE_NORMAL;
Set( control_points_, control_orients_, control_speeds_, num_control_points_, type );
}
inline void BSpline::operator=
(
BSpline &spline
)
{
int i;
Clear();
num_control_points = spline.num_control_points;
loop_control_point = spline.loop_control_point;
curvetype = spline.curvetype;
has_orientation = spline.has_orientation;
control_points = new BSplineControlPoint[num_control_points];
assert( control_points );
for ( i = 0; i < num_control_points ; i++ )
control_points[ i ] = spline.control_points[ i ];
}
inline void BSpline::SetType
(
splinetype_t type
)
{
curvetype = type;
}
inline int BSpline::GetType
(
void
)
{
return curvetype;
}
inline float BSpline::EndPoint
(
void
)
{
return num_control_points;
}
inline Vector *BSpline::GetControlPoint
(
int id
)
{
assert( id >= 0 );
assert( id < num_control_points );
if ( ( id < 0 ) && ( id >= num_control_points ) )
{
// probably wrong, but if we're in release mode we have no recourse
id = 0;
}
return control_points[ id ].GetPosition();
}
inline void BSpline::GetControlPoint
(
int id,
Vector& pos,
Vector& orient,
float& speed
)
{
assert( id >= 0 );
assert( id < num_control_points );
if ( ( id >= 0 ) && ( id < num_control_points ) )
{
control_points[ id ].Get( pos, orient, speed );
}
}
inline void BSpline::SetControlPoint
(
int id,
const Vector& new_control_point
)
{
assert( id >= 0 );
assert( id < num_control_points );
if ( ( id >= 0 ) && ( id < num_control_points ) )
{
control_points[ id ].Set( new_control_point );
}
}
inline void BSpline::SetControlPoint
(
int id,
const Vector& new_control_point,
const Vector& new_control_orient,
const float& speed
)
{
assert( id >= 0 );
assert( id < num_control_points );
if ( ( id >= 0 ) && ( id < num_control_points ) )
{
control_points[ id ].Set( new_control_point, new_control_orient, speed );
}
}
inline EXPORT_FROM_DLL void BSpline::Archive
(
Archiver &arc
)
{
int i;
arc.WriteInteger( num_control_points );
arc.WriteInteger( loop_control_point );
arc.WriteInteger( curvetype );
arc.WriteBoolean( has_orientation );
for( i = 0; i < num_control_points; i++ )
{
control_points[ i ].Archive( arc );
}
}
inline EXPORT_FROM_DLL void BSpline::Unarchive
(
Archiver &arc
)
{
int i;
arc.ReadInteger( &num_control_points );
arc.ReadInteger( &loop_control_point );
arc.ReadInteger( &i );
curvetype = ( splinetype_t )i;
arc.ReadBoolean( &has_orientation );
control_points = new BSplineControlPoint[ num_control_points ];
for( i = 0; i < num_control_points; i++ )
{
control_points[ i ].Unarchive( arc );
}
}
class SplinePath;
#ifdef EXPORT_TEMPLATE
template class EXPORT_FROM_DLL SafePtr<SplinePath>;
#endif
typedef SafePtr<SplinePath> SplinePathPtr;
extern Event EV_SplinePath_Create;
extern Event EV_SplinePath_Loop;
extern Event EV_SplinePath_Speed;
class EXPORT_FROM_DLL SplinePath : public Entity
{
protected:
SplinePathPtr owner;
SplinePathPtr next;
SplinePathPtr loop;
str loop_name;
void CreatePath( Event *ev );
void SetLoop( Event *ev );
void SetSpeed( Event *ev );
public:
float speed;
qboolean doWatch;
str watchEnt;
CLASS_PROTOTYPE( SplinePath );
SplinePath();
~SplinePath();
SplinePath *GetNext( void );
SplinePath *GetPrev( void );
SplinePath *GetLoop( void );
void SetWatch( const char *name );
void NoWatch( void );
Entity *GetWatch( void );
void SetNext( SplinePath *node );
void SetPrev( SplinePath *node );
virtual void Archive( Archiver &arc );
virtual void Unarchive( Archiver &arc );
};
inline EXPORT_FROM_DLL void SplinePath::Archive
(
Archiver &arc
)
{
Entity::Archive( arc );
arc.WriteSafePointer( owner );
arc.WriteSafePointer( next );
arc.WriteSafePointer( loop );
arc.WriteString( loop_name );
arc.WriteFloat( speed );
arc.WriteBoolean( doWatch );
arc.WriteString( watchEnt );
}
inline EXPORT_FROM_DLL void SplinePath::Unarchive
(
Archiver &arc
)
{
Entity::Unarchive( arc );
arc.ReadSafePointer( &owner );
arc.ReadSafePointer( &next );
arc.ReadSafePointer( &loop );
arc.ReadString( &loop_name );
arc.ReadFloat( &speed );
arc.ReadBoolean( &doWatch );
arc.ReadString( &watchEnt );
CancelEventsOfType( EV_SplinePath_Create );
}
#endif /* __BSPLINE_H__ */