delphi10.2的向量数学处理单元Math.Vectors.pas

向量数学的计算 

type
  TEpsilon = record
  const
    Matrix = 1E-5; //矩阵
    Vector = 1E-4; //向量
  end;

  TVector3DType = array [0..3] of Single;  //3维向量数组

  TVectorArray = array [0..2] of Single;  //向量数组

  TPolygon = array of TPointF; //多边形

  TCubicBezier = array [0..3] of TPointF; //3次贝塞尔

  tagVECTOR = record
    case Integer of
      0: (V: TVectorArray;);
      1: (X: Single;
          Y: Single;
          W: Single;);
  end;

1. 矢量

//矢量
  PVector = ^TVector;
  TVector = record
    class function Create(const AX, AY: Single; const AW: Single = 1.0): TVector; overload; static; inline;
    class function Create(const APoint: TPointF): TVector; overload; static; inline;

    class operator Add(const AVector1, AVector2: TVector): TVector; //加法
    class operator Subtract(const AVector1, AVector2: TVector): TVector;  //减法
    class operator Equal(const AVector1, AVector2: TVector): Boolean; inline;//是否相等
    class operator NotEqual(const AVector1, AVector2: TVector): Boolean; inline;  //是否不相等
    class operator Implicit(const APoint: TPointF): TVector; inline;  //隐示
    class operator Explicit(const APoint: TVector): TPointF;           //显式
    class operator Implicit(const APoint: TVector): TPointF; inline; deprecated 'Implicit conversion from TVector to TPointF requires homogenization';
    class operator Implicit(const ASize: TSizeF): TVector; inline;
    class operator Multiply(const AVector: TVector; const AFactor: Single): TVector;  //乘法
    class operator Multiply(const AFactor: Single; const AVector: TVector): TVector; inline; //乘法
    class operator Divide(const AVector: TVector; const AFactor: Single): TVector;  //除法

    /// <summary> 零向量具有(0,0,0)值。 </summary>
    class function Zero: TVector; inline; static;

    function Length: Single;
    function Normalize: TVector;
    function CrossProduct(const AVector: TVector): TVector; //叉积,交叉乘法
    function DotProduct(const AVector: TVector): Single;  //点积,点乘法
    function MidVector(const AVector: TVector): TVector;

    function ToPointF: TPointF; inline; deprecated 'Use explicit typecast instead.';

    case Integer of
      0: (V: TVectorArray;);
      1: (X: Single;
          Y: Single;
          W: Single;);
  end;

2. 3D点

TPoint3D = record
    type
      TPoint3DArray = array [0..2] of Single;
    class function Create(const AX, AY, AZ: Single): TPoint3D; overload; static; inline;
    class function Create(const P: TVector3DType): TPoint3D; overload; static; inline;
    class function Create(const APoint: TPointF; const AZ: Single = 0.0): TPoint3D; overload; static; inline;

    class operator Add(const APoint1, APoint2: TPoint3D): TPoint3D;
    class operator Subtract(const APoint1, APoint2: TPoint3D): TPoint3D;
    class operator Equal(const APoint1, APoint2: TPoint3D): Boolean; inline;
    class operator NotEqual(const APoint1, APoint2: TPoint3D): Boolean; inline;
    class operator Negative(const APoint: TPoint3D): TPoint3D;
    class operator Multiply(const APoint1, APoint2: TPoint3D): TPoint3D;
    class operator Multiply(const APoint: TPoint3D; const AFactor: Single): TPoint3D; inline;
    class operator Multiply(const AFactor: Single; const APoint: TPoint3D): TPoint3D; inline;
    class operator Divide(const APoint: TPoint3D; const AFactor: Single): TPoint3D;

    /// <summary> 0点有(0, 0, 0)值. </summary>
    class function Zero: TPoint3D; inline; static;

    procedure Offset(const ADelta: TPoint3D); overload; inline;
    procedure Offset(const ADeltaX, ADeltaY, ADeltaZ: Single); overload; inline;

    function CrossProduct(const APoint: TPoint3D): TPoint3D;
    function DotProduct(const APoint: TPoint3D): Single; inline;
    function EqualsTo(const APoint: TPoint3D; const Epsilon: Single = 0): Boolean; inline;

    function Length: Single; inline;
    function Normalize: TPoint3D;
    function Distance(const APoint: TPoint3D): Single;
    function Rotate(const AAxis: TPoint3D; const AAngle: Single): TPoint3D; inline;
    function Reflect(const APoint: TPoint3D): TPoint3D; inline;
    function MidPoint(const APoint: TPoint3D): TPoint3D; inline;
    function AngleCosine(const APoint: TPoint3D): Single;

    case Integer of
      0: (V: TPoint3DArray;);
      1: (X: Single;
          Y: Single;
          Z: Single;);
  end;
  PPoint3D = ^TPoint3D;

  tagVECTOR3D = record
    case Integer of
      0: (V: TVector3DType;);
      1: (X: Single;
          Y: Single;
          Z: Single;
          W: Single;);
  end;


  TMatrixArray = array [0..2] of TVector;
  TMaxtrixArrayBase = array[0..2] of tagVECTOR;
  {$NODEFINE TMatrixArray 'System::Math::Vectors::TMatrixArray' 'System::Math::Vectors::TMaxtrixArrayBase'}
  (*$HPPEMIT END OPENNAMESPACE*)
  (*$HPPEMIT END 'typedef TVector TMatrixArray[3];'*)
  (*$HPPEMIT END CLOSENAMESPACE*)

3.  矩阵

 TMatrix = record
  private
    function Scale(const AFactor: Single): TMatrix;
  public
    class function CreateRotation(const AAngle: Single): TMatrix; static;
    class function CreateScaling(const AScaleX, AScaleY: Single): TMatrix; static;
    class function CreateTranslation(const ADeltaX, ADeltaY: Single): TMatrix; static;

    class operator Multiply(const AMatrix1, AMatrix2: TMatrix): TMatrix;
    class operator Multiply(const APoint: TPointF; const AMatrix: TMatrix): TPointF;
    class operator Multiply(const AVector: TVector; const AMatrix: TMatrix): TVector;
    class operator Multiply(const AVector: TPoint3D; const AMatrix: TMatrix): TPoint3D;
    /// <summary>Equal运算符,使用默认值epsilon调用EqualsTo函数。</summary>
    class operator Equal(const RightMatrix, LeftMatrix: TMatrix): Boolean; static;

    function Determinant: Single;
    function Adjoint: TMatrix;
    function Inverse: TMatrix;
    function ExtractScale: TPointF;
    /// <summary>如果所有的值都与给定的矩阵相同使用ε误差阈值,
    ///    这个函数返回true,否则,返回false。</summary>
    function EqualsTo(const AMatrix: TMatrix; const Epsilon: Single = TEpsilon.Matrix): Boolean;

    case Integer of
      0: (M: TMatrixArray;);
      1: (m11, m12, m13: Single;
          m21, m22, m23: Single;
          m31, m32, m33: Single);
  end;

  TMatrixConstants = record helper for TMatrix
    const Identity: TMatrix = (m11: 1; m12: 0; m13: 0; m21: 0; m22: 1; m23: 0; m31: 0; m32: 0; m33: 1);
  end;

4. 3D矢量

