• Stars
    star
    36
  • Rank 710,482 (Top 15 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

2D triangulation library. Allows translating lines and polygons (both based on points) to the language of GPUs.


triangolatte

2D triangulation library. Allows translating lines and polygons (both based on points) to the language of GPUs.

Features normal and miter joint line triangulation. Handles polygons using ear clipping algorithm with hole elimination included.

screenshot

For reference: triangulates 99.76% of 75 thousand buildings in Cracow under 3.43s on average programmer notebook (single threaded).

Table of contents

Installation

Nothing surprising

go get github.com/tchayen/triangolatte

Usage

Basic example

vertices := []Point{{10, 20}, {30, 40}, {50, 60}}
t, err = triangolatte.Polygon(vertices)

Examples

In /examples you can find:

  • buildings – full-blown WebGL previewer of buildings triangulated in city example
  • city – triangulation of whole city downloaded from Open Street Map
  • gpx – GPX format parsing and triangulation of its data
  • wireframe – desktop OpenGL wireframe previewer for triangulated shapes

You will find instructions for running the code there.

Features

API

Polygon(points []Point, holes [][]Point) ([]float64, error)

Takes array of points and produces array of triangle coordinates.

Based on the following paper and inspired by EarCut.

JoinHoles(points [][]Point) ([]Point, error)

Removes holes, joining them with the rest of the polygon. Provides preprocessing for Polygon. First element of the points array is the outer polygon, the rest of them are considered as holes to be removed.

Line(joint Joint, points []Point, width int) ([]float64, error)

Takes array of points and triangulates them to resemble a line of given width. Returns array of two-coordinate CCW triangles one after another.

Types

To select method of joining line segments.

type Joint int

const (
	// No joint correction.
	Normal Joint = 0
	// Producing miter joints, i.e. extending the lines until they meet at some point.
	Miter Joint = 1
)

For calculations using points.

type Point struct {
  X, Y float64
}

A wrapper for Point used in cyclic list.

type Element struct {
	Prev, Next *Element
	Point      Point
}

Helpers

You can have a look at helpers.go file. It stores triangolatte's helper functions used mostly by tests and examples. They are not exported because I don't want to commit to supporting them in the future, but they might turn out useful for you.

Tests

Code is (more or less) covered in tests. You can run them like this:

go test -v

You can also run benchmarks for selected functions (refer to the *_test.go files for availability). For example:

go test -run NONE -bench IsInsideTriangle

Benchmarks

NOTE: This section contains work in progress. Numbers below are better reference point than nothing, but still far from perfect.

Polygon() on shape with 10 vertices takes 754ns on average.

Triangulation of 75 thousand buildings runs for around 3.43s.

Using average programmer's notebook. Expect speed up on faster CPUs or while splitting execution into separate threads.

Flame Graphs

CPU time % usage snaphost using Flame Graphs:

assets/torch.svg

Want to learn what is it or maybe you are willing to generate one yourself? Check FlameGraphs document in this repository.

Future plans

Optimizations

NOTE: this library is developed mostly with map data triangulation in mind and it will be its main performance target.

  • explore possibilities for optimizations in JoinHoles(...)
  • maybe allow reusing point array for massive allocation reduction

Content

  • provide more examples (e.g. desktop OpenGL usage, mobile app, live rendering pipeline, other unusual use cases...)
  • add benchmarks with comparison to libraries in other languages

WebAssembly

One of the core plans for this library's development is creating, as soon as it becomes possible, some kind of WebAssembly module for use in JS.

Contributing

You are welcome to create an issue or pull request if you've got an idea what to do. It is usually a good idea to visit Gitter and discuss your thoughts.

Don't have one, but still want to contribute? Get in touch with us and we can brainstorm some ideas.

License

MIT License – refer to the LICENSE file.

More Repositories

1

red-otter

Self-contained WebGL flexbox layout engine
TypeScript
244
star
2

markdown-links

Command that displays a graph of local links between markdown files
TypeScript
238
star
3

2d-opengl-rendering

Minimal, fast OpenGL renderer for 2D sprites
C++
51
star
4

tonfisk

TypeScript
37
star
5

baked-room

JavaScript
27
star
6

webgl-maps

WebGL map renderer written from scratch
TypeScript
19
star
7

remini

Mini React implementation made for fun and practice. Please do not use in production.
TypeScript
16
star
8

tangram

Example of using CSS animations triggered in JS.
TypeScript
11
star
9

pbr-webgpu

PBR IBL renderer in WebGPU
TypeScript
8
star
10

topojson-osm-fetch

Utility for extracting OSM map data and converting it to TopoJSON
JavaScript
7
star
11

infinite-scribbles

Simple handwritten notes web editor with infinite canvas. Made with graphics tablets in mind. 🚧 WIP 🚧
TypeScript
6
star
12

mini-md

Tiny Markdown editor with preview and prettier functionality
JavaScript
5
star
13

ttf-spacing

TTF parser for extracting letter spacing information
TypeScript
4
star
14

schedule_planner

Student schedule viewer
Python
4
star
15

lambda-chat

Simple chat using websockets.
Haskell
3
star
16

deck

Deck of cards made using CSS
TypeScript
3
star
17

comf

C++ 14 OpenGL Mini Framework
C++
3
star
18

expo-qr-code-reader

Expo QR code reader
JavaScript
2
star
19

prism-github-theme

CSS
2
star
20

fem-lshape

Jupyter Notebook solving FEM L-Shape problem
Jupyter Notebook
2
star
21

gatsby-react-docs-example

Example of documentation site with Gatsby autogenerated from components
TypeScript
2
star
22

drive-editor

Simple text editor storing all files on Google Drive
TypeScript
2
star
23

zoom-scribbling

TypeScript
2
star
24

bundling-ve

TypeScript
1
star
25

bio-updater

Update Twitter and GitHub go at one go.
JavaScript
1
star
26

backup-likes

TypeScript
1
star
27

agh-polish-legal-docs-parser

OOP class assignment. Parser which turns text file with legal document (following Act of rules for legislative techniques in Poland) into object form.
Java
1
star
28

webgl-triangulation

WebGL renderer for TopoJSON files
JavaScript
1
star
29

chess

TypeScript
1
star