【问题标题】:How to get the Voronoi diagram of some points to display in QT?如何获得一些点的Voronoi图在QT中显示?
【发布时间】:2011-12-13 14:51:48
【问题描述】:

我在 QT 中的 QGraphicsScene 上绘制了一些点,并将它们封装到点类中。我想计算这些点的 Voronoi 图并将其显示到场景中。做这个的最好方式是什么?

我正在考虑使用 CGAL,但我找不到这样做的好方法..

【问题讨论】:

    标签: c++ qt computational-geometry cgal voronoi


    【解决方案1】:

    您需要显示的是限制为矩形(您的显示窗口)的 Voronoi 图。这是一个简单的方法。

    相关文档页面herethere

    #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
    #include <CGAL/Delaunay_triangulation_2.h>
    #include <iterator>
    
    typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
    typedef K::Point_2 Point_2;
    typedef K::Iso_rectangle_2 Iso_rectangle_2;
    typedef K::Segment_2 Segment_2;
    typedef K::Ray_2 Ray_2;
    typedef K::Line_2 Line_2;
    typedef CGAL::Delaunay_triangulation_2<K>  Delaunay_triangulation_2;
    
    //A class to recover Voronoi diagram from stream.
    //Rays, lines and segments are cropped to a rectangle
    //so that only segments are stored
    struct Cropped_voronoi_from_delaunay{
      std::list<Segment_2> m_cropped_vd;
      Iso_rectangle_2 m_bbox;
    
      Cropped_voronoi_from_delaunay(const Iso_rectangle_2& bbox):m_bbox(bbox){}
    
      template <class RSL>
      void crop_and_extract_segment(const RSL& rsl){
        CGAL::Object obj = CGAL::intersection(rsl,m_bbox);
        const Segment_2* s=CGAL::object_cast<Segment_2>(&obj);
        if (s) m_cropped_vd.push_back(*s);
      }
    
      void operator<<(const Ray_2& ray)    { crop_and_extract_segment(ray); }
      void operator<<(const Line_2& line)  { crop_and_extract_segment(line); }
      void operator<<(const Segment_2& seg){ crop_and_extract_segment(seg); }
    };
    
    int main(){
      //consider some points
      std::vector<Point_2> points;
      points.push_back(Point_2(0,0));
      points.push_back(Point_2(1,1));
      points.push_back(Point_2(0,1));
    
      Delaunay_triangulation_2 dt2;
      //insert points into the triangulation
      dt2.insert(points.begin(),points.end());
      //construct a rectangle
      Iso_rectangle_2 bbox(-1,-1,2,2);
      Cropped_voronoi_from_delaunay vor(bbox);
      //extract the cropped Voronoi diagram
      dt2.draw_dual(vor);
      //print the cropped Voronoi diagram as segments
      std::copy(vor.m_cropped_vd.begin(),vor.m_cropped_vd.end(),
        std::ostream_iterator<Segment_2>(std::cout,"\n"));
    }
    

    【讨论】:

      【解决方案2】:

      如果您考虑使用 CGAL 库,您是否阅读过its section about Voronoi diagrams?在Software Design 小节中,他们解释了所涉及的数据结构。根据我的阅读,您可以获得代表狄利克雷单元格的多边形,然后您可以告诉 Qt 绘制并用不同的颜色填充它们(一些世界地图着色算法可能在这里有用)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多