map_transformer.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP
  12. #include <cstddef>
  13. #include <boost/geometry/strategies/transform/matrix_transformers.hpp>
  14. namespace boost { namespace geometry
  15. {
  16. // Silence warning C4127: conditional expression is constant
  17. #if defined(_MSC_VER)
  18. #pragma warning(push)
  19. #pragma warning(disable : 4127)
  20. #endif
  21. namespace strategy { namespace transform
  22. {
  23. /*!
  24. \brief Transformation strategy to map from one to another Cartesian coordinate system
  25. \ingroup strategies
  26. \tparam Mirror if true map is mirrored upside-down (in most cases pixels
  27. are from top to bottom, while map is from bottom to top)
  28. */
  29. template
  30. <
  31. typename CalculationType,
  32. std::size_t Dimension1,
  33. std::size_t Dimension2,
  34. bool Mirror = false,
  35. bool SameScale = true
  36. >
  37. class map_transformer
  38. : public matrix_transformer<CalculationType, Dimension1, Dimension2>
  39. {
  40. typedef boost::qvm::mat<CalculationType, Dimension1 + 1, Dimension2 + 1> M;
  41. typedef boost::qvm::mat<CalculationType, 3, 3> matrix33;
  42. public :
  43. template <typename B, typename D>
  44. explicit inline map_transformer(B const& box, D const& width, D const& height)
  45. {
  46. set_transformation(
  47. get<min_corner, 0>(box), get<min_corner, 1>(box),
  48. get<max_corner, 0>(box), get<max_corner, 1>(box),
  49. width, height);
  50. }
  51. template <typename W, typename D>
  52. explicit inline map_transformer(W const& wx1, W const& wy1, W const& wx2, W const& wy2,
  53. D const& width, D const& height)
  54. {
  55. set_transformation(wx1, wy1, wx2, wy2, width, height);
  56. }
  57. private :
  58. template <typename W, typename P, typename S>
  59. inline void set_transformation_point(W const& wx, W const& wy,
  60. P const& px, P const& py,
  61. S const& scalex, S const& scaley)
  62. {
  63. // Translate to a coordinate system centered on world coordinates (-wx, -wy)
  64. matrix33 t1;
  65. qvm::A<0,0>(t1) = 1; qvm::A<0,1>(t1) = 0; qvm::A<0,2>(t1) = -wx;
  66. qvm::A<1,0>(t1) = 0; qvm::A<1,1>(t1) = 1; qvm::A<1,2>(t1) = -wy;
  67. qvm::A<2,0>(t1) = 0; qvm::A<2,1>(t1) = 0; qvm::A<2,2>(t1) = 1;
  68. // Scale the map
  69. matrix33 s;
  70. qvm::A<0,0>(s) = scalex; qvm::A<0,1>(s) = 0; qvm::A<0,2>(s) = 0;
  71. qvm::A<1,0>(s) = 0; qvm::A<1,1>(s) = scaley; qvm::A<1,2>(s) = 0;
  72. qvm::A<2,0>(s) = 0; qvm::A<2,1>(s) = 0; qvm::A<2,2>(s) = 1;
  73. // Translate to a coordinate system centered on the specified pixels (+px, +py)
  74. matrix33 t2;
  75. qvm::A<0,0>(t2) = 1; qvm::A<0,1>(t2) = 0; qvm::A<0,2>(t2) = px;
  76. qvm::A<1,0>(t2) = 0; qvm::A<1,1>(t2) = 1; qvm::A<1,2>(t2) = py;
  77. qvm::A<2,0>(t2) = 0; qvm::A<2,1>(t2) = 0; qvm::A<2,2>(t2) = 1;
  78. // Calculate combination matrix in two steps
  79. this->m_matrix = s * t1;
  80. this->m_matrix = t2 * this->m_matrix;
  81. }
  82. template <typename W, typename D>
  83. void set_transformation(W const& wx1, W const& wy1, W const& wx2, W const& wy2,
  84. D const& width, D const& height)
  85. {
  86. D px1 = 0;
  87. D py1 = 0;
  88. D px2 = width;
  89. D py2 = height;
  90. // Get the same type, but at least a double
  91. typedef typename select_most_precise<D, double>::type type;
  92. // Calculate appropriate scale, take min because whole box must fit
  93. // Scale is in PIXELS/MAPUNITS (meters)
  94. W wdx = wx2 - wx1;
  95. W wdy = wy2 - wy1;
  96. type sx = (px2 - px1) / boost::numeric_cast<type>(wdx);
  97. type sy = (py2 - py1) / boost::numeric_cast<type>(wdy);
  98. if (SameScale)
  99. {
  100. type scale = (std::min)(sx, sy);
  101. sx = scale;
  102. sy = scale;
  103. }
  104. // Calculate centerpoints
  105. W wtx = wx1 + wx2;
  106. W wty = wy1 + wy2;
  107. W two = 2;
  108. W wmx = wtx / two;
  109. W wmy = wty / two;
  110. type pmx = (px1 + px2) / 2.0;
  111. type pmy = (py1 + py2) / 2.0;
  112. set_transformation_point(wmx, wmy, pmx, pmy, sx, sy);
  113. if (Mirror)
  114. {
  115. // Mirror in y-direction
  116. matrix33 m;
  117. qvm::A<0,0>(m) = 1; qvm::A<0,1>(m) = 0; qvm::A<0,2>(m) = 0;
  118. qvm::A<1,0>(m) = 0; qvm::A<1,1>(m) = -1; qvm::A<1,2>(m) = 0;
  119. qvm::A<2,0>(m) = 0; qvm::A<2,1>(m) = 0; qvm::A<2,2>(m) = 1;
  120. // Translate in y-direction such that it fits again
  121. matrix33 y;
  122. qvm::A<0,0>(y) = 1; qvm::A<0,1>(y) = 0; qvm::A<0,2>(y) = 0;
  123. qvm::A<1,0>(y) = 0; qvm::A<1,1>(y) = 1; qvm::A<1,2>(y) = height;
  124. qvm::A<2,0>(y) = 0; qvm::A<2,1>(y) = 0; qvm::A<2,2>(y) = 1;
  125. // Calculate combination matrix in two steps
  126. this->m_matrix = m * this->m_matrix;
  127. this->m_matrix = y * this->m_matrix;
  128. }
  129. }
  130. };
  131. }} // namespace strategy::transform
  132. #if defined(_MSC_VER)
  133. #pragma warning(pop)
  134. #endif
  135. }} // namespace boost::geometry
  136. #endif // BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP