mirror of https://github.com/CGAL/cgal
Add a random_points_generator on a range of triangles, and an exemple for it.
This commit is contained in:
parent
2ac64c542a
commit
a7282912a0
|
|
@ -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<Point_2, Creator>`
|
||||
\sa `CGAL::Random_points_in_cube_3<Point_3, Creator>`
|
||||
\sa `CGAL::Random_points_in_triangle_3<Point_3, Creator>`
|
||||
\sa `CGAL::Random_points_on_sphere_3<Point_3, Creator>`
|
||||
\sa `CGAL::Random_points_in_triangle_mesh_2<Point_2, Triangulation>`
|
||||
\sa `CGAL::Random_points_on_tetrahedral_mesh_boundary<C3T3>`
|
||||
\sa `CGAL::Random_points_in_tetrahedral_mesh_3<C3T3>`
|
||||
\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<typename TriangleRange>
|
||||
Random_points_on_triangles_3(TriangleRange& triangles, Random& rnd = default_random);
|
||||
|
||||
/// @}
|
||||
|
||||
}; /* end Random_points_on_triangles_3 */
|
||||
|
||||
} /* end namespace CGAL */
|
||||
|
||||
namespace CGAL {
|
||||
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
#include <CGAL/Simple_cartesian.h>
|
||||
#include <CGAL/point_generators_3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace CGAL;
|
||||
typedef Simple_cartesian<double> K;
|
||||
typedef K::Point_3 Point;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// Generated points are in that vector
|
||||
std::vector<Point> points;
|
||||
// Create input triangles
|
||||
std::vector<K::Triangle_3> 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<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;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -469,7 +469,68 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
namespace internal
|
||||
{
|
||||
template<class T>
|
||||
class DummyFunctor
|
||||
{
|
||||
public:
|
||||
typedef typename std::iterator_traits<T>::value_type result_type;
|
||||
|
||||
DummyFunctor()
|
||||
{}
|
||||
typename std::iterator_traits<T>::value_type operator()(T triangle)const
|
||||
{
|
||||
return *triangle;
|
||||
}
|
||||
};
|
||||
template<class A>
|
||||
struct Get_ref{
|
||||
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<
|
||||
const typename Kernel_traits<Point_3>::Kernel::Triangle_3*,
|
||||
internal::DummyFunctor<const typename Kernel_traits<Point_3>::Kernel::Triangle_3*>,
|
||||
Random_points_in_triangle_3<Point_3> ,Point_3> {
|
||||
public:
|
||||
typedef typename Kernel_traits<Point_3>::Kernel::Triangle_3 Triangle_3;
|
||||
typedef Generic_random_point_generator<
|
||||
const typename Kernel_traits<Point_3>::Kernel::Triangle_3*,
|
||||
internal::DummyFunctor<const Triangle_3*>,
|
||||
Random_points_in_triangle_3<Point_3> ,Point_3> Base;
|
||||
typedef const Triangle_3* Id;
|
||||
typedef Point_3 result_type;
|
||||
typedef Random_points_on_triangles_3<Point_3> This;
|
||||
|
||||
template<typename TriangleRange>
|
||||
Random_points_on_triangles_3( TriangleRange triangles, Random& rnd = default_random)
|
||||
: Base(make_range( boost::make_transform_iterator(triangles.begin(), internal::Get_ref<Triangle_3>()),
|
||||
boost::make_transform_iterator(triangles.end(), internal::Get_ref<Triangle_3>()) ),
|
||||
internal::DummyFunctor<const Triangle_3*>(),
|
||||
typename Kernel_traits<Point_3>::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 //
|
||||
|
|
|
|||
|
|
@ -28,6 +28,44 @@ typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
|
|||
typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds> CDT;
|
||||
typedef CGAL::Polygon_2<K> Polygon_2;
|
||||
using namespace CGAL;
|
||||
int test_triangles_3()
|
||||
{
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace CGAL;
|
||||
typedef Simple_cartesian<double> K;
|
||||
typedef K::Point_3 Point;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// Generated points are in that vector
|
||||
std::vector<Point> points;
|
||||
// Create input triangles
|
||||
std::vector<K::Triangle_3> 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<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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int test_T2()
|
||||
{
|
||||
typedef CDT::Point Point_2;
|
||||
|
|
|
|||
Loading…
Reference in New Issue