Added new implementation of the sweep line algorithm.

This commit is contained in:
Tali Zvi 2002-12-01 10:26:46 +00:00
parent 0e3e39bd9b
commit f78d341cf5
4 changed files with 1568 additions and 0 deletions

View File

@ -0,0 +1,214 @@
// ======================================================================
//
// Copyright (c) 1997 The CGAL Consortium
//
// This software and related documentation is part of an INTERNAL release
// of the Computational Geometry Algorithms Library (CGAL). It is not
// intended for general use.
//
// ----------------------------------------------------------------------
//
// release : $CGAL_Revision: CGAL-2.3-I-44 $
// release_date : $CGAL_Date: 2001/03/09 $
//
// file : include/CGAL/Sweep_line_event.h
// package : arr (1.87)
// maintainer : Tali Zvi <talizvi@post.tau.ac.il>
// source :
// revision :
// revision_date :
// author(s) : Tali Zvi <talizvi@post.tau.ac.il>
//
//
// coordinator : Tel-Aviv University (Dan Halperin <halperin@math.tau.ac.il>)
//
// Chapter :
// ======================================================================
#ifndef CGAL_SWEEP_LINE_EVENT_H
#define CGAL_SWEEP_LINE_EVENT_H
#include <CGAL/Sweep_line_2/Sweep_line_subcurve.h>
#include <CGAL/Sweep_line_2/Sweep_line_functors.h>
#include <vector>
#include <set>
CGAL_BEGIN_NAMESPACE
template<class SweepLineTraits_2>
class Sweep_line_event
{
public:
typedef SweepLineTraits_2 Traits;
typedef typename Traits::X_curve_2 X_curve_2;
typedef typename Traits::Point_2 Point_2;
#ifdef OLD_IMPL
typedef Sweep_line_subcurve<Traits> SubCurve;
typedef Curve_less_functor<Traits> CurveLess;
typedef typename std::set<SubCurve *, CurveLess> SubcurveContainer;
typedef typename SubcurveContainer::iterator SubCurveIter;
Sweep_line_event(const Point_2 &point, Traits *traits) {
CurveLess lessFunc(traits);
m_leftCurves = new SubcurveContainer(lessFunc);
m_rightCurves = new SubcurveContainer(lessFunc);
m_point = point;
}
#else
typedef Sweep_line_subcurve<Traits> SubCurve;
typedef typename std::list<SubCurve *> SubcurveContainer;
typedef typename SubcurveContainer::iterator SubCurveIter;
Sweep_line_event(const Point_2 &point, Traits *traits) {
m_leftCurves = new SubcurveContainer();
m_rightCurves = new SubcurveContainer();
m_point = point;
m_traits = traits;
}
#endif
~Sweep_line_event() {
delete m_leftCurves;
delete m_rightCurves;
}
void addCurveToLeft(SubCurve *curve)
{
if (m_leftCurves->empty())
m_leftCurves->push_back(curve);
else
{
SubCurveIter iter = m_leftCurves->begin();
while ( iter != m_leftCurves->end() &&
m_traits->curve_compare_at_x(curve->getCurve(),
(*iter)->getCurve(),
*(curve->getReferencePoint()))
== LARGER)
{
++iter;
}
if ( iter == m_leftCurves->end() ||
!m_traits->curve_is_same((*iter)->getCurve(), curve->getCurve()))
m_leftCurves->insert(iter, curve);
}
}
void addCurveToRight(SubCurve *curve)
{
if (m_rightCurves->empty())
m_rightCurves->push_back(curve);
else
{
SubCurveIter iter = m_rightCurves->begin();
while ( iter != m_rightCurves->end() &&
m_traits->curve_compare_at_x_right(curve->getCurve(),
(*iter)->getCurve(),
*(curve->getReferencePoint()))
== LARGER)
{
++iter;
}
if ( iter == m_rightCurves->end() ||
!m_traits->curve_is_same((*iter)->getCurve(), curve->getCurve()))
{
m_rightCurves->insert(iter, curve);
}
}
}
SubCurveIter leftCurvesBegin() {
return m_leftCurves->begin();
}
SubCurveIter leftCurvesEnd() {
return m_leftCurves->end();
}
SubCurveIter rightCurvesBegin() {
return m_rightCurves->begin();
}
SubCurveIter rightCurvesEnd() {
return m_rightCurves->end();
}
int getNumRightCurves() {
return m_rightCurves->size();
}
bool hasLeftCurves() {
return !m_leftCurves->empty();
}
const Point_2 &getPoint() {
return m_point;
}
void Print();
private:
Traits *m_traits;
SubcurveContainer *m_leftCurves;
SubcurveContainer *m_rightCurves;
/*! this is true only if this is a right end of one of the original curves */
bool m_isEndPoint;
Point_2 m_point;
};
template<class SweepLineTraits_2>
void
Sweep_line_event<SweepLineTraits_2>::
Print()
{
std::cout << "\tLeft curves: \n" ;
for ( SubCurveIter iter = m_leftCurves->begin() ;
iter != m_leftCurves->end() ; ++iter )
{
const X_curve_2 &c = (*iter)->getCurve();
std::cout << "\t(" << c << ") \n";
}
std::cout << std::endl;
std::cout << "\tRight curves: \n" ;
for ( SubCurveIter iter = m_rightCurves->begin() ;
iter != m_rightCurves->end() ; ++iter )
{
const X_curve_2 &c = (*iter)->getCurve();
std::cout << "\t(" << c << ") \n";
}
std::cout << std::endl;
}
/*
void addCurveToLeft(SubCurve *curve) {
#ifndef VERBOSE
m_leftCurves->insert(curve);
#else
std::pair<SubCurveIter, bool> res = m_leftCurves->insert(curve);
if (res.second!=true)
std::cout << "Warning: addCurveToLeft\n";
#endif
}
void addCurveToRight(SubCurve *curve) {
#ifndef VERBOSE
m_rightCurves->insert(curve);
#else
std::pair<SubCurveIter, bool> res = m_rightCurves->insert(curve);
if (res.second!=true)
std::cout << "Warning: addCurveToRight\n";
#endif
}
*/
CGAL_END_NAMESPACE
#endif // CGAL_SWEEP_LINE_EVENT_H

