This library consists of two folders. The idea is that one is for testing purposes and the other folder is the folder you drag into your project.
Make sure all input coordinates are normalized to range 0-1 to avoid floating point precision issues! There's a "Normalizer" object that will help you normalize and un-normalize. This is not always needed but if you notice that an algorithm doesn't work, try to normalize the input coordinates.
Some of these algorithms are available in tutorial form here: https://www.habrador.com/tutorials/math/ and here: https://www.habrador.com/tutorials/interpolation/
The code has been tested by using Unity 2018.4 LTS but should work with other versions.
Point-triangle
Point-polygon. Suffers from floating point precision issues
Triangle-triangle
AABB-AABB
Line-line
Ray-plane
Line-plane
Plane-plane
Point-circle
Grid mesh
Mesh shapes: Arrow, circles, lines
A common problem in Computational Geometry is to find the convex hull of a set of points.
Jarvis March. Is also known as "Gift wrapping"
This is the simplest algorithm. The idea is:
This algorithm may be slow, but it is robust and can easily deal with colinear points. Sometimes it's better to use an algorithm which is easy to understand than a more complicated one.
A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=Z_wcJUgvohc
Quickhull.
A good paper on this algorithm is "Implementing Quickhull" from Valve by Dirk Gregorious. It has images so you can see what's going on. But the idea is:
Iterative algorithm. Is very similar to Quickhull.
A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=Yv2OhCV1BpU
You have points on a convex hull you want to triangulate. You have four options here if you have colinear points (points on the same line):
Ear Clipping
Can currently only handle holes - not hole-in-holes. But it has optimizations to get a better looking triangulation. The Ear Clipping algorithm is borrowing ideas from Delaunay triangulation to get nicer looking triangles. So if you encounter problems with this algorithm (because of some edge-case) you can always try the Constrained Delaunay algorithm - they should give the same result. I believe Constrained Delaunay is more robust because you don't have to connect the holes with invisible seams.
A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=mw8aLh_lPoo
Triangulate points with "visible edge" algorithm.
You have some points you want to triangulate, you follow the steps:
A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=MkMXKu1m6A4
Triangulate points with "point-by-point" algorithm.
You have some points you want to triangulate, you follow the steps:
"Incremental" method
You generate a big triangle around all points you want to triangulate. Then you add each point one after the other. The triangle the point ends up in is split into three new triangles. After the split you restore the Delaunay triangulation by flipping edges. When all points have been added you remove the remainings of the first big triangle. This method is also known as the Bowyer–Watson algorithm. A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=YNQR5tH-s40
"Flip edges" method
You triangulate the points by using a "bad" triangulation method (which is in this case either "visible edge" or "point-by-point" from above). Then you go through all edges and check if the edge should be flipped to make a better triangle. When no more edges can be flipped you are done! A visualization of this algorithm can be found: https://www.youtube.com/watch?v=-d7Nb4fxL5s and https://www.youtube.com/watch?v=lR_SzgEkDwk
Constrained triangulation
You add the constraints to the points and generate a Delaunay triangulation by using one of the above methods. Use this triangulation to find which edges interesect with the constraints. Then you flip these edges until they no longer interesect with the constraint. You finally remove the triangles that are "inside" of the constraint. It can handle several holes and a single hull, but not holes-in-holes. If you really need a hole-in-hole you can always run the algorithm twice and then merge the output. A similar algorithm is triangulation by Ear Clipping, but I believe this one is more robust. A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=Z-1ExrWMTfA
Marching squares:
Triangulates points in a 2D grid.
Metacircles:
Metacircles are like Metaballs but in 2D. Is using Marching squares.
From a Delaunay triangulation
You first generate a Delaunay triangulation by using some method. Then you use the fact that you can get the Voronoi diagram from the Delaunay triangulation. For each Delaunay triangle you generate a circle where the triangle-corners are on the edge of the circle. The center of this circle is a corner in the Voronoi diagram face belonging to the triangle.
From a Delaunay triangulation on a sphere
To get the Delaunay triangulation of points on a sphere, you just generate the convex hull of those points. To generate the Voronoi diagram in 3d space, the process is the same as in 2d space - except that you need to find the center of a circle given 3 points on the edge of the circle in 3d space.
A visualization of this algorithm can be found here: https://www.youtube.com/watch?v=xAVL4qz_2AE
Greiner-Hormann method
Sutherland-Hodgman method
Catmull-Rom splines
Bezier curves
Operations on curves
Methods so you can split up the curves into equal steps
Methods to extrude meshes along the curves. The difficult part here is to find an orientation at a point on the curve, and the following methods are included:
The fun part of computational geometry!
Cut mesh with plane (todo)
If the new meshes are not connected, then it will separate the meshes, so you might end up with more than two meshes after the cut.
Simplify mesh
Will generate a mesh similar to the original mesh but with fewer triangles. Is useful for generating LODs, etc. The following algorithms are implemented:
Is a triangle oriented clockwise?
Is a point left, on, or right of vector?
Is a point left, on, or right of a plane? Which is the same as the distance to the plane.
Is a quadrilateral convex?
Is a point between two other points on the same line?
Closest point on a line-segment?
Has passed point?
If we are going from A to B, how do we know if we have passed B? Measuring just the distance to B, and say we have passed B if we are closer than x meter to B is not accurate enough!
2020-12
2020-11
2020-03