RecordedObjectsTransformer.cpp
Go to the documentation of this file.
1 
20 #include "../utility/TableHelper.hpp"
21 #include "../utility/GeometryHelper.hpp"
22 
23 #include <fstream>
24 #include <set>
25 
26 namespace ISM
27 {
28 void RecordedObjectsTransformer::transformRecordedObjects(const std::string& sourceFile, const std::string& targetFile, const std::string& type, const std::string& id,
29  double px, double py, double pz, double qw, double qx, double qy, double qz)
30 {
31  Eigen::Vector3d nullPoint = Eigen::Vector3d(0,0,0);
32  // Eigen::Quaterniond nullQuat = Eigen::Quaterniond(1,0,0,0);
33 
34  Eigen::Vector3d newPoint = Eigen::Vector3d(px,py,pz);
35  Eigen::Quaterniond newQuat = Eigen::Quaterniond(qw,qx,qy,qz);
36 
37 
38  // copy source to target, so that only the copy will be modified
39  std::ifstream source(sourceFile, std::fstream::binary);
40  std::ofstream target(targetFile, std::fstream::trunc|std::fstream::binary);
41  target << source.rdbuf();
42 
43  TableHelper sourceHelper(sourceFile);
44  TableHelper targetHelper(targetFile);
45  // drop tables to write the sets in the right order
46  targetHelper.dropTable("recorded_objects");
47  targetHelper.dropTable("recorded_sets");
48  targetHelper.createTablesIfNecessary();
49  // assume we have only one pattern (scene)
50  std::string pattern = sourceHelper.getRecordedPatternNames().at(0);
51  // counter begins with 1 because databases IDs begin also with 1
52  int counter = 1;
53  ObjectSetPtr currentSet = sourceHelper.getRecordedObjectSet(counter);
54  ObjectPtr reference = getReferenceObject(currentSet, type, id);
55  if (!reference)
56  {
57  std::cout << "Reference Object not found!" << std::endl;
58  exit(0);
59  }
60  Eigen::Vector3d transformPoint = nullPoint - reference->pose->point->eigen + newPoint;
61  Eigen::Quaterniond transformQuat = newQuat * reference->pose->quat->eigen.inverse();
62  Eigen::Vector3d referencePosition = reference->pose->point->eigen;
63 
64  double angle = GeometryHelper::getAngleBetweenQuats(GeometryHelper::eigenQuatToQuat(newQuat), reference->pose->quat);
65  std::cout << "transformQuat: w: " << transformQuat.w() << ", x: " << transformQuat.x() << ", y: " << transformQuat.y() << ", z: " << transformQuat.z() << std::endl;
66  std::cout << "angle: " << angle << std::endl;
67  angle *= M_PI/180; // Convert to radians
68  while (currentSet->objects.size() != 0)
69  {
70  std::cout << "Processing object set " << counter << "..." << std::endl;
71  transformSet(currentSet, transformPoint, transformQuat, referencePosition);
72  targetHelper.insertRecordedObjectSet(currentSet, pattern);
73  ++counter;
74  currentSet = sourceHelper.getRecordedObjectSet(counter);
75  }
76 }
77 
78 void RecordedObjectsTransformer::transformSet(ISM::ObjectSetPtr objectSet, Eigen::Vector3d point, Eigen::Quaterniond quat, Eigen::Vector3d orginalCenter)
79 {
80  Eigen::Translation3d translation(orginalCenter);
81  Eigen::Translation3d translationBack(-orginalCenter);
82  Eigen::Affine3d endTransform;
83  endTransform = translation * quat * translationBack;
84 
85  for (ISM::ObjectPtr object : objectSet->objects)
86  {
87  Eigen::Vector3d transformedPoint = object->pose->point->eigen;
88  transformedPoint = endTransform * transformedPoint;
89  object->pose->point->eigen = transformedPoint + point;
90 
91  Eigen::Quaterniond transformedQuat = quat * object->pose->quat->eigen;
92  transformedQuat.normalize();
93  object->pose->quat->eigen = transformedQuat;
94  }
95 }
96 
97 void RecordedObjectsTransformer::transformSet(ISM::ObjectSetPtr objectSet, Eigen::Vector3d point, double angle)
98 {
99  Eigen::AngleAxisd axis(angle, Eigen::Vector3d::UnitY());
100  for (ISM::ObjectPtr object : objectSet->objects)
101  {
102  Eigen::Vector3d transformedPoint = object->pose->point->eigen + point;
103  Eigen::Quaterniond transformedQuat = axis * object->pose->quat->eigen;
104  transformedQuat.normalize();
105  object->pose->point->eigen = transformedPoint;
106  object->pose->quat->eigen = transformedQuat;
107  }
108 }
109 
110 ObjectPtr RecordedObjectsTransformer::getReferenceObject(ObjectSetPtr oset, const std::string& type, const std::string& id)
111 {
112  std::vector<ObjectPtr> objects = oset->objects;
113  ObjectPtr currentObject;
114  for (unsigned int i = 0; i < objects.size(); i++)
115  {
116  currentObject = objects.at(i);
117  if ((currentObject->type == type) && (currentObject->observedId == id))
118  {
119  std::cout << "Found reference object." << std::endl;
120  return currentObject;
121  }
122  }
123  std::cerr << "Reference object is missing in current Object set" << std::endl;
124  return nullptr;
125 }
126 
127 }
const ObjectSetPtr getRecordedObjectSet(int setId) const
static double getAngleBetweenQuats(const QuaternionPtr &q1, const QuaternionPtr &q2)
void transformSet(ISM::ObjectSetPtr objectSet, Eigen::Vector3d point, Eigen::Quaterniond quat, Eigen::Vector3d orginalCenter)
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
void dropTable(const std::string &tablename) const
ObjectPtr getReferenceObject(ObjectSetPtr oset, const std::string &type, const std::string &id)
getReferenceObject Search for reference object in a set
void transformRecordedObjects(const std::string &sourceFile, const std::string &targetFile, const std::string &type, const std::string &id, double px, double py, double pz, double qw, double qx, double qy, double qz)
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
std::vector< std::string > getRecordedPatternNames() const
void createTablesIfNecessary() const
Definition: TableHelper.cpp:80
this namespace contains all generally usable classes.
static QuaternionPtr eigenQuatToQuat(const Eigen::Quaternion< double > &q)
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40