我发现了一个现象,CGAL AABB树的构造函数对于std::vector
非常慢。
这是一个最小的测试代码:
Point a(1.0, 0.0, 0.0);
Point b(0.0, 1.0, 0.0);
Point c(0.0, 0.0, 1.0);
std::vector<Triangle> triangles;
for (size_t I=0;I<245000;I++)
{
triangles.push_back(Triangle(a,b,c));
}
// constructs AABB tree
MyTree tree(triangles.begin(),triangles.end());
245000原始,未构建,运行速度慢得多。我找到了来源:
template<typename Tr>
template<typename ConstPrimitiveIterator, typename ... T>
void AABB_tree<Tr>::insert(ConstPrimitiveIterator first,
ConstPrimitiveIterator beyond,
T ... t)
{
set_shared_data(t...);
while(first != beyond)
{
m_primitives.push_back(Primitive(first,t...));
++first;
}
m_need_build = true;
}
我怀疑vector
push_back
函数会重新分配内存,但我不确定是什么原因,所以我尝试了这段代码:
template<typename Tr>
template<typename ConstPrimitiveIterator, typename ... T>
void AABB_tree<Tr>::insert(ConstPrimitiveIterator first,
ConstPrimitiveIterator beyond,
T ... t)
{
set_shared_data(t...);
m_primitives.assign(beyond-first,first);//pre allcate promitives
size_t Index = 0;
while(first != beyond)
{
m_primitives[Index] = Primitive(first,t...);
++first;
++Index;
}
m_need_build = true;
}
问题解决了!我需要更多的建议,我认为这需要更新到CGAL源?
================== 14-9-18更新=============== 我做了一个简单的测试:
#include <iostream>
#include <list>
#include "boost/timer/timer.hpp"
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_triangle_primitive.h>
#include <CGAL/Polyhedron_3.h>
typedef CGAL::Simple_cartesian<double> K;
typedef K::FT FT;
typedef K::Ray_3 Ray;
typedef K::Line_3 Line;
typedef K::Point_3 Point;
typedef K::Triangle_3 Triangle;
typedef std::vector<Triangle>::iterator Iterator;
typedef CGAL::AABB_triangle_primitive<K, Iterator> Primitive;
typedef CGAL::AABB_traits<K, Primitive> AABB_triangle_traits;
typedef CGAL::AABB_tree<AABB_triangle_traits> Tree;
int main()
{
Point a(1.0, 0.0, 0.0);
Point b(0.0, 1.0, 0.0);
Point c(0.0, 0.0, 1.0);
std::vector<Triangle> triangles;
for (size_t I=0;I<245000;I++)
{
triangles.push_back(Triangle(a,b,c));
}
#define TIMER_SECOND boost::timer::nanosecond_type(1000000000LL)
auto first = triangles.begin();
auto beyond = triangles.end();
boost::timer::cpu_timer CountTimer;
//Test 1
std::vector<Primitive> TestV1;
CountTimer.start();
while(first != beyond)
{
TestV1.push_back(Primitive(first));
++first;
}
std::cout << "Test1 Time:" << CountTimer.elapsed().wall << std::endl;
//Test 2
first = triangles.begin();
beyond = triangles.end();
std::vector<Primitive> TestV2;
CountTimer.start();
TestV2.reserve(std::distance(first,beyond));
while(first != beyond)
{
TestV2.push_back(Primitive(first));
++first;
}
std::cout << "Test2 Time:" << CountTimer.elapsed().wall << std::endl;
system("pause");
return EXIT_SUCCESS;
}
在发布模式下:
测试1时间:3426459
测试2时间:926136
在调试模式下:
测试1时间:503719044580
测试2时间:224280791