在使用cgal进行增量空间搜索时修改函子变量
Modifying functor variable when using incremental spatial searching with cgal
我修改了计算几何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 with 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
变量?
将Xk()
替换为Xk
或者用构造函数参数替换assign(k)
,然后替换Xk()
与X_not_positive_k(1)
其实真正麻烦的是函子的名字!将名称X_not_positive_k
替换为X_less_than
-因此调用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;
};
你的错误在于
NN_positive_x_iterator it(.., X_not_positive(), .. )
X_not_positive
是类型,X_not_positive()
是构造函数的调用,
而在你的代码
NN_positive_x_iterator it(.., Xk(),.. )
Xk
不是类型而是对象,Xk()
是对operator()
的调用这是零参数的函数操作符,因此出现错误消息。
andreas
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中搜索嵌套多映射值
- 独立读取-修改-写入顺序
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 为什么二进制搜索在我的测试中不起作用
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我可以通过引用修改常量返回
- 对于结构,表达式必须是可修改的ivalue
- 正在尝试重载二进制搜索树分配运算符
- 修改广度优先搜索算法以记住矩阵中的最短路径
- 为什么我的原始二叉搜索树被修改,即使我在另一个节点中复制了根节点
- 我如何使用此二叉搜索函数的修改后的最大/最小值创建新数组
- 通过修改二叉搜索算法来改进它,使其在搜索大量单词(单词列表)中的单词时工作得更快
- 是否可以修改并排程序集搜索序列
- 在使用cgal进行增量空间搜索时修改函子变量
- 二进制搜索树-C++-表达式必须是可修改的左值
- 我想通过引入深度来限制搜索级别,将无与伦比的井字游戏修改为适用于5乘5的区块.任何建议