transformationfile_manager_data.cpp
Go to the documentation of this file.
00001 
00018 #include "Transformation/transformationfile_manager_data.h"
00019 #include <iostream>
00020 #include <fstream>
00021 #include <string>
00022 #ifndef Q_MOC_RUN
00023 #include <ros/ros.h>
00024 #include <boost/algorithm/string.hpp>
00025 #endif
00026 
00027 using namespace std;
00028 
00029 TransformationFile_Manager_Data::TransformationFile_Manager_Data (string filePath) : Abstract_TransformationFile_Manager(filePath) {}
00030 
00031 
00032 bool TransformationFile_Manager_Data::writeToFile(Transformation_Data data)
00033 {
00034     ofstream myfile;
00035     myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
00036 
00037     Eigen::Affine3d tr(data.PTU_Frame);
00038     Eigen::Matrix3d rotation = tr.rotation();
00039 
00040     //Eigen::Vector3d angles = rotation.eulerAngles(2, 0, 2);
00041     Eigen::Vector3d translation(Eigen::Vector3d(data.PTU_Frame(0, 3), data.PTU_Frame(1, 3), data.PTU_Frame(2, 3)));
00042     //myfile << angles[0] << "; " << angles[1] << "; " << angles[2] << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << "; ";
00043 
00044     Eigen::Quaterniond quaternion(rotation);
00045 
00046     myfile << quaternion.x() << "; " << quaternion.y() << "; " <<  quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2]<< "; ";
00047 
00048 
00049     /*transform = data.PTU_Frame;
00050     roll  = atan2f(transform(2,0), transform(0,0));
00051     pitch = asinf(transform(1,0));
00052     yaw   = atan2f(-transform(1,2), transform(1,1));
00053     translation = Eigen::Vector3d(transform(3, 0), transform(3, 1), transform(3, 2));
00054     myfile << roll << " " << pitch << " " << yaw << " " << translation[0] << " " << translation[1] << " " << translation[2] << " ";
00055 */
00056     myfile << data.pan << "; " << data.tilt << std::endl;
00057 
00058     myfile.close();
00059 
00060     return true;
00061 }
00062 
00063 std::vector<Transformation_Data> TransformationFile_Manager_Data::readFromFile(string filePath)
00064 {
00065     std::vector<Transformation_Data> output;
00066     std::ifstream input(filePath);
00067     std::string line;
00068     unsigned int lineNumber = 1;
00069     while(std::getline( input, line ))
00070     {
00071         std::vector<std::string> strs;
00072         boost::split(strs, line, boost::is_any_of(";"));
00073         if (strs.size() == 9)
00074         {
00075             std::vector<double> numericValues;
00076             bool success = true;
00077             for (unsigned int i = 0; i < strs.size(); i++)
00078             {
00079                 try
00080                 {
00081                     std::string temp = strs.at(i);
00082                     boost::algorithm::trim(temp);
00083                     numericValues.push_back(boost::lexical_cast<double>(temp));
00084                 }
00085                 catch (boost::bad_lexical_cast const&)
00086                 {
00087                     ROS_ERROR_STREAM("Line " << lineNumber << ": " <<strs.at(i) << " is not a number.");
00088                     success = false;
00089                     break;
00090                 }
00091             }
00092             if (success)
00093             {
00094                 Eigen::Affine3d rotation(Eigen::Quaterniond(numericValues.at(3),numericValues.at(0),numericValues.at(1),numericValues.at(2)));
00095                 /*Eigen::Affine3d rotation1(Eigen::AngleAxisd(numericValues.at(0), Eigen::Vector3d(0.0,0.0,1.0)));
00096                 Eigen::Affine3d rotation2(Eigen::AngleAxisd(numericValues.at(1), Eigen::Vector3d(1.0,0.0,0.0)));
00097                 Eigen::Affine3d rotation3(Eigen::AngleAxisd(numericValues.at(2), Eigen::Vector3d(0.0,0.0,1.0)));*/
00098                 Eigen::Affine3d translation(Eigen::Translation3d(Eigen::Vector3d(numericValues.at(4),numericValues.at(5),numericValues.at(6))));
00099                 Eigen::Affine3d transformation = translation * rotation;
00100                 Transformation_Data data;
00101                 data.PTU_Frame = transformation.matrix();
00102                 data.pan = numericValues.at(7);
00103                 data.tilt = numericValues.at(8);
00104                 output.push_back(data);
00105             }
00106         }
00107         else
00108         {
00109             ROS_ERROR_STREAM("Line " << lineNumber << ": Expected 8 items, got " << strs.size());
00110         }
00111         lineNumber ++;
00112     }
00113 
00114     return output;
00115 }
00116 
00117 std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> TransformationFile_Manager_Data::readTransformationFromFile(string filePath)
00118 {
00119     std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> output;
00120     std::ifstream input(filePath);
00121     std::string line;
00122     unsigned int lineNumber = 1;
00123     while(std::getline( input, line ))
00124     {
00125         std::vector<std::string> strs;
00126         boost::split(strs, line, boost::is_any_of(";"));
00127         if (strs.size() == 7)
00128         {
00129             std::vector<double> numericValues;
00130             bool success = true;
00131             for (unsigned int i = 0; i < strs.size(); i++)
00132             {
00133                 try
00134                 {
00135                     std::string temp = strs.at(i);
00136                     boost::algorithm::trim(temp);
00137                     numericValues.push_back(boost::lexical_cast<double>(temp));
00138                 }
00139                 catch (boost::bad_lexical_cast const&)
00140                 {
00141                     ROS_ERROR_STREAM("Line " << lineNumber << ": " <<strs.at(i) << " is not a number.");
00142                     success = false;
00143                     break;
00144                 }
00145             }
00146             if (success)
00147             {
00148                 Eigen::Affine3d rotation(Eigen::Quaterniond(numericValues.at(3),numericValues.at(0),numericValues.at(1),numericValues.at(2)));
00149                 /*Eigen::Affine3d rotation1(Eigen::AngleAxisd(numericValues.at(0), Eigen::Vector3d(0.0,0.0,1.0)));
00150                 Eigen::Affine3d rotation2(Eigen::AngleAxisd(numericValues.at(1), Eigen::Vector3d(1.0,0.0,0.0)));
00151                 Eigen::Affine3d rotation3(Eigen::AngleAxisd(numericValues.at(2), Eigen::Vector3d(0.0,0.0,1.0)));*/
00152                 Eigen::Affine3d translation(Eigen::Translation3d(Eigen::Vector3d(numericValues.at(4),numericValues.at(5),numericValues.at(6))));
00153                 Eigen::Affine3d transformation = rotation * translation;
00154                 Eigen::Matrix4d data;
00155                 data = transformation.matrix();
00156                 output.push_back(data);
00157             }
00158         }
00159         else
00160         {
00161             ROS_ERROR_STREAM("Line " << lineNumber << ": Expected 7 items, got " << strs.size());
00162         }
00163         lineNumber ++;
00164     }
00165 
00166     return output;
00167 }
00168 
00169 bool TransformationFile_Manager_Data::writeTransformationToFile(const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> transformation)
00170 {
00171     ofstream myfile;
00172     myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
00173     for (unsigned int i = 0; i < transformation.size(); i++)
00174     {
00175 
00176         Eigen::Matrix4d temp = transformation.at(i);
00177         Eigen::Affine3d tr(temp);
00178         Eigen::Matrix3d rotation = tr.rotation();
00179         Eigen::Quaterniond quaternion(rotation);
00180         Eigen::Vector3d translation(Eigen::Vector3d(temp(0, 3), temp(1, 3), temp(2, 3)));
00181 
00182         myfile << quaternion.x() << "; " << quaternion.y() << "; " <<  quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << std::endl;
00183 
00190     }
00191     myfile.close();
00192 
00193 
00194     return true;
00195 }
00196 
00197 bool TransformationFile_Manager_Data::writeCameraOffsetToFile(const PanTiltOffsetTupleList &offsetData)
00198 {
00199     ofstream myfile;
00200     myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
00201     for (unsigned int i = 0; i < offsetData.size(); i++)
00202     {
00203         PanTiltOffsetTuple currentTuple = offsetData.at(i);
00204         double pan = std::get<0>(currentTuple);
00205         double tilt = std::get<1>(currentTuple);
00206         Eigen::Matrix4d temp = std::get<2>(currentTuple);
00207         Eigen::Matrix4d tempOld = std::get<3>(currentTuple);
00208 
00209         myfile << pan << "; " << tilt << "; " << temp(0,3) << "; " << temp(1,3) << "; " << temp(2,3) << "; "<< tempOld(0,3) << "; " << tempOld(1,3) << "; " << tempOld(2,3) << "; " << std::endl;
00210         //myfile << quaternion.x() << "; " << quaternion.y() << "; " <<  quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << std::endl;
00211 
00218     }
00219     myfile.close();
00220 
00221     return true;
00222 }


asr_mild_calibration_tool
Author(s): Aumann Florian, Heller Florian, Meißner Pascal
autogenerated on Thu Jun 6 2019 21:22:44