matrix_transformers.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2015.
  6. // Modifications copyright (c) 2015 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (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)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP
  15. #include <cstddef>
  16. #include <boost/qvm/mat.hpp>
  17. #include <boost/qvm/vec.hpp>
  18. #include <boost/qvm/mat_access.hpp>
  19. #include <boost/qvm/vec_access.hpp>
  20. #include <boost/qvm/mat_operations.hpp>
  21. #include <boost/qvm/vec_mat_operations.hpp>
  22. #include <boost/qvm/map_mat_mat.hpp>
  23. #include <boost/qvm/map_mat_vec.hpp>
  24. #include <boost/geometry/core/access.hpp>
  25. #include <boost/geometry/core/coordinate_dimension.hpp>
  26. #include <boost/geometry/core/cs.hpp>
  27. #include <boost/geometry/util/math.hpp>
  28. #include <boost/geometry/util/promote_floating_point.hpp>
  29. #include <boost/geometry/util/select_coordinate_type.hpp>
  30. #include <boost/geometry/util/select_most_precise.hpp>
  31. namespace boost { namespace geometry
  32. {
  33. namespace strategy { namespace transform
  34. {
  35. namespace detail { namespace matrix_transformer
  36. {
  37. template
  38. <
  39. typename Point,
  40. std::size_t Dimension = 0,
  41. std::size_t DimensionCount = geometry::dimension<Point>::value
  42. >
  43. struct set_point_from_vec
  44. {
  45. template <typename Vector>
  46. static inline void apply(Point & p, Vector const& v)
  47. {
  48. typedef typename geometry::coordinate_type<Point>::type coord_t;
  49. set<Dimension>(p, boost::numeric_cast<coord_t>(qvm::A<Dimension>(v)));
  50. set_point_from_vec<Point, Dimension + 1, DimensionCount>::apply(p, v);
  51. }
  52. };
  53. template
  54. <
  55. typename Point,
  56. std::size_t DimensionCount
  57. >
  58. struct set_point_from_vec<Point, DimensionCount, DimensionCount>
  59. {
  60. template <typename Vector>
  61. static inline void apply(Point &, Vector const&) {}
  62. };
  63. template
  64. <
  65. typename Point,
  66. std::size_t Dimension = 0,
  67. std::size_t DimensionCount = geometry::dimension<Point>::value
  68. >
  69. struct set_vec_from_point
  70. {
  71. template <typename Vector>
  72. static inline void apply(Point const& p, Vector & v)
  73. {
  74. qvm::A<Dimension>(v) = get<Dimension>(p);
  75. set_vec_from_point<Point, Dimension + 1, DimensionCount>::apply(p, v);
  76. }
  77. };
  78. template
  79. <
  80. typename Point,
  81. std::size_t DimensionCount
  82. >
  83. struct set_vec_from_point<Point, DimensionCount, DimensionCount>
  84. {
  85. template <typename Vector>
  86. static inline void apply(Point const&, Vector &) {}
  87. };
  88. template
  89. <
  90. typename CalculationType,
  91. std::size_t Dimension1,
  92. std::size_t Dimension2
  93. >
  94. class matrix_transformer
  95. {
  96. protected :
  97. typedef CalculationType ct;
  98. typedef boost::qvm::mat<ct, Dimension2 + 1, Dimension1 + 1> matrix_type;
  99. matrix_type m_matrix;
  100. public :
  101. matrix_type const& matrix() const { return m_matrix; }
  102. template <typename P1, typename P2>
  103. inline bool apply(P1 const& p1, P2& p2) const
  104. {
  105. assert_dimension_greater_equal<P1,Dimension1>();
  106. assert_dimension_greater_equal<P2,Dimension2>();
  107. qvm::vec<ct,Dimension1 + 1> p1temp;
  108. qvm::A<Dimension1>(p1temp) = 1;
  109. qvm::vec<ct,Dimension2 + 1> p2temp;
  110. set_vec_from_point<P1, 0, Dimension1>::apply(p1, p1temp);
  111. p2temp = m_matrix * p1temp;
  112. set_point_from_vec<P2, 0, Dimension2>::apply(p2, p2temp);
  113. return true;
  114. }
  115. };
  116. }} // namespace detail::matrix_transform
  117. /*!
  118. \brief Affine transformation strategy in Cartesian system.
  119. \details The strategy serves as a generic definition of an affine transformation
  120. matrix and procedure for applying it to a given point.
  121. \see http://en.wikipedia.org/wiki/Affine_transformation
  122. and http://www.devmaster.net/wiki/Transformation_matrices
  123. \ingroup strategies
  124. \tparam Dimension1 number of dimensions to transform from
  125. \tparam Dimension2 number of dimensions to transform to
  126. */
  127. template
  128. <
  129. typename CalculationType,
  130. std::size_t Dimension1,
  131. std::size_t Dimension2
  132. >
  133. class matrix_transformer : public detail::matrix_transformer::matrix_transformer<CalculationType, Dimension1, Dimension2>
  134. {
  135. public:
  136. template<typename Matrix>
  137. inline matrix_transformer(Matrix const& matrix)
  138. {
  139. qvm::assign(this->m_matrix, matrix);
  140. }
  141. inline matrix_transformer() {}
  142. };
  143. template <typename CalculationType>
  144. class matrix_transformer<CalculationType, 2, 2> : public detail::matrix_transformer::matrix_transformer<CalculationType, 2, 2>
  145. {
  146. typedef CalculationType ct;
  147. public :
  148. template<typename Matrix>
  149. inline matrix_transformer(Matrix const& matrix)
  150. {
  151. qvm::assign(this->m_matrix, matrix);
  152. }
  153. inline matrix_transformer() {}
  154. inline matrix_transformer(
  155. ct const& m_0_0, ct const& m_0_1, ct const& m_0_2,
  156. ct const& m_1_0, ct const& m_1_1, ct const& m_1_2,
  157. ct const& m_2_0, ct const& m_2_1, ct const& m_2_2)
  158. {
  159. qvm::A<0,0>(this->m_matrix) = m_0_0; qvm::A<0,1>(this->m_matrix) = m_0_1; qvm::A<0,2>(this->m_matrix) = m_0_2;
  160. qvm::A<1,0>(this->m_matrix) = m_1_0; qvm::A<1,1>(this->m_matrix) = m_1_1; qvm::A<1,2>(this->m_matrix) = m_1_2;
  161. qvm::A<2,0>(this->m_matrix) = m_2_0; qvm::A<2,1>(this->m_matrix) = m_2_1; qvm::A<2,2>(this->m_matrix) = m_2_2;
  162. }
  163. template <typename P1, typename P2>
  164. inline bool apply(P1 const& p1, P2& p2) const
  165. {
  166. assert_dimension_greater_equal<P1, 2>();
  167. assert_dimension_greater_equal<P2, 2>();
  168. ct const& c1 = get<0>(p1);
  169. ct const& c2 = get<1>(p1);
  170. typedef typename geometry::coordinate_type<P2>::type ct2;
  171. set<0>(p2, boost::numeric_cast<ct2>(c1 * qvm::A<0,0>(this->m_matrix) + c2 * qvm::A<0,1>(this->m_matrix) + qvm::A<0,2>(this->m_matrix)));
  172. set<1>(p2, boost::numeric_cast<ct2>(c1 * qvm::A<1,0>(this->m_matrix) + c2 * qvm::A<1,1>(this->m_matrix) + qvm::A<1,2>(this->m_matrix)));
  173. return true;
  174. }
  175. };
  176. // It IS possible to go from 3 to 2 coordinates
  177. template <typename CalculationType>
  178. class matrix_transformer<CalculationType, 3, 2> : public detail::matrix_transformer::matrix_transformer<CalculationType, 3, 2>
  179. {
  180. typedef CalculationType ct;
  181. public :
  182. template<typename Matrix>
  183. inline matrix_transformer(Matrix const& matrix)
  184. {
  185. qvm::assign(this->m_matrix, matrix);
  186. }
  187. inline matrix_transformer() {}
  188. inline matrix_transformer(
  189. ct const& m_0_0, ct const& m_0_1, ct const& m_0_2,
  190. ct const& m_1_0, ct const& m_1_1, ct const& m_1_2,
  191. ct const& m_2_0, ct const& m_2_1, ct const& m_2_2)
  192. {
  193. qvm::A<0,0>(this->m_matrix) = m_0_0; qvm::A<0,1>(this->m_matrix) = m_0_1; qvm::A<0,2>(this->m_matrix) = 0; qvm::A<0,3>(this->m_matrix) = m_0_2;
  194. qvm::A<1,0>(this->m_matrix) = m_1_0; qvm::A<1,1>(this->m_matrix) = m_1_1; qvm::A<1,2>(this->m_matrix) = 0; qvm::A<1,3>(this->m_matrix) = m_1_2;
  195. qvm::A<2,0>(this->m_matrix) = m_2_0; qvm::A<2,1>(this->m_matrix) = m_2_1; qvm::A<2,2>(this->m_matrix) = 0; qvm::A<2,3>(this->m_matrix) = m_2_2;
  196. }
  197. template <typename P1, typename P2>
  198. inline bool apply(P1 const& p1, P2& p2) const
  199. {
  200. assert_dimension_greater_equal<P1, 3>();
  201. assert_dimension_greater_equal<P2, 2>();
  202. ct const& c1 = get<0>(p1);
  203. ct const& c2 = get<1>(p1);
  204. ct const& c3 = get<2>(p1);
  205. typedef typename geometry::coordinate_type<P2>::type ct2;
  206. set<0>(p2, boost::numeric_cast<ct2>(
  207. c1 * qvm::A<0,0>(this->m_matrix) + c2 * qvm::A<0,1>(this->m_matrix) + c3 * qvm::A<0,2>(this->m_matrix) + qvm::A<0,3>(this->m_matrix)));
  208. set<1>(p2, boost::numeric_cast<ct2>(
  209. c1 * qvm::A<1,0>(this->m_matrix) + c2 * qvm::A<1,1>(this->m_matrix) + c3 * qvm::A<1,2>(this->m_matrix) + qvm::A<1,3>(this->m_matrix)));
  210. return true;
  211. }
  212. };
  213. template <typename CalculationType>
  214. class matrix_transformer<CalculationType, 3, 3> : public detail::matrix_transformer::matrix_transformer<CalculationType, 3, 3>
  215. {
  216. typedef CalculationType ct;
  217. public :
  218. template<typename Matrix>
  219. inline matrix_transformer(Matrix const& matrix)
  220. {
  221. qvm::assign(this->m_matrix, matrix);
  222. }
  223. inline matrix_transformer() {}
  224. inline matrix_transformer(
  225. ct const& m_0_0, ct const& m_0_1, ct const& m_0_2, ct const& m_0_3,
  226. ct const& m_1_0, ct const& m_1_1, ct const& m_1_2, ct const& m_1_3,
  227. ct const& m_2_0, ct const& m_2_1, ct const& m_2_2, ct const& m_2_3,
  228. ct const& m_3_0, ct const& m_3_1, ct const& m_3_2, ct const& m_3_3
  229. )
  230. {
  231. qvm::A<0,0>(this->m_matrix) = m_0_0; qvm::A<0,1>(this->m_matrix) = m_0_1; qvm::A<0,2>(this->m_matrix) = m_0_2; qvm::A<0,3>(this->m_matrix) = m_0_3;
  232. qvm::A<1,0>(this->m_matrix) = m_1_0; qvm::A<1,1>(this->m_matrix) = m_1_1; qvm::A<1,2>(this->m_matrix) = m_1_2; qvm::A<1,3>(this->m_matrix) = m_1_3;
  233. qvm::A<2,0>(this->m_matrix) = m_2_0; qvm::A<2,1>(this->m_matrix) = m_2_1; qvm::A<2,2>(this->m_matrix) = m_2_2; qvm::A<2,3>(this->m_matrix) = m_2_3;
  234. qvm::A<3,0>(this->m_matrix) = m_3_0; qvm::A<3,1>(this->m_matrix) = m_3_1; qvm::A<3,2>(this->m_matrix) = m_3_2; qvm::A<3,3>(this->m_matrix) = m_3_3;
  235. }
  236. template <typename P1, typename P2>
  237. inline bool apply(P1 const& p1, P2& p2) const
  238. {
  239. assert_dimension_greater_equal<P1, 3>();
  240. assert_dimension_greater_equal<P2, 3>();
  241. ct const& c1 = get<0>(p1);
  242. ct const& c2 = get<1>(p1);
  243. ct const& c3 = get<2>(p1);
  244. typedef typename geometry::coordinate_type<P2>::type ct2;
  245. set<0>(p2, boost::numeric_cast<ct2>(
  246. c1 * qvm::A<0,0>(this->m_matrix) + c2 * qvm::A<0,1>(this->m_matrix) + c3 * qvm::A<0,2>(this->m_matrix) + qvm::A<0,3>(this->m_matrix)));
  247. set<1>(p2, boost::numeric_cast<ct2>(
  248. c1 * qvm::A<1,0>(this->m_matrix) + c2 * qvm::A<1,1>(this->m_matrix) + c3 * qvm::A<1,2>(this->m_matrix) + qvm::A<1,3>(this->m_matrix)));
  249. set<2>(p2, boost::numeric_cast<ct2>(
  250. c1 * qvm::A<2,0>(this->m_matrix) + c2 * qvm::A<2,1>(this->m_matrix) + c3 * qvm::A<2,2>(this->m_matrix) + qvm::A<2,3>(this->m_matrix)));
  251. return true;
  252. }
  253. };
  254. /*!
  255. \brief Strategy of translate transformation in Cartesian system.
  256. \details Translate moves a geometry a fixed distance in 2 or 3 dimensions.
  257. \see http://en.wikipedia.org/wiki/Translation_%28geometry%29
  258. \ingroup strategies
  259. \tparam Dimension1 number of dimensions to transform from
  260. \tparam Dimension2 number of dimensions to transform to
  261. */
  262. template
  263. <
  264. typename CalculationType,
  265. std::size_t Dimension1,
  266. std::size_t Dimension2
  267. >
  268. class translate_transformer
  269. {
  270. };
  271. template<typename CalculationType>
  272. class translate_transformer<CalculationType, 2, 2> : public matrix_transformer<CalculationType, 2, 2>
  273. {
  274. public :
  275. // To have translate transformers compatible for 2/3 dimensions, the
  276. // constructor takes an optional third argument doing nothing.
  277. inline translate_transformer(CalculationType const& translate_x,
  278. CalculationType const& translate_y,
  279. CalculationType const& = 0)
  280. : matrix_transformer<CalculationType, 2, 2>(
  281. 1, 0, translate_x,
  282. 0, 1, translate_y,
  283. 0, 0, 1)
  284. {}
  285. };
  286. template <typename CalculationType>
  287. class translate_transformer<CalculationType, 3, 3> : public matrix_transformer<CalculationType, 3, 3>
  288. {
  289. public :
  290. inline translate_transformer(CalculationType const& translate_x,
  291. CalculationType const& translate_y,
  292. CalculationType const& translate_z)
  293. : matrix_transformer<CalculationType, 3, 3>(
  294. 1, 0, 0, translate_x,
  295. 0, 1, 0, translate_y,
  296. 0, 0, 1, translate_z,
  297. 0, 0, 0, 1)
  298. {}
  299. };
  300. /*!
  301. \brief Strategy of scale transformation in Cartesian system.
  302. \details Scale scales a geometry up or down in all its dimensions.
  303. \see http://en.wikipedia.org/wiki/Scaling_%28geometry%29
  304. \ingroup strategies
  305. \tparam Dimension1 number of dimensions to transform from
  306. \tparam Dimension2 number of dimensions to transform to
  307. */
  308. template
  309. <
  310. typename CalculationType,
  311. std::size_t Dimension1,
  312. std::size_t Dimension2
  313. >
  314. class scale_transformer
  315. {
  316. };
  317. template
  318. <
  319. typename CalculationType,
  320. std::size_t Dimension1
  321. >
  322. class scale_transformer<CalculationType, Dimension1, Dimension1> : public matrix_transformer<CalculationType, Dimension1, Dimension1>
  323. {
  324. public:
  325. inline scale_transformer(CalculationType const& scale)
  326. {
  327. boost::qvm::set_identity(this->m_matrix);
  328. this->m_matrix*=scale;
  329. qvm::A<Dimension1,Dimension1>(this->m_matrix) = 1;
  330. }
  331. };
  332. template <typename CalculationType>
  333. class scale_transformer<CalculationType, 2, 2> : public matrix_transformer<CalculationType, 2, 2>
  334. {
  335. public :
  336. inline scale_transformer(CalculationType const& scale_x,
  337. CalculationType const& scale_y,
  338. CalculationType const& = 0)
  339. : matrix_transformer<CalculationType, 2, 2>(
  340. scale_x, 0, 0,
  341. 0, scale_y, 0,
  342. 0, 0, 1)
  343. {}
  344. inline scale_transformer(CalculationType const& scale)
  345. : matrix_transformer<CalculationType, 2, 2>(
  346. scale, 0, 0,
  347. 0, scale, 0,
  348. 0, 0, 1)
  349. {}
  350. };
  351. template <typename CalculationType>
  352. class scale_transformer<CalculationType, 3, 3> : public matrix_transformer<CalculationType, 3, 3>
  353. {
  354. public :
  355. inline scale_transformer(CalculationType const& scale_x,
  356. CalculationType const& scale_y,
  357. CalculationType const& scale_z)
  358. : matrix_transformer<CalculationType, 3, 3>(
  359. scale_x, 0, 0, 0,
  360. 0, scale_y, 0, 0,
  361. 0, 0, scale_z, 0,
  362. 0, 0, 0, 1)
  363. {}
  364. inline scale_transformer(CalculationType const& scale)
  365. : matrix_transformer<CalculationType, 3, 3>(
  366. scale, 0, 0, 0,
  367. 0, scale, 0, 0,
  368. 0, 0, scale, 0,
  369. 0, 0, 0, 1)
  370. {}
  371. };
  372. #ifndef DOXYGEN_NO_DETAIL
  373. namespace detail
  374. {
  375. template <typename DegreeOrRadian>
  376. struct as_radian
  377. {};
  378. template <>
  379. struct as_radian<radian>
  380. {
  381. template <typename T>
  382. static inline T get(T const& value)
  383. {
  384. return value;
  385. }
  386. };
  387. template <>
  388. struct as_radian<degree>
  389. {
  390. template <typename T>
  391. static inline T get(T const& value)
  392. {
  393. typedef typename promote_floating_point<T>::type promoted_type;
  394. return value * math::d2r<promoted_type>();
  395. }
  396. };
  397. template
  398. <
  399. typename CalculationType,
  400. std::size_t Dimension1,
  401. std::size_t Dimension2
  402. >
  403. class rad_rotate_transformer
  404. : public transform::matrix_transformer<CalculationType, Dimension1, Dimension2>
  405. {
  406. public :
  407. inline rad_rotate_transformer(CalculationType const& angle)
  408. : transform::matrix_transformer<CalculationType, Dimension1, Dimension2>(
  409. cos(angle), sin(angle), 0,
  410. -sin(angle), cos(angle), 0,
  411. 0, 0, 1)
  412. {}
  413. };
  414. } // namespace detail
  415. #endif // DOXYGEN_NO_DETAIL
  416. /*!
  417. \brief Strategy for rotate transformation in Cartesian coordinate system.
  418. \details Rotate rotates a geometry by a specified angle about a fixed point (e.g. origin).
  419. \see http://en.wikipedia.org/wiki/Rotation_%28mathematics%29
  420. \ingroup strategies
  421. \tparam DegreeOrRadian degree/or/radian, type of rotation angle specification
  422. \note A single angle is needed to specify a rotation in 2D.
  423. Not yet in 3D, the 3D version requires special things to allow
  424. for rotation around X, Y, Z or arbitrary axis.
  425. \todo The 3D version will not compile.
  426. */
  427. template
  428. <
  429. typename DegreeOrRadian,
  430. typename CalculationType,
  431. std::size_t Dimension1,
  432. std::size_t Dimension2
  433. >
  434. class rotate_transformer : public detail::rad_rotate_transformer<CalculationType, Dimension1, Dimension2>
  435. {
  436. public :
  437. inline rotate_transformer(CalculationType const& angle)
  438. : detail::rad_rotate_transformer
  439. <
  440. CalculationType, Dimension1, Dimension2
  441. >(detail::as_radian<DegreeOrRadian>::get(angle))
  442. {}
  443. };
  444. }} // namespace strategy::transform
  445. }} // namespace boost::geometry
  446. #endif // BOOST_GEOMETRY_STRATEGIES_TRANSFORM_MATRIX_TRANSFORMERS_HPP