{ ****************************************************************************** }
{ * geometry 3D library writen by QQ 600585@qq.com * }
{ * https://zpascal.net * }
{ * https://github.com/PassByYou888/zAI * }
{ * https://github.com/PassByYou888/ZServer4D * }
{ * https://github.com/PassByYou888/PascalString * }
{ * https://github.com/PassByYou888/zRasterization * }
{ * https://github.com/PassByYou888/CoreCipher * }
{ * https://github.com/PassByYou888/zSound * }
{ * https://github.com/PassByYou888/zChinese * }
{ * https://github.com/PassByYou888/zExpression * }
{ * https://github.com/PassByYou888/zGameWare * }
{ * https://github.com/PassByYou888/zAnalysis * }
{ * https://github.com/PassByYou888/FFMPEG-Header * }
{ * https://github.com/PassByYou888/zTranslate * }
{ * https://github.com/PassByYou888/InfiniteIoT * }
{ * https://github.com/PassByYou888/FastMD5 * }
{ ****************************************************************************** }
unit GeometryLib;
{$INCLUDE zDefine.inc}
interface
uses CoreClasses, Geometry2DUnit;
type
TVector2f = array [0 .. 1] of TGeoFloat;
TVector3f = array [0 .. 2] of TGeoFloat;
TVector4f = array [0 .. 3] of TGeoFloat;
TVector4i = array [0 .. 3] of Integer;
TMatrix2f = array [0 .. 1] of TVector2f;
TMatrix3f = array [0 .. 2] of TVector3f;
TMatrix4f = array [0 .. 3] of TVector4f;
const
cMaxArray = (MaxInt shr 4);
cColinearBias = 1E-8;
type
// data types needed for 3D graphics calculation,
// included are 'C like' aliases for each type (to be
// conformal with OpenGL types)
PFloat = PSingle;
PTexPoint = ^TTexPoint;
TTexPoint = record
s, t: TGeoFloat;
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 TGeoFloat;
TSingleArray = array of TGeoFloat;
PDoubleVector = ^TDoubleVector;
PDoubleArray = PDoubleVector;
TDoubleVector = array [0 .. cMaxArray] of Double;
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
PHomogeneousFltVector = ^THomogeneousFltVector;
THomogeneousFltVector = TVector4f;
PAffineFltVector = ^TAffineFltVector;
TAffineFltVector = TVector3f;
PVector2f = ^TVector2f;
// 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 div SizeOf(TAffineVector)) - 1] of TAffineVector;
PVectorArray = ^TVectorArray;
TVectorArray = array [0 .. (MaxInt div SizeOf(TVector)) - 1] of TVector;
PTexPointArray = ^TTexPointArray;
TTexPointArray = array [0 .. (MaxInt div SizeOf(TTexPoint)) - 1] of TTexPoint;
// matrices
THomogeneousFltMatrix = TMatrix4f;
TAffineFltMatrix = TMatrix3f;
// some simplified names
PMatrix = ^TMatrix;
TMatrix = THomogeneousFltMatrix;
TMatrixArray = array [0 .. (MaxInt div SizeOf(TMatrix)) - 1] of TMatrix;
PMatrixArray = ^TMatrixArray;
PHomogeneousMatrix = ^THomogeneousMatrix;
THomogeneousMatrix = THomogeneousFltMatrix;
PAffineMatrix = ^TAffineMatrix;
TAffineMatrix = TAffineFltMatrix;
{ A plane equation.
Defined by its equation A.x+B.y+C.z+D, a plane can be mapped to the
homogeneous space coordinates, and this is what we are doing here.
The typename is just here for easing up data manipulation. }
THmgPlane = TVector;
// q = ([x, y, z], w)
PQuaternion = ^TQuaternion;
TQuaternion = record
ImagPart: TAffineVector;
RealPart: TGeoFloat;
end;
PQuaternionArray = ^TQuaternionArray;
TQuaternionArray = array [0 .. (MaxInt div SizeOf(TQuaternion)) - 1] of TQuaternion;
TRectangle = record
Left, Top, width, height: Integer;
end;
TFrustum = record
pLeft, pTop, pRight, pBottom, pNear, pFar: THmgPlane;
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 TGeoFloat;
TPackedRotationMatrix = array [0 .. 2] of SmallInt;
const
// 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);
MidTexPoint: TTexPoint = (s: 0.5; t: 0.5);
// standard vectors
XVector: TAffineVector = (1, 0, 0);
YVector: TAffineVector = (0, 1, 0);
ZVector: TAffineVector = (0, 0, 1);
XYVector: TAffineVector = (1, 1, 0);
XZVector: TAffineVector = (1, 0, 1);
YZVector: TAffineVector = (0, 1, 1);
XYZVector: TAffineVector = (1, 1, 1);
NullVector: TAffineVector = (0, 0, 0);
MinusXVector: TAffineVector = (-1, 0, 0);
MinusYVector: TAffineVector = (0, -1, 0);
MinusZVector: TAffineVector = (0, 0, -1);
// 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);
XYHmgVector: THomogeneousVector = (1, 1, 0, 0);
YZHmgVector: THomogeneousVector = (0, 1, 1, 0);
XZHmgVector: THomogeneousVector = (1, 0, 1, 0);
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));
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: TGeoFloat = 1E-40;
EPSILON2: TGeoFloat = 1E-30;
// ------------------------------------------------------------------------------
// Vector functions
// ------------------------------------------------------------------------------
function TexPointMake(const s, t: TGeoFloat): TTexPoint; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function AffineVectorMake(const x, y, z: TGeoFloat): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function AffineVectorMake(const v: TVector): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetAffineVector(out v: TAffineVector; const x, y, z: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TAffineVector; const x, y, z: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TAffineVector; const vSrc: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TAffineVector; const vSrc: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorMake(const v: TAffineVector; w: TGeoFloat = 0): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorMake(const x, y, z: TGeoFloat; w: TGeoFloat = 0): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function PointMake(const x, y, z: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function PointMake(const v: TAffineVector): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function PointMake(const v: TVector): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TVector; const x, y, z: TGeoFloat; w: TGeoFloat = 0); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TVector; const av: TAffineVector; w: TGeoFloat = 0); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure SetVector(out v: TVector; const vSrc: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakePoint(out v: TVector; const x, y, z: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakePoint(out v: TVector; const av: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakePoint(out v: TVector; const av: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakeVector(out v: TAffineVector; const x, y, z: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakeVector(out v: TVector; const x, y, z: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakeVector(out v: TVector; const av: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure MakeVector(out v: TVector; const av: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure RstVector(var v: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure RstVector(var v: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 2
function VectorEquals(const v1, v2: TVector2f): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 3x3
function MatrixEquals(const Matrix1, Matrix2: TMatrix3f): Boolean; overload;
// 4x4
function MatrixEquals(const Matrix1, Matrix2: TMatrix4f): Boolean; overload;
// 2x
function Vector2fMake(const x, y: TGeoFloat): TVector2f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector2fMake(const Vector: TVector3f): TVector2f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector2fMake(const Vector: TVector4f): TVector2f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 3x
function Vector3fMake(const x: TGeoFloat; const y: TGeoFloat = 0; const z: TGeoFloat = 0): TVector3f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector3fMake(const Vector: TVector2f; const z: TGeoFloat = 0): TVector3f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector3fMake(const Vector: TVector4f): TVector3f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 4x
function Vector4fMake(const x: TGeoFloat; const y: TGeoFloat = 0; const z: TGeoFloat = 0; const w: TGeoFloat = 0): TVector4f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector4fMake(const Vector: TVector3f; const w: TGeoFloat = 0): TVector4f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function Vector4fMake(const Vector: TVector2f; const z: TGeoFloat = 0; const w: TGeoFloat = 0): TVector4f; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Vector comparison functions:
// ComparedVector
// 3f
function VectorMoreThen(const SourceVector, ComparedVector: TVector3f): Boolean; overload;
function VectorMoreEqualThen(const SourceVector, ComparedVector: TVector3f): Boolean; overload;
function VectorLessThen(const SourceVector, ComparedVector: TVector3f): Boolean; overload;
function VectorLessEqualThen(const SourceVector, ComparedVector: TVector3f): Boolean; overload;
// 4f
function VectorMoreThen(const SourceVector, ComparedVector: TVector4f): Boolean; overload;
function VectorMoreEqualThen(const SourceVector, ComparedVector: TVector4f): Boolean; overload;
function VectorLessThen(const SourceVector, ComparedVector: TVector4f): Boolean; overload;
function VectorLessEqualThen(const SourceVector, ComparedVector: TVector4f): Boolean; overload;
// ComparedNumber
// 3f
function VectorMoreThen(const SourceVector: TVector3f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorMoreEqualThen(const SourceVector: TVector3f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorLessThen(const SourceVector: TVector3f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorLessEqualThen(const SourceVector: TVector3f; const ComparedNumber: TGeoFloat): Boolean; overload;
// 4f
function VectorMoreThen(const SourceVector: TVector4f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorMoreEqualThen(const SourceVector: TVector4f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorLessThen(const SourceVector: TVector4f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorLessEqualThen(const SourceVector: TVector4f; const ComparedNumber: TGeoFloat): Boolean; overload;
function VectorAdd(const v1, v2: TVector2f): TVector2f; 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: TGeoFloat): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Sums up f to each component of the vector
function VectorAdd(const v: TVector; const f: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 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: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Sums up f to each component of the vector
procedure AddVector(var v: TVector; const f: TGeoFloat); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Adds V2 to V1, result is placed in V1. W coordinate is always 1.
procedure AddPoint(var v1: TVector; const v2: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Returns the sum of two homogeneous vectors. W coordinate is always 1.
function PointAdd(var v1: TVector; const v2: TVector): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 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: TVector2f): TVector2f; overload;
// Subtracts V2 from V1, result is placed in V1
procedure SubtractVector(var v1: TVector2f; const v2: TVector2f); 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;
function VectorSubtract(const v1: TAffineVector; Delta: TGeoFloat): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorSubtract(const v1: TVector; Delta: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// 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: TGeoFloat); overload;
procedure CombineVector(var vr: TAffineVector; const v: TAffineVector; pf: PFloat); overload;
// Makes a linear combination of two texpoints
function TexPointCombine(const t1, t2: TTexPoint; f1, f2: TGeoFloat): TTexPoint; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of two vectors and return the result
function VectorCombine(const v1, v2: TAffineVector; const f1, f2: TGeoFloat): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of three vectors and return the result
function VectorCombine3(const v1, v2, v3: TAffineVector; const f1, f2, F3: TGeoFloat): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure VectorCombine3(const v1, v2, v3: TAffineVector; const f1, f2, F3: TGeoFloat; var vr: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Combine the first vector with the second : vr:=vr+v*f
procedure CombineVector(var vr: TVector; const v: TVector; var f: TGeoFloat); overload;
// Combine the first vector with the second : vr:=vr+v*f
procedure CombineVector(var vr: TVector; const v: TAffineVector; var f: TGeoFloat); overload;
// Makes a linear combination of two vectors and return the result
function VectorCombine(const v1, v2: TVector; const f1, f2: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of two vectors and return the result
function VectorCombine(const v1: TVector; const v2: TAffineVector; const f1, f2: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of two vectors and place result in vr
procedure VectorCombine(const v1: TVector; const v2: TAffineVector; const f1, f2: TGeoFloat; var vr: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of two vectors and place result in vr
procedure VectorCombine(const v1, v2: TVector; const f1, f2: TGeoFloat; var vr: TVector); overload;
// Makes a linear combination of two vectors and place result in vr, F1=1.0
procedure VectorCombine(const v1, v2: TVector; const f2: TGeoFloat; 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: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Makes a linear combination of three vectors and return the result
procedure VectorCombine3(const v1, v2, v3: TVector; const f1, f2, F3: TGeoFloat; var vr: TVector); overload;
{ Calculates the dot product between V1 and V2.
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] }
function VectorDotProduct(const v1, v2: TVector2f): TGeoFloat; overload;
{ Calculates the dot product between V1 and V2.
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const v1, v2: TAffineVector): TGeoFloat; overload;
{ Calculates the dot product between V1 and V2.
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const v1, v2: TVector): TGeoFloat; overload;
{ Calculates the dot product between V1 and V2.
Result:=V1[X] * V2[X] + V1[Y] * V2[Y] + V1[Z] * V2[Z] }
function VectorDotProduct(const v1: TVector; const v2: TAffineVector): TGeoFloat; overload;
{ Projects p on the line defined by o and direction.
Performs VectorDotProduct(VectorSubtract(p, origin), direction), which,
if direction is normalized, computes the distance between origin and the
projection of p on the (origin, direction) line. }
function PointProject(const p, origin, direction: TAffineVector): TGeoFloat; overload;
function PointProject(const p, origin, direction: TVector): TGeoFloat; 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: TGeoFloat): TGeoFloat; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Calculates angular interpolation between start and stop at point t
function AngleLerp(Start, stop, t: TGeoFloat): TGeoFloat; {$IFDEF INLINE_ASM} inline; {$ENDIF}
{ This is used for interpolating between 2 matrices. The result
is used to reposition the model parts each frame. }
function MatrixLerp(const m1, m2: TMatrix; const Delta: TGeoFloat): TMatrix;
{ Calculates the angular distance between two angles in radians.
Result is in the [0; PI] range. }
function DistanceBetweenAngles(angle1, angle2: TGeoFloat): TGeoFloat;
// Calculates linear interpolation between texpoint1 and texpoint2 at point t
function TexPointLerp(const t1, t2: TTexPoint; t: TGeoFloat): TTexPoint; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Calculates linear interpolation between vector1 and vector2 at point t
function VectorLerp(const v1, v2: TAffineVector; t: TGeoFloat): TAffineVector; overload;
// Calculates linear interpolation between vector1 and vector2 at point t, places result in vr
procedure VectorLerp(const v1, v2: TAffineVector; t: TGeoFloat; var vr: TAffineVector); overload;
// Calculates linear interpolation between vector1 and vector2 at point t
function VectorLerp(const v1, v2: TVector; t: TGeoFloat): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Calculates linear interpolation between vector1 and vector2 at point t, places result in vr
procedure VectorLerp(const v1, v2: TVector; t: TGeoFloat; var vr: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorAngleLerp(const v1, v2: TAffineVector; t: TGeoFloat): TAffineVector; overload;
function VectorAngleCombine(const v1, v2: TAffineVector; f: TGeoFloat): TAffineVector; overload;
// Calculates linear interpolation between vector arrays
procedure VectorArrayLerp(const src1, src2: PVectorArray; t: TGeoFloat; n: Integer; dest: PVectorArray); overload;
procedure VectorArrayLerp(const src1, src2: PAffineVectorArray; t: TGeoFloat; n: Integer; dest: PAffineVectorArray); overload;
procedure VectorArrayLerp(const src1, src2: PTexPointArray; t: TGeoFloat; n: Integer; dest: PTexPointArray); overload;
type
TGLInterpolationType = (itLinear, itPower, itSin, itSinAlt, itTan, itLn, itExp);
{ There functions that do the same as "Lerp", but add some distortions. }
function InterpolatePower(const Start, stop, Delta: TGeoFloat; const DistortionDegree: TGeoFloat): TGeoFloat;
function InterpolateLn(const Start, stop, Delta: TGeoFloat; const DistortionDegree: TGeoFloat): TGeoFloat;
function InterpolateExp(const Start, stop, Delta: TGeoFloat; const DistortionDegree: TGeoFloat): TGeoFloat;
{ Only valid where Delta belongs to [0..1] }
function InterpolateSin(const Start, stop, Delta: TGeoFloat): TGeoFloat;
function InterpolateTan(const Start, stop, Delta: TGeoFloat): TGeoFloat;
{ "Alt" functions are valid everywhere }
function InterpolateSinAlt(const Start, stop, Delta: TGeoFloat): TGeoFloat;
function InterpolateCombinedFastPower(const OriginalStart, OriginalStop, OriginalCurrent: TGeoFloat; const TargetStart, TargetStop: TGeoFloat; const DistortionDegree: TGeoFloat): TGeoFloat;
function InterpolateCombinedSafe(const OriginalStart, OriginalStop, OriginalCurrent: TGeoFloat; const TargetStart, TargetStop: TGeoFloat; const DistortionDegree: TGeoFloat; const InterpolationType: TGLInterpolationType): TGeoFloat;
function InterpolateCombinedFast(const OriginalStart, OriginalStop, OriginalCurrent: TGeoFloat; const TargetStart, TargetStop: TGeoFloat; const DistortionDegree: TGeoFloat; const InterpolationType: TGLInterpolationType): TGeoFloat;
function InterpolateCombined(const Start, stop, Delta: TGeoFloat; const DistortionDegree: TGeoFloat; const InterpolationType: TGLInterpolationType): TGeoFloat;
{ Calculates the length of a vector following the equation sqrt(x*x+y*y). }
function VectorLength(const x, y: TGeoFloat): TGeoFloat; overload;
{ Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z). }
function VectorLength(const x, y, z: TGeoFloat): TGeoFloat; overload;
// Calculates the length of a vector following the equation sqrt(x*x+y*y).
function VectorLength(const v: TVector2f): TGeoFloat; overload;
// Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z).
function VectorLength(const v: TAffineVector): TGeoFloat; overload;
// Calculates the length of a vector following the equation sqrt(x*x+y*y+z*z+w*w).
function VectorLength(const v: TVector): TGeoFloat; overload;
{ Calculates the length of a vector following the equation: sqrt(x*x+y*y+...).
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(const v: array of TGeoFloat): TGeoFloat; overload;
{ Calculates norm of a vector which is defined as norm = x * x + y * y
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const x, y: TGeoFloat): TGeoFloat; overload;
{ Calculates norm of a vector which is defined as norm = x*x + y*y + z*z
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const v: TAffineVector): TGeoFloat; overload;
{ Calculates norm of a vector which is defined as norm = x*x + y*y + z*z
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(const v: TVector): TGeoFloat; overload;
{ Calculates norm of a vector which is defined as norm = v[0]*v[0] + ...
Also known as "Norm 2" in the math world, this is sqr(VectorLength). }
function VectorNorm(var v: array of TGeoFloat): TGeoFloat; overload;
// Transforms a vector to unit length
procedure NormalizeVector(var v: TVector2f); overload;
// Returns the vector transformed to unit length
// 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: TVector2f): TVector2f; 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.
Result = DotProduct(V1, V2) / (Length(V1) * Length(V2)) }
function VectorAngleCosine(const v1, v2: TAffineVector): TGeoFloat; overload;
{ Calculates the cosine of the angle between Vector1 and Vector2.
Result = DotProduct(V1, V2) / (Length(V1) * Length(V2)) }
function VectorAngleCosine(const v1, v2: TVector): TGeoFloat; overload;
// 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(var v: array of TGeoFloat); overload;
// Scales given vector by a factor
procedure ScaleVector(var v: TVector2f; factor: TGeoFloat); overload;
// Scales given vector by a factor
procedure ScaleVector(var v: TAffineVector; factor: TGeoFloat); overload;
{ Scales given vector by another vector.
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: TGeoFloat); overload;
{ Scales given vector by another vector.
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: TVector2f; factor: TGeoFloat): TVector2f; overload;
// Returns a vector scaled by a factor
function VectorScale(const v: TAffineVector; factor: TGeoFloat): TAffineVector; overload;
// Scales a vector by a factor and places result in vr
procedure VectorScale(const v: TAffineVector; factor: TGeoFloat; var vr: TAffineVector); overload;
// Returns a vector scaled by a factor
function VectorScale(const v: TVector; factor: TGeoFloat): TVector; overload;
// Scales a vector by a factor and places result in vr
procedure VectorScale(const v: TVector; factor: TGeoFloat; var vr: TVector); overload;
// Scales a vector by a factor and places result in vr
procedure VectorScale(const v: TVector; factor: TGeoFloat; var vr: TAffineVector); overload;
// Scales given vector by another vector
function VectorScale(const v: TAffineVector; const factor: TAffineVector): TAffineVector; overload;
// RScales given vector by another vector
function VectorScale(const v: TVector; const factor: TVector): TVector; overload;
{ Divides given vector by another vector.
v[x]:=v[x]/divider[x], v[y]:=v[y]/divider[y] etc. }
procedure DivideVector(var v: TVector; const divider: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
procedure DivideVector(var v: TAffineVector; const divider: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorDivide(const v: TVector; const divider: TVector): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function VectorDivide(const v: TAffineVector; const divider: TAffineVector): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if all components are equal.
function TexpointEquals(const p1, p2: TTexPoint): Boolean; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if all components are equal.
// True if all components are equal.
function VectorEquals(const v1, v2: TVector): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if all components are equal.
function VectorEquals(const v1, v2: TAffineVector): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if X, Y and Z components are equal.
function AffineVectorEquals(const v1, v2: TVector): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if x=y=z=0, w ignored
function VectorIsNull(const v: TVector): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// True if x=y=z=0, w ignored
function VectorIsNull(const v: TAffineVector): Boolean; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
{ Calculates Abs(v1[x]-v2[x])+Abs(v1[y]-v2[y]), also know as "Norm1". }
function VectorSpacing(const v1, v2: TTexPoint): TGeoFloat; overload;
{ Calculates Abs(v1[x]-v2[x])+Abs(v1[y]-v2[y])+..., also know as "Norm1". }
function VectorSpacing(const v1, v2: TAffineVector): TGeoFloat; overload;
{ Calculates Abs(v1[x]-v2[x])+Abs(v1[y]-v2[y])+..., also know as "Norm1". }
function VectorSpacing(const v1, v2: TVector): TGeoFloat; overload;
{ Calculates distance between two vectors.
ie. sqrt(sqr(v1[x]-v2[x])+...) }
function VectorDistance(const v1, v2: TAffineVector): TGeoFloat; overload;
{ Calculates distance between two vectors.
ie. sqrt(sqr(v1[x]-v2[x])+...) (w component ignored) }
function VectorDistance(const v1, v2: TVector): TGeoFloat; overload;
{ Calculates the "Norm 2" between two vectors.
ie. sqr(v1[x]-v2[x])+... }
function VectorDistance2(const v1, v2: TAffineVector): TGeoFloat; overload;
{ Calculates the "Norm 2" between two vectors.
ie. sqr(v1[x]-v2[x])+... (w component ignored) }
function VectorDistance2(const v1, v2: TVector): TGeoFloat; overload;
{ Calculates a vector perpendicular to N.
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: TGeoFloat); overload;
// Rotates Vector about Axis with Angle radians
procedure RotateVector(var Vector: TVector; const axis: TVector; angle: TGeoFloat); overload;
// Rotate given vector around the Y axis (alpha is in rad)
procedure RotateVectorAroundY(var v: TAffineVector; alpha: TGeoFloat);
// Returns given vector rotated around the X axis (alpha is in rad)
function VectorRotateAroundX(const v: TAffineVector; alpha: TGeoFloat): TAffineVector; overload;
// Returns given vector rotated around the Y axis (alpha is in rad)
function VectorRotateAroundY(const v: TAffineVector; alpha: TGeoFloat): TAffineVector; overload;
// Returns given vector rotated around the Y axis in vr (alpha is in rad)
procedure VectorRotateAroundY(const v: TAffineVector; alpha: TGeoFloat; var vr: TAffineVector); overload;
// Returns given vector rotated around the Z axis (alpha is in rad)
function VectorRotateAroundZ(const v: TAffineVector; alpha: TGeoFloat): TAffineVector; overload;
// Vector components are replaced by their Abs() value.
procedure AbsVector(var v: TVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Vector components are replaced by their Abs() value.
procedure AbsVector(var v: TAffineVector); overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Returns a vector with components replaced by their Abs value.
function VectorAbs(const v: TVector): TVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Returns a vector with components replaced by their Abs value.
function VectorAbs(const v: TAffineVector): TAffineVector; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
// Returns true if both vector are colinear
function IsColinear(const v1, v2: TVector2f): Boolean; overload;
// Returns true if both vector are colinear
function IsColinear(const v1, v2: TAffineVector): Boolean; overload;
// Returns true if both vector are colinear
function IsColinear(const v1, v2: TVector): Boolean; overload;
// ------------------------------------------------------------------------------
// Matrix functions
// ------------------------------------------------------------------------------
procedure SetMatrix(var dest: TAffineMatrix; const Src: TMatrix); overload;
procedure SetMatrix(var dest: TMatrix; const Src: TAffineMatrix); overload;
procedure SetMatrixRow(var dest: TMatrix; rowNb: Integer; const aRow: TVector); 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.
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: TGeoFloat): TMatrix; overload;
function CreateRotationMatrixX(const angle: TGeoFloat): TMatrix; overload;
// Creates matrix for rotation about y-axis (angle in rad)
function CreateRotationMatrixY(const sine, cosine: TGeoFloat): TMatrix; overload;
function CreateRotationMatrixY(const angle: TGeoFloat): TMatrix; overload;
// Creates matrix for rotation about z-axis (angle in rad)
function CreateRotationMatrixZ(const sine, cosine: TGeoFloat): TMatrix; overload;
function CreateRotationMatrixZ(const angle: TGeoFloat): TMatrix; overload;
// Creates a rotation matrix along the given Axis by the given Angle in radians.
function CreateRotationMatrix(const anAxis: TAffineVector; angle: TGeoFloat): TMatrix; overload;
function CreateRotationMatrix(const anAxis: TVector; angle: TGeoFloat): TMatrix; overload;
// Creates a rotation matrix along the given Axis by the given Angle in radians.
function CreateAffineRotationMatrix(const anAxis: TAffineVector; angle: TGeoFloat): 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): TGeoFloat; overload;
// Determinant of a 4x4 matrix
function MatrixDeterminant(const M: TMatrix): TGeoFloat; overload;
{ Adjoint of a 4x4 matrix.
used in the computation of the inverse of a 4x4 matrix }
procedure AdjointMatrix(var M: TMatrix); overload;
{ Adjoint of a 3x3 matrix.
used in the computation of the inverse of a 3x3 matrix }
procedure AdjointMatrix(var M: TAffineMatrix); overload;
// Multiplies all elements of a 3x3 matrix with a factor
procedure ScaleMatrix(var M: TAffineMatrix; const factor: TGeoFloat); overload;
// Multiplies all elements of a 4x4 matrix with a factor
procedure ScaleMatrix(var M: TMatrix; const factor: TGeoFloat); overload;
// Adds the translation vector into the matrix
procedure TranslateMatrix(var M: TMatrix; const v: TAffineVector); overload;
procedure TranslateMatrix(var M: TMatrix; const v: TVector); overload;
{ Normalize the matrix and remove the translation component.
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); overload;
function MatrixInvert(const M: TMatrix): TMatrix; overload;
// Finds the inverse of a 3x3 matrix;
procedure InvertMatrix(var M: TAffineMatrix); overload;
function MatrixInvert(const M: TAffineMatrix): TAffineMatrix; overload;
{ Finds the inverse of an angle preserving matrix.
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.
Modified by ml then eg, original Author: Spencer W. Thomas, University of Michigan
The coefficient of each transformation is returned in the corresponding
element of the vector Tran.
Returns true upon success, false if the matrix is singular. }
function MatrixDecompose(const M: TMatrix; var Tran: TTransformations): Boolean;
function CreateLookAtMatrix(const eye, center, normUp: TVector): TMatrix;
function CreateMatrixFromFrustum(Left, Right, Bottom, Top, ZNear, ZFar: TGeoFloat): TMatrix;
function CreatePerspectiveMatrix(FOV, Aspect, ZNear, ZFar: TGeoFloat): TMatrix;
function CreateOrthoMatrix(Left, Right, Bottom, Top, ZNear, ZFar: TGeoFloat): TMatrix;
function CreatePickMatrix(x, y, deltax, deltay: TGeoFloat; const viewport: TVector4i): TMatrix;
function Project(objectVector: TVector; const ViewProjMatrix: TMatrix; const viewport: TVector4i; out WindowVector: TVector): Boolean;
function UnProject(WindowVector: TVector; ViewProjMatrix: TMatrix; const viewport: TVector4i; out objectVector: TVector): Boolean;
// ------------------------------------------------------------------------------
// Plane functions
// ------------------------------------------------------------------------------
// Computes the parameters of a plane defined by three points.
function PlaneMake(const p1, p2, p3: TAffineVector): THmgPlane; overload;
function PlaneMake(const p1, p2, p3: TVector): THmgPlane; overload;
// Computes the parameters of a plane defined by a point and a normal.
function PlaneMake(const Point, normal: TAffineVector): THmgPlane; overload;
function PlaneMake(const Point, normal: TVector): THmgPlane; overload;
// Normalize a plane so that point evaluation = plane distance. }
procedure NormalizePlane(var plane: THmgPlane);
{ Calculates the cross-product between the plane normal and plane to point vector.
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.
This function performs an homogeneous space dot-product. }
function PlaneEvaluatePoint(const plane: THmgPlane; const Point: TAffineVector): TGeoFloat; overload;
function PlaneEvaluatePoint(const plane: THmgPlane; const Point: TVector): TGeoFloat; 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.
The plane itself is not considered to be in the tested halfspace. }
function PointIsInHalfSpace(const Point, planePoint, planeNormal: TVector): Boolean; overload;
function PointIsInHalfSpace(const Point, planePoint, planeNormal: TAffineVector): Boolean; overload;
function PointIsInHalfSpace(const Point: TAffineVector; plane: THmgPlane): Boolean; overload;
{ Computes algebraic distance between point and plane.
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): TGeoFloat; overload;
function PointPlaneDistance(const Point, planePoint, planeNormal: TAffineVector): TGeoFloat; overload;
function PointPlaneDistance(const Point: TAffineVector; plane: THmgPlane): TGeoFloat; overload;
{ Computes point to plane projection. Plane and direction have to be normalized }
function PointPlaneOrthoProjection(const Point: TAffineVector; const plane: THmgPlane; var inter: TAffineVector; bothface: Boolean): Boolean;
function PointPlaneProjection(const Point, direction: TAffineVector; const plane: THmgPlane; var inter: TAffineVector; bothface: Boolean): Boolean;
{ Computes segment / plane intersection return false if there isn't an intersection }
function SegmentPlaneIntersection(const ptA, ptB: TAffineVector; const plane: THmgPlane; var inter: TAffineVector): Boolean;
{ Computes point to triangle projection. Direction has to be normalized }
function PointTriangleOrthoProjection(const Point, ptA, ptB, ptC: TAffineVector; var inter: TAffineVector; bothface: Boolean): Boolean;
function PointTriangleProjection(const Point, direction, ptA, ptB, ptC: TAffineVector; var inter: TAffineVector; bothface: Boolean): Boolean;
{ Returns true if line intersect ABC triangle. }
function IsLineIntersectTriangle(const Point, direction, ptA, ptB, ptC: TAffineVector): Boolean;
{ Computes point to Quad projection. Direction has to be normalized. Quad have to be flat and convex }
function PointQuadOrthoProjection(const Point, ptA, ptB, ptC, ptD: TAffineVector; var inter: TAffineVector; bothface: Boolean): Boolean;
function PointQuadProjection(const Point, direction, ptA, ptB, ptC, ptD: TAffineVector; var inter: TAffineVector; bothface: Boolean): Boolean;
{ Returns true if line intersect ABCD quad. Quad have to be flat and convex }
function IsLineIntersectQuad(const Point, direction, ptA, ptB, ptC, ptD: TAffineVector): Boolean;
{ Computes point to disk projection. Direction has to be normalized }
function PointDiskOrthoProjection(const Point, center, up: TAffineVector; const radius: TGeoFloat; var inter: TAffineVector; bothface: Boolean): Boolean;
function PointDiskProjection(const Point, direction, center, up: TAffineVector; const radius: TGeoFloat; var inter: TAffineVector; bothface: Boolean): Boolean;
{ Computes closest point on a segment (a segment is a limited line). }
function PointSegmentClosestPoint(const Point, segmentStart, segmentStop: TAffineVector): TAffineVector; overload;
function PointSegmentClosestPoint(const Point, segmentStart, segmentStop: TVector): TVector; overload;
{ Computes algebraic distance between segment and line (a segment is a limited line). }
function PointSegmentDistance(const Point, segmentStart, segmentStop: TAffineVector): TGeoFloat;
{ Computes closest point on a line. }
function PointLineClosestPoint(const Point, linePoint, lineDirection: TAffineVector): TAffineVector;
{ Computes algebraic distance between point and line. }
function PointLineDistance(const Point, linePoint, lineDirection: TAffineVector): TGeoFloat;
{ Computes the closest points (2) given two segments. }
procedure SegmentSegmentClosestPoint(const S0Start, S0Stop, S1Start, S1Stop: TAffineVector; var Segment0Closest, Segment1Closest: TAffineVector);
{ Computes the closest distance between two segments. }
function SegmentSegmentDistance(const S0Start, S0Stop, S1Start, S1Stop: TAffineVector): TGeoFloat;
{ Computes the closest distance between two lines. }
function LineLineDistance(const linePt0, lineDir0, linePt1, lineDir1: TAffineVector): TGeoFloat;
// ------------------------------------------------------------------------------
// Quaternion functions
// ------------------------------------------------------------------------------
type
TEulerOrder = (eulXYZ, eulXZY, eulYXZ, eulYZX, eulZXY, eulZYX);
// Creates a quaternion from the given values
function QuaternionMake(const Imag: array of TGeoFloat; Real: TGeoFloat): TQuaternion;
// Returns the conjugate of a quaternion
function QuaternionConjugate(const q: TQuaternion): TQuaternion;
// Returns the magnitude of the quaternion
function QuaternionMagnitude(const q: TQuaternion): TGeoFloat;
// 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 a rotation matrix from (possibly non-unit) quaternion.
Assumes matrix is used to multiply column vector on the left:
vnew = mat vold.
Works correctly for right-handed coordinate system and right-handed rotations. }
function QuaternionToMatrix(quat: TQuaternion): TMatrix;
{ Constructs an affine rotation matrix from (possibly non-unit) quaternion. }
function QuaternionToAffineMatrix(quat: TQuaternion): TAffineMatrix;
// Constructs quaternion from angle (in deg) and axis
function QuaternionFromAngleAxis(const angle: TGeoFloat; const axis: TAffineVector): TQuaternion;
// Constructs quaternion from Euler angles
function QuaternionFromRollPitchYaw(const r, p, y: TGeoFloat): TQuaternion;
// Constructs quaternion from Euler angles in arbitrary order (angles in degrees)
function QuaternionFromEuler(const x, y, z: TGeoFloat; eulerOrder: TEulerOrder): TQuaternion;
{ Returns quaternion product qL * qR.
Note: order is important!
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.
QStart, QEnd - start and end unit quaternions
t - interpolation parameter (0 to 1)
Spin - number of extra spin rotations to involve
}
function QuaternionSlerp(const QStart, QEnd: TQuaternion; Spin: Integer; t: TGeoFloat): TQuaternion; overload;
function QuaternionSlerp(const Source, dest: TQuaternion; const t: TGeoFloat): 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: TGeoFloat): TGeoFloat; 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.
For fractional exponents, or |exponents| > MaxInt, base must be > 0. }
function Power(const Base, Exponent: TGeoFloat): TGeoFloat; overload;
{ Raise base to an integer. }
function Power(Base: TGeoFloat; Exponent: Integer): TGeoFloat; overload;
function Power(Base: TGeoFloat; Exponent: Int64): TGeoFloat; overload;
// ------------------------------------------------------------------------------
// Trigonometric functions
// ------------------------------------------------------------------------------
function DegToRad(const Degrees: Extended): Extended; overload;
function DegToRad(const Degrees: TGeoFloat): TGeoFloat; overload;
function RadToDeg(const Radians: Extended): Extended; overload;
function RadToDeg(const Radians: TGeoFloat): TGeoFloat; overload;
// Normalize to an angle in the [-PI; +PI] range
function NormalizeAngle(angle: TGeoFloat): TGeoFloat;
// Normalize to an angle in the [-180; 180] range
function NormalizeDegAngle(angle: TGeoFloat): TGeoFloat;
// Calculates sine and cosine from the given angle Theta
procedure SinCos(const Theta: Extended; out Sin, Cos: Extended); overload;
// Calculates sine and cosine from the given angle Theta
procedure SinCos(const Theta: TGeoFloat; out Sin, Cos: TGeoFloat); overload;
{ Calculates sine and cosine from the given angle Theta and Radius.
sin and cos values calculated from theta are multiplicated by radius. }
procedure SinCos(const Theta, radius: Double; out Sin, Cos: Extended); overload;
{ Calculates sine and cosine from the given angle Theta and Radius.
sin and cos values calculated from theta are multiplicated by radius. }
procedure SinCos(const Theta, radius: TGeoFloat; out Sin, Cos: TGeoFloat); overload;
{ Fills up the two given dynamic arrays with sin cos values.
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 TGeoFloat; startAngle, stopAngle: TGeoFloat);
function ArcCos(const x: Extended): Extended; overload;
function ArcCos(const x: TGeoFloat): TGeoFloat; overload;
function ArcSin(const x: Extended): Extended; overload;
function ArcSin(const x: TGeoFloat): TGeoFloat; overload;
function ArcTan2(const y, x: Extended): Extended; overload;
function ArcTan2(const y, x: TGeoFloat): TGeoFloat; overload;
{ Fast ArcTan2 approximation, about 0.07 rads accuracy. }
function FastArcTan2(y, x: TGeoFloat): TGeoFloat;
function Tan(const x: Extended): Extended; overload;
function Tan(const x: TGeoFloat): TGeoFloat; overload;
function CoTan(const x: Extended): Extended; overload;
function CoTan(const x: TGeoFloat): TGeoFloat; overload;
// ------------------------------------------------------------------------------
// Hyperbolic Trigonometric functions
// ------------------------------------------------------------------------------
function Sinh(const x: TGeoFloat): TGeoFloat; overload;
function Sinh(const x: Double): Double; overload;
function Cosh(const x: TGeoFloat): TGeoFloat; overload;
function Cosh(const x: Double): Double; overload;
// ------------------------------------------------------------------------------
// Miscellanious math functions
// ------------------------------------------------------------------------------
{ Computes 1/Sqrt(v). }
function RSqrt(v: TGeoFloat): TGeoFloat;
{ Computes 1/Sqrt(Sqr(x)+Sqr(y)). }
function RLength(x, y: TGeoFloat): TGeoFloat;
{ Computes an integer sqrt approximation. }
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.
Point repartition is correctly isotropic with no privilegied direction. }
procedure RandomPointOnSphere(var p: TAffineVector);
{ Rounds the floating point value to the closest integer.
Behaves like Round but returns a floating point value like Int. }
function RoundInt(v: TGeoFloat): TGeoFloat; overload;
function RoundInt(v: Extended): Extended; overload;
function Trunc(x: Extended): Int64;
function Round(x: Extended): Int64;
function Frac(x: Extended): Extended;
function Ceil(v: TGeoFloat): Integer; overload;
function Ceil64(v: Extended): Int64; overload;
function Floor(v: TGeoFloat): Integer; overload;
function Floor64(v: Extended): Int64; overload;
{ Multiples i by s and returns the rounded result. }
function ScaleAndRound(i: Integer; var s: TGeoFloat): Integer;
{ Returns the sign of the x value using the (-1, 0, +1) convention }
function Sign(x: TGeoFloat): Integer;
function SignStrict(x: TGeoFloat): Integer;
{ Returns True if x is in [a; b] }
function IsInRange(const x, a, b: TGeoFloat): Boolean; overload;
function IsInRange(const x, a, b: Double): Boolean; overload;
{ 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): TGeoFloat; overload;
function MinFloat(values: PDoubleArray; nbItems: Integer): Double; overload;
{ Returns the minimum of given values. }
function MinFloat(const v1, v2: TGeoFloat): TGeoFloat; overload;
function MinFloat(const v: array of TGeoFloat): TGeoFloat; overload;
function MinFloat(const v1, v2: Double): Double; overload;
function MinFloat(const v1, v2, v3: TGeoFloat): TGeoFloat; overload;
function MinFloat(const v1, v2, v3: Double): Double; overload;
{ Returns the maximum value of the array. }
function MaxFloat(values: PSingleArray; nbItems: Integer): TGeoFloat; overload;
function MaxFloat(values: PDoubleArray; nbItems: Integer): Double; overload;
function MaxFloat(const v: array of TGeoFloat): TGeoFloat; overload;
{ Returns the maximum of given values. }
function MaxFloat(const v1, v2: TGeoFloat): TGeoFloat; overload;
function MaxFloat(const v1, v2: Double): Double; overload;
function MaxFloat(const v1, v2, v3: TGeoFloat): TGeoFloat; overload;
function MaxFloat(const v1, v2, v3: Double): Double; overload;
function MinInteger(const v1, v2: Integer): Integer; overload;
function MinInteger(const v1, v2: Cardinal): Cardinal; overload;
function MinInteger(const v1, v2, v3: Integer): Integer; overload;
function MinInteger(const v1, v2, v3: Cardinal): Cardinal; overload;
function MaxInteger(const v1, v2: Integer): Integer; overload;
function MaxInteger(const v1, v2: Cardinal): Cardinal; overload;
function MaxInteger(const v1, v2, v3: Integer): Integer; overload;
function MaxInteger(const v1, v2, v3: Cardinal): Cardinal; overload;
function ClampInteger(const Value, Min, Max: Integer): Integer; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
function ClampInteger(const Value, Min, Max: Cardinal): Cardinal; overload; {$IFDEF INLINE_ASM} inline; {$ENDIF}
{ Computes the triangle's area. }
function TriangleArea(const p1, p2, p3: TAffineVector): TGeoFloat; overload;
{ Computes the polygons's area.
Points must be coplanar. Polygon needs not be convex. }
function PolygonArea(const p: PAffineVectorArray; nSides: Integer): TGeoFloat; overload;
{ Computes a 2D triangle's signed area.
Only X and Y coordinates are used, Z is ignored. }
function TriangleSignedArea(const p1, p2, p3: TAffineVector): TGeoFloat; overload;
{ Computes a 2D polygon's signed area.
Only X and Y coordinates are used, Z is ignored. Polygon needs not be convex. }
function PolygonSignedArea(const p: PAffineVectorArray; nSides: Integer): TGeoFloat; overload;
{ Multiplies values in the array by factor.
This function is especially efficient for large arrays, it is not recommended
for arrays that have less than 10 items.
Expected performance is 4 to 5 times that of a Deliph-compiled loop on AMD
CPUs, and 2 to 3 when 3DNow! isn't available. }
procedure ScaleFloatArray(values: PSingleArray; nb: Integer; var factor: TGeoFloat); overload;
procedure ScaleFloatArray(var values: TSingleArray; factor: TGeoFloat); overload;
{ Adds delta to values in the array.
Array size must be a multiple of four. }
procedure OffsetFloatArray(values: PSingleArray; nb: Integer; var Delta: TGeoFloat); overload;
procedure OffsetFloatArray(var values: array of TGeoFloat; Delta: TGeoFloat); overload;
procedure OffsetFloatArray(valuesDest, valuesDelta: PSingleArray; nb: Integer); overload;
{ Returns the max of the X, Y and Z components of a vector (W is ignored). }
function MaxXYZComponent(const v: TVector): TGeoFloat; overload;
function MaxXYZComponent(const v: TAffineVector): TGeoFloat; overload;
{ Returns the min of the X, Y and Z components of a vector (W is ignored). }
function MinXYZComponent(const v: TVector): TGeoFloat; overload;
function MinXYZComponent(const v: TAffineVector): TGeoFloat; overload;
{ Returns the max of the Abs(X), Abs(Y) and Abs(Z) components of a vector (W is ignored). }
function MaxAbsXYZComponent(v: TVector): TGeoFloat;
{ Returns the min of the Abs(X), Abs(Y) and Abs(Z) components of a vector (W is ignored). }
function MinAbsXYZComponent(v: TVector): TGeoFloat;
{ Replace components of v with the max of v or v1 component.
Maximum is computed per component. }
procedure MaxVector(var v: TVector; const v1: TVector); overload;
procedure MaxVector(var v: TAffineVector; const v1: TAffineVector); overload;
{ Replace components of v with the min of v or v1 component.
Minimum is computed per component. }
procedure MinVector(var v: TVector; const v1: TVector); overload;
procedure MinVector(var v: TAffineVector; const v1: TAffineVector); overload;
{ Sorts given array in ascending order.
NOTE : current implementation is a slow bubble sort... }
procedure SortArrayAscending(var a: array of Extended);
{ Clamps aValue in the aMin-aMax interval. }
function ClampValue(const AValue, aMin, aMax: TGeoFloat): TGeoFloat; overload;
{ Clamps aValue in the aMin-INF interval. }
function ClampValue(const AValue, aMin: TGeoFloat): TGeoFloat; overload;
{ Turn a triplet of rotations about x, y, and z (in that order) into an equivalent rotation around a TGeoFloat axis (all in radians). }
function ConvertRotation(const Angles: TAffineVector): TVector;
// miscellaneous functions
function PointInPolygon(var xp, yp: array of TGeoFloat; x, y: TGeoFloat): Boolean;
procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
// coordinate system manipulation functions
// Rotates the given coordinate system (represented by the matrix) around its Y-axis
function Turn(const Matrix: TMatrix; angle: TGeoFloat): TMatrix; overload;
// Rotates the given coordinate system (represented by the matrix) around MasterUp
function Turn(const Matrix: TMatrix; const MasterUp: TAffineVector; angle: TGeoFloat): TMatrix; overload;
// Rotates the given coordinate system (represented by the matrix) around its X-axis
function Pitch(const Matrix: TMatrix; angle: TGeoFloat): TMatrix; overload;
// Rotates the given coordinate system (represented by the matrix) around MasterRight
function Pitch(const Matrix: TMatrix; const MasterRight: TAffineVector; angle: TGeoFloat): TMatrix; overload;
// Rotates the given coordinate system (represented by the matrix) around its Z-axis
function Roll(const Matrix: TMatrix; angle: TGeoFloat): TMatrix; overload;
// Rotates the given coordinate system (represented by the matrix) around MasterDirection
function Roll(const Matrix: TMatrix; const MasterDirection: TAffineVector; angle: TGeoFloat): TMatrix; overload;
// intersection functions
{ Compute the intersection point "res" of a line with a plane.
Return value: