c++ - 无法在 CGAL 中创建 AABB 树类型的 STL 容器

标签 c++ containers computational-geometry cgal

我正在尝试创建类型为 map<int,CGAL::AABB_tree<Traits>> 的 STL 映射(AABB tree's 的 map )当我尝试为 map 分配一个值时,例如(此代码仅用于演示目的):

//CGAL includes begin
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
//CGAL includes end
/*
 * CGAL typedef's for initialization
 */
typedef CGAL::Simple_cartesian<double>                          K;
typedef K::FT                                                   FT;
typedef K::Point_3                                              Point_3;
typedef K::Segment_3                                            Segment;
typedef CGAL::Polyhedron_3<K>                                   Polyhedron;
typedef Polyhedron::HalfedgeDS                                  HalfedgeDS;
typedef Polyhedron::Vertex_const_iterator                       Vertex_const_iterator;
typedef Polyhedron::Facet_const_iterator                        Facet_const_iterator;
typedef Polyhedron::Halfedge_around_facet_const_circulator      Halfedge_around_facet_const_circulator;
typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron>    Primitive;
typedef CGAL::AABB_traits<K, Primitive>                         Traits;
typedef CGAL::AABB_tree<Traits>                                 Tree;
typedef Tree::Point_and_primitive_id                            Point_and_primitive_id;
//end of typedef's

BuildMesh<HalfedgeDS> mesh(V, F);
polyhedron.delegate( mesh);
myMap[someInt] = Tree(polyhedron.facets_begin(),polyhedron.facets_end(),polyhedron);

我收到以下错误:

error C2248: 'CGAL::AABB_tree< AABBTraits >::operator =' : cannot access private member declared in class 'CGAL::AABB_tree< AABBTraits>'

我尝试查看 CGAL 的源代码并在 CGAL\AABB_tree.h 中找到以下几行:

private:
        // Disabled copy constructor & assignment operator
        typedef AABB_tree<AABBTraits> Self;
        AABB_tree(const Self& src);
        Self& operator=(const Self& src);

这意味着复制和赋值构造函数是私有(private)的,因此不可能创建 树类型的 STL 容器。

我尝试使用指针代替我将 map 更改为 map<int,CGAL::AABB_tree < Traits > * > 并尝试:

BuildMesh<HalfedgeDS> mesh(V, F);
polyhedron.delegate( mesh);
myMap[someInt] = new Tree(polyhedron.facets_begin(),polyhedron.facets_end(),polyhedron);

但随后它使我的代码崩溃。

有什么办法可以创建这种类型的 STL 容器吗?

2014 年 3 月 14 日更新

我尝试按照建议使用 Drop 的解决方案,但出现以下错误:

error C2248: 'CGAL::AABB_tree::AABB_tree' : cannot access private member declared in class 'CGAL::AABB_tree'

2014 年 3 月 17 日更新

这是一个无法编译的示例代码:

#include <iostream>
#include <map>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
using std::map;

typedef CGAL::Simple_cartesian<double> K;
typedef K::FT FT;
typedef K::Point_3 Point;
typedef K::Segment_3 Segment;
typedef CGAL::Polyhedron_3<K> Polyhedron;
typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron> Primitive;
typedef CGAL::AABB_traits<K, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree;
typedef Tree::Point_and_primitive_id Point_and_primitive_id;
int main()
{
    map<int,Tree> myMap;
    Point p(1.0, 0.0, 0.0);
    Point q(0.0, 1.0, 0.0);
    Point r(0.0, 0.0, 1.0);
    Point s(0.0, 0.0, 0.0);
    Polyhedron polyhedron;
    polyhedron.make_tetrahedron(p, q, r, s);

    // here i get the error
    myMap.emplace(
        std::piecewise_construct,
        std::forward_as_tuple(1),
        std::forward_as_tuple(polyhedron.facets_begin(), polyhedron.facets_end(),polyhedron));
    myMap[1].accelerate_distance_queries();
    // query point
    Point query(0.0, 0.0, 3.0);
    // computes squared distance from query
    FT sqd = myMap[1].squared_distance(query);
    std::cout << "squared distance: " << sqd << std::endl;
    // computes closest point
    Point closest = myMap[1].closest_point(query);
    std::cout << "closest point: " << closest << std::endl;
    // computes closest point and primitive id
    Point_and_primitive_id pp = myMap[1].closest_point_and_primitive(query);
    Point closest_point = pp.first;
    Polyhedron::Face_handle f = pp.second; // closest primitive id
    std::cout << "closest point: " << closest_point << std::endl;
    std::cout << "closest triangle: ( "
              << f->halfedge()->vertex()->point() << " , " 
              << f->halfedge()->next()->vertex()->point() << " , "
              << f->halfedge()->next()->next()->vertex()->point()
              << " )" << std::endl;

    return EXIT_SUCCESS;
}

有什么想法吗?

最佳答案

使用 std::map::emplace() method, std::pair's piecewise constructorperfect forwarding , 就地构建不可复制和不可分配的数据:

myMap.emplace(
    std::piecewise_construct,  // disambiguation hint
    std::forward_as_tuple(someInt),  // perfectly forward arguments to key_type constructor
    std::forward_as_tuple(polyhedron.facets_begin(), polyhedron.facets_end(),
        polyhedron)); // perfectly forward arguments to value_type constructor

解决方案非常冗长,但完全符合 C++11 标准。

工作示例:Coliru Viewer

编辑: 使用 std::make_tuple而不是 std::forward_as_tuple如果您的编译器不支持它 ( #include <tuple> )。

编辑2:

查看添加的代码片段后,我发现错误不是由 map::emplace 触发的方法(可以通过注释掉它来轻松验证),但稍后使用 map::operator[] .在内部,map::operator[]使用 map::insert (检查它的源代码)。只需使用 map::at() 而不是解决问题。示例:

FT sqd = myMap.at(1).squared_distance(query);

请注意,map::at()如果找不到 key ,则抛出异常。您可能有兴趣(取决于性能需求)通过查看 map::find() 来确保 key 安全。在获取值(value)之前。

另请注意,由于 CGAL 内部使用了“不安全”字符串函数,您收到了详细警告。它们不是错误,可以通过添加 -D_SCL_SECURE_NO_WARNINGS 来禁用。编译器标志。

希望对您有所帮助。编码愉快!

关于c++ - 无法在 CGAL 中创建 AABB 树类型的 STL 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22385624/

相关文章:

docker - 如何在不能使用 `systemctl` 和 `service` 的 centos 容器中运行守护进程?

C++ 映射、 vector 、带引用的对

data-structures - 对于线段数据库,查找与矩形相交的所有线段

c++ - tail->下一个打印地址代替数据循环链表c++

c++ - yaml-cpp 节点诊断有意义的错误

c++ - 使用自定义顺序遍历 boost multi_index

algorithm - 给定平面上的一组点,找到包含它们的最小面积的(不一定是凸的)多边形

algorithm - 在计划中找到最近的交点

c++ - eclipse for c++编译错误

具有成员函数的 C++ 结构与具有公共(public)变量的类