point.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_POINT_HPP
  11. #define BOOST_GEOMETRY_GEOMETRIES_POINT_HPP
  12. #include <cstddef>
  13. #include <boost/mpl/int.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/coordinate_type.hpp>
  17. #include <boost/geometry/core/coordinate_system.hpp>
  18. #include <boost/geometry/core/coordinate_dimension.hpp>
  19. #include <boost/geometry/util/math.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. // Silence warning C4127: conditional expression is constant
  23. #if defined(_MSC_VER)
  24. #pragma warning(push)
  25. #pragma warning(disable : 4127)
  26. #endif
  27. namespace model
  28. {
  29. /*!
  30. \brief Basic point class, having coordinates defined in a neutral way
  31. \details Defines a neutral point class, fulfilling the Point Concept.
  32. Library users can use this point class, or use their own point classes.
  33. This point class is used in most of the samples and tests of Boost.Geometry
  34. This point class is used occasionally within the library, where a temporary
  35. point class is necessary.
  36. \ingroup geometries
  37. \tparam CoordinateType \tparam_numeric
  38. \tparam DimensionCount number of coordinates, usually 2 or 3
  39. \tparam CoordinateSystem coordinate system, for example cs::cartesian
  40. \qbk{[include reference/geometries/point.qbk]}
  41. \qbk{before.synopsis, [heading Model of]}
  42. \qbk{before.synopsis, [link geometry.reference.concepts.concept_point Point Concept]}
  43. */
  44. template
  45. <
  46. typename CoordinateType,
  47. std::size_t DimensionCount,
  48. typename CoordinateSystem
  49. >
  50. class point
  51. {
  52. public:
  53. /// @brief Default constructor, no initialization
  54. inline point()
  55. {}
  56. /// @brief Constructor to set one, two or three values
  57. explicit inline point(CoordinateType const& v0, CoordinateType const& v1 = 0, CoordinateType const& v2 = 0)
  58. {
  59. if (DimensionCount >= 1) m_values[0] = v0;
  60. if (DimensionCount >= 2) m_values[1] = v1;
  61. if (DimensionCount >= 3) m_values[2] = v2;
  62. }
  63. /// @brief Get a coordinate
  64. /// @tparam K coordinate to get
  65. /// @return the coordinate
  66. template <std::size_t K>
  67. inline CoordinateType const& get() const
  68. {
  69. BOOST_STATIC_ASSERT(K < DimensionCount);
  70. return m_values[K];
  71. }
  72. /// @brief Set a coordinate
  73. /// @tparam K coordinate to set
  74. /// @param value value to set
  75. template <std::size_t K>
  76. inline void set(CoordinateType const& value)
  77. {
  78. BOOST_STATIC_ASSERT(K < DimensionCount);
  79. m_values[K] = value;
  80. }
  81. private:
  82. CoordinateType m_values[DimensionCount];
  83. };
  84. } // namespace model
  85. // Adapt the point to the concept
  86. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  87. namespace traits
  88. {
  89. template
  90. <
  91. typename CoordinateType,
  92. std::size_t DimensionCount,
  93. typename CoordinateSystem
  94. >
  95. struct tag<model::point<CoordinateType, DimensionCount, CoordinateSystem> >
  96. {
  97. typedef point_tag type;
  98. };
  99. template
  100. <
  101. typename CoordinateType,
  102. std::size_t DimensionCount,
  103. typename CoordinateSystem
  104. >
  105. struct coordinate_type<model::point<CoordinateType, DimensionCount, CoordinateSystem> >
  106. {
  107. typedef CoordinateType type;
  108. };
  109. template
  110. <
  111. typename CoordinateType,
  112. std::size_t DimensionCount,
  113. typename CoordinateSystem
  114. >
  115. struct coordinate_system<model::point<CoordinateType, DimensionCount, CoordinateSystem> >
  116. {
  117. typedef CoordinateSystem type;
  118. };
  119. template
  120. <
  121. typename CoordinateType,
  122. std::size_t DimensionCount,
  123. typename CoordinateSystem
  124. >
  125. struct dimension<model::point<CoordinateType, DimensionCount, CoordinateSystem> >
  126. : boost::mpl::int_<DimensionCount>
  127. {};
  128. template
  129. <
  130. typename CoordinateType,
  131. std::size_t DimensionCount,
  132. typename CoordinateSystem,
  133. std::size_t Dimension
  134. >
  135. struct access<model::point<CoordinateType, DimensionCount, CoordinateSystem>, Dimension>
  136. {
  137. static inline CoordinateType get(
  138. model::point<CoordinateType, DimensionCount, CoordinateSystem> const& p)
  139. {
  140. return p.template get<Dimension>();
  141. }
  142. static inline void set(
  143. model::point<CoordinateType, DimensionCount, CoordinateSystem>& p,
  144. CoordinateType const& value)
  145. {
  146. p.template set<Dimension>(value);
  147. }
  148. };
  149. } // namespace traits
  150. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  151. #if defined(_MSC_VER)
  152. #pragma warning(pop)
  153. #endif
  154. }} // namespace boost::geometry
  155. #endif // BOOST_GEOMETRY_GEOMETRIES_POINT_HPP