#### Computing edge length of all half-edges in CGAL::Polyhedron_3

In this post I will show how to compute edge length for a half-edge in CGAL::Polyhedron_3.

##### ComputeEdgeLength() Functor

ComputeEdgeLength() is a thread-safe functor for computing the edge length of a given half-edge in a CGAL::Polyhedron_3.

```#ifndef _SMESHLIB_OPERATIONS_COMPUTEEDGELENGTH_H_
#define _SMESHLIB_OPERATIONS_COMPUTEEDGELENGTH_H_

namespace SMeshLib   {
namespace Operations {
;

// The ComputeEdgeLength is a functor (delegate) to compute the length of a halfedge in CGAL::Polyhdeon_3.
// TPolyhedron is a type of CGAL::Polyhdeon_3.
template<class TPolyhedron>
struct ComputeEdgeLength
{
public:

// Redefine types from TPolyhedron for convenience.
typedef typename TPolyhedron::Point_3  Point3;
typedef typename TPolyhedron::Halfedge Halfedge;

// Return type of operator() required by QtConcurrent.
typedef double result_type;

public:

// Compute edge length of a given half edge.
inline double operator () (const Halfedge& h)
{
const Point3& p = h.prev()->vertex()->point();
const Point3& q = h.vertex()->point();
return CGAL::sqrt(CGAL::squared_distance(p, q));
}
};

};	// End namespace Operations.
};	// End namespace SMeshLib.

#endif // _SMESHLIB_OPERATIONS_COMPUTEEDGELENGTH_H_```
##### Using ComputeEdgeLength() Functor

Edge length of a half-edge h can be computed as double length = ComputeEdgeLength(h);.

For most purposes, it is better to compute length of all half-edges once and cache them for later use. It is best to store the results in an associative container which associates the half-edge handle with the edge length. In the following example, I use PropertyMap which is a wrapper for std::set.

```#include "ImportOBJ.h"
#include "ComputeEdgeLength.h"
#include "PropertyMap.h"
#include "CGAL/Simple_cartesian.h"
#include "CGAL/Polyhedron_items_3.h"
#include "CGAL/HalfedgeDS_list.h"
#include "CGAL/Polyhedron_3.h"

typedef CGAL::Simple_cartesian<double> Kernel;
typedef CGAL::Polyhedron_3<Kernel,
CGAL::Polyhedron_items_3,
CGAL::HalfedgeDS_list> CgalPolyhedron;

// Compute the length of all half-edges in a CGAL::Polyhedron_3 and stores it
// in edgeLengths.
// TPolyhedron is a type of CGAL::Polyhedron_3.
// TEdgeLengths is a property map which associates the edge length to half-edge
// in the CGAL::Polyhdeon_3.
template<class TPolyhedron, class TEdgeLengths>
void computeEdgeLengths(const TPolyhedron& polyhedron, TEdgeLengths* edgeLengths)
{
if(edgeLengths == 0)
{
return;
}

typename TPolyhedron::Halfedge_const_iterator _begin = polyhedron.halfedges_begin();
typename TPolyhedron::Halfedge_const_iterator _end   = polyhedron.halfedges_end();

SMeshLib::Operations::ComputeEdgeLength<TPolyhedron> _computeEdgeLength;
CGAL_For_all(_begin, _end)
{
edgeLengths->setValue(&*_begin, _computeEdgeLength(*_begin));
}
}

void testComputeEdgeLength()
{
CgalPolyhedron _poly;
SMeshLib::IO::importOBJ("Venus.obj", &_poly);

typedef SMeshLib::PropertyMap<const CgalPolyhedron::Halfedge*, double> EdgeLengthPM;
EdgeLengthPM _edgeLengths;
computeEdgeLengths(_poly, &_edgeLengths);
}```