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) 6 7 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP 8 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP 9 10 #include <cstddef> 11 12 #include <boost/math/special_functions/fpclassify.hpp> 13 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> 18 19 #include <boost/geometry/strategies/buffer.hpp> 20 #include <boost/geometry/strategies/side.hpp> 21 22 23 24 namespace boost { namespace geometry 25 { 26 27 namespace strategy { namespace buffer 28 { 29 30 31 32 /*! 33 \brief Let the buffer use straight sides along segments (the default) 34 \ingroup strategies 35 \details This strategy can be used as SideStrategy for the buffer algorithm. 36 It is currently the only provided strategy for this purpose 37 38 \qbk{ 39 [heading Example] 40 See the examples for other buffer strategies\, for example 41 [link geometry.reference.strategies.strategy_buffer_join_round join_round] 42 [heading See also] 43 \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)] 44 } 45 */ 46 class side_straight 47 { 48 public : 49 #ifndef DOXYGEN_SHOULD_SKIP_THIS 50 template 51 < 52 typename Point, 53 typename OutputRange, 54 typename DistanceStrategy 55 > apply(Point const & input_p1,Point const & input_p2,buffer_side_selector side,DistanceStrategy const & distance,OutputRange & output_range)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) 61 { 62 typedef typename coordinate_type<Point>::type coordinate_type; 63 typedef typename geometry::select_most_precise 64 < 65 coordinate_type, 66 double 67 >::type promoted_type; 68 69 // Generate a block along (left or right of) the segment 70 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); 74 75 // For normalization [0,1] (=dot product d.d, sqrt) 76 promoted_type const length = geometry::math::sqrt(dx * dx + dy * dy); 77 78 if (! boost::math::isfinite(length)) 79 { 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; 86 #endif 87 return result_error_numerical; 88 } 89 90 if (geometry::math::equals(length, 0)) 91 { 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; 96 } 97 98 promoted_type const d = distance.apply(input_p1, input_p2, side); 99 100 // Generate the normalized perpendicular p, to the left (ccw) 101 promoted_type const px = -dy / length; 102 promoted_type const py = dx / length; 103 104 if (geometry::math::equals(px, 0) 105 && geometry::math::equals(py, 0)) 106 { 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 114 << std::endl; 115 #endif 116 return result_no_output; 117 } 118 119 output_range.resize(2); 120 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); 125 126 return result_normal; 127 } 128 #endif // DOXYGEN_SHOULD_SKIP_THIS 129 }; 130 131 132 }} // namespace strategy::buffer 133 134 }} // namespace boost::geometry 135 136 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_SIDE_STRAIGHT_HPP 137