From a7282912a0a7341c77b8a674370ef4fa29bf5bc7 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Mon, 18 Jul 2016 18:49:59 +0200 Subject: [PATCH] Add a random_points_generator on a range of triangles, and an exemple for it. --- .../doc/Generator/CGAL/point_generators_3.h | 71 +++++++++++++++++++ .../random_points_on_triangles_3.cpp | 36 ++++++++++ Generator/include/CGAL/point_generators_3.h | 61 ++++++++++++++++ .../test/Generator/generic_random_test.cpp | 38 ++++++++++ 4 files changed, 206 insertions(+) create mode 100644 Generator/examples/Generator/random_points_on_triangles_3.cpp diff --git a/Generator/doc/Generator/CGAL/point_generators_3.h b/Generator/doc/Generator/CGAL/point_generators_3.h index 8d99c7ffe7f..0d3c94e1dc1 100644 --- a/Generator/doc/Generator/CGAL/point_generators_3.h +++ b/Generator/doc/Generator/CGAL/point_generators_3.h @@ -331,6 +331,77 @@ get_default_random()); } /* end namespace CGAL */ + +namespace CGAL { + +/*! + +The class `Random_points_on_triangles_3` is an input iterator creating points uniformly +distributed inside the triangles of a range. + +\cgalModels `InputIterator` +\cgalModels `PointGenerator` + +\sa `CGAL::cpp11::copy_n()` +\sa `CGAL::Counting_iterator` +\sa `CGAL::Random_points_in_disc_2` +\sa `CGAL::Random_points_in_cube_3` +\sa `CGAL::Random_points_in_triangle_3` +\sa `CGAL::Random_points_on_sphere_3` +\sa `CGAL::Random_points_in_triangle_mesh_2` +\sa `CGAL::Random_points_on_tetrahedral_mesh_boundary` +\sa `CGAL::Random_points_in_tetrahedral_mesh_3` +\sa `std::random_shuffle` + +*/ +template < class Point_3> +class Random_points_on_triangles_3 { +public: + +/// \name Types +/// @{ + +/*! + +*/ +typedef std::input_iterator_tag iterator_category; + +/*! + +*/ +typedef Point_3 value_type; + +/*! + +*/ +typedef std::ptrdiff_t difference_type; + +/*! + +*/ +typedef const Point_3* pointer; + +/*! + +*/ +typedef const Point_3& reference; + + + +/*! +Creates an input iterator `g` generating points of type `Point_3` uniformly +distributed in the triangles of `TriangleRange`. Each triangle has a probability to be chosen to hold the point depending on its area. +TriangleRange must stay valid during the whole operation. +*/ + typedef +Random_points_on_triangles_3(TriangleRange& triangles, Random& rnd = default_random); + +/// @} + +}; /* end Random_points_on_triangles_3 */ + +} /* end namespace CGAL */ + namespace CGAL { /*! diff --git a/Generator/examples/Generator/random_points_on_triangles_3.cpp b/Generator/examples/Generator/random_points_on_triangles_3.cpp new file mode 100644 index 00000000000..49a3b3318d5 --- /dev/null +++ b/Generator/examples/Generator/random_points_on_triangles_3.cpp @@ -0,0 +1,36 @@ +#include +#include + +#include +#include +using namespace CGAL; +typedef Simple_cartesian K; +typedef K::Point_3 Point; + + +int main() +{ + // Generated points are in that vector + std::vector points; + // Create input triangles + std::vector triangles; + for(int i=0; i< 5; ++i) + { + triangles.push_back(K::Triangle_3(Point(i,0,0.5*i), Point(i+1,0,0.5*i), Point(i+0.5,1,0.5*i))); + } + + // Create the generator, input is the vector of Triangle_3 + Random_points_on_triangles_3 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; +} + + diff --git a/Generator/include/CGAL/point_generators_3.h b/Generator/include/CGAL/point_generators_3.h index 5045ef35b94..e226462c1f1 100644 --- a/Generator/include/CGAL/point_generators_3.h +++ b/Generator/include/CGAL/point_generators_3.h @@ -469,7 +469,68 @@ public: } }; +namespace internal +{ +template +class DummyFunctor +{ +public: + typedef typename std::iterator_traits::value_type result_type; + + DummyFunctor() + {} + typename std::iterator_traits::value_type operator()(T triangle)const + { + return *triangle; + } +}; +template +struct Get_ref{ + typedef const A* result_type; + const A* operator()(const A& a) const + { + return &a; + } +}; +}//namesapce internal + +template +class Random_points_on_triangles_3 : public Generic_random_point_generator< + const typename Kernel_traits::Kernel::Triangle_3*, + internal::DummyFunctor::Kernel::Triangle_3*>, + Random_points_in_triangle_3 ,Point_3> { +public: + typedef typename Kernel_traits::Kernel::Triangle_3 Triangle_3; + typedef Generic_random_point_generator< + const typename Kernel_traits::Kernel::Triangle_3*, + internal::DummyFunctor, + Random_points_in_triangle_3 ,Point_3> Base; + typedef const Triangle_3* Id; + typedef Point_3 result_type; + typedef Random_points_on_triangles_3 This; + + template + Random_points_on_triangles_3( TriangleRange triangles, Random& rnd = default_random) + : Base(make_range( boost::make_transform_iterator(triangles.begin(), internal::Get_ref()), + boost::make_transform_iterator(triangles.end(), internal::Get_ref()) ), + internal::DummyFunctor(), + typename Kernel_traits::Kernel::Compute_area_3() + ,rnd ) + { + } + This& operator++() { + Base::generate_point(); + return *this; + } + This operator++(int) { + This tmp = *this; + ++(*this); + return tmp; + } +}; + } //namespace CGAL + #endif // CGAL_POINT_GENERATORS_3_H // // EOF // diff --git a/Generator/test/Generator/generic_random_test.cpp b/Generator/test/Generator/generic_random_test.cpp index 8e33eecda94..0a82461eecb 100644 --- a/Generator/test/Generator/generic_random_test.cpp +++ b/Generator/test/Generator/generic_random_test.cpp @@ -28,6 +28,44 @@ typedef CGAL::Triangulation_data_structure_2 Tds; typedef CGAL::Constrained_Delaunay_triangulation_2 CDT; typedef CGAL::Polygon_2 Polygon_2; using namespace CGAL; +int test_triangles_3() +{ +#include +#include +using namespace CGAL; +typedef Simple_cartesian K; +typedef K::Point_3 Point; + + +int main() +{ + // Generated points are in that vector + std::vector points; + // Create input triangles + std::vector triangles; + for(int i=0; i< 5; ++i) + { + triangles.push_back(K::Triangle_3(Point(i,0,0.5*i), Point(i+1,0,0.5*i), Point(i+0.5,1,0.5*i))); + } + + // Create the generator, input is the vector of Triangle_3 + Random_points_on_triangles_3 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; +} + + + +} + int test_T2() { typedef CDT::Point Point_2;