Search code examples
c#genericsnuget-packageconvex-hull

How to use MIConvexHull library


I am currently trying to implement convex hull calculation for my project and therefore, I installed the https://www.nuget.org/packages/MIConvexHull/ NuGet package. (This project is a convex hull algorithm and library for 2D, 3D, and higher dimensions.) Here is the link to the github page: https://github.com/DesignEngrLab/MIConvexHull

This is how far I have come and what I have done/understood so far: For creating the convex hull, you need a list of vertices and then a convex hull is basically a list of vertices and a list of faces. So here is how I currently try to implement it.

List<Vertex> MI_convexHullVertices = new List<Vertex>();

var MI_convexHull = MIConvexHull.ConvexHull.Create<Vertex, Face>(vertices);

For this code, I get a lot of compiler errors though because there is no Vertex and no Face class. And I now don't know what to use here.

This is a part from the ConvexHull class inside ConvexHull.cs:

    public static ConvexHull<TVertex, TFace> Create<TVertex, TFace>(IList<TVertex> data,
        double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance)
        where TVertex : IVertex
        where TFace : ConvexFace<TVertex, TFace>, new()
    {
        return ConvexHull<TVertex, TFace>.Create(data, PlaneDistanceTolerance);
    }

And now here comes my question: Can someone explain to me what TVertex and TFace is? And after that, how to use the code?

I feel like it has something to do with generic data types, like TVertex isn't a real type, but what exactly is it and what real class/type can I use to actually implement convex hull calculation?

The thing is, I don't know the syntax at the end there.


Solution

  • Can someone explain to me what TVertex and TFace is? And after that, how to use the code?

    The TVertex and TFace are constructed types and yes, this has to do with generics. When you call the static method ConvexHull you have to provide two instances, one that implements the interface IVertex, this is associated with the TVertex, and the other that inherits from ConvexFace<TVertex, TFace>, this is associated with the TFace.

    If you looked inside the project's github repository, you would find a folder called Examples. There you would find how the library can be used. For instance, here you would find an illustrative example for the case of 2D Convex Hull.

    In broad lines, it creates an array of vertices:

    var vertices = new Vertex[NumberOfVertices];
    

    and then it fills them with new Vertex objects, using the constructor of Vertex class

    new Vertex(size * r.NextDouble(), size * r.NextDouble());
    

    and then gets the points of the ConvexHull as simply as:

    var convexHull = ConvexHull.Create(vertices).Points;