2017-05-21 26 views
1

我正在使用Point_set_2數據結構爲了查找查詢點的k個最近鄰居,我想檢索鄰居的索引;我使用了下面的代碼,但它 - > info()會產生錯誤! 我也看到this post,但對我來說,優先使用Point_set_2方法:CGAL:獲取最近鄰居的信息

#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/Point_set_2.h> 
#include <vector> 

typedef CGAL::Exact_predicates_inexact_constructions_kernel  K; 
typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned, K> Vb; 
typedef CGAL::Triangulation_data_structure_2<Vb>     Tds; 
typedef CGAL::Delaunay_triangulation_2<K, Tds>     Delaunay; 
//typedef Delaunay::Point            Point; 
typedef CGAL::Point_set_2<K,Tds>::Edge_iterator     Edge_iterator; 
typedef CGAL::Point_set_2<K,Tds>::Vertex_handle     Vertex_handle; 
typedef K::Point_2            Point_2; 

CGAL::Point_set_2<K,Tds> PSet; 

int main() 
{ 
    std::vector< std::pair<Point_2,unsigned> > points; 
    points.push_back(std::make_pair(Point_2(0,0),0) ); 
    points.push_back(std::make_pair(Point_2(1,0),1) ); 
    points.push_back(std::make_pair(Point_2(0,1),2) ); 
    points.push_back(std::make_pair(Point_2(14,4),3) ); 
    points.push_back(std::make_pair(Point_2(2,2),4) ); 
    points.push_back(std::make_pair(Point_2(-4,0),5) ); 

    PSet.insert(points.begin(),points.end()); 
    // init 
    Point_2 actual(30,45,10); 
    // nearest neighbor ... 
    Vertex_handle v = PSet.nearest_neighbor(actual); 
    std::cout << "Nearest neighbor:" << v->point() << "\n"; 
    // k nearest neighbors ... 
    std::vector<Vertex_handle> L; 
    std::vector<Vertex_handle>::const_iterator it; 
    PSet.nearest_neighbors(actual,5, std::back_inserter(L)); 
    std::cout << "actual point: " << actual << "\n"; 
    for (it=L.begin();it != L.end(); it++) 
     std::cout << it->info() << "\n"; 
    return 0; 
} 

回答

0

一個Vertex_handle大致相當於一個指針。要訪問它的數據成員,您必須對其進行解引用或使用->運算符。 如果你有一個向量Vertex_handle,那麼迭代器在Vertex_handle 這意味着你必須取消引用迭代器來訪問Vertex_handle。你應該寫(*it)->info()

也許混淆來自三角測量的迭代器可以隱式轉換爲句柄類型的事實。