【问题标题】:Split geometry using Boost Library使用 Boost 库分割几何
【发布时间】:2013-03-05 06:38:09
【问题描述】:

有没有办法使用 Boost 库对几何图形执行拆分操作?

【问题讨论】:

    标签: boost split boost-geometry


    【解决方案1】:

    我不确定您的确切想法,但您可以查看 Boost.Geometry (http://www.boost.org/libs/geometry) 中实现的算法。

    我猜你可以使用 intersection() 或 difference() 算法来实现它。

    【讨论】:

      【解决方案2】:

      您对输入的内容和拆分的定义不够具体。

      有没有办法使用 Boost 库对几何图形执行拆分操作?

      下面是我尝试用一​​个快速简单但不一定是最佳的示例来回答您的问题给定点。

      #include <boost/geometry.hpp>
      #include <algorithm>
      #include <iostream>
      #include <vector>
      namespace bg = boost::geometry;
      using point_2d = bg::model::d2::point_xy<double>;
      using linestring_2d = bg::model::linestring<point_2d>;
      
      int main()
      {
          point_2d pt(2.5, 0);
          linestring_2d ls({{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}});
      
          auto split_begin = std::find_if(bg::segments_begin(ls), bg::segments_end(ls),
              [&pt](auto const& segment) { return bg::intersects(segment, pt); });
      
          linestring_2d line1;
          std::for_each(bg::segments_begin(ls), split_begin, [&line1](auto const& s)
          {
              bg::append(line1, s.first);
          });
          bg::append(line1, split_begin->first);
          bg::append(line1, pt);
      
          linestring_2d line2;
          bg::append(line2, pt);
          bg::append(line2, split_begin->second);
          std::for_each(++split_begin, bg::segments_end(ls), [&line2](auto const& s)
          {
              bg::append(line2, s.second);
          });
      
          std::cout << "line:        " << bg::dsv(ls) << std::endl;
          std::cout << "split point: " << bg::dsv(pt) << std::endl;
          std::cout << "line1:       " << bg::dsv(line1) << std::endl;
          std::cout << "line2:       " << bg::dsv(line2) << std::endl;
      }
      

      输出:

      line:        ((0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0))
      split point: (2.5, 0)
      line1:       ((0, 0), (1, 0), (2, 0), (2.5, 0))
      line2:       ((2.5, 0), (3, 0), (4, 0), (5, 0))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多