Skip to content

Commit

Permalink
Fixed bug in C++ Point<T> constructor when USINGZ defined (#246)
Browse files Browse the repository at this point in the history
Changed Library's C# target framework back to netstandard2.0 (#225)
Updated several sample apps.
File headers updated to version 1.0.5
  • Loading branch information
AngusJohnson committed Oct 2, 2022
1 parent acfeee6 commit 674c822
Show file tree
Hide file tree
Showing 27 changed files with 284 additions and 213 deletions.
11 changes: 0 additions & 11 deletions BinaryUtils/Win64/readme.md

This file was deleted.

36 changes: 25 additions & 11 deletions CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 7 August 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : Core Clipper Library structures and functions *
Expand Down Expand Up @@ -36,26 +36,25 @@ struct Point {
T y;
#ifdef USINGZ
int64_t z;
#endif

template <typename T2>
inline void Init(const T2 x_ = 0, const T2 y_ = 0)
inline void Init(const T2 x_ = 0, const T2 y_ = 0, const int64_t z_ = 0)
{
if constexpr (std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T2>::is_integer)
{
x = static_cast<T>(std::round(x_));
y = static_cast<T>(std::round(y_));
z = z_;
}
else
{
x = static_cast<T>(x_);
y = static_cast<T>(y_);
}
z = z_;
}
}

#ifdef USINGZ

explicit Point() : x(0), y(0), z(0) {};

template <typename T2>
Expand All @@ -66,9 +65,9 @@ struct Point {
}

template <typename T2>
explicit Point<T>(const Point<T2>& p)
{
Init(p.x, p.y);
explicit Point<T>(const Point<T2>& p)
{
Init(p.x, p.y, p.z);
z = 0;
}

Expand All @@ -86,6 +85,22 @@ struct Point {

#else

template <typename T2>
inline void Init(const T2 x_ = 0, const T2 y_ = 0)
{
if constexpr (std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T2>::is_integer)
{
x = static_cast<T>(std::round(x_));
y = static_cast<T>(std::round(y_));
}
else
{
x = static_cast<T>(x_);
y = static_cast<T>(y_);
}
}

explicit Point() : x(0), y(0) {};

template <typename T2>
Expand All @@ -104,7 +119,6 @@ struct Point {
os << point.x << "," << point.y << " ";
return os;
}

#endif

friend bool operator==(const Point &a, const Point &b)
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/include/clipper2/clipper.engine.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 4 September 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : This is the main polygon clipping module *
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/include/clipper2/clipper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 4 August 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : This module provides a simple interface to the Clipper Library *
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/include/clipper2/clipper.minkowski.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.0 *
* Date : 3 August 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : Minkowski Sum and Difference *
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/include/clipper2/clipper.offset.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 14 August 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : Path Offset (Inflate/Shrink) *
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/src/clipper.engine.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 22 September 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : This is the main polygon clipping module *
Expand Down
36 changes: 17 additions & 19 deletions CPP/Clipper2Lib/src/clipper.offset.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Author : Angus Johnson *
* Version : Clipper2 - ver.1.0.4 *
* Date : 14 August 2022 *
* Version : Clipper2 - ver.1.0.5 *
* Date : 2 October 2022 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* Purpose : Path Offset (Inflate/Shrink) *
Expand Down Expand Up @@ -79,6 +79,16 @@ inline bool IsClosedPath(EndType et)
return et == EndType::Polygon || et == EndType::Joined;
}

inline Point64 GetPerpendic(const Point64& pt, const PointD& norm, double delta)
{
return Point64(pt.x + norm.x * delta, pt.y + norm.y * delta);
}

inline PointD GetPerpendicD(const Point64& pt, const PointD& norm, double delta)
{
return PointD(pt.x + norm.x * delta, pt.y + norm.y * delta);
}

//------------------------------------------------------------------------------
// ClipperOffset methods
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -159,16 +169,6 @@ PointD IntersectPoint(const PointD& pt1a, const PointD& pt1b,
}
}

inline Point64 GetPerpendic(const Point64& pt, const PointD& norm, double delta)
{
return Point64(pt.x + norm.x * delta, pt.y + norm.y * delta);
}

inline PointD GetPerpendicD(const Point64& pt, const PointD& norm, double delta)
{
return PointD(pt.x + norm.x * delta, pt.y + norm.y * delta);
}

void ClipperOffset::DoSquare(Group& group, const Path64& path, size_t j, size_t k)
{
PointD vec;
Expand Down Expand Up @@ -242,6 +242,8 @@ void ClipperOffset::OffsetPoint(Group& group, Path64& path, size_t j, size_t& k)
// sin(A) < 0: right turning
// cos(A) < 0: change in angle is more than 90 degree

if (path[j] == path[k]) { k = j; return; }

double sin_a = CrossProduct(norms[j], norms[k]);
double cos_a = DotProduct(norms[j], norms[k]);
if (sin_a > 1.0) sin_a = 1.0;
Expand Down Expand Up @@ -309,9 +311,7 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)
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(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_));
break;
case EndType::Round:
DoRound(group, path, 0,0, PI);
Expand Down Expand Up @@ -339,9 +339,7 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)
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(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_));
break;
case EndType::Round:
DoRound(group, path, highI, highI, PI);
Expand All @@ -358,7 +356,7 @@ void ClipperOffset::OffsetOpenPath(Group& group, Path64& path, EndType end_type)

