diff --git a/Generator/include/CGAL/internal/Generic_random_point_generator.h b/Generator/include/CGAL/internal/Generic_random_point_generator.h new file mode 100644 index 00000000000..9d4a8eef9be --- /dev/null +++ b/Generator/include/CGAL/internal/Generic_random_point_generator.h @@ -0,0 +1,98 @@ +// Copyright (c) 2012 GeometryFactory (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// You can redistribute it and/or modify it under the terms of the GNU +// General Public License as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// +// +// Author(s) : Maxime Gimeno +// +#ifndef GENERIC_RANDOM_POINT_GENERATOR_H +#define GENERIC_RANDOM_POINT_GENERATOR_H + +#include +#include +#include +#include + +namespace CGAL{ +template +class Generic_random_generator_on_object: public Random_generator_base

+{ + typedef Generic_random_generator_on_object This; + typedef typename ConstructObject::reference Geometric_object; + typedef typename GeneratorOnObject::result_type result_type; + std::vector faces; + ConstructObject constructObject; + //vector of weights + std::vector weights; + Random& random; + void generate_point(); +public: + template + Generic_random_generator_on_object(InputRange input, ConstructObject constructObject, + ComputeObjectWeight computeObjectWeight, Random& rnd = get_default_random()) + : Random_generator_base

(), + constructObject(constructObject), + random(rnd) + { + // fill the weights + double last_weight = 0; + BOOST_FOREACH(T fit, input) + { + //create a face + Geometric_object face = get(constructObject, fit); + faces.push_back(fit); + //compute the weight of a face + double weight = computeObjectWeight(face); + last_weight+=weight; + weights.push_back(last_weight); + } + //generate the points + generate_point(); + } + This& operator++() + { + generate_point(); + return *this; + } + This operator++(int) + { + This tmp = *this; + ++(*this); + return tmp; + } +}; + +template < typename T, class ConstructObject, class GeneratorOnObject, class P > +void Generic_random_generator_on_object::generate_point() +{ + //shoot a random value in weights + int target = std::distance( + weights.begin(), + std::upper_bound( + weights.begin(), + weights.end(), + random.get_double(0, weights.back()) + ) + ); + + // generate the points + GeneratorOnObject pointCreator(get(constructObject,faces[target])); + this->d_item = *pointCreator; +} +}//namesape CGAL +#endif // GENERIC_RANDOM_POINT_GENERATOR_H + + diff --git a/Generator/test/Generator/generic_random_test.cpp b/Generator/test/Generator/generic_random_test.cpp new file mode 100644 index 00000000000..bde46f2a059 --- /dev/null +++ b/Generator/test/Generator/generic_random_test.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace CGAL; + +int +main( ) +{ + typedef Simple_cartesian R; + typedef R::Point_3 Point; + typedef R::FT FT; + typedef Polyhedron_3 Polyhedron; + typedef Surface_mesh Surface_mesh; + typedef boost::property_map::type Vertex_point_pmap; + typedef boost::property_map::type Vertex_point_pmap_sm; + typedef Triangle_from_face_descriptor_property_map< + Polyhedron,Vertex_point_pmap> Generator; + typedef Triangle_from_face_descriptor_property_map< + Surface_mesh,Vertex_point_pmap_sm> Generator_SM; + typedef Random_points_in_triangle_3 Creator; + typedef boost::graph_traits::face_descriptor face_iterator; + typedef boost::graph_traits::face_descriptor face_iterator_sm; + + + + std::vector points; + Polyhedron poly; + Surface_mesh sm; + std::ifstream in("../../../Polyhedron/demo/Polyhedron/data/star.off"); + in >> sm; + CGAL_assertion(in && !sm.is_empty()); + + poly.make_tetrahedron(Point(0.0,0.2,0.0), Point(-0.2,0.0,0.0), Point(0.2,0.0,0.0), Point(0.0,0.0,0.2)); + Generator generator(&poly); + Generator_SM sm_generator(&sm); + R::Compute_area_3 weight_computer; + //Generic_random_generator_on_object + // g(faces(sm), sm_generator, weight_computer); + Generic_random_generator_on_object + g(faces(sm), sm_generator, weight_computer); + CGAL::cpp11::copy_n( g, 3000, std::back_inserter(points)); + for (std::size_t i = 0; i