Clipper2 1.2.2 (#1138)

This commit is contained in:
aismann 2023-03-27 15:46:14 +02:00 committed by GitHub
parent a847fdab9a
commit 65875a977e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 191 additions and 172 deletions

View File

@ -532,7 +532,7 @@ std::vector<Vec2> AutoPolygon::expand(const std::vector<Vec2>& points, const ax:
Clipper2Lib::ClipperOffset co;
co.AddPath(result, Clipper2Lib::JoinType::Miter, Clipper2Lib::EndType::Polygon);
solution = co.Execute(epsilon * PRECISION);
co.Execute(epsilon * PRECISION, solution);
// turn the result into simply polygon (AKA, fix overlap)

View File

@ -1,3 +1,9 @@
IMPORTANT INFO:
For correct working of the "third party robot" please update also the
https://github.com/axmolengine/axmol/blob/dev/thirdparty/README.md.in
# The axmol thirdparty libraries
## angle
- Upstream: https://github.com/google/angle
@ -37,7 +43,7 @@
## Clipper2
- [![Upstream](https://img.shields.io/github/v/tag/AngusJohnson/Clipper2?label=Upstream)](https://github.com/AngusJohnson/Clipper2)
- Version: 1.2.0
- Version: 1.2.2
- License: BSL-1.0
## ConvertUTF

View File

@ -37,7 +37,7 @@
## Clipper2
- [![Upstream](https://img.shields.io/github/v/tag/AngusJohnson/Clipper2?label=Upstream)](https://github.com/AngusJohnson/Clipper2)
- Version: 1.2.0
- Version: 1.2.2
- License: BSL-1.0
## ConvertUTF

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 21 February 2023 *
* Date : 22 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : Core Clipper Library structures and functions *
@ -10,6 +10,7 @@
#ifndef CLIPPER_CORE_H
#define CLIPPER_CORE_H
#include <cstdint>
#include <cstdlib>
#include <cmath>
#include <vector>
@ -22,13 +23,13 @@
namespace Clipper2Lib
{
#if __cpp_exceptions
#if (defined(__cpp_exceptions) && __cpp_exceptions) || (defined(__EXCEPTIONS) && __EXCEPTIONS)
class Clipper2Exception : public std::exception {
public:
explicit Clipper2Exception(const char* description) :
m_descr(description) {}
virtual const char* what() const throw() { return m_descr.c_str(); }
virtual const char* what() const throw() override { return m_descr.c_str(); }
private:
std::string m_descr;
};
@ -57,11 +58,10 @@ namespace Clipper2Lib
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
#if (defined(__cpp_exceptions) && __cpp_exceptions) || (defined(__EXCEPTIONS) && __EXCEPTIONS)
switch (error_code)
{
case precision_error_i:
@ -338,69 +338,44 @@ namespace Clipper2Lib
static const Rect64 MaxInvalidRect64 = Rect64(
INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN);
static const RectD MaxInvalidRectD = RectD(
MAX_DBL, MAX_DBL, MIN_DBL, MIN_DBL);
MAX_DBL, MAX_DBL, -MAX_DBL, -MAX_DBL);
inline Rect64 GetBounds(const Path64& path)
template <typename T>
Rect<T> GetBounds(const Path<T>& path)
{
Rect64 rec = MaxInvalidRect64;
for (const Point64& pt : path)
auto xmin = std::numeric_limits<T>::max();
auto ymin = std::numeric_limits<T>::max();
auto xmax = std::numeric_limits<T>::lowest();
auto ymax = std::numeric_limits<T>::lowest();
for (const auto& p : path)
{
if (pt.x < rec.left) rec.left = pt.x;
if (pt.x > rec.right) rec.right = pt.x;
if (pt.y < rec.top) rec.top = pt.y;
if (pt.y > rec.bottom) rec.bottom = pt.y;
if (p.x < xmin) xmin = p.x;
if (p.x > xmax) xmax = p.x;
if (p.y < ymin) ymin = p.y;
if (p.y > ymax) ymax = p.y;
}
if (rec.left == INT64_MAX) return Rect64();
return rec;
return Rect<T>(xmin, ymin, xmax, ymax);
}
inline Rect64 GetBounds(const Paths64& paths)
template <typename T>
Rect<T> GetBounds(const Paths<T>& paths)
{
Rect64 rec = MaxInvalidRect64;
for (const Path64& path : paths)
for (const Point64& pt : path)
auto xmin = std::numeric_limits<T>::max();
auto ymin = std::numeric_limits<T>::max();
auto xmax = std::numeric_limits<T>::lowest();
auto ymax = std::numeric_limits<T>::lowest();
for (const Path<T>& path : paths)
for (const Point<T>& p : path)
{
if (pt.x < rec.left) rec.left = pt.x;
if (pt.x > rec.right) rec.right = pt.x;
if (pt.y < rec.top) rec.top = pt.y;
if (pt.y > rec.bottom) rec.bottom = pt.y;
if (p.x < xmin) xmin = p.x;
if (p.x > xmax) xmax = p.x;
if (p.y < ymin) ymin = p.y;
if (p.y > ymax) ymax = p.y;
}
if (rec.left == INT64_MAX) return Rect64();
return rec;
return Rect<T>(xmin, ymin, xmax, ymax);
}
inline RectD GetBounds(const PathD& path)
{
RectD rec = MaxInvalidRectD;
for (const PointD& pt : path)
{
if (pt.x < rec.left) rec.left = pt.x;
if (pt.x > rec.right) rec.right = pt.x;
if (pt.y < rec.top) rec.top = pt.y;
if (pt.y > rec.bottom) rec.bottom = pt.y;
}
if (rec.left == MAX_DBL) return RectD();
return rec;
}
inline RectD GetBounds(const PathsD& paths)
{
RectD rec = MaxInvalidRectD;
for (const PathD& path : paths)
for (const PointD& pt : path)
{
if (pt.x < rec.left) rec.left = pt.x;
if (pt.x > rec.right) rec.right = pt.x;
if (pt.y < rec.top) rec.top = pt.y;
if (pt.y > rec.bottom) rec.bottom = pt.y;
}
if (rec.left == MAX_DBL) return RectD();
return rec;
}
template <typename T>
std::ostream& operator << (std::ostream& outstream, const Path<T>& path)
{

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 26 February 2023 *
* Date : 26 March 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.2.0";
constexpr auto CLIPPER2_VERSION = "1.2.2";
#include <cstdlib>
#include <iostream>

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 12 February 2023 *
* Date : 23 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This module exports the Clipper2 Library (ie DLL/so) *
@ -157,14 +157,14 @@ EXTERN_DLL_EXPORT CPathsD InflatePathsD(const CPathsD paths,
int precision = 2, double miter_limit = 2.0,
double arc_tolerance = 0.0, bool reverse_solution = false);
// RectClip & RectClipLines:
EXTERN_DLL_EXPORT CPaths64 RectClip64(const CRect64& rect,
// ExecuteRectClip & ExecuteRectClipLines:
EXTERN_DLL_EXPORT CPaths64 ExecuteRectClip64(const CRect64& rect,
const CPaths64 paths, bool convex_only = false);
EXTERN_DLL_EXPORT CPathsD RectClipD(const CRectD& rect,
EXTERN_DLL_EXPORT CPathsD ExecuteRectClipD(const CRectD& rect,
const CPathsD paths, int precision = 2, bool convex_only = false);
EXTERN_DLL_EXPORT CPaths64 RectClipLines64(const CRect64& rect,
EXTERN_DLL_EXPORT CPaths64 ExecuteRectClipLines64(const CRect64& rect,
const CPaths64 paths);
EXTERN_DLL_EXPORT CPathsD RectClipLinesD(const CRectD& rect,
EXTERN_DLL_EXPORT CPathsD ExecuteRectClipLinesD(const CRectD& rect,
const CPathsD paths, int precision = 2);
//////////////////////////////////////////////////////
@ -361,7 +361,8 @@ EXTERN_DLL_EXPORT CPaths64 InflatePaths64(const CPaths64 paths,
ClipperOffset clip_offset( miter_limit,
arc_tolerance, reverse_solution);
clip_offset.AddPaths(pp, JoinType(jointype), EndType(endtype));
Paths64 result = clip_offset.Execute(delta);
Paths64 result;
clip_offset.Execute(delta, result);
return CreateCPaths64(result);
}
@ -375,11 +376,12 @@ EXTERN_DLL_EXPORT CPathsD InflatePathsD(const CPathsD paths,
ClipperOffset clip_offset(miter_limit, arc_tolerance, reverse_solution);
Paths64 pp = ConvertCPathsD(paths, scale);
clip_offset.AddPaths(pp, JoinType(jointype), EndType(endtype));
Paths64 result = clip_offset.Execute(delta * scale);
Paths64 result;
clip_offset.Execute(delta * scale, result);
return CreateCPathsD(result, 1/scale);
}
EXTERN_DLL_EXPORT CPaths64 RectClip64(const CRect64& rect,
EXTERN_DLL_EXPORT CPaths64 ExecuteRectClip64(const CRect64& rect,
const CPaths64 paths, bool convex_only)
{
if (CRectIsEmpty(rect) || !paths) return nullptr;
@ -390,7 +392,7 @@ EXTERN_DLL_EXPORT CPaths64 RectClip64(const CRect64& rect,
return CreateCPaths64(result);
}
EXTERN_DLL_EXPORT CPathsD RectClipD(const CRectD& rect,
EXTERN_DLL_EXPORT CPathsD ExecuteRectClipD(const CRectD& rect,
const CPathsD paths, int precision, bool convex_only)
{
if (CRectIsEmpty(rect) || !paths) return nullptr;
@ -405,7 +407,7 @@ EXTERN_DLL_EXPORT CPathsD RectClipD(const CRectD& rect,
return CreateCPathsD(result, 1/scale);
}
EXTERN_DLL_EXPORT CPaths64 RectClipLines64(const CRect64& rect,
EXTERN_DLL_EXPORT CPaths64 ExecuteRectClipLines64(const CRect64& rect,
const CPaths64 paths)
{
if (CRectIsEmpty(rect) || !paths) return nullptr;
@ -416,7 +418,7 @@ EXTERN_DLL_EXPORT CPaths64 RectClipLines64(const CRect64& rect,
return CreateCPaths64(result);
}
EXTERN_DLL_EXPORT CPathsD RectClipLinesD(const CRectD& rect,
EXTERN_DLL_EXPORT CPathsD ExecuteRectClipLinesD(const CRectD& rect,
const CPathsD paths, int precision)
{
if (CRectIsEmpty(rect) || !paths) return nullptr;

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 9 February 2023 *
* Date : 23 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This module provides a simple interface to the Clipper Library *
@ -139,7 +139,9 @@ namespace Clipper2Lib {
if (!delta) return paths;
ClipperOffset clip_offset(miter_limit, arc_tolerance);
clip_offset.AddPaths(paths, jt, et);
return clip_offset.Execute(delta);
Paths64 solution;
clip_offset.Execute(delta, solution);
return solution;
}
inline PathsD InflatePaths(const PathsD& paths, double delta,
@ -154,8 +156,9 @@ namespace Clipper2Lib {
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, error_code);
Paths64 solution;
clip_offset.Execute(delta * scale, solution);
return ScalePaths<double, int64_t>(solution, 1 / scale, error_code);
}
inline Path64 TranslatePath(const Path64& path, int64_t dx, int64_t dy)
@ -194,23 +197,23 @@ namespace Clipper2Lib {
return result;
}
inline Paths64 RectClip(const Rect64& rect,
inline Paths64 ExecuteRectClip(const Rect64& rect,
const Paths64& paths, bool convex_only = false)
{
if (rect.IsEmpty() || paths.empty()) return Paths64();
class RectClip rc(rect);
RectClip rc(rect);
return rc.Execute(paths, convex_only);
}
inline Paths64 RectClip(const Rect64& rect,
inline Paths64 ExecuteRectClip(const Rect64& rect,
const Path64& path, bool convex_only = false)
{
if (rect.IsEmpty() || path.empty()) return Paths64();
class RectClip rc(rect);
RectClip rc(rect);
return rc.Execute(Paths64{ path }, convex_only);
}
inline PathsD RectClip(const RectD& rect,
inline PathsD ExecuteRectClip(const RectD& rect,
const PathsD& paths, bool convex_only = false, int precision = 2)
{
if (rect.IsEmpty() || paths.empty()) return PathsD();
@ -219,37 +222,37 @@ namespace Clipper2Lib {
if (error_code) return PathsD();
const double scale = std::pow(10, precision);
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
class RectClip rc(r);
RectClip rc(r);
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 PathsD RectClip(const RectD& rect,
inline PathsD ExecuteRectClip(const RectD& rect,
const PathD& path, bool convex_only = false, int precision = 2)
{
return RectClip(rect, PathsD{ path }, convex_only, precision);
return ExecuteRectClip(rect, PathsD{ path }, convex_only, precision);
}
inline Paths64 RectClipLines(const Rect64& rect, const Paths64& lines)
inline Paths64 ExecuteRectClipLines(const Rect64& rect, const Paths64& lines)
{
if (rect.IsEmpty() || lines.empty()) return Paths64();
class RectClipLines rcl(rect);
RectClipLines rcl(rect);
return rcl.Execute(lines);
}
inline Paths64 RectClipLines(const Rect64& rect, const Path64& line)
inline Paths64 ExecuteRectClipLines(const Rect64& rect, const Path64& line)
{
return RectClipLines(rect, Paths64{ line });
return ExecuteRectClipLines(rect, Paths64{ line });
}
inline PathsD RectClipLines(const RectD& rect, const PathD& line, int precision = 2)
inline PathsD ExecuteRectClipLines(const RectD& rect, const PathD& line, int precision = 2)
{
return RectClip(rect, PathsD{ line }, precision);
return ExecuteRectClip(rect, PathsD{ line }, precision);
}
inline PathsD RectClipLines(const RectD& rect, const PathsD& lines, int precision = 2)
inline PathsD ExecuteRectClipLines(const RectD& rect, const PathsD& lines, int precision = 2)
{
if (rect.IsEmpty() || lines.empty()) return PathsD();
int error_code = 0;
@ -257,7 +260,7 @@ namespace Clipper2Lib {
if (error_code) return PathsD();
const double scale = std::pow(10, precision);
Rect64 r = ScaleRect<int64_t, double>(rect, scale);
class RectClipLines rcl(r);
RectClipLines rcl(r);
Paths64 p = ScalePaths<int64_t, double>(lines, scale, error_code);
if (error_code) return PathsD();
p = rcl.Execute(p);

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 15 February 2023 *
* Date : 22 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : Path Offset (Inflate/Shrink) *
@ -24,6 +24,7 @@ enum class EndType {Polygon, Joined, Butt, Square, Round};
//Joined : offsets both sides of a path, with joined ends
//Polygon: offsets only one side of a closed path
class ClipperOffset {
private:
@ -35,8 +36,8 @@ private:
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) {}
Group(const Paths64& _paths, JoinType _join_type, EndType _end_type) :
paths_in(_paths), join_type(_join_type), end_type(_end_type) {}
};
int error_code_ = 0;
@ -45,6 +46,8 @@ private:
double abs_group_delta_ = 0.0;
double temp_lim_ = 0.0;
double steps_per_rad_ = 0.0;
double step_sin_ = 0.0;
double step_cos_ = 0.0;
PathD norms;
Paths64 solution;
std::vector<Group> groups_;
@ -56,7 +59,7 @@ private:
bool preserve_collinear_ = false;
bool reverse_solution_ = false;
#if USINGZ
#ifdef USINGZ
ZCallback64 zCallback64_ = nullptr;
#endif
@ -69,6 +72,7 @@ private:
void OffsetOpenPath(Group& group, Path64& path);
void OffsetPoint(Group& group, Path64& path, size_t j, size_t& k);
void DoGroupOffset(Group &group);
void ExecuteInternal(double delta);
public:
explicit ClipperOffset(double miter_limit = 2.0,
double arc_tolerance = 0.0,
@ -83,11 +87,10 @@ public:
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_);
void AddPaths(const PathsD &p, JoinType jt_, EndType et_);
void Clear() { groups_.clear(); norms.clear(); };
Paths64 Execute(double delta);
void Execute(double delta, Paths64& paths);
void Execute(double delta, PolyTree64& polytree);
double MiterLimit() const { return miter_limit_; }
void MiterLimit(double miter_limit) { miter_limit_ = miter_limit; }
@ -102,7 +105,7 @@ public:
bool ReverseSolution() const { return reverse_solution_; }
void ReverseSolution(bool reverse_solution) {reverse_solution_ = reverse_solution;}
#if USINGZ
#ifdef USINGZ
void SetZCallback(ZCallback64 cb) { zCallback64_ = cb; }
#endif
};

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 21 February 2023 *
* Date : 19 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
@ -594,6 +594,9 @@ namespace Clipper2Lib {
//for each path create a circular double linked list of vertices
Vertex* v0 = v, * curr_v = v, * prev_v = nullptr;
if (path.empty())
continue;
v->prev = nullptr;
int cnt = 0;
for (const Point64& pt : path)
@ -2821,10 +2824,9 @@ namespace Clipper2Lib {
// nb: outrec_list_.size() may change in the following
// while loop because polygons may be split during
// calls to CleanCollinear which calls FixSelfIntersects
size_t i = 0;
while (i < outrec_list_.size())
for (size_t i = 0; i < outrec_list_.size(); ++i)
{
OutRec* outrec = outrec_list_[i++];
OutRec* outrec = outrec_list_[i];
if (outrec->pts == nullptr) continue;
Path64 path;
@ -2850,14 +2852,13 @@ namespace Clipper2Lib {
open_paths.resize(0);
if (has_open_paths_)
open_paths.reserve(outrec_list_.size());
size_t i = 0;
// outrec_list_.size() is not static here because
// CheckBounds below can indirectly add additional
// OutRec (via FixOutRecPts & CleanCollinear)
while (i < outrec_list_.size())
for (size_t i = 0; i < outrec_list_.size(); ++i)
{
OutRec* outrec = outrec_list_[i++];
OutRec* outrec = outrec_list_[i];
if (!outrec || !outrec->pts) continue;
if (outrec->is_open)
{
@ -2928,8 +2929,12 @@ namespace Clipper2Lib {
solutionOpen->reserve(outrec_list_.size());
}
for (OutRec* outrec : outrec_list_)
// outrec_list_.size() is not static here because
// CleanCollinear below can indirectly add additional
// OutRec (via FixOutRecPts)
for (std::size_t i = 0; i < outrec_list_.size(); ++i)
{
OutRec* outrec = outrec_list_[i];
if (outrec->pts == nullptr) continue;
PathD path;

View File

@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 23 February 2023 *
* Date : 22 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : Path Offset (Inflate/Shrink) *
@ -98,7 +98,7 @@ inline bool IsClosedPath(EndType et)
inline Point64 GetPerpendic(const Point64& pt, const PointD& norm, double delta)
{
#if USINGZ
#ifdef 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);
@ -107,7 +107,7 @@ inline Point64 GetPerpendic(const Point64& pt, const PointD& norm, double delta)
inline PointD GetPerpendicD(const Point64& pt, const PointD& norm, double delta)
{
#if USINGZ
#ifdef 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);
@ -120,7 +120,7 @@ inline void NegatePath(PathD& path)
{
pt.x = -pt.x;
pt.y = -pt.y;
#if USINGZ
#ifdef USINGZ
pt.z = pt.z;
#endif
}
@ -143,19 +143,6 @@ void ClipperOffset::AddPaths(const Paths64 &paths, JoinType jt_, EndType et_)
groups_.push_back(Group(paths, jt_, et_));
}
void ClipperOffset::AddPath(const Clipper2Lib::PathD& path, JoinType jt_, EndType et_)
{
PathsD paths;
paths.push_back(path);
AddPaths(paths, jt_, et_);
}
void ClipperOffset::AddPaths(const PathsD& paths, JoinType jt_, EndType et_)
{
if (paths.size() == 0) return;
groups_.push_back(Group(PathsDToPaths64(paths), jt_, et_));
}
void ClipperOffset::BuildNormals(const Path64& path)
{
norms.clear();
@ -169,7 +156,7 @@ void ClipperOffset::BuildNormals(const Path64& path)
inline PointD TranslatePoint(const PointD& pt, double dx, double dy)
{
#if USINGZ
#ifdef USINGZ
return PointD(pt.x + dx, pt.y + dy, pt.z);
#else
return PointD(pt.x + dx, pt.y + dy);
@ -178,7 +165,7 @@ inline PointD TranslatePoint(const PointD& pt, double dx, double dy)
inline PointD ReflectPoint(const PointD& pt, const PointD& pivot)
{
#if USINGZ
#ifdef 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));
@ -236,7 +223,7 @@ 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
#ifdef USINGZ
pt.z = ptQ.z;
#endif
//get the second intersect point through reflecion
@ -247,7 +234,7 @@ void ClipperOffset::DoSquare(Group& group, const Path64& path, size_t j, size_t
{
PointD pt4 = GetPerpendicD(path[j], norms[k], group_delta_);
PointD pt = IntersectPoint(pt1, pt2, pt3, pt4);
#if USINGZ
#ifdef USINGZ
pt.z = ptQ.z;
#endif
group.path.push_back(Point64(pt));
@ -259,7 +246,7 @@ void ClipperOffset::DoSquare(Group& group, const Path64& path, size_t j, size_t
void ClipperOffset::DoMiter(Group& group, const Path64& path, size_t j, size_t k, double cos_a)
{
double q = group_delta_ / (cos_a + 1);
#if USINGZ
#ifdef 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,
@ -275,27 +262,26 @@ void ClipperOffset::DoRound(Group& group, const Path64& path, size_t j, size_t k
{
Point64 pt = path[j];
PointD offsetVec = PointD(norms[k].x * group_delta_, norms[k].y * group_delta_);
if (j == k) offsetVec.Negate();
#if USINGZ
#ifdef 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
{
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);
int steps = static_cast<int>(std::ceil(steps_per_rad_ * std::abs(angle))); // #448, #456
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
offsetVec = PointD(offsetVec.x * step_cos_ - step_sin_ * offsetVec.y,
offsetVec.x * step_sin_ + offsetVec.y * step_cos_);
#ifdef 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_));
@ -316,12 +302,13 @@ void ClipperOffset::OffsetPoint(Group& group, Path64& path, size_t j, size_t& k)
if (sin_a > 1.0) sin_a = 1.0;
else if (sin_a < -1.0) sin_a = -1.0;
if (AlmostZero(cos_a - 1, 0.01)) // almost straight
if (cos_a > 0.99) // almost straight - less than 8 degrees
{
group.path.push_back(GetPerpendic(path[j], norms[k], group_delta_));
group.path.push_back(GetPerpendic(path[j], norms[j], group_delta_)); // (#418)
if (cos_a < 0.9998) // greater than 1 degree (#424)
group.path.push_back(GetPerpendic(path[j], norms[j], group_delta_)); // (#418)
}
else if (!AlmostZero(cos_a + 1, 0.01) && (sin_a * group_delta_ < 0))
else if (cos_a > -0.99 && (sin_a * group_delta_ < 0))
{
// is concave
group.path.push_back(GetPerpendic(path[j], norms[k], group_delta_));
@ -376,7 +363,7 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path)
switch (end_type_)
{
case EndType::Butt:
#if USINGZ
#ifdef USINGZ
group.path.push_back(Point64(
path[0].x - norms[0].x * group_delta_,
path[0].y - norms[0].y * group_delta_,
@ -411,7 +398,7 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path)
switch (end_type_)
{
case EndType::Butt:
#if USINGZ
#ifdef USINGZ
group.path.push_back(Point64(
path[highI].x - norms[highI].x * group_delta_,
path[highI].y - norms[highI].y * group_delta_,
@ -438,8 +425,6 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path)
void ClipperOffset::DoGroupOffset(Group& group)
{
Rect64 r;
int idx = -1;
//the lowermost polygon must be an outer polygon. So we can use that as the
@ -450,7 +435,7 @@ void ClipperOffset::DoGroupOffset(Group& group)
if (group.end_type == EndType::Polygon)
{
double area = Area(group.paths_in[idx]);
if (area == 0) return;
//if (area == 0) return; // probably unhelpful (#430)
group.is_reversed = (area < 0);
if (group.is_reversed) group_delta_ = -delta_;
else group_delta_ = delta_;
@ -483,7 +468,14 @@ void ClipperOffset::DoGroupOffset(Group& group)
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_);
double steps_per_360 = PI / std::acos(1 - arcTol / abs_group_delta_);
if (steps_per_360 > abs_group_delta_ * PI)
steps_per_360 = abs_group_delta_ * PI; //ie avoids excessive precision
step_sin_ = std::sin(2 * PI / steps_per_360);
step_cos_ = std::cos(2 * PI / steps_per_360);
if (group_delta_ < 0.0) step_sin_ = -step_sin_;
steps_per_rad_ = steps_per_360 / (2 *PI);
}
bool is_joined =
@ -506,7 +498,7 @@ void ClipperOffset::DoGroupOffset(Group& group)
{
double radius = abs_group_delta_;
group.path = Ellipse(path[0], radius, radius);
#if USINGZ
#ifdef USINGZ
for (auto& p : group.path) p.z = path[0].z;
#endif
}
@ -515,7 +507,7 @@ void ClipperOffset::DoGroupOffset(Group& group)
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();
#if USINGZ
#ifdef USINGZ
for (auto& p : group.path) p.z = path[0].z;
#endif
}
@ -542,11 +534,11 @@ void ClipperOffset::DoGroupOffset(Group& group)
group.paths_out.clear();
}
Paths64 ClipperOffset::Execute(double delta)
void ClipperOffset::ExecuteInternal(double delta)
{
error_code_ = 0;
solution.clear();
if (groups_.size() == 0) return solution;
if (groups_.size() == 0) return;
if (std::abs(delta) < 0.5)
{
@ -555,39 +547,72 @@ Paths64 ClipperOffset::Execute(double delta)
solution.reserve(solution.size() + group.paths_in.size());
copy(group.paths_in.begin(), group.paths_in.end(), back_inserter(solution));
}
return solution;
}
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)
}
else
{
DoGroupOffset(*git);
if (!error_code_) continue; // all OK
solution.clear();
return solution;
}
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);
if (!error_code_) continue; // all OK
solution.clear();
}
}
}
void ClipperOffset::Execute(double delta, Paths64& paths)
{
paths.clear();
ExecuteInternal(delta);
if (!solution.size()) return;
paths = 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;
#if USINGZ
#ifdef USINGZ
if (zCallback64_) {
c.SetZCallback(zCallback64_);
}
#endif
c.AddSubject(solution);
if (groups_[0].is_reversed)
c.Execute(ClipType::Union, FillRule::Negative, solution);
c.Execute(ClipType::Union, FillRule::Negative, paths);
else
c.Execute(ClipType::Union, FillRule::Positive, solution);
return solution;
c.Execute(ClipType::Union, FillRule::Positive, paths);
}
void ClipperOffset::Execute(double delta, PolyTree64& polytree)
{
polytree.Clear();
ExecuteInternal(delta);
if (!solution.size()) return;
//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;
#ifdef USINGZ
if (zCallback64_) {
c.SetZCallback(zCallback64_);
}
#endif
c.AddSubject(solution);
if (groups_[0].is_reversed)
c.Execute(ClipType::Union, FillRule::Negative, polytree);
else
c.Execute(ClipType::Union, FillRule::Positive, polytree);
}
} // namespace