1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (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)
14 #ifndef BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP
15 #define BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP
20 #include <boost/geometry/strategies/transform/matrix_transformers.hpp>
22 namespace boost { namespace geometry
25 // Silence warning C4127: conditional expression is constant
28 #pragma warning(disable : 4127)
31 namespace strategy { namespace transform
35 \brief Transformation strategy to map from one to another Cartesian coordinate system
37 \tparam Mirror if true map is mirrored upside-down (in most cases pixels
38 are from top to bottom, while map is from bottom to top)
42 typename CalculationType,
43 std::size_t Dimension1,
44 std::size_t Dimension2,
49 : public ublas_transformer<CalculationType, Dimension1, Dimension2>
51 typedef boost::numeric::ublas::matrix<CalculationType> M;
54 template <typename B, typename D>
55 explicit inline map_transformer(B const& box, D const& width, D const& height)
58 get<min_corner, 0>(box), get<min_corner, 1>(box),
59 get<max_corner, 0>(box), get<max_corner, 1>(box),
63 template <typename W, typename D>
64 explicit inline map_transformer(W const& wx1, W const& wy1, W const& wx2, W const& wy2,
65 D const& width, D const& height)
67 set_transformation(wx1, wy1, wx2, wy2, width, height);
72 template <typename W, typename P, typename S>
73 inline void set_transformation_point(W const& wx, W const& wy,
74 P const& px, P const& py,
75 S const& scalex, S const& scaley)
78 // Translate to a coordinate system centered on world coordinates (-wx, -wy)
80 t1(0,0) = 1; t1(0,1) = 0; t1(0,2) = -wx;
81 t1(1,0) = 0; t1(1,1) = 1; t1(1,2) = -wy;
82 t1(2,0) = 0; t1(2,1) = 0; t1(2,2) = 1;
86 s(0,0) = scalex; s(0,1) = 0; s(0,2) = 0;
87 s(1,0) = 0; s(1,1) = scaley; s(1,2) = 0;
88 s(2,0) = 0; s(2,1) = 0; s(2,2) = 1;
90 // Translate to a coordinate system centered on the specified pixels (+px, +py)
92 t2(0,0) = 1; t2(0,1) = 0; t2(0,2) = px;
93 t2(1,0) = 0; t2(1,1) = 1; t2(1,2) = py;
94 t2(2,0) = 0; t2(2,1) = 0; t2(2,2) = 1;
96 // Calculate combination matrix in two steps
97 this->m_matrix = boost::numeric::ublas::prod(s, t1);
98 this->m_matrix = boost::numeric::ublas::prod(t2, this->m_matrix);
102 template <typename W, typename D>
103 void set_transformation(W const& wx1, W const& wy1, W const& wx2, W const& wy2,
104 D const& width, D const& height)
111 // Get the same type, but at least a double
112 typedef typename select_most_precise<D, double>::type type;
115 // Calculate appropriate scale, take min because whole box must fit
116 // Scale is in PIXELS/MAPUNITS (meters)
119 type sx = (px2 - px1) / boost::numeric_cast<type>(wdx);
120 type sy = (py2 - py1) / boost::numeric_cast<type>(wdy);
124 type scale = (std::min)(sx, sy);
129 // Calculate centerpoints
135 type pmx = (px1 + px2) / 2.0;
136 type pmy = (py1 + py2) / 2.0;
138 set_transformation_point(wmx, wmy, pmx, pmy, sx, sy);
142 // Mirror in y-direction
144 m(0,0) = 1; m(0,1) = 0; m(0,2) = 0;
145 m(1,0) = 0; m(1,1) = -1; m(1,2) = 0;
146 m(2,0) = 0; m(2,1) = 0; m(2,2) = 1;
148 // Translate in y-direction such that it fits again
150 y(0,0) = 1; y(0,1) = 0; y(0,2) = 0;
151 y(1,0) = 0; y(1,1) = 1; y(1,2) = height;
152 y(2,0) = 0; y(2,1) = 0; y(2,2) = 1;
154 // Calculate combination matrix in two steps
155 this->m_matrix = boost::numeric::ublas::prod(m, this->m_matrix);
156 this->m_matrix = boost::numeric::ublas::prod(y, this->m_matrix);
162 }} // namespace strategy::transform
164 #if defined(_MSC_VER)
168 }} // namespace boost::geometry
171 #endif // BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MAP_TRANSFORMER_HPP