Skip to content

Commit 74a8eb1

Browse files
committed
Merge pull request #2853 from MaelRL/Generator-Fix_missing_domain_check-GF
Generator: Fix `random_points_in_triangle_mesh_2`
2 parents 7050565 + 6b77d51 commit 74a8eb1

File tree

4 files changed

+196
-105
lines changed

4 files changed

+196
-105
lines changed

Generator/examples/Generator/random_points_on_triangle_mesh_2.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#include <CGAL/Polygon_2.h>
21
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
2+
33
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
4-
#include <CGAL/Triangulation_face_base_with_info_2.h>
54
#include <CGAL/Delaunay_mesh_face_base_2.h>
5+
#include <CGAL/Delaunay_mesh_size_criteria_2.h>
6+
#include <CGAL/Delaunay_mesher_2.h>
7+
#include <CGAL/Triangulation_face_base_with_info_2.h>
8+
#include <CGAL/Polygon_2.h>
69
#include <CGAL/point_generators_2.h>
710

811
#include <iostream>
@@ -13,15 +16,17 @@ typedef CGAL::Triangulation_vertex_base_2<K> Vb;
1316
typedef CGAL::Delaunay_mesh_face_base_2<K> Fb;
1417
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
1518
typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds> CDT;
19+
1620
typedef CDT::Point Point;
1721
typedef CGAL::Polygon_2<K> Polygon_2;
18-
22+
typedef CGAL::Delaunay_mesh_size_criteria_2<CDT> Mesh_2_criteria;
1923

