mirror of https://github.com/vitalif/openscad
Implemented non-cut projection using ClipperLib. Upgraded ClipperLib to V6
parent
eeb23d6723
commit
af59b1983c
|
@ -237,6 +237,7 @@ HEADERS += src/typedefs.h \
|
|||
src/Geometry.h \
|
||||
src/Polygon2d.h \
|
||||
src/clipper-utils.h \
|
||||
src/polyset-utils.h \
|
||||
src/polyset.h \
|
||||
src/printutils.h \
|
||||
src/fileutils.h \
|
||||
|
@ -294,6 +295,7 @@ SOURCES += src/version_check.cc \
|
|||
src/Geometry.cc \
|
||||
src/Polygon2d.cc \
|
||||
src/clipper-utils.cc \
|
||||
src/polyset-utils.cc \
|
||||
src/polyset.cc \
|
||||
src/csgops.cc \
|
||||
src/transform.cc \
|
||||
|
@ -354,6 +356,10 @@ SOURCES += src/version_check.cc \
|
|||
src/openscad.cc \
|
||||
src/mainwin.cc
|
||||
|
||||
# ClipperLib
|
||||
SOURCES += src/polyclipping/clipper.cpp
|
||||
HEADERS += src/polyclipping/clipper.hpp
|
||||
|
||||
unix:!macx {
|
||||
SOURCES += src/imageutils-lodepng.cc
|
||||
SOURCES += src/OffscreenContextGLX.cc
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "cgalutils.h"
|
||||
#include "rendernode.h"
|
||||
#include "clipper-utils.h"
|
||||
#include "polyset-utils.h"
|
||||
#include "CGALEvaluator.h"
|
||||
#include "CGALCache.h"
|
||||
#include "PolySet.h"
|
||||
|
@ -157,7 +158,10 @@ Geometry *GeometryEvaluator::applyToChildren2D(const AbstractNode &node, OpenSCA
|
|||
assert(polygons);
|
||||
// The first Clipper operation will sanitize the polygon, ensuring
|
||||
// contours/holes have the correct winding order
|
||||
ClipperLib::Polygons result = ClipperUtils::fromPolygon2d(*polygons, true);
|
||||
ClipperLib::Polygons result = ClipperUtils::fromPolygon2d(*polygons);
|
||||
result = ClipperUtils::process(result,
|
||||
ClipperLib::ctUnion,
|
||||
ClipperLib::pftEvenOdd);
|
||||
|
||||
// Add correctly winded polygons to the main clipper
|
||||
sumclipper.AddPolygons(result, first ? ClipperLib::ptSubject : ClipperLib::ptClip);
|
||||
|
@ -287,7 +291,11 @@ Response GeometryEvaluator::visit(State &state, const LeafNode &node)
|
|||
const Geometry *geometry = node.createGeometry();
|
||||
const Polygon2d *polygons = dynamic_cast<const Polygon2d*>(geometry);
|
||||
if (polygons) {
|
||||
Polygon2d *p = ClipperUtils::toPolygon2d(ClipperUtils::fromPolygon2d(*polygons, true));
|
||||
ClipperLib::Polygons result = ClipperUtils::fromPolygon2d(*polygons);
|
||||
result = ClipperUtils::process(result,
|
||||
ClipperLib::ctUnion,
|
||||
ClipperLib::pftEvenOdd);
|
||||
Polygon2d *p = ClipperUtils::toPolygon2d(result);
|
||||
delete geometry;
|
||||
geometry = p;
|
||||
}
|
||||
|
@ -626,20 +634,57 @@ Response GeometryEvaluator::visit(State &state, const ProjectionNode &node)
|
|||
if (state.isPostfix()) {
|
||||
shared_ptr<const class Geometry> geom;
|
||||
if (!isCached(node)) {
|
||||
const Geometry *geometry = applyToChildren3D(node, OPENSCAD_UNION);
|
||||
if (geometry) {
|
||||
const CGAL_Nef_polyhedron *Nptr = dynamic_cast<const CGAL_Nef_polyhedron*>(geometry);
|
||||
if (!Nptr) {
|
||||
// FIXME: delete this object
|
||||
Nptr = createNefPolyhedronFromGeometry(*geometry);
|
||||
|
||||
if (!node.cut_mode) {
|
||||
ClipperLib::Clipper sumclipper;
|
||||
BOOST_FOREACH(const ChildItem &item, this->visitedchildren[node.index()]) {
|
||||
const AbstractNode *chnode = item.first;
|
||||
const shared_ptr<const Geometry> &chgeom = item.second;
|
||||
// FIXME: Don't use deep access to modinst members
|
||||
if (chnode->modinst->isBackground()) continue;
|
||||
|
||||
|
||||
// project chgeom -> polygon2d
|
||||
shared_ptr<const PolySet> chPS = dynamic_pointer_cast<const PolySet>(chgeom);
|
||||
if (!chPS) {
|
||||
shared_ptr<const CGAL_Nef_polyhedron> chN = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(chgeom);
|
||||
if (chN) {
|
||||
chPS.reset(chN->convertToPolyset());
|
||||
}
|
||||
}
|
||||
if (chPS) {
|
||||
const Polygon2d *poly = PolysetUtils::project(*chPS);
|
||||
|
||||
ClipperLib::Polygons result = ClipperUtils::fromPolygon2d(*poly);
|
||||
// Using NonZero ensures that we don't create holes from polygons sharing
|
||||
// edges since we're unioning a mesh
|
||||
result = ClipperUtils::process(result,
|
||||
ClipperLib::ctUnion,
|
||||
ClipperLib::pftNonZero);
|
||||
// Add correctly winded polygons to the main clipper
|
||||
sumclipper.AddPolygons(result, ClipperLib::ptSubject);
|
||||
}
|
||||
}
|
||||
if (!Nptr->isNull()) {
|
||||
CGAL_Nef_polyhedron nef_poly = CGAL_project(*Nptr, node.cut_mode);
|
||||
Polygon2d *poly = nef_poly.convertToPolygon2d();
|
||||
assert(poly);
|
||||
poly->setConvexity(node.convexity);
|
||||
geom.reset(poly);
|
||||
delete geometry;
|
||||
ClipperLib::Polygons sumresult;
|
||||
sumclipper.Execute(ClipperLib::ctUnion, sumresult, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
|
||||
geom.reset(ClipperUtils::toPolygon2d(sumresult));
|
||||
}
|
||||
else {
|
||||
const Geometry *geometry = applyToChildren3D(node, OPENSCAD_UNION);
|
||||
if (geometry) {
|
||||
const CGAL_Nef_polyhedron *Nptr = dynamic_cast<const CGAL_Nef_polyhedron*>(geometry);
|
||||
if (!Nptr) {
|
||||
// FIXME: delete this object
|
||||
Nptr = createNefPolyhedronFromGeometry(*geometry);
|
||||
}
|
||||
if (!Nptr->isNull()) {
|
||||
CGAL_Nef_polyhedron nef_poly = CGAL_project(*Nptr, node.cut_mode);
|
||||
Polygon2d *poly = nef_poly.convertToPolygon2d();
|
||||
assert(poly);
|
||||
poly->setConvexity(node.convexity);
|
||||
geom.reset(poly);
|
||||
delete geometry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -334,8 +334,12 @@ Geometry *PolySetCGALEvaluator::evaluateGeometry(const LinearExtrudeNode &node)
|
|||
}
|
||||
}
|
||||
ClipperLib::Clipper clipper;
|
||||
clipper.AddPolygons(ClipperUtils::fromPolygon2d(sum, true), ClipperLib::ptSubject);
|
||||
ClipperLib::Polygons result;
|
||||
ClipperLib::Polygons result = ClipperUtils::fromPolygon2d(sum);
|
||||
clipper.AddPolygons(ClipperUtils::process(result,
|
||||
ClipperLib::ctUnion,
|
||||
ClipperLib::pftEvenOdd),
|
||||
ClipperLib::ptSubject);
|
||||
|
||||
clipper.Execute(ClipperLib::ctUnion, result);
|
||||
|
||||
if (result.size() == 0) return NULL;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "polyset.h"
|
||||
#include "printutils.h"
|
||||
#include "Polygon2d.h"
|
||||
#include "polyset-utils.h"
|
||||
|
||||
#include "cgal.h"
|
||||
#include <CGAL/convex_hull_3.h>
|
||||
|
@ -627,50 +628,16 @@ CGAL_Nef_polyhedron CGAL_project(const CGAL_Nef_polyhedron &N, bool cut)
|
|||
else {
|
||||
PolySet *ps3 = N.convertToPolyset();
|
||||
if (!ps3) return nef_poly;
|
||||
for (size_t i = 0; i < ps3->polygons.size(); i++) {
|
||||
int min_x_p = -1;
|
||||
double min_x_val = 0;
|
||||
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
|
||||
double x = ps3->polygons[i][j][0];
|
||||
if (min_x_p < 0 || x < min_x_val) {
|
||||
min_x_p = j;
|
||||
min_x_val = x;
|
||||
}
|
||||
}
|
||||
int min_x_p1 = (min_x_p+1) % ps3->polygons[i].size();
|
||||
int min_x_p2 = (min_x_p+ps3->polygons[i].size()-1) % ps3->polygons[i].size();
|
||||
double ax = ps3->polygons[i][min_x_p1][0] - ps3->polygons[i][min_x_p][0];
|
||||
double ay = ps3->polygons[i][min_x_p1][1] - ps3->polygons[i][min_x_p][1];
|
||||
double at = atan2(ay, ax);
|
||||
double bx = ps3->polygons[i][min_x_p2][0] - ps3->polygons[i][min_x_p][0];
|
||||
double by = ps3->polygons[i][min_x_p2][1] - ps3->polygons[i][min_x_p][1];
|
||||
double bt = atan2(by, bx);
|
||||
|
||||
double eps = 0.000001;
|
||||
if (fabs(at - bt) < eps || (fabs(ax) < eps && fabs(ay) < eps) ||
|
||||
(fabs(bx) < eps && fabs(by) < eps)) {
|
||||
// this triangle is degenerated in projection
|
||||
continue;
|
||||
}
|
||||
|
||||
std::list<CGAL_Nef_polyhedron2::Point> plist;
|
||||
for (size_t j = 0; j < ps3->polygons[i].size(); j++) {
|
||||
double x = ps3->polygons[i][j][0];
|
||||
double y = ps3->polygons[i][j][1];
|
||||
CGAL_Nef_polyhedron2::Point p = CGAL_Nef_polyhedron2::Point(x, y);
|
||||
if (at > bt)
|
||||
plist.push_front(p);
|
||||
else
|
||||
plist.push_back(p);
|
||||
}
|
||||
// FIXME: Should the CGAL_Nef_polyhedron2 be cached?
|
||||
if (nef_poly.isEmpty()) {
|
||||
const Polygon2d *poly = PolysetUtils::project(*ps3);
|
||||
|
||||
// FIXME: Convert back to Nef2 and delete poly?
|
||||
/* if (nef_poly.isEmpty()) {
|
||||
nef_poly.p2.reset(new CGAL_Nef_polyhedron2(plist.begin(), plist.end(), CGAL_Nef_polyhedron2::INCLUDED));
|
||||
}
|
||||
else {
|
||||
(*nef_poly.p2) += CGAL_Nef_polyhedron2(plist.begin(), plist.end(), CGAL_Nef_polyhedron2::INCLUDED);
|
||||
}
|
||||
}
|
||||
*/
|
||||
delete ps3;
|
||||
}
|
||||
return nef_poly;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
namespace ClipperUtils {
|
||||
|
||||
ClipperLib::Polygons fromPolygon2d(const Polygon2d &poly, bool sanitize) {
|
||||
ClipperLib::Polygons fromPolygon2d(const Polygon2d &poly) {
|
||||
ClipperLib::Polygons result;
|
||||
BOOST_FOREACH(const Outline2d &outline, poly.outlines()) {
|
||||
ClipperLib::Polygon p;
|
||||
|
@ -12,12 +12,6 @@ namespace ClipperUtils {
|
|||
}
|
||||
result.push_back(p);
|
||||
}
|
||||
|
||||
if (sanitize) {
|
||||
ClipperLib::Clipper clipper;
|
||||
clipper.AddPolygons(result, ClipperLib::ptSubject);
|
||||
clipper.Execute(ClipperLib::ctUnion, result, ClipperLib::pftEvenOdd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -34,4 +28,15 @@ namespace ClipperUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
ClipperLib::Polygons process(const ClipperLib::Polygons &polygons,
|
||||
ClipperLib::ClipType cliptype,
|
||||
ClipperLib::PolyFillType polytype)
|
||||
{
|
||||
ClipperLib::Polygons result;
|
||||
ClipperLib::Clipper clipper;
|
||||
clipper.AddPolygons(polygons, ClipperLib::ptSubject);
|
||||
clipper.Execute(cliptype, result, polytype);
|
||||
return result;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
#ifndef CLIPPER_UTILS_H_
|
||||
#define CLIPPER_UTILS_H_
|
||||
|
||||
#include <polyclipping/clipper.hpp>
|
||||
#include "polyclipping/clipper.hpp"
|
||||
#include "Polygon2d.h"
|
||||
|
||||
namespace ClipperUtils {
|
||||
|
||||
static const unsigned int CLIPPER_SCALE = 100000;
|
||||
|
||||
ClipperLib::Polygons fromPolygon2d(const class Polygon2d &poly, bool sanitize);
|
||||
ClipperLib::Polygons fromPolygon2d(const class Polygon2d &poly);
|
||||
Polygon2d *toPolygon2d(const ClipperLib::Polygons &poly);
|
||||
ClipperLib::Polygons process(const ClipperLib::Polygons &polygons,
|
||||
ClipperLib::ClipType, ClipperLib::PolyFillType);
|
||||
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,375 @@
|
|||
/*******************************************************************************
|
||||
* *
|
||||
* Author : Angus Johnson *
|
||||
* Version : 6.0.0 *
|
||||
* Date : 30 October 2013 *
|
||||
* 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
|
||||
|
||||
#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
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <functional>
|
||||
|
||||
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 };
|
||||
|
||||
#ifdef use_int32
|
||||
typedef int cInt;
|
||||
typedef unsigned int cUInt;
|
||||
#else
|
||||
typedef signed long long cInt;
|
||||
typedef unsigned long long cUInt;
|
||||
#endif
|
||||
|
||||
struct IntPoint {
|
||||
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;
|
||||
}
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
|
||||
#ifdef use_deprecated
|
||||
typedef signed long long long64; //backward compatibility only
|
||||
typedef Path Polygon;
|
||||
typedef Paths Polygons;
|
||||
#endif
|
||||
|
||||
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) {}
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifdef use_xyz
|
||||
typedef void (*TZFillCallback)(IntPoint& z1, IntPoint& z2, IntPoint& pt);
|
||||
#endif
|
||||
|
||||
class PolyNode;
|
||||
typedef std::vector< PolyNode* > PolyNodes;
|
||||
|
||||
class PolyNode
|
||||
{
|
||||
public:
|
||||
PolyNode();
|
||||
Path Contour;
|
||||
PolyNodes Childs;
|
||||
PolyNode* Parent;
|
||||
PolyNode* GetNext() const;
|
||||
bool IsHole() const;
|
||||
bool IsOpen() const;
|
||||
int ChildCount() const;
|
||||
private:
|
||||
bool m_IsOpen;
|
||||
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
|
||||
};
|
||||
|
||||
enum InitOptions {ioReverseSolution = 1, ioStrictlySimple = 2, ioPreserveCollinear = 4};
|
||||
enum JoinType {jtSquare, jtRound, jtMiter};
|
||||
enum EndType {etClosed, etButt, etSquare, etRound};
|
||||
|
||||
bool Orientation(const Path &poly);
|
||||
double Area(const Path &poly);
|
||||
|
||||
#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
|
||||
|
||||
void OffsetPaths(const Paths &in_polys, Paths &out_polys,
|
||||
double delta, JoinType jointype, EndType endtype, double limit = 0);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
void MinkowkiSum(const Path& poly, const Path& path, Paths& solution, bool isClosed);
|
||||
void MinkowkiDiff(const Path& poly, const Path& path, Paths& solution, bool isClosed);
|
||||
|
||||
void PolyTreeToPaths(const PolyTree& polytree, Paths& paths);
|
||||
void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths);
|
||||
void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths);
|
||||
|
||||
void ReversePath(Path& p);
|
||||
void ReversePaths(Paths& p);
|
||||
|
||||
struct IntRect { cInt left; cInt top; cInt right; cInt bottom; };
|
||||
|
||||
//enums that are used internally ...
|
||||
enum EdgeSide { esLeft = 1, esRight = 2};
|
||||
|
||||
//forward declarations (for stuff used internally) ...
|
||||
struct TEdge;
|
||||
struct IntersectNode;
|
||||
struct LocalMinima;
|
||||
struct Scanbeam;
|
||||
struct OutPt;
|
||||
struct OutRec;
|
||||
struct Join;
|
||||
|
||||
typedef std::vector < OutRec* > PolyOutList;
|
||||
typedef std::vector < TEdge* > EdgeList;
|
||||
typedef std::vector < Join* > JoinList;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//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.
|
||||
class ClipperBase
|
||||
{
|
||||
public:
|
||||
ClipperBase();
|
||||
virtual ~ClipperBase();
|
||||
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
|
||||
|
||||
virtual void Clear();
|
||||
IntRect GetBounds();
|
||||
bool PreserveCollinear() {return m_PreserveCollinear;};
|
||||
void PreserveCollinear(bool value) {m_PreserveCollinear = value;};
|
||||
protected:
|
||||
void DisposeLocalMinimaList();
|
||||
TEdge* AddBoundsToLML(TEdge *e, bool IsClosed);
|
||||
void PopLocalMinima();
|
||||
virtual void Reset();
|
||||
void InsertLocalMinima(LocalMinima *newLm);
|
||||
void DoMinimaLML(TEdge* E1, TEdge* E2, bool IsClosed);
|
||||
TEdge* DescendToMin(TEdge *&E);
|
||||
void AscendToMax(TEdge *&E, bool Appending, bool IsClosed);
|
||||
LocalMinima *m_CurrentLM;
|
||||
LocalMinima *m_MinimaList;
|
||||
bool m_UseFullRange;
|
||||
EdgeList m_edges;
|
||||
bool m_PreserveCollinear;
|
||||
bool m_HasOpenPaths;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class Clipper : public virtual ClipperBase
|
||||
{
|
||||
public:
|
||||
Clipper(int initOptions = 0);
|
||||
~Clipper();
|
||||
bool Execute(ClipType clipType,
|
||||
Paths &solution,
|
||||
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;};
|
||||
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
|
||||
protected:
|
||||
void Reset();
|
||||
virtual bool ExecuteInternal();
|
||||
private:
|
||||
PolyOutList m_PolyOuts;
|
||||
JoinList m_Joins;
|
||||
JoinList m_GhostJoins;
|
||||
ClipType m_ClipType;
|
||||
std::set< cInt, std::greater<cInt> > m_Scanbeam;
|
||||
TEdge *m_ActiveEdges;
|
||||
TEdge *m_SortedEdges;
|
||||
IntersectNode *m_IntersectNodes;
|
||||
bool m_ExecuteLocked;
|
||||
PolyFillType m_ClipFillType;
|
||||
PolyFillType m_SubjFillType;
|
||||
bool m_ReverseOutput;
|
||||
bool m_UsingPolyTree;
|
||||
bool m_StrictSimple;
|
||||
#ifdef use_xyz
|
||||
TZFillCallback m_ZFill; //custom callback
|
||||
#endif
|
||||
void SetWindingCount(TEdge& edge);
|
||||
bool IsEvenOddFillType(const TEdge& edge) const;
|
||||
bool IsEvenOddAltFillType(const TEdge& edge) const;
|
||||
void InsertScanbeam(const cInt Y);
|
||||
cInt PopScanbeam();
|
||||
void InsertLocalMinimaIntoAEL(const cInt botY);
|
||||
void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge);
|
||||
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;
|
||||
bool IsTopHorz(const cInt XPos);
|
||||
void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2);
|
||||
void DoMaxima(TEdge *e);
|
||||
void PrepareHorzJoins(TEdge* horzEdge, bool isTopOfScanbeam);
|
||||
void ProcessHorizontals(bool IsTopOfScanbeam);
|
||||
void ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam);
|
||||
void AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
|
||||
OutPt* AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
|
||||
OutRec* GetOutRec(int idx);
|
||||
void AppendPolygon(TEdge *e1, TEdge *e2);
|
||||
void IntersectEdges(TEdge *e1, TEdge *e2,
|
||||
const IntPoint &pt, bool protect = false);
|
||||
OutRec* CreateOutRec();
|
||||
OutPt* AddOutPt(TEdge *e, const IntPoint &pt);
|
||||
void DisposeAllOutRecs();
|
||||
void DisposeOutRec(PolyOutList::size_type index);
|
||||
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);
|
||||
void ProcessIntersectList();
|
||||
void ProcessEdgesAtTopOfScanbeam(const cInt topY);
|
||||
void BuildResult(Paths& polys);
|
||||
void BuildResult2(PolyTree& polytree);
|
||||
void SetHoleState(TEdge *e, OutRec *outrec);
|
||||
void DisposeIntersectNodes();
|
||||
bool FixupIntersectionOrder();
|
||||
void FixupOutPolygon(OutRec &outrec);
|
||||
bool IsHole(TEdge *e);
|
||||
void FixHoleLinkage(OutRec &outrec);
|
||||
void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt);
|
||||
void ClearJoins();
|
||||
void ClearGhostJoins();
|
||||
void AddGhostJoin(OutPt *op, const IntPoint offPt);
|
||||
bool JoinPoints(const Join *j, OutPt *&p1, OutPt *&p2);
|
||||
void JoinCommonEdges();
|
||||
void DoSimplePolygons();
|
||||
void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec);
|
||||
void FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec);
|
||||
#ifdef use_xyz
|
||||
void SetZ(IntPoint& pt, TEdge& e);
|
||||
#endif
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include "polyset-utils.h"
|
||||
#include "polyset.h"
|
||||
#include "Polygon2d.h"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
namespace PolysetUtils {
|
||||
|
||||
const Polygon2d *project(const PolySet &ps) {
|
||||
Polygon2d *poly = new Polygon2d;
|
||||
|
||||
BOOST_FOREACH(const PolySet::Polygon &p, ps.polygons) {
|
||||
// Filter away down-facing normal vectors
|
||||
if ((p[1]-p[0]).cross(p[2]-p[0])[2] <=0) continue;
|
||||
|
||||
Outline2d outline;
|
||||
BOOST_FOREACH(const Vector3d &v, p) {
|
||||
outline.push_back(Vector2d(v[0], v[1]));
|
||||
}
|
||||
poly->addOutline(outline);
|
||||
}
|
||||
return poly;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef POLYSET_UTILS_H_
|
||||
#define POLYSET_UTILS_H_
|
||||
|
||||
class Polygon2d;
|
||||
class PolySet;
|
||||
|
||||
namespace PolysetUtils {
|
||||
|
||||
const Polygon2d *project(const PolySet &ps);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -594,6 +594,8 @@ set(COMMON_SOURCES
|
|||
../src/PolySetEvaluator.cc
|
||||
../src/GeometryCache.cc
|
||||
../src/clipper-utils.cc
|
||||
../src/polyclipping/clipper.cpp
|
||||
../src/polyset-utils.cc
|
||||
../src/Tree.cc
|
||||
../src/lodepng.cpp)
|
||||
|
||||
|
|
Loading…
Reference in New Issue