Implements a random point generator for FaceGraphLists, and an unfinished test.

This commit is contained in:
Maxime Gimeno 2016-07-08 09:58:04 +02:00 committed by Sébastien Loriot
parent c772a7a917
commit 709f3984c1
2 changed files with 151 additions and 0 deletions

View File

@ -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 <CGAL/generators.h>
#include <CGAL/Random.h>
#include <vector>
#include <boost/foreach.hpp>
namespace CGAL{
template <typename T, class ConstructObject, class GeneratorOnObject, class P>
class Generic_random_generator_on_object: public Random_generator_base<P>
{
typedef Generic_random_generator_on_object<T, ConstructObject, GeneratorOnObject, P> This;
typedef typename ConstructObject::reference Geometric_object;
typedef typename GeneratorOnObject::result_type result_type;
std::vector<T> faces;
ConstructObject constructObject;
//vector of weights
std::vector<double> weights;
Random& random;
void generate_point();
public:
template<class InputRange, class ComputeObjectWeight>
Generic_random_generator_on_object(InputRange input, ConstructObject constructObject,
ComputeObjectWeight computeObjectWeight, Random& rnd = get_default_random())
: Random_generator_base<P>(),
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<T, ConstructObject, GeneratorOnObject, P>::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

View File

@ -0,0 +1,53 @@
#include <CGAL/internal/Generic_random_point_generator.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/internal/AABB_tree/Halfedge_and_face_graph_property_maps.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Surface_mesh.h>
#include <iostream>
#include <fstream>
using namespace CGAL;
int
main( )
{
typedef Simple_cartesian<double> R;
typedef R::Point_3 Point;
typedef R::FT FT;
typedef Polyhedron_3<R> Polyhedron;
typedef Surface_mesh<Point> Surface_mesh;
typedef boost::property_map<Polyhedron,
vertex_point_t>::type Vertex_point_pmap;
typedef boost::property_map<Surface_mesh,
vertex_point_t>::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<Point> Creator;
typedef boost::graph_traits<Polyhedron>::face_descriptor face_iterator;
typedef boost::graph_traits<Surface_mesh>::face_descriptor face_iterator_sm;
std::vector<Point> 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<face_iterator_sm, Generator_SM, Creator, Point>
// g(faces(sm), sm_generator, weight_computer);
Generic_random_generator_on_object<face_iterator_sm, Generator_SM, Creator, Point>
g(faces(sm), sm_generator, weight_computer);
CGAL::cpp11::copy_n( g, 3000, std::back_inserter(points));
for (std::size_t i = 0; i<points.size(); ++i)
std::cerr<<points[i]<<std::endl;
return 0;
}