test_eigen_tf.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <time.h>
00031 #include <tf_conversions/tf_eigen.h>
00032 #include <gtest/gtest.h>
00033 
00034 using namespace tf;
00035 
00036 double gen_rand(double min, double max)
00037 {
00038   int rand_num = rand()%100+1;
00039   double result = min + (double)((max-min)*rand_num)/101.0;
00040   return result;
00041 }
00042 
00043 TEST(TFEigenConversions, tf_eigen_vector)
00044 {
00045   tf::Vector3 t;
00046   t[0] = gen_rand(-10,10);
00047   t[1] = gen_rand(-10,10);
00048   t[2] = gen_rand(-10,10);
00049 
00050   Eigen::Vector3d k;
00051   VectorTFToEigen(t,k);
00052 
00053   ASSERT_NEAR(t[0],k[0],1e-6);
00054   ASSERT_NEAR(t[1],k[1],1e-6);
00055   ASSERT_NEAR(t[2],k[2],1e-6);
00056 }
00057 
00058 TEST(TFEigenConversions, tf_eigen_quaternion)
00059 {
00060   tf::Quaternion t;
00061   t[0] = gen_rand(-1.0,1.0);
00062   t[1] = gen_rand(-1.0,1.0);
00063   t[2] = gen_rand(-1.0,1.0);
00064   t[3] = gen_rand(-1.0,1.0);
00065   t.normalize();
00066   Eigen::Quaterniond k;
00067   RotationTFToEigen(t,k);
00068 
00069   ASSERT_NEAR(t[0],k.coeffs()(0),1e-6);
00070   ASSERT_NEAR(t[1],k.coeffs()(1),1e-6);
00071   ASSERT_NEAR(t[2],k.coeffs()(2),1e-6);
00072   ASSERT_NEAR(t[3],k.coeffs()(3),1e-6);
00073   ASSERT_NEAR(k.norm(),1.0,1e-10);
00074 }
00075 
00076 TEST(TFEigenConversions, tf_eigen_transform)
00077 {
00078   tf::Transform t;
00079   tf::Quaternion tq;
00080   tq[0] = gen_rand(-1.0,1.0);
00081   tq[1] = gen_rand(-1.0,1.0);
00082   tq[2] = gen_rand(-1.0,1.0);
00083   tq[3] = gen_rand(-1.0,1.0);
00084   tq.normalize();
00085   t.setOrigin(tf::Vector3(gen_rand(-10,10),gen_rand(-10,10),gen_rand(-10,10)));
00086   t.setRotation(tq);
00087 
00088   Eigen::Affine3d k;
00089   TransformTFToEigen(t,k);
00090 
00091   for(int i=0; i < 3; i++)
00092   {
00093     ASSERT_NEAR(t.getOrigin()[i],k.matrix()(i,3),1e-6);
00094     for(int j=0; j < 3; j++)
00095     {      
00096       ASSERT_NEAR(t.getBasis()[i][j],k.matrix()(i,j),1e-6);
00097     }
00098   }
00099   for (int col = 0 ; col < 3; col ++)
00100     ASSERT_NEAR(k.matrix()(3, col), 0, 1e-6);
00101   ASSERT_NEAR(k.matrix()(3,3), 1, 1e-6);
00102   
00103 }
00104 
00105 TEST(TFEigenConversions, eigen_tf_transform)
00106 {
00107   tf::Transform t;
00108   Eigen::Affine3d k;
00109   Eigen::Quaterniond kq;
00110   kq.coeffs()(0) = gen_rand(-1.0,1.0);
00111   kq.coeffs()(1) = gen_rand(-1.0,1.0);
00112   kq.coeffs()(2) = gen_rand(-1.0,1.0);
00113   kq.coeffs()(3) = gen_rand(-1.0,1.0);
00114   kq.normalize();
00115   k.translate(Eigen::Vector3d(gen_rand(-10,10),gen_rand(-10,10),gen_rand(-10,10)));
00116   k.rotate(kq);
00117 
00118   TransformEigenToTF(k,t);
00119   for(int i=0; i < 3; i++)
00120   {
00121     ASSERT_NEAR(t.getOrigin()[i],k.matrix()(i,3),1e-6);
00122     for(int j=0; j < 3; j++)
00123     {      
00124       ASSERT_NEAR(t.getBasis()[i][j],k.matrix()(i,j),1e-6);
00125     }
00126   }
00127 }
00128 
00129 
00130 int main(int argc, char **argv){
00131 /* initialize random seed: */
00132   srand ( time(NULL) );
00133   testing::InitGoogleTest(&argc, argv);
00134   return RUN_ALL_TESTS();
00135 }


tf_conversions
Author(s): Tully Foote
autogenerated on Mon Oct 6 2014 00:18:24