【问题标题】:How to calculate the intersection of a line segment and circle with CGAL如何用CGAL计算线段和圆的交点
【发布时间】:2014-10-07 22:32:58
【问题描述】:

我一直在用头撞墙,试图了解如何使用 CGAL 的圆形内核来计算线段 (Line_Arc_2) 和圆 (Circle_2) 之间的交点。不幸的是,循环内核的示例代码并不多,而且我没有找到参考手册有多大帮助。

这是我认为可以工作的代码,但现在它甚至无法编译(Mac OS 10.9 使用最新的系统编译器):

#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>

typedef CGAL::Exact_circular_kernel_2        CircK;
typedef CGAL::Point_2<CircK>                 Pt2;
typedef CGAL::Circle_2<CircK>                Circ2;
typedef CGAL::Line_arc_2<CircK>              LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;

int main(){
  int n = 0;
  Circ2 c       = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
  LineArc2 l    = LineArc2( Pt2(0,-2), Pt2(0,2) );

  std::vector<Res> result;
  CGAL::intersection(c, l, std::back_inserter(result));

  return 0;
}

我在 result_of 行收到一个错误:“错误:在...中没有名为 'result_type' 的类型”,第二个错误是“没有可行的重载 '='”可用于相交线。

此外,因为这可能是后续问题,一旦它起作用:我如何实际到达放在向量中的交点? CGAL 的文档向我建议“结果”应该包含一对 Circular_arc_point_2 和一个表示其多重性的无符号整数。这是我在这种情况下实际得到的吗?更一般地说,有没有人知道使用 Circular Kernel 和 Spherical Kernel 相交例程的好教程?

谢谢!

【问题讨论】:

    标签: geometry computational-geometry cgal


    【解决方案1】:

    因此,尽管 CGAL 参考手册中建议了 CircularKernel 的交集函数,但 result_of 似乎在这里不起作用。

    这是一个不同的版本,似乎可以正常工作并且可以正确处理输出:

    #include <vector>
    #include <iterator>
    #include <CGAL/Exact_circular_kernel_2.h>
    #include <CGAL/Circular_kernel_intersections.h>
    #include <CGAL/intersections.h>
    #include <CGAL/iterator.h>
    
    typedef CGAL::Exact_circular_kernel_2        CircK;
    typedef CGAL::Point_2<CircK>                 Pt2;
    typedef CGAL::Circle_2<CircK>                Circ2;
    typedef CGAL::Line_arc_2<CircK>              LineArc2;
    typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
    
    using namespace std;
    
    int main(){
      int n = 0;
      Circ2 c       = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
      LineArc2 l    = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
    
      std::vector<IsectOutput> output;
    
      typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>, 
                          CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
      Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
    
      CGAL::intersection(l, c, disp);
    
      cout << output.size() << endl;
      for( const auto& v : output ){
        cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: " 
         << v.second << std::endl;
      }
    
    
      return 0;
    }
    

    【讨论】:

      【解决方案2】:

      result_of 正在工作,但您要求的运算符不存在,您缺少输出迭代器。 但是,我同意该文档具有误导性。我会努力解决的。

      以下代码运行良好:

      #include <vector>
      #include <iterator>
      #include <CGAL/Exact_circular_kernel_2.h>
      #include <CGAL/Circular_kernel_intersections.h>
      #include <CGAL/intersections.h>
      #include <CGAL/result_of.h>
      #include <CGAL/iterator.h>
      #include <CGAL/point_generators_2.h>
      #include <boost/bind.hpp>
      
      typedef CGAL::Exact_circular_kernel_2        CircK;
      typedef CGAL::Point_2<CircK>                 Pt2;
      typedef CGAL::Circle_2<CircK>                Circ2;
      typedef CGAL::Line_arc_2<CircK>              LineArc2;
      typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
      typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
      
      int main(){
        Circ2 c       = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
        LineArc2 l    = LineArc2( Pt2(0,-2), Pt2(0,2) );
      
        std::vector<InterRes> result;
        CGAL::intersection(c, l, std::back_inserter(result));
      
        return 0;
      }
      

      【讨论】:

      • 好的,感谢您的澄清。更新文档会很有帮助!
      猜你喜欢
      • 2016-07-29
      • 2015-07-12
      • 2011-09-26
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多