Browse Source

* some small changes to ugluVectorEx

master
Bergmann89 8 years ago
parent
commit
964accb41a
3 changed files with 95 additions and 72 deletions
  1. +29
    -12
      ugluVectorEx.inc
  2. +7
    -1
      ugluVectorEx.pas
  3. +59
    -59
      ugluVectorExHelper.pas

+ 29
- 12
ugluVectorEx.inc View File

@@ -12,6 +12,10 @@
{$ERROR only vectors of size 2, 3 or 4 are supported}
{$ENDIF}

{$IFDEF __MAXCOLOR}
function ToColor: TColor;
{$ENDIF}

function ToString(const aRound: Integer = -3): String; inline;
class function FromString(const s: String): __VEC; static; inline;
class function TryFromString(const s: String; out v: __VEC): Boolean; static; inline;
@@ -425,6 +429,8 @@
function Subtract(const v: __VEC): __VEC; inline;
{$ENDIF}
function Multiply(const v: __IMPL.TBaseType): __VEC; inline;
function Clamp(const min, max: __VEC): __VEC; inline;
function Clamp(const min, max: __IMPL.TBaseType): __VEC; inline;
end;
{$IF __SIZE <> 4}
operator + (const v1, v2: __VEC): __VEC; inline;
@@ -442,8 +448,6 @@
{$IF __SIZE = 2}
function Angle2(const v: __VEC): Double; inline;
{$ENDIF}
function Clamp(const min, max: __VEC): __VEC; inline;
function Clamp(const min, max: __IMPL.TBaseType): __VEC; inline;
end;
operator * (const v: __VEC; const s: __IMPL.TBaseType): __VEC;
operator * (const s: __IMPL.TBaseType; const v: __VEC): __VEC;
@@ -470,6 +474,18 @@
end;
{$ENDIF}

{$IFDEF __MAXCOLOR}
function __HELPER.ToColor: TColor;
var tmp: __VEC;
begin
tmp := __IMPL.Clamp(self, 0, __MAXCOLOR);
result :=
(Round(255 * (tmp[0] / __MAXCOLOR)) shl 0) or
(Round(255 * (tmp[1] / __MAXCOLOR)) shl 8) or
(Round(255 * (tmp[2] / __MAXCOLOR)) shl 16);
end;
{$ENDIF}

function __HELPER.ToString(const aRound: Integer): String;
begin
result := __IMPL.ToString(self, aRound);
@@ -916,6 +932,16 @@
result := __IMPL.Multiply(self, v);
end;

function __HELPER_I.Clamp(const min, max: __VEC): __VEC;
begin
result := __IMPL.Clamp(self, min, max);
end;

function __HELPER_I.Clamp(const min, max: __IMPL.TBaseType): __VEC;
begin
result := __IMPL.Clamp(self, min, max);
end;

{$IF __SIZE <> 4}
operator + (const v1, v2: __VEC): __VEC;
begin
@@ -957,16 +983,6 @@
end;
{$ENDIF}

function __HELPER_F.Clamp(const min, max: __VEC): __VEC;
begin
result := __IMPL.Clamp(self, min, max);
end;

function __HELPER_F.Clamp(const min, max: __IMPL.TBaseType): __VEC;
begin
result := __IMPL.Clamp(self, min, max);
end;

operator * (const v: __VEC; const s: __IMPL.TBaseType): __VEC;
begin
result := __IMPl.Multiply(v, s);
@@ -989,6 +1005,7 @@
{$UNDEF __IMPL}
{$UNDEF __SIZE}
{$UNDEF __VEC}
{$UNDEF __MAXCOLOR}
{$UNDEF __HELPER}
{$UNDEF __HELPER_I}
{$UNDEF __HELPER_F}

+ 7
- 1
ugluVectorEx.pas View File

@@ -7,7 +7,7 @@ unit ugluVectorEx;
interface

uses
Classes, SysUtils,
Classes, SysUtils, Graphics,
ugluVectorExHelper;

type
@@ -184,6 +184,7 @@ type
{$DEFINE __VEC := TgluVector3ub}
{$DEFINE __HELPER := TgluTypeHelperVector3ub}
{$DEFINE __HELPER_I := TgluTypeHelperVector3ubi}
{$DEFINE __MAXCOLOR := 255}
{$I ugluVectorEx.inc}

{ TgluVector4ub }
@@ -212,6 +213,7 @@ type
{$DEFINE __HELPER := TgluTypeHelperVector3f}
{$DEFINE __HELPER_I := TgluTypeHelperVector3fi}
{$DEFINE __HELPER_F := TgluTypeHelperVector3ff}
{$DEFINE __MAXCOLOR := 1.0}
{$I ugluVectorEx.inc}

{ TgluVector4f }
@@ -241,6 +243,7 @@ type
{$DEFINE __HELPER := TgluTypeHelperVector3d}
{$DEFINE __HELPER_I := TgluTypeHelperVector3di}
{$DEFINE __HELPER_F := TgluTypeHelperVector3df}
{$DEFINE __MAXCOLOR := 1.0}
{$I ugluVectorEx.inc}

{ TgluVector4d }
@@ -368,6 +371,7 @@ implementation
{$DEFINE __VEC := TgluVector3ub}
{$DEFINE __HELPER := TgluTypeHelperVector3ub}
{$DEFINE __HELPER_I := TgluTypeHelperVector3ubi}
{$DEFINE __MAXCOLOR := 255}
{$I ugluVectorEx.inc}

{ TgluVector4ub }
@@ -396,6 +400,7 @@ implementation
{$DEFINE __HELPER := TgluTypeHelperVector3f}
{$DEFINE __HELPER_I := TgluTypeHelperVector3fi}
{$DEFINE __HELPER_F := TgluTypeHelperVector3ff}
{$DEFINE __MAXCOLOR := 1.0}
{$I ugluVectorEx.inc}

{ TgluVector4ub }
@@ -425,6 +430,7 @@ implementation
{$DEFINE __HELPER := TgluTypeHelperVector3d}
{$DEFINE __HELPER_I := TgluTypeHelperVector3di}
{$DEFINE __HELPER_F := TgluTypeHelperVector3df}
{$DEFINE __MAXCOLOR := 1.0}
{$I ugluVectorEx.inc}

{ TgluVector4d }


+ 59
- 59
ugluVectorExHelper.pas View File

@@ -56,6 +56,14 @@ type

class function Sub(const v1, v2: TVector2): TVector2; overload; inline;
class function Sub(const v1, v2: TVector3): TVector3; overload; inline;

class function ClampVal(const v, aMin, aMax: T): T; overload; inline;
class function Clamp(const v, aMin, aMax: TVector2): TVector2; overload; inline;
class function Clamp(const v, aMin, aMax: TVector3): TVector3; overload; inline;
class function Clamp(const v, aMin, aMax: TVector4): TVector4; overload; inline;
class function Clamp(const v: TVector2; const aMin, aMax: T): TVector2; overload; inline;
class function Clamp(const v: TVector3; const aMin, aMax: T): TVector3; overload; inline;
class function Clamp(const v: TVector4; const aMin, aMax: T): TVector4; overload; inline;
end;

generic TgluVectorHelperF<T> = class(specialize TgluVectorHelperI<T>)
@@ -77,14 +85,6 @@ type
class function Angle(const v1: TVector2; const v2: TVector2): Double; overload; inline;
class function Angle(const v1: TVector3; const v2: TVector3): Double; overload; inline;
class function Angle2(const v1: TVector2; const v2: TVector2): Double; overload; inline;

class function ClampVal(const v, aMin, aMax: T): T; overload; inline;
class function Clamp(const v, aMin, aMax: TVector2): TVector2; overload; inline;
class function Clamp(const v, aMin, aMax: TVector3): TVector3; overload; inline;
class function Clamp(const v, aMin, aMax: TVector4): TVector4; overload; inline;
class function Clamp(const v: TVector2; const aMin, aMax: T): TVector2; overload; inline;
class function Clamp(const v: TVector3; const aMin, aMax: T): TVector3; overload; inline;
class function Clamp(const v: TVector4; const aMin, aMax: T): TVector4; overload; inline;
end;

TgluVectorP = specialize TgluVectorHelper<Pointer>;
@@ -433,6 +433,57 @@ begin
result[2] := v1[2] - v2[2];
end;

class function TgluVectorHelperI.ClampVal(const v, aMin, aMax: T): T;
begin
if (v < aMin) then
result := aMin
else if (v > aMax) then
result := aMax
else
result := v;
end;

class function TgluVectorHelperI.Clamp(const v, aMin, aMax: TVector2): TVector2;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
end;

class function TgluVectorHelperI.Clamp(const v, aMin, aMax: TVector3): TVector3;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
result[2] := ClampVal(v[2], aMin[2], aMax[2]);
end;

class function TgluVectorHelperI.Clamp(const v, aMin, aMax: TVector4): TVector4;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
result[2] := ClampVal(v[2], aMin[2], aMax[2]);
result[3] := ClampVal(v[3], aMin[3], aMax[3]);
end;

class function TgluVectorHelperI.Clamp(const v: TVector2; const aMin, aMax: T): TVector2;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
end;

class function TgluVectorHelperI.Clamp(const v: TVector3; const aMin, aMax: T): TVector3;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
result[2] := ClampVal(v[2], aMin, aMax);
end;

class function TgluVectorHelperI.Clamp(const v: TVector4; const aMin, aMax: T): TVector4;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
result[2] := ClampVal(v[2], aMin, aMax);
end;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TgluVectorHelperF/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -521,56 +572,5 @@ begin
v2[0] * v1[0] + v2[1] * v1[1]);
end;

class function TgluVectorHelperF.ClampVal(const v, aMin, aMax: T): T;
begin
if (v < aMin) then
result := aMin
else if (v > aMax) then
result := aMax
else
result := v;
end;

class function TgluVectorHelperF.Clamp(const v, aMin, aMax: TVector2): TVector2;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
end;

class function TgluVectorHelperF.Clamp(const v, aMin, aMax: TVector3): TVector3;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
result[2] := ClampVal(v[2], aMin[2], aMax[2]);
end;

class function TgluVectorHelperF.Clamp(const v, aMin, aMax: TVector4): TVector4;
begin
result[0] := ClampVal(v[0], aMin[0], aMax[0]);
result[1] := ClampVal(v[1], aMin[1], aMax[1]);
result[2] := ClampVal(v[2], aMin[2], aMax[2]);
result[3] := ClampVal(v[3], aMin[3], aMax[3]);
end;

class function TgluVectorHelperF.Clamp(const v: TVector2; const aMin, aMax: T): TVector2;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
end;

class function TgluVectorHelperF.Clamp(const v: TVector3; const aMin, aMax: T): TVector3;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
result[2] := ClampVal(v[2], aMin, aMax);
end;

class function TgluVectorHelperF.Clamp(const v: TVector4; const aMin, aMax: T): TVector4;
begin
result[0] := ClampVal(v[0], aMin, aMax);
result[1] := ClampVal(v[1], aMin, aMax);
result[2] := ClampVal(v[2], aMin, aMax);
end;

end.


Loading…
Cancel
Save