box.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_GEOMETRIES_BOX_HPP
  11. #define BOOST_GEOMETRY_GEOMETRIES_BOX_HPP
  12. #include <cstddef>
  13. #include <boost/concept/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/geometry/algorithms/convert.hpp>
  16. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  17. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  18. #include <boost/geometry/core/assert.hpp>
  19. #endif
  20. namespace boost { namespace geometry
  21. {
  22. namespace model
  23. {
  24. /*!
  25. \brief Class box: defines a box made of two describing points
  26. \ingroup geometries
  27. \details Box is always described by a min_corner() and a max_corner() point. If another
  28. rectangle is used, use linear_ring or polygon.
  29. \note Boxes are for selections and for calculating the envelope of geometries. Not all algorithms
  30. are implemented for box. Boxes are also used in Spatial Indexes.
  31. \tparam Point point type. The box takes a point type as template parameter.
  32. The point type can be any point type.
  33. It can be 2D but can also be 3D or more dimensional.
  34. The box can also take a latlong point type as template parameter.
  35. \qbk{[include reference/geometries/box.qbk]}
  36. \qbk{before.synopsis, [heading Model of]}
  37. \qbk{before.synopsis, [link geometry.reference.concepts.concept_box Box Concept]}
  38. */
  39. template<typename Point>
  40. class box
  41. {
  42. BOOST_CONCEPT_ASSERT( (concepts::Point<Point>) );
  43. public:
  44. #if !defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  45. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  46. /// \constructor_default_no_init
  47. box() = default;
  48. #else
  49. /// \constructor_default_no_init
  50. inline box()
  51. {}
  52. #endif
  53. #else // defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  54. inline box()
  55. {
  56. m_created = 1;
  57. }
  58. ~box()
  59. {
  60. m_created = 0;
  61. }
  62. #endif
  63. /*!
  64. \brief Constructor taking the minimum corner point and the maximum corner point
  65. */
  66. inline box(Point const& min_corner, Point const& max_corner)
  67. {
  68. geometry::convert(min_corner, m_min_corner);
  69. geometry::convert(max_corner, m_max_corner);
  70. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  71. m_created = 1;
  72. #endif
  73. }
  74. inline Point const& min_corner() const
  75. {
  76. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  77. BOOST_GEOMETRY_ASSERT(m_created == 1);
  78. #endif
  79. return m_min_corner;
  80. }
  81. inline Point const& max_corner() const
  82. {
  83. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  84. BOOST_GEOMETRY_ASSERT(m_created == 1);
  85. #endif
  86. return m_max_corner;
  87. }
  88. inline Point& min_corner()
  89. {
  90. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  91. BOOST_GEOMETRY_ASSERT(m_created == 1);
  92. #endif
  93. return m_min_corner;
  94. }
  95. inline Point& max_corner()
  96. {
  97. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  98. BOOST_GEOMETRY_ASSERT(m_created == 1);
  99. #endif
  100. return m_max_corner;
  101. }
  102. private:
  103. Point m_min_corner;
  104. Point m_max_corner;
  105. #if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
  106. int m_created;
  107. #endif
  108. };
  109. } // namespace model
  110. // Traits specializations for box above
  111. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  112. namespace traits
  113. {
  114. template <typename Point>
  115. struct tag<model::box<Point> >
  116. {
  117. typedef box_tag type;
  118. };
  119. template <typename Point>
  120. struct point_type<model::box<Point> >
  121. {
  122. typedef Point type;
  123. };
  124. template <typename Point, std::size_t Dimension>
  125. struct indexed_access<model::box<Point>, min_corner, Dimension>
  126. {
  127. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  128. static inline coordinate_type get(model::box<Point> const& b)
  129. {
  130. return geometry::get<Dimension>(b.min_corner());
  131. }
  132. static inline void set(model::box<Point>& b, coordinate_type const& value)
  133. {
  134. geometry::set<Dimension>(b.min_corner(), value);
  135. }
  136. };
  137. template <typename Point, std::size_t Dimension>
  138. struct indexed_access<model::box<Point>, max_corner, Dimension>
  139. {
  140. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  141. static inline coordinate_type get(model::box<Point> const& b)
  142. {
  143. return geometry::get<Dimension>(b.max_corner());
  144. }
  145. static inline void set(model::box<Point>& b, coordinate_type const& value)
  146. {
  147. geometry::set<Dimension>(b.max_corner(), value);
  148. }
  149. };
  150. } // namespace traits
  151. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  152. }} // namespace boost::geometry
  153. #endif // BOOST_GEOMETRY_GEOMETRIES_BOX_HPP