Skip to content

Commit

Permalink
add tests for autoref
Browse files Browse the repository at this point in the history
  • Loading branch information
sloriot committed Dec 12, 2023
1 parent d0c224f commit 07acb78
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include <tbb/concurrent_vector.h>
#if TBB_INTERFACE_VERSION < 12010 && !defined(TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS)
#define CGAL_HAS_DEFINED_TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS
#define TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS
#define TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS 1
#endif
#include <tbb/concurrent_map.h>
#include <tbb/parallel_for.h>
Expand Down Expand Up @@ -857,7 +857,16 @@ void autorefine_triangle_soup(PointRange& soup_points,
CGAL_PMP_AUTOREFINE_VERBOSE("collect intersecting pairs");
triangle_soup_self_intersections<Concurrency_tag>(soup_points, soup_triangles, std::back_inserter(si_pairs), np);

if (si_pairs.empty()) return;
if (si_pairs.empty())
{
if constexpr (!std::is_same_v<Autorefinement::Default_visitor, Visitor>)
{
visitor.number_of_output_triangles(soup_triangles.size());
for(std::size_t i=0; i<soup_triangles.size(); ++i)
visitor.verbatim_triangle_copy(i, i);
}
return;
}

// mark degenerate faces so that we can ignore them
std::vector<bool> is_degen(soup_triangles.size(), false);
Expand Down Expand Up @@ -1125,7 +1134,16 @@ void autorefine_triangle_soup(PointRange& soup_points,
TriangleRange soup_triangles_out;
soup_triangles_out.reserve(soup_triangles.size());

visitor.number_of_output_triangles(soup_triangles.size()+new_triangles.size());
if constexpr (!std::is_same_v<Autorefinement::Default_visitor, Visitor>)
{
std::size_t nbt=0;
for (Input_TID f=0; f<soup_triangles.size(); ++f)
{
int tiid = tri_inter_ids[f];
if (tiid == -1) ++nbt;
}
visitor.number_of_output_triangles(nbt+new_triangles.size());
}

// raw copy of input triangles with no intersection
std::vector<std::size_t> tri_inter_ids_inverse(triangles.size());
Expand All @@ -1136,7 +1154,7 @@ void autorefine_triangle_soup(PointRange& soup_points,
int tiid = tri_inter_ids[f];
if (tiid == -1)
{
visitor.verbatim_triangle_copy(soup_triangles.size(), f);
visitor.verbatim_triangle_copy(soup_triangles_out.size(), f);
soup_triangles_out.push_back(
{soup_triangles[f][0], soup_triangles[f][1], soup_triangles[f][2]}
);
Expand Down Expand Up @@ -1200,7 +1218,7 @@ void autorefine_triangle_soup(PointRange& soup_points,

soup_triangles_out.resize(offset + new_triangles.size());
//use map iterator triple for triangles to create them concurrently and safely
std::vector<std::array<tbb::concurrent_map<EK::Point_3, std::size_t>::iterator, 3>> triangle_buffer(new_triangles.size());
std::vector<std::array<typename Point_id_map::iterator, 3>> triangle_buffer(new_triangles.size());
tbb::parallel_for(tbb::blocked_range<size_t>(0, new_triangles.size()),
[&](const tbb::blocked_range<size_t>& r) {
for (size_t ti = r.begin(); ti != r.end(); ++ti) {
Expand Down Expand Up @@ -1238,10 +1256,8 @@ void autorefine_triangle_soup(PointRange& soup_points,
std::vector<std::array<typename Point_id_map::iterator, 3>> triangle_buffer(new_triangles.size());
tbb::parallel_for(tbb::blocked_range<size_t>(0, new_triangles.size()),
[&](const tbb::blocked_range<size_t>& r) {
for (size_t ti = r.begin(); ti != r.end(); ++ti) {
if (offset + ti > soup_triangles_out.size()) {
std::cout << "ti = " << ti << std::endl;
}
for (size_t ti = r.begin(); ti != r.end(); ++ti)
{
const std::array<EK::Point_3, 3>& t = new_triangles[ti].first;
visitor.new_subtriangle(offset+ti, tri_inter_ids_inverse[new_triangles[ti].second]);
triangle_buffer[ti] = CGAL::make_array(concurrent_get_point_id(t[0]), concurrent_get_point_id(t[1]), concurrent_get_point_id(t[2]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ if(TARGET CGAL::TBB_support)
target_link_libraries(test_pmp_distance PUBLIC CGAL::TBB_support)
target_link_libraries(orient_polygon_soup_test PUBLIC CGAL::TBB_support)
target_link_libraries(self_intersection_surface_mesh_test PUBLIC CGAL::TBB_support)
target_link_libraries(test_autorefinement PUBLIC CGAL::TBB_support)
else()
message(STATUS "NOTICE: Intel TBB was not found. Tests will use sequential code.")
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
data-autoref/test_01.off 0 0 4 1 4 0
data-autoref/test_02.off 1 5 13 1 5 0
data-autoref/test_03.off 4 8 18 0 18 0
data-autoref/test_04.off 1 5 17 0 17 0
data-autoref/test_05.off 1 5 17 0 17 0
data-autoref/test_06.off 3 55 141 1 76 0
data-autoref/test_07.off 1 4 10 1 4 0
data-autoref/test_08.off 1 31 87 1 52 0
data-autoref/test_09.off 1 4 5 1 4 0
data-autoref/test_10.off 1 3 13 0 13 0
data-autoref/test_11.off 1 3 12 0 12 0
data-autoref/test_12.off 2 2 11 1 11 0
data-autoref/test_13.off 1 5 16 1 8 0
data-autoref/test_14.off 1 5 16 1 12 0
data-autoref/test_15.off 3 8 16 1 12 0
data-autoref/test_16.off 1 2 6 1 4 0
data-autoref/test_17.off 1 2 6 1 4 0
data-autoref/triple_inter_exception/triple.off 0 0 0 0 0 1
data-autoref/triple_inter_exception/cubes_cpln_1.off 0 0 0 0 0 1
data-autoref/triple_inter_exception/cubes_cpln_2.off 0 0 0 0 0 1
data-autoref/triple_inter_exception/cubes_cpln_3.off 0 0 0 0 0 1
data-autoref/open_01.off 1 65 1377 1 1313 0
data-autoref/open_02.off 1 33 595 1 562 0
data-autoref/cpln_01.off 18 42 48 1 30 0
data-autoref/cpln_02.off 28 56 40 1 24 0
data-autoref/cpln_03.off 15 35 42 1 27 0
data-autoref/four_cubes.off 12 94 184 1 78 0
data-autoref/spiral.off 7 14 26 0 26 0
data-autoref/test_01.off 0 0 4 1 4 0 4 2
data-autoref/test_02.off 1 5 13 1 5 0 10 17
data-autoref/test_03.off 4 8 18 0 18 0 14 20
data-autoref/test_04.off 1 5 17 0 17 0 13 20
data-autoref/test_05.off 1 5 17 0 17 0 13 20
data-autoref/test_06.off 3 55 141 1 76 0 92 248
data-autoref/test_07.off 1 4 10 1 4 0 8 12
data-autoref/test_08.off 1 31 87 1 52 0 57 148
data-autoref/test_09.off 1 4 5 1 4 0 4 4
data-autoref/test_10.off 1 3 13 0 13 0 10 13
data-autoref/test_11.off 1 3 12 0 12 0 9 12
data-autoref/test_12.off 2 2 11 1 11 0 9 6
data-autoref/test_13.off 1 5 16 1 8 0 16 22
data-autoref/test_14.off 1 5 16 1 12 0 16 22
data-autoref/test_15.off 3 8 16 1 12 0 16 24
data-autoref/test_16.off 1 2 6 1 4 0 6 2
data-autoref/test_17.off 1 2 6 1 4 0 6 2
data-autoref/triple_inter_exception/triple.off 0 0 0 0 0 1 15 18
data-autoref/triple_inter_exception/cubes_cpln_1.off 0 0 0 0 0 1 66 224
data-autoref/triple_inter_exception/cubes_cpln_2.off 0 0 0 0 0 1 54 196
data-autoref/triple_inter_exception/cubes_cpln_3.off 0 0 0 0 0 1 61 204
data-autoref/open_01.off 1 65 1377 1 1313 0 1317 2622
data-autoref/open_02.off 1 33 595 1 562 0 565 1056
data-autoref/cpln_01.off 18 42 48 1 30 0 30 88
data-autoref/cpln_02.off 28 56 40 1 24 0 24 72
data-autoref/cpln_03.off 15 35 42 1 27 0 27 76
data-autoref/four_cubes.off 12 94 184 1 78 0 106 352
data-autoref/spiral.off 7 14 26 0 26 0 19 31
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <CGAL/Polygon_mesh_processing/intersection.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/Polygon_mesh_processing/autorefinement.h>
#include <CGAL/Polygon_mesh_processing/self_intersections.h>

#include <CGAL/IO/polygon_soup_io.h>

#include <fstream>
#include <sstream>
Expand All @@ -13,23 +17,64 @@ typedef CGAL::Surface_mesh<K::Point_3> Mesh;
namespace PMP = CGAL::Polygon_mesh_processing;

template <class TriangleMesh>
struct My_visitor :
struct My_exp_visitor :
public CGAL::Polygon_mesh_processing::Corefinement::Default_visitor<TriangleMesh>
{
void after_subface_creations(TriangleMesh&){++(*i);}

My_visitor()
My_exp_visitor()
: i (new int(0) )
{}

std::shared_ptr<int> i;
};

void test(const char* fname, std::size_t nb_polylines, std::size_t total_nb_points,
std::size_t nb_vertices_after_autorefine, bool all_fixed, std::size_t nb_vertices_after_fix,
bool triple_intersection)
struct My_visitor
{
std::cout << "Running tests on " << fname << "\n";
My_visitor(std::size_t nb_input, std::size_t expected_nb_output)
: nb_input(nb_input)
, expected_nb_output(expected_nb_output)
{}

~My_visitor()
{
for(std::size_t i=0; i<tgt_check.size(); ++i)
{
assert( tgt_check[i]==1 );
}
}
void number_of_output_triangles(std::size_t nbt)
{
tgt_check.assign(expected_nb_output, 0);
assert(nbt==expected_nb_output);
}

void verbatim_triangle_copy(std::size_t tgt_id, std::size_t src_id)
{
assert(src_id<nb_input);
assert(tgt_id<expected_nb_output);
assert(tgt_check.size()==expected_nb_output && tgt_check[tgt_id]==0);
tgt_check[tgt_id]=1;
}

void new_subtriangle(std::size_t tgt_id, std::size_t src_id)
{
assert(src_id<nb_input);
assert(tgt_id<expected_nb_output);
assert(tgt_check.size()==expected_nb_output && tgt_check[tgt_id]==0);
tgt_check[tgt_id]=1;
}

std::size_t nb_input;
std::size_t expected_nb_output;
std::vector<int> tgt_check;
};

void test_coref_based(const char* fname, std::size_t nb_polylines, std::size_t total_nb_points,
std::size_t nb_vertices_after_autorefine, bool all_fixed, std::size_t nb_vertices_after_fix,
bool triple_intersection)
{
std::cout << "Running tests (coref based) on " << fname << "\n";
std::ifstream input(fname);

Mesh mesh;
Expand All @@ -41,7 +86,7 @@ void test(const char* fname, std::size_t nb_polylines, std::size_t total_nb_poin
input.close();
std::size_t nb_vertices_before_autorefine = num_vertices(mesh);

// Testing surface_self_intersection()
// Testing PMP::experimental::surface_self_intersection()
try{
std::vector< std::vector<K::Point_3> >polylines;
PMP::experimental::surface_self_intersection(mesh, std::back_inserter(polylines));
Expand All @@ -57,9 +102,9 @@ void test(const char* fname, std::size_t nb_polylines, std::size_t total_nb_poin
assert( triple_intersection );
}

// Testing autorefine()
// Testing PMP::experimental::autorefine()
try{
My_visitor<Mesh> visitor;
My_exp_visitor<Mesh> visitor;
PMP::experimental::autorefine(mesh,
CGAL::parameters::visitor(visitor));
mesh.collect_garbage();
Expand All @@ -72,7 +117,7 @@ void test(const char* fname, std::size_t nb_polylines, std::size_t total_nb_poin
assert( triple_intersection );
}

// Testing autorefine_and_remove_self_intersections()
// Testing PMP::experimental::autorefine_and_remove_self_intersections()
try{
input.open(fname);
mesh.clear();
Expand All @@ -89,10 +134,42 @@ void test(const char* fname, std::size_t nb_polylines, std::size_t total_nb_poin
}
}

template <class Tag>
void test(const char* fname, std::size_t nb_vertices_after_autorefine, std::size_t expected_nb_output, Tag tag)
{
std::cout << "Running tests on " << fname;
if (std::is_same_v<Tag, CGAL::Sequential_tag>)
std::cout << " (Sequential)\n";
else
std::cout << " (Parallel)\n";

std::vector<K::Point_3> points;
std::vector< std::vector<std::size_t> > triangles;
if (!CGAL::IO::read_polygon_soup(fname, points, triangles))
{
std::cerr << " Input mesh is not a valid file." << std::endl;
exit(EXIT_FAILURE);
}

// Testing autorefine()
My_visitor visitor(triangles.size(), expected_nb_output);
PMP::autorefine_triangle_soup(points, triangles, CGAL::parameters::visitor(visitor).concurrency_tag(tag));
assert( nb_vertices_after_autorefine==points.size());
assert( expected_nb_output==triangles.size());
assert( !PMP::does_triangle_soup_self_intersect(points, triangles) );
// CGAL::IO::write_polygon_soup("/tmp/debug.off", points, triangles);
}

int main(int argc, const char** argv)
{
// file nb_polylines total_nb_points nb_vertices_after_autorefine all_fixed nb_vertices_after_fix triple_intersection
for (int i=0;i<(argc-1)/7; ++i)
test(argv[1+7*i], atoi(argv[1+7*i+1]), atoi(argv[1+7*i+2]),
atoi(argv[1+7*i+3]), atoi(argv[1+7*i+4])==0?false:true, atoi(argv[1+7*i+5]), atoi(argv[1+7*i+6])==0?false:true);
for (int i=0;i<(argc-1)/9; ++i)
{
test_coref_based(argv[1+9*i], atoi(argv[1+9*i+1]), atoi(argv[1+9*i+2]),
atoi(argv[1+9*i+3]), atoi(argv[1+9*i+4])==0?false:true, atoi(argv[1+9*i+5]), atoi(argv[1+9*i+6])==0?false:true);
test(argv[1+9*i], atoi(argv[1+9*i+7]), atoi(argv[1+9*i+8]), CGAL::Sequential_tag());
#ifdef CGAL_LINKED_WITH_TBB
test(argv[1+9*i], atoi(argv[1+9*i+7]), atoi(argv[1+9*i+8]), CGAL::Parallel_tag());
#endif
}
}

0 comments on commit 07acb78

Please sign in to comment.