Skip to content

Commit

Permalink
Another minor code tidy (C++)
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusJohnson committed Jan 23, 2023
1 parent bde0477 commit be8ccf9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
40 changes: 24 additions & 16 deletions CPP/Clipper2Lib/include/clipper2/clipper.engine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 22 January 2023 *
* Date : 23 January 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -175,7 +175,9 @@ namespace Clipper2Lib {
#endif

typedef std::vector<HorzSegment> HorzSegmentList;

typedef std::unique_ptr<LocalMinima> LocalMinima_ptr;
typedef std::vector<LocalMinima_ptr> LocalMinimaList;
typedef std::vector<IntersectNode> IntersectNodeList;

// ClipperBase -------------------------------------------------------------

Expand All @@ -189,11 +191,11 @@ namespace Clipper2Lib {
bool using_polytree_ = false;
Active* actives_ = nullptr;
Active *sel_ = nullptr;
std::vector<LocalMinima*> minima_list_; //pointers in case of memory reallocs
std::vector<LocalMinima*>::iterator current_locmin_iter_;
LocalMinimaList minima_list_; //pointers in case of memory reallocs
LocalMinimaList::iterator current_locmin_iter_;
std::vector<Vertex*> vertex_lists_;
std::priority_queue<int64_t> scanline_list_;
std::vector<IntersectNode> intersect_nodes_;
IntersectNodeList intersect_nodes_;
HorzSegmentList horz_seg_list_;
std::vector<HorzJoin> horz_join_list_;
void Reset();
Expand Down Expand Up @@ -282,7 +284,7 @@ namespace Clipper2Lib {
PolyPath* parent_;
public:
PolyPath(PolyPath* parent = nullptr): parent_(parent){}
virtual ~PolyPath() { Clear(); };
//virtual ~PolyPath() {};
//https://en.cppreference.com/w/cpp/language/rule_of_three
PolyPath(const PolyPath&) = delete;
PolyPath& operator=(const PolyPath&) = delete;
Expand All @@ -297,7 +299,7 @@ namespace Clipper2Lib {

virtual PolyPath* AddChild(const Path64& path) = 0;

virtual void Clear() {};
virtual void Clear() = 0;
virtual size_t Count() const { return 0; }

const PolyPath* Parent() const { return parent_; }
Expand All @@ -310,18 +312,21 @@ namespace Clipper2Lib {
}
};

typedef typename std::vector<std::unique_ptr<PolyPath64>>::const_iterator PolyPath64_itor;
typedef typename std::vector<std::unique_ptr<PolyPathD>>::const_iterator PolyPathD_itor;
typedef typename std::vector<std::unique_ptr<PolyPath64>> PolyPath64List;
typedef typename std::vector<std::unique_ptr<PolyPathD>> PolyPathDList;

class PolyPath64 : public PolyPath {
private:
std::vector<std::unique_ptr<PolyPath64>> childs_;
PolyPath64List childs_;
Path64 polygon_;
public:
explicit PolyPath64(PolyPath64* parent = nullptr) : PolyPath(parent) {}
~PolyPath64() {
childs_.resize(0);
}
PolyPath64* operator [] (size_t index) const { return childs_[index].get(); } const
PolyPath64_itor begin() const { return childs_.cbegin(); }
PolyPath64_itor end() const { return childs_.cend(); }
PolyPath64List::const_iterator begin() const { return childs_.cbegin(); }
PolyPath64List::const_iterator end() const { return childs_.cend(); }

PolyPath64* AddChild(const Path64& path) override
{
Expand Down Expand Up @@ -354,20 +359,23 @@ namespace Clipper2Lib {

class PolyPathD : public PolyPath {
private:
std::vector<std::unique_ptr<PolyPathD>> childs_;
PolyPathDList childs_;
double inv_scale_;
PathD polygon_;
public:
explicit PolyPathD(PolyPathD* parent = nullptr) : PolyPath(parent)
{
inv_scale_ = parent ? parent->inv_scale_ : 1.0;
}
PolyPathD* operator [] (size_t index)
~PolyPathD() {
childs_.resize(0);
}
PolyPathD* operator [] (size_t index)
{
return childs_[index].get();
}
PolyPathD_itor begin() const { return childs_.cbegin(); }
PolyPathD_itor end() const { return childs_.cend(); }
PolyPathDList::const_iterator begin() const { return childs_.cbegin(); }
PolyPathDList::const_iterator end() const { return childs_.cend(); }

void SetInvScale(double value) { inv_scale_ = value; }
double InvScale() { return inv_scale_; }
Expand Down
10 changes: 5 additions & 5 deletions CPP/Clipper2Lib/include/clipper2/clipper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 22 January 2023 *
* Date : 23 January 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This module provides a simple interface to the Clipper Library *
Expand Down Expand Up @@ -565,7 +565,7 @@ namespace Clipper2Lib {
preamble += (!last_child) ? "| " : " ";
if (pp.Count())
{
PolyPath64_itor it = pp.begin();
PolyPath64List::const_iterator it = pp.begin();
for (; it < pp.end() - 1; ++it)
OutlinePolyPath64(os, **it, preamble, false);
OutlinePolyPath64(os, **it, preamble, true);
Expand All @@ -579,7 +579,7 @@ namespace Clipper2Lib {
preamble += (!last_child) ? "| " : " ";
if (pp.Count())
{
PolyPathD_itor it = pp.begin();
PolyPathDList::const_iterator it = pp.begin();
for (; it < pp.end() - 1; ++it)
OutlinePolyPathD(os, **it, preamble, false);
OutlinePolyPathD(os, **it, preamble, true);
Expand All @@ -590,7 +590,7 @@ namespace Clipper2Lib {

inline std::ostream& operator<< (std::ostream& os, const PolyTree64& pp)
{
PolyPath64_itor it = pp.begin();
PolyPath64List::const_iterator it = pp.begin();
for (; it < pp.end() - 1; ++it)
details::OutlinePolyPath64(os, **it, " ", false);
details::OutlinePolyPath64(os, **it, " ", true);
Expand All @@ -601,7 +601,7 @@ namespace Clipper2Lib {

inline std::ostream& operator<< (std::ostream& os, const PolyTreeD& pp)
{
PolyPathD_itor it = pp.begin();
PolyPathDList::const_iterator it = pp.begin();
for (; it < pp.end() - 1; ++it)
details::OutlinePolyPathD(os, **it, " ", false);
details::OutlinePolyPathD(os, **it, " ", true);
Expand Down
19 changes: 10 additions & 9 deletions CPP/Clipper2Lib/src/clipper.engine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 22 January 2023 *
* Date : 23 January 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand All @@ -13,6 +13,7 @@
#include <vector>
#include <numeric>
#include <algorithm>

#include "clipper2/clipper.engine.h"

// https://github.com/AngusJohnson/Clipper2/discussions/334
Expand Down Expand Up @@ -56,7 +57,8 @@ namespace Clipper2Lib {
};

struct LocMinSorter {
inline bool operator()(const LocalMinima* locMin1, const LocalMinima* locMin2)
inline bool operator()(const LocalMinima_ptr& locMin1,
const LocalMinima_ptr& locMin2)
{
if (locMin2->vertex->pt.y != locMin1->vertex->pt.y)
return locMin2->vertex->pt.y < locMin1->vertex->pt.y;
Expand Down Expand Up @@ -512,8 +514,8 @@ namespace Clipper2Lib {
scanline_list_ = std::priority_queue<int64_t>();
intersect_nodes_.clear();
DisposeAllOutRecs();
horz_seg_list_.resize(0);
horz_join_list_.resize(0);
horz_seg_list_.clear();
horz_join_list_.clear();
}


Expand All @@ -534,7 +536,7 @@ namespace Clipper2Lib {
std::sort(minima_list_.begin(), minima_list_.end(), LocMinSorter());
minima_list_sorted_ = true;
}
std::vector<LocalMinima*>::const_reverse_iterator i;
LocalMinimaList::const_reverse_iterator i;
for (i = minima_list_.rbegin(); i != minima_list_.rend(); ++i)
InsertScanline((*i)->vertex->pt.y);

Expand Down Expand Up @@ -704,7 +706,7 @@ namespace Clipper2Lib {
bool ClipperBase::PopLocalMinima(int64_t y, LocalMinima*& local_minima)
{
if (current_locmin_iter_ == minima_list_.end() || (*current_locmin_iter_)->vertex->pt.y != y) return false;
local_minima = (*current_locmin_iter_++);
local_minima = (current_locmin_iter_++)->get();
return true;
}

Expand All @@ -720,7 +722,6 @@ namespace Clipper2Lib {

void ClipperBase::DisposeVerticesAndLocalMinima()
{
for (auto lm : minima_list_) delete lm;
minima_list_.clear();
for (auto v : vertex_lists_) delete[] v;
vertex_lists_.clear();
Expand All @@ -733,7 +734,7 @@ namespace Clipper2Lib {
if ((VertexFlags::LocalMin & vert.flags) != VertexFlags::None) return;

vert.flags = (vert.flags | VertexFlags::LocalMin);
minima_list_.push_back(new LocalMinima(&vert, polytype, is_open));
minima_list_.push_back(std::make_unique <LocalMinima>(&vert, polytype, is_open));
}

bool ClipperBase::IsContributingClosed(const Active& e) const
Expand Down Expand Up @@ -2269,7 +2270,7 @@ namespace Clipper2Lib {
//Now as we process these intersections, we must sometimes adjust the order
//to ensure that intersecting edges are always adjacent ...

std::vector<IntersectNode>::iterator node_iter, node_iter2;
IntersectNodeList::iterator node_iter, node_iter2;
for (node_iter = intersect_nodes_.begin();
node_iter != intersect_nodes_.end(); ++node_iter)
{
Expand Down

0 comments on commit be8ccf9

Please sign in to comment.