From 1a7a1350d464b8572f20e86ac0316f1d2b3426e2 Mon Sep 17 00:00:00 2001 From: Olly Date: Sun, 25 Feb 2024 16:15:30 +0000 Subject: [PATCH] Clean up ATPA exclude/extract*** --- Source/array/simba.array_pointarray.pas | 82 +++++++++++++++++---- Source/script/imports/simba.import_atpa.pas | 60 +++++++++++---- Source/simba.quad.pas | 10 +-- Tests/atpa_excludeextract.simba | 40 ++++++++++ 4 files changed, 156 insertions(+), 36 deletions(-) create mode 100644 Tests/atpa_excludeextract.simba diff --git a/Source/array/simba.array_pointarray.pas b/Source/array/simba.array_pointarray.pas index 79849130b..4863b2494 100644 --- a/Source/array/simba.array_pointarray.pas +++ b/Source/array/simba.array_pointarray.pas @@ -40,9 +40,13 @@ interface function SortByShortSide(LowToHigh: Boolean): T2DPointArray; function SortByLongSide(LowToHigh: Boolean): T2DPointArray; - function ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; overload; - function ExcludeSize(MinLen, MaxLen: Integer): T2DPointArray; overload; - function ExcludeSizeEx(MaxLen: Integer): T2DPointArray; + function ExtractSize(Len: Integer; KeepIf: EComparator): T2DPointArray; + function ExtractSizeEx(MinLen, MaxLen: Integer): T2DPointArray; + function ExtractDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray; + function ExtractDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; + + function ExcludeSize(Len: Integer; RemoveIf: EComparator): T2DPointArray; + function ExcludeSizeEx(MinLen, MaxLen: Integer): T2DPointArray; function ExcludeDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray; function ExcludeDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; @@ -122,7 +126,7 @@ function T2DPointArrayHelper.SortFromFirstPoint(From: TPoint): T2DPointArray; I: Integer; Weights: TIntegerArray; begin - Result := Self.ExcludeSize(0, __EQ__); + Result := Self.ExtractSize(0, __GT__); SetLength(Weights, Length(Self)); for I := 0 to High(Self) do @@ -136,7 +140,7 @@ function T2DPointArrayHelper.SortFromFirstPointX(From: TPoint): T2DPointArray; I: Integer; Weights: TIntegerArray; begin - Result := Self.ExcludeSize(0, __EQ__); + Result := Self.ExtractSize(0, __GT__); SetLength(Weights, Length(Self)); for I := 0 to High(Self) do @@ -150,7 +154,7 @@ function T2DPointArrayHelper.SortFromFirstPointY(From: TPoint): T2DPointArray; I: Integer; Weights: TIntegerArray; begin - Result := Self.ExcludeSize(0, __GT__); + Result := Self.ExtractSize(0, __GT__); SetLength(Weights, Length(Self)); for I := 0 to High(Self) do @@ -274,7 +278,7 @@ function T2DPointArrayHelper.SortByLongSide(LowToHigh: Boolean): T2DPointArray; Result := Sort(Weights, LowToHigh); end; -function T2DPointArrayHelper.ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; +function T2DPointArrayHelper.ExtractSize(Len: Integer; KeepIf: EComparator): T2DPointArray; var I: Integer; Buffer: TSimbaPointArrayBuffer; @@ -294,7 +298,7 @@ function T2DPointArrayHelper.ExcludeSize(Len: Integer; KeepIf: EComparator): T2D Result := Buffer.ToArray(False); end; -function T2DPointArrayHelper.ExcludeSize(MinLen, MaxLen: Integer): T2DPointArray; +function T2DPointArrayHelper.ExtractSizeEx(MinLen, MaxLen: Integer): T2DPointArray; var I: Integer; Buffer: TSimbaPointArrayBuffer; @@ -305,12 +309,61 @@ function T2DPointArrayHelper.ExcludeSize(MinLen, MaxLen: Integer): T2DPointArray if InRange(Length(Self[I]), MinLen, MaxLen) then Buffer.Add(Copy(Self[I])); - Result := Buffer.ToArray(False);; + Result := Buffer.ToArray(False); end; -function T2DPointArrayHelper.ExcludeSizeEx(MaxLen: Integer): T2DPointArray; +function T2DPointArrayHelper.ExtractDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray; +var + I: Integer; + Buffer: TSimbaPointArrayBuffer; begin - Result := Self.ExcludeSize(0, MaxLen); + Buffer.Init(Length(Self)); + + for I := 0 to High(Self) do + with Self[I].MinAreaRect() do + if InRange(ShortSideLen, MinShortSide, MaxShortSide) and InRange(LongSideLen, MinLongSide, MaxLongSide) then + Buffer.Add(Copy(Self[I])); + + Result := Buffer.ToArray(False); + end; + +function T2DPointArrayHelper.ExtractDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; +begin + Result := Self.ExtractDimensions(MinShortSide, MinLongSide, Integer.MaxValue, Integer.MaxValue); +end; + +function T2DPointArrayHelper.ExcludeSize(Len: Integer; RemoveIf: EComparator): T2DPointArray; +var + I: Integer; + Buffer: TSimbaPointArrayBuffer; +begin + Buffer.Init(Length(Self)); + + for I := 0 to High(Self) do + case RemoveIf of + __LT__: if not (Length(Self[I]) < Len) then Buffer.Add(Copy(Self[I])); + __GT__: if not (Length(Self[I]) > Len) then Buffer.Add(Copy(Self[I])); + __EQ__: if not (Length(Self[I]) = Len) then Buffer.Add(Copy(Self[I])); + __LE__: if not (Length(Self[I]) <= Len) then Buffer.Add(Copy(Self[I])); + __GE__: if not (Length(Self[I]) >= Len) then Buffer.Add(Copy(Self[I])); + __NE__: if not (Length(Self[I]) <> Len) then Buffer.Add(Copy(Self[I])); + end; + + Result := Buffer.ToArray(False); +end; + +function T2DPointArrayHelper.ExcludeSizeEx(MinLen, MaxLen: Integer): T2DPointArray; +var + I: Integer; + Buffer: TSimbaPointArrayBuffer; +begin + Buffer.Init(Length(Self)); + + for I := 0 to High(Self) do + if not InRange(Length(Self[I]), MinLen, MaxLen) then + Buffer.Add(Copy(Self[I])); + + Result := Buffer.ToArray(False); end; function T2DPointArrayHelper.ExcludeDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray; @@ -322,11 +375,11 @@ function T2DPointArrayHelper.ExcludeDimensions(MinShortSide, MinLongSide, MaxSho for I := 0 to High(Self) do with Self[I].MinAreaRect() do - if InRange(ShortSideLen, MinShortSide, MaxShortSide) and InRange(LongSideLen, MinLongSide, MaxLongSide) then + if not (InRange(ShortSideLen, MinShortSide, MaxShortSide) and InRange(LongSideLen, MinLongSide, MaxLongSide)) then Buffer.Add(Copy(Self[I])); - Result := Buffer.ToArray(False);; -end; + Result := Buffer.ToArray(False); + end; function T2DPointArrayHelper.ExcludeDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; begin @@ -451,6 +504,5 @@ function T2DPointArrayHelper.Intersection: TPointArray; Result := Buffer.ToArray(False);; end; - end. diff --git a/Source/script/imports/simba.import_atpa.pas b/Source/script/imports/simba.import_atpa.pas index d97d54a66..7e0ba44e7 100644 --- a/Source/script/imports/simba.import_atpa.pas +++ b/Source/script/imports/simba.import_atpa.pas @@ -193,33 +193,63 @@ procedure _LapeATPA_SortByLongSide(const Params: PParamArray; const Result: Poin end; (* -T2DPointArray.ExcludeSize +T2DPointArray.ExtractSize ------------------------- -> function T2DPointArray.ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; +> function T2DPointArray.ExtractSize(Len: Integer; KeepIf: EComparator): T2DPointArray; *) -procedure _LapeATPA_ExcludeSize1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +procedure _LapeATPA_ExtractSize(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV begin - P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExcludeSize(PInteger(Params^[1])^, PComparator(Params^[2])^); + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExtractSize(PInteger(Params^[1])^, PComparator(Params^[2])^); +end; + +(* +T2DPointArray.ExtractSizeEx +--------------------------- +> function T2DPointArray.ExtractSize(Len: Integer; KeepIf: EComparator): T2DPointArray; +*) +procedure _LapeATPA_ExtractSizeEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExtractSizeEx(PInteger(Params^[1])^, PInteger(Params^[2])^); +end; + +(* +T2DPointArray.ExtractDimensions +------------------------------- +> function T2DPointArray.ExtractDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray; +*) +procedure _LapeATPA_ExtractDimensions(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExtractDimensions(PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^); +end; + +(* +T2DPointArray.ExtractDimensionsEx +--------------------------------- +> function T2DPointArray.ExtractDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; +*) +procedure _LapeATPA_ExtractDimensionsEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExtractDimensionsEx(PInteger(Params^[1])^, PInteger(Params^[2])^); end; (* T2DPointArray.ExcludeSize ------------------------- -> function T2DPointArray.ExcludeSize(MinLen, MaxLen: Integer): T2DPointArray; +> function T2DPointArray.ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; *) -procedure _LapeATPA_ExcludeSize2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +procedure _LapeATPA_ExcludeSize(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV begin - P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExcludeSize(PInteger(Params^[1])^, PInteger(Params^[2])^); + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExcludeSize(PInteger(Params^[1])^, PComparator(Params^[2])^); end; (* T2DPointArray.ExcludeSizeEx --------------------------- -> function T2DPointArray.ExcludeSizeEx(MaxLen: Integer): T2DPointArray; +> function T2DPointArray.ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; *) procedure _LapeATPA_ExcludeSizeEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV begin - P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExcludeSizeEx(PInteger(Params^[1])^); + P2DPointArray(Result)^ := P2DPointArray(Params^[0])^.ExcludeSizeEx(PInteger(Params^[1])^, PInteger(Params^[2])^); end; (* @@ -235,7 +265,7 @@ procedure _LapeATPA_ExcludeDimensions(const Params: PParamArray; const Result: P (* T2DPointArray.ExcludeDimensionsEx --------------------------------- -> function T2DPointArray.ExcludeDimensionsEx(MaxShortSide, MaxLongSide: Integer): T2DPointArray; +> function T2DPointArray.ExcludeDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray; *) procedure _LapeATPA_ExcludeDimensionsEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV begin @@ -353,9 +383,13 @@ procedure ImportATPA(Compiler: TSimbaScript_Compiler); addGlobalFunc('function T2DPointArray.SortByShortSide(LowToHigh: Boolean): T2DPointArray', @_LapeATPA_SortByShortSide); addGlobalFunc('function T2DPointArray.SortByLongSide(LowToHigh: Boolean): T2DPointArray', @_LapeATPA_SortByLongSide); - addGlobalFunc('function T2DPointArray.ExcludeSize(Len: Integer; KeepIf: EComparator): T2DPointArray; overload', @_LapeATPA_ExcludeSize1); - addGlobalFunc('function T2DPointArray.ExcludeSize(MinLen, MaxLen: Integer): T2DPointArray; overload', @_LapeATPA_ExcludeSize2); - addGlobalFunc('function T2DPointArray.ExcludeSizeEx(MaxLen: Integer): T2DPointArray', @_LapeATPA_ExcludeSizeEx); + addGlobalFunc('function T2DPointArray.ExtractSize(Len: Integer; KeepIf: EComparator): T2DPointArray', @_LapeATPA_ExtractSize); + addGlobalFunc('function T2DPointArray.ExtractSizeEx(MinLen, MaxLen: Integer): T2DPointArray', @_LapeATPA_ExtractSizeEx); + addGlobalFunc('function T2DPointArray.ExtractDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray', @_LapeATPA_ExtractDimensions); + addGlobalFunc('function T2DPointArray.ExtractDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray', @_LapeATPA_ExtractDimensionsEx); + + addGlobalFunc('function T2DPointArray.ExcludeSize(Len: Integer; RemoveIf: EComparator): T2DPointArray', @_LapeATPA_ExcludeSize); + addGlobalFunc('function T2DPointArray.ExcludeSizeEx(MinLen, MaxLen: Integer): T2DPointArray', @_LapeATPA_ExcludeSizeEx); addGlobalFunc('function T2DPointArray.ExcludeDimensions(MinShortSide, MinLongSide, MaxShortSide, MaxLongSide: Integer): T2DPointArray', @_LapeATPA_ExcludeDimensions); addGlobalFunc('function T2DPointArray.ExcludeDimensionsEx(MinShortSide, MinLongSide: Integer): T2DPointArray', @_LapeATPA_ExcludeDimensionsEx); diff --git a/Source/simba.quad.pas b/Source/simba.quad.pas index 9820d637c..f6f60b99f 100644 --- a/Source/simba.quad.pas +++ b/Source/simba.quad.pas @@ -124,18 +124,12 @@ function TQuadHelper.Bounds: TBox; function TQuadHelper.ShortSideLen: Integer; begin - if (Hypot(Left.Y-Top.Y, Left.X-Top.X) < Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X)) then - Result := Round(Hypot(Left.Y-Top.Y, Left.X-Top.X) / 2) - else - Result := Round(Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X) / 2); + Result := Round(Min(Hypot(Left.Y-Top.Y, Left.X-Top.X), Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X))); end; function TQuadHelper.LongSideLen: Integer; begin - if (Hypot(Left.Y-Top.Y, Left.X-Top.X) > Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X)) then - Result := Round(Hypot(Left.Y-Top.Y, Left.X-Top.X) / 2) - else - Result := Round(Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X) / 2); + Result := Round(Max(Hypot(Left.Y-Top.Y, Left.X-Top.X), Hypot(Left.Y-Bottom.Y, Left.X-Bottom.X))); end; function TQuadHelper.Mean: TPoint; diff --git a/Tests/atpa_excludeextract.simba b/Tests/atpa_excludeextract.simba new file mode 100644 index 000000000..2214d87fd --- /dev/null +++ b/Tests/atpa_excludeextract.simba @@ -0,0 +1,40 @@ +{$assertions on} + +procedure test(atpa: T2DPointArray; expected: T2DPointArray); +var + i: Integer; +begin + Assert(Length(atpa) = Length(expected)); + for i:=0 to High(expected) do + Assert(atpa[i].Equals(expected[i])); +end; + +var + a,b,c,d,e: TPointArray; + atpa: T2DPointArray; +begin + a += TPointArray.CreateFromBox([10,10,20,20],True); + b += TPointArray.CreateFromBox([40,40,60,60],True); + c += TPointArray.CreateFromBox([80,80,120,120],True); + d += TPointArray.CreateFromBox([140,140,200,200],True); + e += TPointArray.CreateFromBox([220,220,320,320],True); + + atpa := [a,b,c,d,e]; + + test(atpa.ExcludeSize(550, __LT__), [c,d,e]); + test(atpa.ExtractSize(550, __LT__), [a,b]); + + test(atpa.ExcludeSize(550, __GT__), [a,b]); + test(atpa.ExtractSize(550, __GT__), [c,d,e]); + + test(atpa.ExcludeSize(1681, __EQ__), [a,b,d,e]); + test(atpa.ExtractSize(1681, __EQ__), [c]); + test(atpa.ExtractSizeEx(400,1700), [b,c]); + + test(atpa.ExtractDimensions(20,20,40,40), [b,c]); + test(atpa.ExtractDimensionsEx(41,41), [d,e]); + + test(atpa.ExcludeDimensions(20,20,40,40), [a,d,e]); + test(atpa.ExcludeDimensionsEx(41,41), [a,b,c]); +end; +