View File

@ -0,0 +1,148 @@
// ======================================================================
//
// Copyright (c) 1997 The CGAL Consortium
//
// This software and related documentation is part of an INTERNAL release
// of the Computational Geometry Algorithms Library (CGAL). It is not
// intended for general use.
//
// ----------------------------------------------------------------------
//
// release : $CGAL_Revision: CGAL-2.3-I-44 $
// release_date : $CGAL_Date: 2001/03/09 $
//
// file : include/CGAL/SL.h
// package : arr (1.87)
// maintainer : Tali Zvi <talizvi@post.tau.ac.il>
// source :
// revision :
// revision_date :
// author(s) : Tali Zvi <talizvi@post.tau.ac.il>
//
//
// coordinator : Tel-Aviv University (Dan Halperin <halperin@math.tau.ac.il>)
//
// Chapter :
// ======================================================================
#ifndef CGAL_SWEEP_LINE_FUNCTORS_H
#define CGAL_SWEEP_LINE_FUNCTORS_H
CGAL_BEGIN_NAMESPACE
template <class Point, class SweepLineTraits_2>
class Point_less_functor
{
public:
typedef SweepLineTraits_2 Traits;
Point_less_functor(Traits *traits) : m_traits(traits) {}
bool operator()(const Point& p1, const Point& p2) const {
Comparison_result rx = m_traits->compare_x(p1,p2);
if (rx == SMALLER)
return true;
if (rx == LARGER)
return false;
Comparison_result ry = m_traits->compare_y(p1,p2);
if (ry == SMALLER)
return true;
else if (ry == LARGER)
return false;
return false; // get here only if p1 == p2.
}
private:
/*! a pointer to a trits class */
Traits *m_traits;
};
template <class SweepLineTraits_2>
class Curve_less_functor
{
public:
typedef SweepLineTraits_2 Traits;
typedef typename Traits::Point_2 Point_2;
typedef typename Traits::X_curve_2 X_curve_2;
typedef Sweep_line_subcurve<Traits> Subcurve;
Curve_less_functor(Traits *traits) : m_traits(traits) {}
bool operator()(const Subcurve* c1, const Subcurve* c2) const {
const Point_2 *p = c1->getReferencePoint();
if ( !m_traits->curve_is_in_x_range(c1->getCurve(), *p))
p = &(c1->getLastPoint());
if ( !m_traits->curve_is_in_x_range(c2->getCurve(), *p))
p = &(c2->getLastPoint());
Comparison_result r =
m_traits->curve_compare_at_x_right(c1->getCurve(),
c2->getCurve(),
*p);
if ( r == SMALLER) {
return true;
}
return false;
}
void setReference(Point_2 point) {
m_point = point;
}
private:
/*! a pointer to a traits class */
Traits *m_traits;
};
template <class SweepLineTraits_2>
class Status_line_curve_less_functor
{
public:
typedef SweepLineTraits_2 Traits;
typedef typename Traits::Point_2 Point_2;
typedef typename Traits::X_curve_2 X_curve_2;
typedef Sweep_line_subcurve<Traits> Subcurve;
Status_line_curve_less_functor(Traits *traits) : m_traits(traits) {}
bool operator()(const Subcurve* c1, const Subcurve* c2) const {
const Point_2 *p = c1->getReferencePoint();
#if 0
std::cout << "\t\tComparing between:" << *p << "\n"
<< "\t\t " << c1->getCurve() << "\n"
<< "\t\t " << c2->getCurve() << "\n";
#endif
Comparison_result r =
m_traits->curve_compare_at_x_right(c1->getCurve(),
c2->getCurve(),
*p);
if ( r == SMALLER) {
//std::cout << "at_x_right SMALLER\n";
return true;
}
if ( r == LARGER ) {
//std::cout << "at_x_right LARGER\n";
return false;
}
//std::cout << "at_x_right EQUAL\n";
return false;
}
void setReference(Point_2 point) {
m_point = point;
}
private:
/*! a pointer to a traits class */
Traits *m_traits;
};
CGAL_END_NAMESPACE
#endif // CGAL_SWEEP_LINE_FUNCTORS_H