//3D矢量
  PVector3D = ^TVector3D;
  TVector3D = record
    class function Create(const AX, AY, AZ: Single; const AW: Single = 1.0): TVector3D; overload; static; inline;
    class function Create(const APoint: TPoint3D; const AW: Single = 1.0): TVector3D; overload; static; inline;

    class operator Add(const AVector1, AVector2: TVector3D): TVector3D;
    class operator Subtract(const AVector1, AVector2: TVector3D): TVector3D;
    class operator Equal(const AVector1, AVector2: TVector3D): Boolean;
    class operator NotEqual(const AVector1, AVector2: TVector3D): Boolean;
    class operator Negative(const AVector: TVector3D): TVector3D;
    class operator Implicit(const APoint: TPoint3D): TVector3D;
    class operator Explicit(const AVector: TVector3D): TPoint3D;
    class operator Implicit(const AVector: TVector3D): TPoint3D; inline; deprecated 'Implicit conversion from TVector3D to TPoint3D requires homogenization';
    class operator Multiply(const AVector1, AVector2: TVector3D): TVector3D;
    class operator Multiply(const AVector: TVector3D; const AFactor: Single): TVector3D; inline;
    class operator Multiply(const AFactor: Single; const AVector: TVector3D): TVector3D; inline;
    class operator Divide(const AVector: TVector3D; const AFactor: Single): TVector3D;

    /// <summary> 0向量具有(0,0,0,0)的值。 </summary>
    class function Zero: TVector3D; inline; static;

    procedure Offset(const ADelta: TPoint3D); overload; inline; deprecated 'Use TPoint3D.Offset';
    procedure Offset(const ADeltaX, ADeltaY, ADeltaZ: Single); overload; inline; deprecated 'Use TPoint3D.Offset';

    function CrossProduct(const AVector: TVector3D): TVector3D; deprecated 'Use TPoint3D.CrossProduct';
    function DotProduct(const AVector: TVector3D): Single; deprecated 'Use TPoint3D.DotProduct';
    function EqualsTo(const AVector: TVector3D; const Epsilon: Single = 0): Boolean; inline;

    function Length: Single;
    function Normalize: TVector3D;
    function Distance(const AVector: TVector3D): Single;
    function Rotate(const AAxis: TPoint3D; const AAngle: Single): TVector3D; inline; deprecated 'Use TPoint3D.Rotate';
    function Reflect(const AVector: TVector3D): TVector3D; inline; deprecated 'Use TPoint3D.Reflect';
    function MidVector(const AVector: TVector3D): TVector3D;
    function AngleCosine(const AVector: TVector3D): Single; deprecated 'Use TPoint3D.AngleCosine';

    // 将4D(3D+W)矢量转换为3D矢量(当ATransform为True时,除以W。divides by W)
    function ToPoint3D(const ATransform: Boolean = False): TPoint3D; deprecated 'Use explicit typecast instead.';

    case Integer of
      0: (V: TVector3DType;);
      1: (X: Single;
          Y: Single;
          Z: Single;
          W: Single;);
  end;

  TVector3DArray = array [0..2] of TVector3D;
  {$NODEFINE TVector3DArray 'System::Types::TVector3DArray' 'System::Types::TVector3DArrayBase'}
  (*$HPPEMIT END OPENNAMESPACE*)
  (*$HPPEMIT END 'typedef TVector3D TVector3DArray[3];'*)
  (*$HPPEMIT END CLOSENAMESPACE*)
  TVector3DArrayBase = array[0..2] of tagVECTOR3D;

  TMatrix3DType = array [0..3] of TVector3D;
  {$NODEFINE TMatrix3DType 'System::Math::Vectors::TMatrix3DType' 'System::Math::Vectors::TMatrix3DTypeBase'}
  (*$HPPEMIT END OPENNAMESPACE*)
  (*$HPPEMIT END 'typedef TVector3D TMatrix3DType[3];'*)
  (*$HPPEMIT END CLOSENAMESPACE*)
  TMatrix3DTypeBase = array[0..3] of tagVECTOR3D;

5. 3D矩阵

//3D矩阵
  TMatrix3D = record
  private
    function DetInternal(const a1, a2, a3, b1, b2, b3, c1, c2, c3: Single): Single; inline;
    function Scale(const AFactor: Single): TMatrix3D;
  public
    // 通过指定的单个值来创建矩阵。
    constructor Create(const AM11, AM12, AM13, AM14, AM21, AM22, AM23, AM24, AM31, AM32, AM33,
      AM34, AM41, AM42, AM43, AM44: Single); overload;
    constructor Create(const AArray: TSingleDynArray); overload;

    // 创建简单的变换矩阵。
    class function CreateScaling(const AScale: TPoint3D): TMatrix3D; static;
    class function CreateTranslation(const ATranslation: TPoint3D): TMatrix3D; static;

    // 在其中一个轴上创建旋转矩阵(工作速度比复合变换快)
    class function CreateRotationX(const AAngle: Single): TMatrix3D; static;
    class function CreateRotationY(const AAngle: Single): TMatrix3D; static;
    class function CreateRotationZ(const AAngle: Single): TMatrix3D; static;

    // 复合旋转矩阵的创建(比简单的变换更慢但更灵活)
    class function CreateRotation(const AAxis: TPoint3D; const AAngle: Single): TMatrix3D; static;
    class function CreateRotationYawPitchRoll(const AYaw, APitch, ARoll: Single): TMatrix3D; static;
    class function CreateRotationHeadingPitchBank(const AHeading, APitch, ABank: Single): TMatrix3D; static;

    // 创建视图/摄像机矩阵。
    class function CreateLookAtRH(const ASource, ATarget, ACeiling: TPoint3D): TMatrix3D; static;
    class function CreateLookAtLH(const ASource, ATarget, ACeiling: TPoint3D): TMatrix3D; static;
    class function CreateLookAtDirRH(const ASource, ADirection, ACeiling: TPoint3D): TMatrix3D; static;
    class function CreateLookAtDirLH(const ASource, ADirection, ACeiling: TPoint3D): TMatrix3D; static;
    class function CreateOrthoLH(const AWidth, AHeight, AZNear, AZFar: Single): TMatrix3D; static;
    class function CreateOrthoRH(const AWidth, AHeight, AZNear, AZFar: Single): TMatrix3D; static;
    class function CreateOrthoOffCenterLH(const ALeft, ATop, ARight, ABottom, AZNear, AZFar: Single): TMatrix3D; static;
    class function CreateOrthoOffCenterRH(const ALeft, ATop, ARight, ABottom, AZNear, AZFar: Single): TMatrix3D; static;
    class function CreatePerspectiveFovLH(const AFOV, AAspect, AZNear, AZFar: Single;
     const AHorizontalFOV: Boolean = False): TMatrix3D; static;
    class function CreatePerspectiveFovRH(const AFOV, AAspect, AZNear, AZFar: Single;
     const AHorizontalFOV: Boolean = False): TMatrix3D; static;
    // 两个3D矩阵的乘法
    class operator Multiply(const APoint1, APoint2: TMatrix3D): TMatrix3D;
    // 3D矢量和矩阵的乘法
    class operator Multiply(const APoint: TPoint3D; const AMatrix: TMatrix3D): TPoint3D;
    // 4D(3D + w) 矢量和3D (4x4)矩阵 的乘法
    class operator Multiply(const AVector: TVector3D; const AMatrix: TMatrix3D): TVector3D;

    function Transpose: TMatrix3D;
    function Determinant: Single;
    function Adjoint: TMatrix3D;
    function Inverse: TMatrix3D;

    function ToMatrix: TMatrix;

    // 从3D矩阵计算眼睛位置
    function EyePosition: TPoint3D;

    case Integer of
      0: (M: TMatrix3DType;);
      1: (m11, m12, m13, m14: Single;
          m21, m22, m23, m24: Single;
          m31, m32, m33, m34: Single;
          m41, m42, m43, m44: Single);
  end;

  TMatrix3DConstants = record helper for TMatrix3D
    const Identity: TMatrix3D = (m11: 1; m12: 0; m13: 0; m14: 0; m21: 0; m22: 1; m23: 0; m24: 0; m31: 0; m32: 0;
      m33: 1; m34: 0; m41: 0; m42: 0; m43: 0; m44: 1;);
  end;

6. 3D四元数

 //3D四元数
  TQuaternion3D = record
    constructor Create(const AAxis: TPoint3D; const AAngle: Single); overload;
    constructor Create(const AYaw, APitch, ARoll: Single); overload;
    constructor Create(const AMatrix: TMatrix3D); overload;

    class operator Implicit(const AQuaternion: TQuaternion3D): TMatrix3D;
    class operator Multiply(const AQuaternion1, AQuaternion2: TQuaternion3D): TQuaternion3D;

    // 计算四元数的量级(quaternion magnitude)
    function Length: Single;
    function Normalize: TQuaternion3D;

    case Integer of
      0: (V: TVector3DType;);
      1: (ImagPart: TPoint3D;
          RealPart: Single;);
  end;

  TQuaternion3DConstants = record helper for TQuaternion3D
    const Identity: TQuaternion3D = (ImagPart: (X: 0; Y: 0; Z: 0); RealPart: 1);
  end;

7. 常量

  //常量
const
  NullVector3D: TVector3D = (X: 0; Y: 0; Z: 0; W: 1);
  NullPoint3D: TPoint3D = (X: 0; Y: 0; Z: 0);

function Vector(const X, Y: Single; const W: Single = 1.0): TVector; overload;
function Vector(const P: TPointF; const W: Single = 1.0): TVector; overload;
function Vector3D(const X, Y, Z: Single; const W: Single = 1.0): TVector3D; overload;
function Vector3D(const P: TPoint3D; const W: Single = 1.0): TVector3D; overload;
function Point3D(const X, Y, Z: Single): TPoint3D; overload;
function Point3D(const AVector3D: TVector3D; const ATransform: Boolean = False): TPoint3D; overload; deprecated 'Use direct typecast instead.';
function PointF(const V: TVector): TPointF; inline; overload;


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独的学者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值