Skip to content

Commit 273ca49

Browse files
committedJul 27, 2024·
Extra params for TriangulatePolygon
1 parent a03dd46 commit 273ca49

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed
 

‎Source/script/imports/simba.import_math.pas

+8-3
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,22 @@ procedure _LapeIsConvexPolygon(const Params: PParamArray; const Result: Pointer)
217217
(*
218218
TriangulatePolygon
219219
-----------
220-
> function TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
220+
> function TriangulatePolygon(Polygon: TPointArray; MinArea: Double=0; MaxDepth: Int32=0): TTriangleArray;
221221
222222
Break the polygon into triangles, the smallest possible polygon. The order of the
223223
input does matter, if it fails, try to reverse the Poly with Poly.Reversed()
224224
225225
This is a custom algorithm by slacky, based around the concept of trimming "ears",
226226
if you dont like the output, you may have more luck with rolling the Polygon before calling.
227+
228+
Two default params exists as well, `MinArea` and `MaxDepth`, they work in tandom,
229+
`MinArea` parameter is for setting a minimum size of triangles added to result, and as this method
230+
works iteratively, removing triangles in a circle around the shape over and over, `MaxDepth` refers
231+
to the max number of rounds it has moved around the shape before it ignores `MinArea` paramater.
227232
*)
228233
procedure _LapeTriangulatePolygon(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
229234
begin
230-
PTriangleArray(Result)^ := TSimbaGeometry.TriangulatePolygon(PPointArray(Params^[0])^);
235+
PTriangleArray(Result)^ := TSimbaGeometry.TriangulatePolygon(PPointArray(Params^[0])^, PSingle(Params^[1])^, PInt32(Params^[2])^);
231236
end;
232237

233238
(*
@@ -428,7 +433,7 @@ procedure ImportMath(Compiler: TSimbaScript_Compiler);
428433
addGlobalFunc('function Modulo(const X, Y: Double): Double; overload', @_LapeModuloF);
429434

430435
addGlobalFunc('function IsConvexPolygon(const Polygon: TPointArray): Boolean', @_LapeIsConvexPolygon);
431-
addGlobalFunc('function TriangulatePolygon(const Polygon: TPointArray): TTriangleArray', @_LapeTriangulatePolygon);
436+
addGlobalFunc('function TriangulatePolygon(const Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray', @_LapeTriangulatePolygon);
432437
addGlobalFunc('function LineInPolygon(a1, a2: TPoint; const Polygon: TPointArray): Boolean', @_LapeLineInPolygon);
433438

434439
addGlobalFunc('function DeltaAngle(const DegreesA, DegreesB: Double; R: Double = 360): Double', @_LapeDeltaAngle);

‎Source/simba.geometry.pas

+11-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TSimbaGeometry = class
4848

4949
class function IsConvexPolygon(const Polygon: TPointArray): Boolean;
5050
class function LineInPolygon(a1, a2: TPoint; const Polygon: TPointArray): Boolean;
51-
class function TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
51+
class function TriangulatePolygon(Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray;
5252
class function PolygonArea(const Polygon: TPointArray): Double; static; inline;
5353
class function ExpandPolygon(const Polygon: TPointArray; Amount: Integer): TPointArray; static;
5454
class function CrossProduct(const r, p, q: TPoint): Int64; static; overload; inline;
@@ -394,9 +394,9 @@ class function TSimbaGeometry.LineInPolygon(a1, a2: TPoint; const Polygon: TPoin
394394
Result := True;
395395
end;
396396

397-
class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
397+
class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray;
398398
var
399-
i,rshift: Int32;
399+
i,j,rshift: Int32;
400400
A,B,C: TPoint;
401401
tmp1,tmp2: TPointArray;
402402
valid: Boolean;
@@ -407,6 +407,7 @@ class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangl
407407
rshift := 0;
408408
while Length(tmp1) > 3 do
409409
begin
410+
Inc(j);
410411
valid := False;
411412
i := 0;
412413
rshift := 0;
@@ -418,10 +419,13 @@ class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangl
418419

419420
if (CrossProduct(A,B,C) >= 0) and LineInPolygon(A,C, Polygon) then
420421
begin
421-
SetLength(Result, Length(Result)+1);
422-
Result[High(Result)].A := A;
423-
Result[High(Result)].B := B;
424-
Result[High(Result)].C := C;
422+
if (j >= MaxDepth) or (PolygonArea([A,B,C]) > MinArea) then
423+
begin
424+
SetLength(Result, Length(Result)+1);
425+
Result[High(Result)].A := A;
426+
Result[High(Result)].B := B;
427+
Result[High(Result)].C := C;
428+
end;
425429

426430
tmp2[rshift+i] := A;
427431
tmp2[rshift+i+1] := C;

0 commit comments

Comments
 (0)