1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_ASSIGN_VALUES_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_ASSIGN_VALUES_HPP
20 #include <boost/concept/requires.hpp>
21 #include <boost/concept_check.hpp>
22 #include <boost/mpl/assert.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/numeric/conversion/bounds.hpp>
25 #include <boost/numeric/conversion/cast.hpp>
27 #include <boost/geometry/arithmetic/arithmetic.hpp>
28 #include <boost/geometry/algorithms/append.hpp>
29 #include <boost/geometry/algorithms/clear.hpp>
30 #include <boost/geometry/core/access.hpp>
31 #include <boost/geometry/core/exterior_ring.hpp>
32 #include <boost/geometry/core/tags.hpp>
34 #include <boost/geometry/geometries/concepts/check.hpp>
37 #include <boost/geometry/util/for_each_coordinate.hpp>
40 namespace boost { namespace geometry
43 #ifndef DOXYGEN_NO_DETAIL
44 namespace detail { namespace assign
48 template <std::size_t Index, std::size_t Dimension, std::size_t DimensionCount>
51 template <typename Box>
52 static inline void apply(Box& box, typename coordinate_type<Box>::type const& value)
54 geometry::set<Index, Dimension>(box, value);
55 initialize<Index, Dimension + 1, DimensionCount>::apply(box, value);
60 template <std::size_t Index, std::size_t DimensionCount>
61 struct initialize<Index, DimensionCount, DimensionCount>
63 template <typename Box>
64 static inline void apply(Box&, typename coordinate_type<Box>::type const&)
69 struct assign_zero_point
71 template <typename Point>
72 static inline void apply(Point& point)
74 geometry::assign_value(point, 0);
79 struct assign_inverse_box_or_segment
82 template <typename BoxOrSegment>
83 static inline void apply(BoxOrSegment& geometry)
85 typedef typename point_type<BoxOrSegment>::type point_type;
86 typedef typename coordinate_type<point_type>::type bound_type;
88 initialize<0, 0, dimension<BoxOrSegment>::type::value>::apply(
89 geometry, boost::numeric::bounds<bound_type>::highest()
91 initialize<1, 0, dimension<BoxOrSegment>::type::value>::apply(
92 geometry, boost::numeric::bounds<bound_type>::lowest()
98 struct assign_zero_box_or_segment
100 template <typename BoxOrSegment>
101 static inline void apply(BoxOrSegment& geometry)
103 typedef typename coordinate_type<BoxOrSegment>::type coordinate_type;
105 initialize<0, 0, dimension<BoxOrSegment>::type::value>::apply(
106 geometry, coordinate_type()
108 initialize<1, 0, dimension<BoxOrSegment>::type::value>::apply(
109 geometry, coordinate_type()
117 std::size_t Corner1, std::size_t Corner2,
118 typename Box, typename Point
120 inline void assign_box_2d_corner(Box const& box, Point& point)
122 // Be sure both are 2-Dimensional
123 assert_dimension<Box, 2>();
124 assert_dimension<Point, 2>();
127 typedef typename coordinate_type<Point>::type coordinate_type;
129 geometry::set<0>(point, boost::numeric_cast<coordinate_type>(get<Corner1, 0>(box)));
130 geometry::set<1>(point, boost::numeric_cast<coordinate_type>(get<Corner2, 1>(box)));
137 typename Geometry, typename Point,
139 std::size_t Dimension, std::size_t DimensionCount
141 struct assign_point_to_index
144 static inline void apply(Point const& point, Geometry& geometry)
146 geometry::set<Index, Dimension>(geometry, boost::numeric_cast
148 typename coordinate_type<Geometry>::type
149 >(geometry::get<Dimension>(point)));
151 assign_point_to_index
153 Geometry, Point, Index, Dimension + 1, DimensionCount
154 >::apply(point, geometry);
160 typename Geometry, typename Point,
162 std::size_t DimensionCount
164 struct assign_point_to_index
168 DimensionCount, DimensionCount
171 static inline void apply(Point const& , Geometry& )
179 typename Geometry, typename Point,
181 std::size_t Dimension, std::size_t DimensionCount
183 struct assign_point_from_index
186 static inline void apply(Geometry const& geometry, Point& point)
188 geometry::set<Dimension>( point, boost::numeric_cast
190 typename coordinate_type<Point>::type
191 >(geometry::get<Index, Dimension>(geometry)));
193 assign_point_from_index
195 Geometry, Point, Index, Dimension + 1, DimensionCount
196 >::apply(geometry, point);
202 typename Geometry, typename Point,
204 std::size_t DimensionCount
206 struct assign_point_from_index
210 DimensionCount, DimensionCount
213 static inline void apply(Geometry const&, Point&)
219 template <typename Geometry>
220 struct assign_2d_box_or_segment
222 typedef typename coordinate_type<Geometry>::type coordinate_type;
224 // Here we assign 4 coordinates to a box of segment
225 // -> Most logical is: x1,y1,x2,y2
226 // In case the user reverses x1/x2 or y1/y2, for a box, we could reverse them (THAT IS NOT IMPLEMENTED)
228 template <typename Type>
229 static inline void apply(Geometry& geometry,
230 Type const& x1, Type const& y1, Type const& x2, Type const& y2)
232 geometry::set<0, 0>(geometry, boost::numeric_cast<coordinate_type>(x1));
233 geometry::set<0, 1>(geometry, boost::numeric_cast<coordinate_type>(y1));
234 geometry::set<1, 0>(geometry, boost::numeric_cast<coordinate_type>(x2));
235 geometry::set<1, 1>(geometry, boost::numeric_cast<coordinate_type>(y2));
240 }} // namespace detail::assign
241 #endif // DOXYGEN_NO_DETAIL
243 #ifndef DOXYGEN_NO_DISPATCH
247 template <typename GeometryTag, typename Geometry, std::size_t DimensionCount>
252 false, NOT_OR_NOT_YET_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE
257 template <typename Point>
258 struct assign<point_tag, Point, 2>
260 typedef typename coordinate_type<Point>::type coordinate_type;
262 template <typename T>
263 static inline void apply(Point& point, T const& c1, T const& c2)
265 set<0>(point, boost::numeric_cast<coordinate_type>(c1));
266 set<1>(point, boost::numeric_cast<coordinate_type>(c2));
270 template <typename Point>
271 struct assign<point_tag, Point, 3>
273 typedef typename coordinate_type<Point>::type coordinate_type;
275 template <typename T>
276 static inline void apply(Point& point, T const& c1, T const& c2, T const& c3)
278 set<0>(point, boost::numeric_cast<coordinate_type>(c1));
279 set<1>(point, boost::numeric_cast<coordinate_type>(c2));
280 set<2>(point, boost::numeric_cast<coordinate_type>(c3));
284 template <typename Box>
285 struct assign<box_tag, Box, 2>
286 : detail::assign::assign_2d_box_or_segment<Box>
289 template <typename Segment>
290 struct assign<segment_tag, Segment, 2>
291 : detail::assign::assign_2d_box_or_segment<Segment>
296 template <typename GeometryTag, typename Geometry>
297 struct assign_zero {};
300 template <typename Point>
301 struct assign_zero<point_tag, Point>
302 : detail::assign::assign_zero_point
305 template <typename Box>
306 struct assign_zero<box_tag, Box>
307 : detail::assign::assign_zero_box_or_segment
310 template <typename Segment>
311 struct assign_zero<segment_tag, Segment>
312 : detail::assign::assign_zero_box_or_segment
316 template <typename GeometryTag, typename Geometry>
317 struct assign_inverse {};
319 template <typename Box>
320 struct assign_inverse<box_tag, Box>
321 : detail::assign::assign_inverse_box_or_segment
324 template <typename Segment>
325 struct assign_inverse<segment_tag, Segment>
326 : detail::assign::assign_inverse_box_or_segment
330 } // namespace dispatch
331 #endif // DOXYGEN_NO_DISPATCH
333 }} // namespace boost::geometry
336 #endif // BOOST_GEOMETRY_ALGORITHMS_ASSIGN_VALUES_HPP