使用boost::geometry::union_ 合并边界(内、外):方案二

使用boost::geometry::union_ 合并边界(内、外):方案二


typedef boost::geometry::model::d2::point_xy<double> boost_point;
typedef boost::geometry::model::polygon<boost_point> boost_Polygon;

struct Point
{
    float x;
    float y;
    Point(float _x,float _y)
    {
      x = _x;
      y = _y;
    }
    Point(const boost_point & pt)
    {
      x = pt.x();
      y = pt.y();
    }
    operator boost_point()
    {
      return boost_point(x, y);
    }
};




bool TestClientTrafficEvent::TestBorderCombine()
{


//str_points_of_cur_frame_data = "POLYGON((1.500000 1.500000,1.500000 2.500000,2.500000 2.500000,2.500000 1.500000,1.500000 1.500000))".


  std::vector<Point> lhs;
  {
    Point lhs_p1(1.5, 1.5); 
    Point lhs_p2(1.5,2.5); //remain
    Point lhs_p3(2.5, 2.5); //remain
    Point lhs_p4(2.5, 1.5);
    Point lhs_p5(1.5, 1.5);
   
    lhs.push_back(lhs_p1);
    lhs.push_back(lhs_p2);
    lhs.push_back(lhs_p3);
    lhs.push_back(lhs_p4);
    lhs.push_back(lhs_p5);
  }

//str_points_of_compared_frame_data = "POLYGON((1.000000 1.000000,1.000000 2.000000,2.000000 2.000000,2.000000 1.000000,1.000000 1.000000))";
  std::vector<Point> rhs;
  {
    Point rhs_p1(1.0, 1.0); 
    Point rhs_p2(1.0, 2.0);
    Point rhs_p3(2.0, 2.0);
    Point rhs_p4(2.0, 1.0);
    Point rhs_p5(1.0, 1.0);    
  }
  
  boost_Polygon lhs_lt, rhs_lt;
  for (auto & pt : lhs)
  {
      boost::geometry::append(lhs_lt, (boost_point)pt);
  }
  
  for (auto & pt : rhs)
  {
      boost::geometry::append(rhs_lt, (boost_point)pt);
  }


 //进行并集计算
  std::vector<boost_Polygon> r;
  boost::geometry::union_(lhs_lt, rhs_lt, r);


  //通过验证:与传入的顺序无关,均返回相同的结果。
  std::vector<Point> vctr_combined ;
  if (!r.empty())//有结果返回
  {
    for (auto & pt : r[0].outer())
    {
      vctr_combined.push_back(pt);
    }
  }    


  std::cout << "vctr_combined.size()  = " << vctr_combined.size() << std::endl;

  for(auto iter : vctr_combined)
  {
    std::cout << "iter.x  = " << iter.x << ", iter.y  = " << iter.y << std::endl;
  }

  return true;
  
}


  • P1、P5 为新增自动生成的交叉点

  • P1为首位重合点

  • P1 ~ P8 ~ P8 为顺时针顺序

使用boost::geometry::union_ 合并边界(内、外):方案二_第1张图片

你可能感兴趣的:(C++,boost,c++,算法,boost)