C# Class Project290.Physics.Common.Decomposition.EarclipDecomposer

Ported from jBox2D. Original author: ewjordan Triangulates a polygon using simple ear-clipping algorithm. Only works on simple polygons. Triangles may be degenerate, especially if you have identical points in the input to the algorithm. Check this before you use them.
Datei anzeigen Open project: scastle/Solitude

Public Methods

Method Description
ConvexPartition ( Vertices vertices ) : List

Decomposes a non-convex polygon into a number of convex polygons, up to maxPolys (remaining pieces are thrown out). Each resulting polygon will have no more than Settings.MaxPolygonVertices vertices. Warning: Only works on simple polygons

ConvexPartition ( Vertices vertices, int maxPolys, float tolerance ) : List

Decomposes a non-convex polygon into a number of convex polygons, up to maxPolys (remaining pieces are thrown out). Each resulting polygon will have no more than Settings.MaxPolygonVertices vertices. Warning: Only works on simple polygons

PolygonizeTriangles ( List triangulated, int maxPolys, float tolerance ) : List

Turns a list of triangles into a list of convex polygons. Very simple method - start with a seed triangle, keep adding triangles to it until you can't add any more without making the polygon non-convex. Returns an integer telling how many polygons were created. Will fill polys array up to polysLength entries, which may be smaller or larger than the return value. Takes O(N///P) where P is the number of resultant polygons, N is triangle count. The final polygon list will not necessarily be minimal, though in practice it works fairly well.

TriangulatePolygon ( Vertices vertices ) : List

Triangulates a polygon using simple ear-clipping algorithm. Returns size of Triangle array unless the polygon can't be triangulated. This should only happen if the polygon self-intersects, though it will not _always_ return null for a bad polygon - it is the caller's responsibility to check for self-intersection, and if it doesn't, it should at least check that the return value is non-null before using. You're warned! Triangles may be degenerate, especially if you have identical points in the input to the algorithm. Check this before you use them. This is totally unoptimized, so for large polygons it should not be part of the simulation loop. Warning: Only works on simple polygons.

Private Methods

Method Description
AddTriangle ( Triangle t, Vertices vertices ) : Vertices
IsEar ( int i, float xv, float yv, int xvLength ) : bool

Checks if vertex i is the tip of an ear in polygon defined by xv[] and yv[]. Assumes clockwise orientation of polygon...ick

Remainder ( int x, int modulus ) : int

Fix for obnoxious behavior for the % operator for negative numbers...

ResolvePinchPoint ( Vertices pin, Vertices &poutA, Vertices &poutB ) : bool

Finds and fixes "pinch points," points where two polygon vertices are at the same point. If a pinch point is found, pin is broken up into poutA and poutB and true is returned; otherwise, returns false. Mostly for internal use. O(N^2) time, which sucks...

Method Details

ConvexPartition() public static method

Decomposes a non-convex polygon into a number of convex polygons, up to maxPolys (remaining pieces are thrown out). Each resulting polygon will have no more than Settings.MaxPolygonVertices vertices. Warning: Only works on simple polygons
public static ConvexPartition ( Vertices vertices ) : List
vertices Vertices The vertices.
return List

ConvexPartition() public static method

Decomposes a non-convex polygon into a number of convex polygons, up to maxPolys (remaining pieces are thrown out). Each resulting polygon will have no more than Settings.MaxPolygonVertices vertices. Warning: Only works on simple polygons
public static ConvexPartition ( Vertices vertices, int maxPolys, float tolerance ) : List
vertices Vertices The vertices.
maxPolys int The maximum number of polygons.
tolerance float The tolerance.
return List

PolygonizeTriangles() public static method

Turns a list of triangles into a list of convex polygons. Very simple method - start with a seed triangle, keep adding triangles to it until you can't add any more without making the polygon non-convex. Returns an integer telling how many polygons were created. Will fill polys array up to polysLength entries, which may be smaller or larger than the return value. Takes O(N///P) where P is the number of resultant polygons, N is triangle count. The final polygon list will not necessarily be minimal, though in practice it works fairly well.
public static PolygonizeTriangles ( List triangulated, int maxPolys, float tolerance ) : List
triangulated List The triangulated.
maxPolys int The maximun number of polygons
tolerance float The tolerance
return List

TriangulatePolygon() public static method

Triangulates a polygon using simple ear-clipping algorithm. Returns size of Triangle array unless the polygon can't be triangulated. This should only happen if the polygon self-intersects, though it will not _always_ return null for a bad polygon - it is the caller's responsibility to check for self-intersection, and if it doesn't, it should at least check that the return value is non-null before using. You're warned! Triangles may be degenerate, especially if you have identical points in the input to the algorithm. Check this before you use them. This is totally unoptimized, so for large polygons it should not be part of the simulation loop. Warning: Only works on simple polygons.
public static TriangulatePolygon ( Vertices vertices ) : List
vertices Vertices
return List