Giter VIP home page Giter VIP logo

delaunator-gdscript's Introduction

Delaunator GDScript

Godot v3.x GitHub release (latest by date) GitHub license

A GDScript port of Delaunator: A fast library for Delaunay triangulation of 2D points.

This is a port of Mapbox's Delaunator.

Note: It seems like GDScript is not that fast (in reference of the slogan above, taken from the original library). See the performance benchmarks.

Delaunay triangulation Delaunay triangulation

Voronoi cells Voronoi cells

๐Ÿš€ Usage

const Delaunator = preload("res://Delaunator.gd")

var points = PoolVector2Array([
  Vector2(0, 0), Vector2(1024, 0), Vector2(1024, 600), Vector2(0, 600), Vector2(29, 390), Vector2(859, 300), Vector2(65, 342), Vector2(86, 333), Vector2(962, 212), Vector2(211, 351), Vector2(3, 594), Vector2(421, 278), Vector2(608, 271), Vector2(230, 538), Vector2(870, 454), Vector2(850, 351), Vector2(583, 385), Vector2(907, 480), Vector2(749, 533), Vector2(877, 232), Vector2(720, 546), Vector2(1003, 541), Vector2(696, 594), Vector2(102, 306)
])

var delaunay = Delaunator.new(points)

print(delaunay.triangles)
# >> [11, 16, 12, 11, 13, 16, 22, 20, 16, 16, 15, 12, 20, 18, 16, 13, 22, 16, 20, 22, 18, 18, 15, 16, 15, 5, 12, 9, 13, 11, 8, 19, 5, 5, 19, 12, 12, 0, 11, 18, 14, 15, 15, 8, 5, 17, 14, 18, 0, 23, 11, 11, 23, 9, 9, 4, 13, 14, 17, 15, 21, 17, 2, 23, 7, 9, 23, 6, 7, 7, 4, 9, 17, 8, 15, 19, 1, 12, 6, 4, 7, 2, 17, 18, 17, 21, 8, 22, 2, 18, 21, 1, 8, 4, 10, 13, 13, 3, 22, 22, 3, 2, 4, 3, 10, 10, 3, 13, 2, 1, 21, 8, 1, 19, 1, 0, 12, 23, 0, 6, 6, 0, 4, 4, 0, 3]

print(delaunay.halfedges)
# >> [5, 11, 38, 28, 17, 0, 18, 14, 16, 22, 26, 1, 20, 23, 7, 98, 8, 4, 6, 89, 12, 41, 9, 13, 44, 35, 10, 56, 3, 53, 113, 33, 43, 31, 77, 25, 115, 50, 2, 46, 59, 21, 73, 32, 24, 57, 39, 82, 117, 51, 37, 49, 65, 29, 70, 95, 27, 45, 74, 40, 84, 81, 110, 68, 71, 52, 119, 80, 63, 79, 54, 64, 86, 42, 58, 112, 116, 34, 122, 69, 67, 61, 47, 88, 60, 92, 72, 101, 83, 19, 109, 111, 85, 104, 107, 55, 106, 99, 15, 97, -1, 87, 125, 105, 93, 103, 96, 94, -1, 90, 62, 91, 75, 30, -1, 36, 76, 48, 120, 66, 118, 123, 78, 121, -1, 102]

print(delaunay.hull)
# >> [1, 0, 3, 2]

print(delaunay.coords)
# >> [0, 0, 1024, 0, 1024, 600, 0, 600, 29, 390, 859, 300, 65, 342, 86, 333, 962, 212, 211, 351, 3, 594, 421, 278, 608, 271, 230, 538, 870, 454, 850, 351, 583, 385, 907, 480, 749, 533, 877, 232, 720, 546, 1003, 541, 696, 594, 102, 306]

๐Ÿ“‘ API Reference

Delaunator.new(points)

Constructs a Delaunay triangulation object given an array of points (Vector2(x, y)). Duplicate points are skipped.

Delaunator.new(points).triangles

An array of triangle vertex indices (each group of three numbers forms a triangle). All triangles are directed counterclockwise.

To get the coordinates of all triangles, use:

var coordinates = []

for i in range(0, triangles.size(), 3):
  coordinates.append([
    points[triangles[i]],
    points[triangles[i + 1]],
    points[triangles[i + 2]]
  ])

Delaunator.new(points).halfedges

An array of triangle half-edge indices that allows you to traverse the triangulation. i-th half-edge in the array corresponds to vertex triangles[i] the half-edge is coming from. halfedges[i] is the index of a twin half-edge in an adjacent triangle (or -1 for outer half-edges on the convex hull).

The flat array-based data structures might be counterintuitive, but they're one of the key reasons this library is fast.

Delaunator.new(points).hull

An array of indices that reference points on the convex hull of the input data, counter-clockwise.

Delaunator.new(points).coords

An array of input coordinates in the form [x0, y0, x1, y1, ...], of the type provided in the constructor.

Delaunator.new(points).update()

Updates the triangulation if you modified Delaunator.new(points).coords values in place, avoiding expensive memory allocations. Useful for iterative relaxation algorithms such as Lloyd's.

๐Ÿ“ˆ Performance

Benchmark results performed on a Macbook Pro Retina 15" 2015 with Godot 3.2 and 3.3 using this method:

var start = OS.get_ticks_msec()
var delaunay = Delaunator.new(points)
var elapsed = OS.get_ticks_msec() - start
print(elapsed)
10 points 100 points 1.000 points 10.000 points 100.000 points
Godot 3.2 ~1ms ~6ms ~67ms ~760ms ~9.4s
Godot 3.3 ~1ms ~8ms ~77ms ~850ms ~10.0s

๐Ÿ—’๏ธ Changelog

See CHANGELOG.

๐Ÿ‘ค Author

hiulit

๐Ÿค Contributing

Feel free to:

๐Ÿ™Œ Supporting this project

If you love this project or find it helpful, please consider supporting it through any size donations to help make it better โค๏ธ.

Become a patron

Suppor me on Ko-Fi

Buy me a coffee

Donate Paypal

If you can't, consider sharing it with the world...

... or giving it a star โญ๏ธ.

๐Ÿ‘ Credits

Thanks to:

๐Ÿ“ Licenses

delaunator-gdscript's People

Contributors

hiulit avatar itsalxl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

delaunator-gdscript's Issues

Suggestion: Use static typing

Great script - thanks!

I noticed that you only mentioned the variable types as comment instead of using static typing:

var coords = [] # PoolRealArray.
var halfedges = [] # PoolIntArray.

I wonder whether you could increase the script performance by switching to static typing?

var coords : PoolRealArray = []
...

Even if the performance would not be affected, it would increase readability and has some other advantages, too.

The "Start a new discussion" link is dead

I was having trouble getting the code to run in the Godot 4 alpha, and since it's not reasonable to start a new issue for that, I was hoping to see what else I could do! The link currently takes me to a 404 page

For godot4 users: slice end is now exclusive

Godot4 array api has changed a lot, and the meaning of end param in slice method has changed, which is the inclusive index previously and now is the exclusive index, same as the original javascript lib.

In this case, line 295-296 should be

    triangles = _triangles.slice(0, triangles_len)
    halfedges = _halfedges.slice(0, triangles_len)

otherwise you will got a non-3-multiply length array .triangles, which is invild.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.