从下面的代码中我计算了两个多边形的交集。我希望如果输出不是多边形,则输出可以为NULL。但是输出是(((240,52.9999),(240,53),(240,53),(240,52.9999)))。这不是多边形。有没有办法检查输出是否真的是多边形??
#include <iostream>
#include <deque>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/algorithms/append.hpp>
#include <algorithm> // for reverse, unique
#include <iostream>
#include <string>
int main()
{
typedef boost::geometry::model::d2::point_xy<double, boost::geometry::cs::cartesian> point_2d;
typedef boost::geometry::model::polygon<point_2d> polygon_2d;
polygon_2d green;
boost::geometry::append(green, point_2d(286.188, 90.9575));
boost::geometry::append(green, point_2d(274.902, 56.2215));
boost::geometry::append(green, point_2d(274.238, 55.7393));
boost::geometry::append(green, point_2d(246.908, 51.9765));
boost::geometry::append(green, point_2d(194.477, 59.7441));
boost::geometry::append(green, point_2d(159.213, 101.141));
boost::geometry::append(green, point_2d(203.576, 149.537));
boost::geometry::append(green, point_2d(286.188, 90.9575));
polygon_2d blue;
boost::geometry::append(blue, point_2d(240, 53));
boost::geometry::append(blue, point_2d(240, -53));
boost::geometry::append(blue, point_2d(-60, -53));
boost::geometry::append(blue, point_2d(-60, 53));
boost::geometry::append(blue, point_2d(240, 53));
boost::geometry::correct(green);
boost::geometry::correct(blue);
std::vector<polygon_2d> output;
boost::geometry::intersection(green, blue, output);
std::cout << "green && blue:" << std::endl;
if(output.size())
{
std::cout<<boost::geometry::dsv(output[0])<<std::endl;
std::cout<<boost::geometry::area(output[0])<<std::endl;
}
return 0;
}
答案 0 :(得分:1)
您可以清除无效的几何图形:
output.erase(
std::remove_copy_if(output.begin(), output.end(), output.begin(), [](polygon_2d const&g) { return boost::geometry::is_valid(g); }),
output.end());
现在,结果将按预期为空。
我感兴趣的是&#34;无效&#34;几何形状将导致第一位。您可能已经意识到您的多边形选择得非常好:
让我们放大一点:
现在,您可能正在考虑一些浮点精度问题,例如long long
points或增强多精度的相同实验,例如:
typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<1024*128>> oopmh; // that's a lotta oomph
typedef boost::geometry::model::d2::point_xy<oopmh, boost::geometry::cs::cartesian> point_2d;
你会看到我们得到了
green && blue:
(((2.4e+06, 530000), (-600000, 530000), (-600000, -530000), (2.4e+06, -530000), (2.4e+06, 530000)))
nan
所以它看起来非常像我们只是得到一个奇点的区域。如果你不想要这个,那么也许你应该只定义一个&#34; epsilon&#34;排序值,您停止考虑重叠重叠的阈值。