sourceforge官网:http://sourceforge.net/projects/polyclipping/
之前工程里面使用4.8.6,最近升级到最新版本6.2.1,接口层面有点差别:
老版本使用Polygon概念,最新版本用Path代替了Polygon,对用的Polygons用Paths代替,Clipper::AddPath的时候还需要制定是否封闭。
一个测试,回字上半部分和下半部分,两半部分进行合并,但是输出结果总是不对:
void transform_array_to_path(int* arr, int size, ClipperLib::Path& path, int scale = 1) { for (int i = 0; i < size; i += 2) { path.push_back(ClipperLib::IntPoint(arr[i] * scale, arr[i + 1] * scale)); } } void ClipperTest::merge_case() { using namespace ClipperLib; Clipper union_worker; Paths solution; Path positive_path; { int points[] = { 1, 1, 1, 0, 2, 0, 2, 2, -2, 2, -2, 0, -1, 0, -1, 1 }; transform_array_to_path(points, sizeof(points) / sizeof(points[0]), positive_path, 10); } union_worker.AddPath(positive_path, ClipperLib::ptSubject, true); Path negative_path; { int points[] = { 1, -1, 1, 0, 2, 0, 2, -2, -2, -2, -2, 0, -1, 0, -1, -1 }; transform_array_to_path(points, sizeof(points) / sizeof(points[0]), negative_path, 10); } union_worker.AddPath(negative_path, ClipperLib::ptClip, true); union_worker.Execute(ClipperLib::ctUnion, solution, pftEvenOdd, pftEvenOdd); for (int k = 0; k < solution.size(); k++) { Path& path = solution[k]; printf("[ %dth ] : ", k + 1); for (int t = 0; t < path.size(); t++) { printf("%d,%d ", path[t].X, path[t].Y); } printf("\n"); } }合并后的结果输出:
// [1th] : -10, -1 - 10, -1 10, 0 10, 0 // [2th] : -20, -1 - 20, -1 20, 0 20, 0结果百思不得其解,结果怎么是一个线段了,莫名其妙???正确结果如下图,合并后是一个回字型。
不断地跟clipper自带的demo程序比对,终于发现了问题所在:问题出在Clipper内部的IntPoint,如果没有定义宏use_int32,采用的是long long存储顶点XY值,而上面code中printf是%d,使用%lld或者cout 就没问题了。坑啊。。。
void ClipperTest::polygon_with_hole_merge_test() { using namespace ClipperLib; Path path1_outer; Path path1_inner; { int outer[] = { -2, -2, 2, -2, 2, 2, -2, 2 }; int inner[] = { -1, -1, 1, -1, 1, 1, -1, 1 }; transform_array_to_path(outer, sizeof(outer)/sizeof(outer[0]), path1_outer); transform_array_to_path(inner, sizeof(inner)/sizeof(inner[0]), path1_inner); } Path path2; { int outer[] = { 2, 2, 3, 2, 3, -2, 2, -2 }; transform_array_to_path(outer, sizeof(outer) / sizeof(outer[0]), path2); } Paths sub_poly; sub_poly.push_back(path1_outer); sub_poly.push_back(path1_inner); Clipper union_worker; union_worker.AddPaths(sub_poly, ptSubject, true); union_worker.AddPath(path2, ptClip, true); Paths solution; union_worker.Execute(ClipperLib::ctUnion, solution, pftEvenOdd, pftEvenOdd); for (int k = 0; k < solution.size(); k++) { Path& path = solution[k]; printf("[ %dth ] : ", k + 1); for (int t = 0; t < path.size(); t++) { // printf("%d,%d ", path[t].X, path[t].Y); cout << path[t].X << "," << path[t].Y << " "; } printf("\n"); } }不用care顶点顺序,效果图如下:
原文地址:http://blog.csdn.net/ryfdizuo/article/details/40682639