add tests for point_sets

This commit is contained in:
Maxime Gimeno 2020-06-10 15:37:30 +02:00
parent b6d4a9a372
commit 23a7a12cd6
1 changed files with 168 additions and 82 deletions

View File

@ -12,22 +12,79 @@
typedef CGAL::Simple_cartesian<double> Kernel; typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point_3; typedef Kernel::Point_3 Point_3;
typedef Kernel::Vector_3 Vector_3; typedef Kernel::Vector_3 Vector_3;
typedef std::pair<Point_3, Vector_3> 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<Point_3> points; if (t1 == t2)
points.push_back(Point_3(0,0,0)); return true;
points.push_back(Point_3(1,0,0)); if (CGAL::abs(t1 - t2) / (CGAL::max)(CGAL::abs(t1), CGAL::abs(t2)) < std::abs(t1)*epsilon)
points.push_back(Point_3(0,1,0)); return true;
points.push_back(Point_3(0,0,1));
points.push_back(Point_3(1,1,1));
return CGAL::write_points(s, points); std::cout << " Approximate comparison failed between : " << t1 << " and " << t2 << std::endl;
std::cout<<"abs(t1 - t2) = "<<CGAL::abs(t1 - t2)<<"n";
std::cout<<"abs(t1) = "<<CGAL::abs(t1)<<"n";
std::cout<<"abs(t2) = "<<CGAL::abs(t2)<<"n";
std::cout<<"std::abs(t1)*epsilon = "<<std::abs(t1)*epsilon<<"n";
std::cout<<"CGAL::abs(t1 - t2) / (CGAL::max)(CGAL::abs(t1), CGAL::abs(t2) == "<<
CGAL::abs(t1 - t2) / (CGAL::max)(CGAL::abs(t1), CGAL::abs(t2))<<std::endl;
return false;
} }
bool write_ps_with_np(std::string s)
typedef std::pair<Point_3, Vector_3> PointVectorPair;
bool ps_are_equal(const CGAL::Point_set_3<Point_3, Vector_3>& ps,
const CGAL::Point_set_3<Point_3, Vector_3>& ps2)
{
if(ps.size() != ps2.size())
return false;
typedef CGAL::Point_set_3<Point_3, Vector_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<Point_3, Vector_3> PointVectorPair;
bool points_are_equal(const std::vector<Point_3>& ps,
const std::vector<Point_3>& ps2)
{
if(ps.size() != ps2.size())
return false;
typedef std::vector<Point_3>::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<PointVectorPair> points; std::vector<PointVectorPair> points;
points.push_back(std::make_pair(Point_3(0,0,0), Vector_3(0,0,0))); 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(0,0,1), Vector_3(0,0,1)));
points.push_back(std::make_pair(Point_3(1,1,1), Vector_3(1,1,1))); points.push_back(std::make_pair(Point_3(1,1,1), Vector_3(1,1,1)));
return CGAL::write_points(s, points, bool ok = CGAL::write_points(s, points,
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()). CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()).
normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>())); normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()));
} assert(ok);
bool write_point_set(std::string s)
{
CGAL::Point_set_3<Point_3, Vector_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<Point_3, Vector_3> ps;
if(!CGAL::read_point_set(s, ps))
return false;
if(ps.size() != 5)
{
std::cerr <<"error: wrong number of points read."<<std::endl;
return false;
}
return true;
}
bool read_ps(std::string s)
{
std::vector<Point_3> points;
if(!CGAL::read_points(s, std::back_inserter(points)))
return false;
if(points.size() != 5)
{
std::cerr <<"error: wrong number of points read."<<std::endl;
return false;
}
return true;
}
bool read_ps_with_np(std::string s)
{
std::vector<PointVectorPair> pv_pairs; std::vector<PointVectorPair> pv_pairs;
if(!CGAL::read_points(s, ok = CGAL::read_points(s,
std::back_inserter(pv_pairs), std::back_inserter(pv_pairs),
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()). CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()).
normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()))) normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()));
return false; assert(ok);
if(pv_pairs.size() != 5) 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)));
std::cerr <<"error: wrong number of points read."<<std::endl; assert(pv_pairs[2] == std::make_pair(Point_3(0,1,0), Vector_3(0,1,0)));
return false; 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)));
} }
return true;
#define CGAL_DEF_TEST_PS_FUNCTION(TYPE, type)\
void test_##TYPE(std::string s) \
{ \
CGAL::Point_set_3<Point_3, Vector_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<Point_3, Vector_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<Point_3> 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<Point_3> 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() int main()
{ {
std::vector<std::string> filenames = {"test.xyz" , test_XYZ("data/read_test/ok_2.xyz");
"test.off" , test_OFF("data/read_test/ok_1.off");
"test.ply"}; 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 #ifdef CGAL_LINKED_WITH_LASLIB
filenames.push_back("test.las"); test_LAS("data/read_test/pig_points.las");
test_points_LAS("data/read_test/pig_points.las");
#endif #endif
for(const std::string filename : filenames)
{ test_points_with_np("test.xyz");
assert(write_ps(filename)); test_points_with_np("test.off");
assert(read_ps(filename)); test_points_with_np("test.ply");
assert(write_ps_with_np(filename));
assert(read_ps_with_np(filename)); //don't test normals with LAS as it is not supported by the format
assert(write_point_set(filename));
assert(read_point_set(filename));
}
return 0; return 0;
} }