Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84614 - in trunk/boost/geometry/extensions/algebra: . algorithms
From: adam.wulkiewicz_at_[hidden]
Date: 2013-06-02 17:16:59


Author: awulkiew
Date: 2013-06-02 17:16:59 EDT (Sun, 02 Jun 2013)
New Revision: 84614
URL: http://svn.boost.org/trac/boost/changeset/84614

Log:
geometry extensions: added convert(), removed assign_zero() and added assign_identity() for rotations.
Added:
   trunk/boost/geometry/extensions/algebra/algorithms/convert.hpp (contents, props changed)
Text files modified:
   trunk/boost/geometry/extensions/algebra/algebra.hpp | 1
   trunk/boost/geometry/extensions/algebra/algorithms/assign.hpp | 51 +++++++++++++++++++++++++++++----------
   trunk/boost/geometry/extensions/algebra/algorithms/detail.hpp | 42 ++++++++++++++++++++++++++++++++
   3 files changed, 81 insertions(+), 13 deletions(-)

Modified: trunk/boost/geometry/extensions/algebra/algebra.hpp
==============================================================================
--- trunk/boost/geometry/extensions/algebra/algebra.hpp (original)
+++ trunk/boost/geometry/extensions/algebra/algebra.hpp 2013-06-02 17:16:59 EDT (Sun, 02 Jun 2013)
@@ -31,6 +31,7 @@
 #include <boost/geometry/extensions/algebra/geometries/rotation_matrix.hpp>
 
 #include <boost/geometry/extensions/algebra/algorithms/assign.hpp>
+#include <boost/geometry/extensions/algebra/algorithms/convert.hpp>
 
 #include <boost/geometry/extensions/algebra/algorithms/translation.hpp>
 #include <boost/geometry/extensions/algebra/algorithms/rotation.hpp>

Modified: trunk/boost/geometry/extensions/algebra/algorithms/assign.hpp
==============================================================================
--- trunk/boost/geometry/extensions/algebra/algorithms/assign.hpp (original)
+++ trunk/boost/geometry/extensions/algebra/algorithms/assign.hpp 2013-06-02 17:16:59 EDT (Sun, 02 Jun 2013)
@@ -25,8 +25,7 @@
 {
 
 #ifndef DOXYGEN_NO_DISPATCH
-namespace dispatch
-{
+namespace dispatch {
 
 template <typename Vector>
 struct assign_zero<vector_tag, Vector>
@@ -41,30 +40,56 @@
     }
 };
 
+template <typename GeometryTag, typename Geometry>
+struct assign_identity
+{
+ BOOST_MPL_ASSERT_MSG(false, NOT_IMPLEMENTED_FOR_THIS_GEOMETRY, (GeometryTag, Geometry));
+};
+
 template <typename R>
-struct assign_zero<rotation_quaternion_tag, R>
+struct assign_identity<rotation_quaternion_tag, R>
 {
     static inline void apply(R & g)
     {
- detail::algebra::assign_value<
- R, typename coordinate_type<R>::type,
- 0, 4
- >::apply(g, 0);
+ set<0>(g, 1); set<1>(g, 0); set<2>(g, 0); set<3>(g, 0);
     }
 };
 
 template <typename R>
-struct assign_zero<rotation_matrix_tag, R>
+struct assign_identity<rotation_matrix_tag, R>
 {
- static inline void apply(R & g)
+ static inline void apply(R & r)
     {
- detail::algebra::indexed_assign_value<
- R, typename coordinate_type<R>::type,
- 0, 0, dimension<R>::type::value, dimension<R>::type::value
- >::apply(g, 0);
+ detail::algebra::identity_matrix<
+ R, 0, 0, dimension<R>::value, dimension<R>::value
+ >::apply(r);
     }
 };
 
+} // namespace dispatch
+#endif // DOXYGEN_NO_DISPATCH
+
+/*!
+\brief assign identity to Transformation
+\ingroup assign
+\details The assign_identity function initializes a rotation or transformation with values indicating no rotation
+\tparam Rotation The rotation type.
+\param rotation The rotation.
+ */
+template <typename Rotation>
+inline void assign_identity(Rotation & rotation)
+{
+ concept::check<Rotation>();
+
+ dispatch::assign_identity<
+ typename tag<Rotation>::type,
+ Rotation
+ >::apply(rotation);
+}
+
+#ifndef DOXYGEN_NO_DISPATCH
+namespace dispatch {
+
 template <typename V>
 struct assign<vector_tag, V, 2>
 {

Added: trunk/boost/geometry/extensions/algebra/algorithms/convert.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/geometry/extensions/algebra/algorithms/convert.hpp 2013-06-02 17:16:59 EDT (Sun, 02 Jun 2013)
@@ -0,0 +1,85 @@
+// Boost.Geometry (aka GGL, Generic Geometry Library)
+
+// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
+// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
+// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
+// Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
+
+// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
+// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
+
+// Use, modification and distribution is subject to the Boost Software License,
+// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_GEOMETRY_EXTENSIONS_ALGEBRA_ALGORITHMS_CONVERT_HPP
+#define BOOST_GEOMETRY_EXTENSIONS_ALGEBRA_ALGORITHMS_CONVERT_HPP
+
+#include <boost/geometry/algorithms/convert.hpp>
+
+#include <boost/geometry/extensions/algebra/core/tags.hpp>
+
+#include <boost/geometry/extensions/algebra/algorithms/detail.hpp>
+
+namespace boost { namespace geometry
+{
+
+#ifndef DOXYGEN_NO_DISPATCH
+namespace dispatch
+{
+
+template <typename RQuaternion, typename RMatrix>
+struct convert<RQuaternion, RMatrix, rotation_quaternion_tag, rotation_matrix_tag, 3, false>
+{
+ static inline void apply(RQuaternion const& q, RMatrix& m)
+ {
+ typedef typename coordinate_type<RQuaternion>::type T;
+
+ // quaternion should be normalized
+
+ T xx2 = get<1>(q) * get<1>(q) * 2;
+ T yy2 = get<2>(q) * get<2>(q) * 2;
+ T zz2 = get<3>(q) * get<3>(q) * 2;
+ T xy2 = get<1>(q) * get<2>(q) * 2;
+ T yz2 = get<2>(q) * get<3>(q) * 2;
+ T xz2 = get<1>(q) * get<3>(q) * 2;
+ T wx2 = get<0>(q) * get<1>(q) * 2;
+ T wy2 = get<0>(q) * get<2>(q) * 2;
+ T wz2 = get<0>(q) * get<3>(q) * 2;
+
+ // WARNING!
+ // Quaternion (0, 0, 0, 0) is converted to identity matrix!
+
+ set<0, 0>(m, 1-yy2-zz2); set<0, 1>(m, xy2-wz2); set<0, 2>(m, xz2+wy2);
+ set<1, 0>(m, xy2+wz2); set<1, 1>(m, 1-xx2-zz2); set<1, 2>(m, yz2-wx2);
+ set<2, 0>(m, xz2-wy2); set<2, 1>(m, yz2+wx2); set<2, 2>(m, 1-xx2-yy2);
+ }
+};
+
+template <typename RMatrix, typename RQuaternion>
+struct convert<RMatrix, RQuaternion, rotation_matrix_tag, rotation_quaternion_tag, 3, false>
+{
+ static inline void apply(RMatrix const& m, RQuaternion & q)
+ {
+ typedef typename coordinate_type<RMatrix>::type T;
+
+ // WARNING!
+ // Zero matrix is converted to quaternion(0.5, 0, 0, 0)!
+
+ T w = ::sqrt(1 + get<0, 0>(m) + get<1, 1>(m) + get<2, 2>(m)) / 2;
+ T iw4 = 0.25 / w;
+ set<0>(q, w);
+ set<1>(q, (get<2, 1>(m) - get<1, 2>(m)) * iw4);
+ set<2>(q, (get<0, 2>(m) - get<2, 0>(m)) * iw4);
+ set<3>(q, (get<1, 0>(m) - get<0, 1>(m)) * iw4);
+ }
+};
+
+
+} // namespace dispatch
+#endif // DOXYGEN_NO_DISPATCH
+
+
+}} // namespace boost::geometry
+
+#endif // BOOST_GEOMETRY_EXTENSIONS_ALGEBRA_ALGORITHMS_ASSIGN_HPP

Modified: trunk/boost/geometry/extensions/algebra/algorithms/detail.hpp
==============================================================================
--- trunk/boost/geometry/extensions/algebra/algorithms/detail.hpp (original)
+++ trunk/boost/geometry/extensions/algebra/algorithms/detail.hpp 2013-06-02 17:16:59 EDT (Sun, 02 Jun 2013)
@@ -228,6 +228,48 @@
     static inline void apply(G &, V const&) {}
 };
 
+template <typename G, std::size_t BI, std::size_t BD, std::size_t EI, std::size_t ED>
+struct identity_matrix_per_index
+{
+ static inline void apply(G & g)
+ {
+ set<BI, BD>(g, 0);
+ identity_matrix_per_index<G, BI, BD+1, EI, ED>::apply(g);
+ }
+};
+
+template <typename G, std::size_t BI, std::size_t EI, std::size_t ED>
+struct identity_matrix_per_index<G, BI, BI, EI, ED>
+{
+ static inline void apply(G & g)
+ {
+ set<BI, BI>(g, 1);
+ identity_matrix_per_index<G, BI, BI+1, EI, ED>::apply(g);
+ }
+};
+
+template <typename G, std::size_t BI, std::size_t EI, std::size_t ED>
+struct identity_matrix_per_index<G, BI, ED, EI, ED>
+{
+ static inline void apply(G &) {}
+};
+
+template <typename G, std::size_t BI, std::size_t BD, std::size_t EI, std::size_t ED>
+struct identity_matrix
+{
+ static inline void apply(G & g)
+ {
+ identity_matrix_per_index<G, BI, BD, EI, ED>::apply(g);
+ identity_matrix<G, BI+1, BD, EI, ED>::apply(g);
+ }
+};
+
+template <typename G, std::size_t BD, std::size_t EI, std::size_t ED>
+struct identity_matrix<G, EI, BD, EI, ED>
+{
+ static inline void apply(G &) {}
+};
+
 }} // namespace detail::algebra
 #endif // DOXYGEN_NO_DETAIL
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk