Documentation
¶
Overview ¶
Package earstrip triangulates polygons with holes into triangle strips.
It is a Go port of earstrip (https://github.com/GLMap/earstrip) by Evgen Bodunov: ear clipping that cuts ears along zig-zag strips, about 1.7 indices per triangle instead of 3, with a constrained Delaunay triangulation for polygons whose rings cross or touch. The ideas are told step by step in https://globus.software/blog/2026/10/16/cutting-map-polygons/.
A Triangulator is meant to be reused, so that once it has seen the largest polygon it allocates nothing more. It is not safe for concurrent use: use one per goroutine (or a sync.Pool). The slices it returns (Strips, StripEnds, StripClockwise, TriangleList, ExtraPoints) alias its buffers and stay valid until the next run or Shrink.
Coordinates must be finite: NaN or infinite coordinates give arbitrary triangles, possibly with Status Exact.
Index ¶
- func Earcut(data []float64, holeIndices []int, dim int) []uint32
- func Triangulate(polygon [][][2]float64) []uint32
- type FlatRings
- type Index
- type Polygon
- type Rings
- type Status
- type Triangulator
- func (t *Triangulator[I]) ExtraPoints() []float64
- func (t *Triangulator[I]) HolesLeftOut() bool
- func (t *Triangulator[I]) Restart(dst []I, restart I) []I
- func (t *Triangulator[I]) Run(polygon [][][2]float64) Status
- func (t *Triangulator[I]) RunFlat(coords []float64, dim int, holeStarts []int) Status
- func (t *Triangulator[I]) RunRings(rings Rings) Status
- func (t *Triangulator[I]) SetConstrained(on bool)
- func (t *Triangulator[I]) SetExtraPoints(on bool)
- func (t *Triangulator[I]) SetForcedCuts(on bool)
- func (t *Triangulator[I]) SetTriangleList(on bool)
- func (t *Triangulator[I]) Shrink()
- func (t *Triangulator[I]) StripClockwise() []uint8
- func (t *Triangulator[I]) StripEnds() []uint32
- func (t *Triangulator[I]) Strips() []I
- func (t *Triangulator[I]) TriangleList() []I
- func (t *Triangulator[I]) Triangles(dst []I) []I
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Earcut ¶
Earcut takes earcut's arguments, flat coordinates, the vertex index where each hole starts and the numbers per vertex (0 means 2), and returns the same kind of result: three indices per counter-clockwise triangle, every index an input vertex.
Example ¶
Earcut takes earcut's arguments: flat coordinates, the vertex index where each hole starts, numbers per vertex.
package main
import (
"fmt"
"github.com/cwbudde/go-earstrip"
)
func main() {
coords := []float64{0, 0, 10, 0, 10, 10, 0, 10, 3, 3, 7, 3, 7, 7, 3, 7}
fmt.Println(earstrip.Earcut(coords, []int{4}, 2))
}
Output: [1 5 0 0 5 4 0 4 3 3 4 7 3 7 2 2 7 6 2 6 1 1 6 5]
func Triangulate ¶
Triangulate triangulates a polygon (rings of points, outer ring first) into a triangle list, three indices per counter-clockwise triangle.
Example ¶
package main
import (
"fmt"
"github.com/cwbudde/go-earstrip"
)
func main() {
l := [][][2]float64{{{0, 0}, {10, 0}, {10, 4}, {4, 4}, {4, 10}, {0, 10}}}
fmt.Println(earstrip.Triangulate(l))
}
Output: [1 2 0 0 2 3 0 3 5 5 3 4]
Types ¶
type FlatRings ¶
FlatRings is a polygon as flat coordinates, Dim numbers per vertex (x and y first; a Dim below 2 means 2), and the vertex index where each hole starts, as earcut takes it. HoleStarts must not decrease and must lie within [0, len(Coords)/Dim]; trailing coordinates of an incomplete vertex are ignored.
type Index ¶
Index is the type of the vertex indices in the strips; a polygon may have at most as many vertices as it can count.
type Polygon ¶
type Polygon [][][2]float64
Polygon is a polygon as a slice of rings, each a slice of points; ring 0 is the outer ring.
type Rings ¶
type Rings interface {
Count() int // number of rings
Size(r int) int // number of points of ring r
X(r, i int) float64 // x of point i of ring r
Y(r, i int) float64 // y of point i of ring r
}
Rings is a polygon as rings of points: ring 0 is the outer ring, the others are holes. Either orientation is fine, a repeated closing point too. Indices in the result count the vertices of all rings in order.
type Status ¶
type Status uint8
Status tells how a polygon was covered.
const ( // Exact means the triangles cover the polygon exactly. Exact Status = iota // HolesLeftOut means a hole could not be joined to the boundary (it lies outside or crosses it) and is left out. HolesLeftOut // Approximate means the boundary ran out of ears (the polygon is not simple); the rest was cut by forced cuts. Approximate // Refused means as Approximate, with forced cuts switched off: the strips cover only what the ears did. Refused // Constrained means covered by the constrained triangulation: the rings cross, a hole could not be joined or there // are more than 128 rings; what lies inside ring 0 by non-zero winding and outside every hole. Constrained )
type Triangulator ¶
type Triangulator[I Index] struct { // contains filtered or unexported fields }
Triangulator triangulates polygons into triangle strips. An instance is meant to be reused: its buffers only grow. The zero value is ready to use, with forced cuts, the constrained triangulation and extra points on.
func (*Triangulator[I]) ExtraPoints ¶
func (t *Triangulator[I]) ExtraPoints() []float64
ExtraPoints returns the points added by the last run as x, y pairs: index n + k in the strips is point k, n being the number of input vertices of all rings.
Example ¶
Rings that cross go to the constrained triangulation, which adds the crossing as a point: index 4 here.
package main
import (
"fmt"
"github.com/cwbudde/go-earstrip"
)
func main() {
var t earstrip.Triangulator[uint32]
status := t.Run([][][2]float64{{{0, 0}, {10, 10}, {10, 0}, {0, 10}}}) // a bow tie
fmt.Println(status, "added:", t.ExtraPoints())
fmt.Println("strips:", t.Strips(), "ends:", t.StripEnds())
}
Output: Constrained added: [5 5] strips: [1 4 2 3 0 4] ends: [3 6]
func (*Triangulator[I]) HolesLeftOut ¶
func (t *Triangulator[I]) HolesLeftOut() bool
HolesLeftOut reports whether the last run left out a hole that could not be joined to the boundary.
func (*Triangulator[I]) Restart ¶
func (t *Triangulator[I]) Restart(dst []I, restart I) []I
Restart appends all strips to dst as one index buffer for a triangle strip draw call, restart between strips, as WebGL 2 and WebGPU take it; no restart index comes before the first strip, and dst keeps what it holds. Every triangle is counter-clockwise: a strip that would start clockwise begins with its first index twice.
func (*Triangulator[I]) Run ¶
func (t *Triangulator[I]) Run(polygon [][][2]float64) Status
Run triangulates a polygon given as rings of points, ring 0 the outer ring. It panics when the polygon has more vertices than I can count.
Example ¶
A square with a square hole becomes one strip of ten indices: eight triangles.
package main
import (
"fmt"
"github.com/cwbudde/go-earstrip"
)
func main() {
var t earstrip.Triangulator[uint32]
status := t.Run([][][2]float64{
{{0, 0}, {10, 0}, {10, 10}, {0, 10}}, // outer ring
{{3, 3}, {7, 3}, {7, 7}, {3, 7}}, // hole
})
fmt.Println(status)
fmt.Printf("strips: %v ends: %v clockwise: %v\n", t.Strips(), t.StripEnds(), t.StripClockwise())
fmt.Println("triangles:", t.Triangles(nil))
}
Output: Exact strips: [1 5 0 4 3 7 2 6 1 5] ends: [10] clockwise: [0] triangles: [1 5 0 0 5 4 0 4 3 3 4 7 3 7 2 2 7 6 2 6 1 1 6 5]
func (*Triangulator[I]) RunFlat ¶
func (t *Triangulator[I]) RunFlat(coords []float64, dim int, holeStarts []int) Status
RunFlat triangulates a polygon given as flat coordinates, dim numbers per vertex (x and y first), and the vertex index where each hole starts (see FlatRings). It panics when the hole starts are out of order or out of range, or when the polygon has more vertices than I can count.
func (*Triangulator[I]) RunRings ¶
func (t *Triangulator[I]) RunRings(rings Rings) Status
RunRings triangulates a polygon given through the Rings interface, which must answer consistently (non-negative sizes, every point in range). It panics when the polygon has more vertices than I can count.
func (*Triangulator[I]) SetConstrained ¶
func (t *Triangulator[I]) SetConstrained(on bool)
SetConstrained sets whether polygons the ears cannot finish, those with a hole the ears leave out (when that covers the rings' area better) and those with more than 128 rings go to the constrained triangulation (default on): Status Constrained.
func (*Triangulator[I]) SetExtraPoints ¶
func (t *Triangulator[I]) SetExtraPoints(on bool)
SetExtraPoints lets the constrained triangulation add points where rings cross (default on); off, such polygons take the ears' way instead, so every index is an input vertex.
func (*Triangulator[I]) SetForcedCuts ¶
func (t *Triangulator[I]) SetForcedCuts(on bool)
SetForcedCuts sets whether, when the boundary runs out of ears, the corners that do the least damage are cut instead of giving up (default on). Only used when the constrained triangulation is off or fails.
func (*Triangulator[I]) SetTriangleList ¶
func (t *Triangulator[I]) SetTriangleList(on bool)
SetTriangleList makes Run write a triangle list instead of strips: Run then fills TriangleList (three indices per triangle, each counter-clockwise, the same as Triangles) and leaves Strips empty.
func (*Triangulator[I]) Shrink ¶
func (t *Triangulator[I]) Shrink()
Shrink frees the buffers, which otherwise keep the size of the largest polygon seen, and the last result; the settings stay. The next run allocates again.
func (*Triangulator[I]) StripClockwise ¶
func (t *Triangulator[I]) StripClockwise() []uint8
StripClockwise returns, per strip, 1 when its first triangle runs clockwise.
func (*Triangulator[I]) StripEnds ¶
func (t *Triangulator[I]) StripEnds() []uint32
StripEnds returns the end of each strip in Strips.
func (*Triangulator[I]) Strips ¶
func (t *Triangulator[I]) Strips() []I
Strips returns the vertex indices of all strips, one after another. Within a strip the triangles alternate as a GPU expects; StripClockwise tells, per strip, whether its first triangle runs clockwise.
func (*Triangulator[I]) TriangleList ¶
func (t *Triangulator[I]) TriangleList() []I
TriangleList returns the triangle list of the last run in triangle list mode.
func (*Triangulator[I]) Triangles ¶
func (t *Triangulator[I]) Triangles(dst []I) []I
Triangles appends the same triangles as a list to dst, keeping what dst holds, three indices each, every one counter-clockwise (positive area with y up).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
demo
command
Command demo is the browser demo of go-earstrip: built for js/wasm by "just build-demo" and loaded by web/src/wasm/earstrip.ts, it publishes globalThis.earstrip, whose functions the web app calls.
|
Command demo is the browser demo of go-earstrip: built for js/wasm by "just build-demo" and loaded by web/src/wasm/earstrip.ts, it publishes globalThis.earstrip, whose functions the web app calls. |
|
earstrip-bench
command
Command earstrip-bench times go-earstrip over a PLY1 file of polygons, as earstrip's bench/bench.cpp does.
|
Command earstrip-bench times go-earstrip over a PLY1 file of polygons, as earstrip's bench/bench.cpp does. |
|
naturalearth
command
Command naturalearth converts the admin-0 countries of Natural Earth into the world map of the demo's Showcase tab: web/public/world-110m.bin and world-50m.bin.
|
Command naturalearth converts the admin-0 countries of Natural Earth into the world map of the demo's Showcase tab: web/public/world-110m.bin and world-50m.bin. |
|
internal
|
|
|
cases
Package cases holds the polygons of earstrip's test/test.cpp (the named cases of main(), its fixtures and its random generators) and further generators for the differential tests, shared by the package tests and the oracle tests.
|
Package cases holds the polygons of earstrip's test/test.cpp (the named cases of main(), its fixtures and its random generators) and further generators for the differential tests, shared by the package tests and the oracle tests. |
|
demo
Package demo holds what the browser demo (cmd/demo) computes: the preset polygons, the generated ones and a triangulation run with its result and timing.
|
Package demo holds what the browser demo (cmd/demo) computes: the preset polygons, the generated ones and a triangulation run with its result and timing. |
|
oracle
Package oracle runs the C++ reference implementation (third_party/earstrip/earstrip.hpp) through cgo, for the differential tests of go-earstrip.
|
Package oracle runs the C++ reference implementation (third_party/earstrip/earstrip.hpp) through cgo, for the differential tests of go-earstrip. |
|
plydata
Package plydata reads the PLY1 polygon files of earstrip's benchmark data (bench-data-1: z13.bin, z9.bin, world.bin, gom4old.bin) and computes the result hash that bench/bench.cpp writes into them.
|
Package plydata reads the PLY1 polygon files of earstrip's benchmark data (bench-data-1: z13.bin, z9.bin, world.bin, gom4old.bin) and computes the result hash that bench/bench.cpp writes into them. |
|
verify
Package verify holds the invariant checks of the tests, shared by the package tests, the fuzz targets and the oracle tests (which run them on the C++ results to show that the checks themselves are right).
|
Package verify holds the invariant checks of the tests, shared by the package tests, the fuzz targets and the oracle tests (which run them on the C++ results to show that the checks themselves are right). |