View File

@ -0,0 +1,136 @@
// ======================================================================
//
// Copyright (c) 1997 The CGAL Consortium
//
// This software and related documentation is part of an INTERNAL release
// of the Computational Geometry Algorithms Library (CGAL). It is not
// intended for general use.
//
// ----------------------------------------------------------------------
//
// release : $CGAL_Revision: CGAL-2.3-I-44 $
// release_date : $CGAL_Date: 2001/03/09 $
//
// file : include/CGAL/Sweep_line_event.h
// package : arr (1.87)
// maintainer : Tali Zvi <talizvi@post.tau.ac.il>
// source :
// revision :
// revision_date :
// author(s) : Tali Zvi <talizvi@post.tau.ac.il>
//
//
// coordinator : Tel-Aviv University (Dan Halperin <halperin@math.tau.ac.il>)
//
// Chapter :
// ======================================================================
#ifndef CGAL_SWEEP_LINE_SUBCURVE_H
#define CGAL_SWEEP_LINE_SUBCURVE_H
#include <vector>
CGAL_BEGIN_NAMESPACE
template<class SweepLineTraits_2>
class Sweep_line_subcurve
{
public:
typedef SweepLineTraits_2 Traits;
typedef typename Traits::Point_2 Point_2;
typedef typename Traits::Curve_2 Curve_2;
typedef typename Traits::X_curve_2 X_curve_2;
Sweep_line_subcurve(X_curve_2 &curve, Point_2 *reference,
SweepLineTraits_2 *traits);
const X_curve_2 &getCurve() const {
return m_curve;
}
void setCurve(X_curve_2 &curve) {
m_curve = curve;
}
const Point_2 *getReferencePoint() const {
return m_referencePoint;
}
const Point_2 &getLastPoint() const { return m_lastPoint; }
void setLastPoint(const Point_2 &point) { m_lastPoint = point; }
const X_curve_2 &getLastCurve() const { return m_lastCurve; }
void setLastCurve(const X_curve_2 &cv) { m_lastCurve = cv; }
bool isSourceLeftToTarget() const { return m_isRightSide; }
bool isSource(const Point_2 &p) {
return m_traits->point_is_same(p, m_source);
}
bool isTarget(const Point_2 &p) {
return m_traits->point_is_same(p, m_target);
}
/*! returns true if the specified point is the source or the target
of the curve. Returns false otherwise.
*/
bool isEndPoint(const Point_2 &p) {
return isTarget(p) || isSource(p);
}
/*! returns true if the last point is an end point and the specified
point is an end point. Otherwise returns false;
*/
bool isUnsplitCurve(const Point_2 &p) {
if ( isEndPoint(p) && isEndPoint(m_lastPoint) )
return true;
return false;
}
void Print() const;
private:
Traits *m_traits;
X_curve_2 m_curve;
Point_2 *m_referencePoint;
Point_2 m_lastPoint;
X_curve_2 m_lastCurve;
bool m_isRightSide;
Point_2 m_source;
Point_2 m_target;
};
template<class SweepLineTraits_2>
inline Sweep_line_subcurve<SweepLineTraits_2>::
Sweep_line_subcurve(X_curve_2 &curve, Point_2 *reference,
SweepLineTraits_2 *traits) : m_traits(traits)
{
m_curve = curve;
m_referencePoint = reference;
m_source = traits->curve_source(curve);
m_target = traits->curve_target(curve);
if ( traits->compare_x(m_source, m_target) == LARGER )
{
m_lastPoint = m_target; //traits->curve_target(curve);
m_isRightSide = false;
}
else
{
m_lastPoint = m_source; //traits->curve_source(curve);
m_isRightSide = true;
}
}
template<class SweepLineTraits_2>
void
Sweep_line_subcurve<SweepLineTraits_2>::
Print() const
{
std::cout << "Curve (" << m_curve << ") "
<< "p = (" << m_lastPoint << ")" << std::endl;
}
CGAL_END_NAMESPACE
#endif // CGAL_SWEEP_LINE_SUBCURVE_H

File diff suppressed because it is too large Load Diff