结果中的 boost 几何形状不正确

标签 boost geometry

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>


int main()
{

  using Point = boost::geometry::model::d2::point_xy<double>;
  using MultiPoint = boost::geometry::model::multi_point<Point>;

  MultiPoint test_poly;
  boost::geometry::append(test_poly, Point(2, 0));
  boost::geometry::append(test_poly, Point(-2, 0));
  boost::geometry::append(test_poly, Point(0, 2));
  boost::geometry::append(test_poly, Point(2, 0));

  std::cout << "within_result: "
            << boost::geometry::within(Point(0, 1), test_poly) << std::endl;

  return 0;
}

虽然 (0,0) 显然在多边形内部,但为什么结果是 false?是多点引起的问题吗?

最佳答案

是的。 (0,0) 显然在多边形内部。

您将变量称为test_poly,但您的模型不是多边形。多点没有任何面积。点位于多点“内”的唯一方法是使确切的点位于点集中。

与下面的测试平台比较:

<强> Live On Compiler Explorer

#include <boost/core/demangle.hpp>
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg     = boost::geometry;
using Point      = bg::model::d2::point_xy<double>;
using Polygon    = bg::model::polygon<Point>;
using MultiPoint = bg::model::multi_point<Point>;
using MultiPoly  = bg::model::multi_polygon<Polygon>;

template <typename Geometry> void buildBox(int offset, Geometry& g) {
    using P = typename bg::point_type<Geometry>::type;
    bg::append(g, P(offset, 0));
    bg::append(g, P(-offset, 0));
    bg::append(g, P(0, offset));
    bg::append(g, P(offset, 0));
}

void buildBox(int offset, MultiPoly& g) { buildBox(offset, g.emplace_back()); }

template <typename Geometry> void do_test() {
    Geometry g;
    buildBox(2, g);

    std::cout << "\n--- " << boost::core::demangle(typeid(Geometry).name())
              << " --- \n"
              << bg::wkt(g) << "\n";

    if (std::string reason; not bg::is_valid(g, reason)) {
        std::cout << "trying to correct: " << reason << "\n";
        bg::correct(g);
        std::cout << " -> (" << bg::wkt(g) << ")\n";
    }

    for (auto test : {Point{0, 1}, {0, 2}, {0, 3}})
        std::cout << "within(" << bg::wkt(test)
                  << ", g): " << bg::within(test, g) << std::endl;
}

int main() {
    std::cout << std::boolalpha;
    do_test<MultiPoint>();
    do_test<Polygon>();
    do_test<MultiPoly>();
}

哪个打印

--- boost::geometry::model::multi_point<boost::geometry::model::d2::point_xy<double, boost::geometry::cs::cartesian>, std::vector, std::allocator> ---
MULTIPOINT((2 0),(-2 0),(0 2),(2 0))
within(POINT(0 1), g): false
within(POINT(0 2), g): true

--- boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double, boost::geometry::cs::cartesian>, true, true, std::vector, std::vector, std::allocator, std::allocator> ---
POLYGON((2 0,-2 0,0 2,2 0))
within(POINT(0 1), g): true
within(POINT(0 2), g): false

--- boost::geometry::model::multi_polygon<boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double, boost::geometry::cs::cartesian>, true, true, std::vector, std::vector, std::a
llocator, std::allocator>, std::vector, std::allocator> ---
MULTIPOLYGON(((2 0,-2 0,0 2,2 0)))
within(POINT(0 1), g): true
within(POINT(0 2), g): false

关于结果中的 boost 几何形状不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74134057/

相关文章:

python - 返回矩形内的点列表

c++ - 在 boost 上从 bitset 到 bitset 的无序(哈希)映射

c++ - 功能引用列表

java - 递归将同心圆压缩 10%,同时保持相对于中心的位置

python - 如何使用PIL绘制圆弧?

unity-game-engine - 具有 8 个网格球体的 3D 球形地形。网格的边缘很明显,我不知道为什么

c++ - 我对C++ boost::asio和std::async有疑问

c++ - boost::asio::deadline_timer renew 仍然调用处理函数

c++ - 为什么 intrusive_ptr 和 shared_ptr 不能与 boost::intrusive 容器一起使用?

postgresql - 如何使用 Postgis 缓冲点?