我正在使用 boost 图形库并尝试初始化 aMutableGraph
以开始作为网格的生活。边缘将在以后的生活中添加和删除,所以我认为adjacency_list<vecS,listS,undirectedS>
是正确的选择。
我对 BGL 的阅读表明,用这些边缘初始化它的明智方法是boost::grid_graph
利用
boost::copy_graph
to copy from aboost::grid_graph
可以免费为我制作所有初始边缘。我认为这是有道理的——copy_graph
从一个模型复制VertexListGraph
到一个模型MutableGraph
,这正是我所拥有的。
我最初尝试使用 2 参数版本copy_graph
,模糊地希望其余的默认值会发生一些明智的事情。事实证明并非如此,grid_graph
(由于我无法弄清楚的原因)似乎没有将PropertyMap
s 与边或顶点一起使用的工具,因此默认vertex_copy
和edge_copy
失败(带有编译器错误)复制特性。
由于 2-argument 版本显然看起来不合适,我继续并尝试实现我自己的二元运算符来复制顶点和边。即使使用“无操作”副本,它也不能像我希望的那样工作(即它不能编译)。
我整理了一个说明问题的最小工作示例:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/copy.hpp>
struct Position {
int x, y;
};
struct VertexProperties {
Position pos;
};
typedef boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS,
VertexProperties> Graph;
struct MyCopy {
template <typename S, typename D>
void operator()(const S& /*src*/, D& /*dest*/) {
// Nothing for now, deduced types to try and just make it compile
// TODO: set values for pos to reflect position on grid.
}
};
int main() {
boost::array<std::size_t, 2> lengths = { { 3, 3 } };
boost::grid_graph<2> grid(lengths);
Graph graph;
MyCopy copier;
// Using 3-Arg version of copy_graph so we can specify a custom way of copying to create the properties
boost::copy_graph(grid,graph,boost::bgl_named_params<MyCopy,boost::vertex_copy_t,
boost::bgl_named_params<MyCopy,boost::edge_copy_t> >(copier));
}
此示例无法编译:
g++ -Wextra -Wall -O2 -g -o copytest.o -c copytest.cc
In file included from /usr/include/boost/graph/grid_graph.hpp:24:0,
from copytest.cc:2:
/usr/include/boost/iterator/transform_iterator.hpp: In constructor ‘boost::transform_iterator<UnaryFunction, Iterator, Reference, Value>::transform_iterator() [with UnaryFunc = boost::detail::grid_graph_vertex_at<boost::grid_graph<2u> >, Iterator = boost::counting_iterator<unsigned int, boost::use_default, boost::use_default>, Reference = boost::use_default, Value = boost::use_default]’:
/usr/include/boost/graph/copy.hpp:115:55: instantiated from ‘static void boost::detail::copy_graph_impl<0>::apply(const Graph&, MutableGraph&, CopyVertex, CopyEdge, Orig2CopyVertexIndexMap, IndexMap) [with Graph = boost::grid_graph<2u>, MutableGraph = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, VertexProperties>, CopyVertex = MyCopy, CopyEdge = MyCopy, IndexMap = boost::grid_graph_index_map<boost::grid_graph<2u>, boost::array<unsigned int, 2u>, unsigned int>, Orig2CopyVertexIndexMap = boost::iterator_property_map<__gnu_cxx::__normal_iterator<void**, std::vector<void*, std::allocator<void*> > >, boost::grid_graph_index_map<boost::grid_graph<2u>, boost::array<unsigned int, 2u>, unsigned int>, void*, void*&>]’
/usr/include/boost/graph/copy.hpp:327:5: instantiated from ‘void boost::copy_graph(const VertexListGraph&, MutableGraph&, const boost::bgl_named_params<P, T, R>&) [with VertexListGraph = boost::grid_graph<2u>, MutableGraph = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, VertexProperties>, P = MyCopy, T = boost::vertex_copy_t, R = boost::bgl_named_params<MyCopy, boost::edge_copy_t>]’
/mnt/home/ajw/code/hpcwales/copytest.cc:31:66: instantiated from here
/usr/include/boost/iterator/transform_iterator.hpp:100:26: error: no matching function for call to ‘boost::detail::grid_graph_vertex_at<boost::grid_graph<2u> >::grid_graph_vertex_at()’
/usr/include/boost/graph/grid_graph.hpp:104:7: note: candidates are: boost::detail::grid_graph_vertex_at<Graph>::grid_graph_vertex_at(const Graph*) [with Graph = boost::grid_graph<2u>]
/usr/include/boost/graph/grid_graph.hpp:100:33: note: boost::detail::grid_graph_vertex_at<boost::grid_graph<2u> >::grid_graph_vertex_at(const boost::detail::grid_graph_vertex_at<boost::grid_graph<2u> >&)
我对该错误的分析是,它似乎试图默认构造 的内部结构的一部分grid_graph
,由于某种原因,这对我来说并不是很清楚。(clang 并没有真正告诉我任何我从 g++ 看不到的东西)。
问题:
- 这是将可变图形初始化为常规网格的正确方法吗?我最初认为这比自己编写函数要容易得多,但现在我不太确定了!
- 为什么这里的默认值
orig_to_copy
和/或vertex_index
不合适?我假设这两个是错误的原因。(如果有的话,实际上是哪个导致了问题?我无法破译当前错误的根本原因是什么)。 - 解决此问题的“正确”方法是什么?