From 7033337149ea250b146cc0b464c23d181e357a76 Mon Sep 17 00:00:00 2001 From: Adam Wulkiewicz Date: Mon, 24 Feb 2014 15:16:37 +0100 Subject: [PATCH] IP coordinates rounded to the nearest value instead of a cast in segments_intersection_points if coordinate_type is integer --- .../policies/relate/intersection_points.hpp | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/include/boost/geometry/policies/relate/intersection_points.hpp b/include/boost/geometry/policies/relate/intersection_points.hpp index ff8ec1949..8a315ef57 100644 --- a/include/boost/geometry/policies/relate/intersection_points.hpp +++ b/include/boost/geometry/policies/relate/intersection_points.hpp @@ -22,13 +22,42 @@ #include #include - namespace boost { namespace geometry { namespace policies { namespace relate { +template ::is_integer> +struct round_dispatch +{ + template + static inline Result apply(T const& v) + { + return v < 0 ? + boost::numeric_cast(ceil(v - 0.5f)) : + boost::numeric_cast(floor(v + 0.5f)); + } +}; + +template +struct round_dispatch +{ + template + static inline Result apply(T const& v) + { + return boost::numeric_cast(v); + } +}; + +template +inline Result round(T const& v) +{ + // NOTE: boost::round() could be used instead but it throws in some situations + + //BOOST_STATIC_ASSERT(!std::numeric_limits::is_integer); + return round_dispatch::apply(v); +} template struct segments_intersection_points @@ -60,9 +89,9 @@ struct segments_intersection_points return_type result; result.count = 1; set<0>(result.intersections[0], - boost::numeric_cast(R(s1x) + r * R(dx1))); + round(R(s1x) + r * R(dx1))); set<1>(result.intersections[0], - boost::numeric_cast(R(s1y) + r * R(dy1))); + round(R(s1y) + r * R(dy1))); return result; }