mapnik/deps/clipper/include/clipper.hpp

377 lines
14 KiB
C++
Raw Normal View History

2013-02-27 17:57:19 +01:00
/*******************************************************************************
* *
* Author : Angus Johnson *
2013-11-06 22:37:30 +01:00
* Version : 6.0.0 *
* Date : 30 October 2013 *
2013-02-27 17:57:19 +01:00
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2013 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1. *
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm: *
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
*******************************************************************************/
#ifndef clipper_hpp
#define clipper_hpp
2013-05-10 03:11:49 +02:00
#include <mapnik/config.hpp>
2013-11-06 22:37:30 +01:00
#define CLIPPER_VERSION "6.0.0"
//use_int32: When enabled 32bit ints are used instead of 64bit ints. This
//improve performance but coordinate values are limited to the range +/- 46340
//#define use_int32
//use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance.
//#define use_xyz
//use_lines: Enables line clipping. Adds a very minor cost to performance.
//#define use_lines
//When enabled, code developed with earlier versions of Clipper
//(ie prior to ver 6) should compile without changes.
//In a future update, this compatability code will be removed.
//#define use_deprecated
2013-05-10 03:11:49 +02:00
2013-02-27 17:57:19 +01:00
#include <vector>
2013-11-06 22:37:30 +01:00
#include <set>
2013-02-27 17:57:19 +01:00
#include <stdexcept>
#include <cstring>
#include <cstdlib>
#include <ostream>
2013-11-06 22:37:30 +01:00
#include <functional>
2013-02-27 17:57:19 +01:00
namespace ClipperLib {
enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor };
enum PolyType { ptSubject, ptClip };
//By far the most widely used winding rules for polygon filling are
//EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32)
//Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL)
//see http://glprogramming.com/red/chapter11.html
enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative };
2013-11-06 22:37:30 +01:00
#ifdef use_int32
typedef int cInt;
typedef unsigned int cUInt;
#else
typedef signed long long cInt;
typedef unsigned long long cUInt;
#endif
2013-02-27 17:57:19 +01:00
struct IntPoint {
2013-11-06 22:37:30 +01:00
cInt X;
cInt Y;
#ifdef use_xyz
cInt Z;
IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {};
#else
IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {};
#endif
friend inline bool operator== (const IntPoint& a, const IntPoint& b)
{
return a.X == b.X && a.Y == b.Y;
}
friend inline bool operator!= (const IntPoint& a, const IntPoint& b)
{
return a.X != b.X || a.Y != b.Y;
}
2013-02-27 17:57:19 +01:00
};
2013-11-06 22:37:30 +01:00
//------------------------------------------------------------------------------
typedef std::vector< IntPoint > Path;
typedef std::vector< Path > Paths;
inline Path& operator <<(Path& poly, const IntPoint& p) {poly.push_back(p); return poly;}
inline Paths& operator <<(Paths& polys, const Path& p) {polys.push_back(p); return polys;}
std::ostream& operator <<(std::ostream &s, const IntPoint &p);
std::ostream& operator <<(std::ostream &s, const Path &p);
std::ostream& operator <<(std::ostream &s, const Paths &p);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
#ifdef use_deprecated
typedef signed long long long64; //backward compatibility only
typedef Path Polygon;
typedef Paths Polygons;
#endif
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
struct DoublePoint
{
double X;
double Y;
DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
};
//------------------------------------------------------------------------------
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
#ifdef use_xyz
typedef void (*TZFillCallback)(IntPoint& z1, IntPoint& z2, IntPoint& pt);
#endif
2013-02-27 17:57:19 +01:00
class PolyNode;
typedef std::vector< PolyNode* > PolyNodes;
class PolyNode
{
public:
PolyNode();
2013-11-06 22:37:30 +01:00
Path Contour;
2013-02-27 17:57:19 +01:00
PolyNodes Childs;
PolyNode* Parent;
PolyNode* GetNext() const;
bool IsHole() const;
2013-11-06 22:37:30 +01:00
bool IsOpen() const;
2013-02-27 17:57:19 +01:00
int ChildCount() const;
private:
2013-11-06 22:37:30 +01:00
bool m_IsOpen;
2013-02-27 17:57:19 +01:00
PolyNode* GetNextSiblingUp() const;
unsigned Index; //node index in Parent.Childs
void AddChild(PolyNode& child);
friend class Clipper; //to access Index
};
class PolyTree: public PolyNode
{
public:
~PolyTree(){Clear();};
PolyNode* GetFirst() const;
void Clear();
int Total() const;
private:
PolyNodes AllNodes;
friend class Clipper; //to access AllNodes
};
2013-11-06 22:37:30 +01:00
enum InitOptions {ioReverseSolution = 1, ioStrictlySimple = 2, ioPreserveCollinear = 4};
enum JoinType {jtSquare, jtRound, jtMiter};
enum EndType {etClosed, etButt, etSquare, etRound};
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
bool Orientation(const Path &poly);
double Area(const Path &poly);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
#ifdef use_deprecated
void OffsetPolygons(const Polygons &in_polys, Polygons &out_polys,
double delta, JoinType jointype = jtSquare, double limit = 0, bool autoFix = true);
void PolyTreeToPolygons(const PolyTree& polytree, Paths& paths);
void ReversePolygon(Path& p);
void ReversePolygons(Paths& p);
#endif
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void OffsetPaths(const Paths &in_polys, Paths &out_polys,
double delta, JoinType jointype, EndType endtype, double limit = 0);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
void SimplifyPolygons(Paths &polys, PolyFillType fillType = pftEvenOdd);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void CleanPolygon(const Path& in_poly, Path& out_poly, double distance = 1.415);
void CleanPolygon(Path& poly, double distance = 1.415);
void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance = 1.415);
void CleanPolygons(Paths& polys, double distance = 1.415);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void MinkowkiSum(const Path& poly, const Path& path, Paths& solution, bool isClosed);
void MinkowkiDiff(const Path& poly, const Path& path, Paths& solution, bool isClosed);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void PolyTreeToPaths(const PolyTree& polytree, Paths& paths);
void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths);
void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
void ReversePath(Path& p);
void ReversePaths(Paths& p);
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
struct IntRect { cInt left; cInt top; cInt right; cInt bottom; };
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
//enums that are used internally ...
enum EdgeSide { esLeft = 1, esRight = 2};
2013-02-27 17:57:19 +01:00
2013-11-06 22:37:30 +01:00
//forward declarations (for stuff used internally) ...
struct TEdge;
struct IntersectNode;
struct LocalMinima;
struct Scanbeam;
struct OutPt;
struct OutRec;
struct Join;
2013-02-27 17:57:19 +01:00
typedef std::vector < OutRec* > PolyOutList;
typedef std::vector < TEdge* > EdgeList;
2013-11-06 22:37:30 +01:00
typedef std::vector < Join* > JoinList;
//------------------------------------------------------------------------------
2013-02-27 17:57:19 +01:00
//ClipperBase is the ancestor to the Clipper class. It should not be
//instantiated directly. This class simply abstracts the conversion of sets of
//polygon coordinates into edge objects that are stored in a LocalMinima list.
2013-05-10 03:11:49 +02:00
class MAPNIK_DECL ClipperBase
2013-02-27 17:57:19 +01:00
{
public:
ClipperBase();
virtual ~ClipperBase();
2013-11-06 22:37:30 +01:00
bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed);
bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed);
#ifdef use_deprecated
bool AddPolygon(const Path &pg, PolyType PolyTyp);
bool AddPolygons(const Paths &ppg, PolyType PolyTyp);
#endif
2013-02-27 17:57:19 +01:00
virtual void Clear();
IntRect GetBounds();
2013-11-06 22:37:30 +01:00
bool PreserveCollinear() {return m_PreserveCollinear;};
void PreserveCollinear(bool value) {m_PreserveCollinear = value;};
2013-02-27 17:57:19 +01:00
protected:
void DisposeLocalMinimaList();
2013-11-06 22:37:30 +01:00
TEdge* AddBoundsToLML(TEdge *e, bool IsClosed);
2013-02-27 17:57:19 +01:00
void PopLocalMinima();
virtual void Reset();
void InsertLocalMinima(LocalMinima *newLm);
2013-11-06 22:37:30 +01:00
void DoMinimaLML(TEdge* E1, TEdge* E2, bool IsClosed);
TEdge* DescendToMin(TEdge *&E);
void AscendToMax(TEdge *&E, bool Appending, bool IsClosed);
2013-02-27 17:57:19 +01:00
LocalMinima *m_CurrentLM;
LocalMinima *m_MinimaList;
bool m_UseFullRange;
EdgeList m_edges;
2013-11-06 22:37:30 +01:00
bool m_PreserveCollinear;
bool m_HasOpenPaths;
2013-02-27 17:57:19 +01:00
};
2013-11-06 22:37:30 +01:00
//------------------------------------------------------------------------------
2013-02-27 17:57:19 +01:00
2013-05-10 03:11:49 +02:00
class MAPNIK_DECL Clipper : public virtual ClipperBase
2013-02-27 17:57:19 +01:00
{
public:
2013-11-06 22:37:30 +01:00
Clipper(int initOptions = 0);
2013-02-27 17:57:19 +01:00
~Clipper();
bool Execute(ClipType clipType,
2013-11-06 22:37:30 +01:00
Paths &solution,
2013-02-27 17:57:19 +01:00
PolyFillType subjFillType = pftEvenOdd,
PolyFillType clipFillType = pftEvenOdd);
bool Execute(ClipType clipType,
PolyTree &polytree,
PolyFillType subjFillType = pftEvenOdd,
PolyFillType clipFillType = pftEvenOdd);
void Clear();
bool ReverseSolution() {return m_ReverseOutput;};
void ReverseSolution(bool value) {m_ReverseOutput = value;};
2013-11-06 22:37:30 +01:00
bool StrictlySimple() {return m_StrictSimple;};
void StrictlySimple(bool value) {m_StrictSimple = value;};
//set the callback function for z value filling on intersections (otherwise Z is 0)
#ifdef use_xyz
void ZFillFunction(TZFillCallback zFillFunc);
#endif
2013-02-27 17:57:19 +01:00
protected:
void Reset();
virtual bool ExecuteInternal();
private:
PolyOutList m_PolyOuts;
JoinList m_Joins;
2013-11-06 22:37:30 +01:00
JoinList m_GhostJoins;
2013-02-27 17:57:19 +01:00
ClipType m_ClipType;
2013-11-06 22:37:30 +01:00
std::set< cInt, std::greater<cInt> > m_Scanbeam;
2013-02-27 17:57:19 +01:00
TEdge *m_ActiveEdges;
TEdge *m_SortedEdges;
IntersectNode *m_IntersectNodes;
bool m_ExecuteLocked;
PolyFillType m_ClipFillType;
PolyFillType m_SubjFillType;
bool m_ReverseOutput;
bool m_UsingPolyTree;
2013-11-06 22:37:30 +01:00
bool m_StrictSimple;
#ifdef use_xyz
TZFillCallback m_ZFill; //custom callback
#endif
2013-02-27 17:57:19 +01:00
void SetWindingCount(TEdge& edge);
bool IsEvenOddFillType(const TEdge& edge) const;
bool IsEvenOddAltFillType(const TEdge& edge) const;
2013-11-06 22:37:30 +01:00
void InsertScanbeam(const cInt Y);
cInt PopScanbeam();
void InsertLocalMinimaIntoAEL(const cInt botY);
void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge);
2013-02-27 17:57:19 +01:00
void AddEdgeToSEL(TEdge *edge);
void CopyAELToSEL();
void DeleteFromSEL(TEdge *e);
void DeleteFromAEL(TEdge *e);
void UpdateEdgeIntoAEL(TEdge *&e);
void SwapPositionsInSEL(TEdge *edge1, TEdge *edge2);
bool IsContributing(const TEdge& edge) const;
2013-11-06 22:37:30 +01:00
bool IsTopHorz(const cInt XPos);
2013-02-27 17:57:19 +01:00
void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2);
2013-11-06 22:37:30 +01:00
void DoMaxima(TEdge *e);
void PrepareHorzJoins(TEdge* horzEdge, bool isTopOfScanbeam);
void ProcessHorizontals(bool IsTopOfScanbeam);
void ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam);
2013-02-27 17:57:19 +01:00
void AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
2013-11-06 22:37:30 +01:00
OutPt* AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
2013-05-10 03:11:49 +02:00
OutRec* GetOutRec(int idx);
2013-02-27 17:57:19 +01:00
void AppendPolygon(TEdge *e1, TEdge *e2);
void IntersectEdges(TEdge *e1, TEdge *e2,
2013-11-06 22:37:30 +01:00
const IntPoint &pt, bool protect = false);
2013-02-27 17:57:19 +01:00
OutRec* CreateOutRec();
2013-11-06 22:37:30 +01:00
OutPt* AddOutPt(TEdge *e, const IntPoint &pt);
void DisposeAllOutRecs();
2013-02-27 17:57:19 +01:00
void DisposeOutRec(PolyOutList::size_type index);
2013-11-06 22:37:30 +01:00
bool ProcessIntersections(const cInt botY, const cInt topY);
void InsertIntersectNode(TEdge *e1, TEdge *e2, const IntPoint &pt);
void BuildIntersectList(const cInt botY, const cInt topY);
2013-02-27 17:57:19 +01:00
void ProcessIntersectList();
2013-11-06 22:37:30 +01:00
void ProcessEdgesAtTopOfScanbeam(const cInt topY);
void BuildResult(Paths& polys);
2013-02-27 17:57:19 +01:00
void BuildResult2(PolyTree& polytree);
2013-05-10 03:11:49 +02:00
void SetHoleState(TEdge *e, OutRec *outrec);
2013-02-27 17:57:19 +01:00
void DisposeIntersectNodes();
bool FixupIntersectionOrder();
2013-05-10 03:11:49 +02:00
void FixupOutPolygon(OutRec &outrec);
2013-02-27 17:57:19 +01:00
bool IsHole(TEdge *e);
2013-05-10 03:11:49 +02:00
void FixHoleLinkage(OutRec &outrec);
2013-11-06 22:37:30 +01:00
void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt);
2013-02-27 17:57:19 +01:00
void ClearJoins();
2013-11-06 22:37:30 +01:00
void ClearGhostJoins();
void AddGhostJoin(OutPt *op, const IntPoint offPt);
bool JoinPoints(const Join *j, OutPt *&p1, OutPt *&p2);
2013-02-27 17:57:19 +01:00
void JoinCommonEdges();
2013-05-10 03:11:49 +02:00
void DoSimplePolygons();
2013-02-27 17:57:19 +01:00
void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec);
void FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec);
2013-11-06 22:37:30 +01:00
#ifdef use_xyz
void SetZ(IntPoint& pt, TEdge& e);
#endif
2013-02-27 17:57:19 +01:00
};
//------------------------------------------------------------------------------
class clipperException : public std::exception
{
public:
clipperException(const char* description): m_descr(description) {}
virtual ~clipperException() throw() {}
virtual const char* what() const throw() {return m_descr.c_str();}
private:
std::string m_descr;
};
//------------------------------------------------------------------------------
} //ClipperLib namespace
#endif //clipper_hpp