Skip to content

Commit

Permalink
New TPA methods: Median, ToAxes, CreateFromAxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Mar 17, 2024
1 parent c1e06f5 commit 6676be8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Source/array/simba.array_point.pas
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ interface
class function CreateFromBox(Box: TBox; Filled: Boolean): TPointArray; static;
class function CreateFromPolygon(Poly: TPointArray; Filled: Boolean): TPointArray; static;
class function CreateFromSimplePolygon(Center: TPoint; Sides: Integer; Size: Integer; Filled: Boolean): TPointArray; static;
class function CreateFromAxes(X, Y: TIntegerArray): TPointArray; static;

function IndexOf(P: TPoint): Integer;
function IndicesOf(P: TPoint): TIntegerArray;
Expand Down Expand Up @@ -153,6 +154,9 @@ interface
function ConcaveHull(Epsilon:Double=2.5; kCount:Int32=5): TPointArray;
function ConcaveHullEx(MaxLeap: Double=-1; Epsilon:Double=2): T2DPointArray;
function ConvexityDefects(Epsilon: Single; Mode: EConvexityDefects = EConvexityDefects.NONE): TPointArray;

procedure ToAxes(out X, Y: TIntegerArray);
function Median: TPoint;
end;

implementation
Expand Down Expand Up @@ -462,6 +466,21 @@ class function TPointArrayHelper.CreateFromSimplePolygon(Center: TPoint; Sides:
Result := Result.ShapeFill();
end;

class function TPointArrayHelper.CreateFromAxes(X, Y: TIntegerArray): TPointArray;
var
I: Integer;
begin
if (Length(X) <> Length(Y)) then
SimbaException('TPointArray.CreateFromAxes: X and Y lengths differ (%d,%d)', [Length(X), Length(Y)]);

SetLength(Result, Length(X));
for I := 0 to High(Result) do
begin
Result[I].X := X[I];
Result[I].Y := Y[I];
end;
end;

function TPointArrayHelper.IndexOf(P: TPoint): Integer;
begin
Result := specialize IndexOf<TPoint>(P, Self);
Expand Down Expand Up @@ -2656,5 +2675,49 @@ function TPointArrayHelper.ConvexityDefects(Epsilon: Single; Mode: EConvexityDef
Result := Buffer.ToArray(False);
end;

procedure TPointArrayHelper.ToAxes(out X, Y: TIntegerArray);
var
I,H:Integer;
begin
H := High(Self);
SetLength(X, H+1);
SetLength(Y, H+1);
for I := 0 to H do
begin
X[I] := Self[I].X;
Y[I] := Self[I].Y;
end;
end;

function TPointArrayHelper.Median: TPoint;
var
X, Y: TIntegerArray;
Mid: Integer;
begin
if (Length(Self) = 0) then
Result := TPoint.ZERO
else
if (Length(Self) = 1) then
Result := Self[0]
else
begin
ToAxes(X, Y);
X.Sort();
Y.Sort();

Mid := Length(Self) div 2;

if (Mid mod 2) = 0 then
begin
Result.X := X[Mid];
Result.Y := Y[Mid];
end else
begin
Result.X := Round((X[Mid] + X[Mid+1]) / 2);
Result.Y := Round((Y[Mid] + Y[Mid+1]) / 2);
end;
end;
end;

end.

24 changes: 24 additions & 0 deletions Source/script/imports/simba.import_tpa.pas
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ procedure _LapeTPACreateFromSimplePolygon(const Params: PParamArray; const Resul
PPointArray(Result)^ := TPointArray.CreateFromSimplePolygon(PPoint(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PBoolean(Params^[3])^);
end;

(*
TPointArray.CreateFromAxes
--------------------------
> function TPointArray.CreateFromAxes(X, Y: TIntegerArray): TPointArray; static;
*)
procedure _LapeTPACreateFromAxes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Result)^ := TPointArray.CreateFromAxes(PIntegerArray(Params^[0])^, PIntegerArray(Params^[1])^);
end;

(*
TPointArray.Rows
----------------
Expand Down Expand Up @@ -810,6 +820,16 @@ procedure _LapeTPAConvexityDefects(const Params: PParamArray; const Result: Poin
PPointArray(Result)^ := PPointArray(Params^[0])^.ConvexityDefects(PDouble(Params^[1])^, EConvexityDefects(Params^[2]^));
end;

procedure _LapeTPAToAxes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Params^[0])^.ToAxes(PIntegerArray(Params^[1])^, PIntegerArray(Params^[2])^);
end;

procedure _LapeTPAMedian(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPoint(Result)^ := PPointArray(Params^[0])^.Median();
end;

procedure ImportTPA(Compiler: TSimbaScript_Compiler);
begin
with Compiler do
Expand All @@ -822,6 +842,7 @@ procedure ImportTPA(Compiler: TSimbaScript_Compiler);
addGlobalFunc('function TPointArray.CreateFromLine(Start, Stop: TPoint): TPointArray; static', @_LapeTPACreateFromLine);
addGlobalFunc('function TPointArray.CreateFromPolygon(Poly: TPointArray; Filled: Boolean): TPointArray; static', @_LapeTPACreateFromPolygon);
addGlobalFunc('function TPointArray.CreateFromSimplePolygon(Center: TPoint; Sides: Integer; Size: Integer; Filled: Boolean): TPointArray; static', @_LapeTPACreateFromSimplePolygon);
addGlobalFunc('function TPointArray.CreateFromAxes(X, Y: TIntegerArray): TPointArray; static', @_LapeTPACreateFromAxes);

addGlobalFunc('function TPointArray.ExcludePie(StartDegree, EndDegree, MinRadius, MaxRadius: Single; Center: TPoint): TPointArray;', @_LapeTPAExcludePie);
addGlobalFunc('function TPointArray.ExcludeDist(Center: TPoint; MinDist, MaxDist: Double): TPointArray', @_LapeTPAExcludeDist);
Expand Down Expand Up @@ -913,6 +934,9 @@ procedure ImportTPA(Compiler: TSimbaScript_Compiler);
addGlobalType('enum(NONE, ALL, MINIMAL)', 'EConvexityDefects');
addGlobalFunc('function TPointArray.ConvexityDefects(Epsilon: Single = 0; Mode: EConvexityDefects = EConvexityDefects.NONE): TPointArray;', @_LapeTPAConvexityDefects);

addGlobalFunc('procedure TPointArray.ToAxes(out X, Y: TIntegerArray);', @_LapeTPAToAxes);
addGlobalFunc('function TPointArray.Median: TPoint; overload', @_LapeTPAMedian);

ImportingSection := '';
end;
end;
Expand Down

0 comments on commit 6676be8

Please sign in to comment.