2024
using namespace CGAL;
2125
int main()
2226
{
2327
// Generated points are in that vector
2428
std::vector<Point> points;
29+
2530
//Construct two non-intersecting nested polygons
2631
::Polygon_2 polygon1;
2732
polygon1.push_back(Point(0,0));
@@ -38,15 +43,17 @@ int main()
3843
cdt.insert_constraint(polygon1.vertices_begin(), polygon1.vertices_end(), true);
3944
cdt.insert_constraint(polygon2.vertices_begin(), polygon2.vertices_end(), true);
4045

46+
// Refine the triangulation (and mark the faces as inside/outside)
47+
CGAL::refine_Delaunay_mesh_2(cdt, Mesh_2_criteria(0.125, 0.5));
48+
4149
// Create the generator, input is the Triangulation_2 cdt
42-
Random_points_in_triangle_mesh_2<Point, CDT>
43-
g(cdt);
50+
Random_points_in_triangle_mesh_2<Point, CDT> g(cdt);
4451

4552
// Get 100 random points in cdt
46-
CGAL::cpp11::copy_n( g, 100, std::back_inserter(points));
53+
CGAL::cpp11::copy_n(g, 100, std::back_inserter(points));
4754

4855
// Check that we have really created 100 points.
49-
assert( points.size() == 100);
56+
assert(points.size() == 100);
5057

5158
// print the first point that was generated
5259
std::cout << points[0] << std::endl;

Generator/include/CGAL/internal/Generic_random_point_generator.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
#ifndef CGAL_INTERNAL_GENERIC_RANDOM_POINT_GENERATOR_H
2222
#define CGAL_INTERNAL_GENERIC_RANDOM_POINT_GENERATOR_H
2323

24+
#include <CGAL/assertions.h>
25+
#include <CGAL/Iterator_range.h>
2426
#include <CGAL/generators.h>
2527
#include <CGAL/Random.h>
2628
#include <CGAL/property_map.h>
27-
#include <vector>
29+
2830
#include <boost/foreach.hpp>
29-
#include <CGAL/Iterator_range.h>
31+
32+
#include <vector>
3033

3134
namespace CGAL {
3235

@@ -56,6 +59,8 @@ class Generic_random_point_generator : public Random_generator_base<P>
5659
, random(rnd)
5760
{
5861
std::size_t input_size = input.size();
62+
CGAL_precondition(input_size > 0);
63+
5964
ids.reserve(input_size);
6065
weights.reserve(input_size);
6166

@@ -70,9 +75,11 @@ class Generic_random_point_generator : public Random_generator_base<P>
7075
total_weight += to_double( compute_weight(object) );
7176
weights.push_back(total_weight);
7277
}
78+
7379
//generate the first point
7480
generate_point();
7581
}
82+
7683
This& operator++()
7784
{
7885
generate_point();
@@ -84,6 +91,7 @@ class Generic_random_point_generator : public Random_generator_base<P>
8491
++(*this);
8592
return tmp;
8693
}
94+
8795
double sum_of_weights() const
8896
{
8997
if (weights.empty())

Generator/include/CGAL/point_generators_2.h

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828

2929
#ifndef CGAL_POINT_GENERATORS_2_H
3030
#define CGAL_POINT_GENERATORS_2_H 1
31+
3132
#include <CGAL/generators.h>
32-
#include <iterator>
3333
#include <CGAL/number_type_basic.h>
3434
#include <CGAL/internal/Generic_random_point_generator.h>
3535
#include <CGAL/iterator.h>
3636

37+
#include <iterator>
38+
3739
namespace CGAL {
3840

3941
template < class P, class Creator =
@@ -547,6 +549,7 @@ void Random_points_in_triangle_2<P, Creator>::generate_point() {
547549
}
548550

549551
namespace internal {
552+
550553
//Functor returning Triangle_2 from Triangulation_2 Faces
551554
template <class T>
552555
class Triangle_from_face_2
@@ -556,38 +559,75 @@ class Triangle_from_face_2
556559
typedef Triangle result_type;
557560
Triangle_from_face_2() {}
558561

559-
Triangle operator()(typename T::Face_handle face)const {
562+
Triangle operator()(typename T::Finite_faces_iterator face) const {
560563
return Triangle(face->vertex(0)->point(), face->vertex(1)->point(), face->vertex(2)->point());
561564
}
562565
};
566+
567+
struct Is_not_in_domain
568+
{
569+
typedef bool result_type;
570+
571+
template <class FH>
572+
result_type operator()(const FH fh) const {
573+
return (!fh->is_in_domain());
574+
}
575+
};
576+
577+
template <class T>
578+
class In_domain_finite_faces_iterator
579+
: public Filter_iterator<typename T::Finite_faces_iterator, Is_not_in_domain>
580+
{
581+
typedef CGAL::Filter_iterator<typename T::Finite_faces_iterator, Is_not_in_domain> Base;
582+
typedef In_domain_finite_faces_iterator<T> Self;
583+
584+
typedef typename T::Face_handle Face_handle;
585+
typedef typename T::Finite_faces_iterator Finite_faces_iterator;
586+
587+
public:
588+
In_domain_finite_faces_iterator() : Base() {}
589+
In_domain_finite_faces_iterator(const Base &b) : Base(b) {}
590+
Self & operator++() { Base::operator++(); return *this; }
591+
Self & operator--() { Base::operator--(); return *this; }
592+
Self operator++(int) { Self tmp(*this); ++(*this); return tmp; }
593+
Self operator--(int) { Self tmp(*this); --(*this); return tmp; }
594+
operator Finite_faces_iterator() const { return Base::base(); }
595+
operator Face_handle() const { return Face_handle(Base::base()); }
596+
};
597+
563598
}//end namespace internal
599+
564600
template <class P,
565601
class T,
566-
class Creator =
567-
Creator_uniform_2<typename Kernel_traits<P>::Kernel::RT,P>
568-
>
569-
class Random_points_in_triangle_mesh_2 : public Generic_random_point_generator<
570-
typename T::Face_handle ,
571-
internal::Triangle_from_face_2<T>,
572-
Random_points_in_triangle_2<P> , P> {
602+
class Creator = Creator_uniform_2<typename Kernel_traits<P>::Kernel::RT, P> >
603+
class Random_points_in_triangle_mesh_2
604+
: public Generic_random_point_generator<internal::In_domain_finite_faces_iterator<T>,
605+
internal::Triangle_from_face_2<T>,
606+
Random_points_in_triangle_2<P, Creator>,
607+
P>
608+
{
573609
public:
574-
typedef Generic_random_point_generator<typename T::Face_handle,
610+
typedef Generic_random_point_generator<internal::In_domain_finite_faces_iterator<T>,
575611
internal::Triangle_from_face_2<T>,
576612
Random_points_in_triangle_2<P, Creator>,
577613
P> Base;
578614
typedef typename T::Face_handle Id;
579-
typedef P result_type;
615+
typedef P result_type;
580616
typedef Random_points_in_triangle_mesh_2<P, T, Creator> This;
581617

582-
583-
Random_points_in_triangle_mesh_2( const T& triangulation, Random& rnd = get_default_random())
584-
: Base( CGAL::make_prevent_deref_range(triangulation.finite_faces_begin(),
585-
triangulation.finite_faces_end()),
586-
internal::Triangle_from_face_2<T>(),
587-
typename Kernel_traits<P>::Kernel::Compute_area_2(),
588-
rnd )
618+
Random_points_in_triangle_mesh_2(const T& triangulation, Random& rnd = get_default_random())
619+
: Base(CGAL::make_prevent_deref_range(
620+
CGAL::filter_iterator(triangulation.finite_faces_end(),
621+
internal::Is_not_in_domain(),
622+
triangulation.finite_faces_begin()),
623+
CGAL::filter_iterator(triangulation.finite_faces_end(),
624+
internal::Is_not_in_domain())),
625+
internal::Triangle_from_face_2<T>(),
626+
typename Kernel_traits<P>::Kernel::Compute_area_2(),
627+
rnd)
589628
{
590629
}
630+
591631
This& operator++() {
592632
Base::generate_point();
593633
return *this;

0 commit comments

Comments
 (0)