fixed benchmark by switching to Rigid_triangle_mesh_collision_detection

This commit is contained in:
Sven Oesau 2024-04-16 14:49:24 +02:00
parent 8e7b7da17c
commit 7dd2d0be13
1 changed files with 28 additions and 30 deletions

View File

@ -1,4 +1,4 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h> #include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/intersection.h> #include <CGAL/Polygon_mesh_processing/intersection.h>
@ -7,7 +7,7 @@
#include <CGAL/boost/graph/copy_face_graph.h> #include <CGAL/boost/graph/copy_face_graph.h>
#include <CGAL/AABB_tree.h> #include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits_3.h> #include <CGAL/AABB_traits_3.h>
#include <CGAL/Polygon_mesh_processing/internal/AABB_traversal_traits_with_transformation.h> #include <CGAL/Rigid_triangle_mesh_collision_detection.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h> #include <CGAL/AABB_face_graph_triangle_primitive.h>
#include <CGAL/Side_of_triangle_mesh.h> #include <CGAL/Side_of_triangle_mesh.h>
@ -19,12 +19,11 @@ typedef CGAL::Surface_mesh<K::Point_3> Surface_mesh;
typedef CGAL::AABB_face_graph_triangle_primitive<Surface_mesh> Primitive; typedef CGAL::AABB_face_graph_triangle_primitive<Surface_mesh> Primitive;
typedef CGAL::AABB_traits_3<K, Primitive> Traits; typedef CGAL::AABB_traits_3<K, Primitive> Traits;
//typedef CGAL::AABB_do_intersect_transform_traits<K, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree; typedef CGAL::AABB_tree<Traits> Tree;
namespace PMP = CGAL::Polygon_mesh_processing; namespace PMP = CGAL::Polygon_mesh_processing;
void naive_test(int k, const char* fname, void naive_test(int k, const std::string& fname,
int& nb_inter, int& nb_no_inter, int& nb_include) int& nb_inter, int& nb_no_inter, int& nb_include)
{ {
std::ifstream input(fname); std::ifstream input(fname);
@ -34,7 +33,7 @@ void naive_test(int k, const char* fname,
CGAL::Aff_transformation_3<K> init1(CGAL::SCALING, 6.0); CGAL::Aff_transformation_3<K> init1(CGAL::SCALING, 6.0);
PMP::transform(init1, tm); PMP::transform(init1, tm);
CGAL::Bbox_3 box = PMP::bbox(tm); CGAL::Bbox_3 box = PMP::bbox(tm);
typedef CGAL::AABB_tree<Traits> Tree;
Tree tmTree(tm.faces_begin(), tm.faces_end(), tm); Tree tmTree(tm.faces_begin(), tm.faces_end(), tm);
Tree tmTree2(tm2.faces_begin(), tm2.faces_end(), tm2); Tree tmTree2(tm2.faces_begin(), tm2.faces_end(), tm2);
CGAL::Aff_transformation_3<K> init2(CGAL::TRANSLATION, - K::Vector_3( CGAL::Aff_transformation_3<K> init2(CGAL::TRANSLATION, - K::Vector_3(
@ -87,7 +86,8 @@ void naive_test(int k, const char* fname,
T0 = CGAL::Aff_transformation_3<K>(CGAL::TRANSLATION, -i*unit_vec); T0 = CGAL::Aff_transformation_3<K>(CGAL::TRANSLATION, -i*unit_vec);
} }
} }
void test_no_collision(int k, const char* fname,
void test_no_collision(int k, const std::string &fname,
int& nb_inter, int& nb_no_inter, int& nb_include) int& nb_inter, int& nb_no_inter, int& nb_include)
{ {
std::ifstream input(fname); std::ifstream input(fname);
@ -98,9 +98,11 @@ void test_no_collision(int k, const char* fname,
PMP::transform(init1, tm); PMP::transform(init1, tm);
CGAL::Bbox_3 box = PMP::bbox(tm); CGAL::Bbox_3 box = PMP::bbox(tm);
Tree tmTree(tm.faces_begin(), tm.faces_end(), tm); Tree tmTree(tm.faces_begin(), tm.faces_end(), tm);
Tree tmTree2(tm2.faces_begin(), tm2.faces_end(), tm2); Tree tmTree2(tm2.faces_begin(), tm2.faces_end(), tm2);
CGAL::Aff_transformation_3<K> init2(CGAL::TRANSLATION, - K::Vector_3( CGAL::Aff_transformation_3<K> init2(CGAL::TRANSLATION, - K::Vector_3(
(box.xmax()-box.xmin()),0,0)); (box.xmax()-box.xmin()),0,0));
PMP::transform(init2, tm2); PMP::transform(init2, tm2);
tmTree.build(); tmTree.build();
@ -114,6 +116,12 @@ void test_no_collision(int k, const char* fname,
CGAL::Side_of_triangle_mesh<Surface_mesh, K, CGAL::Side_of_triangle_mesh<Surface_mesh, K,
VPM, Tree> sotm1(tmTree); VPM, Tree> sotm1(tmTree);
CGAL::Rigid_triangle_mesh_collision_detection<Surface_mesh> collision_detection;
collision_detection.add_mesh(tm);
collision_detection.add_mesh(tm2);
for(int i=1; i<k+1; ++i) for(int i=1; i<k+1; ++i)
{ {
K::FT rot[9]; K::FT rot[9];
@ -131,34 +139,24 @@ void test_no_collision(int k, const char* fname,
rot[6], rot[7], rot[8]); rot[6], rot[7], rot[8]);
CGAL::Aff_transformation_3<K> T1 = CGAL::Aff_transformation_3<K>(CGAL::TRANSLATION, i*unit_vec); CGAL::Aff_transformation_3<K> T1 = CGAL::Aff_transformation_3<K>(CGAL::TRANSLATION, i*unit_vec);
CGAL::Aff_transformation_3<K> transfo = R*T1; CGAL::Aff_transformation_3<K> transfo = R*T1;
tmTree2.traits().set_transformation(transfo);
CGAL::Interval_nt_advanced::Protector protector; collision_detection.set_transformation(1, transfo);
if(tmTree2.do_intersect(tmTree))
std::vector< std::pair<std::size_t, bool> > res = collision_detection.get_all_intersections_and_inclusions(0);
if (res.empty())
nb_no_inter++;
else if(!res[0].second)
++nb_inter; ++nb_inter;
else else
{ ++nb_include;
if(sotm1(transfo.transform(vpm2[*tm2.vertices().begin()])) != CGAL::ON_UNBOUNDED_SIDE)
{
++nb_include;
}
else
{
CGAL::Side_of_triangle_mesh<Surface_mesh, K,
VPM, Tree> sotm2(tmTree2);
if(sotm2(tm.point(*tm.vertices().begin())) != CGAL::ON_UNBOUNDED_SIDE)
++nb_include;
else
++nb_no_inter;
}
}
} }
} }
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
int k = (argc>1) ? atoi(argv[1]) : 10; int k = (argc>1) ? atoi(argv[1]) : 20;
const char* path = (argc>2)?argv[2]:"data/handle" std::string path = (argc>2)?argv[2]: CGAL::data_file_path("meshes/handle.off");
".off";
std::cout<< k<<" steps in "<<path<<std::endl; std::cout<< k<<" steps in "<<path<<std::endl;
int nb_inter(0), nb_no_inter(0), nb_include(0), int nb_inter(0), nb_no_inter(0), nb_include(0),
@ -166,12 +164,12 @@ int main(int argc, const char** argv)
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
naive_test(k, path, naive_inter, naive_no_inter, naive_include); naive_test(k, path, naive_inter, naive_no_inter, naive_include);
auto end = std::chrono::steady_clock::now(); auto end = std::chrono::steady_clock::now();
std::cout<<"Naive test :"<<naive_inter<<" collisions, "<<naive_include<<" inclusions, "<<naive_no_inter<<" no collision, calculated in " std::cout<<"Naive test : "<<naive_inter<<" collisions, "<<naive_include<<" inclusions, "<<naive_no_inter<<" no collision, calculated in "
<<std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "μs." << std::endl; <<std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms." << std::endl;
start = std::chrono::steady_clock::now(); start = std::chrono::steady_clock::now();
test_no_collision(k, path,nb_inter, nb_no_inter, nb_include); test_no_collision(k, path,nb_inter, nb_no_inter, nb_include);
end = std::chrono::steady_clock::now(); end = std::chrono::steady_clock::now();
std::cout<<"With transform_traits: "<<nb_inter<<" collisions, "<<nb_include<<" inclusions, "<<nb_no_inter<<" no collision, calculated in " std::cout<<"With transform_traits: "<<nb_inter<<" collisions, "<<nb_include<<" inclusions, "<<nb_no_inter<<" no collision, calculated in "
<<std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "μs." << std::endl; <<std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms." << std::endl;
return 0; return 0;
} }