使用 C# 查找多边形的中轴
我的任务是弄清楚如何找到多边形的中心线。 我的谷歌搜索让我相信我需要的是“中轴”。 像这样:
(来源:kiev.ua)
根据我读过的、我需要的可以通过使用分段的 2D Voronoi 图构造算法来生成。
我在 codeplex (FortuneVoronoi) 上找到了 Voronoi 算法的 C# 版本,在将多边形应用到它之后,我最终得到了这个:
替代文本 http://www.carbonatlas.com/geonotes/gaia_voronoi.png
绿色是原始多边形。 橙色是 Voronoi 顶点,黑色线是 Voronoi 边。
我可以在这些顶点中看到我需要的东西的构成,但我不确定过滤掉所有我不需要的东西所需的下一步。
如果您能提供任何帮助,我将不胜感激。
I've been tasked to figure out how to find the centerline of a polygon. My google searches led me to believe that what I need is called the 'Medial Axis'. Like this:
(source: kiev.ua)
According to what I've read, what I need can be produced by using a 2D Voronoi diagram construction algorithm for segments.
I've found a C# version of the Voronoi algorithm on codeplex (FortuneVoronoi) and after applying my polygon to it, I end up with this:
alt text http://www.carbonatlas.com/geonotes/gaia_voronoi.png
The green is the original polygon. The orange are the Voronoi vertices and the black lines are the voronoi edges.
I can see the makings of what I need in those vertices, but I'm unsure of the next step required to filter out all the stuff I don't need.
I'd appreciate any help you can offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
评论中建议了一种简单的解决方案:
http://en.wikipedia.org/wiki/Point_in_polygon)
如果你有大量数据,交叉点的成本可能会相当高。
然后您可以执行类似的方法,例如 问题,以及这个解决方案也可能适合您。 我的做法是:
PS。 请注意,这两种解决方案都给出了中轴的一些近似值,精确计算它的成本要高得多,但作为一个预告......您可以为黑色输入样本点获得如下结果:
One simple solution would be as suggested in the comments:
http://en.wikipedia.org/wiki/Point_in_polygon)
If you have huge data the intersections might be quite costly.
Then you could do a similar approach like in the question, and this solution could work for you, as well. The way I would do it:
PS. Note that both solutions give some approximation of the medial axis, computing it exactly is much more costly but as a teaser... you can get results like this for the black input sample points:
类似的构造是直骨架,可以通过将多边形收缩到自身并追踪来构造当顶点接近中心时。 这可能更容易构建,尽管它与中轴不是完全相同的曲线。
A similar construct is the Straight skeleton, which can be constructed by shrinking the polygon into itself and tracing the vertices as they approach the center. This may be a little easier to construct, though it's not quite the same curve as the medial axis.
哇。 我将在这里冒险并建议算法可能对多边形的内部与外部感到困惑。 当您定义原始多边形的边和顶点时,您必须确保它们的定义方式始终可以使用“右手定则”之类的方法找到“内部”。 只要看看右下角的多边形,看起来多边形的边缘实际上与自身相交。 也许算法将该部分和其他部分视为“由内而外”。 左下角也一样。
这是我的直觉,算法似乎无法确定什么方向在内部,什么方向在外部。
我认为一个简单的方法是过滤掉多边形之外的所有 Voroni“节点”,但是,我认为这不会看起来。 仔细看看你的图,看起来每个节点都有 3 个边将其连接到其他节点。 也许您可以过滤掉任何 3 条边连接到多边形外部节点的节点。 那行得通吗?
Wow. I'm going to go out on a limb here and suggest that maybe the algorithm is confused about the inside vs. the outside of the polygon. When you define the edges and vertices of your original polygon, you have to make sure they're defined in such a way that "inside" is always found using something like the "right hand rule". Just looking at the polygon in the bottom right corner, it looks like the edge of your polygon actually crosses itself. Maybe the algorithm is seeing that section, and others, as "inside out". The same in the bottom left.
That's my gut feeling, that the algorithm doesn't seem to be able to determine what direction is inside and what is outside.
I think a naive approach would be to filter out all Voroni "nodes" that are outside the polygon, however, I don't think that will look. Taking a closer look at your diagram, it looks like each node has 3 edges that connect it to other nodes. Perhaps you can filter out nodes where any of the 3 edges are connected to nodes outside the polygon. Would that work?