Skip to content

Instantly share code, notes, and snippets.

@visorz
Created September 26, 2015 17:46
Show Gist options
  • Save visorz/7061fd1be9377e79dc08 to your computer and use it in GitHub Desktop.
Save visorz/7061fd1be9377e79dc08 to your computer and use it in GitHub Desktop.
Point iterator providing equal access on Polyhedron_3 and Surfacemesh
#include <CGAL/Simple_cartesian.h>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::FT Scalar;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
#include <CGAL/Surface_mesh.h>
typedef CGAL::Surface_mesh<Point> Surfacemesh;
#include <CGAL/Polyhedron_3.h>
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
#include <CGAL/centroid.h>
#include <CGAL/bounding_box.h>
#include <boost/ref.hpp>
class PointIteratorSurfacemesh
{
private:
struct V2P {
typedef Point result_type;
boost::reference_wrapper<const Surfacemesh> mesh_; // CGAL::bounding_box() needs assignable iterators, not only a ref
V2P( const Surfacemesh& mesh ) : mesh_( mesh ) {}
const Point& operator()( Surfacemesh::Vertex_index v ) const { return mesh_.get().point( v ); }
};
V2P v2p_;
const Surfacemesh& mesh_;
typedef boost::transform_iterator<V2P, Surfacemesh::Vertex_iterator> Point_iterator;
public:
PointIteratorSurfacemesh( const Surfacemesh& mesh ) : v2p_( mesh ), mesh_( mesh ) {}
Point_iterator begin() { return boost::make_transform_iterator( mesh_.vertices().begin(), v2p_ ); }
Point_iterator end() { return boost::make_transform_iterator( mesh_.vertices().end(), v2p_ ); }
};
class PointIteratorPolyhedron
{
private:
const Polyhedron& mesh_;
typedef Polyhedron::Point_const_iterator iterator;
public:
PointIteratorPolyhedron( const Polyhedron& mesh ) : mesh_( mesh ) {}
iterator begin() const { return mesh_.points_begin(); }
iterator end() const { return mesh_.points_end(); }
};
/// returns assignable iterator over points of mesh
PointIteratorSurfacemesh points( const Surfacemesh& mesh ) { return PointIteratorSurfacemesh( mesh ); }
/// returns assignable iterator over points of mesh
PointIteratorPolyhedron points( const Polyhedron& mesh ) { return PointIteratorPolyhedron( mesh ); }
int main( int argc, char* argv[] )
{
Polyhedron p;
Surfacemesh s;
CGAL::centroid( points( s ).begin(), points( s ).end() );
CGAL::centroid( points( p ).begin(), points( p ).end() );
CGAL::bounding_box( points( s ).begin(), points( s ).end() );
CGAL::bounding_box( points( p ).begin(), points( p ).end() );
for ( Point point : points( p ) ) {
point.x();
}
for ( Point point : points( s ) ) {
point.y();
}
// BOOST_FOREACH macro does not work, but I don't need it
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment