如何使用 MIConvexHull 库

How to use MIConvexHull library

我目前正在尝试为我的项目实施凸包计算,因此,我安装了 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

这是我已经走了多远以及到目前为止我所拥有的done/understood: 要创建凸包,您需要一个顶点列表,然后凸包基本上是一个顶点列表和一个面列表。所以这就是我目前尝试实现它的方式。

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

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

对于这段代码,我遇到了很多编译器错误,因为没有顶点和面 class。我现在不知道在这里用什么。

这是ConvexHullclass里面的一部分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);
    }

现在我的问题来了:谁能给我解释一下 TVertex 和 TFace 是什么?之后,如何使用代码?

我觉得它与通用数据类型有关,例如 TVertex 不是真正的类型,但它到底是什么以及我可以使用什么真正的 class/type 来实际实现凸包计算?

问题是,我不知道最后的语法。

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

TVertexTFaceconstructed types 是的,这与泛型有关。当你调用静态方法 ConvexHull 时,你必须提供两个实例,一个实现了接口 IVertex,它与 TVertex 相关联,另一个继承自 ConvexFace<TVertex, TFace> ], 这与 TFace.

相关联

如果您查看该项目的内部 github repository, you would find a folder called Examples. There you would find how the library can be used. For instance, here,您会发现 2D 凸包的示例。

简而言之,它创建了一个顶点数组:

var vertices = new Vertex[NumberOfVertices];

然后使用 Vertex class

的构造函数用新的 Vertex 对象填充它们
new Vertex(size * r.NextDouble(), size * r.NextDouble());

然后得到ConvexHull的点数,就像:

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

我已经用许多凸包算法完成了基准测试,包括 MIConvexHull 和我的。如果您浏览提供的代码,您可能会发现如何使用代码进行 2D 使用:

Fast and improved 2D Convex Hull algorithm and its implementation in O(n log h)