mirror of https://github.com/axmolengine/axmol.git
Improve clipper2 cmake
This commit is contained in:
parent
dcdbc83725
commit
7f33df2640
|
@ -37,7 +37,7 @@
|
|||
|
||||
## Clipper2
|
||||
- [![Upstream](https://img.shields.io/github/v/tag/AngusJohnson/Clipper2?label=Upstream)](https://github.com/AngusJohnson/Clipper2)
|
||||
- Version: 1.1.1
|
||||
- Version: git 1.1.1-df1c129
|
||||
- License: BSL-1.0
|
||||
|
||||
## ConvertUTF
|
||||
|
|
|
@ -4,17 +4,8 @@ set(target_name ${lib_name})
|
|||
|
||||
project(${lib_name})
|
||||
|
||||
add_library(${target_name} STATIC
|
||||
clipper.core.h
|
||||
clipper.engine.cpp
|
||||
clipper.engine.h
|
||||
clipper.export.h
|
||||
clipper.h
|
||||
clipper.minkowski.h
|
||||
clipper.offset.cpp
|
||||
clipper.offset.h
|
||||
clipper.rectclip.cpp
|
||||
clipper.rectclip.h
|
||||
)
|
||||
file(GLOB_RECURSE CLIPPER_SOURCES *.h;*.cpp)
|
||||
|
||||
target_include_directories(${target_name} PUBLIC .)
|
||||
add_library(${target_name} STATIC ${CLIPPER_SOURCES})
|
||||
|
||||
target_include_directories(${target_name} PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include")
|
||||
|
|
|
@ -1,513 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 14 January 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2022 *
|
||||
* Purpose : FAST rectangular clipping *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
||||
#include <cmath>
|
||||
#include "clipper.h"
|
||||
#include "clipper.rectclip.h"
|
||||
|
||||
namespace Clipper2Lib {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Miscellaneous methods
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
inline PointInPolygonResult Path1ContainsPath2(const Path64& path1, const Path64& path2)
|
||||
{
|
||||
PointInPolygonResult result = PointInPolygonResult::IsOn;
|
||||
for(const Point64& pt : path2)
|
||||
{
|
||||
result = PointInPolygon(pt, path1);
|
||||
if (result != PointInPolygonResult::IsOn) break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool GetLocation(const Rect64& rec,
|
||||
const Point64& pt, Location& loc)
|
||||
{
|
||||
if (pt.x == rec.left && pt.y >= rec.top && pt.y <= rec.bottom)
|
||||
{
|
||||
loc = Location::Left;
|
||||
return false;
|
||||
}
|
||||
else if (pt.x == rec.right && pt.y >= rec.top && pt.y <= rec.bottom)
|
||||
{
|
||||
loc = Location::Right;
|
||||
return false;
|
||||
}
|
||||
else if (pt.y == rec.top && pt.x >= rec.left && pt.x <= rec.right)
|
||||
{
|
||||
loc = Location::Top;
|
||||
return false;
|
||||
}
|
||||
else if (pt.y == rec.bottom && pt.x >= rec.left && pt.x <= rec.right)
|
||||
{
|
||||
loc = Location::Bottom;
|
||||
return false;
|
||||
}
|
||||
else if (pt.x < rec.left) loc = Location::Left;
|
||||
else if (pt.x > rec.right) loc = Location::Right;
|
||||
else if (pt.y < rec.top) loc = Location::Top;
|
||||
else if (pt.y > rec.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool GetIntersection(const Path64& rectPath,
|
||||
const Point64& p, const Point64& p2, Location& loc, Point64& ip)
|
||||
{
|
||||
// gets the intersection closest to 'p'
|
||||
// when Result = false, loc will remain unchanged
|
||||
switch (loc)
|
||||
{
|
||||
case Location::Left:
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
else if (p.y < rectPath[0].y &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Top:
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
else if (p.x < rectPath[0].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (p.x > rectPath[1].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Right:
|
||||
if (SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
else if (p.y < rectPath[0].y &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Bottom:
|
||||
if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
else if (p.x < rectPath[3].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (p.x > rectPath[2].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
default: // loc == rInside
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline Location GetAdjacentLocation(Location loc, bool isClockwise)
|
||||
{
|
||||
int delta = (isClockwise) ? 1 : 3;
|
||||
return static_cast<Location>((static_cast<int>(loc) + delta) % 4);
|
||||
}
|
||||
|
||||
inline bool HeadingClockwise(Location prev, Location curr)
|
||||
{
|
||||
return (static_cast<int>(prev) + 1) % 4 == static_cast<int>(curr);
|
||||
}
|
||||
|
||||
inline bool AreOpposites(Location prev, Location curr)
|
||||
{
|
||||
return abs(static_cast<int>(prev) - static_cast<int>(curr)) == 2;
|
||||
}
|
||||
|
||||
inline bool IsClockwise(Location prev, Location curr,
|
||||
const Point64& prev_pt, const Point64& curr_pt, const Point64& rect_mp)
|
||||
{
|
||||
if (AreOpposites(prev, curr))
|
||||
return CrossProduct(prev_pt, rect_mp, curr_pt) < 0;
|
||||
else
|
||||
return HeadingClockwise(prev, curr);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// RectClip64
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
void RectClip::AddCorner(Location prev, Location curr)
|
||||
{
|
||||
if (HeadingClockwise(prev, curr))
|
||||
result_.push_back(rectPath_[static_cast<int>(prev)]);
|
||||
else
|
||||
result_.push_back(rectPath_[static_cast<int>(curr)]);
|
||||
}
|
||||
|
||||
void RectClip::AddCorner(Location& loc, bool isClockwise)
|
||||
{
|
||||
if (isClockwise)
|
||||
{
|
||||
result_.push_back(rectPath_[static_cast<int>(loc)]);
|
||||
loc = GetAdjacentLocation(loc, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
loc = GetAdjacentLocation(loc, false);
|
||||
result_.push_back(rectPath_[static_cast<int>(loc)]);
|
||||
}
|
||||
}
|
||||
|
||||
void RectClip::GetNextLocation(const Path64& path,
|
||||
Location& loc, int& i, int highI)
|
||||
{
|
||||
switch (loc)
|
||||
{
|
||||
case Location::Left:
|
||||
while (i <= highI && path[i].x <= rect_.left) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Top:
|
||||
while (i <= highI && path[i].y <= rect_.top) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Right:
|
||||
while (i <= highI && path[i].x >= rect_.right) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Bottom:
|
||||
while (i <= highI && path[i].y >= rect_.bottom) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Inside:
|
||||
while (i <= highI)
|
||||
{
|
||||
if (path[i].x < rect_.left) loc = Location::Left;
|
||||
else if (path[i].x > rect_.right) loc = Location::Right;
|
||||
else if (path[i].y > rect_.bottom) loc = Location::Bottom;
|
||||
else if (path[i].y < rect_.top) loc = Location::Top;
|
||||
else { result_.push_back(path[i]); ++i; continue; }
|
||||
break; //inner loop
|
||||
}
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
Path64 RectClip::Execute(const Path64& path)
|
||||
{
|
||||
if (rect_.IsEmpty() || path.size() < 3) return Path64();
|
||||
|
||||
result_.clear();
|
||||
start_locs_.clear();
|
||||
int i = 0, highI = static_cast<int>(path.size()) - 1;
|
||||
Location prev = Location::Inside, loc;
|
||||
Location crossing_loc = Location::Inside;
|
||||
Location first_cross_ = Location::Inside;
|
||||
if (!GetLocation(rect_, path[highI], loc))
|
||||
{
|
||||
i = highI - 1;
|
||||
while (i >= 0 && !GetLocation(rect_, path[i], prev)) --i;
|
||||
if (i < 0) return path;
|
||||
if (prev == Location::Inside) loc = Location::Inside;
|
||||
i = 0;
|
||||
}
|
||||
Location starting_loc = loc;
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
while (i <= highI)
|
||||
{
|
||||
prev = loc;
|
||||
Location crossing_prev = crossing_loc;
|
||||
|
||||
GetNextLocation(path, loc, i, highI);
|
||||
|
||||
if (i > highI) break;
|
||||
Point64 ip, ip2;
|
||||
Point64 prev_pt = (i) ? path[static_cast<size_t>(i - 1)] : path[highI];
|
||||
|
||||
crossing_loc = loc;
|
||||
if (!GetIntersection(rectPath_, path[i], prev_pt, crossing_loc, ip))
|
||||
{
|
||||
// ie remaining outside
|
||||
|
||||
if (crossing_prev == Location::Inside)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, loc, prev_pt, path[i], mp_);
|
||||
do {
|
||||
start_locs_.push_back(prev);
|
||||
prev = GetAdjacentLocation(prev, isClockw);
|
||||
} while (prev != loc);
|
||||
crossing_loc = crossing_prev; // still not crossed
|
||||
}
|
||||
else if (prev != Location::Inside && prev != loc)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, loc, prev_pt, path[i], mp_);
|
||||
do {
|
||||
AddCorner(prev, isClockw);
|
||||
} while (prev != loc);
|
||||
}
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// we must be crossing the rect boundary to get here
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
if (loc == Location::Inside) // path must be entering rect
|
||||
{
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
first_cross_ = crossing_loc;
|
||||
start_locs_.push_back(prev);
|
||||
}
|
||||
else if (prev != crossing_loc)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, crossing_loc, prev_pt, path[i], mp_);
|
||||
do {
|
||||
AddCorner(prev, isClockw);
|
||||
} while (prev != crossing_loc);
|
||||
}
|
||||
}
|
||||
else if (prev != Location::Inside)
|
||||
{
|
||||
// passing right through rect. 'ip' here will be the second
|
||||
// intersect pt but we'll also need the first intersect pt (ip2)
|
||||
loc = prev;
|
||||
GetIntersection(rectPath_, prev_pt, path[i], loc, ip2);
|
||||
if (crossing_prev != Location::Inside)
|
||||
AddCorner(crossing_prev, loc);
|
||||
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
first_cross_ = loc;
|
||||
start_locs_.push_back(prev);
|
||||
}
|
||||
|
||||
loc = crossing_loc;
|
||||
result_.push_back(ip2);
|
||||
if (ip == ip2)
|
||||
{
|
||||
// it's very likely that path[i] is on rect
|
||||
GetLocation(rect_, path[i], loc);
|
||||
AddCorner(crossing_loc, loc);
|
||||
crossing_loc = loc;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else // path must be exiting rect
|
||||
{
|
||||
loc = crossing_loc;
|
||||
if (first_cross_ == Location::Inside)
|
||||
first_cross_ = crossing_loc;
|
||||
}
|
||||
|
||||
result_.push_back(ip);
|
||||
|
||||
} //while i <= highI
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
if (starting_loc == Location::Inside) return path;
|
||||
Rect64 tmp_rect = Bounds(path);
|
||||
if (tmp_rect.Contains(rect_) &&
|
||||
Path1ContainsPath2(path, rectPath_) !=
|
||||
PointInPolygonResult::IsOutside) return rectPath_;
|
||||
else
|
||||
return Path64();
|
||||
}
|
||||
|
||||
if (loc != Location::Inside &&
|
||||
(loc != first_cross_ || start_locs_.size() > 2))
|
||||
{
|
||||
if (start_locs_.size() > 0)
|
||||
{
|
||||
prev = loc;
|
||||
for (auto loc2 : start_locs_)
|
||||
{
|
||||
if (prev == loc2) continue;
|
||||
AddCorner(prev, HeadingClockwise(prev, loc2));
|
||||
prev = loc2;
|
||||
}
|
||||
loc = prev;
|
||||
}
|
||||
if (loc != first_cross_)
|
||||
AddCorner(loc, HeadingClockwise(loc, first_cross_));
|
||||
}
|
||||
|
||||
if (result_.size() < 3) return Path64();
|
||||
|
||||
// tidy up duplicates and collinear segments
|
||||
Path64 res;
|
||||
res.reserve(result_.size());
|
||||
size_t k = 0; highI = static_cast<int>(result_.size()) - 1;
|
||||
Point64 prev_pt = result_[highI];
|
||||
res.push_back(result_[0]);
|
||||
Path64::const_iterator cit;
|
||||
for (cit = result_.cbegin() + 1; cit != result_.cend(); ++cit)
|
||||
{
|
||||
if (CrossProduct(prev_pt, res[k], *cit))
|
||||
{
|
||||
prev_pt = res[k++];
|
||||
res.push_back(*cit);
|
||||
}
|
||||
else
|
||||
res[k] = *cit;
|
||||
}
|
||||
|
||||
if (k < 2) return Path64();
|
||||
// and a final check for collinearity
|
||||
else if (!CrossProduct(res[0], res[k - 1], res[k])) res.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
Paths64 RectClipLines::Execute(const Path64& path)
|
||||
{
|
||||
result_.clear();
|
||||
Paths64 result;
|
||||
if (rect_.IsEmpty() || path.size() == 0) return result;
|
||||
|
||||
int i = 1, highI = static_cast<int>(path.size()) - 1;
|
||||
|
||||
Location prev = Location::Inside, loc;
|
||||
Location crossing_loc;
|
||||
if (!GetLocation(rect_, path[0], loc))
|
||||
{
|
||||
while (i <= highI && !GetLocation(rect_, path[i], prev)) ++i;
|
||||
if (i > highI) {
|
||||
result.push_back(path);
|
||||
return result;
|
||||
}
|
||||
if (prev == Location::Inside) loc = Location::Inside;
|
||||
i = 1;
|
||||
}
|
||||
if (loc == Location::Inside) result_.push_back(path[0]);
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
while (i <= highI)
|
||||
{
|
||||
prev = loc;
|
||||
GetNextLocation(path, loc, i, highI);
|
||||
if (i > highI) break;
|
||||
Point64 ip, ip2;
|
||||
Point64 prev_pt = path[static_cast<size_t>(i - 1)];
|
||||
|
||||
crossing_loc = loc;
|
||||
if (!GetIntersection(rectPath_, path[i], prev_pt, crossing_loc, ip))
|
||||
{
|
||||
// ie remaining outside
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// we must be crossing the rect boundary to get here
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
if (loc == Location::Inside) // path must be entering rect
|
||||
{
|
||||
result_.push_back(ip);
|
||||
}
|
||||
else if (prev != Location::Inside)
|
||||
{
|
||||
// passing right through rect. 'ip' here will be the second
|
||||
// intersect pt but we'll also need the first intersect pt (ip2)
|
||||
crossing_loc = prev;
|
||||
GetIntersection(rectPath_, prev_pt, path[i], crossing_loc, ip2);
|
||||
result_.push_back(ip2);
|
||||
result_.push_back(ip);
|
||||
result.push_back(result_);
|
||||
result_.clear();
|
||||
}
|
||||
else // path must be exiting rect
|
||||
{
|
||||
result_.push_back(ip);
|
||||
result.push_back(result_);
|
||||
result_.clear();
|
||||
}
|
||||
} //while i <= highI
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
if (result_.size() > 1)
|
||||
result.push_back(result_);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -1,50 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 26 October 2022 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2022 *
|
||||
* Purpose : FAST rectangular clipping *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef CLIPPER_RECTCLIP_H
|
||||
#define CLIPPER_RECTCLIP_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include "clipper.h"
|
||||
#include "clipper.core.h"
|
||||
|
||||
namespace Clipper2Lib
|
||||
{
|
||||
|
||||
enum class Location { Left, Top, Right, Bottom, Inside };
|
||||
|
||||
class RectClip {
|
||||
protected:
|
||||
const Rect64 rect_;
|
||||
const Point64 mp_;
|
||||
const Path64 rectPath_;
|
||||
Path64 result_;
|
||||
std::vector<Location> start_locs_;
|
||||
|
||||
void GetNextLocation(const Path64& path,
|
||||
Location& loc, int& i, int highI);
|
||||
void AddCorner(Location prev, Location curr);
|
||||
void AddCorner(Location& loc, bool isClockwise);
|
||||
public:
|
||||
explicit RectClip(const Rect64& rect) :
|
||||
rect_(rect),
|
||||
mp_(rect.MidPoint()),
|
||||
rectPath_(rect.AsPath()) {}
|
||||
Path64 Execute(const Path64& path);
|
||||
};
|
||||
|
||||
class RectClipLines : public RectClip {
|
||||
public:
|
||||
explicit RectClipLines(const Rect64& rect) : RectClip(rect) {};
|
||||
Paths64 Execute(const Path64& path);
|
||||
};
|
||||
|
||||
} // Clipper2Lib namespace
|
||||
#endif // CLIPPER_RECTCLIP_H
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 26 January 2023 *
|
||||
* Date : 21 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : Core Clipper Library structures and functions *
|
||||
|
@ -17,11 +17,12 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <numeric>
|
||||
|
||||
namespace Clipper2Lib
|
||||
{
|
||||
|
||||
#ifdef __cpp_exceptions
|
||||
#if __cpp_exceptions
|
||||
|
||||
class Clipper2Exception : public std::exception {
|
||||
public:
|
||||
|
@ -36,17 +37,47 @@ namespace Clipper2Lib
|
|||
"Precision exceeds the permitted range";
|
||||
static const char* range_error =
|
||||
"Values exceed permitted range";
|
||||
|
||||
static const char* scale_error =
|
||||
"Invalid scale (either 0 or too large)";
|
||||
static const char* non_pair_error =
|
||||
"There must be 2 values for each coordinate";
|
||||
#endif
|
||||
|
||||
// error codes (2^n)
|
||||
const int precision_error_i = 1; // non-fatal
|
||||
const int scale_error_i = 2; // non-fatal
|
||||
const int non_pair_error_i = 4; // non-fatal
|
||||
const int range_error_i = 64;
|
||||
|
||||
static const double PI = 3.141592653589793238;
|
||||
static const int64_t MAX_COORD = INT64_MAX >> 2;
|
||||
static const int64_t MIN_COORD = -MAX_COORD;
|
||||
static const int64_t INVALID = INT64_MAX;
|
||||
const double max_coord = static_cast<double>(MAX_COORD);
|
||||
const double min_coord = static_cast<double>(MIN_COORD);
|
||||
|
||||
static const double MAX_DBL = (std::numeric_limits<double>::max)();
|
||||
static const double MIN_DBL = (std::numeric_limits<double>::min)();
|
||||
|
||||
static void DoError(int error_code)
|
||||
{
|
||||
#if __cpp_exceptions
|
||||
switch (error_code)
|
||||
{
|
||||
case precision_error_i:
|
||||
throw Clipper2Exception(precision_error);
|
||||
case scale_error_i:
|
||||
throw Clipper2Exception(scale_error);
|
||||
case non_pair_error_i:
|
||||
throw Clipper2Exception(non_pair_error);
|
||||
case range_error_i:
|
||||
throw Clipper2Exception(range_error);
|
||||
}
|
||||
#else
|
||||
++error_code; // only to stop compiler warning
|
||||
#endif
|
||||
}
|
||||
|
||||
//By far the most widely used filling rules for polygons are EvenOdd
|
||||
//and NonZero, sometimes called Alternate and Winding respectively.
|
||||
//https://en.wikipedia.org/wiki/Nonzero-rule
|
||||
|
@ -214,6 +245,18 @@ namespace Clipper2Lib
|
|||
right(r),
|
||||
bottom(b) {}
|
||||
|
||||
Rect(bool is_valid)
|
||||
{
|
||||
if (is_valid)
|
||||
{
|
||||
left = right = top = bottom = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = top = std::numeric_limits<T>::max();
|
||||
right = bottom = -std::numeric_limits<int64_t>::max();
|
||||
}
|
||||
}
|
||||
|
||||
T Width() const { return right - left; }
|
||||
T Height() const { return bottom - top; }
|
||||
|
@ -258,8 +301,8 @@ namespace Clipper2Lib
|
|||
|
||||
bool Intersects(const Rect<T>& rec) const
|
||||
{
|
||||
return ((std::max)(left, rec.left) < (std::min)(right, rec.right)) &&
|
||||
((std::max)(top, rec.top) < (std::min)(bottom, rec.bottom));
|
||||
return ((std::max)(left, rec.left) <= (std::min)(right, rec.right)) &&
|
||||
((std::max)(top, rec.top) <= (std::min)(bottom, rec.bottom));
|
||||
};
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Rect<T>& rect) {
|
||||
|
@ -299,7 +342,7 @@ namespace Clipper2Lib
|
|||
static const RectD MaxInvalidRectD = RectD(
|
||||
MAX_DBL, MAX_DBL, MIN_DBL, MIN_DBL);
|
||||
|
||||
inline Rect64 Bounds(const Path64& path)
|
||||
inline Rect64 GetBounds(const Path64& path)
|
||||
{
|
||||
Rect64 rec = MaxInvalidRect64;
|
||||
for (const Point64& pt : path)
|
||||
|
@ -309,11 +352,11 @@ namespace Clipper2Lib
|
|||
if (pt.y < rec.top) rec.top = pt.y;
|
||||
if (pt.y > rec.bottom) rec.bottom = pt.y;
|
||||
}
|
||||
if (rec.IsEmpty()) return Rect64();
|
||||
if (rec.left == INT64_MAX) return Rect64();
|
||||
return rec;
|
||||
}
|
||||
|
||||
inline Rect64 Bounds(const Paths64& paths)
|
||||
inline Rect64 GetBounds(const Paths64& paths)
|
||||
{
|
||||
Rect64 rec = MaxInvalidRect64;
|
||||
for (const Path64& path : paths)
|
||||
|
@ -324,11 +367,11 @@ namespace Clipper2Lib
|
|||
if (pt.y < rec.top) rec.top = pt.y;
|
||||
if (pt.y > rec.bottom) rec.bottom = pt.y;
|
||||
}
|
||||
if (rec.IsEmpty()) return Rect64();
|
||||
if (rec.left == INT64_MAX) return Rect64();
|
||||
return rec;
|
||||
}
|
||||
|
||||
inline RectD Bounds(const PathD& path)
|
||||
inline RectD GetBounds(const PathD& path)
|
||||
{
|
||||
RectD rec = MaxInvalidRectD;
|
||||
for (const PointD& pt : path)
|
||||
|
@ -338,11 +381,11 @@ namespace Clipper2Lib
|
|||
if (pt.y < rec.top) rec.top = pt.y;
|
||||
if (pt.y > rec.bottom) rec.bottom = pt.y;
|
||||
}
|
||||
if (rec.IsEmpty()) return RectD();
|
||||
if (rec.left == MAX_DBL) return RectD();
|
||||
return rec;
|
||||
}
|
||||
|
||||
inline RectD Bounds(const PathsD& paths)
|
||||
inline RectD GetBounds(const PathsD& paths)
|
||||
{
|
||||
RectD rec = MaxInvalidRectD;
|
||||
for (const PathD& path : paths)
|
||||
|
@ -353,7 +396,7 @@ namespace Clipper2Lib
|
|||
if (pt.y < rec.top) rec.top = pt.y;
|
||||
if (pt.y > rec.bottom) rec.bottom = pt.y;
|
||||
}
|
||||
if (rec.IsEmpty()) return RectD();
|
||||
if (rec.left == MAX_DBL) return RectD();
|
||||
return rec;
|
||||
}
|
||||
|
||||
|
@ -381,9 +424,19 @@ namespace Clipper2Lib
|
|||
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline Path<T1> ScalePath(const Path<T2>& path, double scale_x, double scale_y)
|
||||
inline Path<T1> ScalePath(const Path<T2>& path,
|
||||
double scale_x, double scale_y, int& error_code)
|
||||
{
|
||||
Path<T1> result;
|
||||
if (scale_x == 0 || scale_y == 0)
|
||||
{
|
||||
error_code |= scale_error_i;
|
||||
DoError(scale_error_i);
|
||||
// if no exception, treat as non-fatal error
|
||||
if (scale_x == 0) scale_x = 1.0;
|
||||
if (scale_y == 0) scale_y = 1.0;
|
||||
}
|
||||
|
||||
result.reserve(path.size());
|
||||
#ifdef USINGZ
|
||||
std::transform(path.begin(), path.end(), back_inserter(result),
|
||||
|
@ -398,44 +451,45 @@ namespace Clipper2Lib
|
|||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline Path<T1> ScalePath(const Path<T2>& path, double scale)
|
||||
inline Path<T1> ScalePath(const Path<T2>& path,
|
||||
double scale, int& error_code)
|
||||
{
|
||||
return ScalePath<T1, T2>(path, scale, scale);
|
||||
return ScalePath<T1, T2>(path, scale, scale, error_code);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline Paths<T1> ScalePaths(const Paths<T2>& paths, double scale_x, double scale_y)
|
||||
inline Paths<T1> ScalePaths(const Paths<T2>& paths,
|
||||
double scale_x, double scale_y, int& error_code)
|
||||
{
|
||||
Paths<T1> result;
|
||||
|
||||
if constexpr (std::numeric_limits<T1>::is_integer &&
|
||||
!std::numeric_limits<T2>::is_integer)
|
||||
{
|
||||
RectD r = Bounds(paths);
|
||||
const double max_coord_d = static_cast<double>(MAX_COORD);
|
||||
const double min_coord_d = static_cast<double>(MIN_COORD);
|
||||
if ((r.left * scale_x) < min_coord_d ||
|
||||
(r.right * scale_x) > max_coord_d ||
|
||||
(r.top * scale_y) < min_coord_d ||
|
||||
(r.bottom * scale_y) > max_coord_d)
|
||||
#ifdef __cpp_exceptions
|
||||
throw Clipper2Exception(range_error);
|
||||
#else
|
||||
return result;
|
||||
#endif
|
||||
RectD r = GetBounds(paths);
|
||||
if ((r.left * scale_x) < min_coord ||
|
||||
(r.right * scale_x) > max_coord ||
|
||||
(r.top * scale_y) < min_coord ||
|
||||
(r.bottom * scale_y) > max_coord)
|
||||
{
|
||||
error_code |= range_error_i;
|
||||
DoError(range_error_i);
|
||||
return result; // empty path
|
||||
}
|
||||
}
|
||||
|
||||
result.reserve(paths.size());
|
||||
std::transform(paths.begin(), paths.end(), back_inserter(result),
|
||||
[scale_x, scale_y](const auto& path)
|
||||
{ return ScalePath<T1, T2>(path, scale_x, scale_y); });
|
||||
[=, &error_code](const auto& path)
|
||||
{ return ScalePath<T1, T2>(path, scale_x, scale_y, error_code); });
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline Paths<T1> ScalePaths(const Paths<T2>& paths, double scale)
|
||||
inline Paths<T1> ScalePaths(const Paths<T2>& paths,
|
||||
double scale, int& error_code)
|
||||
{
|
||||
return ScalePaths<T1, T2>(paths, scale, scale);
|
||||
return ScalePaths<T1, T2>(paths, scale, scale, error_code);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
|
@ -565,14 +619,18 @@ namespace Clipper2Lib
|
|||
|
||||
// Miscellaneous ------------------------------------------------------------
|
||||
|
||||
inline void CheckPrecision(int& precision)
|
||||
inline void CheckPrecision(int& precision, int& error_code)
|
||||
{
|
||||
if (precision >= -8 && precision <= 8) return;
|
||||
#ifdef __cpp_exceptions
|
||||
throw Clipper2Exception(precision_error);
|
||||
#else
|
||||
error_code |= precision_error_i; // non-fatal error
|
||||
DoError(precision_error_i); // unless exceptions enabled
|
||||
precision = precision > 8 ? 8 : -8;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void CheckPrecision(int& precision)
|
||||
{
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -659,9 +717,6 @@ namespace Clipper2Lib
|
|||
return Area<T>(poly) >= 0;
|
||||
}
|
||||
|
||||
static const double max_coord = static_cast<double>(MAX_COORD);
|
||||
static const double min_coord = static_cast<double>(MIN_COORD);
|
||||
|
||||
inline int64_t CheckCastInt64(double val)
|
||||
{
|
||||
if ((val >= max_coord) || (val <= min_coord)) return INVALID;
|
||||
|
@ -732,56 +787,78 @@ namespace Clipper2Lib
|
|||
return PointInPolygonResult::IsOutside;
|
||||
|
||||
int val = 0;
|
||||
typename Path<T>::const_iterator start = polygon.cbegin(), cit = start;
|
||||
typename Path<T>::const_iterator cend = polygon.cend(), pit = cend - 1;
|
||||
typename Path<T>::const_iterator cbegin = polygon.cbegin(), first = cbegin, curr, prev;
|
||||
typename Path<T>::const_iterator cend = polygon.cend();
|
||||
|
||||
while (pit->y == pt.y)
|
||||
{
|
||||
if (pit == start) return PointInPolygonResult::IsOutside;
|
||||
--pit;
|
||||
}
|
||||
bool is_above = pit->y < pt.y;
|
||||
while (first != cend && first->y == pt.y) ++first;
|
||||
if (first == cend) // not a proper polygon
|
||||
return PointInPolygonResult::IsOutside;
|
||||
|
||||
while (cit != cend)
|
||||
bool is_above = first->y < pt.y, starting_above = is_above;
|
||||
curr = first +1;
|
||||
while (true)
|
||||
{
|
||||
if (curr == cend)
|
||||
{
|
||||
if (cend == first || first == cbegin) break;
|
||||
cend = first;
|
||||
curr = cbegin;
|
||||
}
|
||||
|
||||
if (is_above)
|
||||
{
|
||||
while (cit != cend && cit->y < pt.y) ++cit;
|
||||
if (cit == cend) break;
|
||||
while (curr != cend && curr->y < pt.y) ++curr;
|
||||
if (curr == cend) continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (cit != cend && cit->y > pt.y) ++cit;
|
||||
if (cit == cend) break;
|
||||
while (curr != cend && curr->y > pt.y) ++curr;
|
||||
if (curr == cend) continue;
|
||||
}
|
||||
|
||||
if (cit == start) pit = cend - 1;
|
||||
else pit = cit - 1;
|
||||
if (curr == cbegin)
|
||||
prev = polygon.cend() - 1; //nb: NOT cend (since might equal first)
|
||||
else
|
||||
prev = curr - 1;
|
||||
|
||||
if (cit->y == pt.y)
|
||||
if (curr->y == pt.y)
|
||||
{
|
||||
if (cit->x == pt.x || (cit->y == pit->y &&
|
||||
((pt.x < pit->x) != (pt.x < cit->x))))
|
||||
return PointInPolygonResult::IsOn;
|
||||
++cit;
|
||||
if (curr->x == pt.x ||
|
||||
(curr->y == prev->y &&
|
||||
((pt.x < prev->x) != (pt.x < curr->x))))
|
||||
return PointInPolygonResult::IsOn;
|
||||
++curr;
|
||||
if (curr == first) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pt.x < cit->x && pt.x < pit->x)
|
||||
if (pt.x < curr->x && pt.x < prev->x)
|
||||
{
|
||||
// we're only interested in edges crossing on the left
|
||||
}
|
||||
else if (pt.x > pit->x && pt.x > cit->x)
|
||||
else if (pt.x > prev->x && pt.x > curr->x)
|
||||
val = 1 - val; // toggle val
|
||||
else
|
||||
{
|
||||
double d = CrossProduct(*pit, *cit, pt);
|
||||
double d = CrossProduct(*prev, *curr, pt);
|
||||
if (d == 0) return PointInPolygonResult::IsOn;
|
||||
if ((d < 0) == is_above) val = 1 - val;
|
||||
}
|
||||
is_above = !is_above;
|
||||
++cit;
|
||||
++curr;
|
||||
}
|
||||
|
||||
if (is_above != starting_above)
|
||||
{
|
||||
cend = polygon.cend();
|
||||
if (curr == cend) curr = cbegin;
|
||||
if (curr == cbegin) prev = cend - 1;
|
||||
else prev = curr - 1;
|
||||
double d = CrossProduct(*prev, *curr, pt);
|
||||
if (d == 0) return PointInPolygonResult::IsOn;
|
||||
if ((d < 0) == is_above) val = 1 - val;
|
||||
}
|
||||
|
||||
return (val == 0) ?
|
||||
PointInPolygonResult::IsOutside :
|
||||
PointInPolygonResult::IsInside;
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 27 January 2023 *
|
||||
* Date : 28 January 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : This is the main polygon clipping module *
|
||||
|
@ -10,7 +10,7 @@
|
|||
#ifndef CLIPPER_ENGINE_H
|
||||
#define CLIPPER_ENGINE_H
|
||||
|
||||
constexpr auto CLIPPER2_VERSION = "1.1.0";
|
||||
constexpr auto CLIPPER2_VERSION = "1.1.1";
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
@ -199,13 +199,13 @@ namespace Clipper2Lib {
|
|||
HorzSegmentList horz_seg_list_;
|
||||
std::vector<HorzJoin> horz_join_list_;
|
||||
void Reset();
|
||||
void InsertScanline(int64_t y);
|
||||
bool PopScanline(int64_t &y);
|
||||
bool PopLocalMinima(int64_t y, LocalMinima*& local_minima);
|
||||
inline void InsertScanline(int64_t y);
|
||||
inline bool PopScanline(int64_t &y);
|
||||
inline bool PopLocalMinima(int64_t y, LocalMinima*& local_minima);
|
||||
void DisposeAllOutRecs();
|
||||
void DisposeVerticesAndLocalMinima();
|
||||
void DeleteEdges(Active*& e);
|
||||
void AddLocMin(Vertex &vert, PathType polytype, bool is_open);
|
||||
inline void AddLocMin(Vertex &vert, PathType polytype, bool is_open);
|
||||
bool IsContributingClosed(const Active &e) const;
|
||||
inline bool IsContributingOpen(const Active &e) const;
|
||||
void SetWindCountForClosedPathEdge(Active &edge);
|
||||
|
@ -239,15 +239,17 @@ namespace Clipper2Lib {
|
|||
void FixSelfIntersects(OutRec* outrec);
|
||||
void DoSplitOp(OutRec* outRec, OutPt* splitOp);
|
||||
|
||||
void AddTrialHorzJoin(OutPt* op);
|
||||
inline void AddTrialHorzJoin(OutPt* op);
|
||||
void ConvertHorzSegsToJoins();
|
||||
void ProcessHorzJoins();
|
||||
|
||||
void Split(Active& e, const Point64& pt);
|
||||
void CheckJoinLeft(Active& e, const Point64& pt);
|
||||
void CheckJoinRight(Active& e,
|
||||
inline void CheckJoinLeft(Active& e,
|
||||
const Point64& pt, bool check_curr_x = false);
|
||||
inline void CheckJoinRight(Active& e,
|
||||
const Point64& pt, bool check_curr_x = false);
|
||||
protected:
|
||||
int error_code_ = 0;
|
||||
bool has_open_paths_ = false;
|
||||
bool succeeded_ = true;
|
||||
OutRecList outrec_list_; //pointers in case list memory reallocated
|
||||
|
@ -265,6 +267,7 @@ namespace Clipper2Lib {
|
|||
void AddPaths(const Paths64& paths, PathType polytype, bool is_open);
|
||||
public:
|
||||
virtual ~ClipperBase();
|
||||
int ErrorCode() { return error_code_; };
|
||||
bool PreserveCollinear = true;
|
||||
bool ReverseSolution = false;
|
||||
void Clear();
|
||||
|
@ -401,9 +404,10 @@ namespace Clipper2Lib {
|
|||
double InvScale() { return inv_scale_; }
|
||||
PolyPathD* AddChild(const Path64& path) override
|
||||
{
|
||||
int error_code = 0;
|
||||
auto p = std::make_unique<PolyPathD>(this);
|
||||
PolyPathD* result = childs_.emplace_back(std::move(p)).get();
|
||||
result->polygon_ = ScalePath<double, int64_t>(path, inv_scale_);
|
||||
result->polygon_ = ScalePath<double, int64_t>(path, inv_scale_, error_code);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -499,7 +503,7 @@ namespace Clipper2Lib {
|
|||
public:
|
||||
explicit ClipperD(int precision = 2) : ClipperBase()
|
||||
{
|
||||
CheckPrecision(precision);
|
||||
CheckPrecision(precision, error_code_);
|
||||
// to optimize scaling / descaling precision
|
||||
// set the scale to a power of double's radix (2) (#25)
|
||||
scale_ = std::pow(std::numeric_limits<double>::radix,
|
||||
|
@ -543,17 +547,17 @@ namespace Clipper2Lib {
|
|||
|
||||
void AddSubject(const PathsD& subjects)
|
||||
{
|
||||
AddPaths(ScalePaths<int64_t, double>(subjects, scale_), PathType::Subject, false);
|
||||
AddPaths(ScalePaths<int64_t, double>(subjects, scale_, error_code_), PathType::Subject, false);
|
||||
}
|
||||
|
||||
void AddOpenSubject(const PathsD& open_subjects)
|
||||
{
|
||||
AddPaths(ScalePaths<int64_t, double>(open_subjects, scale_), PathType::Subject, true);
|
||||
AddPaths(ScalePaths<int64_t, double>(open_subjects, scale_, error_code_), PathType::Subject, true);
|
||||
}
|
||||
|
||||
void AddClip(const PathsD& clips)
|
||||
{
|
||||
AddPaths(ScalePaths<int64_t, double>(clips, scale_), PathType::Clip, false);
|
||||
AddPaths(ScalePaths<int64_t, double>(clips, scale_, error_code_), PathType::Clip, false);
|
||||
}
|
||||
|
||||
bool Execute(ClipType clip_type, FillRule fill_rule, PathsD& closed_paths)
|
|
@ -1,8 +1,8 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 11 December 2022 *
|
||||
* Date : 12 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2022 *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : This module exports the Clipper2 Library (ie DLL/so) *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
@ -159,9 +159,9 @@ EXTERN_DLL_EXPORT CPathsD InflatePathsD(const CPathsD paths,
|
|||
|
||||
// RectClip & RectClipLines:
|
||||
EXTERN_DLL_EXPORT CPaths64 RectClip64(const CRect64& rect,
|
||||
const CPaths64 paths);
|
||||
const CPaths64 paths, bool convex_only = false);
|
||||
EXTERN_DLL_EXPORT CPathsD RectClipD(const CRectD& rect,
|
||||
const CPathsD paths, int precision = 2);
|
||||
const CPathsD paths, int precision = 2, bool convex_only = false);
|
||||
EXTERN_DLL_EXPORT CPaths64 RectClipLines64(const CRect64& rect,
|
||||
const CPaths64 paths);
|
||||
EXTERN_DLL_EXPORT CPathsD RectClipLinesD(const CRectD& rect,
|
||||
|
@ -380,54 +380,28 @@ EXTERN_DLL_EXPORT CPathsD InflatePathsD(const CPathsD paths,
|
|||
}
|
||||
|
||||
EXTERN_DLL_EXPORT CPaths64 RectClip64(const CRect64& rect,
|
||||
const CPaths64 paths)
|
||||
const CPaths64 paths, bool convex_only)
|
||||
{
|
||||
if (CRectIsEmpty(rect) || !paths) return nullptr;
|
||||
Rect64 r64 = CRectToRect(rect);
|
||||
class RectClip rc(r64);
|
||||
Paths64 pp = ConvertCPaths64(paths);
|
||||
Paths64 result;
|
||||
result.reserve(pp.size());
|
||||
for (const Path64& p : pp)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!r64.Intersects(pathRec)) continue;
|
||||
|
||||
if (r64.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Path64 p2 = rc.Execute(p);
|
||||
if (!p2.empty()) result.push_back(std::move(p2));
|
||||
}
|
||||
}
|
||||
Paths64 result = rc.Execute(pp, convex_only);
|
||||
return CreateCPaths64(result);
|
||||
}
|
||||
|
||||
EXTERN_DLL_EXPORT CPathsD RectClipD(const CRectD& rect,
|
||||
const CPathsD paths, int precision)
|
||||
const CPathsD paths, int precision, bool convex_only)
|
||||
{
|
||||
if (CRectIsEmpty(rect) || !paths) return nullptr;
|
||||
if (precision < -8 || precision > 8) return nullptr;
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(CRectToRect(rect), scale);
|
||||
|
||||
RectD r = CRectToRect(rect);
|
||||
Rect64 rec = ScaleRect<int64_t, double>(r, scale);
|
||||
Paths64 pp = ConvertCPathsD(paths, scale);
|
||||
class RectClip rc(r);
|
||||
Paths64 result;
|
||||
result.reserve(pp.size());
|
||||
for (const Path64& p : pp)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!r.Intersects(pathRec)) continue;
|
||||
|
||||
if (r.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Path64 p2 = rc.Execute(p);
|
||||
if (!p2.empty()) result.push_back(std::move(p2));
|
||||
}
|
||||
}
|
||||
class RectClip rc(rec);
|
||||
Paths64 result = rc.Execute(pp, convex_only);
|
||||
return CreateCPathsD(result, 1/scale);
|
||||
}
|
||||
|
||||
|
@ -438,52 +412,20 @@ EXTERN_DLL_EXPORT CPaths64 RectClipLines64(const CRect64& rect,
|
|||
Rect64 r = CRectToRect(rect);
|
||||
class RectClipLines rcl (r);
|
||||
Paths64 pp = ConvertCPaths64(paths);
|
||||
Paths64 result;
|
||||
result.reserve(pp.size());
|
||||
|
||||
for (const Path64& p : pp)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!r.Intersects(pathRec)) continue;
|
||||
|
||||
if (r.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Paths64 pp2 = rcl.Execute(p);
|
||||
if (!pp2.empty())
|
||||
result.insert(result.end(), pp2.begin(), pp2.end());
|
||||
}
|
||||
}
|
||||
Paths64 result = rcl.Execute(pp);
|
||||
return CreateCPaths64(result);
|
||||
}
|
||||
|
||||
EXTERN_DLL_EXPORT CPathsD RectClipLinesD(const CRectD& rect,
|
||||
const CPathsD paths, int precision)
|
||||
{
|
||||
Paths64 result;
|
||||
if (CRectIsEmpty(rect) || !paths) return nullptr;
|
||||
if (precision < -8 || precision > 8) return nullptr;
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(CRectToRect(rect), scale);
|
||||
class RectClipLines rcl(r);
|
||||
Paths64 pp = ConvertCPathsD(paths, scale);
|
||||
|
||||
result.reserve(pp.size());
|
||||
for (const Path64& p : pp)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!r.Intersects(pathRec)) continue;
|
||||
|
||||
if (r.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Paths64 pp2 = rcl.Execute(p);
|
||||
if (pp2.empty()) continue;
|
||||
result.insert(result.end(), pp2.begin(), pp2.end());
|
||||
}
|
||||
}
|
||||
Paths64 result = rcl.Execute(pp);
|
||||
return CreateCPathsD(result, 1/scale);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 27 January 2023 *
|
||||
* Date : 9 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : This module provides a simple interface to the Clipper Library *
|
||||
|
@ -11,6 +11,7 @@
|
|||
#define CLIPPER_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "clipper.core.h"
|
||||
|
@ -43,11 +44,13 @@ namespace Clipper2Lib {
|
|||
}
|
||||
|
||||
inline PathsD BooleanOp(ClipType cliptype, FillRule fillrule,
|
||||
const PathsD& subjects, const PathsD& clips, int decimal_prec = 2)
|
||||
const PathsD& subjects, const PathsD& clips, int precision = 2)
|
||||
{
|
||||
CheckPrecision(decimal_prec);
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
PathsD result;
|
||||
ClipperD clipper(decimal_prec);
|
||||
if (error_code) return result;
|
||||
ClipperD clipper(precision);
|
||||
clipper.AddSubject(subjects);
|
||||
clipper.AddClip(clips);
|
||||
clipper.Execute(cliptype, fillrule, result);
|
||||
|
@ -56,11 +59,13 @@ namespace Clipper2Lib {
|
|||
|
||||
inline void BooleanOp(ClipType cliptype, FillRule fillrule,
|
||||
const PathsD& subjects, const PathsD& clips,
|
||||
PolyTreeD& polytree, int decimal_prec = 2)
|
||||
PolyTreeD& polytree, int precision = 2)
|
||||
{
|
||||
CheckPrecision(decimal_prec);
|
||||
PathsD result;
|
||||
ClipperD clipper(decimal_prec);
|
||||
polytree.Clear();
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (error_code) return;
|
||||
ClipperD clipper(precision);
|
||||
clipper.AddSubject(subjects);
|
||||
clipper.AddClip(clips);
|
||||
clipper.Execute(cliptype, fillrule, polytree);
|
||||
|
@ -95,11 +100,13 @@ namespace Clipper2Lib {
|
|||
return result;
|
||||
}
|
||||
|
||||
inline PathsD Union(const PathsD& subjects, FillRule fillrule, int decimal_prec = 2)
|
||||
inline PathsD Union(const PathsD& subjects, FillRule fillrule, int precision = 2)
|
||||
{
|
||||
CheckPrecision(decimal_prec);
|
||||
PathsD result;
|
||||
ClipperD clipper(decimal_prec);
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (error_code) return result;
|
||||
ClipperD clipper(precision);
|
||||
clipper.AddSubject(subjects);
|
||||
clipper.Execute(ClipType::Union, fillrule, result);
|
||||
return result;
|
||||
|
@ -126,22 +133,29 @@ namespace Clipper2Lib {
|
|||
}
|
||||
|
||||
inline Paths64 InflatePaths(const Paths64& paths, double delta,
|
||||
JoinType jt, EndType et, double miter_limit = 2.0)
|
||||
JoinType jt, EndType et, double miter_limit = 2.0,
|
||||
double arc_tolerance = 0.0)
|
||||
{
|
||||
ClipperOffset clip_offset(miter_limit);
|
||||
if (!delta) return paths;
|
||||
ClipperOffset clip_offset(miter_limit, arc_tolerance);
|
||||
clip_offset.AddPaths(paths, jt, et);
|
||||
return clip_offset.Execute(delta);
|
||||
}
|
||||
|
||||
inline PathsD InflatePaths(const PathsD& paths, double delta,
|
||||
JoinType jt, EndType et, double miter_limit = 2.0, int precision = 2)
|
||||
JoinType jt, EndType et, double miter_limit = 2.0,
|
||||
int precision = 2, double arc_tolerance = 0.0)
|
||||
{
|
||||
CheckPrecision(precision);
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (!delta) return paths;
|
||||
if (error_code) return PathsD();
|
||||
const double scale = std::pow(10, precision);
|
||||
ClipperOffset clip_offset(miter_limit);
|
||||
clip_offset.AddPaths(ScalePaths<int64_t,double>(paths, scale), jt, et);
|
||||
ClipperOffset clip_offset(miter_limit, arc_tolerance);
|
||||
clip_offset.AddPaths(ScalePaths<int64_t,double>(paths, scale, error_code), jt, et);
|
||||
if (error_code) return PathsD();
|
||||
Paths64 tmp = clip_offset.Execute(delta * scale);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale, error_code);
|
||||
}
|
||||
|
||||
inline Path64 TranslatePath(const Path64& path, int64_t dx, int64_t dy)
|
||||
|
@ -180,153 +194,82 @@ namespace Clipper2Lib {
|
|||
return result;
|
||||
}
|
||||
|
||||
inline Path64 RectClip(const Rect64& rect, const Path64& path)
|
||||
{
|
||||
if (rect.IsEmpty() || path.empty()) return Path64();
|
||||
Rect64 pathRec = Bounds(path);
|
||||
if (!rect.Intersects(pathRec)) return Path64();
|
||||
if (rect.Contains(pathRec)) return path;
|
||||
class RectClip rc(rect);
|
||||
return rc.Execute(path);
|
||||
}
|
||||
|
||||
inline Paths64 RectClip(const Rect64& rect, const Paths64& paths)
|
||||
inline Paths64 RectClip(const Rect64& rect,
|
||||
const Paths64& paths, bool convex_only = false)
|
||||
{
|
||||
if (rect.IsEmpty() || paths.empty()) return Paths64();
|
||||
class RectClip rc(rect);
|
||||
Paths64 result;
|
||||
result.reserve(paths.size());
|
||||
|
||||
for (const Path64& p : paths)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!rect.Intersects(pathRec))
|
||||
continue;
|
||||
else if (rect.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Path64 p2 = rc.Execute(p);
|
||||
if (!p2.empty()) result.push_back(std::move(p2));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return rc.Execute(paths, convex_only);
|
||||
}
|
||||
|
||||
inline PathD RectClip(const RectD& rect, const PathD& path, int precision = 2)
|
||||
inline Paths64 RectClip(const Rect64& rect,
|
||||
const Path64& path, bool convex_only = false)
|
||||
{
|
||||
if (rect.IsEmpty() || path.empty() ||
|
||||
!rect.Contains(Bounds(path))) return PathD();
|
||||
CheckPrecision(precision);
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
|
||||
class RectClip rc(r);
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale);
|
||||
return ScalePath<double, int64_t>(rc.Execute(p), 1 / scale);
|
||||
if (rect.IsEmpty() || path.empty()) return Paths64();
|
||||
class RectClip rc(rect);
|
||||
Paths64 tmp;
|
||||
tmp.push_back(path);
|
||||
return rc.Execute(tmp, convex_only);
|
||||
}
|
||||
|
||||
inline PathsD RectClip(const RectD& rect, const PathsD& paths, int precision = 2)
|
||||
inline PathsD RectClip(const RectD& rect,
|
||||
const PathsD& paths, bool convex_only = false, int precision = 2)
|
||||
{
|
||||
if (rect.IsEmpty() || paths.empty()) return PathsD();
|
||||
CheckPrecision(precision);
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (error_code) return PathsD();
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
|
||||
class RectClip rc(r);
|
||||
PathsD result;
|
||||
result.reserve(paths.size());
|
||||
for (const PathD& path : paths)
|
||||
{
|
||||
RectD pathRec = Bounds(path);
|
||||
if (!rect.Intersects(pathRec))
|
||||
continue;
|
||||
else if (rect.Contains(pathRec))
|
||||
result.push_back(path);
|
||||
else
|
||||
{
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale);
|
||||
p = rc.Execute(p);
|
||||
if (!p.empty())
|
||||
result.push_back(ScalePath<double, int64_t>(p, 1 / scale));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
Paths64 pp = ScalePaths<int64_t, double>(paths, scale, error_code);
|
||||
if (error_code) return PathsD(); // ie: error_code result is lost
|
||||
return ScalePaths<double, int64_t>(
|
||||
rc.Execute(pp, convex_only), 1 / scale, error_code);
|
||||
}
|
||||
|
||||
inline Paths64 RectClipLines(const Rect64& rect, const Path64& path)
|
||||
inline PathsD RectClip(const RectD& rect,
|
||||
const PathD& path, bool convex_only = false, int precision = 2)
|
||||
{
|
||||
Paths64 result;
|
||||
if (rect.IsEmpty() || path.empty()) return result;
|
||||
Rect64 pathRec = Bounds(path);
|
||||
if (!rect.Intersects(pathRec)) return result;
|
||||
if (rect.Contains(pathRec))
|
||||
{
|
||||
result.push_back(path);
|
||||
return result;
|
||||
}
|
||||
PathsD tmp;
|
||||
tmp.push_back(path);
|
||||
return RectClip(rect, tmp, convex_only, precision);
|
||||
}
|
||||
|
||||
inline Paths64 RectClipLines(const Rect64& rect, const Paths64& lines)
|
||||
{
|
||||
if (rect.IsEmpty() || lines.empty()) return Paths64();
|
||||
class RectClipLines rcl(rect);
|
||||
return rcl.Execute(path);
|
||||
return rcl.Execute(lines);
|
||||
}
|
||||
|
||||
inline Paths64 RectClipLines(const Rect64& rect, const Paths64& paths)
|
||||
inline Paths64 RectClipLines(const Rect64& rect, const Path64& line)
|
||||
{
|
||||
Paths64 result;
|
||||
if (rect.IsEmpty() || paths.empty()) return result;
|
||||
class RectClipLines rcl(rect);
|
||||
for (const Path64& p : paths)
|
||||
{
|
||||
Rect64 pathRec = Bounds(p);
|
||||
if (!rect.Intersects(pathRec))
|
||||
continue;
|
||||
else if (rect.Contains(pathRec))
|
||||
result.push_back(p);
|
||||
else
|
||||
{
|
||||
Paths64 pp = rcl.Execute(p);
|
||||
if (!pp.empty())
|
||||
result.insert(result.end(), pp.begin(), pp.end());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
Paths64 tmp;
|
||||
tmp.push_back(line);
|
||||
return RectClipLines(rect, tmp);
|
||||
}
|
||||
|
||||
inline PathsD RectClipLines(const RectD& rect, const PathD& path, int precision = 2)
|
||||
inline PathsD RectClipLines(const RectD& rect, const PathD& line, int precision = 2)
|
||||
{
|
||||
if (rect.IsEmpty() || path.empty() ||
|
||||
!rect.Contains(Bounds(path))) return PathsD();
|
||||
CheckPrecision(precision);
|
||||
PathsD tmp;
|
||||
tmp.push_back(line);
|
||||
return RectClip(rect, tmp, precision);
|
||||
}
|
||||
|
||||
inline PathsD RectClipLines(const RectD& rect, const PathsD& lines, int precision = 2)
|
||||
{
|
||||
if (rect.IsEmpty() || lines.empty()) return PathsD();
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (error_code) return PathsD();
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
|
||||
class RectClipLines rcl(r);
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale);
|
||||
return ScalePaths<double, int64_t>(rcl.Execute(p), 1 / scale);
|
||||
}
|
||||
|
||||
inline PathsD RectClipLines(const RectD& rect, const PathsD& paths, int precision = 2)
|
||||
{
|
||||
PathsD result;
|
||||
if (rect.IsEmpty() || paths.empty()) return result;
|
||||
CheckPrecision(precision);
|
||||
const double scale = std::pow(10, precision);
|
||||
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
|
||||
class RectClipLines rcl(r);
|
||||
result.reserve(paths.size());
|
||||
for (const PathD& path : paths)
|
||||
{
|
||||
RectD pathRec = Bounds(path);
|
||||
if (!rect.Intersects(pathRec))
|
||||
continue;
|
||||
else if (rect.Contains(pathRec))
|
||||
result.push_back(path);
|
||||
else
|
||||
{
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale);
|
||||
Paths64 pp = rcl.Execute(p);
|
||||
if (pp.empty()) continue;
|
||||
PathsD ppd = ScalePaths<double, int64_t>(pp, 1 / scale);
|
||||
result.insert(result.end(), ppd.begin(), ppd.end());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
Paths64 p = ScalePaths<int64_t, double>(lines, scale, error_code);
|
||||
if (error_code) return PathsD();
|
||||
p = rcl.Execute(p);
|
||||
return ScalePaths<double, int64_t>(p, 1 / scale, error_code);
|
||||
}
|
||||
|
||||
namespace details
|
||||
|
@ -378,94 +321,6 @@ namespace Clipper2Lib {
|
|||
return true;
|
||||
}
|
||||
|
||||
inline bool GetInt(std::string::const_iterator& iter, const
|
||||
std::string::const_iterator& end_iter, int64_t& val)
|
||||
{
|
||||
val = 0;
|
||||
bool is_neg = *iter == '-';
|
||||
if (is_neg) ++iter;
|
||||
std::string::const_iterator start_iter = iter;
|
||||
while (iter != end_iter &&
|
||||
((*iter >= '0') && (*iter <= '9')))
|
||||
{
|
||||
val = val * 10 + (static_cast<int64_t>(*iter++) - '0');
|
||||
}
|
||||
if (is_neg) val = -val;
|
||||
return (iter != start_iter);
|
||||
}
|
||||
|
||||
inline bool GetFloat(std::string::const_iterator& iter, const
|
||||
std::string::const_iterator& end_iter, double& val)
|
||||
{
|
||||
val = 0;
|
||||
bool is_neg = *iter == '-';
|
||||
if (is_neg) ++iter;
|
||||
int dec_pos = 1;
|
||||
const std::string::const_iterator start_iter = iter;
|
||||
while (iter != end_iter && (*iter == '.' ||
|
||||
((*iter >= '0') && (*iter <= '9'))))
|
||||
{
|
||||
if (*iter == '.')
|
||||
{
|
||||
if (dec_pos != 1) break;
|
||||
dec_pos = 0;
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
if (dec_pos != 1) --dec_pos;
|
||||
val = val * 10 + ((int64_t)(*iter++) - '0');
|
||||
}
|
||||
if (iter == start_iter || dec_pos == 0) return false;
|
||||
if (dec_pos < 0)
|
||||
val *= std::pow(10, dec_pos);
|
||||
if (is_neg)
|
||||
val *= -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void SkipWhiteSpace(std::string::const_iterator& iter,
|
||||
const std::string::const_iterator& end_iter)
|
||||
{
|
||||
while (iter != end_iter && *iter <= ' ') ++iter;
|
||||
}
|
||||
|
||||
inline void SkipSpacesWithOptionalComma(std::string::const_iterator& iter,
|
||||
const std::string::const_iterator& end_iter)
|
||||
{
|
||||
bool comma_seen = false;
|
||||
while (iter != end_iter)
|
||||
{
|
||||
if (*iter == ' ') ++iter;
|
||||
else if (*iter == ',')
|
||||
{
|
||||
if (comma_seen) return; // don't skip 2 commas!
|
||||
comma_seen = true;
|
||||
++iter;
|
||||
}
|
||||
else return;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool has_one_match(const char c, char* chrs)
|
||||
{
|
||||
while (*chrs > 0 && c != *chrs) ++chrs;
|
||||
if (!*chrs) return false;
|
||||
*chrs = ' '; // only match once per char
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
inline void SkipUserDefinedChars(std::string::const_iterator& iter,
|
||||
const std::string::const_iterator& end_iter, const std::string& skip_chars)
|
||||
{
|
||||
const size_t MAX_CHARS = 16;
|
||||
char buff[MAX_CHARS] = {0};
|
||||
std::copy(skip_chars.cbegin(), skip_chars.cend(), &buff[0]);
|
||||
while (iter != end_iter &&
|
||||
(*iter <= ' ' || has_one_match(*iter, buff))) ++iter;
|
||||
return;
|
||||
}
|
||||
|
||||
static void OutlinePolyPath(std::ostream& os,
|
||||
bool isHole, size_t count, const std::string& preamble)
|
||||
{
|
||||
|
@ -565,39 +420,77 @@ namespace Clipper2Lib {
|
|||
return true;
|
||||
}
|
||||
|
||||
inline Path64 MakePath(const std::string& s)
|
||||
{
|
||||
const std::string skip_chars = " ,(){}[]";
|
||||
Path64 result;
|
||||
std::string::const_iterator s_iter = s.cbegin();
|
||||
details::SkipUserDefinedChars(s_iter, s.cend(), skip_chars);
|
||||
while (s_iter != s.cend())
|
||||
namespace details {
|
||||
|
||||
template<typename T, typename U>
|
||||
inline constexpr void MakePathGeneric(const T list, size_t size,
|
||||
std::vector<U>& result)
|
||||
{
|
||||
int64_t y = 0, x = 0;
|
||||
if (!details::GetInt(s_iter, s.cend(), x)) break;
|
||||
details::SkipSpacesWithOptionalComma(s_iter, s.cend());
|
||||
if (!details::GetInt(s_iter, s.cend(), y)) break;
|
||||
result.push_back(Point64(x, y));
|
||||
details::SkipUserDefinedChars(s_iter, s.cend(), skip_chars);
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
#ifdef USINGZ
|
||||
result[i / 2] = U{list[i], list[++i], 0};
|
||||
#else
|
||||
result[i / 2] = U{list[i], list[++i]};
|
||||
#endif
|
||||
}
|
||||
|
||||
} // end details namespace
|
||||
|
||||
template<typename T,
|
||||
typename std::enable_if<
|
||||
std::is_integral<T>::value &&
|
||||
!std::is_same<char, T>::value, bool
|
||||
>::type = true>
|
||||
inline Path64 MakePath(const std::vector<T>& list)
|
||||
{
|
||||
const auto size = list.size() - list.size() % 2;
|
||||
if (list.size() != size)
|
||||
DoError(non_pair_error_i); // non-fatal without exception handling
|
||||
Path64 result(size / 2); // else ignores unpaired value
|
||||
details::MakePathGeneric(list, size, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline PathD MakePathD(const std::string& s)
|
||||
|
||||
template<typename T, std::size_t N,
|
||||
typename std::enable_if<
|
||||
std::is_integral<T>::value &&
|
||||
!std::is_same<char, T>::value, bool
|
||||
>::type = true>
|
||||
inline Path64 MakePath(const T(&list)[N])
|
||||
{
|
||||
const std::string skip_chars = " ,(){}[]";
|
||||
PathD result;
|
||||
std::string::const_iterator s_iter = s.cbegin();
|
||||
details::SkipUserDefinedChars(s_iter, s.cend(), skip_chars);
|
||||
while (s_iter != s.cend())
|
||||
{
|
||||
double y = 0, x = 0;
|
||||
if (!details::GetFloat(s_iter, s.cend(), x)) break;
|
||||
details::SkipSpacesWithOptionalComma(s_iter, s.cend());
|
||||
if (!details::GetFloat(s_iter, s.cend(), y)) break;
|
||||
result.push_back(PointD(x, y));
|
||||
details::SkipUserDefinedChars(s_iter, s.cend(), skip_chars);
|
||||
}
|
||||
// Make the compiler error on unpaired value (i.e. no runtime effects).
|
||||
static_assert(N % 2 == 0, "MakePath requires an even number of arguments");
|
||||
Path64 result(N / 2);
|
||||
details::MakePathGeneric(list, N, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T,
|
||||
typename std::enable_if<
|
||||
std::is_arithmetic<T>::value &&
|
||||
!std::is_same<char, T>::value, bool
|
||||
>::type = true>
|
||||
inline PathD MakePathD(const std::vector<T>& list)
|
||||
{
|
||||
const auto size = list.size() - list.size() % 2;
|
||||
if (list.size() != size)
|
||||
DoError(non_pair_error_i); // non-fatal without exception handling
|
||||
PathD result(size / 2); // else ignores unpaired value
|
||||
details::MakePathGeneric(list, size, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N,
|
||||
typename std::enable_if<
|
||||
std::is_arithmetic<T>::value &&
|
||||
!std::is_same<char, T>::value, bool
|
||||
>::type = true>
|
||||
inline PathD MakePathD(const T(&list)[N])
|
||||
{
|
||||
// Make the compiler error on unpaired value (i.e. no runtime effects).
|
||||
static_assert(N % 2 == 0, "MakePath requires an even number of arguments");
|
||||
PathD result(N / 2);
|
||||
details::MakePathGeneric(list, N, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -650,11 +543,14 @@ namespace Clipper2Lib {
|
|||
|
||||
inline PathD TrimCollinear(const PathD& path, int precision, bool is_open_path = false)
|
||||
{
|
||||
CheckPrecision(precision);
|
||||
int error_code = 0;
|
||||
CheckPrecision(precision, error_code);
|
||||
if (error_code) return PathD();
|
||||
const double scale = std::pow(10, precision);
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale);
|
||||
Path64 p = ScalePath<int64_t, double>(path, scale, error_code);
|
||||
if (error_code) return PathD();
|
||||
p = TrimCollinear(p, is_open_path);
|
||||
return ScalePath<double, int64_t>(p, 1/scale);
|
||||
return ScalePath<double, int64_t>(p, 1/scale, error_code);
|
||||
}
|
||||
|
||||
template <typename T>
|
|
@ -1,8 +1,8 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 15 October 2022 *
|
||||
* Date : 28 January 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2022 *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : Minkowski Sum and Difference *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
@ -92,11 +92,12 @@ namespace Clipper2Lib
|
|||
|
||||
inline PathsD MinkowskiSum(const PathD& pattern, const PathD& path, bool isClosed, int decimalPlaces = 2)
|
||||
{
|
||||
int error_code = 0;
|
||||
double scale = pow(10, decimalPlaces);
|
||||
Path64 pat64 = ScalePath<int64_t, double>(pattern, scale);
|
||||
Path64 path64 = ScalePath<int64_t, double>(path, scale);
|
||||
Path64 pat64 = ScalePath<int64_t, double>(pattern, scale, error_code);
|
||||
Path64 path64 = ScalePath<int64_t, double>(path, scale, error_code);
|
||||
Paths64 tmp = detail::Union(detail::Minkowski(pat64, path64, true, isClosed), FillRule::NonZero);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale, error_code);
|
||||
}
|
||||
|
||||
inline Paths64 MinkowskiDiff(const Path64& pattern, const Path64& path, bool isClosed)
|
||||
|
@ -106,11 +107,12 @@ namespace Clipper2Lib
|
|||
|
||||
inline PathsD MinkowskiDiff(const PathD& pattern, const PathD& path, bool isClosed, int decimalPlaces = 2)
|
||||
{
|
||||
int error_code = 0;
|
||||
double scale = pow(10, decimalPlaces);
|
||||
Path64 pat64 = ScalePath<int64_t, double>(pattern, scale);
|
||||
Path64 path64 = ScalePath<int64_t, double>(path, scale);
|
||||
Path64 pat64 = ScalePath<int64_t, double>(pattern, scale, error_code);
|
||||
Path64 path64 = ScalePath<int64_t, double>(path, scale, error_code);
|
||||
Paths64 tmp = detail::Union(detail::Minkowski(pat64, path64, false, isClosed), FillRule::NonZero);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale);
|
||||
return ScalePaths<double, int64_t>(tmp, 1 / scale, error_code);
|
||||
}
|
||||
|
||||
} // Clipper2Lib namespace
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 25 January 2023 *
|
||||
* Date : 15 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : Path Offset (Inflate/Shrink) *
|
||||
|
@ -11,6 +11,7 @@
|
|||
#define CLIPPER_OFFSET_H_
|
||||
|
||||
#include "clipper.core.h"
|
||||
#include "clipper.engine.h"
|
||||
|
||||
namespace Clipper2Lib {
|
||||
|
||||
|
@ -28,16 +29,18 @@ private:
|
|||
|
||||
class Group {
|
||||
public:
|
||||
Paths64 paths_in_;
|
||||
Paths64 paths_out_;
|
||||
Path64 path_;
|
||||
bool is_reversed_ = false;
|
||||
JoinType join_type_;
|
||||
EndType end_type_;
|
||||
Paths64 paths_in;
|
||||
Paths64 paths_out;
|
||||
Path64 path;
|
||||
bool is_reversed = false;
|
||||
JoinType join_type;
|
||||
EndType end_type;
|
||||
Group(const Paths64& paths, JoinType join_type, EndType end_type) :
|
||||
paths_in_(paths), join_type_(join_type), end_type_(end_type) {}
|
||||
paths_in(paths), join_type(join_type), end_type(end_type) {}
|
||||
};
|
||||
|
||||
int error_code_ = 0;
|
||||
double delta_ = 0.0;
|
||||
double group_delta_ = 0.0;
|
||||
double abs_group_delta_ = 0.0;
|
||||
double temp_lim_ = 0.0;
|
||||
|
@ -46,22 +49,26 @@ private:
|
|||
Paths64 solution;
|
||||
std::vector<Group> groups_;
|
||||
JoinType join_type_ = JoinType::Square;
|
||||
|
||||
EndType end_type_ = EndType::Polygon;
|
||||
|
||||
double miter_limit_ = 0.0;
|
||||
double arc_tolerance_ = 0.0;
|
||||
bool preserve_collinear_ = false;
|
||||
bool reverse_solution_ = false;
|
||||
|
||||
#if USINGZ
|
||||
ZCallback64 zCallback64_ = nullptr;
|
||||
#endif
|
||||
|
||||
void DoSquare(Group& group, const Path64& path, size_t j, size_t k);
|
||||
void DoMiter(Group& group, const Path64& path, size_t j, size_t k, double cos_a);
|
||||
void DoRound(Group& group, const Path64& path, size_t j, size_t k, double angle);
|
||||
void BuildNormals(const Path64& path);
|
||||
void OffsetPolygon(Group& group, Path64& path);
|
||||
void OffsetOpenJoined(Group& group, Path64& path);
|
||||
void OffsetOpenPath(Group& group, Path64& path, EndType endType);
|
||||
void OffsetPoint(Group& group, Path64& path,
|
||||
size_t j, size_t& k, bool reversing = false);
|
||||
void DoGroupOffset(Group &group, double delta);
|
||||
void OffsetOpenPath(Group& group, Path64& path);
|
||||
void OffsetPoint(Group& group, Path64& path, size_t j, size_t& k);
|
||||
void DoGroupOffset(Group &group);
|
||||
public:
|
||||
explicit ClipperOffset(double miter_limit = 2.0,
|
||||
double arc_tolerance = 0.0,
|
||||
|
@ -73,6 +80,7 @@ public:
|
|||
|
||||
~ClipperOffset() { Clear(); };
|
||||
|
||||
int ErrorCode() { return error_code_; };
|
||||
void AddPath(const Path64& path, JoinType jt_, EndType et_);
|
||||
void AddPaths(const Paths64& paths, JoinType jt_, EndType et_);
|
||||
void AddPath(const PathD &p, JoinType jt_, EndType et_);
|
||||
|
@ -93,6 +101,10 @@ public:
|
|||
|
||||
bool ReverseSolution() const { return reverse_solution_; }
|
||||
void ReverseSolution(bool reverse_solution) {reverse_solution_ = reverse_solution;}
|
||||
|
||||
#if USINGZ
|
||||
void SetZCallback(ZCallback64 cb) { zCallback64_ = cb; }
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 9 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : FAST rectangular clipping *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef CLIPPER_RECTCLIP_H
|
||||
#define CLIPPER_RECTCLIP_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include "clipper.h"
|
||||
#include "clipper.core.h"
|
||||
|
||||
namespace Clipper2Lib
|
||||
{
|
||||
|
||||
enum class Location { Left, Top, Right, Bottom, Inside };
|
||||
|
||||
class OutPt2;
|
||||
typedef std::vector<OutPt2*> OutPt2List;
|
||||
|
||||
class OutPt2 {
|
||||
public:
|
||||
Point64 pt;
|
||||
size_t owner_idx;
|
||||
OutPt2List* edge;
|
||||
OutPt2* next;
|
||||
OutPt2* prev;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// RectClip
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class RectClip {
|
||||
private:
|
||||
void ExecuteInternal(const Path64& path);
|
||||
Path64 GetPath(OutPt2*& op);
|
||||
protected:
|
||||
const Rect64 rect_;
|
||||
const Path64 rect_as_path_;
|
||||
const Point64 rect_mp_;
|
||||
Rect64 path_bounds_;
|
||||
std::deque<OutPt2> op_container_;
|
||||
OutPt2List results_; // each path can be broken into multiples
|
||||
OutPt2List edges_[8]; // clockwise and counter-clockwise
|
||||
std::vector<Location> start_locs_;
|
||||
void CheckEdges();
|
||||
void TidyEdges(int idx, OutPt2List& cw, OutPt2List& ccw);
|
||||
void GetNextLocation(const Path64& path,
|
||||
Location& loc, int& i, int highI);
|
||||
OutPt2* Add(Point64 pt, bool start_new = false);
|
||||
void AddCorner(Location prev, Location curr);
|
||||
void AddCorner(Location& loc, bool isClockwise);
|
||||
public:
|
||||
explicit RectClip(const Rect64& rect) :
|
||||
rect_(rect),
|
||||
rect_as_path_(rect.AsPath()),
|
||||
rect_mp_(rect.MidPoint()) {}
|
||||
Paths64 Execute(const Paths64& paths, bool convex_only = false);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// RectClipLines
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class RectClipLines : public RectClip {
|
||||
private:
|
||||
void ExecuteInternal(const Path64& path);
|
||||
Path64 GetPath(OutPt2*& op);
|
||||
public:
|
||||
explicit RectClipLines(const Rect64& rect) : RectClip(rect) {};
|
||||
Paths64 Execute(const Paths64& paths);
|
||||
};
|
||||
|
||||
} // Clipper2Lib namespace
|
||||
#endif // CLIPPER_RECTCLIP_H
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 27 January 2023 *
|
||||
* Date : 21 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : This is the main polygon clipping module *
|
||||
|
@ -14,7 +14,7 @@
|
|||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
#include "clipper.engine.h"
|
||||
#include "clipper2/clipper.engine.h"
|
||||
|
||||
// https://github.com/AngusJohnson/Clipper2/discussions/334
|
||||
// #discussioncomment-4248602
|
||||
|
@ -28,12 +28,7 @@
|
|||
|
||||
namespace Clipper2Lib {
|
||||
|
||||
static const Rect64 invalid_rect = Rect64(
|
||||
std::numeric_limits<int64_t>::max(),
|
||||
std::numeric_limits<int64_t>::max(),
|
||||
-std::numeric_limits<int64_t>::max(),
|
||||
-std::numeric_limits<int64_t>::max()
|
||||
);
|
||||
static const Rect64 invalid_rect = Rect64(false);
|
||||
|
||||
// Every closed path (or polygon) is made up of a series of vertices forming
|
||||
// edges that alternate between going up (relative to the Y-axis) and going
|
||||
|
@ -686,7 +681,7 @@ namespace Clipper2Lib {
|
|||
} // end AddPaths
|
||||
|
||||
|
||||
inline void ClipperBase::InsertScanline(int64_t y)
|
||||
void ClipperBase::InsertScanline(int64_t y)
|
||||
{
|
||||
scanline_list_.push(y);
|
||||
}
|
||||
|
@ -1828,7 +1823,6 @@ namespace Clipper2Lib {
|
|||
return resultOp;
|
||||
}
|
||||
|
||||
|
||||
inline void ClipperBase::DeleteFromAEL(Active& e)
|
||||
{
|
||||
Active* prev = e.prev_in_ael;
|
||||
|
@ -1917,21 +1911,22 @@ namespace Clipper2Lib {
|
|||
{
|
||||
if (op == op->next || op->prev == op->next)
|
||||
return PointInPolygonResult::IsOutside;
|
||||
|
||||
OutPt* op2 = op;
|
||||
do
|
||||
{
|
||||
if (op->pt.y != pt.y) break;
|
||||
op = op->next;
|
||||
} while (op != op2);
|
||||
if (op == op2) return PointInPolygonResult::IsOutside;
|
||||
if (op->pt.y == pt.y) // not a proper polygon
|
||||
return PointInPolygonResult::IsOutside;
|
||||
|
||||
// must be above or below to get here
|
||||
bool isAbove = op->pt.y < pt.y;
|
||||
bool is_above = op->pt.y < pt.y, starting_above = is_above;
|
||||
int val = 0;
|
||||
op2 = op->next;
|
||||
while (op2 != op)
|
||||
{
|
||||
if (isAbove)
|
||||
if (is_above)
|
||||
while (op2 != op && op2->pt.y < pt.y) op2 = op2->next;
|
||||
else
|
||||
while (op2 != op && op2->pt.y > pt.y) op2 = op2->next;
|
||||
|
@ -1947,6 +1942,7 @@ namespace Clipper2Lib {
|
|||
return PointInPolygonResult::IsOn;
|
||||
|
||||
op2 = op2->next;
|
||||
if (op2 == op) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1959,11 +1955,19 @@ namespace Clipper2Lib {
|
|||
{
|
||||
double d = CrossProduct(op2->prev->pt, op2->pt, pt);
|
||||
if (d == 0) return PointInPolygonResult::IsOn;
|
||||
if ((d < 0) == isAbove) val = 1 - val;
|
||||
if ((d < 0) == is_above) val = 1 - val;
|
||||
}
|
||||
isAbove = !isAbove;
|
||||
is_above = !is_above;
|
||||
op2 = op2->next;
|
||||
}
|
||||
|
||||
if (is_above != starting_above)
|
||||
{
|
||||
double d = CrossProduct(op2->prev->pt, op2->pt, pt);
|
||||
if (d == 0) return PointInPolygonResult::IsOn;
|
||||
if ((d < 0) == is_above) val = 1 - val;
|
||||
}
|
||||
|
||||
if (val == 0) return PointInPolygonResult::IsOutside;
|
||||
else return PointInPolygonResult::IsInside;
|
||||
}
|
||||
|
@ -1987,25 +1991,6 @@ namespace Clipper2Lib {
|
|||
return PointInOpPolygon(mp, op2) == PointInPolygonResult::IsInside;
|
||||
}
|
||||
|
||||
inline bool Path1InsidePath2(const OutRec* or1, const OutRec* or2)
|
||||
{
|
||||
// we need to make some accommodation for rounding errors
|
||||
// so we won't jump if the first vertex is found outside
|
||||
int outside_cnt = 0;
|
||||
OutPt* op = or1->pts;
|
||||
do
|
||||
{
|
||||
PointInPolygonResult result = PointInPolygon(op->pt, or2->path);
|
||||
if (result == PointInPolygonResult::IsOutside) ++outside_cnt;
|
||||
else if (result == PointInPolygonResult::IsInside) --outside_cnt;
|
||||
op = op->next;
|
||||
} while (op != or1->pts && std::abs(outside_cnt) < 2);
|
||||
if (std::abs(outside_cnt) > 1) return (outside_cnt < 0);
|
||||
// since path1's location is still equivocal, check its midpoint
|
||||
Point64 mp = GetBounds(op).MidPoint();
|
||||
return PointInPolygon(mp, or2->path) == PointInPolygonResult::IsInside;
|
||||
}
|
||||
|
||||
inline bool SetHorzSegHeadingForward(HorzSegment& hs, OutPt* opP, OutPt* opN)
|
||||
{
|
||||
if (opP->pt.x == opN->pt.x) return false;
|
||||
|
@ -2287,7 +2272,7 @@ namespace Clipper2Lib {
|
|||
|
||||
node.edge1->curr_x = node.pt.x;
|
||||
node.edge2->curr_x = node.pt.x;
|
||||
CheckJoinLeft(*node.edge2, node.pt);
|
||||
CheckJoinLeft(*node.edge2, node.pt, true);
|
||||
CheckJoinRight(*node.edge1, node.pt, true);
|
||||
}
|
||||
}
|
||||
|
@ -2646,16 +2631,22 @@ namespace Clipper2Lib {
|
|||
}
|
||||
}
|
||||
|
||||
void ClipperBase::CheckJoinLeft(Active& e, const Point64& pt)
|
||||
void ClipperBase::CheckJoinLeft(Active& e,
|
||||
const Point64& pt, bool check_curr_x)
|
||||
{
|
||||
Active* prev = e.prev_in_ael;
|
||||
if (IsOpen(e) || !IsHotEdge(e) || !prev || IsOpen(*prev) ||
|
||||
!IsHotEdge(*prev) || e.curr_x != prev->curr_x ||
|
||||
pt.y <= e.top.y || pt.y <= prev->top.y ||
|
||||
IsJoined(e) || IsOpen(e) ||
|
||||
CrossProduct(e.top, pt, prev->top))
|
||||
if (IsOpen(e) || !IsHotEdge(e) || !prev ||
|
||||
IsOpen(*prev) || !IsHotEdge(*prev) ||
|
||||
pt.y < e.top.y + 2 || pt.y < prev->top.y + 2) // avoid trivial joins
|
||||
return;
|
||||
|
||||
if (check_curr_x)
|
||||
{
|
||||
if (DistanceFromLineSqrd(pt, prev->bot, prev->top) > 0.25) return;
|
||||
}
|
||||
else if (e.curr_x != prev->curr_x) return;
|
||||
if (CrossProduct(e.top, pt, prev->top)) return;
|
||||
|
||||
if (e.outrec->idx == prev->outrec->idx)
|
||||
AddLocalMaxPoly(*prev, e, pt);
|
||||
else if (e.outrec->idx < prev->outrec->idx)
|
||||
|
@ -2670,15 +2661,18 @@ namespace Clipper2Lib {
|
|||
const Point64& pt, bool check_curr_x)
|
||||
{
|
||||
Active* next = e.next_in_ael;
|
||||
if (IsOpen(e) || !IsHotEdge(e) || IsJoined(e) ||
|
||||
if (IsOpen(e) || !IsHotEdge(e) ||
|
||||
!next || IsOpen(*next) || !IsHotEdge(*next) ||
|
||||
pt.y < e.top.y +2 || pt.y < next->top.y +2) // avoids trivial joins
|
||||
return;
|
||||
|
||||
if (check_curr_x) next->curr_x = TopX(*next, pt.y);
|
||||
if (e.curr_x != next->curr_x ||
|
||||
CrossProduct(e.top, pt, next->top)) return;
|
||||
|
||||
if (check_curr_x)
|
||||
{
|
||||
if (DistanceFromLineSqrd(pt, next->bot, next->top) > 0.35) return;
|
||||
}
|
||||
else if (e.curr_x != next->curr_x) return;
|
||||
if (CrossProduct(e.top, pt, next->top)) return;
|
||||
|
||||
if (e.outrec->idx == next->outrec->idx)
|
||||
AddLocalMaxPoly(e, *next, pt);
|
||||
else if (e.outrec->idx < next->outrec->idx)
|
||||
|
@ -2711,20 +2705,6 @@ namespace Clipper2Lib {
|
|||
}
|
||||
}
|
||||
|
||||
inline Rect64 GetBounds(const Path64& path)
|
||||
{
|
||||
if (path.empty()) return Rect64();
|
||||
Rect64 result = invalid_rect;
|
||||
for (const Point64& pt : path)
|
||||
{
|
||||
if (pt.x < result.left) result.left = pt.x;
|
||||
if (pt.x > result.right) result.right = pt.x;
|
||||
if (pt.y < result.top) result.top = pt.y;
|
||||
if (pt.y > result.bottom) result.bottom = pt.y;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BuildPath64(OutPt* op, bool reverse, bool isOpen, Path64& path)
|
||||
{
|
||||
if (!op || op->next == op || (!isOpen && op->next == op->prev))
|
||||
|
@ -2791,13 +2771,13 @@ namespace Clipper2Lib {
|
|||
|
||||
while (outrec->owner)
|
||||
if (outrec->owner->bounds.Contains(outrec->bounds) &&
|
||||
Path1InsidePath2(outrec, outrec->owner))
|
||||
Path1InsidePath2(outrec->pts, outrec->owner->pts))
|
||||
break; // found - owner contain outrec!
|
||||
else
|
||||
outrec->owner = outrec->owner->owner;
|
||||
|
||||
if (outrec->owner)
|
||||
outrec->polypath = outrec->owner->polypath->AddChild(outrec->path);
|
||||
outrec->polypath = outrec->owner->polypath->AddChild(outrec->path);
|
||||
else
|
||||
outrec->polypath = polypath->AddChild(outrec->path);
|
||||
}
|
||||
|
@ -2815,7 +2795,7 @@ namespace Clipper2Lib {
|
|||
if (split && split != outrec &&
|
||||
split != outrec->owner && CheckBounds(split) &&
|
||||
split->bounds.Contains(outrec->bounds) &&
|
||||
Path1InsidePath2(outrec, split))
|
||||
Path1InsidePath2(outrec->pts, split->pts))
|
||||
{
|
||||
RecursiveCheckOwners(split, polypath);
|
||||
outrec->owner = split; //found in split
|
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 25 January 2023 *
|
||||
* Date : 23 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : Path Offset (Inflate/Shrink) *
|
||||
|
@ -8,8 +8,8 @@
|
|||
*******************************************************************************/
|
||||
|
||||
#include <cmath>
|
||||
#include "clipper.h"
|
||||
#include "clipper.offset.h"
|
||||
#include "clipper2/clipper.h"
|
||||
#include "clipper2/clipper.offset.h"
|
||||
|
||||
namespace Clipper2Lib {
|
||||
|
||||
|
@ -41,18 +41,17 @@ void GetBoundsAndLowestPolyIdx(const Paths64& paths, Rect64& r, int & idx)
|
|||
if (p.x > r.right) r.right = p.x;
|
||||
else if (p.x < r.left) r.left = p.x;
|
||||
}
|
||||
if (idx < 0) r = Rect64(0, 0, 0, 0);
|
||||
if (r.top == INT64_MIN) r.bottom = r.top;
|
||||
if (r.left == INT64_MIN) r.left = r.right;
|
||||
//if (idx < 0) r = Rect64(0, 0, 0, 0);
|
||||
//if (r.top == INT64_MIN) r.bottom = r.top;
|
||||
//if (r.left == INT64_MIN) r.left = r.right;
|
||||
}
|
||||
|
||||
bool IsSafeOffset(const Rect64& r, int64_t delta)
|
||||
bool IsSafeOffset(const Rect64& r, double abs_delta)
|
||||
{
|
||||
if (delta < 0) return true;
|
||||
return r.left > INT64_MIN + delta &&
|
||||
r.right < INT64_MAX - delta &&
|
||||
r.top > INT64_MIN + delta &&
|
||||
r.bottom < INT64_MAX - delta;
|
||||
return r.left > min_coord + abs_delta &&
|
||||
r.right < max_coord - abs_delta &&
|
||||
r.top > min_coord + abs_delta &&
|
||||
r.bottom < max_coord - abs_delta;
|
||||
}
|
||||
|
||||
PointD GetUnitNormal(const Point64& pt1, const Point64& pt2)
|
||||
|
@ -99,12 +98,32 @@ inline bool IsClosedPath(EndType et)
|
|||
|
||||
inline Point64 GetPerpendic(const Point64& pt, const PointD& norm, double delta)
|
||||
{
|
||||
#if USINGZ
|
||||
return Point64(pt.x + norm.x * delta, pt.y + norm.y * delta, pt.z);
|
||||
#else
|
||||
return Point64(pt.x + norm.x * delta, pt.y + norm.y * delta);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline PointD GetPerpendicD(const Point64& pt, const PointD& norm, double delta)
|
||||
{
|
||||
#if USINGZ
|
||||
return PointD(pt.x + norm.x * delta, pt.y + norm.y * delta, pt.z);
|
||||
#else
|
||||
return PointD(pt.x + norm.x * delta, pt.y + norm.y * delta);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void NegatePath(PathD& path)
|
||||
{
|
||||
for (PointD& pt : path)
|
||||
{
|
||||
pt.x = -pt.x;
|
||||
pt.y = -pt.y;
|
||||
#if USINGZ
|
||||
pt.z = pt.z;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -150,12 +169,20 @@ void ClipperOffset::BuildNormals(const Path64& path)
|
|||
|
||||
inline PointD TranslatePoint(const PointD& pt, double dx, double dy)
|
||||
{
|
||||
#if USINGZ
|
||||
return PointD(pt.x + dx, pt.y + dy, pt.z);
|
||||
#else
|
||||
return PointD(pt.x + dx, pt.y + dy);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline PointD ReflectPoint(const PointD& pt, const PointD& pivot)
|
||||
{
|
||||
#if USINGZ
|
||||
return PointD(pivot.x + (pivot.x - pt.x), pivot.y + (pivot.y - pt.y), pt.z);
|
||||
#else
|
||||
return PointD(pivot.x + (pivot.x - pt.x), pivot.y + (pivot.y - pt.y));
|
||||
#endif
|
||||
}
|
||||
|
||||
PointD IntersectPoint(const PointD& pt1a, const PointD& pt1b,
|
||||
|
@ -209,51 +236,72 @@ void ClipperOffset::DoSquare(Group& group, const Path64& path, size_t j, size_t
|
|||
{
|
||||
PointD pt4 = PointD(pt3.x + vec.x * group_delta_, pt3.y + vec.y * group_delta_);
|
||||
PointD pt = IntersectPoint(pt1, pt2, pt3, pt4);
|
||||
#if USINGZ
|
||||
pt.z = ptQ.z;
|
||||
#endif
|
||||
//get the second intersect point through reflecion
|
||||
group.path_.push_back(Point64(ReflectPoint(pt, ptQ)));
|
||||
group.path_.push_back(Point64(pt));
|
||||
group.path.push_back(Point64(ReflectPoint(pt, ptQ)));
|
||||
group.path.push_back(Point64(pt));
|
||||
}
|
||||
else
|
||||
{
|
||||
PointD pt4 = GetPerpendicD(path[j], norms[k], group_delta_);
|
||||
PointD pt = IntersectPoint(pt1, pt2, pt3, pt4);
|
||||
group.path_.push_back(Point64(pt));
|
||||
#if USINGZ
|
||||
pt.z = ptQ.z;
|
||||
#endif
|
||||
group.path.push_back(Point64(pt));
|
||||
//get the second intersect point through reflecion
|
||||
group.path_.push_back(Point64(ReflectPoint(pt, ptQ)));
|
||||
group.path.push_back(Point64(ReflectPoint(pt, ptQ)));
|
||||
}
|
||||
}
|
||||
|
||||
void ClipperOffset::DoMiter(Group& group, const Path64& path, size_t j, size_t k, double cos_a)
|
||||
{
|
||||
double q = group_delta_ / (cos_a + 1);
|
||||
group.path_.push_back(Point64(
|
||||
#if USINGZ
|
||||
group.path.push_back(Point64(
|
||||
path[j].x + (norms[k].x + norms[j].x) * q,
|
||||
path[j].y + (norms[k].y + norms[j].y) * q,
|
||||
path[j].z));
|
||||
#else
|
||||
group.path.push_back(Point64(
|
||||
path[j].x + (norms[k].x + norms[j].x) * q,
|
||||
path[j].y + (norms[k].y + norms[j].y) * q));
|
||||
#endif
|
||||
}
|
||||
|
||||
void ClipperOffset::DoRound(Group& group, const Path64& path, size_t j, size_t k, double angle)
|
||||
{
|
||||
//even though angle may be negative this is a convex join
|
||||
Point64 pt = path[j];
|
||||
int steps = static_cast<int>(std::floor(steps_per_rad_ * std::abs(angle)));
|
||||
double step_sin = std::sin(angle / steps);
|
||||
double step_cos = std::cos(angle / steps);
|
||||
|
||||
PointD pt2 = PointD(norms[k].x * group_delta_, norms[k].y * group_delta_);
|
||||
if (j == k) pt2.Negate();
|
||||
|
||||
group.path_.push_back(Point64(pt.x + pt2.x, pt.y + pt2.y));
|
||||
for (int i = 0; i < steps; ++i)
|
||||
PointD offsetVec = PointD(norms[k].x * group_delta_, norms[k].y * group_delta_);
|
||||
if (j == k) offsetVec.Negate();
|
||||
#if USINGZ
|
||||
group.path.push_back(Point64(pt.x + offsetVec.x, pt.y + offsetVec.y, pt.z));
|
||||
#else
|
||||
group.path.push_back(Point64(pt.x + offsetVec.x, pt.y + offsetVec.y));
|
||||
#endif
|
||||
if (angle > -PI + 0.01) // avoid 180deg concave
|
||||
{
|
||||
pt2 = PointD(pt2.x * step_cos - step_sin * pt2.y,
|
||||
pt2.x * step_sin + pt2.y * step_cos);
|
||||
group.path_.push_back(Point64(pt.x + pt2.x, pt.y + pt2.y));
|
||||
int steps = std::max(2, static_cast<int>(
|
||||
std::floor(steps_per_rad_ * std::abs(angle))));
|
||||
double step_sin = std::sin(angle / steps);
|
||||
double step_cos = std::cos(angle / steps);
|
||||
for (int i = 1; i < steps; ++i) // ie 1 less than steps
|
||||
{
|
||||
offsetVec = PointD(offsetVec.x * step_cos - step_sin * offsetVec.y,
|
||||
offsetVec.x * step_sin + offsetVec.y * step_cos);
|
||||
#if USINGZ
|
||||
group.path.push_back(Point64(pt.x + offsetVec.x, pt.y + offsetVec.y, pt.z));
|
||||
#else
|
||||
group.path.push_back(Point64(pt.x + offsetVec.x, pt.y + offsetVec.y));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
group.path_.push_back(GetPerpendic(path[j], norms[j], group_delta_));
|
||||
group.path.push_back(GetPerpendic(path[j], norms[j], group_delta_));
|
||||
}
|
||||
|
||||
void ClipperOffset::OffsetPoint(Group& group,
|
||||
Path64& path, size_t j, size_t& k, bool reversing)
|
||||
void ClipperOffset::OffsetPoint(Group& group, Path64& path, size_t j, size_t& k)
|
||||
{
|
||||
// Let A = change in angle where edges join
|
||||
// A == 0: ie no change in angle (flat join)
|
||||
|
@ -268,66 +316,77 @@ void ClipperOffset::OffsetPoint(Group& group,
|
|||
if (sin_a > 1.0) sin_a = 1.0;
|
||||
else if (sin_a < -1.0) sin_a = -1.0;
|
||||
|
||||
bool almostNoAngle = AlmostZero(cos_a - 1);
|
||||
bool is180DegSpike = AlmostZero(cos_a + 1) && reversing;
|
||||
// when there's almost no angle of deviation or it's concave
|
||||
if (almostNoAngle || is180DegSpike || (sin_a * group_delta_ < 0))
|
||||
if (AlmostZero(cos_a - 1, 0.01)) // almost straight
|
||||
{
|
||||
//almost no angle or concave
|
||||
group.path_.push_back(GetPerpendic(path[j], norms[k], group_delta_));
|
||||
// create a simple self-intersection that will be cleaned up later
|
||||
if (!almostNoAngle) group.path_.push_back(path[j]);
|
||||
group.path_.push_back(GetPerpendic(path[j], norms[j], group_delta_));
|
||||
group.path.push_back(GetPerpendic(path[j], norms[k], group_delta_));
|
||||
group.path.push_back(GetPerpendic(path[j], norms[j], group_delta_)); // (#418)
|
||||
}
|
||||
else
|
||||
else if (!AlmostZero(cos_a + 1, 0.01) && (sin_a * group_delta_ < 0))
|
||||
{
|
||||
// it's convex
|
||||
if (join_type_ == JoinType::Round)
|
||||
DoRound(group, path, j, k, std::atan2(sin_a, cos_a));
|
||||
else if (join_type_ == JoinType::Miter)
|
||||
{
|
||||
// miter unless the angle is so acute the miter would exceeds ML
|
||||
if (cos_a > temp_lim_ - 1) DoMiter(group, path, j, k, cos_a);
|
||||
else DoSquare(group, path, j, k);
|
||||
}
|
||||
// don't bother squaring angles that deviate < ~20 degrees because
|
||||
// squaring will be indistinguishable from mitering and just be a lot slower
|
||||
else if (cos_a > 0.9)
|
||||
DoMiter(group, path, j, k, cos_a);
|
||||
else
|
||||
DoSquare(group, path, j, k);
|
||||
// is concave
|
||||
group.path.push_back(GetPerpendic(path[j], norms[k], group_delta_));
|
||||
// this extra point is the only (simple) way to ensure that
|
||||
// path reversals are fully cleaned with the trailing clipper
|
||||
group.path.push_back(path[j]); // (#405)
|
||||
group.path.push_back(GetPerpendic(path[j], norms[j], group_delta_));
|
||||
}
|
||||
else if (join_type_ == JoinType::Round)
|
||||
DoRound(group, path, j, k, std::atan2(sin_a, cos_a));
|
||||
else if (join_type_ == JoinType::Miter)
|
||||
{
|
||||
// miter unless the angle is so acute the miter would exceeds ML
|
||||
if (cos_a > temp_lim_ - 1) DoMiter(group, path, j, k, cos_a);
|
||||
else DoSquare(group, path, j, k);
|
||||
}
|
||||
// don't bother squaring angles that deviate < ~20 degrees because
|
||||
// squaring will be indistinguishable from mitering and just be a lot slower
|
||||
else if (cos_a > 0.9)
|
||||
DoMiter(group, path, j, k, cos_a);
|
||||
else
|
||||
DoSquare(group, path, j, k);
|
||||
|
||||
k = j;
|
||||
}
|
||||
|
||||
void ClipperOffset::OffsetPolygon(Group& group, Path64& path)
|
||||
{
|
||||
group.path_.clear();
|
||||
for (Path64::size_type i = 0, j = path.size() -1; i < path.size(); j = i, ++i)
|
||||
OffsetPoint(group, path, i, j);
|
||||
group.paths_out_.push_back(group.path_);
|
||||
group.paths_out.push_back(group.path);
|
||||
}
|
||||
|
||||
void ClipperOffset::OffsetOpenJoined(Group& group, Path64& path)
|
||||
{
|
||||
OffsetPolygon(group, path);
|
||||
std::reverse(path.begin(), path.end());
|
||||
BuildNormals(path);
|
||||
|
||||
//rebuild normals // BuildNormals(path);
|
||||
std::reverse(norms.begin(), norms.end());
|
||||
norms.push_back(norms[0]);
|
||||
norms.erase(norms.begin());
|
||||
NegatePath(norms);
|
||||
|
||||
group.path.clear();
|
||||
OffsetPolygon(group, path);
|
||||
}
|
||||
|
||||
void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)
|
||||
void ClipperOffset::OffsetOpenPath(Group& group, Path64& path)
|
||||
{
|
||||
group.path_.clear();
|
||||
|
||||
// do the line start cap
|
||||
switch (end_type)
|
||||
switch (end_type_)
|
||||
{
|
||||
case EndType::Butt:
|
||||
group.path_.push_back(Point64(
|
||||
#if USINGZ
|
||||
group.path.push_back(Point64(
|
||||
path[0].x - norms[0].x * group_delta_,
|
||||
path[0].y - norms[0].y * group_delta_,
|
||||
path[0].z));
|
||||
#else
|
||||
group.path.push_back(Point64(
|
||||
path[0].x - norms[0].x * group_delta_,
|
||||
path[0].y - norms[0].y * group_delta_));
|
||||
group.path_.push_back(GetPerpendic(path[0], norms[0], group_delta_));
|
||||
#endif
|
||||
group.path.push_back(GetPerpendic(path[0], norms[0], group_delta_));
|
||||
break;
|
||||
case EndType::Round:
|
||||
DoRound(group, path, 0,0, PI);
|
||||
|
@ -349,13 +408,20 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)
|
|||
norms[0] = norms[highI];
|
||||
|
||||
// do the line end cap
|
||||
switch (end_type)
|
||||
switch (end_type_)
|
||||
{
|
||||
case EndType::Butt:
|
||||
group.path_.push_back(Point64(
|
||||
#if USINGZ
|
||||
group.path.push_back(Point64(
|
||||
path[highI].x - norms[highI].x * group_delta_,
|
||||
path[highI].y - norms[highI].y * group_delta_,
|
||||
path[highI].z));
|
||||
#else
|
||||
group.path.push_back(Point64(
|
||||
path[highI].x - norms[highI].x * group_delta_,
|
||||
path[highI].y - norms[highI].y * group_delta_));
|
||||
group.path_.push_back(GetPerpendic(path[highI], norms[highI], group_delta_));
|
||||
#endif
|
||||
group.path.push_back(GetPerpendic(path[highI], norms[highI], group_delta_));
|
||||
break;
|
||||
case EndType::Round:
|
||||
DoRound(group, path, highI, highI, PI);
|
||||
|
@ -366,95 +432,128 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)
|
|||
}
|
||||
|
||||
for (size_t i = highI, k = 0; i > 0; --i)
|
||||
OffsetPoint(group, path, i, k, true);
|
||||
group.paths_out_.push_back(group.path_);
|
||||
OffsetPoint(group, path, i, k);
|
||||
group.paths_out.push_back(group.path);
|
||||
}
|
||||
|
||||
void ClipperOffset::DoGroupOffset(Group& group, double delta)
|
||||
void ClipperOffset::DoGroupOffset(Group& group)
|
||||
{
|
||||
if (group.end_type_ != EndType::Polygon) delta = std::abs(delta) * 0.5;
|
||||
bool isClosedPaths = IsClosedPath(group.end_type_);
|
||||
|
||||
|
||||
Rect64 r;
|
||||
int idx = -1;
|
||||
//the lowermost polygon must be an outer polygon. So we can use that as the
|
||||
//designated orientation for outer polygons (needed for tidy-up clipping)
|
||||
Rect64 r;
|
||||
int idx = 0;
|
||||
GetBoundsAndLowestPolyIdx(group.paths_in_, r, idx);
|
||||
if (!IsSafeOffset(r, static_cast<int64_t>(std::ceil(delta))))
|
||||
throw "Range error - the offset delta is too large";
|
||||
GetBoundsAndLowestPolyIdx(group.paths_in, r, idx);
|
||||
if (idx < 0) return;
|
||||
|
||||
if (isClosedPaths)
|
||||
if (group.end_type == EndType::Polygon)
|
||||
{
|
||||
double area = Area(group.paths_in_[idx]);
|
||||
if (area == 0) return;
|
||||
group.is_reversed_ = (area < 0);
|
||||
if (group.is_reversed_) delta = -delta;
|
||||
double area = Area(group.paths_in[idx]);
|
||||
if (area == 0) return;
|
||||
group.is_reversed = (area < 0);
|
||||
if (group.is_reversed) group_delta_ = -delta_;
|
||||
else group_delta_ = delta_;
|
||||
}
|
||||
else
|
||||
group.is_reversed_ = false;
|
||||
|
||||
group_delta_ = delta;
|
||||
abs_group_delta_ = std::abs(group_delta_);
|
||||
join_type_ = group.join_type_;
|
||||
|
||||
double arcTol = (arc_tolerance_ > floating_point_tolerance ? arc_tolerance_
|
||||
: std::log10(2 + abs_group_delta_) * default_arc_tolerance); // empirically derived
|
||||
|
||||
//calculate a sensible number of steps (for 360 deg for the given offset
|
||||
if (group.join_type_ == JoinType::Round || group.end_type_ == EndType::Round)
|
||||
{
|
||||
steps_per_rad_ = PI / std::acos(1 - arcTol / abs_group_delta_) / (PI *2);
|
||||
group.is_reversed = false;
|
||||
group_delta_ = std::abs(delta_) * 0.5;
|
||||
}
|
||||
abs_group_delta_ = std::fabs(group_delta_);
|
||||
|
||||
// do range checking
|
||||
if (!IsSafeOffset(r, abs_group_delta_))
|
||||
{
|
||||
DoError(range_error_i);
|
||||
error_code_ |= range_error_i;
|
||||
return;
|
||||
}
|
||||
|
||||
bool is_closed_path = IsClosedPath(group.end_type_);
|
||||
Paths64::const_iterator path_iter;
|
||||
for(path_iter = group.paths_in_.cbegin(); path_iter != group.paths_in_.cend(); ++path_iter)
|
||||
{
|
||||
Path64 path = StripDuplicates(*path_iter, is_closed_path);
|
||||
Path64::size_type cnt = path.size();
|
||||
if (cnt == 0) continue;
|
||||
join_type_ = group.join_type;
|
||||
end_type_ = group.end_type;
|
||||
|
||||
//calculate a sensible number of steps (for 360 deg for the given offset
|
||||
if (group.join_type == JoinType::Round || group.end_type == EndType::Round)
|
||||
{
|
||||
// arcTol - when arc_tolerance_ is undefined (0), the amount of
|
||||
// curve imprecision that's allowed is based on the size of the
|
||||
// offset (delta). Obviously very large offsets will almost always
|
||||
// require much less precision. See also offset_triginometry2.svg
|
||||
double arcTol = (arc_tolerance_ > floating_point_tolerance ?
|
||||
std::min(abs_group_delta_, arc_tolerance_) :
|
||||
std::log10(2 + abs_group_delta_) * default_arc_tolerance);
|
||||
steps_per_rad_ = 0.5 / std::acos(1 - arcTol / abs_group_delta_);
|
||||
}
|
||||
|
||||
bool is_joined =
|
||||
(end_type_ == EndType::Polygon) ||
|
||||
(end_type_ == EndType::Joined);
|
||||
Paths64::const_iterator path_iter;
|
||||
for(path_iter = group.paths_in.cbegin(); path_iter != group.paths_in.cend(); ++path_iter)
|
||||
{
|
||||
Path64 path = StripDuplicates(*path_iter, is_joined);
|
||||
Path64::size_type cnt = path.size();
|
||||
if (cnt == 0 || ((cnt < 3) && group.end_type == EndType::Polygon))
|
||||
continue;
|
||||
|
||||
group.path.clear();
|
||||
if (cnt == 1) // single point - only valid with open paths
|
||||
{
|
||||
group.path_ = Path64();
|
||||
if (group_delta_ < 1) continue;
|
||||
//single vertex so build a circle or square ...
|
||||
if (group.join_type_ == JoinType::Round)
|
||||
if (group.join_type == JoinType::Round)
|
||||
{
|
||||
double radius = abs_group_delta_;
|
||||
group.path_ = Ellipse(path[0], radius, radius);
|
||||
group.path = Ellipse(path[0], radius, radius);
|
||||
#if USINGZ
|
||||
for (auto& p : group.path) p.z = path[0].z;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
int d = (int)std::ceil(abs_group_delta_);
|
||||
r = Rect64(path[0].x - d, path[0].y - d, path[0].x + d, path[0].y + d);
|
||||
group.path_ = r.AsPath();
|
||||
group.path = r.AsPath();
|
||||
#if USINGZ
|
||||
for (auto& p : group.path) p.z = path[0].z;
|
||||
#endif
|
||||
}
|
||||
group.paths_out_.push_back(group.path_);
|
||||
group.paths_out.push_back(group.path);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((cnt == 2) && (group.end_type == EndType::Joined))
|
||||
{
|
||||
if (group.join_type == JoinType::Round)
|
||||
end_type_ = EndType::Round;
|
||||
else
|
||||
end_type_ = EndType::Square;
|
||||
}
|
||||
|
||||
BuildNormals(path);
|
||||
if (group.end_type_ == EndType::Polygon) OffsetPolygon(group, path);
|
||||
else if (group.end_type_ == EndType::Joined) OffsetOpenJoined(group, path);
|
||||
else OffsetOpenPath(group, path, group.end_type_);
|
||||
if (end_type_ == EndType::Polygon) OffsetPolygon(group, path);
|
||||
else if (end_type_ == EndType::Joined) OffsetOpenJoined(group, path);
|
||||
else OffsetOpenPath(group, path);
|
||||
}
|
||||
}
|
||||
solution.reserve(solution.size() + group.paths_out_.size());
|
||||
copy(group.paths_out_.begin(), group.paths_out_.end(), back_inserter(solution));
|
||||
group.paths_out_.clear();
|
||||
solution.reserve(solution.size() + group.paths_out.size());
|
||||
copy(group.paths_out.begin(), group.paths_out.end(), back_inserter(solution));
|
||||
group.paths_out.clear();
|
||||
}
|
||||
|
||||
Paths64 ClipperOffset::Execute(double delta)
|
||||
{
|
||||
error_code_ = 0;
|
||||
solution.clear();
|
||||
if (groups_.size() == 0) return solution;
|
||||
|
||||
if (std::abs(delta) < default_arc_tolerance)
|
||||
if (std::abs(delta) < 0.5)
|
||||
{
|
||||
for (const Group& group : groups_)
|
||||
{
|
||||
solution.reserve(solution.size() + group.paths_in_.size());
|
||||
copy(group.paths_in_.begin(), group.paths_in_.end(), back_inserter(solution));
|
||||
solution.reserve(solution.size() + group.paths_in.size());
|
||||
copy(group.paths_in.begin(), group.paths_in.end(), back_inserter(solution));
|
||||
}
|
||||
return solution;
|
||||
}
|
||||
|
@ -462,22 +561,32 @@ Paths64 ClipperOffset::Execute(double delta)
|
|||
temp_lim_ = (miter_limit_ <= 1) ?
|
||||
2.0 :
|
||||
2.0 / (miter_limit_ * miter_limit_);
|
||||
|
||||
|
||||
delta_ = delta;
|
||||
std::vector<Group>::iterator git;
|
||||
for (git = groups_.begin(); git != groups_.end(); ++git)
|
||||
DoGroupOffset(*git, delta);
|
||||
{
|
||||
DoGroupOffset(*git);
|
||||
if (!error_code_) continue; // all OK
|
||||
solution.clear();
|
||||
return solution;
|
||||
}
|
||||
|
||||
//clean up self-intersections ...
|
||||
Clipper64 c;
|
||||
c.PreserveCollinear = false;
|
||||
//the solution should retain the orientation of the input
|
||||
c.ReverseSolution = reverse_solution_ != groups_[0].is_reversed_;
|
||||
c.ReverseSolution = reverse_solution_ != groups_[0].is_reversed;
|
||||
#if USINGZ
|
||||
if (zCallback64_) {
|
||||
c.SetZCallback(zCallback64_);
|
||||
}
|
||||
#endif
|
||||
c.AddSubject(solution);
|
||||
if (groups_[0].is_reversed_)
|
||||
if (groups_[0].is_reversed)
|
||||
c.Execute(ClipType::Union, FillRule::Negative, solution);
|
||||
else
|
||||
c.Execute(ClipType::Union, FillRule::Positive, solution);
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
|
@ -0,0 +1,976 @@
|
|||
/*******************************************************************************
|
||||
* Author : Angus Johnson *
|
||||
* Date : 14 February 2023 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2023 *
|
||||
* Purpose : FAST rectangular clipping *
|
||||
* License : http://www.boost.org/LICENSE_1_0.txt *
|
||||
*******************************************************************************/
|
||||
|
||||
#include <cmath>
|
||||
#include "clipper2/clipper.h"
|
||||
#include "clipper2/clipper.rectclip.h"
|
||||
|
||||
namespace Clipper2Lib {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Miscellaneous methods
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
inline bool Path1ContainsPath2(const Path64& path1, const Path64& path2)
|
||||
{
|
||||
int io_count = 0;
|
||||
// precondition: no (significant) overlap
|
||||
for (const Point64& pt : path2)
|
||||
{
|
||||
PointInPolygonResult pip = PointInPolygon(pt, path1);
|
||||
switch (pip)
|
||||
{
|
||||
case PointInPolygonResult::IsOutside: ++io_count; break;
|
||||
case PointInPolygonResult::IsInside: --io_count; break;
|
||||
default: continue;
|
||||
}
|
||||
if (std::abs(io_count) > 1) break;
|
||||
}
|
||||
return io_count <= 0;
|
||||
}
|
||||
|
||||
inline bool GetLocation(const Rect64& rec,
|
||||
const Point64& pt, Location& loc)
|
||||
{
|
||||
if (pt.x == rec.left && pt.y >= rec.top && pt.y <= rec.bottom)
|
||||
{
|
||||
loc = Location::Left;
|
||||
return false;
|
||||
}
|
||||
else if (pt.x == rec.right && pt.y >= rec.top && pt.y <= rec.bottom)
|
||||
{
|
||||
loc = Location::Right;
|
||||
return false;
|
||||
}
|
||||
else if (pt.y == rec.top && pt.x >= rec.left && pt.x <= rec.right)
|
||||
{
|
||||
loc = Location::Top;
|
||||
return false;
|
||||
}
|
||||
else if (pt.y == rec.bottom && pt.x >= rec.left && pt.x <= rec.right)
|
||||
{
|
||||
loc = Location::Bottom;
|
||||
return false;
|
||||
}
|
||||
else if (pt.x < rec.left) loc = Location::Left;
|
||||
else if (pt.x > rec.right) loc = Location::Right;
|
||||
else if (pt.y < rec.top) loc = Location::Top;
|
||||
else if (pt.y > rec.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool GetIntersection(const Path64& rectPath,
|
||||
const Point64& p, const Point64& p2, Location& loc, Point64& ip)
|
||||
{
|
||||
// gets the intersection closest to 'p'
|
||||
// when Result = false, loc will remain unchanged
|
||||
switch (loc)
|
||||
{
|
||||
case Location::Left:
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
else if (p.y < rectPath[0].y &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Top:
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
else if (p.x < rectPath[0].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (p.x > rectPath[1].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Right:
|
||||
if (SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
else if (p.y < rectPath[0].y &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
case Location::Bottom:
|
||||
if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
else if (p.x < rectPath[3].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (p.x > rectPath[2].x &&
|
||||
SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
|
||||
default: // loc == rInside
|
||||
if (SegmentsIntersect(p, p2, rectPath[0], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[3], ip);
|
||||
loc = Location::Left;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[0], rectPath[1], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[0], rectPath[1], ip);
|
||||
loc = Location::Top;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[1], rectPath[2], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[1], rectPath[2], ip);
|
||||
loc = Location::Right;
|
||||
}
|
||||
else if (SegmentsIntersect(p, p2, rectPath[2], rectPath[3], true))
|
||||
{
|
||||
GetIntersectPoint(p, p2, rectPath[2], rectPath[3], ip);
|
||||
loc = Location::Bottom;
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline Location GetAdjacentLocation(Location loc, bool isClockwise)
|
||||
{
|
||||
int delta = (isClockwise) ? 1 : 3;
|
||||
return static_cast<Location>((static_cast<int>(loc) + delta) % 4);
|
||||
}
|
||||
|
||||
inline bool HeadingClockwise(Location prev, Location curr)
|
||||
{
|
||||
return (static_cast<int>(prev) + 1) % 4 == static_cast<int>(curr);
|
||||
}
|
||||
|
||||
inline bool AreOpposites(Location prev, Location curr)
|
||||
{
|
||||
return abs(static_cast<int>(prev) - static_cast<int>(curr)) == 2;
|
||||
}
|
||||
|
||||
inline bool IsClockwise(Location prev, Location curr,
|
||||
const Point64& prev_pt, const Point64& curr_pt, const Point64& rect_mp)
|
||||
{
|
||||
if (AreOpposites(prev, curr))
|
||||
return CrossProduct(prev_pt, rect_mp, curr_pt) < 0;
|
||||
else
|
||||
return HeadingClockwise(prev, curr);
|
||||
}
|
||||
|
||||
inline OutPt2* UnlinkOp(OutPt2* op)
|
||||
{
|
||||
if (op->next == op) return nullptr;
|
||||
op->prev->next = op->next;
|
||||
op->next->prev = op->prev;
|
||||
return op->next;
|
||||
}
|
||||
|
||||
inline OutPt2* UnlinkOpBack(OutPt2* op)
|
||||
{
|
||||
if (op->next == op) return nullptr;
|
||||
op->prev->next = op->next;
|
||||
op->next->prev = op->prev;
|
||||
return op->prev;
|
||||
}
|
||||
|
||||
inline uint32_t GetEdgesForPt(const Point64& pt, const Rect64& rec)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
if (pt.x == rec.left) result = 1;
|
||||
else if (pt.x == rec.right) result = 4;
|
||||
if (pt.y == rec.top) result += 2;
|
||||
else if (pt.y == rec.bottom) result += 8;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool IsHeadingClockwise(const Point64& pt1, const Point64& pt2, int edgeIdx)
|
||||
{
|
||||
switch (edgeIdx)
|
||||
{
|
||||
case 0: return pt2.y < pt1.y;
|
||||
case 1: return pt2.x > pt1.x;
|
||||
case 2: return pt2.y > pt1.y;
|
||||
default: return pt2.x < pt1.x;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool HasHorzOverlap(const Point64& left1, const Point64& right1,
|
||||
const Point64& left2, const Point64& right2)
|
||||
{
|
||||
return (left1.x < right2.x) && (right1.x > left2.x);
|
||||
}
|
||||
|
||||
inline bool HasVertOverlap(const Point64& top1, const Point64& bottom1,
|
||||
const Point64& top2, const Point64& bottom2)
|
||||
{
|
||||
return (top1.y < bottom2.y) && (bottom1.y > top2.y);
|
||||
}
|
||||
|
||||
inline void AddToEdge(OutPt2List& edge, OutPt2* op)
|
||||
{
|
||||
if (op->edge) return;
|
||||
op->edge = &edge;
|
||||
edge.push_back(op);
|
||||
}
|
||||
|
||||
inline void UncoupleEdge(OutPt2* op)
|
||||
{
|
||||
if (!op->edge) return;
|
||||
for (size_t i = 0; i < op->edge->size(); ++i)
|
||||
{
|
||||
OutPt2* op2 = (*op->edge)[i];
|
||||
if (op2 == op)
|
||||
{
|
||||
(*op->edge)[i] = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
op->edge = nullptr;
|
||||
}
|
||||
|
||||
inline void SetNewOwner(OutPt2* op, size_t new_idx)
|
||||
{
|
||||
op->owner_idx = new_idx;
|
||||
OutPt2* op2 = op->next;
|
||||
while (op2 != op)
|
||||
{
|
||||
op2->owner_idx = new_idx;
|
||||
op2 = op2->next;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// RectClip64
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
OutPt2* RectClip::Add(Point64 pt, bool start_new)
|
||||
{
|
||||
// this method is only called by InternalExecute.
|
||||
// Later splitting & rejoining won't create additional op's,
|
||||
// though they will change the (non-storage) results_ count.
|
||||
int curr_idx = static_cast<int>(results_.size()) - 1;
|
||||
OutPt2* result;
|
||||
if (curr_idx < 0 || start_new)
|
||||
{
|
||||
result = &op_container_.emplace_back(OutPt2());
|
||||
result->pt = pt;
|
||||
result->next = result;
|
||||
result->prev = result;
|
||||
results_.push_back(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
OutPt2* prevOp = results_[curr_idx];
|
||||
if (prevOp->pt == pt) return prevOp;
|
||||
result = &op_container_.emplace_back(OutPt2());
|
||||
result->owner_idx = curr_idx;
|
||||
result->pt = pt;
|
||||
result->next = prevOp->next;
|
||||
prevOp->next->prev = result;
|
||||
prevOp->next = result;
|
||||
result->prev = prevOp;
|
||||
results_[curr_idx] = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void RectClip::AddCorner(Location prev, Location curr)
|
||||
{
|
||||
if (HeadingClockwise(prev, curr))
|
||||
Add(rect_as_path_[static_cast<int>(prev)]);
|
||||
else
|
||||
Add(rect_as_path_[static_cast<int>(curr)]);
|
||||
}
|
||||
|
||||
void RectClip::AddCorner(Location& loc, bool isClockwise)
|
||||
{
|
||||
if (isClockwise)
|
||||
{
|
||||
Add(rect_as_path_[static_cast<int>(loc)]);
|
||||
loc = GetAdjacentLocation(loc, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
loc = GetAdjacentLocation(loc, false);
|
||||
Add(rect_as_path_[static_cast<int>(loc)]);
|
||||
}
|
||||
}
|
||||
|
||||
void RectClip::GetNextLocation(const Path64& path,
|
||||
Location& loc, int& i, int highI)
|
||||
{
|
||||
switch (loc)
|
||||
{
|
||||
case Location::Left:
|
||||
while (i <= highI && path[i].x <= rect_.left) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Top:
|
||||
while (i <= highI && path[i].y <= rect_.top) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Right:
|
||||
while (i <= highI && path[i].x >= rect_.right) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].y >= rect_.bottom) loc = Location::Bottom;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Bottom:
|
||||
while (i <= highI && path[i].y >= rect_.bottom) ++i;
|
||||
if (i > highI) break;
|
||||
else if (path[i].y <= rect_.top) loc = Location::Top;
|
||||
else if (path[i].x <= rect_.left) loc = Location::Left;
|
||||
else if (path[i].x >= rect_.right) loc = Location::Right;
|
||||
else loc = Location::Inside;
|
||||
break;
|
||||
|
||||
case Location::Inside:
|
||||
while (i <= highI)
|
||||
{
|
||||
if (path[i].x < rect_.left) loc = Location::Left;
|
||||
else if (path[i].x > rect_.right) loc = Location::Right;
|
||||
else if (path[i].y > rect_.bottom) loc = Location::Bottom;
|
||||
else if (path[i].y < rect_.top) loc = Location::Top;
|
||||
else { Add(path[i]); ++i; continue; }
|
||||
break; //inner loop
|
||||
}
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
void RectClip::ExecuteInternal(const Path64& path)
|
||||
{
|
||||
int i = 0, highI = static_cast<int>(path.size()) - 1;
|
||||
Location prev = Location::Inside, loc;
|
||||
Location crossing_loc = Location::Inside;
|
||||
Location first_cross_ = Location::Inside;
|
||||
if (!GetLocation(rect_, path[highI], loc))
|
||||
{
|
||||
i = highI - 1;
|
||||
while (i >= 0 && !GetLocation(rect_, path[i], prev)) --i;
|
||||
if (i < 0)
|
||||
{
|
||||
// all of path must be inside fRect
|
||||
for (const auto& pt : path) Add(pt);
|
||||
return;
|
||||
}
|
||||
if (prev == Location::Inside) loc = Location::Inside;
|
||||
i = 0;
|
||||
}
|
||||
Location startingLoc = loc;
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
while (i <= highI)
|
||||
{
|
||||
prev = loc;
|
||||
Location crossing_prev = crossing_loc;
|
||||
|
||||
GetNextLocation(path, loc, i, highI);
|
||||
|
||||
if (i > highI) break;
|
||||
Point64 ip, ip2;
|
||||
Point64 prev_pt = (i) ?
|
||||
path[static_cast<size_t>(i - 1)] :
|
||||
path[highI];
|
||||
|
||||
crossing_loc = loc;
|
||||
if (!GetIntersection(rect_as_path_,
|
||||
path[i], prev_pt, crossing_loc, ip))
|
||||
{
|
||||
// ie remaining outside
|
||||
if (crossing_prev == Location::Inside)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, loc, prev_pt, path[i], rect_mp_);
|
||||
do {
|
||||
start_locs_.push_back(prev);
|
||||
prev = GetAdjacentLocation(prev, isClockw);
|
||||
} while (prev != loc);
|
||||
crossing_loc = crossing_prev; // still not crossed
|
||||
}
|
||||
else if (prev != Location::Inside && prev != loc)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, loc, prev_pt, path[i], rect_mp_);
|
||||
do {
|
||||
AddCorner(prev, isClockw);
|
||||
} while (prev != loc);
|
||||
}
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// we must be crossing the rect boundary to get here
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
if (loc == Location::Inside) // path must be entering rect
|
||||
{
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
first_cross_ = crossing_loc;
|
||||
start_locs_.push_back(prev);
|
||||
}
|
||||
else if (prev != crossing_loc)
|
||||
{
|
||||
bool isClockw = IsClockwise(prev, crossing_loc, prev_pt, path[i], rect_mp_);
|
||||
do {
|
||||
AddCorner(prev, isClockw);
|
||||
} while (prev != crossing_loc);
|
||||
}
|
||||
}
|
||||
else if (prev != Location::Inside)
|
||||
{
|
||||
// passing right through rect. 'ip' here will be the second
|
||||
// intersect pt but we'll also need the first intersect pt (ip2)
|
||||
loc = prev;
|
||||
GetIntersection(rect_as_path_, prev_pt, path[i], loc, ip2);
|
||||
if (crossing_prev != Location::Inside)
|
||||
AddCorner(crossing_prev, loc);
|
||||
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
first_cross_ = loc;
|
||||
start_locs_.push_back(prev);
|
||||
}
|
||||
|
||||
loc = crossing_loc;
|
||||
Add(ip2);
|
||||
if (ip == ip2)
|
||||
{
|
||||
// it's very likely that path[i] is on rect
|
||||
GetLocation(rect_, path[i], loc);
|
||||
AddCorner(crossing_loc, loc);
|
||||
crossing_loc = loc;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else // path must be exiting rect
|
||||
{
|
||||
loc = crossing_loc;
|
||||
if (first_cross_ == Location::Inside)
|
||||
first_cross_ = crossing_loc;
|
||||
}
|
||||
|
||||
Add(ip);
|
||||
|
||||
} //while i <= highI
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
if (first_cross_ == Location::Inside)
|
||||
{
|
||||
// path never intersects
|
||||
if (startingLoc != Location::Inside)
|
||||
{
|
||||
// path is outside rect
|
||||
// but being outside, it still may not contain rect
|
||||
if (path_bounds_.Contains(rect_) &&
|
||||
Path1ContainsPath2(path, rect_as_path_))
|
||||
{
|
||||
// yep, the path does fully contain rect
|
||||
// so add rect to the solution
|
||||
for (size_t j = 0; j < 4; ++j)
|
||||
{
|
||||
Add(rect_as_path_[j]);
|
||||
// we may well need to do some splitting later, so
|
||||
AddToEdge(edges_[j * 2], results_[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (loc != Location::Inside &&
|
||||
(loc != first_cross_ || start_locs_.size() > 2))
|
||||
{
|
||||
if (start_locs_.size() > 0)
|
||||
{
|
||||
prev = loc;
|
||||
for (auto loc2 : start_locs_)
|
||||
{
|
||||
if (prev == loc2) continue;
|
||||
AddCorner(prev, HeadingClockwise(prev, loc2));
|
||||
prev = loc2;
|
||||
}
|
||||
loc = prev;
|
||||
}
|
||||
if (loc != first_cross_)
|
||||
AddCorner(loc, HeadingClockwise(loc, first_cross_));
|
||||
}
|
||||
}
|
||||
|
||||
void RectClip::CheckEdges()
|
||||
{
|
||||
for (size_t i = 0; i < results_.size(); ++i)
|
||||
{
|
||||
OutPt2* op = results_[i];
|
||||
if (!op) continue;
|
||||
OutPt2* op2 = op;
|
||||
do
|
||||
{
|
||||
if (!CrossProduct(op2->prev->pt,
|
||||
op2->pt, op2->next->pt))
|
||||
{
|
||||
if (op2 == op)
|
||||
{
|
||||
op2 = UnlinkOpBack(op2);
|
||||
if (!op2) break;
|
||||
op = op2->prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
op2 = UnlinkOpBack(op2);
|
||||
if (!op2) break;
|
||||
}
|
||||
}
|
||||
else
|
||||
op2 = op2->next;
|
||||
} while (op2 != op);
|
||||
|
||||
if (!op2)
|
||||
{
|
||||
results_[i] = nullptr;
|
||||
continue;
|
||||
}
|
||||
results_[i] = op; // safety first
|
||||
|
||||
uint32_t edgeSet1 = GetEdgesForPt(op->prev->pt, rect_);
|
||||
op2 = op;
|
||||
do
|
||||
{
|
||||
uint32_t edgeSet2 = GetEdgesForPt(op2->pt, rect_);
|
||||
if (edgeSet2 && !op2->edge)
|
||||
{
|
||||
uint32_t combinedSet = (edgeSet1 & edgeSet2);
|
||||
for (int j = 0; j < 4; ++j)
|
||||
{
|
||||
if (combinedSet & (1 << j))
|
||||
{
|
||||
if (IsHeadingClockwise(op2->prev->pt, op2->pt, j))
|
||||
AddToEdge(edges_[j * 2], op2);
|
||||
else
|
||||
AddToEdge(edges_[j * 2 + 1], op2);
|
||||
}
|
||||
}
|
||||
}
|
||||
edgeSet1 = edgeSet2;
|
||||
op2 = op2->next;
|
||||
} while (op2 != op);
|
||||
}
|
||||
}
|
||||
|
||||
void RectClip::TidyEdges(int idx, OutPt2List& cw, OutPt2List& ccw)
|
||||
{
|
||||
if (ccw.empty()) return;
|
||||
bool isHorz = ((idx == 1) || (idx == 3));
|
||||
bool cwIsTowardLarger = ((idx == 1) || (idx == 2));
|
||||
size_t i = 0, j = 0;
|
||||
OutPt2* p1, * p2, * p1a, * p2a, * op, * op2;
|
||||
|
||||
while (i < cw.size())
|
||||
{
|
||||
p1 = cw[i];
|
||||
if (!p1 || p1->next == p1->prev)
|
||||
{
|
||||
cw[i++]->edge = nullptr;
|
||||
j = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t jLim = ccw.size();
|
||||
while (j < jLim &&
|
||||
(!ccw[j] || ccw[j]->next == ccw[j]->prev)) ++j;
|
||||
|
||||
if (j == jLim)
|
||||
{
|
||||
++i;
|
||||
j = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cwIsTowardLarger)
|
||||
{
|
||||
// p1 >>>> p1a;
|
||||
// p2 <<<< p2a;
|
||||
p1 = cw[i]->prev;
|
||||
p1a = cw[i];
|
||||
p2 = ccw[j];
|
||||
p2a = ccw[j]->prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
// p1 <<<< p1a;
|
||||
// p2 >>>> p2a;
|
||||
p1 = cw[i];
|
||||
p1a = cw[i]->prev;
|
||||
p2 = ccw[j]->prev;
|
||||
p2a = ccw[j];
|
||||
}
|
||||
|
||||
if ((isHorz && !HasHorzOverlap(p1->pt, p1a->pt, p2->pt, p2a->pt)) ||
|
||||
(!isHorz && !HasVertOverlap(p1->pt, p1a->pt, p2->pt, p2a->pt)))
|
||||
{
|
||||
++j;
|
||||
continue;
|
||||
}
|
||||
|
||||
// to get here we're either splitting or rejoining
|
||||
bool isRejoining = cw[i]->owner_idx != ccw[j]->owner_idx;
|
||||
|
||||
if (isRejoining)
|
||||
{
|
||||
results_[p2->owner_idx] = nullptr;
|
||||
SetNewOwner(p2, p1->owner_idx);
|
||||
}
|
||||
|
||||
// do the split or re-join
|
||||
if (cwIsTowardLarger)
|
||||
{
|
||||
// p1 >> | >> p1a;
|
||||
// p2 << | << p2a;
|
||||
p1->next = p2;
|
||||
p2->prev = p1;
|
||||
p1a->prev = p2a;
|
||||
p2a->next = p1a;
|
||||
}
|
||||
else
|
||||
{
|
||||
// p1 << | << p1a;
|
||||
// p2 >> | >> p2a;
|
||||
p1->prev = p2;
|
||||
p2->next = p1;
|
||||
p1a->next = p2a;
|
||||
p2a->prev = p1a;
|
||||
}
|
||||
|
||||
if (!isRejoining)
|
||||
{
|
||||
size_t new_idx = results_.size();
|
||||
results_.push_back(p1a);
|
||||
SetNewOwner(p1a, new_idx);
|
||||
}
|
||||
|
||||
if (cwIsTowardLarger)
|
||||
{
|
||||
op = p2;
|
||||
op2 = p1a;
|
||||
}
|
||||
else
|
||||
{
|
||||
op = p1;
|
||||
op2 = p2a;
|
||||
}
|
||||
results_[op->owner_idx] = op;
|
||||
results_[op2->owner_idx] = op2;
|
||||
|
||||
// and now lots of work to get ready for the next loop
|
||||
|
||||
bool opIsLarger, op2IsLarger;
|
||||
if (isHorz) // X
|
||||
{
|
||||
opIsLarger = op->pt.x > op->prev->pt.x;
|
||||
op2IsLarger = op2->pt.x > op2->prev->pt.x;
|
||||
}
|
||||
else // Y
|
||||
{
|
||||
opIsLarger = op->pt.y > op->prev->pt.y;
|
||||
op2IsLarger = op2->pt.y > op2->prev->pt.y;
|
||||
}
|
||||
|
||||
if ((op->next == op->prev) ||
|
||||
(op->pt == op->prev->pt))
|
||||
{
|
||||
if (op2IsLarger == cwIsTowardLarger)
|
||||
{
|
||||
cw[i] = op2;
|
||||
ccw[j++] = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ccw[j] = op2;
|
||||
cw[i++] = nullptr;
|
||||
}
|
||||
}
|
||||
else if ((op2->next == op2->prev) ||
|
||||
(op2->pt == op2->prev->pt))
|
||||
{
|
||||
if (opIsLarger == cwIsTowardLarger)
|
||||
{
|
||||
cw[i] = op;
|
||||
ccw[j++] = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ccw[j] = op;
|
||||
cw[i++] = nullptr;
|
||||
}
|
||||
}
|
||||
else if (opIsLarger == op2IsLarger)
|
||||
{
|
||||
if (opIsLarger == cwIsTowardLarger)
|
||||
{
|
||||
cw[i] = op;
|
||||
UncoupleEdge(op2);
|
||||
AddToEdge(cw, op2);
|
||||
ccw[j++] = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
cw[i++] = nullptr;
|
||||
ccw[j] = op2;
|
||||
UncoupleEdge(op);
|
||||
AddToEdge(ccw, op);
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (opIsLarger == cwIsTowardLarger)
|
||||
cw[i] = op;
|
||||
else
|
||||
ccw[j] = op;
|
||||
if (op2IsLarger == cwIsTowardLarger)
|
||||
cw[i] = op2;
|
||||
else
|
||||
ccw[j] = op2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Path64 RectClip::GetPath(OutPt2*& op)
|
||||
{
|
||||
if (!op || op->next == op->prev) return Path64();
|
||||
|
||||
OutPt2* op2 = op->next;
|
||||
while (op2 && op2 != op)
|
||||
{
|
||||
if (CrossProduct(op2->prev->pt,
|
||||
op2->pt, op2->next->pt) == 0)
|
||||
{
|
||||
op = op2->prev;
|
||||
op2 = UnlinkOp(op2);
|
||||
}
|
||||
else
|
||||
op2 = op2->next;
|
||||
}
|
||||
op = op2; // needed for op cleanup
|
||||
if (!op2) return Path64();
|
||||
|
||||
Path64 result;
|
||||
result.push_back(op->pt);
|
||||
op2 = op->next;
|
||||
while (op2 != op)
|
||||
{
|
||||
result.push_back(op2->pt);
|
||||
op2 = op2->next;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Paths64 RectClip::Execute(const Paths64& paths, bool convex_only)
|
||||
{
|
||||
Paths64 result;
|
||||
if (rect_.IsEmpty()) return result;
|
||||
|
||||
for (const auto& path : paths)
|
||||
{
|
||||
if (path.size() < 3) continue;
|
||||
path_bounds_ = GetBounds(path);
|
||||
if (!rect_.Intersects(path_bounds_))
|
||||
continue; // the path must be completely outside rect_
|
||||
else if (rect_.Contains(path_bounds_))
|
||||
{
|
||||
// the path must be completely inside rect_
|
||||
result.push_back(path);
|
||||
continue;
|
||||
}
|
||||
|
||||
ExecuteInternal(path);
|
||||
if (!convex_only)
|
||||
{
|
||||
CheckEdges();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
TidyEdges(i, edges_[i * 2], edges_[i * 2 + 1]);
|
||||
}
|
||||
|
||||
for (OutPt2*& op : results_)
|
||||
{
|
||||
Path64 tmp = GetPath(op);
|
||||
if (!tmp.empty())
|
||||
result.emplace_back(tmp);
|
||||
}
|
||||
|
||||
//clean up after every loop
|
||||
op_container_ = std::deque<OutPt2>();
|
||||
results_.clear();
|
||||
for (OutPt2List edge : edges_) edge.clear();
|
||||
start_locs_.clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// RectClipLines
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
Paths64 RectClipLines::Execute(const Paths64& paths)
|
||||
{
|
||||
Paths64 result;
|
||||
if (rect_.IsEmpty()) return result;
|
||||
|
||||
for (const auto& path : paths)
|
||||
{
|
||||
if (path.size() < 2) continue;
|
||||
Rect64 pathrec = GetBounds(path);
|
||||
|
||||
if (!rect_.Intersects(pathrec)) continue;
|
||||
|
||||
ExecuteInternal(path);
|
||||
|
||||
for (OutPt2*& op : results_)
|
||||
{
|
||||
Path64 tmp = GetPath(op);
|
||||
if (!tmp.empty())
|
||||
result.emplace_back(tmp);
|
||||
}
|
||||
results_.clear();
|
||||
|
||||
op_container_ = std::deque<OutPt2>();
|
||||
start_locs_.clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void RectClipLines::ExecuteInternal(const Path64& path)
|
||||
{
|
||||
if (rect_.IsEmpty() || path.size() < 2) return;
|
||||
|
||||
results_.clear();
|
||||
op_container_ = std::deque<OutPt2>();
|
||||
start_locs_.clear();
|
||||
|
||||
int i = 1, highI = static_cast<int>(path.size()) - 1;
|
||||
|
||||
Location prev = Location::Inside, loc;
|
||||
Location crossing_loc;
|
||||
if (!GetLocation(rect_, path[0], loc))
|
||||
{
|
||||
while (i <= highI && !GetLocation(rect_, path[i], prev)) ++i;
|
||||
if (i > highI)
|
||||
{
|
||||
// all of path must be inside fRect
|
||||
for (const auto& pt : path) Add(pt);
|
||||
return;
|
||||
}
|
||||
if (prev == Location::Inside) loc = Location::Inside;
|
||||
i = 1;
|
||||
}
|
||||
if (loc == Location::Inside) Add(path[0]);
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
while (i <= highI)
|
||||
{
|
||||
prev = loc;
|
||||
GetNextLocation(path, loc, i, highI);
|
||||
if (i > highI) break;
|
||||
Point64 ip, ip2;
|
||||
Point64 prev_pt = path[static_cast<size_t>(i - 1)];
|
||||
|
||||
crossing_loc = loc;
|
||||
if (!GetIntersection(rect_as_path_,
|
||||
path[i], prev_pt, crossing_loc, ip))
|
||||
{
|
||||
// ie remaining outside
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// we must be crossing the rect boundary to get here
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
if (loc == Location::Inside) // path must be entering rect
|
||||
{
|
||||
Add(ip, true);
|
||||
}
|
||||
else if (prev != Location::Inside)
|
||||
{
|
||||
// passing right through rect. 'ip' here will be the second
|
||||
// intersect pt but we'll also need the first intersect pt (ip2)
|
||||
crossing_loc = prev;
|
||||
GetIntersection(rect_as_path_,
|
||||
prev_pt, path[i], crossing_loc, ip2);
|
||||
Add(ip2, true);
|
||||
Add(ip);
|
||||
}
|
||||
else // path must be exiting rect
|
||||
{
|
||||
Add(ip);
|
||||
}
|
||||
} //while i <= highI
|
||||
///////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
Path64 RectClipLines::GetPath(OutPt2*& op)
|
||||
{
|
||||
Path64 result;
|
||||
if (!op || op == op->next) return result;
|
||||
op = op->next; // starting at path beginning
|
||||
result.push_back(op->pt);
|
||||
OutPt2 *op2 = op->next;
|
||||
while (op2 != op)
|
||||
{
|
||||
result.push_back(op2->pt);
|
||||
op2 = op2->next;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
Loading…
Reference in New Issue