Point Cloud Library (PCL)  1.3.1
eigen.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR a PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  */
00035 
00036 #include <pcl/pcl_macros.h>
00037 #include <pcl/win32_macros.h>
00038 
00039 void pcl::getTransFromUnitVectorsZY(const Eigen::Vector3f& z_axis, const Eigen::Vector3f& y_direction, Eigen::Affine3f& transformation)
00040 {
00041   Eigen::Vector3f tmp0 = (y_direction.cross(z_axis)).normalized();
00042   Eigen::Vector3f tmp1 = (z_axis.cross(tmp0)).normalized();
00043   Eigen::Vector3f tmp2 = z_axis.normalized();
00044   
00045   transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
00046   transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
00047   transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
00048   transformation(3,0)=0.0f;    transformation(3,1)=0.0f;    transformation(3,2)=0.0f;    transformation(3,3)=1.0f;
00049 }
00050 
00051 Eigen::Affine3f pcl::getTransFromUnitVectorsZY(const Eigen::Vector3f& z_axis, const Eigen::Vector3f& y_direction)
00052 {
00053   Eigen::Affine3f transformation;
00054   getTransFromUnitVectorsZY(z_axis, y_direction, transformation);
00055   return transformation;
00056 }
00057 
00058 void pcl::getTransFromUnitVectorsXY(const Eigen::Vector3f& x_axis, const Eigen::Vector3f& y_direction, Eigen::Affine3f& transformation)
00059 {
00060   Eigen::Vector3f tmp2 = (x_axis.cross(y_direction)).normalized();
00061   Eigen::Vector3f tmp1 = (tmp2.cross(x_axis)).normalized();
00062   Eigen::Vector3f tmp0 = x_axis.normalized();
00063   
00064   transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
00065   transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
00066   transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
00067   transformation(3,0)=0.0f;    transformation(3,1)=0.0f;    transformation(3,2)=0.0f;    transformation(3,3)=1.0f;
00068 }
00069 
00070 Eigen::Affine3f pcl::getTransFromUnitVectorsXY(const Eigen::Vector3f& x_axis, const Eigen::Vector3f& y_direction)
00071 {
00072   Eigen::Affine3f transformation;
00073   getTransFromUnitVectorsXY(x_axis, y_direction, transformation);
00074   return transformation;
00075 }
00076 
00077 void pcl::getTransformationFromTwoUnitVectors(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis, Eigen::Affine3f& transformation)
00078 {
00079   getTransFromUnitVectorsZY(z_axis, y_direction, transformation);
00080 }
00081 
00082 Eigen::Affine3f pcl::getTransformationFromTwoUnitVectors(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis)
00083 {
00084   Eigen::Affine3f transformation;
00085   getTransformationFromTwoUnitVectors(y_direction, z_axis, transformation);
00086   return transformation;
00087 }
00088 
00089 void pcl::getTransformationFromTwoUnitVectorsAndOrigin(const Eigen::Vector3f& y_direction, const Eigen::Vector3f& z_axis,
00090                                                   const Eigen::Vector3f& origin, Eigen::Affine3f& transformation)
00091 {
00092   getTransformationFromTwoUnitVectors(y_direction, z_axis, transformation);
00093   Eigen::Vector3f translation = transformation*origin;
00094   transformation(0,3)=-translation[0];  transformation(1,3)=-translation[1];  transformation(2,3)=-translation[2];
00095 }
00096 
00097 void pcl::getTranslationAndEulerAngles(const Eigen::Affine3f& t, float& x, float& y, float& z, float& roll, float& pitch, float& yaw)
00098 {
00099   x = t(0,3);
00100   y = t(1,3);
00101   z = t(2,3);
00102   roll  = atan2f(t(2,1), t(2,2));
00103   pitch = asinf(-t(2,0));
00104   yaw   = atan2f(t(1,0), t(0,0));
00105 }
00106 
00107 void pcl::getTransformation(float x, float y, float z, float roll, float pitch, float yaw, Eigen::Affine3f& t)
00108 {
00109   float A=cosf(yaw),  B=sinf(yaw),  C=cosf(pitch), D=sinf(pitch),
00110         E=cosf(roll), F=sinf(roll), DE=D*E,        DF=D*F;
00111   t(0,0) = A*C;  t(0,1) = A*DF - B*E;  t(0,2) = B*F + A*DE;  t(0,3) = x;
00112   t(1,0) = B*C;  t(1,1) = A*E + B*DF;  t(1,2) = B*DE - A*F;  t(1,3) = y;
00113   t(2,0) = -D;   t(2,1) = C*F;         t(2,2) = C*E;         t(2,3) = z;
00114   t(3,0) = 0;    t(3,1) = 0;           t(3,2) = 0;           t(3,3) = 1;
00115 }
00116 
00117 Eigen::Affine3f pcl::getTransformation(float x, float y, float z, float roll, float pitch, float yaw)
00118 {
00119   Eigen::Affine3f t;
00120   getTransformation(x, y, z, roll, pitch, yaw, t);
00121   return t;
00122 }
00123 
00124 template <typename Derived>
00125 void pcl::saveBinary(const Eigen::MatrixBase<Derived>& matrix, std::ostream& file)
00126 {
00127   uint32_t rows=matrix.rows(), cols=matrix.cols();
00128   file.write((char*) &rows, sizeof(rows));
00129   file.write((char*) &cols, sizeof(cols));
00130   for (uint32_t i=0; i<rows; ++i)
00131     for (uint32_t j=0; j<cols; ++j)
00132     {
00133       typename Derived::Scalar tmp = matrix(i,j);
00134       file.write((char*) &tmp, sizeof(tmp));
00135     }
00136 }
00137 
00138 template <typename Derived>
00139 void pcl::loadBinary(Eigen::MatrixBase<Derived>& matrix, std::istream& file)
00140 {
00141   uint32_t rows, cols;
00142   file.read((char*) &rows, sizeof(rows));
00143   file.read((char*) &cols, sizeof(cols));
00144   //std::cout << rows <<" rows and "<<cols<<" cols.\n";
00145   if (matrix.rows()!=(int)rows || matrix.cols()!=(int)cols)
00146   {
00147     //std::cerr << __PRETTY_FUNCTION__ << ": matrix size does not fit!\n";
00148     matrix.resize(rows, cols);
00149   }
00150   
00151   for (uint32_t i=0; i<rows; ++i)
00152     for (uint32_t j=0; j<cols; ++j)
00153     {
00154       typename Derived::Scalar tmp;
00155       file.read((char*) &tmp, sizeof(tmp));
00156       matrix(i,j) = tmp;
00157     }
00158 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines