This commit is contained in:
Efi Fogel 2013-08-25 01:26:53 +03:00
parent b99a327ab0
commit 40d1879c5e
8 changed files with 139 additions and 134 deletions

View File

@ -10,11 +10,11 @@
#include "print_utils.h"
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
int main()
{

View File

@ -15,7 +15,8 @@
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Efi Fogel <efifogel@gmail.com>
#ifndef CGAL_POLYGON_DECOMPOSITION_STRATEGY_ADAPTER_H
#define CGAL_POLYGON_DECOMPOSITION_STRATEGY_ADAPTER_H
@ -27,66 +28,87 @@
namespace CGAL {
struct Tag_optimal_convex_parition
{
bool dummy;
};
struct Tag_optimal_convex_parition { bool dummy; };
struct Tag_approx_convex_parition
{
bool dummy;
};
struct Tag_approx_convex_parition { bool dummy; };
struct Tag_Greene_convex_parition
{
bool dummy;
};
struct Tag_Greene_convex_parition { bool dummy; };
/*!
* \class
* An adapter of the global planar polygonal partitioning functions
* to a decomposition strategy-class.
*/
template <class Kernel_, class Container_, class StrategyTag_>
class Polygon_decomposition_strategy_adapter
{
template <typename Kernel_, typename Container_, typename StrategyTag_>
class Polygon_decomposition_strategy_adapter {
public:
typedef Kernel_ Kernel;
typedef CGAL::Polygon_2<Kernel, Container_> Polygon_2;
typedef typename Kernel::Point_2 Point_2;
typedef StrategyTag_ Strategy_tag;
protected:
typedef Partition_traits_2<Kernel> Traits_2;
typedef typename Traits_2::Polygon_2 Traits_polygon_2;
// Data members:
Traits_2 traits;
const Traits_2* m_traits;
bool m_own_traits; // inidicates whether the kernel should be freed up.
public:
/*! Default constructor. */
Polygon_decomposition_strategy_adapter () :
traits()
{}
Polygon_decomposition_strategy_adapter() :
m_traits(NULL),
m_own_traits(false)
{ init(); }
/*! Constructor. */
Polygon_decomposition_strategy_adapter(const Traits_2& traits) :
m_traits(traits),
m_own_traits(false)
{ init(); }
/*! Destructor */
~Polygon_decomposition_strategy_adapter()
{
if (m_own_traits) {
if (m_traits != NULL) {
delete m_traits;
m_traits = NULL;
}
m_own_traits = false;
}
}
//! Initialize
void init()
{
// Allocate the traits if not provided.
if (m_traits == NULL) {
m_traits = new Traits_2;
m_own_traits = true;
}
}
/*!
* Obtain the traits
* \return the traits
*/
const Traits_2* traits() const { return m_traits; }
/*!
* Decompose a simple polygon to convex sub-polygons.
* \param pgn The input polygon.
* \param oi An output iterator of convex polygons.
* \return A past-the-end iterator for the sub-polygons.
*/
template <class OutputIterator>
OutputIterator operator() (const Polygon_2& pgn,
OutputIterator oi) const
template <typename OutputIterator>
OutputIterator operator()(const Polygon_2& pgn, OutputIterator oi) const
{
std::list<Traits_polygon_2> pgns;
typename std::list<Traits_polygon_2>::const_iterator pgn_it;
std::list<Traits_polygon_2> pgns;
typename std::list<Traits_polygon_2>::const_iterator pgn_it;
if (pgn.orientation() == CLOCKWISE)
{
if (pgn.orientation() == CLOCKWISE) {
// Make a local copy of the polygon, and reverse the order of its
// vertices to make it counterclockwise oriented.
Polygon_2 my_pgn = pgn;
@ -94,69 +116,61 @@ public:
my_pgn.reverse_orientation();
// Perform the decomposition.
_decompose (my_pgn, Strategy_tag(), std::back_inserter (pgns));
_decompose (my_pgn, Strategy_tag(), std::back_inserter(pgns));
}
else
{
else {
// Perform the decomposition on the original polygon.
_decompose (pgn, Strategy_tag(), std::back_inserter (pgns));
_decompose (pgn, Strategy_tag(), std::back_inserter(pgns));
}
// Copy the polygons to the output iterator.
for (pgn_it = pgns.begin(); pgn_it != pgns.end(); ++pgn_it)
{
*oi = Polygon_2 (pgn_it->vertices_begin(), pgn_it->vertices_end());
++oi;
}
*oi++ = Polygon_2(pgn_it->vertices_begin(), pgn_it->vertices_end());
return (oi);
}
private:
/*!
* Decompose the given counter clockwise-oriented polygon using the optimal
* convex-partition method.
*/
template <class OutputIterator>
OutputIterator _decompose (const Polygon_2& pgn,
Tag_optimal_convex_parition ,
OutputIterator oi) const
template <typename OutputIterator>
OutputIterator _decompose(const Polygon_2& pgn,
Tag_optimal_convex_parition ,
OutputIterator oi) const
{
return (optimal_convex_partition_2 (pgn.vertices_begin(),
pgn.vertices_end(),
oi,
traits));
return (optimal_convex_partition_2(pgn.vertices_begin(),
pgn.vertices_end(),
oi, *m_traits));
}
/*!
* Decompose the given counter clockwise-oriented polygon using the
* approximated convex-partition method.
*/
template <class OutputIterator>
OutputIterator _decompose (const Polygon_2& pgn,
Tag_approx_convex_parition ,
OutputIterator oi) const
template <typename OutputIterator>
OutputIterator _decompose(const Polygon_2& pgn,
Tag_approx_convex_parition ,
OutputIterator oi) const
{
return (approx_convex_partition_2 (pgn.vertices_begin(),
pgn.vertices_end(),
oi,
traits));
return (approx_convex_partition_2(pgn.vertices_begin(),
pgn.vertices_end(),
oi, *m_traits));
}
/*!
* Decompose the given counter clockwise-oriented polygon using Greene's
* approximated convex-partition method.
*/
template <class OutputIterator>
OutputIterator _decompose (const Polygon_2& pgn,
Tag_Greene_convex_parition ,
OutputIterator oi) const
template <typename OutputIterator>
OutputIterator _decompose(const Polygon_2& pgn,
Tag_Greene_convex_parition ,
OutputIterator oi) const
{
return (greene_approx_convex_partition_2 (pgn.vertices_begin(),
pgn.vertices_end(),
oi,
traits));
return (greene_approx_convex_partition_2(pgn.vertices_begin(),
pgn.vertices_end(),
oi, *m_traits));
}
};

View File

@ -15,7 +15,8 @@
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Efi Fogel <efifogel@gmail.com>
#ifndef CGAL_MINKOWSKI_SUM_CONV_H
#define CGAL_MINKOWSKI_SUM_CONV_H
@ -123,8 +124,8 @@ private:
public:
/*! Default constructor. */
Minkowski_sum_by_convolution_2() :
m_kernel(new Kernel),
m_own_kernel(true)
m_kernel(NULL),
m_own_kernel(false)
{ init(); }
/*! Constructor. */
@ -181,11 +182,10 @@ public:
* \pre Both input polygons are simple.
* \return A past-the-end iterator for the holes in the sum.
*/
template <class OutputIterator>
OutputIterator operator()(const Polygon_2& pgn1,
const Polygon_2& pgn2,
Polygon_2& sum_bound,
OutputIterator sum_holes) const
template <typename OutputIterator>
OutputIterator operator()(const Polygon_2& pgn1, const Polygon_2& pgn2,
Polygon_2& sum_bound, OutputIterator sum_holes)
const
{
CGAL_precondition(pgn1.is_simple());
CGAL_precondition(pgn2.is_simple());

View File

@ -152,7 +152,7 @@ public:
}
// Allocate the strategy if not provided.
if (m_decomposition_strategy == NULL) {
m_decomposition_strategy = new Decomposition_strategy(*m_traits);
m_decomposition_strategy = new Decomposition_strategy;
m_own_strategy = true;
}
// Obtain kernel functors.

View File

@ -30,18 +30,16 @@ namespace CGAL {
* The O(n^4) optimal strategy for decomposing a polygon into convex
* sub-polygons.
*/
template <class Kernel_,
class Container_ = std::vector<typename Kernel_::Point_2> >
template <typename Kernel_,
typename Container_ = std::vector<typename Kernel_::Point_2> >
class Optimal_convex_decomposition_2 :
public Polygon_decomposition_strategy_adapter<Kernel_, Container_,
Tag_optimal_convex_parition>
{
public:
typedef Kernel_ Kernel;
typedef CGAL::Polygon_2<Kernel, Container_> Polygon_2;
typedef typename Kernel::Point_2 Point_2;
};
/*!
@ -49,18 +47,16 @@ public:
* Hertel and Mehlhorn's O(n) approximation strategy for decomposing a
* polygon into convex sub-polygons.
*/
template <class Kernel_,
class Container_ = std::vector<typename Kernel_::Point_2> >
template <typename Kernel_,
typename Container_ = std::vector<typename Kernel_::Point_2> >
class Hertel_Mehlhorn_convex_decomposition_2 :
public Polygon_decomposition_strategy_adapter<Kernel_, Container_,
Tag_approx_convex_parition>
{
public:
typedef Kernel_ Kernel;
typedef CGAL::Polygon_2<Kernel, Container_> Polygon_2;
typedef typename Kernel::Point_2 Point_2;
};
/*!
@ -68,18 +64,16 @@ public:
* Greene's O(n log(n)) approximation strategy for decomposing a polygon into
* convex sub-polygons.
*/
template <class Kernel_,
class Container_ = std::vector<typename Kernel_::Point_2> >
template <typename Kernel_,
typename Container_ = std::vector<typename Kernel_::Point_2> >
class Greene_convex_decomposition_2 :
public Polygon_decomposition_strategy_adapter<Kernel_, Container_,
Tag_Greene_convex_parition>
{
public:
typedef Kernel_ Kernel;
typedef CGAL::Polygon_2<Kernel, Container_> Polygon_2;
typedef typename Kernel::Point_2 Point_2;
};
} //namespace CGAL

View File

@ -15,8 +15,7 @@
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// (based on an old version by Eyal Flato)
// Author(s) : Efi Fogel <efifogel@gmail.com>
#ifndef CGAL_SMALL_SIDE_ANGLE_BISECTOR_DECOMPOSITION_2_H
#define CGAL_SMALL_SIDE_ANGLE_BISECTOR_DECOMPOSITION_2_H

View File

@ -15,8 +15,9 @@
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// (based on an old version by Eyal Flato)
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// (based on an old version by Eyal Flato)
// Efi Fogel <efifogel@gmail.com>
#ifndef CGAL_SMALL_SIDE_ANGLE_BISECTOR_DECOMPOSITION_2_H
#define CGAL_SMALL_SIDE_ANGLE_BISECTOR_DECOMPOSITION_2_H
@ -31,8 +32,8 @@ namespace CGAL {
* \class
* Small-side angle-bisector decomposition strategy.
*/
template <class Kernel_,
class Container_ = std::vector<typename Kernel_::Point_2> >
template <typename Kernel_,
typename Container_ = std::vector<typename Kernel_::Point_2> >
class Small_side_angle_bisector_decomposition_2 {
public:
typedef Kernel_ Kernel;
@ -152,7 +153,7 @@ public:
* \param oi An output iterator of convex polygons.
* \return A past-the-end iterator for the sub-polygons.
*/
template <class OutputIterator>
template <typename OutputIterator>
OutputIterator operator()(const Polygon_2& pgn, OutputIterator oi) const
{
// Construct a point-info vector that represents the input polygon.
@ -493,12 +494,12 @@ private:
bool _compute_min_diagonal(Point_vector_2& vec,
unsigned int& u_ind, unsigned int& v_ind) const
{
unsigned int ind1, ind2;
Reflex_zone_position zone_pos;
unsigned int curr_count;
unsigned int min_count = 0;
bool found = false;
unsigned int dist;
unsigned int ind1, ind2;
Reflex_zone_position zone_pos;
unsigned int curr_count;
unsigned int min_count = 0;
bool found = false;
unsigned int dist;
// Let the distance between pairs of vertices we try vary between 2 and
// half the size of the polygon.
@ -564,9 +565,9 @@ private:
Point_vector_2& vec1,
Point_vector_2& vec2) const
{
unsigned int i1, i2;
Indices_iterator iter;
unsigned int reflex_count;
unsigned int i1, i2;
Indices_iterator iter;
unsigned int reflex_count;
// Make sure that the index ind2 is greater than ind1.
if (ind1 > ind2) {
@ -763,26 +764,25 @@ private:
const double succ_y = CGAL::to_double(succ_pt.y());
// Compute the edge length and the diagonal length.
const double len_uv = ::sqrt((ux - vx) * (ux - vx) +
(uy - vy) * (uy - vy));
const double len_pred = ::sqrt((pred_x - vx) * (pred_x - vx) +
(pred_y - vy) * (pred_y - vy));
const double len_succ = ::sqrt((succ_x - vx) * (succ_x - vx) +
(succ_y - vy) * (succ_y - vy));
const double len_uv =
::sqrt((ux - vx) * (ux - vx) + (uy - vy) * (uy - vy));
const double len_pred =
::sqrt((pred_x - vx) * (pred_x - vx) + (pred_y - vy) * (pred_y - vy));
const double len_succ =
::sqrt((succ_x - vx) * (succ_x - vx) + (succ_y - vy) * (succ_y - vy));
// Compute the angle a1 = <) (pred_pt, v_pt, u_pt):
const double cos_a1 = ((ux - vx) * (pred_x - vx) +
(uy - vy) * (pred_y - vy)) / (len_uv * len_pred);
double a1 = ::acos(cos_a1);
const double cos_a1 = ((ux - vx) * (pred_x - vx) +
(uy - vy) * (pred_y - vy)) / (len_uv * len_pred);
double a1 = ::acos(cos_a1);
if (f_orientation(pred_pt, v_pt, u_pt) == RIGHT_TURN)
// The angle a1 is larger than 180 degree:
a1 = _2_PI - a1;
// The angle a1 is larger than 180 degree:
if (f_orientation(pred_pt, v_pt, u_pt) == RIGHT_TURN) a1 = _2_PI - a1;
// Compute the angle a2 = <) (u_pt, v_pt, succ_pt):
const double cos_a2 = ((ux - vx) * (succ_x - vx) +
(uy - vy) * (succ_y - vy)) / (len_uv * len_succ);
double a2 = ::acos(cos_a2);
const double cos_a2 = ((ux - vx) * (succ_x - vx) +
(uy - vy) * (succ_y - vy)) / (len_uv * len_succ);
double a2 = ::acos(cos_a2);
if (f_orientation(u_pt, v_pt, succ_pt) == RIGHT_TURN)
// The angle a1 is larger than 180 degree:
@ -809,11 +809,11 @@ private:
unsigned int& reflex_ind,
unsigned int& other_ind) const
{
unsigned int ind1, ind2;
double curr_ratio;
double min_ratio = 0;
bool found = false;
unsigned int dist;
unsigned int ind1, ind2;
double curr_ratio;
double min_ratio = 0;
bool found = false;
unsigned int dist;
// Let the distance between pairs of vertices we try vary between 2 and
// the size of the polygon - 2.
@ -855,17 +855,14 @@ private:
* \param oi The output iterator.
* \return A past-the-end iterator for the sub-polygons.
*/
template <class OutputIterator>
template <typename OutputIterator>
OutputIterator _output_polygon(const Point_vector_2& vec,
OutputIterator oi) const
{
const unsigned int n = static_cast<int>(vec.size());
Polygon_2 pgn;
unsigned int k;
for (k = 0; k < n; ++k) pgn.push_back(vec[k].point);
*oi = pgn;
++oi;
const unsigned int n = static_cast<int>(vec.size());
Polygon_2 pgn;
for (unsigned int k = 0; k < n; ++k) pgn.push_back(vec[k].point);
*oi++ = pgn;
return (oi);
}
};

View File

@ -15,7 +15,8 @@
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Efi Fogel <efifogel@gmail.com>
#ifndef CGAL_MINKOWSKI_SUM_2_H
#define CGAL_MINKOWSKI_SUM_2_H