RotationInvariantObjectsRotator.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 
29 void RotationInvariantObjectsRotator::rotateRotationInvariantObjects(const std::string& sourceFile, const std::string& targetFile, const std::string& objectType, const std::string& objectId, bool useMapFrame, const std::vector<std::string>& rotationInvariantTypes)
30 {
31  // copy source to target, so that only the copy will be modified
32  std::ifstream source(sourceFile, std::fstream::binary);
33  std::ofstream target(targetFile, std::fstream::trunc|std::fstream::binary);
34  target << source.rdbuf();
35 
36  TableHelper sourceHelper(sourceFile);
37  TableHelper targetHelper(targetFile);
38  // drop tables to write the sets in the right order
39  targetHelper.dropTable("recorded_objects");
40  targetHelper.dropTable("recorded_sets");
41  targetHelper.createTablesIfNecessary();
42  // assume we have only one pattern (scene)
43  std::string pattern = sourceHelper.getRecordedPatternNames().at(0);
44  // counter begins with 1 because databases IDs begin also with 1
45  int counter = 1;
46  ObjectSetPtr currentSet = sourceHelper.getRecordedObjectSet(counter);
47  while (currentSet->objects.size() != 0)
48  {
49  std::cout << "Processing object set " << counter << "..." << std::endl;
50  if (!useMapFrame)
51  {
52  normalizeRotationInvarienceObjects(currentSet, getReferenceObject(currentSet, objectType, objectId)->pose->quat->eigen, rotationInvariantTypes);
53  }
54  else
55  {
56  normalizeRotationInvarienceObjects(currentSet, Eigen::Quaterniond(1.0, 0.0, 0.0, 0.0), rotationInvariantTypes);
57  }
58  targetHelper.insertRecordedObjectSet(currentSet, pattern);
59  ++counter;
60  currentSet = sourceHelper.getRecordedObjectSet(counter);
61  }
62 }
63 
64 Eigen::AngleAxisd RotationInvariantObjectsRotator::calculateTransform(const Eigen::Vector3d& objectAxis, const Eigen::Vector3d& referenceAxis, const Eigen::Vector3d& unitAxis)
65 {
66  double angle = ISM::GeometryHelper::getAngleBetweenAxes(referenceAxis, objectAxis);
67  Eigen::Vector3d rotationAxis = objectAxis.cross(referenceAxis);
68  rotationAxis.normalize();
69  angle *= M_PI/180; // Convert to radians
70  if (angle < 0.001)
71  {
72  return Eigen::AngleAxisd(0, unitAxis);
73  }
74  else if (fabs(angle - M_PI) < 0.001)
75  {
76  return Eigen::AngleAxisd(M_PI, unitAxis);
77  }
78  else
79  {
80  return Eigen::AngleAxisd(angle, rotationAxis);
81  }
82 }
83 
84 void RotationInvariantObjectsRotator::normalizeRotationInvarienceObjects(ObjectSetPtr objectSet, Eigen::Quaterniond quat, const std::vector<std::string>& rotationInvariantTypes)
85 {
86  // std::cout << "Set normalized to (" << quat.w << ", " << quat.x << ", " << quat.y << ", " << quat.z << ")" << std::endl;
87  Eigen::Quaterniond orientationRef = quat;
88  Eigen::Vector3d yUnitVector = yUnitVector = Eigen::Vector3d::UnitY();
89  orientationRef.normalize();
90 
91  for (ISM::ObjectPtr object : objectSet->objects)
92  {
93  if (std::find(rotationInvariantTypes.begin(), rotationInvariantTypes.end(), object->type) != rotationInvariantTypes.end())
94  {
95  Eigen::Quaterniond orientation = ISM::GeometryHelper::quatToEigenQuat(object->pose->quat);
96  orientation.normalize();
97  Eigen::Vector3d objYAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientation), Eigen::Vector3d::UnitY());
98  Eigen::Vector3d refYAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientationRef), yUnitVector);
99  Eigen::AngleAxisd yTransform = calculateTransform(objYAxis, refYAxis, Eigen::Vector3d::UnitX());
100  Eigen::Quaterniond yAlignedOrientation = (yTransform * orientation).normalized();
101  Eigen::Vector3d objXAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(yAlignedOrientation), Eigen::Vector3d::UnitX());
102  Eigen::Vector3d refXAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientationRef), Eigen::Vector3d::UnitX());
103  object->pose->quat->eigen = (yTransform.inverse() * calculateTransform(objXAxis, refXAxis, Eigen::Vector3d::UnitY())).normalized() * yAlignedOrientation;
104  }
105  }
106 }
107 
108 ObjectPtr RotationInvariantObjectsRotator::getReferenceObject(ObjectSetPtr oset, const std::string& type, const std::string& id)
109 {
110  std::vector<ObjectPtr> objects = oset->objects;
111  ObjectPtr currentObject;
112  for (unsigned int i = 0; i < objects.size(); i++)
113  {
114  currentObject = objects.at(i);
115  if ((currentObject->type == type) && (currentObject->observedId == id))
116  {
117  std::cout << "Found reference object." << std::endl;
118  return currentObject;
119  }
120  }
121  std::cerr << "Reference object is missing in current Object set" << std::endl;
122  return nullptr;
123 }
124 
125 }
const ObjectSetPtr getRecordedObjectSet(int setId) const
Eigen::AngleAxisd calculateTransform(const Eigen::Vector3d &objectAxis, const Eigen::Vector3d &referenceAxis, const Eigen::Vector3d &unitAxis)
calculateTransform Calculate transform between two axis
static Eigen::Vector3d getAxisFromQuat(const QuaternionPtr &quat, const Eigen::Vector3d &viewport=Eigen::Vector3d::UnitX())
static Eigen::Quaternion< double > quatToEigenQuat(const QuaternionPtr &q)
void rotateRotationInvariantObjects(const std::string &sourceFile, const std::string &targetFile, const std::string &objectType, const std::string &objectId, bool useMapFrame, const std::vector< std::string > &rotationInvariantTypes)
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
static double getAngleBetweenAxes(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
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 normalizeRotationInvarienceObjects(ISM::ObjectSetPtr objectSet, Eigen::Quaterniond quat, const std::vector< std::string > &rotationInvariantTypes)
normalizeRotationInvarienceObjects Rotate CS of rotation invariant objects in an object set...
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