1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
3 // Use, modification and distribution is subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP
8 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP
12 #include <boost/math/special_functions/fpclassify.hpp>
14 #include <boost/geometry/core/coordinate_type.hpp>
15 #include <boost/geometry/core/access.hpp>
16 #include <boost/geometry/util/math.hpp>
17 #include <boost/geometry/util/select_most_precise.hpp>
19 #include <boost/geometry/strategies/buffer.hpp>
20 #include <boost/geometry/strategies/side.hpp>
24 namespace boost { namespace geometry
27 namespace strategy { namespace buffer
33 \brief Let the buffer use straight sides along segments (the default)
35 \details This strategy can be used as SideStrategy for the buffer algorithm.
36 It is currently the only provided strategy for this purpose
40 See the examples for other buffer strategies\, for example
41 [link geometry.reference.strategies.strategy_buffer_join_round join_round]
43 \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)]
49 #ifndef DOXYGEN_SHOULD_SKIP_THIS
54 typename DistanceStrategy
56 static inline result_code apply(
57 Point const& input_p1, Point const& input_p2,
58 buffer_side_selector side,
59 DistanceStrategy const& distance,
60 OutputRange& output_range)
62 typedef typename coordinate_type<Point>::type coordinate_type;
63 typedef typename geometry::select_most_precise
67 >::type promoted_type;
69 // Generate a block along (left or right of) the segment
71 // Simulate a vector d (dx,dy)
72 coordinate_type const dx = get<0>(input_p2) - get<0>(input_p1);
73 coordinate_type const dy = get<1>(input_p2) - get<1>(input_p1);
75 // For normalization [0,1] (=dot product d.d, sqrt)
76 promoted_type const length = geometry::math::sqrt(dx * dx + dy * dy);
78 if (! boost::math::isfinite(length))
80 // In case of coordinates differences of e.g. 1e300, length
81 // will overflow and we should not generate output
82 #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
83 std::cout << "Error in length calculation for points "
84 << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
85 << " length: " << length << std::endl;
87 return result_error_numerical;
90 if (geometry::math::equals(length, 0))
92 // Coordinates are simplified and therefore most often not equal.
93 // But if simplify is skipped, or for lines with two
94 // equal points, length is 0 and we cannot generate output.
95 return result_no_output;
98 promoted_type const d = distance.apply(input_p1, input_p2, side);
100 // Generate the normalized perpendicular p, to the left (ccw)
101 promoted_type const px = -dy / length;
102 promoted_type const py = dx / length;
104 if (geometry::math::equals(px, 0)
105 && geometry::math::equals(py, 0))
107 // This basically should not occur - because of the checks above.
108 // There are no unit tests triggering this condition
109 #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
110 std::cout << "Error in perpendicular calculation for points "
111 << geometry::wkt(input_p1) << " " << geometry::wkt(input_p2)
112 << " length: " << length
113 << " distance: " << d
116 return result_no_output;
119 output_range.resize(2);
121 set<0>(output_range.front(), get<0>(input_p1) + px * d);
122 set<1>(output_range.front(), get<1>(input_p1) + py * d);
123 set<0>(output_range.back(), get<0>(input_p2) + px * d);
124 set<1>(output_range.back(), get<1>(input_p2) + py * d);
126 return result_normal;
128 #endif // DOXYGEN_SHOULD_SKIP_THIS
132 }} // namespace strategy::buffer
134 }} // namespace boost::geometry
136 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP