Browse Source

Convincing ClipperLib to use Slic3r's own Point type internally.

Vojtech Bubnik 3 years ago
parent
commit
8d0950ce12

+ 0 - 2
CMakeLists.txt

@@ -268,8 +268,6 @@ set(LIBDIR_BIN ${CMAKE_CURRENT_BINARY_DIR}/src)
 include_directories(${LIBDIR})
 # For generated header files
 include_directories(${LIBDIR_BIN}/platform)
-# For libslic3r.h
-include_directories(${LIBDIR}/clipper)
 
 if(WIN32)
     add_definitions(-D_USE_MATH_DEFINES -D_WIN32 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)

+ 3 - 2
src/clipper/CMakeLists.txt

@@ -2,8 +2,9 @@ project(clipper)
 cmake_minimum_required(VERSION 2.6)
 
 add_library(clipper STATIC
-    clipper.cpp
-    clipper.hpp
+# We are using ClipperLib compiled as part of the libslic3r project using Slic3r::Point as its base type.
+#    clipper.cpp
+#    clipper.hpp
     clipper_z.cpp
     clipper_z.hpp
 )

+ 17 - 9
src/clipper/clipper.cpp

@@ -61,11 +61,15 @@
 	#define CLIPPERLIB_PROFILE_BLOCK(name)
 #endif
 
-#ifdef use_xyz
+#ifdef CLIPPERLIB_NAMESPACE_PREFIX
+namespace CLIPPERLIB_NAMESPACE_PREFIX {
+#endif // CLIPPERLIB_NAMESPACE_PREFIX
+
+#ifdef CLIPPERLIB_USE_XYZ
 namespace ClipperLib_Z {
-#else /* use_xyz */
+#else /* CLIPPERLIB_USE_XYZ */
 namespace ClipperLib {
-#endif /* use_xyz */
+#endif /* CLIPPERLIB_USE_XYZ */
 
 static double const pi = 3.141592653589793238;
 static double const two_pi = pi *2;
@@ -335,7 +339,7 @@ inline cInt TopX(TEdge &edge, const cInt currentY)
 
 void IntersectPoint(TEdge &Edge1, TEdge &Edge2, IntPoint &ip)
 {
-#ifdef use_xyz  
+#ifdef CLIPPERLIB_USE_XYZ  
   ip.z() = 0;
 #endif
 
@@ -467,7 +471,7 @@ inline void ReverseHorizontal(TEdge &e)
   //progression of the bounds - ie so their xbots will align with the
   //adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
   std::swap(e.Top.x(), e.Bot.x());
-#ifdef use_xyz  
+#ifdef CLIPPERLIB_USE_XYZ  
   std::swap(e.Top.z(), e.Bot.z());
 #endif
 }
@@ -1073,7 +1077,7 @@ Clipper::Clipper(int initOptions) :
   m_StrictSimple = ((initOptions & ioStrictlySimple) != 0);
   m_PreserveCollinear = ((initOptions & ioPreserveCollinear) != 0);
   m_HasOpenPaths = false;
-#ifdef use_xyz  
+#ifdef CLIPPERLIB_USE_XYZ  
   m_ZFill = 0;
 #endif
 }
@@ -1637,7 +1641,7 @@ void Clipper::DeleteFromSEL(TEdge *e)
 }
 //------------------------------------------------------------------------------
 
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
 void Clipper::SetZ(IntPoint& pt, TEdge& e1, TEdge& e2)
 {
   if (pt.z() != 0 || !m_ZFill) return;
@@ -1655,7 +1659,7 @@ void Clipper::IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &Pt)
   bool e1Contributing = ( e1->OutIdx >= 0 );
   bool e2Contributing = ( e2->OutIdx >= 0 );
 
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
         SetZ(Pt, *e1, *e2);
 #endif
 
@@ -2641,7 +2645,7 @@ void Clipper::ProcessEdgesAtTopOfScanbeam(const cInt topY)
           (ePrev->Curr.x() == e->Curr.x()) && (ePrev->WindDelta != 0))
         {
           IntPoint pt = e->Curr;
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
           SetZ(pt, *ePrev, *e);
 #endif
           OutPt* op = AddOutPt(ePrev, pt);
@@ -4204,3 +4208,7 @@ std::ostream& operator <<(std::ostream &s, const Paths &p)
 //------------------------------------------------------------------------------
 
 } //ClipperLib namespace
+
+#ifdef CLIPPERLIB_NAMESPACE_PREFIX
+} // namespace CLIPPERLIB_NAMESPACE_PREFIX
+#endif // CLIPPERLIB_NAMESPACE_PREFIX

+ 29 - 46
src/clipper/clipper.hpp

@@ -41,8 +41,8 @@
 
 #define CLIPPER_VERSION "6.2.6"
 
-//use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance.
-//#define use_xyz
+//CLIPPERLIB_USE_XYZ: adds a Z member to IntPoint. Adds a minor cost to perfomance.
+//#define CLIPPERLIB_USE_XYZ
 
 //use_lines: Enables line clipping. Adds a very minor cost to performance.
 #define use_lines
@@ -59,11 +59,15 @@
 #include <functional>
 #include <queue>
 
-#ifdef use_xyz
-namespace ClipperLib_Z {
-#else /* use_xyz */
-namespace ClipperLib {
-#endif /* use_xyz */
+#ifdef CLIPPERLIB_NAMESPACE_PREFIX
+  namespace CLIPPERLIB_NAMESPACE_PREFIX {
+#endif // CLIPPERLIB_NAMESPACE_PREFIX
+
+#ifdef CLIPPERLIB_USE_XYZ
+  namespace ClipperLib_Z {
+#else
+  namespace ClipperLib {
+#endif
 
 enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor };
 enum PolyType { ptSubject, ptClip };
@@ -90,43 +94,20 @@ enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative };
   static constexpr cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL;
 #endif // CLIPPERLIB_INT32
 
-#if 1
+#ifdef CLIPPERLIB_INTPOINT_TYPE
+using IntPoint = CLIPPERLIB_INTPOINT_TYPE;
+#else // CLIPPERLIB_INTPOINT_TYPE
 using IntPoint = Eigen::Matrix<cInt, 
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
   3
-#else // use_xyz
+#else // CLIPPERLIB_USE_XYZ
   2
-#endif // use_xyz
+#endif // CLIPPERLIB_USE_XYZ
   , 1, Eigen::DontAlign>;
+#endif // CLIPPERLIB_INTPOINT_TYPE
+
 using DoublePoint = Eigen::Matrix<double, 2, 1, Eigen::DontAlign>;
-#else
-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; 
-  }
-};
-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()) {}
-};
-#endif
 //------------------------------------------------------------------------------
 
 typedef std::vector<IntPoint> Path;
@@ -141,7 +122,7 @@ std::ostream& operator <<(std::ostream &s, const Paths &p);
 
 //------------------------------------------------------------------------------
 
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
 typedef std::function<void(const IntPoint& e1bot, const IntPoint& e1top, const IntPoint& e2bot, const IntPoint& e2top, IntPoint& pt)> ZFillCallback;
 #endif
 
@@ -282,11 +263,11 @@ enum EdgeSide { esLeft = 1, esRight = 2};
   };
 
   // Point of an output polygon.
-  // 36B on 64bit system without use_xyz.
+  // 36B on 64bit system without CLIPPERLIB_USE_XYZ.
   struct OutPt {
     // 4B
     int       Idx;
-    // 16B without use_xyz / 24B with use_xyz
+    // 16B without CLIPPERLIB_USE_XYZ / 24B with CLIPPERLIB_USE_XYZ
     IntPoint  Pt;
     // 4B on 32bit system, 8B on 64bit system
     OutPt    *Next;
@@ -381,7 +362,7 @@ public:
   bool StrictlySimple() const {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
+#ifdef CLIPPERLIB_USE_XYZ
   void ZFillFunction(ZFillCallback zFillFunc) { m_ZFill = zFillFunc; }
 #endif
 protected:
@@ -414,7 +395,7 @@ private:
   // Does the result go to a PolyTree or Paths?
   bool                  m_UsingPolyTree; 
   bool                  m_StrictSimple;
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
   ZFillCallback         m_ZFill; //custom callback 
 #endif
   void SetWindingCount(TEdge& edge) const;
@@ -467,7 +448,7 @@ private:
   void DoSimplePolygons();
   void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec) const;
   void FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec) const;
-#ifdef use_xyz
+#ifdef CLIPPERLIB_USE_XYZ
   void SetZ(IntPoint& pt, TEdge& e1, TEdge& e2);
 #endif
 };