void ClipperOffset::DoGroupOffset(Group& group, double delta)
{
if (group.end_type_ != EndType::Polygon) delta = std::abs(delta) / 2;
if (group.end_type_ != EndType::Polygon) delta = std::abs(delta) * 0.5;
bool isClosedPaths = IsClosedPath(group.end_type_);

if (isClosedPaths)
Expand Down
2 changes: 1 addition & 1 deletion CPP/Examples/ConsoleDemo1/ConsoleDemo1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main()

//////////////////////////////////////////////////////////////////////////
//test_type options: Simple; Benchmark; All; MemoryLeak;
TestType test_type = TestType::All;
TestType test_type = TestType::Simple;
//////////////////////////////////////////////////////////////////////////

switch (test_type)
Expand Down
24 changes: 24 additions & 0 deletions CPP/Examples/InflateDemo/InflateDemo1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ void System(const std::string& filename)

void DoSimpleShapes()
{

//open path offsets
Paths64 op1, op2;

FillRule fr2 = FillRule::EvenOdd;
SvgWriter svg2;
op1.push_back(MakePath("100,100, 20,20 180,20 180,180, 20,180"));
op2 = InflatePaths(op1, 20, JoinType::Square, EndType::Square);
SvgAddOpenSubject(svg2, op1, fr2, false);
SvgAddSolution(svg2, Paths64ToPathsD(op2), fr2, false);

op1 = TranslatePaths(op1, 250, 0);
op2 = InflatePaths(op1, 20, JoinType::Miter, EndType::Butt, 5);
SvgAddOpenSubject(svg2, op1, fr2, false);
SvgAddSolution(svg2, Paths64ToPathsD(op2), fr2, false);

op1 = TranslatePaths(op1, 250, 0);
op2 = InflatePaths(op1, 20, JoinType::Round, EndType::Round);
SvgAddOpenSubject(svg2, op1, fr2, false);
SvgAddSolution(svg2, Paths64ToPathsD(op2), fr2, false);

SvgSaveToFile(svg2, "open_paths.svg", 800, 600, 20);
System("open_paths.svg");

//triangle offset - with large miter
Paths64 p, pp;
p.push_back(MakePath("30, 150, 60, 350, 0, 350"));
Expand Down
64 changes: 58 additions & 6 deletions CPP/Examples/UsingZ/UsingZ1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,37 @@ using namespace Clipper2Lib;


void System(const std::string &filename);
void TestInt64();
void TestDouble();

class MyClass {
public:
// Point64 callback - see TestInt64()
void myZCB(const Point64& e1bot, const Point64& e1top,
const Point64& e2bot, const Point64& e2top, Point64& pt)
{
pt.z = 1;
}

// PointD callback - see TestDouble()
void myZCBD(const PointD& e1bot, const PointD& e1top,
const PointD& e2bot, const PointD& e2top, PointD& pt)
{
pt.z = 1;
}
};

int main(int argc, char* argv[])
{
Paths64 subject, solution;
TestInt64();
TestDouble();
}
//---------------------------------------------------------------------------

void TestInt64()
{

Paths64 subject, solution;
MyClass mc;
Clipper64 c64;

Expand All @@ -35,22 +52,57 @@ int main(int argc, char* argv[])

c64.Execute(ClipType::Union, FillRule::NonZero, solution);

/*
SvgWriter svg;
SvgAddSolution(svg, solution, FillRule::NonZero, false);
if (solution.size() > 0) {
// draw circles around intersection points
// draw circles around intersection points - flagged by z == 1
PathsD ellipses;
double r = 3.0;
for (const Point64& pt : solution[0])
if (pt.z == 1)
{
ellipses.push_back(Ellipse(RectD(pt.x - 3., pt.y - 3., pt.x + 3., pt.y + 3.), 11));
ellipses.push_back(Ellipse(RectD(pt.x - r, pt.y - r, pt.x + r, pt.y + r), 11));
}
SvgAddClip(svg, ellipses, FillRule::NonZero);
}
SvgSaveToFile(svg, "solution_off.svg", 800, 600, 20);
System("solution_off.svg");
SvgSaveToFile(svg, "using_z_64.svg", 800, 600, 20);
System("using_z_64.svg");
*/
}

void TestDouble()
{

PathsD subject, solution;
MyClass mc;
ClipperD c;

subject.push_back(MakePathD("100, 50, 10, 79, 65, 2, 65, 98, 10, 21 "));
c.AddSubject(subject);
c.SetZCallback(
std::bind(&MyClass::myZCBD, mc, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5));

c.Execute(ClipType::Union, FillRule::NonZero, solution);

SvgWriter svg;
SvgAddSolution(svg, solution, FillRule::NonZero, false);
if (solution.size() > 0) {
// draw circles around intersection points - flagged by z == 1
PathsD ellipses;
double r = 5.0;
for (const PointD& pt : solution[0])
if (pt.z == 1)
{
ellipses.push_back(Ellipse(RectD(pt.x - r, pt.y - r, pt.x + r, pt.y + r), 11));
}
SvgAddClip(svg, ellipses, FillRule::NonZero);
}
SvgSaveToFile(svg, "using_z_d.svg", 800, 600, 20);
System("using_z_d.svg");
}
//---------------------------------------------------------------------------

void System(const std::string &filename)
{
Expand Down
11 changes: 10 additions & 1 deletion CPP/MS Visual Studio.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### MS Visual Studio

In the **\cmake\win64** folder there are a couple of batch files that will set up Visual Studio solution files (for those that want them). These batch files will set the `CMAKE_INSTALL_PREFIX` to a directory in the source tree rather than using the default (C:\\Program Files\\) location.
# To install the optional CI testing:
1. Download "googletest" from https://github.com/google/googletest
2. Copy the follow file and folders into the empty _CPP/Tests/googletest_ folder ...
a. CMakeLists.txt
b. googlemock subfolder
c. googletest" subfolder

In Visual Studio, open Clipper2's CPP folder and wait for ...
_CMake generation finished._
Rebuild all files.
Loading

0 comments on commit 674c822

Please sign in to comment.