Skip to content

Commit

Permalink
clipper.core.h - fixed ambiguous 'round' (#824)
Browse files Browse the repository at this point in the history
                 also fixed a compiler warning (#823)
  • Loading branch information
AngusJohnson committed Apr 27, 2024
1 parent adc000a commit b4ee033
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 24 March 2024 *
* Date : 27 April 2024 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2024 *
* Purpose : Core Clipper Library structures and functions *
Expand Down Expand Up @@ -93,6 +93,13 @@ namespace Clipper2Lib
#endif
}

// can we call std::round on T? (default false) (#824)
template <typename T, typename = void>
struct is_round_invocable : std::false_type {};

template <typename T>
struct is_round_invocable<T, std::void_t<decltype(std::round(std::declval<T>()))>> : std::true_type {};


//By far the most widely used filling rules for polygons are EvenOdd
//and NonZero, sometimes called Alternate and Winding respectively.
Expand All @@ -111,8 +118,8 @@ namespace Clipper2Lib
template <typename T2>
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)
if constexpr (std::is_integral_v<T> &&
is_round_invocable<T2>::value && !std::is_integral_v<T2>)
{
x = static_cast<T>(std::round(x_));
y = static_cast<T>(std::round(y_));
Expand Down Expand Up @@ -165,8 +172,8 @@ namespace Clipper2Lib
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)
if constexpr (std::is_integral_v<T> &&
is_round_invocable<T2>::value && !std::is_integral_v<T2>)
{
x = static_cast<T>(std::round(x_));
y = static_cast<T>(std::round(y_));
Expand All @@ -184,7 +191,7 @@ namespace Clipper2Lib
Point(const T2 x_, const T2 y_) { Init(x_, y_); }

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

This comment has been minimized.

Copy link
@akien-mga

akien-mga Apr 27, 2024

I didn't test, but I suspect the two constructors in the USINGZ branch might trigger the same warning.


Point operator * (const double scale) const
{
Expand Down Expand Up @@ -365,8 +372,8 @@ namespace Clipper2Lib
{
Rect<T1> result;

if constexpr (std::numeric_limits<T1>::is_integer &&
!std::numeric_limits<T2>::is_integer)
if constexpr (std::is_integral_v<T1> &&
is_round_invocable<T2>::value && !std::is_integral_v<T2>)
{
result.left = static_cast<T1>(std::round(rect.left * scale));
result.top = static_cast<T1>(std::round(rect.top * scale));
Expand Down Expand Up @@ -518,7 +525,7 @@ namespace Clipper2Lib
{
Paths<T1> result;

if constexpr (std::numeric_limits<T1>::is_integer)
if constexpr (std::is_integral_v<T1>)
{
RectD r = GetBounds<double, T2>(paths);
if ((r.left * scale_x) < min_coord ||
Expand Down Expand Up @@ -763,7 +770,7 @@ namespace Clipper2Lib
T bb1maxx = CC_MAX(ln2a.x, ln2b.x);
T bb1maxy = CC_MAX(ln2a.y, ln2b.y);

if constexpr (std::numeric_limits<T>::is_integer)
if constexpr (std::is_integral_v<T>)
{
int64_t originx = (CC_MIN(bb0maxx, bb1maxx) + CC_MAX(bb0minx, bb1minx)) >> 1;
int64_t originy = (CC_MIN(bb0maxy, bb1maxy) + CC_MAX(bb0miny, bb1miny)) >> 1;
Expand Down Expand Up @@ -872,7 +879,7 @@ namespace Clipper2Lib
static_cast<double>(offPt.y - seg1.y) * dy) /
(Sqr(dx) + Sqr(dy));
if (q < 0) q = 0; else if (q > 1) q = 1;
if constexpr (std::numeric_limits<T>::is_integer)
if constexpr (std::is_integral_v<T>)
return Point<T>(
seg1.x + static_cast<T>(nearbyint(q * dx)),
seg1.y + static_cast<T>(nearbyint(q * dy)));
Expand Down

0 comments on commit b4ee033

Please sign in to comment.