@@ -519,6 +500,8 @@ class clipperException : public std::exception
 
 } //ClipperLib namespace
 
-#endif //clipper_hpp
-
+#ifdef CLIPPERLIB_NAMESPACE_PREFIX
+} // namespace CLIPPERLIB_NAMESPACE_PREFIX
+#endif // CLIPPERLIB_NAMESPACE_PREFIX
 
+#endif //clipper_hpp

+ 1 - 1
src/clipper/clipper_z.cpp

@@ -1,7 +1,7 @@
 // Hackish wrapper around the ClipperLib library to compile the Clipper library with the Z support.
 
 // Enable the Z coordinate support.
-#define use_xyz
+#define CLIPPERLIB_USE_XYZ
 
 // and let it compile
 #include "clipper.cpp"

+ 3 - 3
src/clipper/clipper_z.hpp

@@ -2,17 +2,17 @@
 
 #ifndef clipper_z_hpp
 #ifdef clipper_hpp
-#error "You should include the clipper_z.hpp first"
+#error "You should include clipper_z.hpp before clipper.hpp"
 #endif
 
 #define clipper_z_hpp
 
 // Enable the Z coordinate support.
-#define use_xyz
+#define CLIPPERLIB_USE_XYZ
 
 #include "clipper.hpp"
 
 #undef clipper_hpp
-#undef use_xyz
+#undef CLIPPERLIB_USE_XYZ
 
 #endif // clipper_z_hpp

+ 2 - 2
src/libnest2d/include/libnest2d/backends/libslic3r/geometries.hpp

@@ -132,14 +132,14 @@ template<>
 inline void offset(Slic3r::ExPolygon& sh, coord_t distance, const PolygonTag&)
 {
 #define DISABLE_BOOST_OFFSET
-    auto res = Slic3r::offset_ex(sh, distance, ClipperLib::jtSquare);
+    auto res = Slic3r::offset_ex(sh, distance, Slic3r::ClipperLib::jtSquare);
     if (!res.empty()) sh = res.front();
 }
 
 template<>
 inline void offset(Slic3r::Polygon& sh, coord_t distance, const PathTag&)
 {
-    auto res = Slic3r::offset(sh, distance, ClipperLib::jtSquare);
+    auto res = Slic3r::offset(sh, distance, Slic3r::ClipperLib::jtSquare);
     if (!res.empty()) sh = res.front();
 }
 

+ 2 - 2
src/libslic3r/Arrange.cpp

@@ -54,7 +54,7 @@ namespace Slic3r {
 
 template<class Tout = double, class = FloatingOnly<Tout>, int...EigenArgs>
 inline constexpr Eigen::Matrix<Tout, 2, EigenArgs...> unscaled(
-    const ClipperLib::IntPoint &v) noexcept
+    const Slic3r::ClipperLib::IntPoint &v) noexcept
 {
     return Eigen::Matrix<Tout, 2, EigenArgs...>{unscaled<Tout>(v.x()),
                                                 unscaled<Tout>(v.y())};
@@ -616,7 +616,7 @@ void arrange(ArrangePolygons &      arrangables,
              const BedT &           bed,
              const ArrangeParams &  params)
 {
-    namespace clppr = ClipperLib;
+    namespace clppr = Slic3r::ClipperLib;
     
     std::vector<Item> items, fixeditems;
     items.reserve(arrangables.size());

+ 2 - 0
src/libslic3r/CMakeLists.txt

@@ -23,6 +23,8 @@ add_library(libslic3r STATIC
     BridgeDetector.hpp
     Brim.cpp
     Brim.hpp
+    clipper.cpp
+    clipper.hpp
     ClipperUtils.cpp
     ClipperUtils.hpp
     Config.cpp

+ 3 - 3
src/libslic3r/ClipperUtils.hpp

@@ -8,9 +8,9 @@
 #include "Surface.hpp"
 
 // import these wherever we're included
-using ClipperLib::jtMiter;
-using ClipperLib::jtRound;
-using ClipperLib::jtSquare;
+using Slic3r::ClipperLib::jtMiter;
+using Slic3r::ClipperLib::jtRound;
+using Slic3r::ClipperLib::jtSquare;
 
 #define CLIPPERUTILS_UNSAFE_OFFSET
 

Some files were not shown because too many files changed in this diff