-
Notifications
You must be signed in to change notification settings - Fork 19
/
Geometry.pas
7214 lines (6548 loc) · 226 KB
/
Geometry.pas
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{: Geometry<p>
Base classes and structures for GLScene.<p>
Most common functions/procedures come in various flavours (using overloads),
the naming convention is :<ul>
<li>TypeOperation: functions returning a result, or accepting a "var" as last
parameter to place result (VectorAdd, VectorCrossProduct...)
<li>OperationType : procedures taking as first parameter a "var" that will be
used as operand and result (AddVector, CombineVector...)
</ul><p>
As a general rule, procedures implementations (asm or not) are the fastest
(up to 800% faster than function equivalents), due to reduced return value
duplication overhead (the exception being the matrix operations).<p>
For better performance, it is recommended <b>not</b> to use the "Math" unit
that comes with Delphi, and only use functions/procedures from this unit
(the single-based functions have been optimized and are up to 100% faster,
than extended-based ones from "Math").<p>
3DNow! SIMD instructions are automatically detected and used in *some* of the
functions/procedures, typical gains (over FPU implementation) are approx a
100% speed increase on K6-2/3, and 20-60% on K7, and sometimes more
(f.i. 650% on 4x4 matrix multiplication for the K6, 300% for RSqrt on K7).<p>
Cyrix, NexGen and other "exotic" CPUs may fault in the 3DNow! detection
(initialization section), comment out or replace with your own detection
routines if you want to support these. All AMD processors after K5, and
all Intel processors after Pentium should be immune to this.<p>
<b>History : </b><font size=-1><ul>
<li>04/09/03 - EG - New Abs/Max functions, VectorTransform(affine, hmgMatrix)
now considers the matrix as 4x3 (was 3x3)
<li>21/08/02 - EG - Added Pack/UnPackRotationMatrix
<li>13/08/02 - EG - Added Area functions
<li>20/07/02 - EG - Fixed RayCastTriangleIntersect "backward" hits
<li>05/07/02 - EG - Started adding non-asm variants (GEOMETRY_NO_ASM)3
<li>22/02/02 - EG - Temporary Quaternion fix for VectorAngleLerp
<li>12/02/02 - EG - Added QuaternionFromEuler (Alex Grigny de Castro)
<li>11/02/02 - EG - Non-spinned QuaternionSlerp (Alex Grigny de Castro)
<li>07/02/02 - EG - Added AnglePreservingMatrixInvert
<li>30/01/02 - EG - New Quaternion<->Matrix code (Alex Grigny de Castro)
<li>29/01/02 - EG - Fixed AngleLerp, added DistanceBetweenAngles (Alex Grigny de Castro)
<li>20/01/02 - EG - Added VectorArrayAdd, ScaleFloatArray, OffsetFloatArray
<li>11/01/02 - EG - 3DNow Optim for VectorAdd (hmg)
<li>10/01/02 - EG - Fixed VectorEquals ("True" wasn't Pascal compliant "1"),
3DNow optims for vector mormalizations (affine),
Added RSqrt
<li>04/01/02 - EG - Updated/fixed RayCastTriangleIntersect
<li>13/12/01 - EG - Fixed MakeReflectionMatrix
<li>02/11/01 - EG - Faster mode for PrepareSinCosCache (by Nelson Chu)
<li>22/08/01 - EG - Some new overloads
<li>19/08/01 - EG - Added sphere raycasting functions
<li>08/08/01 - EG - Added MaxFloat overloads
<li>24/07/01 - EG - VectorAngle renamed to VectorAngleCosine to avoid confusions
<li>06/07/01 - EG - Added NormalizeDegAngle
<li>04/07/01 - EG - Now uses VectorTypes
<li>18/03/01 - EG - Added AngleLerp and NormalizeAngle
<li>15/03/01 - EG - Added Int, Ceil and Floor, faster "Frac"
<li>06/03/01 - EG - Fix in PointInPolygon by Pavel Vassiliev
<li>04/03/01 - EG - Added NormalizeVectorArray
<li>03/03/01 - EG - Added MakeReflectionMatrix
<li>02/03/01 - EG - New PointInPolygon code by Pavel Vassiliev
<li>25/02/01 - EG - Fixed 'VectorSubstract', added VectorArrayLerp and a few minors
<li>22/02/01 - EG - Added MinXYZ/MaxXYZ variants and Plane-Line intersection
<li>21/02/01 - EG - Added Sign, MinFloat & MaxFloat
<li>15/02/01 - EG - Faster Vector Transforms (3DNow! optimizations)
<li>14/02/01 - EG - Faster Matrix multiplications (3DNow! & FPU optimizations),
Added support for FPU-only sections
<li>05/02/01 - EG - Faster VectorEquals
<li>21/01/01 - EG - Fixed MakePoint/Vector affine variants (thx Jacques Tur)
<li>17/01/00 - EG - VectoAdd return type fix (thx Jacques Tur),
also added a few new overloads
<li>05/11/00 - EG - Added RayCastPlaneIntersect
<li>08/10/00 - EG - Added SetMatrix
<li>13/08/00 - EG - Added Plane geometry support
<li>06/08/00 - EG - Various minor additions
<li>16/07/00 - EG - Added some new mixed vector/scalar funcs and new overloads
<li>12/07/00 - EG - New overloads and replacements for Power, Trunc, Frac & Round
<li>25/06/00 - EG - End of major update
<li>13/06/00 - EG - Start of major update
<li>09/06/00 - EG - Some additions and fixes in preparation for major changes
<li>05/06/00 - EG - Added VectorLength overloads
<li>26/05/00 - EG - [0..0] arrays changed to [0..cMaxArray]
<li>23/05/00 - EG - Added intersection functions,
Replaced some xxxAffinexxx funcs with overloads
<li>22/03/00 - EG - Added MakeShadowMatrix (adapted from "OpenGL SuperBible" book)
<li>21/03/00 - EG - Removed PWordArray (was a SysUtils's duplicate)
<li>06/02/00 - EG - Added VectorEquals
<li>05/02/00 - EG - Added some "const", more still needed,
Added overloads for some of the MakeXXXVector funcs,
Added homogeneous vector consts, VectorSpacing
</ul>
}
unit Geometry;
// This unit contains many needed types, functions and procedures for
// quaternion, vector and matrix arithmetics. It is specifically designed
// for geometric calculations within R3 (affine vector space)
// and R4 (homogeneous vector space).
//
// Note: The terms 'affine' or 'affine coordinates' are not really correct here
// because an 'affine transformation' describes generally a transformation which leads
// to a uniquely solvable system of equations and has nothing to do with the dimensionality
// of a vector. One could use 'projective coordinates' but this is also not really correct
// and since I haven't found a better name (or even any correct one), 'affine' is as good
// as any other one.
//
// Identifiers containing no dimensionality (like affine or homogeneous)
// and no datatype (integer..extended) are supposed as R4 representation
// with 'single' floating point type (examples are TVector, TMatrix,
// and TQuaternion). The default data type is 'single' ('GLFloat' for OpenGL)
// and used in all routines (except conversions and trigonometric functions).
//
// Routines with an open array as argument can either take Func([1,2,3,4,..]) or Func(Vect).
// The latter is prefered, since no extra stack operations is required.
// Note: Be careful while passing open array elements! If you pass more elements
// than there's room in the result the behaviour will be unpredictable.
//
// If not otherwise stated, all angles are given in radians
// (instead of degrees). Use RadToDeg or DegToRad to convert between them.
//
// Geometry.pas was assembled from different sources (like GraphicGems)
// and relevant books or based on self written code, respectivly.
//
// Note: Some aspects need to be considered when using Delphi and pure
// assembler code. Delphi esnures that the direction flag is always
// cleared while entering a function and expects it cleared on return.
// This is in particular important in routines with (CPU) string commands (MOVSD etc.)
// The registers EDI, ESI and EBX (as well as the stack management
// registers EBP and ESP) must not be changed! EAX, ECX and EDX are
// freely available and mostly used for parameter.
//
// Version 2.5
// last change : 04. January 2000
//
// (c) Copyright 1999, Dipl. Ing. Mike Lischke ([email protected])
interface
uses VectorTypes;
const
cMaxArray = (MaxInt shr 4);
type
// data types needed for 3D graphics calculation,
// included are 'C like' aliases for each type (to be
// conformal with OpenGL types)
PByte = ^Byte;
PWord = ^Word;
PInteger = ^Integer;
PFloat = ^Single;
PDouble = ^Double;
PExtended = ^Extended;
PPointer = ^Pointer;
PTexPoint = ^TTexPoint;
TTexPoint = packed record
S,T : Single;
end;
// types to specify continous streams of a specific type
// switch off range checking to access values beyond the limits
PByteVector = ^TByteVector;
PByteArray = PByteVector;
TByteVector = array[0..cMaxArray] of Byte;
PWordVector = ^TWordVector;
TWordVector = array[0..cMaxArray] of Word;
PIntegerVector = ^TIntegerVector;
PIntegerArray = PIntegerVector;
TIntegerVector = array[0..cMaxArray] of Integer;
PFloatVector = ^TFloatVector;
PFloatArray = PFloatVector;
PSingleArray = PFloatArray;
TFloatVector = array[0..cMaxArray] of Single;
TSingleArray = array of Single;
PDoubleVector = ^TDoubleVector;
PDoubleArray = PDoubleVector;
TDoubleVector = array[0..cMaxArray] of Double;
PExtendedVector = ^TExtendedVector;
PExtendedArray = PExtendedVector;
TExtendedVector = array[0..cMaxArray] of Extended;
PPointerVector = ^TPointerVector;
PPointerArray = PPointerVector;
TPointerVector = array[0..cMaxArray] of Pointer;
PCardinalVector = ^TCardinalVector;
PCardinalArray = PCardinalVector;
TCardinalVector = array[0..cMaxArray] of Cardinal;
// common vector and matrix types with predefined limits
// indices correspond like: x -> 0
// y -> 1
// z -> 2
// w -> 3
PHomogeneousByteVector = ^THomogeneousByteVector;
THomogeneousByteVector = array[0..3] of Byte;
TVector4b = THomogeneousByteVector;
PHomogeneousWordVector = ^THomogeneousWordVector;
THomogeneousWordVector = array[0..3] of Word;
TVector4w = THomogeneousWordVector;
PHomogeneousIntVector = ^THomogeneousIntVector;
THomogeneousIntVector = TVector4i;
PHomogeneousFltVector = ^THomogeneousFltVector;
THomogeneousFltVector = TVector4f;
PHomogeneousDblVector = ^THomogeneousDblVector;
THomogeneousDblVector = TVector4d;
PHomogeneousExtVector = ^THomogeneousExtVector;
THomogeneousExtVector = array[0..3] of Extended;
TVector4e = THomogeneousExtVector;
PHomogeneousPtrVector = ^THomogeneousPtrVector;
THomogeneousPtrVector = array[0..3] of Pointer;
TVector4p = THomogeneousPtrVector;
PAffineByteVector = ^TAffineByteVector;
TAffineByteVector = array[0..2] of Byte;
TVector3b = TAffineByteVector;
PAffineWordVector = ^TAffineWordVector;
TAffineWordVector = array[0..2] of Word;
TVector3w = TAffineWordVector;
PAffineIntVector = ^TAffineIntVector;
TAffineIntVector = TVector3i;
PAffineFltVector = ^TAffineFltVector;
TAffineFltVector = TVector3f;
PAffineDblVector = ^TAffineDblVector;
TAffineDblVector = TVector3d;
PAffineExtVector = ^TAffineExtVector;
TAffineExtVector = array[0..2] of Extended;
TVector3e = TAffineExtVector;
PAffinePtrVector = ^TAffinePtrVector;
TAffinePtrVector = array[0..2] of Pointer;
TVector3p = TAffinePtrVector;
// some simplified names
PVector = ^TVector;
TVector = THomogeneousFltVector;
PHomogeneousVector = ^THomogeneousVector;
THomogeneousVector = THomogeneousFltVector;
PAffineVector = ^TAffineVector;
TAffineVector = TVector3f;
PVertex = ^TVertex;
TVertex = TAffineVector;
// arrays of vectors
PAffineVectorArray = ^TAffineVectorArray;
TAffineVectorArray = array[0..MAXINT shr 4] of TAffineVector;
PVectorArray = ^TVectorArray;
TVectorArray = array[0..MAXINT shr 5] of TVector;
PTexPointArray = ^TTexPointArray;
TTexPointArray = array [0..MaxInt shr 4] of TTexPoint;
// matrices
THomogeneousByteMatrix = array[0..3] of THomogeneousByteVector;
TMatrix4b = THomogeneousByteMatrix;
THomogeneousWordMatrix = array[0..3] of THomogeneousWordVector;
TMatrix4w = THomogeneousWordMatrix;
THomogeneousIntMatrix = TMatrix4i;
THomogeneousFltMatrix = TMatrix4f;
THomogeneousDblMatrix = TMatrix4d;
THomogeneousExtMatrix = array[0..3] of THomogeneousExtVector;
TMatrix4e = THomogeneousExtMatrix;
TAffineByteMatrix = array[0..2] of TAffineByteVector;
TMatrix3b = TAffineByteMatrix;
TAffineWordMatrix = array[0..2] of TAffineWordVector;
TMatrix3w = TAffineWordMatrix;
TAffineIntMatrix = TMatrix3i;
TAffineFltMatrix = TMatrix3f;
TAffineDblMatrix = TMatrix3d;
TAffineExtMatrix = array[0..2] of TAffineExtVector;
TMatrix3e = TAffineExtMatrix;
// some simplified names
PMatrix = ^TMatrix;
TMatrix = THomogeneousFltMatrix;
TMatrixArray = array [0..MaxInt shr 7] of TMatrix;
PMatrixArray = ^TMatrixArray;
PHomogeneousMatrix = ^THomogeneousMatrix;
THomogeneousMatrix = THomogeneousFltMatrix;
PAffineMatrix = ^TAffineMatrix;
TAffineMatrix = TAffineFltMatrix;
{: A plane equation.<p>
Defined by its equation A.x+B.y+C.z+D<p>, a plane can be mapped to the
homogeneous space coordinates, and this is what we are doing here.<br>
The typename is just here for easing up data manipulation. }
THmgPlane = TVector;
TDoubleHmgPlane = THomogeneousDblVector;
// q = ([x, y, z], w)
TQuaternion = record
case Integer of
0:
(ImagPart: TAffineVector;
RealPart: Single);
1:
(Vector: TVector4f);
end;
TRectangle = record
Left,
Top,
Width,
Height: Integer;
end;
TTransType = (ttScaleX, ttScaleY, ttScaleZ,
ttShearXY, ttShearXZ, ttShearYZ,
ttRotateX, ttRotateY, ttRotateZ,
ttTranslateX, ttTranslateY, ttTranslateZ,
ttPerspectiveX, ttPerspectiveY, ttPerspectiveZ, ttPerspectiveW);
// used to describe a sequence of transformations in following order:
// [Sx][Sy][Sz][ShearXY][ShearXZ][ShearZY][Rx][Ry][Rz][Tx][Ty][Tz][P(x,y,z,w)]
// constants are declared for easier access (see MatrixDecompose below)
TTransformations = array [TTransType] of Single;
// TRenderContextClippingInfo
//
TRenderContextClippingInfo = record
origin : TVector;
clippingDirection : TVector;
viewPortRadius : Single; // viewport bounding radius per distance unit
farClippingDistance : Single;
end;
TPackedRotationMatrix = array [0..2] of SmallInt;
const
// useful constants
// TexPoints (2D space)
XTexPoint : TTexPoint = (S:1; T:0);
YTexPoint : TTexPoint = (S:0; T:1);
XYTexPoint : TTexPoint = (S:1; T:1);
NullTexPoint : TTexPoint = (S:0; T:0);
// standard vectors
XVector : TAffineVector = (1, 0, 0);
YVector : TAffineVector = (0, 1, 0);
ZVector : TAffineVector = (0, 0, 1);
XYZVector : TAffineVector = (1, 1, 1);
NullVector : TAffineVector = (0, 0, 0);
// standard homogeneous vectors
XHmgVector : THomogeneousVector = (1, 0, 0, 0);
YHmgVector : THomogeneousVector = (0, 1, 0, 0);
ZHmgVector : THomogeneousVector = (0, 0, 1, 0);
WHmgVector : THomogeneousVector = (0, 0, 0, 1);
XYZHmgVector : THomogeneousVector = (1, 1, 1, 0);
XYZWHmgVector : THomogeneousVector = (1, 1, 1, 1);
NullHmgVector : THomogeneousVector = (0, 0, 0, 0);
// standard homogeneous points
XHmgPoint : THomogeneousVector = (1, 0, 0, 1);
YHmgPoint : THomogeneousVector = (0, 1, 0, 1);
ZHmgPoint : THomogeneousVector = (0, 0, 1, 1);
WHmgPoint : THomogeneousVector = (0, 0, 0, 1);
NullHmgPoint : THomogeneousVector = (0, 0, 0, 1);
IdentityMatrix: TAffineMatrix = ((1, 0, 0),
(0, 1, 0),
(0, 0, 1));
IdentityHmgMatrix: TMatrix = ((1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1));
IdentityHmgDblMatrix: THomogeneousDblMatrix = ((1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1));
EmptyMatrix: TAffineMatrix = ((0, 0, 0),
(0, 0, 0),
(0, 0, 0));
EmptyHmgMatrix: TMatrix = ((0, 0, 0, 0),
(0, 0, 0, 0),
(0, 0, 0, 0),
(0, 0, 0, 0));
// Quaternions
IdentityQuaternion: TQuaternion = (ImagPart:(0,0,0); RealPart: 1);
// some very small numbers
EPSILON : Single = 1e-40;
EPSILON2 : Single = 1e-30;
//------------------------------------------------------------------------------
// Vector functions
//------------------------------------------------------------------------------
function TexPointMake(const s, t : Single) : TTexPoint;
function AffineVectorMake(const x, y, z : Single) : TAffineVector; overload;
function AffineVectorMake(const v : TVector) : TAffineVector; overload;
procedure SetAffineVector(var v : TAffineVector; const x, y, z : Single); overload;
procedure SetVector(var v : TAffineVector; const x, y, z : Single); overload;
procedure SetVector(var v : TAffineVector; const vSrc : TVector); overload;
procedure SetVector(var v : TAffineVector; const vSrc : TAffineVector); overload;
procedure SetVector(var v : TAffineDblVector; const vSrc : TAffineVector); overload;
procedure SetVector(var v : TAffineDblVector; const vSrc : TVector); overload;
function VectorMake(const v : TAffineVector; w : Single = 0) : TVector; overload;
function VectorMake(const x, y, z: Single; w : Single = 0) : TVector; overload;
function PointMake(const x, y, z: Single) : TVector; overload;
function PointMake(const v : TAffineVector) : TVector; overload;
procedure SetVector(var v : TVector; const x, y, z : Single; w : Single = 0); overload;
procedure SetVector(var v : TVector; const av : TAffineVector; w : Single = 0); overload;
procedure SetVector(var v : TVector; const vSrc : TVector); overload;
procedure MakePoint(var v : TVector; const x, y, z: Single); overload;
procedure MakePoint(var v : TVector; const av : TAffineVector); overload;
procedure MakePoint(var v : TVector; const av : TVector); overload;
procedure MakeVector(var v : TAffineVector; const x, y, z: Single); overload;
procedure MakeVector(var v : TVector; const x, y, z: Single); overload;
procedure MakeVector(var v : TVector; const av : TAffineVector); overload;
procedure MakeVector(var v : TVector; const av : TVector); overload;
procedure RstVector(var v : TAffineVector); overload;
procedure RstVector(var v : TVector); overload;
//: Returns the sum of two affine vectors
function VectorAdd(const v1, v2 : TAffineVector) : TAffineVector; overload;
//: Adds two vectors and places result in vr
procedure VectorAdd(const v1, v2 : TAffineVector; var vr : TAffineVector); overload;
procedure VectorAdd(const v1, v2 : TAffineVector; vr : PAffineVector); overload;
//: Returns the sum of two homogeneous vectors
function VectorAdd(const v1, v2 : TVector) : TVector; overload;
procedure VectorAdd(const v1, v2 : TVector; var vr : TVector); overload;
//: Sums up f to each component of the vector
function VectorAdd(const v : TAffineVector; const f : Single) : TAffineVector; overload;
//: Sums up f to each component of the vector
function VectorAdd(const v : TVector; const f : Single) : TVector; overload;
//: Adds V2 to V1, result is placed in V1
procedure AddVector(var v1 : TAffineVector; const v2 : TAffineVector); overload;
//: Adds V2 to V1, result is placed in V1
procedure AddVector(var v1 : TAffineVector; const v2 : TVector); overload;
//: Adds V2 to V1, result is placed in V1
procedure AddVector(var v1 : TVector; const v2 : TVector); overload;
//: Sums up f to each component of the vector
procedure AddVector(var v : TAffineVector; const f : Single); overload;
//: Sums up f to each component of the vector
procedure AddVector(var v : TVector; const f : Single); overload;
//: Adds delta to nb texpoints in src and places result in dest
procedure TexPointArrayAdd(const src : PTexPointArray; const delta : TTexPoint;
const nb : Integer;
dest : PTexPointArray); overload;
procedure TexPointArrayScaleAndAdd(const src : PTexPointArray; const delta : TTexPoint;
const nb : Integer; const scale : TTexPoint;
dest : PTexPointArray); overload;
//: Adds delta to nb vectors in src and places result in dest
procedure VectorArrayAdd(const src : PAffineVectorArray; const delta : TAffineVector;
const nb : Integer;
dest : PAffineVectorArray); overload;
//: Returns V1-V2
function VectorSubtract(const V1, V2 : TAffineVector) : TAffineVector; overload;
//: Subtracts V2 from V1 and return value in result
procedure VectorSubtract(const v1, v2 : TAffineVector; var result : TAffineVector); overload;
//: Subtracts V2 from V1 and return value in result
procedure VectorSubtract(const v1, v2 : TAffineVector; var result : TVector); overload;
//: Subtracts V2 from V1 and return value in result
procedure VectorSubtract(const v1 : TVector; v2 : TAffineVector; var result : TVector); overload;
//: Returns V1-V2
function VectorSubtract(const V1, V2 : TVector) : TVector; overload;
//: Subtracts V2 from V1 and return value in result
procedure VectorSubtract(const v1, v2 : TVector; var result : TVector); overload;
//: Subtracts V2 from V1 and return value in result
procedure VectorSubtract(const v1, v2 : TVector; var result : TAffineVector); overload;
//: Subtracts V2 from V1, result is placed in V1
procedure SubtractVector(var V1 : TAffineVector; const V2 : TAffineVector); overload;
//: Subtracts V2 from V1, result is placed in V1
procedure SubtractVector(var V1 : TVector; const V2 : TVector); overload;
//: Combine the first vector with the second : vr:=vr+v*f
procedure CombineVector(var vr : TAffineVector; const v : TAffineVector; var f : Single); overload;
procedure CombineVector(var vr : TAffineVector; const v : TAffineVector; pf : PFloat); overload;
//: Makes a linear combination of two vectors and return the result
function VectorCombine(const V1, V2: TAffineVector; const F1, F2: Single): TAffineVector; overload;
//: Makes a linear combination of three vectors and return the result
function VectorCombine3(const V1, V2, V3: TAffineVector; const F1, F2, F3: Single): TAffineVector; overload;
//: Combine the first vector with the second : vr:=vr+v*f
procedure CombineVector(var vr : TVector; const v : TVector; var f : Single); overload;
//: Combine the first vector with the second : vr:=vr+v*f
procedure CombineVector(var vr : TVector; const v : TAffineVector; var f : Single); overload;
//: Makes a linear combination of two vectors and return the result
function VectorCombine(const V1, V2: TVector; const F1, F2: Single): TVector; overload;
//: Makes a linear combination of two vectors and return the result
function VectorCombine(const V1 : TVector; const V2: TAffineVector; const F1, F2: Single): TVector; overload;
//: Makes a linear combination of two vectors and place result in vr
procedure VectorCombine(const V1 : TVector; const V2: TAffineVector; const F1, F2: Single; var vr : TVector); overload;
//: Makes a linear combination of two vectors and place result in vr
procedure VectorCombine(const V1, V2: TVector; const F1, F2: Single; var vr : TVector); overload;
//: Makes a linear combination of three vectors and return the result
function VectorCombine3(const V1, V2, V3: TVector; const F1, F2, F3: Single): TVector; overload;
//: Makes a linear combination of three vectors and return the result
procedure VectorCombine3(const V1, V2, V3: TVector; const F1, F2, F3: Single; var vr : TVector); overload;
{: Calculates the dot product between V1 and V2.<p>
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const V1, V2 : TAffineVector) : Single; overload;
{: Calculates the dot product between V1 and V2.<p>
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const V1, V2 : TVector) : Single; overload;
{: Calculates the dot product between V1 and V2.<p>
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const V1 : TVector; const V2 : TAffineVector) : Single; overload;
//: Calculates the cross product between vector 1 and 2
function VectorCrossProduct(const V1, V2 : TAffineVector): TAffineVector; overload;
//: Calculates the cross product between vector 1 and 2
function VectorCrossProduct(const V1, V2 : TVector): TVector; overload;
//: Calculates the cross product between vector 1 and 2, place result in vr
procedure VectorCrossProduct(const v1, v2 : TVector; var vr : TVector); overload;
//: Calculates the cross product between vector 1 and 2, place result in vr
procedure VectorCrossProduct(const v1, v2 : TAffineVector; var vr : TVector); overload;
//: Calculates the cross product between vector 1 and 2, place result in vr
procedure VectorCrossProduct(const v1, v2 : TVector; var vr : TAffineVector); overload;
//: Calculates the cross product between vector 1 and 2, place result in vr
procedure VectorCrossProduct(const v1, v2 : TAffineVector; var vr : TAffineVector); overload;
//: Calculates linear interpolation between start and stop at point t
function Lerp(const start, stop, t : Single) : Single;
//: Calculates angular interpolation between start and stop at point t
function AngleLerp(start, stop, t : Single) : Single;
{: Calculates the angular distance between two angles in radians.<p>
Result is in the [0; PI] range. }
function DistanceBetweenAngles(angle1, angle2 : Single) : Single;
//: Calculates linear interpolation between vector1 and vector2 at point t
function VectorLerp(const v1, v2 : TAffineVector; t : Single) : TAffineVector; overload;
//: Calculates linear interpolation between vector1 and vector2 at point t, places result in vr
procedure VectorLerp(const v1, v2 : TAffineVector; t : Single; var vr : TAffineVector); overload;
//: Calculates linear interpolation between vector1 and vector2 at point t
function VectorLerp(const v1, v2 : TVector; t : Single) : TVector; overload;
//: Calculates linear interpolation between vector1 and vector2 at point t, places result in vr
procedure VectorLerp(const v1, v2 : TVector; t : Single; var vr : TVector); overload;
function VectorAngleLerp(const v1, v2 : TAffineVector; t : Single) : TAffineVector; overload;
function VectorAngleCombine(const v1, v2 : TAffineVector; f : Single) : TAffineVector; overload;
//: Calculates linear interpolation between vector arrays
procedure VectorArrayLerp(const src1, src2 : PVectorArray; t : Single; n : Integer; dest : PVectorArray); overload;
procedure VectorArrayLerp(const src1, src2 : PAffineVectorArray; t : Single; n : Integer; dest : PAffineVectorArray); overload;
{: Calculates the length of a vector following the equation sqrt(x*x+y*y). }
function VectorLength(const x, y : Single) : Single; overload;
{: Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z). }
function VectorLength(const x, y, z : Single) : Single; overload;
//: Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z).
function VectorLength(const v : TAffineVector) : Single; overload;
//: Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z+w*w).
function VectorLength(const v : TVector) : Single; overload;
{: Calculates the length of a vector following the equation: sqrt(x*x+y*y+...).<p>
Note: The parameter of this function is declared as open array. Thus
there's no restriction about the number of the components of the vector. }
function VectorLength(v : array of Single) : Single; overload;
{: Calculates norm of a vector which is defined as norm = x * x + y * y<p>
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const x, y : Single) : Single; overload;
{: Calculates norm of a vector which is defined as norm = x*x + y*y + z*z<p>
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const v : TAffineVector) : Single; overload;
{: Calculates norm of a vector which is defined as norm = x*x + y*y + z*z<p>
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const v : TVector) : Single; overload;
{: Calculates norm of a vector which is defined as norm = v[0]*v[0] + ...<p>
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(var V: array of Single) : Single; overload;
//: Transforms a vector to unit length
procedure NormalizeVector(var v : TAffineVector); overload;
//: Transforms a vector to unit length
procedure NormalizeVector(var v : TVector); overload;
//: Returns the vector transformed to unit length
function VectorNormalize(const v : TAffineVector) : TAffineVector; overload;
//: Returns the vector transformed to unit length (w component dropped)
function VectorNormalize(const v : TVector) : TVector; overload;
//: Transforms vectors to unit length
procedure NormalizeVectorArray(list : PAffineVectorArray; n : Integer); overload;
{: Calculates the cosine of the angle between Vector1 and Vector2.<p>
Result = DotProduct(V1, V2) / (Length(V1) * Length(V2)) }
function VectorAngleCosine(const V1, V2: TAffineVector) : Single;
//: Negates the vector
function VectorNegate(const v : TAffineVector) : TAffineVector; overload;
function VectorNegate(const v : TVector) : TVector; overload;
//: Negates the vector
procedure NegateVector(var V : TAffineVector); overload;
//: Negates the vector
procedure NegateVector(var V : TVector); overload;
//: Negates the vector
procedure NegateVector(V : array of Single); overload;
//: Scales given vector by a factor
procedure ScaleVector(var v : TAffineVector; factor : Single); overload;
{: Scales given vector by another vector.<p>
v[x]:=v[x]*factor[x], v[y]:=v[y]*factor[y] etc. }
procedure ScaleVector(var v : TAffineVector; const factor : TAffineVector); overload;
//: Scales given vector by a factor
procedure ScaleVector(var v : TVector; factor : Single); overload;
{: Scales given vector by another vector.<p>
v[x]:=v[x]*factor[x], v[y]:=v[y]*factor[y] etc. }
procedure ScaleVector(var v : TVector; const factor : TVector); overload;
//: Returns a vector scaled by a factor
function VectorScale(const v : TAffineVector; factor : Single) : TAffineVector; overload;
//: Scales a vector by a factor and places result in vr
procedure VectorScale(const v : TAffineVector; factor : Single; var vr : TAffineVector); overload;
//: Returns a vector scaled by a factor
function VectorScale(const v : TVector; factor : Single) : TVector; overload;
//: Scales a vector by a factor and places result in vr
procedure VectorScale(const v : TVector; factor : Single; var vr : TVector); overload;
//: Scales a vector by a factor and places result in vr
procedure VectorScale(const v : TVector; factor : Single; var vr : TAffineVector); overload;
{: Divides given vector by another vector.<p>
v[x]:=v[x]/divider[x], v[y]:=v[y]/divider[y] etc. }
procedure DivideVector(var v : TVector; const divider : TVector); overload;
//: True if all components are equal.
function VectorEquals(const V1, V2: TVector) : Boolean; overload;
//: True if all components are equal.
function VectorEquals(const V1, V2: TAffineVector) : Boolean; overload;
//: True if x=y=z=0, w ignored
function VectorIsNull(const v : TVector) : Boolean; overload;
//: True if x=y=z=0, w ignored
function VectorIsNull(const v : TAffineVector) : Boolean; overload;
{: Calculates Abs(v1[x]-v2[x])+Abs(v1[y]-v2[y])+..., also know as "Norm1".<p> }
function VectorSpacing(const v1, v2 : TAffineVector): Single; overload;
{: Calculates Abs(v1[x]-v2[x])+Abs(v1[y]-v2[y])+..., also know as "Norm1".<p> }
function VectorSpacing(const v1, v2 : TVector): Single; overload;
{: Calculates distance between two vectors.<p>
ie. sqrt(sqr(v1[x]-v2[x])+...) }
function VectorDistance(const v1, v2 : TAffineVector): Single; overload;
{: Calculates distance between two vectors.<p>
ie. sqrt(sqr(v1[x]-v2[x])+...) (w component ignored) }
function VectorDistance(const v1, v2 : TVector): Single; overload;
{: Calculates the "Norm 2" between two vectors.<p>
ie. sqr(v1[x]-v2[x])+... }
function VectorDistance2(const v1, v2 : TAffineVector): Single; overload;
{: Calculates the "Norm 2" between two vectors.<p>
ie. sqr(v1[x]-v2[x])+... (w component ignored) }
function VectorDistance2(const v1, v2 : TVector): Single; overload;
{: Calculates a vector perpendicular to N.<p>
N is assumed to be of unit length, subtract out any component parallel to N }
function VectorPerpendicular(const V, N: TAffineVector): TAffineVector;
//: Reflects vector V against N (assumes N is normalized)
function VectorReflect(const V, N: TAffineVector): TAffineVector;
//: Rotates Vector about Axis with Angle radians
procedure RotateVector(var vector : TVector; const axis : TAffineVector; angle : Single); overload;
//: Rotates Vector about Axis with Angle radians
procedure RotateVector(var vector : TVector; const axis : TVector; angle : Single); overload;
//: Rotate given vector around the Y axis (alpha is in rad)
procedure RotateVectorAroundY(var v : TAffineVector; alpha : Single);
//: Returns given vector rotated around the X axis (alpha is in rad)
function VectorRotateAroundX(const v : TAffineVector; alpha : Single) : TAffineVector; overload;
//: Returns given vector rotated around the Y axis (alpha is in rad)
function VectorRotateAroundY(const v : TAffineVector; alpha : Single) : TAffineVector; overload;
//: Returns given vector rotated around the Y axis in vr (alpha is in rad)
procedure VectorRotateAroundY(const v : TAffineVector; alpha : Single; var vr : TAffineVector); overload;
//: Returns given vector rotated around the Z axis (alpha is in rad)
function VectorRotateAroundZ(const v : TAffineVector; alpha : Single) : TAffineVector; overload;
//: Vector components are replaced by their Abs() value. }
procedure AbsVector(var v : TVector); overload;
//: Vector components are replaced by their Abs() value. }
procedure AbsVector(var v : TAffineVector); overload;
//: Returns a vector with components replaced by their Abs value. }
function VectorAbs(const v : TVector) : TVector; overload;
//------------------------------------------------------------------------------
// Matrix functions
//------------------------------------------------------------------------------
procedure SetMatrix(var dest : THomogeneousDblMatrix; const src : TMatrix); overload;
//: Creates scale matrix
function CreateScaleMatrix(const v : TAffineVector) : TMatrix; overload;
//: Creates scale matrix
function CreateScaleMatrix(const v : TVector) : TMatrix; overload;
//: Creates translation matrix
function CreateTranslationMatrix(const V : TAffineVector): TMatrix; overload;
//: Creates translation matrix
function CreateTranslationMatrix(const V : TVector): TMatrix; overload;
{: Creates a scale+translation matrix.<p>
Scale is applied BEFORE applying offset }
function CreateScaleAndTranslationMatrix(const scale, offset : TVector): TMatrix; overload;
//: Creates matrix for rotation about x-axis (angle in rad)
function CreateRotationMatrixX(const sine, cosine: Single) : TMatrix; overload;
function CreateRotationMatrixX(const angle: Single) : TMatrix; overload;
//: Creates matrix for rotation about y-axis (angle in rad)
function CreateRotationMatrixY(const sine, cosine: Single) : TMatrix; overload;
function CreateRotationMatrixY(const angle: Single) : TMatrix; overload;
//: Creates matrix for rotation about z-axis (angle in rad)
function CreateRotationMatrixZ(const sine, cosine: Single) : TMatrix; overload;
function CreateRotationMatrixZ(const angle: Single) : TMatrix; overload;
//: Creates a rotation matrix along the given Axis by the given Angle in radians.
function CreateRotationMatrix(const anAxis: TAffineVector; angle: Single): TMatrix;
//: Creates a rotation matrix along the given Axis by the given Angle in radians.
function CreateAffineRotationMatrix(const anAxis: TAffineVector; angle: Single): TAffineMatrix;
//: Multiplies two 3x3 matrices
function MatrixMultiply(const M1, M2 : TAffineMatrix) : TAffineMatrix; overload
//: Multiplies two 4x4 matrices
function MatrixMultiply(const M1, M2 : TMatrix) : TMatrix; overload
//: Multiplies M1 by M2 and places result in MResult
procedure MatrixMultiply(const M1, M2 : TMatrix; var MResult : TMatrix); overload
//: Transforms a homogeneous vector by multiplying it with a matrix
function VectorTransform(const V: TVector; const M: TMatrix): TVector; overload;
//: Transforms a homogeneous vector by multiplying it with a matrix
function VectorTransform(const V: TVector; const M: TAffineMatrix): TVector; overload;
//: Transforms an affine vector by multiplying it with a matrix
function VectorTransform(const V: TAffineVector; const M: TMatrix): TAffineVector; overload;
//: Transforms an affine vector by multiplying it with a matrix
function VectorTransform(const V: TAffineVector; const M: TAffineMatrix): TAffineVector; overload;
//: Determinant of a 3x3 matrix
function MatrixDeterminant(const M: TAffineMatrix): Single; overload;
//: Determinant of a 4x4 matrix
function MatrixDeterminant(const M: TMatrix): Single; overload;
{: Adjoint of a 4x4 matrix.<p>
used in the computation of the inverse of a 4x4 matrix }
procedure AdjointMatrix(var M : TMatrix);
//: Multiplies all elements of a 3x3 matrix with a factor
procedure ScaleMatrix(var M : TAffineMatrix; const factor : Single); overload;
//: Multiplies all elements of a 4x4 matrix with a factor
procedure ScaleMatrix(var M : TMatrix; const factor : Single); overload;
{: Normalize the matrix and remove the translation component.<p>
The resulting matrix is an orthonormal matrix (Y direction preserved, then Z) }
procedure NormalizeMatrix(var M : TMatrix);
//: Computes transpose of 3x3 matrix
procedure TransposeMatrix(var M: TAffineMatrix); overload;
//: Computes transpose of 4x4 matrix
procedure TransposeMatrix(var M: TMatrix); overload;
//: Finds the inverse of a 4x4 matrix
procedure InvertMatrix(var M: TMatrix);
{: Finds the inverse of an angle preserving matrix.<p>
Angle preserving matrices can combine translation, rotation and isotropic
scaling, other matrices won't be properly inverted by this function. }
function AnglePreservingMatrixInvert(const mat : TMatrix) : TMatrix;
{: Decompose a non-degenerated 4x4 transformation matrix into the sequence of transformations that produced it.<p>
Modified by ml then eg, original Author: Spencer W. Thomas, University of Michigan<p>
The coefficient of each transformation is returned in the corresponding
element of the vector Tran.<p>
Returns true upon success, false if the matrix is singular. }
function MatrixDecompose(const M: TMatrix; var Tran: TTransformations): Boolean;
//------------------------------------------------------------------------------
// Plane functions
//------------------------------------------------------------------------------
//: Computes the parameters of a plane defined by three points.
function PlaneMake(const p1, p2, p3 : TAffineVector) : THmgPlane; overload;
//: Computes the parameters of a plane defined by a point and a normal.
function PlaneMake(const point, normal : TAffineVector) : THmgPlane; overload;
//: Converts from single to double representation
procedure SetPlane(var dest : TDoubleHmgPlane; const src : THmgPlane);
{: Calculates the cross-product between the plane normal and plane to point vector.<p>
This functions gives an hint as to were the point is, if the point is in the
half-space pointed by the vector, result is positive.<p>
This function performs an homogeneous space dot-product. }
function PlaneEvaluatePoint(const plane : THmgPlane; const point : TAffineVector) : Single; overload;
function PlaneEvaluatePoint(const plane : THmgPlane; const point : TVector) : Single; overload;
{: Calculate the normal of a plane defined by three points. }
function CalcPlaneNormal(const p1, p2, p3 : TAffineVector) : TAffineVector; overload;
procedure CalcPlaneNormal(const p1, p2, p3 : TAffineVector; var vr : TAffineVector); overload;
procedure CalcPlaneNormal(const p1, p2, p3 : TVector; var vr : TAffineVector); overload;
{: Returns true if point is in the half-space defined by a plane with normal.<p>
The plane itself is not considered to be in the tested halfspace. }
function PointIsInHalfSpace(const point, planePoint, planeNormal : TVector) : Boolean;
{: Computes algebraic distance between point and plane.<p>
Value will be positive if the point is in the halfspace pointed by the normal,
negative on the other side. }
function PointPlaneDistance(const point, planePoint, planeNormal : TVector) : Single;
//------------------------------------------------------------------------------
// Quaternion functions
//------------------------------------------------------------------------------
type
TEulerOrder = (eulXYZ, eulXZY, eulYXZ, eulYZX, eulZXY, eulZYX);
//: Creates a quaternion from the given values
function QuaternionMake(Imag: array of Single; Real: Single): TQuaternion;
//: Returns the conjugate of a quaternion
function QuaternionConjugate(const Q : TQuaternion) : TQuaternion;
//: Returns the magnitude of the quaternion
function QuaternionMagnitude(const Q : TQuaternion) : Single;
//: Normalizes the given quaternion
procedure NormalizeQuaternion(var Q : TQuaternion);
//: Constructs a unit quaternion from two points on unit sphere
function QuaternionFromPoints(const V1, V2: TAffineVector): TQuaternion;
//: Converts a unit quaternion into two points on a unit sphere
procedure QuaternionToPoints(const Q: TQuaternion; var ArcFrom, ArcTo: TAffineVector);
//: Constructs a unit quaternion from a rotation matrix
function QuaternionFromMatrix(const mat : TMatrix) : TQuaternion;
{: Constructs rotation matrix from (possibly non-unit) quaternion.<p>
Assumes matrix is used to multiply column vector on the left:<br>
vnew = mat vold.<p>
Works correctly for right-handed coordinate system and right-handed rotations. }
function QuaternionToMatrix(quat : TQuaternion) : TMatrix;
//: Constructs quaternion from angle (in deg) and axis
function QuaternionFromAngleAxis(const angle : Single; const axis : TAffineVector) : TQuaternion;
//: Constructs quaternion from Euler angles
function QuaternionFromRollPitchYaw(const r, p, y : Single) : TQuaternion;
//: Constructs quaternion from Euler angles in arbitrary order (angles in degrees)
function QuaternionFromEuler(const x, y, z: Single; eulerOrder : TEulerOrder) : TQuaternion;
{: Returns quaternion product qL * qR.<p>
Note: order is important!<p>
To combine rotations, use the product QuaternionMuliply(qSecond, qFirst),
which gives the effect of rotating by qFirst then qSecond. }
function QuaternionMultiply(const qL, qR: TQuaternion): TQuaternion;
{: Spherical linear interpolation of unit quaternions with spins.<p>
QStart, QEnd - start and end unit quaternions<br>
t - interpolation parameter (0 to 1)<br>
Spin - number of extra spin rotations to involve<br> }
function QuaternionSlerp(const QStart, QEnd: TQuaternion; Spin: Integer; t: Single): TQuaternion; overload;
function QuaternionSlerp(const source, dest: TQuaternion; const t : Single) : TQuaternion; overload;
//------------------------------------------------------------------------------
// Logarithmic and exponential functions
//------------------------------------------------------------------------------
{: Return ln(1 + X), accurate for X near 0. }
function LnXP1(X: Extended): Extended;
{: Log base 10 of X}
function Log10(X: Extended): Extended;
{: Log base 2 of X }
function Log2(X: Extended): Extended; overload;
{: Log base 2 of X }
function Log2(X: Single): Single; overload;
{: Log base N of X }
function LogN(Base, X: Extended): Extended;
{: Raise base to an integer. }
function IntPower(Base: Extended; Exponent: Integer): Extended;
{: Raise base to any power.<p>
For fractional exponents, or |exponents| > MaxInt, base must be > 0. }
function Power(const Base, Exponent: Single): Single; overload;
{: Raise base to an integer. }
function Power(Base: Single; Exponent: Integer): Single; overload;
//------------------------------------------------------------------------------
// Trigonometric functions
//------------------------------------------------------------------------------
function DegToRad(const Degrees: Extended): Extended; overload;
function DegToRad(const Degrees: Single): Single; overload;
function RadToDeg(const Radians: Extended): Extended; overload;
function RadToDeg(const Radians: Single): Single; overload;
//: Normalize to an angle in the [-PI; +PI] range
function NormalizeAngle(angle : Single) : Single;
//: Normalize to an angle in the [-180; 180] range
function NormalizeDegAngle(angle : Single) : Single;
//: Calculates sine and cosine from the given angle Theta
procedure SinCos(const Theta: Extended; var Sin, Cos: Extended); overload;
//: Calculates sine and cosine from the given angle Theta
procedure SinCos(const Theta: Double; var Sin, Cos: Double); overload;
//: Calculates sine and cosine from the given angle Theta
procedure SinCos(const Theta: Single; var Sin, Cos: Single); overload;
{: Calculates sine and cosine from the given angle Theta and Radius.<p>
sin and cos values calculated from theta are multiplicated by radius. }
procedure SinCos(const theta, radius : Double; var Sin, Cos: Extended); overload;
{: Calculates sine and cosine from the given angle Theta and Radius.<p>
sin and cos values calculated from theta are multiplicated by radius. }
procedure SinCos(const theta, radius : Double; var Sin, Cos: Double); overload;
{: Calculates sine and cosine from the given angle Theta and Radius.<p>
sin and cos values calculated from theta are multiplicated by radius. }
procedure SinCos(const theta, radius : Single; var Sin, Cos: Single); overload;
{: Fills up the two given dynamic arrays with sin cos values.<p>
start and stop angles must be given in degrees, the number of steps is
determined by the length of the given arrays. }
procedure PrepareSinCosCache(var s, c : array of Single;
startAngle, stopAngle : Single);
function ArcCos(const X: Extended) : Extended; overload;
function ArcCos(const x : Single) : Single; overload;
function ArcSin(const X : Extended) : Extended; overload;
function ArcSin(const X : Single) : Single; overload;
function ArcTan2(const Y, X : Extended) : Extended; overload;
function ArcTan2(const Y, X : Single) : Single; overload;
function Tan(const X : Extended) : Extended; overload;
function Tan(const X : Single) : Single; overload;
function CoTan(const X : Extended) : Extended; overload;
function CoTan(const X : Single) : Single; overload;
//------------------------------------------------------------------------------
// Miscellanious math functions
//------------------------------------------------------------------------------
{: Computes 1/Sqrt(v).<p> }
function RSqrt(v : Single) : Single;
{: Computes 1/Sqrt(Sqr(x)+Sqr(y)). }
function RLength(x, y : Single) : Single;
{: Computes an integer sqrt approximation.<p> }
function ISqrt(i : Integer) : Integer;
{: Computes an integer length Result:=Sqrt(x*x+y*y). }
function ILength(x, y : Integer) : Integer; overload;
function ILength(x, y, z : Integer) : Integer; overload;
{: Generates a random point on the unit sphere.<p>
Point repartition is correctly isotropic with no privilegied direction. }
procedure RandomPointOnSphere(var p : TAffineVector);
function Trunc(v : Single) : Integer; overload;
function Trunc64(v : Extended) : Int64; overload;
function Int(v : Single) : Single; overload;
function Int(v : Extended) : Extended; overload;
function Frac(v : Single) : Single; overload;
function Frac(v : Extended) : Extended; overload;
function Round(v : Single) : Integer; overload;
function Round64(v : Extended) : Int64; overload;
function Ceil(v : Single) : Integer; overload;
function Ceil64(v : Extended) : Int64; overload;
function Floor(v : Single) : Integer; overload;
function Floor64(v : Extended) : Int64; overload;
{: Returns the sign of the x value using the (-1, 0, +1) convention }
function Sign(x : Single) : Integer;
{: Returns True if x is in [a; b] }
function IsInRange(const x, a, b : Single) : Boolean;
{: Returns True if p is in the cube defined by d. }
function IsInCube(const p, d : TAffineVector) : Boolean; overload;
function IsInCube(const p, d : TVector) : Boolean; overload;
{: Returns the minimum value of the array. }
function MinFloat(values : PSingleArray; nbItems : Integer) : Single; overload;
function MinFloat(values : PDoubleArray; nbItems : Integer) : Double; overload;
function MinFloat(values : PExtendedArray; nbItems : Integer) : Extended; overload;
function MinFloat(const v1, v2 : Single) : Single; overload;
{: Returns the maximum value of the array. }
function MaxFloat(values : PSingleArray; nbItems : Integer) : Single; overload;
function MaxFloat(values : PDoubleArray; nbItems : Integer) : Double; overload;
function MaxFloat(values : PExtendedArray; nbItems : Integer) : Extended; overload;
{: Returns the maximum of given values. }
function MaxFloat(const v1, v2 : Single) : Single; overload;
function MaxFloat(const v1, v2 : Double) : Double; overload;