【问题标题】:Boost point in polygon giving erroneous results?多边形中的提升点会给出错误的结果?
【发布时间】:2016-06-27 13:59:24
【问题描述】:

我有以下单元测试,两项检查失败

BOOST_AUTO_TEST_CASE(test_point_in_polygon)
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    boost::geometry::model::polygon<point_type> poly;
    boost::geometry::read_wkt("POLYGON((-57.0635 -3.58045, -57.8088 -4.75336, -56.7036 -7.65533, -56.4646 -9.08261, -55.219 -9.42072, -54.6047 -9.54237, -53.868 -9.22164, -52.4139 -8.58345, -49.4691 -6.94159, -49.2295 -6.87687, -47.1079 -6.18013, -45.9159 -5.91411, -43.7897 -5.89581, -42.285 -5.95883, -40.1693 -6.38999, -38.1374 -6.87424, -35.7062 -7.67065, -34.6193 -8.40713, -34.1769 -8.74957, -31.785 -9.31504, -30.574 -9.70903, -26.909 -10.5865, -24.9817 -11.268, -22.6134 -12.2833, -21.0927 -13.3427, -20.006 -13.7029, -16.8407 -15.2466, -13.2203 -16.816, -11.3922 -17.4133, -10.7577 -17.6366, -8.54581 -18.2736, -7.18058 -18.6037, -4.86031 -19.0997, -4.25834 -19.3046, -3.39545 -19.3122, -0.985195 -19.7047, -0.340967 -19.9409, 0.75209 -19.8968, 3.30498 -20.3562, 6.82228 -20.6405, 10.4384 -20.5995, 13.8557 -20.2595, 14.7523 -20.1371, 18.3468 -19.4284, 18.9915 -19.4505, 22.5377 -18.5053, 23.2192 -18.4658, 26.4885 -17.3378, 27.2915 -17.2273, 30.5962 -15.8816, 32.7278 -14.6911, 33.8878 -14.1893, 36.2752 -12.7482, 38.5306 -11.1893, 40.8465 -9.28305, 41.1732 -9.05935, 43.372 -7.07535, 45.2436 -5.2073, 46.9925 -3.21699, 48.616 -1.35438, 50.628 0.592424, 52.158 1.37445, 53.1148 2.07603, 50.9152 5.07491, 50.3177 6.38521, 49.9412 7.3891, 48.8311 9.29466, 47.7653 12.692, 45.6416 16.3693, 43.7106 17.9319, 41.4998 17.9314, 37.4335 19.4761, 34.3477 18.4887, 32.1299 18.0676, 29.2684 18.0666, 26.5269 15.0811, 19.7706 14.5304, 15.8916 12.9664, 14.8124 12.5622, 12.783 10.0368, 12.4488 9.65713, 11.1008 7.47097, 8.9187 5.4486, 5.63935 1.10272, -6.34225 4.45488, -8.60563 7.7494, -16.2454 11.2148, -23.399 18.8388, -25.8497 20.2781, -28.8931 22.7068, -31.4379 22.8942, -32.5636 21.9563, -35.2968 20.2469, -37.3132 19.0133, -37.9703 18.2828, -40.4433 15.553, -40.9543 14.1728, -42.8783 10.3486, -46.3297 7.28714, -48.959 5.88183, -52.1939 2.65934, -54.8196 -0.896786))", poly);

    point_type point (-57.8088, -1.5755);
    BOOST_CHECK_EQUAL(boost::geometry::within(point, poly), false);

    point = point_type(-100, -2);
    BOOST_CHECK_EQUAL(boost::geometry::within(point, poly), false);

}

数据如下所示:

我使用它的方式与文档 (http://www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry/reference/algorithms/within/within_2.html) 类似。

如图,很明显两个点都在多边形之外。 我做错了什么,还是我发现了 boost 中的错误?

【问题讨论】:

  • @DieterLücking 感谢您的评论。我重新运行测试,将所有内容都转换为 int,但得到相同的结果。
  • @DieterLücking 库错误,指的是 Boost.Polygon,OP 正在使用 Boost.Geometry。从官方文档链接的页面中的示例使用 double 作为其坐标类型。
  • 我已经用另一种方法进行了编辑。

标签: c++ boost boost-geometry


【解决方案1】:

问题是由您使用的多边形类型与您提供的多边形数据不匹配引起的。通过使用boost::geometry::model::polygon&lt;point_type&gt;,您可以创建一个多边形,它的点类型为point_type,具有顺时针方向,即闭合,以及由默认模板参数配置的其他几项内容。在您提供的数据中,第一个点不等于最后一个点(这是closed 在此上下文中的定义)。您可以选择make your polygon not be closed (polygon&lt;point_type,true,false&gt;)make sure that your first and last points are equal (by copying the first point at the end)

编辑:另一种(可能更好)方法可能是使用boost::geometry::correct(poly);,而无需更改任何其他内容,如此(非常有趣)answer 所示。

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2013-01-26
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多