From 964accb41a413a5d3df8ceaeee44eaf79d832bdf Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Sat, 13 Feb 2016 23:47:37 +0100 Subject: [PATCH] * some small changes to ugluVectorEx --- ugluVectorEx.inc | 41 +++++++++----- ugluVectorEx.pas | 8 ++- ugluVectorExHelper.pas | 118 ++++++++++++++++++++--------------------- 3 files changed, 95 insertions(+), 72 deletions(-) diff --git a/ugluVectorEx.inc b/ugluVectorEx.inc index 57def86..9a65f54 100644 --- a/ugluVectorEx.inc +++ b/ugluVectorEx.inc @@ -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} diff --git a/ugluVectorEx.pas b/ugluVectorEx.pas index 86ded69..13fa381 100644 --- a/ugluVectorEx.pas +++ b/ugluVectorEx.pas @@ -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 } diff --git a/ugluVectorExHelper.pas b/ugluVectorExHelper.pas index fb1b544..a9ad56d 100644 --- a/ugluVectorExHelper.pas +++ b/ugluVectorExHelper.pas @@ -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 = class(specialize TgluVectorHelperI) @@ -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; @@ -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.