Skip to content

Commit

Permalink
[runtime] Drop support of triangulation with holes, remove dependency…
Browse files Browse the repository at this point in the history
… to earcut and poly2tri in favor of a port of libgdx's `EarClippingTriangulator.java`
  • Loading branch information
jeremyfa committed Dec 28, 2024
1 parent 164acb9 commit dc54213
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 163 deletions.
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
[submodule "git/generate"]
path = git/generate
url = https://github.com/jeremyfa/generate.git
[submodule "git/earcut"]
path = git/earcut
url = https://github.com/ceramic-engine/earcut.git
[submodule "git/format-tiled"]
path = git/format-tiled
url = https://github.com/ceramic-engine/format-tiled.git
Expand Down Expand Up @@ -56,9 +53,6 @@
[submodule "git/linc_dialogs"]
path = git/linc_dialogs
url = https://github.com/ceramic-engine/linc_dialogs.git
[submodule "git/poly2tri"]
path = git/poly2tri
url = https://github.com/ceramic-engine/poly2tri.git
[submodule "git/clay"]
path = git/clay
url = https://github.com/ceramic-engine/clay.git
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Ceramic was created by **[Jérémy Faivre](https://github.com/jeremyfa)**, as we

* **[Extrude Polyline](https://github.com/mattdesl/extrude-polyline) by Matt DesLauriers**, via Haxe port used in Ceramic to draw lines.

* **[Earcut](https://github.com/mapbox/earcut) from Mapbox**, via Haxe port to triangulate shapes.
* **[LibGDX](https://github.com/libgdx/libgdx)**, used as a reference for polygon triangulation.

* **[Haxe Format Tiled](https://github.com/Yanrishatum/haxe-format-tiled) by Pavel Alexandrov** to parse Tiled Map Editor's TMX format.

Expand Down
1 change: 0 additions & 1 deletion git/earcut
Submodule earcut deleted from 187aa1
1 change: 0 additions & 1 deletion git/poly2tri
Submodule poly2tri deleted from 1e5056
28 changes: 28 additions & 0 deletions runtime/src/ceramic/GeometryUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,32 @@ class GeometryUtils {

}

/**
* Returns whether the polygon vertices are ordered clockwise or counterclockwise.
* @param vertices Array of polygon vertices as [x,y,x,y,...]
* @param offset Starting index in the array
* @param count Number of array indices to use
* @return true if clockwise, false if counterclockwise
*/
public static function isClockwise(vertices:Array<Float>, offset:Int, count:Int):Bool {
if (count <= 2) return false;

var area:Float = 0;
var last:Int = offset + count - 2;
var x1:Float = vertices[last];
var y1:Float = vertices[last + 1];

var i:Int = offset;
while (i <= last) {
var x2:Float = vertices[i];
var y2:Float = vertices[i + 1];
area += x1 * y2 - x2 * y1;
x1 = x2;
y1 = y2;
i += 2;
}

return area < 0;
}

}
30 changes: 1 addition & 29 deletions runtime/src/ceramic/Shape.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,6 @@ class Shape extends Mesh {
return points;
}

public var triangulation(default, set):TriangulateMethod = POLY2TRI;
inline function set_triangulation(triangulation:TriangulateMethod) {
if (this.triangulation != triangulation) {
this.triangulation = triangulation;
contentDirty = true;
}
return triangulation;
}

/**
* An array of hole indices, if any.
* (e.g. `[5, 8]` for a 12-vertex input would mean
* one hole with vertices 5–7 and another with 8–11).
* Note: when editing array content without reassigning it,
* `contentDirty` must be set to `true` to let the shape being updated accordingly.
*/
public var holes:Array<Int> = null;
inline function set_holes(holes:Array<Int>):Array<Int> {
this.holes = holes;
contentDirty = true;
return holes;
}

/**
* If set to `true`, width and heigh will be computed from shape points.
*/
Expand Down Expand Up @@ -85,12 +62,7 @@ class Shape extends Mesh {
if (indices == null)
indices = [];

if (holes != null && holes.length > 0) {
Triangulate.triangulate(vertices, indices, holes, triangulation);
}
else {
Triangulate.triangulate(vertices, indices, triangulation);
}
Triangulate.triangulate(vertices, indices);
}

if (autoComputeSize)
Expand Down
Loading

0 comments on commit dc54213

Please sign in to comment.