2013-05-18 25 views
0

我修改了計算幾何cgal庫(link)給出的示例,演示了在2D平面上進行增量搜索(第49.3.2節)。該示例使用函數來設置空間範圍,以僅搜索飛機上的正點。使用cgal進行增量空間搜索時修改函數變量

我想修改函子,以便k可以傳入,如struct X_not_positive_k所示。下面的完整示例程序顯示了修改後的代碼和原始代碼。

#include <CGAL/Simple_cartesian.h> 
#include <CGAL/Orthogonal_incremental_neighbor_search.h> 
#include <CGAL/Search_traits_2.h> 

typedef CGAL::Simple_cartesian<double> K; 
typedef K::Point_2 Point_d; 
typedef CGAL::Search_traits_2<K> TreeTraits; 
typedef CGAL::Orthogonal_incremental_neighbor_search<TreeTraits> NN_incremental_search; 
typedef NN_incremental_search::iterator NN_iterator; 
typedef NN_incremental_search::Tree Tree; 

int main() { 

    Tree tree; 
    tree.insert(Point_d(0,0)); 
    tree.insert(Point_d(1,1)); 
    tree.insert(Point_d(0,1)); 
    tree.insert(Point_d(10,110)); 
    tree.insert(Point_d(45,0)); 
    tree.insert(Point_d(0,2340)); 
    tree.insert(Point_d(0,30)); 

    Point_d query(0,0); 

    // A functor that returns true, iff the x-coordinate of a dD point is not positive 
    // [ORIGINAL CODE] 
    struct X_not_positive { 
     bool operator()(const NN_iterator& it) { return ((*it).first)[0]<0; } 
    }; 

    // [MODIFIED CODE] 
    // This does not work when used below. 
    struct X_not_positive_k { 
    public: 
     void assign_k(int k) {this->k = k; } 
     bool operator()(const NN_iterator& it) { return ((*it).first)[0] < k; } 
    private: 
     int k; 
    }; 
    X_not_positive_k Xk; 
    Xk.assign_k(1); 

    // An iterator that only enumerates dD points with positive x-coordinate 
    // [ORIGINAL CODE] 
    // typedef CGAL::Filter_iterator<NN_iterator, X_not_positive> NN_positive_x_iterator; 

    // [MODIFIED CODE] 
    typedef CGAL::Filter_iterator<NN_iterator, X_not_positive_k> NN_positive_x_iterator; 

    NN_incremental_search NN(tree, query); 

    // [ORIGINAL CODE] 
    // NN_positive_x_iterator it(NN.end(), X_not_positive(), NN.begin()), end(NN.end(), X_not_positive()); 

    // [MODIFIED CODE] 
    NN_positive_x_iterator it(NN.end(), Xk(), NN.begin()), end(NN.end(), Xk()); 
    // error occurs here 

    std::cout << "The first 5 nearest neighbours with positive x-coord are: " << std::endl; 
    for (int j=0; (j < 5)&&(it!=end); ++j,++it) 
     std::cout << (*it).first << " at squared distance = " << (*it).second << std::endl; 

return 0; 
} 

但是,編譯此程序(Windows 7與Visual Studio 2010)會導致以下編譯器錯誤。錯誤的位置在上面的代碼中標出。

2>..\main.cpp(56): error C2064: term does not evaluate to a function taking 0 arguments 
2>   class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments 
2>..\main.cpp(56): error C2064: term does not evaluate to a function taking 0 arguments 
2>   class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments 
2> 

可以做些什麼來擺脫錯誤?是否有另一種方法可以設置k變量?

回答

1

只需更換您的Xk()Xk在這條線56

或者與構造函數的參數取代assign(k),然後用X_not_positive_k(1)

實際上真正的麻煩取代Xk() 是仿函數的名字!與X_less_than更換名稱X_not_positive_k - 所以打電話X_less_than(1)看起來管線56

喜歡這個漂亮:

struct X_less_than { 
public: 
    X_less_than(int i) 
     : k(i) 
    { 
    } 
    bool operator()(const NN_iterator& it) { return ((*it).first)[0] < k; } 
private: 
    int k; 
}; 
+0

非常感謝你這個美麗的答案!你是對的;所有這些方法都很好。這很棒。 –

1

你的錯誤是,在該行

NN_positive_x_iterator it(.., X_not_positive(), ..)

X_not_positive是一個類型, X_not_positive()構造函數的調用,

而在代碼

NN_positive_x_iterator it(.., Xk(),..)

Xk不是一個類型,但一個對象,並且Xk()是該呼叫到operator() 即函數算子與零個參數,因此,錯誤消息。

andreas

+0

謝謝安德烈亞斯;這非常有啓發性。 –