diff --git a/Point_set_processing_3/test/Point_set_processing_3/test_read_write_point_set.cpp b/Point_set_processing_3/test/Point_set_processing_3/test_read_write_point_set.cpp index d176b88491a..d1f4514c014 100644 --- a/Point_set_processing_3/test/Point_set_processing_3/test_read_write_point_set.cpp +++ b/Point_set_processing_3/test/Point_set_processing_3/test_read_write_point_set.cpp @@ -12,22 +12,79 @@ typedef CGAL::Simple_cartesian Kernel; typedef Kernel::Point_3 Point_3; typedef Kernel::Vector_3 Vector_3; -typedef std::pair PointVectorPair; -bool write_ps(std::string s) + +const double epsilon=5e-4; +template < typename Type > +bool approx_equal_nt(const Type &t1, const Type &t2) { - std::vector points; - points.push_back(Point_3(0,0,0)); - points.push_back(Point_3(1,0,0)); - points.push_back(Point_3(0,1,0)); - points.push_back(Point_3(0,0,1)); - points.push_back(Point_3(1,1,1)); + if (t1 == t2) + return true; + if (CGAL::abs(t1 - t2) / (CGAL::max)(CGAL::abs(t1), CGAL::abs(t2)) < std::abs(t1)*epsilon) + return true; - return CGAL::write_points(s, points); + std::cout << " Approximate comparison failed between : " << t1 << " and " << t2 << std::endl; + std::cout<<"abs(t1 - t2) = "< PointVectorPair; +bool ps_are_equal(const CGAL::Point_set_3& ps, + const CGAL::Point_set_3& ps2) +{ + if(ps.size() != ps2.size()) + return false; + typedef CGAL::Point_set_3::const_iterator Iterator; + for(Iterator it1 = ps.begin(), it2 = ps2.begin(); + it1!=ps.end(), it2!=ps2.end(); ++it1, ++it2) + { + Point_3 p1 = ps.point(*it1); + Point_3 p2 = ps2.point(*it2); + if(!(approx_equal_nt(p1.x(), p2.x()) + && approx_equal_nt(p1.y(), p2.y()) + && approx_equal_nt(p1.z(), p2.z()))) + { + return false; + } + } + + return true; +} + + +typedef std::pair PointVectorPair; +bool points_are_equal(const std::vector& ps, + const std::vector& ps2) +{ + if(ps.size() != ps2.size()) + return false; + typedef std::vector::const_iterator Iterator; + for(Iterator it1 = ps.begin(), it2 = ps2.begin(); + it1!=ps.end(), it2!=ps2.end(); ++it1, ++it2) + { + Point_3 p1 = *it1; + Point_3 p2 = *it2; + if(!(approx_equal_nt(p1.x(), p2.x()) + && approx_equal_nt(p1.y(), p2.y()) + && approx_equal_nt(p1.z(), p2.z()))) + { + return false; + } + } + + return true; +} + + +bool test_points_with_np(std::string s) { std::vector points; points.push_back(std::make_pair(Point_3(0,0,0), Vector_3(0,0,0))); @@ -36,83 +93,112 @@ bool write_ps_with_np(std::string s) points.push_back(std::make_pair(Point_3(0,0,1), Vector_3(0,0,1))); points.push_back(std::make_pair(Point_3(1,1,1), Vector_3(1,1,1))); - return CGAL::write_points(s, points, - CGAL::parameters::point_map(CGAL::First_of_pair_property_map()). - normal_map(CGAL::Second_of_pair_property_map())); -} - -bool write_point_set(std::string s) -{ - CGAL::Point_set_3 point_set; - point_set.insert (Point_3(0,0,0)); - point_set.insert (Point_3(1,0,0)); - point_set.insert (Point_3(0,1,0)); - point_set.insert (Point_3(0,0,1)); - point_set.insert (Point_3(1,1,1)); - - point_set.add_normal_map(); - return CGAL::write_point_set(s, point_set); -} - -bool read_point_set(std::string s) -{ - CGAL::Point_set_3 ps; - if(!CGAL::read_point_set(s, ps)) - return false; - if(ps.size() != 5) - { - std::cerr <<"error: wrong number of points read."< points; - if(!CGAL::read_points(s, std::back_inserter(points))) - return false; - if(points.size() != 5) - { - std::cerr <<"error: wrong number of points read."<()). + normal_map(CGAL::Second_of_pair_property_map())); + assert(ok); std::vector pv_pairs; - if(!CGAL::read_points(s, - std::back_inserter(pv_pairs), - CGAL::parameters::point_map(CGAL::First_of_pair_property_map()). - normal_map(CGAL::Second_of_pair_property_map()))) - return false; - if(pv_pairs.size() != 5) - { - std::cerr <<"error: wrong number of points read."<()). + normal_map(CGAL::Second_of_pair_property_map())); + assert(ok); + assert(pv_pairs[0] == std::make_pair(Point_3(0,0,0), Vector_3(0,0,0))); + assert(pv_pairs[1] == std::make_pair(Point_3(1,0,0), Vector_3(1,0,0))); + assert(pv_pairs[2] == std::make_pair(Point_3(0,1,0), Vector_3(0,1,0))); + assert(pv_pairs[3] == std::make_pair(Point_3(0,0,1), Vector_3(0,0,1))); + assert(pv_pairs[4] == std::make_pair(Point_3(1,1,1), Vector_3(1,1,1))); } +#define CGAL_DEF_TEST_PS_FUNCTION(TYPE, type)\ +void test_##TYPE(std::string s) \ +{ \ + CGAL::Point_set_3 ps; \ + bool ok = CGAL::read_##TYPE(s, ps); \ + assert(ok); \ + const char* ext= type; \ + std::string fname = "tmp."; \ + fname.append(ext); \ + ok = CGAL::write_##TYPE(fname, ps); \ + assert(ok); \ + CGAL::Point_set_3 ps2; \ + std::ifstream is(fname); \ + ok = CGAL::read_##TYPE(is, ps2); \ + assert(ok); \ + assert(ps_are_equal(ps, ps2)); \ + ok = CGAL::write_point_set(fname, ps2); \ + assert(ok); \ + ps2.clear(); \ + ok = CGAL::read_point_set(fname, ps2); \ + assert(ok); \ + assert(ps_are_equal(ps, ps2)); \ +} + +CGAL_DEF_TEST_PS_FUNCTION(XYZ, "xyz") +CGAL_DEF_TEST_PS_FUNCTION(OFF,"off") +CGAL_DEF_TEST_PS_FUNCTION(PLY,"ply") +#ifdef CGAL_LINKED_WITH_LASLIB +CGAL_DEF_TEST_PS_FUNCTION(LAS,"las") +#endif +#undef CGAL_DEF_INITIALIZE_ID_FUCNTION + +#define CGAL_DEF_TEST_POINTS_FUNCTION(TYPE, type) \ +void test_points_##TYPE(std::string s) \ +{ \ + std::vector ps; \ + bool ok = CGAL::read_##TYPE(s, std::back_inserter(ps)); \ + assert(ok); \ + const char* ext= type; \ + std::string fname = "tmp."; \ + fname.append(ext); \ + ok = CGAL::write_##TYPE(fname, ps); \ + assert(ok); \ + std::vector ps2; \ + std::ifstream is(fname); \ + ok = CGAL::read_##TYPE(is, std::back_inserter(ps2)); \ + assert(ok); \ + assert(points_are_equal(ps, ps2)); \ + ok = CGAL::write_points(fname, ps2); \ + assert(ok); \ + ps2.clear(); \ + ok = CGAL::read_points(fname, std::back_inserter(ps2)); \ + assert(ok); \ + assert(points_are_equal(ps, ps2)); \ +} + +CGAL_DEF_TEST_POINTS_FUNCTION(XYZ, "xyz") +CGAL_DEF_TEST_POINTS_FUNCTION(OFF,"off") +CGAL_DEF_TEST_POINTS_FUNCTION(PLY,"ply") +#ifdef CGAL_LINKED_WITH_LASLIB +CGAL_DEF_TEST_POINTS_FUNCTION(LAS,"las") +#endif +#undef CGAL_DEF_INITIALIZE_ID_FUCNTION + + + int main() { - std::vector filenames = {"test.xyz" , - "test.off" , - "test.ply"}; -#ifdef CGAL_LINKED_WITH_LASLIB - filenames.push_back("test.las"); -#endif - for(const std::string filename : filenames) - { - assert(write_ps(filename)); - assert(read_ps(filename)); - assert(write_ps_with_np(filename)); - assert(read_ps_with_np(filename)); - assert(write_point_set(filename)); - assert(read_point_set(filename)); - } + test_XYZ("data/read_test/ok_2.xyz"); + test_OFF("data/read_test/ok_1.off"); + test_PLY("data/read_test/simple_ascii.ply"); + test_PLY("data/read_test/simple.ply"); + + test_points_XYZ("data/read_test/ok_2.xyz"); + test_points_OFF("data/read_test/ok_1.off"); + test_points_PLY("data/read_test/simple_ascii.ply"); + test_points_PLY("data/read_test/simple.ply"); + + + #ifdef CGAL_LINKED_WITH_LASLIB + test_LAS("data/read_test/pig_points.las"); + test_points_LAS("data/read_test/pig_points.las"); + #endif + + test_points_with_np("test.xyz"); + test_points_with_np("test.off"); + test_points_with_np("test.ply"); + + //don't test normals with LAS as it is not supported by the format return 0; }