Addition of the class, example and test for Random_points_in_triangle_2

This commit is contained in:
Maxime Gimeno 2016-07-20 10:34:06 +02:00 committed by Sébastien Loriot
parent 79fc76c738
commit 472f9beffb
4 changed files with 135 additions and 25 deletions

View File

@ -0,0 +1,36 @@
#include <CGAL/Simple_cartesian.h>
#include <CGAL/point_generators_2.h>
#include <iostream>
#include <fstream>
using namespace CGAL;
typedef Simple_cartesian<double> K;
typedef K::Point_2 Point;
int main()
{
// Generated points are in that vector
std::vector<Point> points;
// Create input triangles
std::vector<K::Triangle_2> triangles;
for(int i=0; i< 5; ++i)
{
triangles.push_back(K::Triangle_2(Point(i,0), Point(i+1,0), Point(i+0.5,1)));
}
// Create the generator, input is the vector of Triangle_2
Random_points_in_triangles_2<Point> g(triangles);
// Get 100 random points in cdt
CGAL::cpp11::copy_n(g, 1000, std::back_inserter(points));
// Check that we have really created 100 points.
assert( points.size() == 1000);
// print the first point that was generated
std::cout << points[0] << std::endl;
return 0;
}

View File

@ -1,4 +1,4 @@
// Copyright (c) 1997
// Copyright (c) 1997
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
@ -595,6 +595,66 @@ public:
}
};
namespace internal
{
template<class T>
class Deref
{
public:
typedef typename std::iterator_traits<T>::value_type result_type;
typename std::iterator_traits<T>::value_type operator()(T triangle)const
{
return *triangle;
}
};
template<class A>
struct Address_of{
typedef const A* result_type;
const A* operator()(const A& a) const
{
return &a;
}
};
}//namesapce internal
template <class Point_2>
class Random_points_in_triangles_2 : public Generic_random_point_generator<
const typename Kernel_traits<Point_2>::Kernel::Triangle_2*,
internal::Deref<const typename Kernel_traits<Point_2>::Kernel::Triangle_2*>,
Random_points_in_triangle_2<Point_2> ,
Point_2> {
public:
typedef typename Kernel_traits<Point_2>::Kernel::Triangle_2 Triangle_2;
typedef Generic_random_point_generator<
const typename Kernel_traits<Point_2>::Kernel::Triangle_2*,
internal::Deref<const Triangle_2*>,
Random_points_in_triangle_2<Point_2> ,Point_2> Base;
typedef const Triangle_2* Id;
typedef Point_2 result_type;
typedef Random_points_in_triangles_2<Point_2> This;
template<typename TriangleRange>
Random_points_in_triangles_2( const TriangleRange& triangles, Random& rnd = default_random)
: Base(make_range( boost::make_transform_iterator(triangles.begin(), internal::Address_of<Triangle_2>()),
boost::make_transform_iterator(triangles.end(), internal::Address_of<Triangle_2>()) ),
internal::Deref<const Triangle_2*>(),
typename Kernel_traits<Point_2>::Kernel::Compute_area_2()
,rnd )
{
}
This& operator++() {
Base::generate_point();
return *this;
}
This operator++(int) {
This tmp = *this;
++(*this);
return tmp;
}
};
} //namespace CGAL
#endif // CGAL_POINT_GENERATORS_2_H //
// EOF //

View File

@ -469,27 +469,6 @@ public:
}
};
namespace internal
{
template<class T>
class Deref
{
public:
typedef typename std::iterator_traits<T>::value_type result_type;
typename std::iterator_traits<T>::value_type operator()(T triangle)const
{
return *triangle;
}
};
template<class A>
struct Address_of{
typedef const A* result_type;
const A* operator()(const A& a) const
{
return &a;
}
};
}//namesapce internal
template <class Point_3>
class Random_points_on_triangles_3 : public Generic_random_point_generator<

View File

@ -30,6 +30,40 @@ typedef CGAL::Polygon_2<K> Polygon_2;
using namespace CGAL;
int test_triangles_2()
{
typedef K::Point_2 Point;
// Generated points are in that vector
std::vector<Point> points;
// Create input triangles
std::vector<K::Triangle_2> triangles;
triangles.push_back(K::Triangle_2(Point(0,0), Point(0.5,0), Point(0,0.5)));
triangles.push_back(K::Triangle_2(Point(0,0.5), Point(0.5,0), Point(0.5,0.5)));
// Create the generator, input is the vector of Triangle_2
Random_points_in_triangles_2<Point> g(triangles);
// Get 100 random points in triangle range
CGAL::cpp11::copy_n(g, 100, std::back_inserter(points));
// Check that we have really created 100 points.
assert( points.size() == 100);
BOOST_FOREACH(Point p, points)
{
bool on_quad = p.x() > -0.01 && p.x() < 0.51
&& p.y() > -0.01 && p.y() < 0.51;
if(!on_quad)
{
std::cerr<<p<<std::endl;
std::cerr<<"ERROR : Generated point is not on the triangle range."<<std::endl;
return 0;
}
}
return 1;
}
int test_triangles_3()
{
typedef K::Point_3 Point;
@ -45,7 +79,7 @@ int test_triangles_3()
// Create the generator, input is the vector of Triangle_3
Random_points_on_triangles_3<Point> g(triangles);
// Get 100 random points in cdt
// Get 100 random points in triangle range
CGAL::cpp11::copy_n(g, 100, std::back_inserter(points));
// Check that we have really created 100 points.
@ -236,8 +270,9 @@ main( )
int validity =
test_triangles_3()
*test_volume_mesh(polyhedron)
test_triangles_2()
*test_triangles_3()
*test_volume_mesh(polyhedron)
*test_T2()
*test_on_c3t3(polyhedron)
*test_in_c3t3(polyhedron)