PoseInterpolator.cpp
Go to the documentation of this file.
1 
19 #include "PoseInterpolator.hpp"
20 #include "../utility/TableHelper.hpp"
21 #include "../utility/GeometryHelper.hpp"
22 
23 #include <fstream>
24 #include <set>
25 
26 namespace ISM
27 {
28 void PoseInterpolator::interpolate(const std::string& sourceFile, const std::string& targetFile, int stepNumber)
29 {
30  if (stepNumber < 1)
31  {
32  std::cerr << "stepNumber is < 1!" << std::endl;
33  exit(0);
34  }
35 
36  // copy source to target, so that only the copy will be modified
37  std::ifstream source(sourceFile, std::fstream::binary);
38  std::ofstream target(targetFile, std::fstream::trunc|std::fstream::binary);
39  target << source.rdbuf();
40 
41  TableHelper sourceHelper(sourceFile);
42  TableHelper targetHelper(targetFile);
43  // drop tables to write the sets in the right order
44  targetHelper.dropTable("recorded_objects");
45  targetHelper.dropTable("recorded_sets");
46  targetHelper.createTablesIfNecessary();
47 
48  // assume we have only one pattern (scene)
49  std::string pattern = sourceHelper.getRecordedPatternNames().at(0);
50  // counter begins with 1 because databases IDs begin also with 1
51  int counter = 1;
52  ObjectSetPtr from = sourceHelper.getRecordedObjectSet(counter);
53  ObjectSetPtr to = sourceHelper.getRecordedObjectSet(counter + 1);
54  while (from->objects.size() != 0 && to->objects.size() != 0)
55  {
56  std::cout << "Interpolating between object sets " << counter << " and " << counter + 1 << "..." << std::endl;
57  targetHelper.insertRecordedObjectSet(from, pattern);
58  for (ObjectSetPtr objectSet : interpolateSets(from, to, stepNumber))
59  {
60  targetHelper.insertRecordedObjectSet(objectSet, pattern);
61  }
62  ++counter;
63  from = sourceHelper.getRecordedObjectSet(counter);
64  to = sourceHelper.getRecordedObjectSet(counter + 1);
65  }
66  // insert the last ObjectSet
67  targetHelper.insertRecordedObjectSet(from, pattern);
68  std::cout << "Finished!" << std::endl;
69 }
70 
71 std::vector<PosePtr> PoseInterpolator::interpolatePoses(PosePtr from, PosePtr to, int stepNumber)
72 {
73  std::vector<PosePtr> result;
74  int divisor = stepNumber + 1;
75  Eigen::Vector3d fromPosition = GeometryHelper::pointToVector(from->point);
76  Eigen::Vector3d toPosition = GeometryHelper::pointToVector(to->point);
77  Eigen::Quaterniond fromOrient = GeometryHelper::quatToEigenQuat(from->quat);
78  Eigen::Quaterniond toOrient = GeometryHelper::quatToEigenQuat(to->quat);
79  Eigen::Vector3d interpolatedPosition;
80  Eigen::Quaterniond interpolatedQuat;
81  for (int i = 1; i <= stepNumber; i++)
82  {
83  interpolatedPosition = fromPosition * (divisor - i)/divisor + toPosition * i/divisor;
84  interpolatedQuat = fromOrient.slerp((double)i/divisor, toOrient);
85  result.push_back(PosePtr(new Pose(GeometryHelper::vectorToPoint(interpolatedPosition), GeometryHelper::eigenQuatToQuat(interpolatedQuat))));
86  }
87 
88  return result;
89 }
90 
91 std::vector<ObjectSetPtr> PoseInterpolator::interpolateSets(const ObjectSetPtr from, const ObjectSetPtr to, const int stepNumber)
92 {
93  std::vector<ObjectSetPtr> result;
94  // prepare sets in the vector
95  for (int i = 0; i < stepNumber; i++)
96  {
97  result.push_back(ObjectSetPtr(new ObjectSet()));
98  }
99  std::vector<PosePtr> interpolatedPoses;
100  PosePtr pose;
101  ObjectPtr interpolatedObject;
102  for (unsigned i = 0; i < from->objects.size(); i++)
103  {
104  interpolatedPoses = interpolatePoses(from->objects.at(i)->pose, to->objects.at(i)->pose, stepNumber);
105  for (unsigned j = 0; j < interpolatedPoses.size(); j++)
106  {
107  pose = interpolatedPoses.at(j);
108  interpolatedObject = ObjectPtr(new Object(*(from->objects.at(i).get()))); // copy objectPtr
109  interpolatedObject->pose = pose;
110  result.at(j)->insert(interpolatedObject);
111  }
112  }
113 
114  return result;
115 }
116 
117 }
const ObjectSetPtr getRecordedObjectSet(int setId) const
static PointPtr vectorToPoint(const Eigen::Vector3d &v)
static Eigen::Quaternion< double > quatToEigenQuat(const QuaternionPtr &q)
void interpolate(const std::string &sourceFile, const std::string &targetFile, int stepNumber)
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
void dropTable(const std::string &tablename) const
std::vector< ObjectSetPtr > interpolateSets(const ObjectSetPtr from, const ObjectSetPtr to, const int stepNumber)
interpolate poses between two given ObjectSets
std::vector< PosePtr > interpolatePoses(PosePtr from, PosePtr to, int stepNumber)
interpolate poses between two given poses
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
static Eigen::Vector3d pointToVector(const PointPtr &p)
boost::shared_ptr< Pose > PosePtr
Definition: Pose.hpp:79
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