Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAL: How to efficiently calculate the area of facets of a polyhedron?

I have a polyhedron whose facets are triangles. I am aware that in CGAL, Triangle_3 class offers 'squared_area' method through which we can calculate the area of a triangle. Is there any way we can apply this to polyhedral facets? Or any ideas as to how to calculate area of each facet?

like image 618
sandeep p Avatar asked Dec 21 '22 05:12

sandeep p


2 Answers

Here is an example:

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <numeric>
#include <functional>
#include <boost/iterator/transform_iterator.hpp>

typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Polyhedron_3<K> Polyhedron;

struct Compute_area:
  public std::unary_function<const Polyhedron::Facet, double>
{
  double operator()(const Polyhedron::Facet& f) const{
    return K::Compute_area_3()(
      f.halfedge()->vertex()->point(),
      f.halfedge()->next()->vertex()->point(),
      f.halfedge()->opposite()->vertex()->point() );
  }
};

int main()
{
  Polyhedron p;
  p.make_tetrahedron(
    K::Point_3(0,0,0),
    K::Point_3(0,1,0),
    K::Point_3(1,1,0),
    K::Point_3(1,1,3)
  );

CGAL_assertion( p.is_pure_triangle() );

  Compute_area ca;

  std::cout <<
    std::accumulate(
      boost::make_transform_iterator(p.facets_begin(), ca),
      boost::make_transform_iterator(p.facets_end(), ca),
      0.)
  << std::endl;
}

EDIT There is the free function CGAL::Polygon_mesh_processing::area() that is available in recent releases of CGAL.

like image 58
sloriot Avatar answered Dec 24 '22 02:12

sloriot


Expanding @sloriot answer, since std::unary_function is deprecated in C++11 and will be removed from C++14 onwards. However the unary_function isn't needed anymore and Compute_area can simply be implemented as

struct Compute_area
{
   double operator()(const Polyhedron::Facet& f) const {
   return K::Compute_area_3()(
     f.halfedge()->vertex()->point(),
     f.halfedge()->next()->vertex()->point(),
     f.halfedge()->opposite()->vertex()->point() );
    }
};                                                                              
like image 34
greole Avatar answered Dec 24 '22 01:12

greole