translating_transformer.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014.
  7. // Modifications copyright (c) 2014 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_CENTROID_TRANSLATING_TRANSFORMER_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_CENTROID_TRANSLATING_TRANSFORMER_HPP
  14. #include <cstddef>
  15. #include <boost/core/addressof.hpp>
  16. #include <boost/core/ref.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/core/tag_cast.hpp>
  19. #include <boost/geometry/core/tags.hpp>
  20. #include <boost/geometry/core/point_type.hpp>
  21. #include <boost/geometry/arithmetic/arithmetic.hpp>
  22. #include <boost/geometry/iterators/point_iterator.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace centroid
  27. {
  28. // NOTE: There is no need to translate in other coordinate systems than
  29. // cartesian. But if it was needed then one should translate using
  30. // CS-specific technique, e.g. in spherical/geographic a translation
  31. // vector should contain coordinates being multiplies of 2PI or 360 deg.
  32. template <typename Geometry,
  33. typename CastedTag = typename tag_cast
  34. <
  35. typename tag<Geometry>::type,
  36. areal_tag
  37. >::type,
  38. typename CSTag = typename cs_tag<Geometry>::type>
  39. struct translating_transformer
  40. {
  41. typedef typename geometry::point_type<Geometry>::type point_type;
  42. typedef boost::reference_wrapper<point_type const> result_type;
  43. explicit translating_transformer(Geometry const&) {}
  44. explicit translating_transformer(point_type const&) {}
  45. result_type apply(point_type const& pt) const
  46. {
  47. return result_type(pt);
  48. }
  49. template <typename ResPt>
  50. void apply_reverse(ResPt &) const {}
  51. };
  52. // Specialization for Areal Geometries in cartesian CS
  53. template <typename Geometry>
  54. struct translating_transformer<Geometry, areal_tag, cartesian_tag>
  55. {
  56. typedef typename geometry::point_type<Geometry>::type point_type;
  57. typedef point_type result_type;
  58. explicit translating_transformer(Geometry const& geom)
  59. : m_origin(NULL)
  60. {
  61. geometry::point_iterator<Geometry const>
  62. pt_it = geometry::points_begin(geom);
  63. if ( pt_it != geometry::points_end(geom) )
  64. {
  65. m_origin = boost::addressof(*pt_it);
  66. }
  67. }
  68. explicit translating_transformer(point_type const& origin)
  69. : m_origin(boost::addressof(origin))
  70. {}
  71. result_type apply(point_type const& pt) const
  72. {
  73. point_type res = pt;
  74. if ( m_origin )
  75. geometry::subtract_point(res, *m_origin);
  76. return res;
  77. }
  78. template <typename ResPt>
  79. void apply_reverse(ResPt & res_pt) const
  80. {
  81. if ( m_origin )
  82. geometry::add_point(res_pt, *m_origin);
  83. }
  84. const point_type * m_origin;
  85. };
  86. }} // namespace detail::centroid
  87. #endif // DOXYGEN_NO_DETAIL
  88. }} // namespace boost::geometry
  89. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_CENTROID_TRANSLATING_TRANSFORMER_HPP