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   quaternionTFToEigen(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 affine;
00089   Eigen::Isometry3d isometry;
00090   transformTFToEigen(t, affine);
00091   transformTFToEigen(t, isometry);
00092 
00093   for(int i=0; i < 3; i++)
00094   {
00095     ASSERT_NEAR(t.getOrigin()[i],affine.matrix()(i,3),1e-6);
00096     ASSERT_NEAR(t.getOrigin()[i],isometry.matrix()(i,3),1e-6);
00097     for(int j=0; j < 3; j++)
00098     {
00099       ASSERT_NEAR(t.getBasis()[i][j],affine.matrix()(i,j),1e-6);
00100       ASSERT_NEAR(t.getBasis()[i][j],isometry.matrix()(i,j),1e-6);
00101     }
00102   }
00103   for (int col = 0 ; col < 3; col ++)
00104   {
00105     ASSERT_NEAR(affine.matrix()(3, col), 0, 1e-6);
00106     ASSERT_NEAR(isometry.matrix()(3, col), 0, 1e-6);
00107   }
00108   ASSERT_NEAR(affine.matrix()(3,3), 1, 1e-6);
00109   ASSERT_NEAR(isometry.matrix()(3,3), 1, 1e-6);
00110 }
00111 
00112 TEST(TFEigenConversions, eigen_tf_transform)
00113 {
00114   tf::Transform t1;
00115   tf::Transform t2;
00116   Eigen::Affine3d affine;
00117   Eigen::Isometry3d isometry;
00118   Eigen::Quaterniond kq;
00119   kq.coeffs()(0) = gen_rand(-1.0,1.0);
00120   kq.coeffs()(1) = gen_rand(-1.0,1.0);
00121   kq.coeffs()(2) = gen_rand(-1.0,1.0);
00122   kq.coeffs()(3) = gen_rand(-1.0,1.0);
00123   kq.normalize();
00124   isometry.translate(Eigen::Vector3d(gen_rand(-10,10),gen_rand(-10,10),gen_rand(-10,10)));
00125   isometry.rotate(kq);
00126   affine = isometry;
00127 
00128   transformEigenToTF(affine,t1);
00129   transformEigenToTF(isometry,t2);
00130   for(int i=0; i < 3; i++)
00131   {
00132     ASSERT_NEAR(t1.getOrigin()[i],affine.matrix()(i,3),1e-6);
00133     ASSERT_NEAR(t2.getOrigin()[i],isometry.matrix()(i,3),1e-6);
00134     for(int j=0; j < 3; j++)
00135     {
00136       ASSERT_NEAR(t1.getBasis()[i][j],affine.matrix()(i,j),1e-6);
00137       ASSERT_NEAR(t2.getBasis()[i][j],isometry.matrix()(i,j),1e-6);
00138     }
00139   }
00140 }
00141 
00142 
00143 int main(int argc, char **argv){
00144 /* initialize random seed: */
00145   srand ( time(NULL) );
00146   testing::InitGoogleTest(&argc, argv);
00147   return RUN_ALL_TESTS();
00148 }


tf_conversions
Author(s): Tully Foote
autogenerated on Thu Jun 6 2019 18:45:44