注意:请参阅本文底部的编辑,了解解决我问题的代码。
我正在CGAL中构建一个Voronoi图。我的图表中的每个站点都有一些与其相关的非几何信息(如城市的人口,或某个位置的温度)。在构建Voronoi图之后,我将对其执行位置查询,并且我将需要能够从查询结果中恢复此非几何信息。似乎CGAL有能力做到这一点;这在Delaunay三角测量的背景下讨论了例如here。但我无法弄清楚如何将这种技术应用于Voronoi图。这是我尝试过的,无法编译的内容:
删除了破碎的代码。请参阅下面的工作代码示例。
删除了问题的剩余部分,因为它与删除的,损坏的代码有关。请参阅下面的工作代码示例。
编辑1:修正了由sloriot指出的错误,包括完整的编译器输出。
编辑2:在评论中,Fabri博士建议我首先修改CGAL示例vd_2_point_location.cpp。根据他的建议,我在这里发布一个工作代码。这是vd_2_point_location.cpp的一个非常轻微修改的版本。
// standard includes
#include <iostream>
#include <fstream>
#include <cassert>
// includes for defining the Voronoi diagram adaptor
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Delaunay_triangulation_adaptation_traits_2.h>
#include <CGAL/Delaunay_triangulation_adaptation_policies_2.h>
// typedefs for defining the adaptor
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_vertex_base_with_info_2<int, K> VB;
typedef CGAL::Triangulation_data_structure_2<VB> TDS;
typedef CGAL::Delaunay_triangulation_2<K,TDS> DT;
typedef CGAL::Delaunay_triangulation_adaptation_traits_2<DT> AT;
typedef CGAL::Delaunay_triangulation_caching_degeneracy_removal_policy_2<DT> AP;
typedef CGAL::Voronoi_diagram_2<DT,AT,AP> VD;
// typedef for the result type of the point location
typedef AT::Site_2 Site_2;
typedef AT::Point_2 Point_2;
typedef VD::Locate_result Locate_result;
typedef VD::Vertex_handle Vertex_handle;
typedef VD::Face_handle Face_handle;
typedef VD::Halfedge_handle Halfedge_handle;
typedef VD::Ccb_halfedge_circulator Ccb_halfedge_circulator;
void print_endpoint(Halfedge_handle e, bool is_src) {
std::cout << "\t";
if ( is_src ) {
if ( e->has_source() ) std::cout << e->source()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
} else {
if ( e->has_target() ) std::cout << e->target()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
}
}
int main()
{
std::ifstream ifs("data/data1.dt.cin");
assert( ifs );
VD vd;
Site_2 t;
int ctr=0;
while ( ifs >> t ) {
Face_handle fh = vd.insert(t); // Add the site...
fh->dual()->info() = ctr++; // ...attach some info to it
}
ifs.close();
assert( vd.is_valid() );
std::ifstream ifq("data/queries1.dt.cin");
assert( ifq );
Point_2 p;
while ( ifq >> p ) {
std::cout << "Query point (" << p.x() << "," << p.y()
<< ") lies on a Voronoi " << std::flush;
Locate_result lr = vd.locate(p);
if ( Vertex_handle* v = boost::get<Vertex_handle>(&lr) ) {
std::cout << "vertex." << std::endl;
std::cout << "The Voronoi vertex is:" << std::endl;
std::cout << "\t" << (*v)->point() << std::endl;
} else if ( Halfedge_handle* e = boost::get<Halfedge_handle>(&lr) ) {
std::cout << "edge." << std::endl;
std::cout << "The source and target vertices "
<< "of the Voronoi edge are:" << std::endl;
print_endpoint(*e, true);
print_endpoint(*e, false);
} else if ( Face_handle* f = boost::get<Face_handle>(&lr) ) {
std::cout << "face." << std::endl;
std::cout << "The info associated with this face is "
<< (*f)->dual()->info() << std::endl;
std::cout << "The vertices of the Voronoi face are"
<< " (in counterclockwise order):" << std::endl;
Ccb_halfedge_circulator ec_start = (*f)->ccb();
Ccb_halfedge_circulator ec = ec_start;
do {
print_endpoint(ec, false);
} while ( ++ec != ec_start );
}
std::cout << std::endl;
}
ifq.close();
return 0;
}