Code Monkey home page Code Monkey logo

mygal's Introduction

MyGAL

Build Status codecov

MyGAL is a computational geometry algorithms library.

Features

For the moment, the library is essentially based on my implementation of Fortune's algorithm. It includes:

MyGAL is:

  • easy to use
  • easy to install (header-only and no external dependency)
  • fast
  • well documented
  • multi-precision

Getting started

To get started, you just have to add the include folder of MyGAL to the include directories of your project. Then, you just have to add this header to start using MyGAL:

#include <MyGAL/FortuneAlgorithm.h>

To use the Fortune's algorithm to generate a Voronoi diagram, do the following:

auto points = std::vector<Vector2<double>>{{0.354, 0.678}, {0.632, 0.189}, {0.842, 0.942}}; // Generate some points
auto algorithm = FortuneAlgorithm<double>(points); // Initialize an instance of Fortune's algorithm
algorithm.construct(); // Construct the diagram

The template parameter corresponds to the floating point type used in computations. double is the recommended one.

The output diagram is unbounded but most of the time you will want a bounded one. To do that, we will compute the intersection between the diagram and a box. In MyGAL, we do that in two steps:

algorithm.bound(Box<double>{-0.05, -0.05, 1.05, 1.05}); // Bound the diagram
auto diagram = algorithm.getDiagram(); // Get the constructed diagram
diagram.intersect(Box<double>{0.0, 0.0, 1.0, 1.0}); // Compute the intersection between the diagram and a box

Firstly, we bound the diagram then we compute the intersection. The two steps are due to technical details, you can read this article if you want to know more. It is recommended to use a box slightly bigger for the bounded step than the one for the intersection step. Otherwise you might face numerical issues.

You can also obtain a Delaunay triangulation from the diagram:

auto triangulation = diagram.computeTriangulation();

Or you can apply Lloyd's algorithm:

auto relaxedPoints = diagram.computeLloydRelaxation()

Example

If you want to build the example, you can use the cmake file present in the example folder. You will need SFML.

The controls of the example are:

  • N: to generate new random points
  • R: to apply the Lloyd's algorithm
  • T: to show the Delaunay triangulation

Known issues

  • If several points are aligned horizontally (exactly the same y-coordinate), the diagram may be incorrect.
  • At least two points are expected.
  • The algorithms are tuned to work with coordinates between 0 and 1. You may want to scale your data to obtain better results.

Documentation

The documentation is available online here.

If you want to build the documentation, you have to have Doxygen installed. Then you just have to execute the doxygen command in the doc folder.

To know more about the implementation you can read some articles on my blog.

License

Distributed under the GNU Lesser GENERAL PUBLIC LICENSE version 3

Images

Voronoi diagram:

Voronoi diagram

Delaunay triangulation:

Delaunay triangulation

Lloyd's relaxation:

Lloyd's relaxation

mygal's People

Contributors

pvigier 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

Watchers

 avatar  avatar  avatar

mygal's Issues

Problem to build diagram on dense point set

Hello Pierre,

I continue to experiment with your algorithm.
Now I'm trying to run it on some real data.

I have attached file with points.
When I read it, I have got segmentation fault during intersection stage.
Seems it is caused by duplicating points.
The matter is that halfEdge in do-while loop in intersect(Box box) method becomes a null pointer.
If I change condition to "while (halfEdge && halfEdge != site.face->outerComponent);" it goes to infinite loop until it hangs entire system.

If duplicates are removed, it works, but produces redundant (to my view) link crossing several zones in the middle:

artifact

Best Regards!
case.txt

Is "free()" right here?

With reference to this line (also something similar at line 64 shortly after):

free(mRoot);

I wonder whether the free is out of place. From what my rusty memory tells me, it is supposed to be used only once and only on memory that has been previously allocated "the C way" (i.e. not with new but malloc/calloc). Looking at the initialization (root pointing the same as nil) it seems just something waiting to explode.

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.