box.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2020.
  6. // Modifications copyright (c) 2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (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)
  13. #ifndef BOOST_GEOMETRY_GEOMETRIES_BOX_HPP
  14. #define BOOST_GEOMETRY_GEOMETRIES_BOX_HPP
  15. #include <cstddef>
  16. #include <type_traits>
  17. #include <boost/concept/assert.hpp>
  18. #include <boost/geometry/algorithms/convert.hpp>
  19. #include <boost/geometry/core/access.hpp>
  20. #include <boost/geometry/core/make.hpp>
  21. #include <boost/geometry/core/point_type.hpp>
  22. #include <boost/geometry/core/tag.hpp>
  23. #include <boost/geometry/core/tags.hpp>
  24. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  25. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  26. #include <boost/geometry/core/assert.hpp>
  27. #endif
  28. namespace boost { namespace geometry
  29. {
  30. namespace model
  31. {
  32. /*!
  33. \brief Class box: defines a box made of two describing points
  34. \ingroup geometries
  35. \details Box is always described by a min_corner() and a max_corner() point. If another
  36. rectangle is used, use linear_ring or polygon.
  37. \note Boxes are for selections and for calculating the envelope of geometries. Not all algorithms
  38. are implemented for box. Boxes are also used in Spatial Indexes.
  39. \tparam Point point type. The box takes a point type as template parameter.
  40. The point type can be any point type.
  41. It can be 2D but can also be 3D or more dimensional.
  42. The box can also take a latlong point type as template parameter.
  43. \qbk{[include reference/geometries/box.qbk]}
  44. \qbk{before.synopsis, [heading Model of]}
  45. \qbk{before.synopsis, [link geometry.reference.concepts.concept_box Box Concept]}
  46. */
  47. template<typename Point>
  48. class box
  49. {
  50. BOOST_CONCEPT_ASSERT( (concepts::Point<Point>) );
  51. public:
  52. // TODO: constexpr requires LiteralType and until C++20
  53. // it has to have trivial destructor which makes access
  54. // debugging impossible with constexpr.
  55. #if !defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  56. /// \constructor_default_no_init
  57. constexpr box() = default;
  58. #else // defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  59. box()
  60. {
  61. m_created = 1;
  62. }
  63. ~box()
  64. {
  65. m_created = 0;
  66. }
  67. #endif
  68. /*!
  69. \brief Constructor taking the minimum corner point and the maximum corner point
  70. */
  71. template
  72. <
  73. typename P = Point,
  74. std::enable_if_t
  75. <
  76. ! std::is_copy_constructible<P>::value,
  77. int
  78. > = 0
  79. >
  80. box(Point const& min_corner, Point const& max_corner)
  81. {
  82. geometry::convert(min_corner, m_min_corner);
  83. geometry::convert(max_corner, m_max_corner);
  84. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  85. m_created = 1;
  86. #endif
  87. }
  88. /*!
  89. \brief Constructor taking the minimum corner point and the maximum corner point
  90. */
  91. template
  92. <
  93. typename P = Point,
  94. std::enable_if_t
  95. <
  96. std::is_copy_constructible<P>::value,
  97. int
  98. > = 0
  99. >
  100. #if ! defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  101. constexpr
  102. #endif
  103. box(Point const& min_corner, Point const& max_corner)
  104. : m_min_corner(min_corner)
  105. , m_max_corner(max_corner)
  106. {
  107. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  108. m_created = 1;
  109. #endif
  110. }
  111. #if ! defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  112. constexpr
  113. #endif
  114. Point const& min_corner() const
  115. {
  116. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  117. BOOST_GEOMETRY_ASSERT(m_created == 1);
  118. #endif
  119. return m_min_corner;
  120. }
  121. #if ! defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  122. constexpr
  123. #endif
  124. Point const& max_corner() const
  125. {
  126. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  127. BOOST_GEOMETRY_ASSERT(m_created == 1);
  128. #endif
  129. return m_max_corner;
  130. }
  131. Point& min_corner()
  132. {
  133. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  134. BOOST_GEOMETRY_ASSERT(m_created == 1);
  135. #endif
  136. return m_min_corner;
  137. }
  138. Point& max_corner()
  139. {
  140. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  141. BOOST_GEOMETRY_ASSERT(m_created == 1);
  142. #endif
  143. return m_max_corner;
  144. }
  145. private:
  146. Point m_min_corner;
  147. Point m_max_corner;
  148. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  149. int m_created;
  150. #endif
  151. };
  152. } // namespace model
  153. // Traits specializations for box above
  154. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  155. namespace traits
  156. {
  157. template <typename Point>
  158. struct tag<model::box<Point> >
  159. {
  160. typedef box_tag type;
  161. };
  162. template <typename Point>
  163. struct point_type<model::box<Point> >
  164. {
  165. typedef Point type;
  166. };
  167. template <typename Point, std::size_t Dimension>
  168. struct indexed_access<model::box<Point>, min_corner, Dimension>
  169. {
  170. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  171. static constexpr coordinate_type get(model::box<Point> const& b)
  172. {
  173. return geometry::get<Dimension>(b.min_corner());
  174. }
  175. static void set(model::box<Point>& b, coordinate_type const& value)
  176. {
  177. geometry::set<Dimension>(b.min_corner(), value);
  178. }
  179. };
  180. template <typename Point, std::size_t Dimension>
  181. struct indexed_access<model::box<Point>, max_corner, Dimension>
  182. {
  183. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  184. static constexpr coordinate_type get(model::box<Point> const& b)
  185. {
  186. return geometry::get<Dimension>(b.max_corner());
  187. }
  188. static void set(model::box<Point>& b, coordinate_type const& value)
  189. {
  190. geometry::set<Dimension>(b.max_corner(), value);
  191. }
  192. };
  193. template <typename Point>
  194. struct make<model::box<Point> >
  195. {
  196. typedef model::box<Point> box_type;
  197. static const bool is_specialized = true;
  198. static constexpr box_type apply(Point const& min_corner, Point const& max_corner)
  199. {
  200. return box_type(min_corner, max_corner);
  201. }
  202. };
  203. } // namespace traits
  204. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  205. }} // namespace boost::geometry
  206. #endif // BOOST_GEOMETRY_GEOMETRIES_BOX_HPP