bounded_view.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Boost.Geometry Index
  2. //
  3. // This view makes possible to treat some simple primitives as its bounding geometry
  4. // e.g. box, nsphere, etc.
  5. //
  6. // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
  7. //
  8. // This file was modified by Oracle on 2019-2020.
  9. // Modifications copyright (c) 2019-2020 Oracle and/or its affiliates.
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP
  17. #include <boost/geometry/algorithms/envelope.hpp>
  18. #include <boost/geometry/core/static_assert.hpp>
  19. #include <boost/geometry/strategies/default_strategy.hpp>
  20. #include <boost/geometry/strategies/index/services.hpp>
  21. namespace boost { namespace geometry {
  22. namespace index { namespace detail {
  23. template <typename Geometry, typename BoundingGeometry, typename Strategy>
  24. struct bounded_view_base_cs_tag
  25. {
  26. typedef typename Strategy::cs_tag type;
  27. };
  28. template <typename Geometry, typename BoundingGeometry>
  29. struct bounded_view_base_cs_tag<Geometry, BoundingGeometry, default_strategy>
  30. : geometry::cs_tag<Geometry>
  31. {};
  32. template
  33. <
  34. typename Geometry,
  35. typename BoundingGeometry,
  36. typename Strategy,
  37. typename Tag = typename geometry::tag<Geometry>::type,
  38. typename BoundingTag = typename geometry::tag<BoundingGeometry>::type,
  39. typename CSTag = typename bounded_view_base_cs_tag
  40. <
  41. Geometry, BoundingGeometry, Strategy
  42. >::type
  43. >
  44. struct bounded_view_base
  45. {
  46. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  47. "Not implemented for these Geometries.",
  48. Geometry, BoundingGeometry, Strategy, Tag, BoundingTag, CSTag);
  49. };
  50. // Segment -> Box
  51. template <typename Segment, typename Box, typename Strategy>
  52. struct bounded_view_base<Segment, Box, Strategy, segment_tag, box_tag, cartesian_tag>
  53. {
  54. public:
  55. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  56. bounded_view_base(Segment const& segment, Strategy const& )
  57. : m_segment(segment)
  58. {}
  59. template <std::size_t Dimension>
  60. inline coordinate_type get_min() const
  61. {
  62. return boost::numeric_cast<coordinate_type>(
  63. (std::min)( geometry::get<0, Dimension>(m_segment),
  64. geometry::get<1, Dimension>(m_segment) ) );
  65. }
  66. template <std::size_t Dimension>
  67. inline coordinate_type get_max() const
  68. {
  69. return boost::numeric_cast<coordinate_type>(
  70. (std::max)( geometry::get<0, Dimension>(m_segment),
  71. geometry::get<1, Dimension>(m_segment) ) );
  72. }
  73. private:
  74. Segment const& m_segment;
  75. };
  76. template <typename Segment, typename Box, typename Strategy, typename CSTag>
  77. struct bounded_view_base<Segment, Box, Strategy, segment_tag, box_tag, CSTag>
  78. {
  79. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  80. bounded_view_base(Segment const& segment, Strategy const& strategy)
  81. {
  82. geometry::envelope(segment, m_box, strategy);
  83. }
  84. template <std::size_t Dimension>
  85. inline coordinate_type get_min() const
  86. {
  87. return geometry::get<min_corner, Dimension>(m_box);
  88. }
  89. template <std::size_t Dimension>
  90. inline coordinate_type get_max() const
  91. {
  92. return geometry::get<max_corner, Dimension>(m_box);
  93. }
  94. private:
  95. Box m_box;
  96. };
  97. // Box -> Box
  98. template <typename BoxIn, typename Box, typename Strategy, typename CSTag>
  99. struct bounded_view_base<BoxIn, Box, Strategy, box_tag, box_tag, CSTag>
  100. {
  101. public:
  102. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  103. bounded_view_base(BoxIn const& box, Strategy const& )
  104. : m_box(box)
  105. {}
  106. template <std::size_t Dimension>
  107. inline coordinate_type get_min() const
  108. {
  109. return boost::numeric_cast<coordinate_type>(
  110. geometry::get<min_corner, Dimension>(m_box) );
  111. }
  112. template <std::size_t Dimension>
  113. inline coordinate_type get_max() const
  114. {
  115. return boost::numeric_cast<coordinate_type>(
  116. geometry::get<max_corner, Dimension>(m_box) );
  117. }
  118. private:
  119. BoxIn const& m_box;
  120. };
  121. // Point -> Box
  122. template <typename Point, typename Box, typename Strategy, typename CSTag>
  123. struct bounded_view_base<Point, Box, Strategy, point_tag, box_tag, CSTag>
  124. {
  125. public:
  126. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  127. bounded_view_base(Point const& point, Strategy const& )
  128. : m_point(point)
  129. {}
  130. template <std::size_t Dimension>
  131. inline coordinate_type get_min() const
  132. {
  133. return boost::numeric_cast<coordinate_type>(
  134. geometry::get<Dimension>(m_point) );
  135. }
  136. template <std::size_t Dimension>
  137. inline coordinate_type get_max() const
  138. {
  139. return boost::numeric_cast<coordinate_type>(
  140. geometry::get<Dimension>(m_point) );
  141. }
  142. private:
  143. Point const& m_point;
  144. };
  145. template <typename Geometry,
  146. typename BoundingGeometry,
  147. typename Strategy,
  148. typename Tag = typename geometry::tag<Geometry>::type,
  149. typename BoundingTag = typename geometry::tag<BoundingGeometry>::type>
  150. struct bounded_view
  151. : bounded_view_base<Geometry, BoundingGeometry, Strategy>
  152. {
  153. typedef bounded_view_base<Geometry, BoundingGeometry, Strategy> base_type;
  154. bounded_view(Geometry const& geometry, Strategy const& strategy)
  155. : base_type(geometry, strategy)
  156. {}
  157. };
  158. template <typename Geometry,
  159. typename BoundingGeometry,
  160. typename Tag,
  161. typename BoundingTag>
  162. struct bounded_view<Geometry, BoundingGeometry, default_strategy, Tag, BoundingTag>
  163. : bounded_view_base
  164. <
  165. Geometry,
  166. BoundingGeometry,
  167. typename strategies::index::services::default_strategy<Geometry>::type
  168. >
  169. {
  170. typedef typename strategies::index::services::default_strategy
  171. <
  172. Geometry
  173. >::type strategy_type;
  174. typedef bounded_view_base
  175. <
  176. Geometry,
  177. BoundingGeometry,
  178. strategy_type
  179. > base_type;
  180. explicit bounded_view(Geometry const& geometry, default_strategy const& )
  181. : base_type(geometry, strategy_type())
  182. {}
  183. };
  184. }} // namespace index::detail
  185. // XXX -> Box
  186. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  187. namespace traits
  188. {
  189. template <typename Geometry, typename Box, typename Strategy, typename Tag>
  190. struct tag< index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag> >
  191. {
  192. typedef box_tag type;
  193. };
  194. template <typename Geometry, typename Box, typename Strategy, typename Tag>
  195. struct point_type< index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag> >
  196. {
  197. typedef typename point_type<Box>::type type;
  198. };
  199. template <typename Geometry, typename Box, typename Strategy, typename Tag, std::size_t Dimension>
  200. struct indexed_access<index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag>,
  201. min_corner, Dimension>
  202. {
  203. typedef index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag> box_type;
  204. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  205. static inline coordinate_type get(box_type const& b)
  206. {
  207. return b.template get_min<Dimension>();
  208. }
  209. //static inline void set(box_type & b, coordinate_type const& value)
  210. //{
  211. // BOOST_GEOMETRY_INDEX_ASSERT(false, "unable to modify a box through view");
  212. //}
  213. };
  214. template <typename Geometry, typename Box, typename Strategy, typename Tag, std::size_t Dimension>
  215. struct indexed_access<index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag>,
  216. max_corner, Dimension>
  217. {
  218. typedef index::detail::bounded_view<Geometry, Box, Strategy, Tag, box_tag> box_type;
  219. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  220. static inline coordinate_type get(box_type const& b)
  221. {
  222. return b.template get_max<Dimension>();
  223. }
  224. //static inline void set(box_type & b, coordinate_type const& value)
  225. //{
  226. // BOOST_GEOMETRY_INDEX_ASSERT(false, "unable to modify a box through view");
  227. //}
  228. };
  229. } // namespace traits
  230. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  231. }} // namespace boost::geometry
  232. #endif // BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP