0

我修改了计算几何cgal库(链接)给出的示例,该示例演示了在 2D 平面上的增量搜索(第 49.3.2 节)。该示例使用函子设置空间边界,以便仅搜索平面上的正点。

我想修改仿函数以便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变量吗?

4

2 回答 2

1

只需在第 56 行替换你Xk()Xk

或者替换assign(k)为构造函数参数,然后替换Xk()X_not_positive_k(1)

实际上真正的麻烦是函子的名字!将 name 替换为X_not_positive_k-X_less_than所以在第 56 行调用 toX_less_than(1)看起来不错

像这样:

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;
};
于 2013-05-18T22:49:57.463 回答
1

你的错误是在这条线上

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

X_not_positive是一个类型和X_not_positive() 构造函数的调用,

在你的代码中

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

Xk不是类型而是对象,并且Xk()是对operator() 具有零参数的函数运算符的调用,因此是错误消息。

安德烈亚斯

于 2013-05-19T09:38:48.577 回答