简体   繁体   English

从许多多边形的联合构造多边形

[英]Construct polygons out of union of many polygons

Supposed that I have many polygons, what is the best algorithm to construct a polygon--maybe with holes- out of the union of all those polygons?假设我有很多多边形,从所有这些多边形的联合中构建多边形的最佳算法是什么?

For my purpose, you can imagine each piece of a polygon as a jigsaw puzzle piece, when you complete them you will get a nice picture.为了我的目的,你可以把多边形的每一块想象成一个拼图,当你完成它们时,你会得到一张漂亮的图片。 But the catch is that a small portion ( say <5%) of the jigsaw is missing, and you are still require to form a picture as complete as possible;但问题是拼图的一小部分(比如 <5%)丢失了,你仍然需要尽可能完整地形成一张图片; that's the polygon ( or polygons)-- maybe with holes-- that I want to form.这就是我想要形成的多边形(或多边形)——可能有洞。

My naive approach is to take two polygons, union them, and take another polygon, union it with the union of the two polygons, and repeat this process until every single piece is union.我天真的方法是取两个多边形,将它们合并,然后取另一个多边形,将其与两个多边形的并集合并,然后重复此过程,直到每个部分都合并为止。 Then I will run through the union polygon list and check whether there are still some polygons can be combined, and I will repeat this process until a satisfactory result is achieved.然后我将遍历并集多边形列表并检查是否还有一些多边形可以组合,我将重复此过程,直到获得满意的结果。

But this seems to be like an extremely naive approach.但这似乎是一种极其幼稚的做法。 I just wonder is there any other better algorithm?我只是想知道还有其他更好的算法吗?

You need a polygon clipping library - and I'll plug my own Clipper library since it's written in C# (and C++ and Delphi), it's open source freeware, and it'll do exactly what you want.您需要一个多边形裁剪库 - 我将插入我自己的Clipper库,因为它是用 C#(以及 C++ 和 Delphi)编写的,它是开源的免费软件,并且可以完全满足您的需求。

My naive approach is to take two polygons, union them, and take another polygon, union it with the union of the two polygons, and repeat this process until every single piece is union我天真的方法是取两个多边形,将它们合并,然后取另一个多边形,将其与两个多边形的并集合并,然后重复此过程,直到每个部分都合并

That would be a very inefficient approach.那将是一种非常低效的方法。 A much better way would be to 'union' them all in one operation ...更好的方法是将它们全部“联合”在一次操作中......

using ClipperLib;
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
...

//precondition: all your polygons have the same orientation 
//(ie either clockwise or counter clockwise)
Polygons polys = new Polygons(PolyCnt);
for (int i = 0; i < PolyCnt; i++)
    polys.Add(loadPolyFromFile(String.Format("poly{0}.txt", i +1)));

Polygons solution = new Polygons();
Clipper c = new Clipper();
c.AddPolygons(polys, PolyType.ptSubject);
c.Execute(ClipType.ctUnion, solution, 
    PolyFillType.pftNonZero, PolyFillType.pftNonZero);

//code to display solution here.

That's brute force what's your doing.这是蛮力你在做什么。 A better way of doing brute force is branch and bound.执行蛮力的更好方法是分支定界。 But that still scales horribly.但这仍然非常可怕。

The next step is to try metaheuristic algorithms (tabu search, simulated annealing, ...) or just reuse a framework like Drools Planner (open source, java) which implements them for you.下一步是尝试元启发式算法(禁忌搜索、模拟退火等),或者只是重用像Drools Planner (开源,java)这样的框架来为你实现它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM