eigen_utils.h
Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2010, Stephan Weiss, ASL, ETH Zurich, Switzerland
00004 You can contact the author at <stephan dot weiss at ieee dot org>
00005 
00006 All rights reserved.
00007 
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010 * Redistributions of source code must retain the above copyright
00011 notice, this list of conditions and the following disclaimer.
00012 * Redistributions in binary form must reproduce the above copyright
00013 notice, this list of conditions and the following disclaimer in the
00014 documentation and/or other materials provided with the distribution.
00015 * Neither the name of ETHZ-ASL nor the
00016 names of its contributors may be used to endorse or promote products
00017 derived from this software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL ETHZ-ASL BE LIABLE FOR ANY
00023 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 */
00031 
00032 #ifndef EIGEN_UTILS_H_
00033 #define EIGEN_UTILS_H_
00034 
00035 #include <Eigen/Dense>
00036 #include <Eigen/Geometry>
00037 
00039 template<class Derived>
00040   inline Eigen::Matrix<typename Derived::Scalar, 3, 3> skew(const Eigen::MatrixBase<Derived> & vec)
00041   {
00042     EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
00043     return (Eigen::Matrix<typename Derived::Scalar, 3, 3>() << 0.0, -vec[2], vec[1], vec[2], 0.0, -vec[0], -vec[1], vec[0], 0.0).finished();
00044   }
00045 
00047 
00052 template<class Derived>
00053   inline Eigen::Matrix<typename Derived::Scalar, 4, 4> omegaMatJPL(const Eigen::MatrixBase<Derived> & vec)
00054   {
00055     EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
00056     return (
00057         Eigen::Matrix<typename Derived::Scalar, 4, 4>() <<
00058         0, vec[2], -vec[1], vec[0],
00059         -vec[2], 0, vec[0], vec[1],
00060         vec[1], -vec[0], 0, vec[2],
00061         -vec[0], -vec[1], -vec[2], 0
00062         ).finished();
00063   }
00064 
00066 
00071 template<class Derived>
00072   inline Eigen::Matrix<typename Derived::Scalar, 4, 4> omegaMatHamilton(const Eigen::MatrixBase<Derived> & vec)
00073   {
00074     EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
00075     return (
00076         Eigen::Matrix<typename Derived::Scalar, 4, 4>() <<
00077         0, -vec[2], vec[1], vec[0],
00078         vec[2], 0, -vec[0], vec[1],
00079         -vec[1], vec[0], 0, vec[2],
00080         -vec[0], -vec[1], -vec[2], 0
00081         ).finished();
00082   }
00083 
00085 template<class Derived>
00086   Eigen::Quaternion<typename Derived::Scalar> quaternionFromSmallAngle(const Eigen::MatrixBase<Derived> & theta)
00087   {
00088   typedef typename Derived::Scalar Scalar;
00089   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived);
00090   EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
00091   const Scalar q_squared = theta.squaredNorm() / 4.0;
00092 
00093     if ( q_squared < 1)
00094     {
00095       return Eigen::Quaternion<Scalar>(sqrt(1 - q_squared), theta[0] * 0.5, theta[1] * 0.5, theta[2] * 0.5);
00096     }
00097     else
00098     {
00099       const Scalar w = 1.0 / sqrt(1 + q_squared);
00100       const Scalar f = w*0.5;
00101       return Eigen::Quaternion<Scalar>(w, theta[0] * f, theta[1] * f, theta[2] * f);
00102     }
00103   }
00104 
00106 template<class T>
00107   bool checkForNumeric(const T & vec, int size, const std::string & info)
00108   {
00109     for (int i = 0; i < size; i++)
00110     {
00111       if (isnan(vec[i]))
00112       {
00113         std::cerr << "=== ERROR ===  " << info << ": NAN at index " << i << std::endl;
00114         return false;
00115       }
00116       if (isinf(vec[i]))
00117       {
00118         std::cerr << "=== ERROR ===  " << info << ": INF at index " << i << std::endl;
00119         return false;
00120       }
00121     }
00122     return true;
00123   }
00124 
00125 #endif /* EIGEN_UTILS_H_ */


ssf_core
Author(s): Stephan Weiss, Markus Achtelik
autogenerated on Mon Oct 6 2014 10:27:03