transformationfile_manager_data.cpp
Go to the documentation of this file.
1 
19 #include <iostream>
20 #include <fstream>
21 #include <string>
22 #ifndef Q_MOC_RUN
23 #include <ros/ros.h>
24 #include <boost/algorithm/string.hpp>
25 #endif
26 
27 using namespace std;
28 
30 
31 
33 {
34  ofstream myfile;
35  myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
36 
37  Eigen::Affine3d tr(data.PTU_Frame);
38  Eigen::Matrix3d rotation = tr.rotation();
39 
40  //Eigen::Vector3d angles = rotation.eulerAngles(2, 0, 2);
41  Eigen::Vector3d translation(Eigen::Vector3d(data.PTU_Frame(0, 3), data.PTU_Frame(1, 3), data.PTU_Frame(2, 3)));
42  //myfile << angles[0] << "; " << angles[1] << "; " << angles[2] << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << "; ";
43 
44  Eigen::Quaterniond quaternion(rotation);
45 
46  myfile << quaternion.x() << "; " << quaternion.y() << "; " << quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2]<< "; ";
47 
48 
49  /*transform = data.PTU_Frame;
50  roll = atan2f(transform(2,0), transform(0,0));
51  pitch = asinf(transform(1,0));
52  yaw = atan2f(-transform(1,2), transform(1,1));
53  translation = Eigen::Vector3d(transform(3, 0), transform(3, 1), transform(3, 2));
54  myfile << roll << " " << pitch << " " << yaw << " " << translation[0] << " " << translation[1] << " " << translation[2] << " ";
55 */
56  myfile << data.pan << "; " << data.tilt << std::endl;
57 
58  myfile.close();
59 
60  return true;
61 }
62 
63 std::vector<Transformation_Data> TransformationFile_Manager_Data::readFromFile(string filePath)
64 {
65  std::vector<Transformation_Data> output;
66  std::ifstream input(filePath);
67  std::string line;
68  unsigned int lineNumber = 1;
69  while(std::getline( input, line ))
70  {
71  std::vector<std::string> strs;
72  boost::split(strs, line, boost::is_any_of(";"));
73  if (strs.size() == 9)
74  {
75  std::vector<double> numericValues;
76  bool success = true;
77  for (unsigned int i = 0; i < strs.size(); i++)
78  {
79  try
80  {
81  std::string temp = strs.at(i);
82  boost::algorithm::trim(temp);
83  numericValues.push_back(boost::lexical_cast<double>(temp));
84  }
85  catch (boost::bad_lexical_cast const&)
86  {
87  ROS_ERROR_STREAM("Line " << lineNumber << ": " <<strs.at(i) << " is not a number.");
88  success = false;
89  break;
90  }
91  }
92  if (success)
93  {
94  Eigen::Affine3d rotation(Eigen::Quaterniond(numericValues.at(3),numericValues.at(0),numericValues.at(1),numericValues.at(2)));
95  /*Eigen::Affine3d rotation1(Eigen::AngleAxisd(numericValues.at(0), Eigen::Vector3d(0.0,0.0,1.0)));
96  Eigen::Affine3d rotation2(Eigen::AngleAxisd(numericValues.at(1), Eigen::Vector3d(1.0,0.0,0.0)));
97  Eigen::Affine3d rotation3(Eigen::AngleAxisd(numericValues.at(2), Eigen::Vector3d(0.0,0.0,1.0)));*/
98  Eigen::Affine3d translation(Eigen::Translation3d(Eigen::Vector3d(numericValues.at(4),numericValues.at(5),numericValues.at(6))));
99  Eigen::Affine3d transformation = translation * rotation;
100  Transformation_Data data;
101  data.PTU_Frame = transformation.matrix();
102  data.pan = numericValues.at(7);
103  data.tilt = numericValues.at(8);
104  output.push_back(data);
105  }
106  }
107  else
108  {
109  ROS_ERROR_STREAM("Line " << lineNumber << ": Expected 8 items, got " << strs.size());
110  }
111  lineNumber ++;
112  }
113 
114  return output;
115 }
116 
117 std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> TransformationFile_Manager_Data::readTransformationFromFile(string filePath)
118 {
119  std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> output;
120  std::ifstream input(filePath);
121  std::string line;
122  unsigned int lineNumber = 1;
123  while(std::getline( input, line ))
124  {
125  std::vector<std::string> strs;
126  boost::split(strs, line, boost::is_any_of(";"));
127  if (strs.size() == 7)
128  {
129  std::vector<double> numericValues;
130  bool success = true;
131  for (unsigned int i = 0; i < strs.size(); i++)
132  {
133  try
134  {
135  std::string temp = strs.at(i);
136  boost::algorithm::trim(temp);
137  numericValues.push_back(boost::lexical_cast<double>(temp));
138  }
139  catch (boost::bad_lexical_cast const&)
140  {
141  ROS_ERROR_STREAM("Line " << lineNumber << ": " <<strs.at(i) << " is not a number.");
142  success = false;
143  break;
144  }
145  }
146  if (success)
147  {
148  Eigen::Affine3d rotation(Eigen::Quaterniond(numericValues.at(3),numericValues.at(0),numericValues.at(1),numericValues.at(2)));
149  /*Eigen::Affine3d rotation1(Eigen::AngleAxisd(numericValues.at(0), Eigen::Vector3d(0.0,0.0,1.0)));
150  Eigen::Affine3d rotation2(Eigen::AngleAxisd(numericValues.at(1), Eigen::Vector3d(1.0,0.0,0.0)));
151  Eigen::Affine3d rotation3(Eigen::AngleAxisd(numericValues.at(2), Eigen::Vector3d(0.0,0.0,1.0)));*/
152  Eigen::Affine3d translation(Eigen::Translation3d(Eigen::Vector3d(numericValues.at(4),numericValues.at(5),numericValues.at(6))));
153  Eigen::Affine3d transformation = rotation * translation;
154  Eigen::Matrix4d data;
155  data = transformation.matrix();
156  output.push_back(data);
157  }
158  }
159  else
160  {
161  ROS_ERROR_STREAM("Line " << lineNumber << ": Expected 7 items, got " << strs.size());
162  }
163  lineNumber ++;
164  }
165 
166  return output;
167 }
168 
169 bool TransformationFile_Manager_Data::writeTransformationToFile(const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> transformation)
170 {
171  ofstream myfile;
172  myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
173  for (unsigned int i = 0; i < transformation.size(); i++)
174  {
175 
176  Eigen::Matrix4d temp = transformation.at(i);
177  Eigen::Affine3d tr(temp);
178  Eigen::Matrix3d rotation = tr.rotation();
179  Eigen::Quaterniond quaternion(rotation);
180  Eigen::Vector3d translation(Eigen::Vector3d(temp(0, 3), temp(1, 3), temp(2, 3)));
181 
182  myfile << quaternion.x() << "; " << quaternion.y() << "; " << quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << std::endl;
183 
190  }
191  myfile.close();
192 
193 
194  return true;
195 }
196 
198 {
199  ofstream myfile;
200  myfile.open (this->filePath, std::ofstream::out | std::ofstream::app);
201  for (unsigned int i = 0; i < offsetData.size(); i++)
202  {
203  PanTiltOffsetTuple currentTuple = offsetData.at(i);
204  double pan = std::get<0>(currentTuple);
205  double tilt = std::get<1>(currentTuple);
206  Eigen::Matrix4d temp = std::get<2>(currentTuple);
207  Eigen::Matrix4d tempOld = std::get<3>(currentTuple);
208 
209  myfile << pan << "; " << tilt << "; " << temp(0,3) << "; " << temp(1,3) << "; " << temp(2,3) << "; "<< tempOld(0,3) << "; " << tempOld(1,3) << "; " << tempOld(2,3) << "; " << std::endl;
210  //myfile << quaternion.x() << "; " << quaternion.y() << "; " << quaternion.z() << "; " << quaternion.w() << "; " << translation[0] << "; " << translation[1] << "; " << translation[2] << std::endl;
211 
218  }
219  myfile.close();
220 
221  return true;
222 }
std::vector< Eigen::Matrix4d, Eigen::aligned_allocator< Eigen::Matrix4d > > readTransformationFromFile(string filePath)
std::vector< PanTiltOffsetTuple, Eigen::aligned_allocator< Eigen::Vector4f > > PanTiltOffsetTupleList
bool writeTransformationToFile(const std::vector< Eigen::Matrix4d, Eigen::aligned_allocator< Eigen::Matrix4d >> transformation)
Eigen::Matrix4d PTU_Frame
std::vector< Transformation_Data > readFromFile()
#define ROS_ERROR_STREAM(args)
bool writeCameraOffsetToFile(const PanTiltOffsetTupleList &offsetData)
std::tuple< double, double, Eigen::Matrix4d, Eigen::Matrix4d > PanTiltOffsetTuple


asr_mild_calibration_tool
Author(s): Aumann Florian, Heller Florian, Meißner Pascal
autogenerated on Mon Dec 2 2019 03:11:43