ring.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2010-2012 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. #ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_RING_HPP
  7. #define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_RING_HPP
  8. // Adapts Geometries from Boost.Polygon for usage in Boost.Geometry
  9. // boost::polygon::polygon_data -> boost::geometry::ring
  10. #include <cstddef>
  11. #include <boost/polygon/polygon.hpp>
  12. #include <boost/geometry/core/access.hpp>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/core/coordinate_dimension.hpp>
  15. #include <boost/geometry/core/coordinate_type.hpp>
  16. #include <boost/geometry/core/mutable_range.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  19. namespace boost { namespace geometry
  20. {
  21. namespace traits
  22. {
  23. template <typename CoordinateType>
  24. struct tag<boost::polygon::polygon_data<CoordinateType> >
  25. {
  26. typedef ring_tag type;
  27. };
  28. template <typename CoordinateType>
  29. struct clear<boost::polygon::polygon_data<CoordinateType> >
  30. {
  31. static inline void apply(boost::polygon::polygon_data<CoordinateType>& data)
  32. {
  33. // There is no "clear" function but we can assign
  34. // a newly (and therefore empty) constructed polygon
  35. boost::polygon::assign(data, boost::polygon::polygon_data<CoordinateType>());
  36. }
  37. };
  38. template <typename CoordinateType>
  39. struct push_back<boost::polygon::polygon_data<CoordinateType> >
  40. {
  41. typedef boost::polygon::point_data<CoordinateType> point_type;
  42. static inline void apply(boost::polygon::polygon_data<CoordinateType>& data,
  43. point_type const& point)
  44. {
  45. // Boost.Polygon's polygons are not appendable. So create a temporary vector,
  46. // add a record and set it to the original. Of course: this is not efficient.
  47. // But there seems no other way (without using a wrapper)
  48. std::vector<point_type> temporary_vector
  49. (
  50. boost::polygon::begin_points(data),
  51. boost::polygon::end_points(data)
  52. );
  53. temporary_vector.push_back(point);
  54. data.set(temporary_vector.begin(), temporary_vector.end());
  55. }
  56. };
  57. template <typename CoordinateType>
  58. struct resize<boost::polygon::polygon_data<CoordinateType> >
  59. {
  60. typedef boost::polygon::point_data<CoordinateType> point_type;
  61. static inline void apply(boost::polygon::polygon_data<CoordinateType>& data,
  62. std::size_t new_size)
  63. {
  64. // Boost.Polygon's polygons are not resizable. So create a temporary vector,
  65. // resize it and set it to the original. Of course: this is not efficient.
  66. // But there seems no other way (without using a wrapper)
  67. std::vector<point_type> temporary_vector
  68. (
  69. boost::polygon::begin_points(data),
  70. boost::polygon::end_points(data)
  71. );
  72. temporary_vector.resize(new_size);
  73. data.set(temporary_vector.begin(), temporary_vector.end());
  74. }
  75. };
  76. } // namespace traits
  77. }} // namespace boost::geometry
  78. // Adapt Boost.Polygon's polygon_data to Boost.Range
  79. // This just translates to
  80. // polygon_data.begin() and polygon_data.end()
  81. namespace boost
  82. {
  83. template<typename CoordinateType>
  84. struct range_mutable_iterator<boost::polygon::polygon_data<CoordinateType> >
  85. {
  86. typedef typename boost::polygon::polygon_traits
  87. <
  88. boost::polygon::polygon_data<CoordinateType>
  89. >::iterator_type type;
  90. };
  91. template<typename CoordinateType>
  92. struct range_const_iterator<boost::polygon::polygon_data<CoordinateType> >
  93. {
  94. typedef typename boost::polygon::polygon_traits
  95. <
  96. boost::polygon::polygon_data<CoordinateType>
  97. >::iterator_type type;
  98. };
  99. template<typename CoordinateType>
  100. struct range_size<boost::polygon::polygon_data<CoordinateType> >
  101. {
  102. typedef std::size_t type;
  103. };
  104. } // namespace boost
  105. // Support Boost.Polygon's polygon_data for Boost.Range ADP
  106. namespace boost { namespace polygon
  107. {
  108. template<typename CoordinateType>
  109. inline typename polygon_traits
  110. <
  111. polygon_data<CoordinateType>
  112. >::iterator_type range_begin(polygon_data<CoordinateType>& polygon)
  113. {
  114. return polygon.begin();
  115. }
  116. template<typename CoordinateType>
  117. inline typename polygon_traits
  118. <
  119. polygon_data<CoordinateType>
  120. >::iterator_type range_begin(polygon_data<CoordinateType> const& polygon)
  121. {
  122. return polygon.begin();
  123. }
  124. template<typename CoordinateType>
  125. inline typename polygon_traits
  126. <
  127. polygon_data<CoordinateType>
  128. >::iterator_type range_end(polygon_data<CoordinateType>& polygon)
  129. {
  130. return polygon.end();
  131. }
  132. template<typename CoordinateType>
  133. inline typename polygon_traits
  134. <
  135. polygon_data<CoordinateType>
  136. >::iterator_type range_end(polygon_data<CoordinateType> const& polygon)
  137. {
  138. return polygon.end();
  139. }
  140. template<typename CoordinateType>
  141. inline std::size_t range_calculate_size(polygon_data<CoordinateType> const& polygon)
  142. {
  143. return polygon.size();
  144. }
  145. }}
  146. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  147. #endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_RING_HPP