Skip to content
This repository was archived by the owner on Oct 28, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .github/workflows/actions_csharp.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ option(BUILD_SHARED_LIBS "Build shared libs" OFF)
set(CLIPPER2_USINGZ "ON" CACHE STRING "Build Clipper2Z, either \"ON\" or \"OFF\" or \"ONLY\"")
# CLIPPER2_MAX_DECIMAL_PRECISION: maximum decimal precision when scaling PathsD to Paths64.
# Caution: excessive scaling will increase the likelihood of integer overflow errors.
# See https://www.angusj.com/clipper2/Docs/Overview.htm#coord_range for more info.
# See https://github.com/PolyClipping/Clipper2/wiki/Overview#coord_range for more info.
set(CLIPPER2_MAX_DECIMAL_PRECISION 8 CACHE STRING "Maximum decimal precision range")

##########################################################################
Expand Down
2 changes: 1 addition & 1 deletion CPP/Clipper2.pc.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: Clipper2@PCFILE_LIB_SUFFIX@
Description: A Polygon Clipping and Offsetting library in C++
Version: @PROJECT_VERSION@
URL: https://github.com/AngusJohnson/Clipper2
URL: https://github.com/PolyClipping/Clipper2
Requires:
Libs: -L${libdir} -lClipper2@PCFILE_LIB_SUFFIX@
Cflags: -I${includedir}
87 changes: 86 additions & 1 deletion CPP/Clipper2Lib/include/clipper2/clipper.engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,75 @@
#include <queue>
#include <functional>
#include <memory>
#include <new>

namespace Clipper2Lib {

// Allocates objects from larger chunks of memory so that they can all be
// released together (by Clear) instead of being allocated and deleted
// one at a time. Clear doesn't call destructors, so any objects that need
// destroying must be destroyed by their owner first.
template <typename T>
class ObjectPool {
public:
ObjectPool() = default;
ObjectPool(const ObjectPool&) = delete;
ObjectPool& operator=(const ObjectPool&) = delete;
~ObjectPool() { Clear(); }

// sets the size of the first chunk (when the pool is empty), so that
// small clipping operations don't allocate more than they need
void SetFirstChunkSize(size_t n) { first_chunk_size_ = n; }

template <typename... Args>
T* New(Args&&... args)
{
if (next_ == end_) AddChunk();
return new (next_++) T(std::forward<Args>(args)...);
}

void Clear()
{
while (last_chunk_)
{
Chunk* prev = last_chunk_->prev;
::operator delete(last_chunk_);
last_chunk_ = prev;
}
last_chunk_size_ = 0;
next_ = nullptr;
end_ = nullptr;
}

private:
// each chunk starts with a link to the previous one, followed by its objects
struct Chunk { Chunk* prev; };
static constexpr size_t header_size_ =
(sizeof(Chunk) + alignof(T) - 1) / alignof(T) * alignof(T);
static constexpr size_t max_chunk_size_ = 4096;
Chunk* last_chunk_ = nullptr;
size_t first_chunk_size_ = 32;
size_t last_chunk_size_ = 0;
T* next_ = nullptr;
T* end_ = nullptr;

void AddChunk()
{
// the first chunk is sized by SetFirstChunkSize, and later chunks
// double in size (from 32 to max_chunk_size_ objects)
size_t n = last_chunk_size_ ? last_chunk_size_ * 2 : first_chunk_size_;
if (last_chunk_size_ && n < 32) n = 32;
else if (n < 1) n = 1;
if (n > max_chunk_size_) n = max_chunk_size_;
Chunk* chunk = static_cast<Chunk*>(::operator new(header_size_ + n * sizeof(T)));
chunk->prev = last_chunk_;
last_chunk_ = chunk;
last_chunk_size_ = n;
next_ = reinterpret_cast<T*>(reinterpret_cast<char*>(chunk) + header_size_);
end_ = next_ + n;
}
};

struct Scanline;
struct IntersectNode;
struct Active;
Expand Down Expand Up @@ -161,6 +227,8 @@ namespace Clipper2Lib {
};

#ifdef USINGZ
// Z callbacks must not change the floating point rounding mode because
// edge positions rounded before a callback can be reused after it.
typedef std::function<void(const Point64& e1bot, const Point64& e1top,
const Point64& e2bot, const Point64& e2top, Point64& pt)> ZCallback64;

Expand Down Expand Up @@ -195,6 +263,9 @@ namespace Clipper2Lib {
FillRule fillrule_ = FillRule::EvenOdd;
FillRule fillpos = FillRule::Positive;
int64_t bot_y_ = 0;
// Every active edge's curr_x is already TopX(top_y). Set by DoIntersections
// and only valid for the DoTopOfScanbeam call that immediately follows it.
bool curr_x_at_top_ = false;
bool minima_list_sorted_ = false;
bool using_polytree_ = false;
Active* actives_ = nullptr;
Expand All @@ -208,6 +279,9 @@ namespace Clipper2Lib {
std::vector<HorzJoin> horz_join_list_;
void Reset();
inline void InsertScanline(int64_t y);
inline bool PeekLocMinY(int64_t &y) const;
// A y that comes from a local minima isn't consumed here. It stays pending
// until PopLocalMinima, so InsertLocalMinimaIntoAEL must run before the next pop.
inline bool PopScanline(int64_t &y);
inline bool PopLocalMinima(int64_t y, LocalMinima*& local_minima);
void DisposeAllOutRecs();
Expand All @@ -226,7 +300,7 @@ namespace Clipper2Lib {
inline void UpdateEdgeIntoAEL(Active *e);
void IntersectEdges(Active &e1, Active &e2, const Point64& pt);
inline void DeleteFromAEL(Active &e);
inline void AdjustCurrXAndCopyToSEL(const int64_t top_y);
inline bool AdjustCurrXAndCopyToSEL(const int64_t top_y);
void DoIntersections(const int64_t top_y);
void AddNewIntersectNode(Active &e1, Active &e2, const int64_t top_y);
bool BuildIntersectList(const int64_t top_y);
Expand Down Expand Up @@ -262,6 +336,17 @@ namespace Clipper2Lib {
bool has_open_paths_ = false;
bool succeeded_ = true;
OutRecList outrec_list_; //pointers in case list memory reallocated
// all OutPt, OutRec and Active objects are owned by these and released
// in CleanUp
ObjectPool<OutPt> outpt_pool_;
ObjectPool<OutRec> outrec_pool_;
ObjectPool<Active> active_pool_;
std::vector<Active*> free_actives_; // deleted actives, ready for reuse
std::vector<OutPt*> recheck_list_; // used by CleanCollinear
OutPt* NewOutPt(const Point64& pt, OutRec* outrec) { return outpt_pool_.New(pt, outrec); }
inline Active* NewActive();
inline void PoolDeletedActive(Active& e);
OutPt* DuplicateOp(OutPt* op, bool insert_after);
bool ExecuteInternal(ClipType ct, FillRule ft, bool use_polytrees);
void CleanCollinear(OutRec* outrec);
bool CheckBounds(OutRec* outrec);
Expand Down
Loading