bounded_view.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP
  12. #define BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP
  13. #include <boost/geometry/algorithms/envelope.hpp>
  14. namespace boost { namespace geometry {
  15. namespace index { namespace detail {
  16. template <typename Geometry,
  17. typename BoundingGeometry,
  18. typename Tag = typename geometry::tag<Geometry>::type,
  19. typename BoundingTag = typename geometry::tag<BoundingGeometry>::type,
  20. typename CSystem = typename geometry::coordinate_system<Geometry>::type>
  21. struct bounded_view
  22. {
  23. BOOST_MPL_ASSERT_MSG(
  24. (false),
  25. NOT_IMPLEMENTED_FOR_THOSE_GEOMETRIES,
  26. (BoundingTag, Tag));
  27. };
  28. // Segment -> Box
  29. template <typename Segment, typename Box>
  30. struct bounded_view<Segment, Box, segment_tag, box_tag, cs::cartesian>
  31. {
  32. public:
  33. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  34. explicit bounded_view(Segment const& segment)
  35. : m_segment(segment)
  36. {}
  37. template <std::size_t Dimension>
  38. inline coordinate_type get_min() const
  39. {
  40. return boost::numeric_cast<coordinate_type>(
  41. (std::min)( geometry::get<0, Dimension>(m_segment),
  42. geometry::get<1, Dimension>(m_segment) ) );
  43. }
  44. template <std::size_t Dimension>
  45. inline coordinate_type get_max() const
  46. {
  47. return boost::numeric_cast<coordinate_type>(
  48. (std::max)( geometry::get<0, Dimension>(m_segment),
  49. geometry::get<1, Dimension>(m_segment) ) );
  50. }
  51. private:
  52. Segment const& m_segment;
  53. };
  54. template <typename Segment, typename Box, typename CSystem>
  55. struct bounded_view<Segment, Box, segment_tag, box_tag, CSystem>
  56. {
  57. public:
  58. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  59. explicit bounded_view(Segment const& segment)
  60. {
  61. geometry::envelope(segment, m_box);
  62. }
  63. template <std::size_t Dimension>
  64. inline coordinate_type get_min() const
  65. {
  66. return geometry::get<min_corner, Dimension>(m_box);
  67. }
  68. template <std::size_t Dimension>
  69. inline coordinate_type get_max() const
  70. {
  71. return geometry::get<max_corner, Dimension>(m_box);
  72. }
  73. private:
  74. Box m_box;
  75. };
  76. // Box -> Box
  77. template <typename BoxIn, typename Box, typename CSystem>
  78. struct bounded_view<BoxIn, Box, box_tag, box_tag, CSystem>
  79. {
  80. public:
  81. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  82. explicit bounded_view(BoxIn const& box)
  83. : m_box(box)
  84. {}
  85. template <std::size_t Dimension>
  86. inline coordinate_type get_min() const
  87. {
  88. return boost::numeric_cast<coordinate_type>(
  89. geometry::get<min_corner, Dimension>(m_box) );
  90. }
  91. template <std::size_t Dimension>
  92. inline coordinate_type get_max() const
  93. {
  94. return boost::numeric_cast<coordinate_type>(
  95. geometry::get<max_corner, Dimension>(m_box) );
  96. }
  97. private:
  98. BoxIn const& m_box;
  99. };
  100. // Point -> Box
  101. template <typename Point, typename Box, typename CSystem>
  102. struct bounded_view<Point, Box, point_tag, box_tag, CSystem>
  103. {
  104. public:
  105. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  106. explicit bounded_view(Point const& point)
  107. : m_point(point)
  108. {}
  109. template <std::size_t Dimension>
  110. inline coordinate_type get_min() const
  111. {
  112. return boost::numeric_cast<coordinate_type>(
  113. geometry::get<Dimension>(m_point) );
  114. }
  115. template <std::size_t Dimension>
  116. inline coordinate_type get_max() const
  117. {
  118. return boost::numeric_cast<coordinate_type>(
  119. geometry::get<Dimension>(m_point) );
  120. }
  121. private:
  122. Point const& m_point;
  123. };
  124. }} // namespace index::detail
  125. // XXX -> Box
  126. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  127. namespace traits
  128. {
  129. template <typename Geometry, typename Box, typename Tag, typename CSystem>
  130. struct tag< index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem> >
  131. {
  132. typedef box_tag type;
  133. };
  134. template <typename Geometry, typename Box, typename Tag, typename CSystem>
  135. struct point_type< index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem> >
  136. {
  137. typedef typename point_type<Box>::type type;
  138. };
  139. template <typename Geometry, typename Box, typename Tag, typename CSystem, std::size_t Dimension>
  140. struct indexed_access<index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem>,
  141. min_corner, Dimension>
  142. {
  143. typedef index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem> box_type;
  144. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  145. static inline coordinate_type get(box_type const& b)
  146. {
  147. return b.template get_min<Dimension>();
  148. }
  149. //static inline void set(box_type & b, coordinate_type const& value)
  150. //{
  151. // BOOST_GEOMETRY_INDEX_ASSERT(false, "unable to modify a box through view");
  152. //}
  153. };
  154. template <typename Geometry, typename Box, typename Tag, typename CSystem, std::size_t Dimension>
  155. struct indexed_access<index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem>,
  156. max_corner, Dimension>
  157. {
  158. typedef index::detail::bounded_view<Geometry, Box, Tag, box_tag, CSystem> box_type;
  159. typedef typename geometry::coordinate_type<Box>::type coordinate_type;
  160. static inline coordinate_type get(box_type const& b)
  161. {
  162. return b.template get_max<Dimension>();
  163. }
  164. //static inline void set(box_type & b, coordinate_type const& value)
  165. //{
  166. // BOOST_GEOMETRY_INDEX_ASSERT(false, "unable to modify a box through view");
  167. //}
  168. };
  169. } // namespace traits
  170. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  171. }} // namespace boost::geometry
  172. #endif // BOOST_GEOMETRY_INDEX_DETAIL_BOUNDED_VIEW_HPP