dust3d/thirdparty/cgal/CGAL-5.1/include/CGAL/compute_outer_frame_margin.h

118 lines
3.7 KiB
C
Raw Normal View History

// Copyright (c) 2006-2008 Fernando Luis Cacciola Carballal. All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
2020-10-13 12:44:25 +00:00
// $URL: https://github.com/CGAL/cgal/blob/v5.1/Straight_skeleton_2/include/CGAL/compute_outer_frame_margin.h $
// $Id: compute_outer_frame_margin.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Fernando Cacciola <fernando_cacciola@ciudad.com.ar>
//
#ifndef CGAL_COMPUTE_OUTER_FRAME_MARGIN_H
#define CGAL_COMPUTE_OUTER_FRAME_MARGIN_H
#include <CGAL/license/Straight_skeleton_2.h>
#include <vector>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <boost/optional/optional.hpp>
#include <CGAL/algorithm.h>
#include <CGAL/Polygon_offset_builder_traits_2.h>
namespace CGAL {
template<class ForwardPointIterator, class Traits>
boost::optional< typename Traits::FT > compute_outer_frame_margin ( ForwardPointIterator aBegin
, ForwardPointIterator aEnd
, typename Traits::FT aOffset
, Traits const& aTraits
)
{
typedef typename Traits::Kernel Kernel ;
typedef typename Traits::FT FT ;
typedef typename Traits::Point_2 Point_2 ;
typedef typename Traits::Segment_2 Segment_2 ;
typedef typename Traits::Trisegment_2_ptr Trisegment_2_ptr ;
2020-10-13 12:44:25 +00:00
Kernel kernel ;
2020-10-13 12:44:25 +00:00
typename Kernel::Equal_2 equal = kernel.equal_2_object();
typename Kernel::Collinear_2 collinear = kernel.collinear_2_object();
typename Kernel::Compute_squared_distance_2 squared_distance = kernel.compute_squared_distance_2_object();
typename Kernel::Construct_segment_2 construct_segment = kernel.construct_segment_2_object();
2020-10-13 12:44:25 +00:00
typedef boost::optional<Point_2> OptionalPoint_2 ;
2020-10-13 12:44:25 +00:00
FT lMaxSDist(0.0) ;
2020-10-13 12:44:25 +00:00
ForwardPointIterator lLast = std::prev(aEnd) ;
bool lOverflow = false ;
for ( ForwardPointIterator lCurr = aBegin ; lCurr != aEnd ; ++ lCurr )
{
2020-10-13 12:44:25 +00:00
ForwardPointIterator lPrev = ( lCurr == aBegin ? lLast : std::prev (lCurr) ) ;
ForwardPointIterator lNext = ( lCurr == lLast ? aBegin : std::next (lCurr) ) ;
if ( !equal(*lPrev,*lCurr) && !equal(*lCurr,*lNext) && !collinear(*lPrev,*lCurr,*lNext) )
{
Segment_2 lLEdge = construct_segment(*lPrev,*lCurr);
Segment_2 lREdge = construct_segment(*lCurr,*lNext);
2020-10-13 12:44:25 +00:00
OptionalPoint_2 lP = Construct_offset_point_2(aTraits)(aOffset,lLEdge,lREdge, Trisegment_2_ptr() );
2020-10-13 12:44:25 +00:00
if ( !lP )
{
lOverflow = true ;
break ;
}
2020-10-13 12:44:25 +00:00
FT lSDist = squared_distance(*lCurr,*lP);
2020-10-13 12:44:25 +00:00
if ( ! CGAL_NTS is_valid ( lSDist )
|| ! CGAL_NTS is_finite( lSDist )
2020-10-13 12:44:25 +00:00
)
{
lOverflow = true ;
break ;
2020-10-13 12:44:25 +00:00
}
if ( lSDist > lMaxSDist )
lMaxSDist = lSDist ;
}
}
2020-10-13 12:44:25 +00:00
if ( ! lOverflow )
{
FT lDist = CGAL_SS_i::inexact_sqrt(lMaxSDist) ;
2020-10-13 12:44:25 +00:00
return boost::optional<FT>( lDist + ( aOffset * FT(1.05) ) ) ; // Add a %5 gap
}
else
return boost::optional<FT>();
2020-10-13 12:44:25 +00:00
}
template<class ForwardPointIterator, class FT>
boost::optional<FT> compute_outer_frame_margin ( ForwardPointIterator aBegin, ForwardPointIterator aEnd, FT aOffset )
{
typedef typename std::iterator_traits<ForwardPointIterator>::value_type Point_2 ;
2020-10-13 12:44:25 +00:00
typedef typename Kernel_traits<Point_2>::Kernel K;
Polygon_offset_builder_traits_2<K> traits ;
2020-10-13 12:44:25 +00:00
return compute_outer_frame_margin(aBegin,aEnd,aOffset,traits);
2020-10-13 12:44:25 +00:00
}
} // end namespace CGAL
#endif // CGAL_COMPUTE_OUTER_FRAME_MARGIN_H //
// EOF //
2020-10-13 12:44:25 +00:00