Skip to content

Commit

Permalink
Clean up ATPA exclude/extract***
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Feb 25, 2024
1 parent 2606f6f commit 1a7a135
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 36 deletions.
82 changes: 67 additions & 15 deletions Source/array/simba.array_pointarray.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -451,6 +504,5 @@ function T2DPointArrayHelper.Intersection: TPointArray;
Result := Buffer.ToArray(False);;
end;


end.

60 changes: 47 additions & 13 deletions Source/script/imports/simba.import_atpa.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;

(*
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 2 additions & 8 deletions Source/simba.quad.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions Tests/atpa_excludeextract.simba
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1a7a135

Please sign in to comment.