草庐IT

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

coder 2024-02-24 原文

我正在尝试创建类型为 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/

有关c++ - 无法在 CGAL 中创建 AABB 树类型的 STL 容器的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  3. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

  8. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  9. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  10. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

随机推荐