1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2015.
8 // Modifications copyright (c) 2015 Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP
20 #define BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP
25 // Remove the ublas checking, otherwise the inverse might fail
26 // (while nothing seems to be wrong)
27 #ifdef BOOST_UBLAS_TYPE_CHECK
28 #undef BOOST_UBLAS_TYPE_CHECK
30 #define BOOST_UBLAS_TYPE_CHECK 0
32 #include <boost/numeric/conversion/cast.hpp>
34 #if defined(__clang__)
35 // Avoid warning about unused UBLAS function: boost_numeric_ublas_abs
36 #pragma clang diagnostic push
37 #pragma clang diagnostic ignored "-Wunused-function"
40 #include <boost/numeric/ublas/vector.hpp>
41 #include <boost/numeric/ublas/matrix.hpp>
43 #if defined(__clang__)
44 #pragma clang diagnostic pop
47 #include <boost/geometry/core/access.hpp>
48 #include <boost/geometry/core/coordinate_dimension.hpp>
49 #include <boost/geometry/core/cs.hpp>
50 #include <boost/geometry/util/math.hpp>
51 #include <boost/geometry/util/promote_floating_point.hpp>
52 #include <boost/geometry/util/select_coordinate_type.hpp>
53 #include <boost/geometry/util/select_most_precise.hpp>
56 namespace boost { namespace geometry
59 namespace strategy { namespace transform
63 \brief Affine transformation strategy in Cartesian system.
64 \details The strategy serves as a generic definition of affine transformation matrix
65 and procedure of application it to given point.
66 \see http://en.wikipedia.org/wiki/Affine_transformation
67 and http://www.devmaster.net/wiki/Transformation_matrices
69 \tparam Dimension1 number of dimensions to transform from
70 \tparam Dimension2 number of dimensions to transform to
74 typename CalculationType,
75 std::size_t Dimension1,
76 std::size_t Dimension2
78 class ublas_transformer
83 template <typename CalculationType>
84 class ublas_transformer<CalculationType, 2, 2>
87 typedef CalculationType ct;
88 typedef boost::numeric::ublas::matrix<ct> matrix_type;
93 inline ublas_transformer(
94 ct const& m_0_0, ct const& m_0_1, ct const& m_0_2,
95 ct const& m_1_0, ct const& m_1_1, ct const& m_1_2,
96 ct const& m_2_0, ct const& m_2_1, ct const& m_2_2)
99 m_matrix(0,0) = m_0_0; m_matrix(0,1) = m_0_1; m_matrix(0,2) = m_0_2;
100 m_matrix(1,0) = m_1_0; m_matrix(1,1) = m_1_1; m_matrix(1,2) = m_1_2;
101 m_matrix(2,0) = m_2_0; m_matrix(2,1) = m_2_1; m_matrix(2,2) = m_2_2;
104 inline ublas_transformer(matrix_type const& matrix)
109 inline ublas_transformer() : m_matrix(3, 3) {}
111 template <typename P1, typename P2>
112 inline bool apply(P1 const& p1, P2& p2) const
114 assert_dimension_greater_equal<P1, 2>();
115 assert_dimension_greater_equal<P2, 2>();
117 ct const& c1 = get<0>(p1);
118 ct const& c2 = get<1>(p1);
120 ct p2x = c1 * m_matrix(0,0) + c2 * m_matrix(0,1) + m_matrix(0,2);
121 ct p2y = c1 * m_matrix(1,0) + c2 * m_matrix(1,1) + m_matrix(1,2);
123 typedef typename geometry::coordinate_type<P2>::type ct2;
124 set<0>(p2, boost::numeric_cast<ct2>(p2x));
125 set<1>(p2, boost::numeric_cast<ct2>(p2y));
130 matrix_type const& matrix() const { return m_matrix; }
134 // It IS possible to go from 3 to 2 coordinates
135 template <typename CalculationType>
136 class ublas_transformer<CalculationType, 3, 2> : public ublas_transformer<CalculationType, 2, 2>
138 typedef CalculationType ct;
141 inline ublas_transformer(
142 ct const& m_0_0, ct const& m_0_1, ct const& m_0_2,
143 ct const& m_1_0, ct const& m_1_1, ct const& m_1_2,
144 ct const& m_2_0, ct const& m_2_1, ct const& m_2_2)
145 : ublas_transformer<CalculationType, 2, 2>(
151 inline ublas_transformer()
152 : ublas_transformer<CalculationType, 2, 2>()
157 template <typename CalculationType>
158 class ublas_transformer<CalculationType, 3, 3>
161 typedef CalculationType ct;
162 typedef boost::numeric::ublas::matrix<ct> matrix_type;
163 matrix_type m_matrix;
166 inline ublas_transformer(
167 ct const& m_0_0, ct const& m_0_1, ct const& m_0_2, ct const& m_0_3,
168 ct const& m_1_0, ct const& m_1_1, ct const& m_1_2, ct const& m_1_3,
169 ct const& m_2_0, ct const& m_2_1, ct const& m_2_2, ct const& m_2_3,
170 ct const& m_3_0, ct const& m_3_1, ct const& m_3_2, ct const& m_3_3
174 m_matrix(0,0) = m_0_0; m_matrix(0,1) = m_0_1; m_matrix(0,2) = m_0_2; m_matrix(0,3) = m_0_3;
175 m_matrix(1,0) = m_1_0; m_matrix(1,1) = m_1_1; m_matrix(1,2) = m_1_2; m_matrix(1,3) = m_1_3;
176 m_matrix(2,0) = m_2_0; m_matrix(2,1) = m_2_1; m_matrix(2,2) = m_2_2; m_matrix(2,3) = m_2_3;
177 m_matrix(3,0) = m_3_0; m_matrix(3,1) = m_3_1; m_matrix(3,2) = m_3_2; m_matrix(3,3) = m_3_3;
180 inline ublas_transformer() : m_matrix(4, 4) {}
182 template <typename P1, typename P2>
183 inline bool apply(P1 const& p1, P2& p2) const
185 ct const& c1 = get<0>(p1);
186 ct const& c2 = get<1>(p1);
187 ct const& c3 = get<2>(p1);
189 typedef typename geometry::coordinate_type<P2>::type ct2;
191 set<0>(p2, boost::numeric_cast<ct2>(
192 c1 * m_matrix(0,0) + c2 * m_matrix(0,1) + c3 * m_matrix(0,2) + m_matrix(0,3)));
193 set<1>(p2, boost::numeric_cast<ct2>(
194 c1 * m_matrix(1,0) + c2 * m_matrix(1,1) + c3 * m_matrix(1,2) + m_matrix(1,3)));
195 set<2>(p2, boost::numeric_cast<ct2>(
196 c1 * m_matrix(2,0) + c2 * m_matrix(2,1) + c3 * m_matrix(2,2) + m_matrix(2,3)));
201 matrix_type const& matrix() const { return m_matrix; }
206 \brief Strategy of translate transformation in Cartesian system.
207 \details Translate moves a geometry a fixed distance in 2 or 3 dimensions.
208 \see http://en.wikipedia.org/wiki/Translation_%28geometry%29
210 \tparam Dimension1 number of dimensions to transform from
211 \tparam Dimension2 number of dimensions to transform to
215 typename CalculationType,
216 std::size_t Dimension1,
217 std::size_t Dimension2
219 class translate_transformer
224 template<typename CalculationType>
225 class translate_transformer<CalculationType, 2, 2> : public ublas_transformer<CalculationType, 2, 2>
228 // To have translate transformers compatible for 2/3 dimensions, the
229 // constructor takes an optional third argument doing nothing.
230 inline translate_transformer(CalculationType const& translate_x,
231 CalculationType const& translate_y,
232 CalculationType const& = 0)
233 : ublas_transformer<CalculationType, 2, 2>(
241 template <typename CalculationType>
242 class translate_transformer<CalculationType, 3, 3> : public ublas_transformer<CalculationType, 3, 3>
245 inline translate_transformer(CalculationType const& translate_x,
246 CalculationType const& translate_y,
247 CalculationType const& translate_z)
248 : ublas_transformer<CalculationType, 3, 3>(
249 1, 0, 0, translate_x,
250 0, 1, 0, translate_y,
251 0, 0, 1, translate_z,
259 \brief Strategy of scale transformation in Cartesian system.
260 \details Scale scales a geometry up or down in all its dimensions.
261 \see http://en.wikipedia.org/wiki/Scaling_%28geometry%29
263 \tparam Dimension1 number of dimensions to transform from
264 \tparam Dimension2 number of dimensions to transform to
268 typename CalculationType,
269 std::size_t Dimension1,
270 std::size_t Dimension2
272 class scale_transformer
277 template <typename CalculationType>
278 class scale_transformer<CalculationType, 2, 2> : public ublas_transformer<CalculationType, 2, 2>
282 inline scale_transformer(CalculationType const& scale_x,
283 CalculationType const& scale_y,
284 CalculationType const& = 0)
285 : ublas_transformer<CalculationType, 2, 2>(
292 inline scale_transformer(CalculationType const& scale)
293 : ublas_transformer<CalculationType, 2, 2>(
301 template <typename CalculationType>
302 class scale_transformer<CalculationType, 3, 3> : public ublas_transformer<CalculationType, 3, 3>
305 inline scale_transformer(CalculationType const& scale_x,
306 CalculationType const& scale_y,
307 CalculationType const& scale_z)
308 : ublas_transformer<CalculationType, 3, 3>(
316 inline scale_transformer(CalculationType const& scale)
317 : ublas_transformer<CalculationType, 3, 3>(
326 #ifndef DOXYGEN_NO_DETAIL
331 template <typename DegreeOrRadian>
337 struct as_radian<radian>
339 template <typename T>
340 static inline T get(T const& value)
347 struct as_radian<degree>
349 template <typename T>
350 static inline T get(T const& value)
352 typedef typename promote_floating_point<T>::type promoted_type;
353 return value * math::d2r<promoted_type>();
361 typename CalculationType,
362 std::size_t Dimension1,
363 std::size_t Dimension2
365 class rad_rotate_transformer
366 : public ublas_transformer<CalculationType, Dimension1, Dimension2>
369 inline rad_rotate_transformer(CalculationType const& angle)
370 : ublas_transformer<CalculationType, Dimension1, Dimension2>(
371 cos(angle), sin(angle), 0,
372 -sin(angle), cos(angle), 0,
378 } // namespace detail
379 #endif // DOXYGEN_NO_DETAIL
383 \brief Strategy for rotate transformation in Cartesian coordinate system.
384 \details Rotate rotates a geometry of specified angle about a fixed point (e.g. origin).
385 \see http://en.wikipedia.org/wiki/Rotation_%28mathematics%29
387 \tparam DegreeOrRadian degree/or/radian, type of rotation angle specification
388 \note A single angle is needed to specify a rotation in 2D.
389 Not yet in 3D, the 3D version requires special things to allow
390 for rotation around X, Y, Z or arbitrary axis.
391 \todo The 3D version will not compile.
395 typename DegreeOrRadian,
396 typename CalculationType,
397 std::size_t Dimension1,
398 std::size_t Dimension2
400 class rotate_transformer : public detail::rad_rotate_transformer<CalculationType, Dimension1, Dimension2>
404 inline rotate_transformer(CalculationType const& angle)
405 : detail::rad_rotate_transformer
407 CalculationType, Dimension1, Dimension2
408 >(detail::as_radian<DegreeOrRadian>::get(angle))
413 }} // namespace strategy::transform
416 }} // namespace boost::geometry
419 #endif // BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP