ObjectDatabaseEntry.cpp
Go to the documentation of this file.
1 
22 #include <ros_uri/ros_uri.hpp>
23 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <vector>
27 #include <boost/algorithm/string/predicate.hpp>
28 #include <boost/lexical_cast.hpp>
29 
30 #include <math.h>
32 
33 namespace object_database
34 
35 {
36 namespace fs = boost::filesystem;
37 
39  const std::string uniqueName, const fs::path path,
40  const fs::path rvizMeshResourcePath,
41  const fs::path normalVectorResourcePath) :
42  mRecognizerPtr(recognizerPtr),
43  mUniqueName(uniqueName),
44  mPath(path),
45  mRvizMeshResourcePath(rvizMeshResourcePath),
46  mNormalVectorResourcePath(normalVectorResourcePath),
47  mNormalVectors()
48 { }
49 
51  const std::string uniqueName,
52  const fs::path path, const fs::path rvizMeshResourcePath,
53  const fs::path normalVectorResourcePath,
54  const fs::path deviation_resource_path,
55  const fs::path rotation_invariance_resource_path):
56  mRecognizerPtr(recognizerPtr),
57  mUniqueName(uniqueName),
58  mPath(path),
59  mRvizMeshResourcePath(rvizMeshResourcePath),
60  mNormalVectorResourcePath(normalVectorResourcePath),
61  deviation_resource_path_(deviation_resource_path),
62  rotation_invariance_resource_path(rotation_invariance_resource_path),
64  deviations_(),
65  rotation_invariant(false)
66 {}
67 
69 {
70  return mRecognizerPtr;
71 }
72 
74 {
75  return mUniqueName;
76 }
77 
79 {
80  return mPath;
81 }
82 
84 {
85  return mRvizMeshResourcePath;
86 }
87 
89 {
91 }
92 
93 std::vector<geometry_msgs::Point> ObjectDatabaseEntry::getNormalVectors()
94 {
95  // if there is no path given or the normal vectors are already given
96  if (mNormalVectors.size() > 0)
97  {
98  ROS_DEBUG("Returning cached normal vectors for object '%s'.", this->getUniqueName().c_str());
99  return mNormalVectors;
100  }
101  else if (this->getNormalVectorResourcePath() == fs::path())
102  {
103  ROS_WARN("No normal vector file found for object '%s'.", this->getUniqueName().c_str());
104  return mNormalVectors;
105  }
106 
107  if (fs::exists(this->getNormalVectorResourcePath()))
108  {
109  std::ifstream normalVectorFileHandle(this->getNormalVectorResourcePath().c_str());
110  if (normalVectorFileHandle.is_open())
111  {
112  ROS_INFO("Reading normal vector file for object '%s'", this->getUniqueName().c_str());
113  std::string normalVectorString;
114  std::size_t startSearchPos = 0;
115  while (std::getline(normalVectorFileHandle, normalVectorString))
116  {
117  while (startSearchPos < normalVectorString.size())
118  {
119  std::size_t searchPos = normalVectorString.find(';', startSearchPos);
120  if (searchPos == std::string::npos)
121  {
122  searchPos = normalVectorString.size();
123  }
124 
125  // get the coordinate string.
126  std::string coordinateString = normalVectorString.substr(startSearchPos,
127  (searchPos - startSearchPos));
128 
129  std::size_t startValueSearchPos = 0;
130  std::vector<double> coordinateStack;
131  while (startValueSearchPos < coordinateString.size())
132  {
133  std::size_t valueSearchPos = coordinateString.find(',', startValueSearchPos);
134  if (valueSearchPos == std::string::npos)
135  {
136  valueSearchPos = coordinateString.size();
137  }
138 
139  std::string valueString = coordinateString.substr(startValueSearchPos,
140  (valueSearchPos - startValueSearchPos));
141 
142 
143  double coordinateValue = boost::lexical_cast<double>(valueString);
144  coordinateStack.push_back(coordinateValue);
145 
146  startValueSearchPos = valueSearchPos + 1;
147  }
148 
149  if (coordinateStack.size() != 3)
150  {
151  ROS_WARN("Ignoring a coordinate because there were not exactly three values given.");
152  continue;
153  }
154 
155  // create the normal vector and add it
156  geometry_msgs::Point normalVector;
157  normalVector.x = coordinateStack [0];
158  normalVector.y = coordinateStack [1];
159  normalVector.z = coordinateStack [2];
160  mNormalVectors.push_back(normalVector);
161 
162  startSearchPos = searchPos + 1;
163  }
164  }
165 
166 
167  // close file.
168  normalVectorFileHandle.close();
169  return mNormalVectors;
170  }
171  }
172 
173  //if(fs::exists(this->getRvizMeshResourcePath()) && fs::exists(this->getPath()))
174  else if(fs::exists(this->getPath()))
175  {
176  ROS_INFO("Calculating normal vectors for object '%s'", this->getUniqueName().c_str());
177  // calculate the normal vector
178  mNormalVectors = NormalGenerator::getNormalVectors(30 * M_PI / 180, 30 * M_PI / 180, 30 * M_PI / 180,
179  this->getPath().string() + "/" +
180  this->getUniqueName() + ".nv.txt");
181  }
182 
183  return mNormalVectors;
184 }
185 
187 {
189 }
190 
192 {
193  if (deviations_.size() == 6)
194  {
195  return deviations_;
196  }
197  if (fs::exists(getDeviationResourcePath()))
198  {
199  std::ifstream deviation_file_handle(getDeviationResourcePath().c_str());
200  if (deviation_file_handle.is_open())
201  {
202  ROS_INFO("Reading deviation file for object '%s'", getUniqueName().c_str());
203  std::string deviation_string;
204 
205  unsigned int line_number = 0;
206  while (std::getline(deviation_file_handle, deviation_string))
207  {
208 
209  line_number++;
210  if (boost::starts_with(deviation_string, "#"))
211  continue;
212  try
213  {
214  deviations_.push_back(std::stod(deviation_string));
215  }
216  catch (const std::invalid_argument& ia)
217  {
218  ROS_ERROR_STREAM("Error by parsing" << getDeviationResourcePath().c_str() <<
219  " in line " << line_number << ": " << ia.what());
220  }
221  if(deviations_.size() >= 6)
222  break;
223  }
224  if (deviations_.size() < 6)
225  {
226  ROS_ERROR_STREAM("There were only " << deviations_.size()
227  << "values for the deviation, but 6 are needed");
228  deviations_.clear();
229  }
230  for (double d: deviations_)
231  ROS_INFO_STREAM("read deviation:" << d );
232  deviation_file_handle.close();
233  }
234  }
235  return deviations_;
236 }
237 
239 {
241 }
242 
244 {
245  // if there is no path given or the normal vectors are already given
246  if (rotation_invariant)
247  {
248  return rotation_invariant;
249  }
250  else if (this->getRotationInvarianceResourcePath() == fs::path())
251  {
252  ROS_WARN("No rotation invariance file found for object '%s'.", this->getUniqueName().c_str());
253  return rotation_invariant;
254  }
255 
256  if (fs::exists(this->getRotationInvarianceResourcePath()))
257  {
258  std::ifstream rotation_invariance_file_handle(this->getRotationInvarianceResourcePath().c_str());
259  if (rotation_invariance_file_handle.is_open())
260  {
261  ROS_INFO("Reading rotation invariance file for object '%s'", this->getUniqueName().c_str());
262  std::string rotation_invariance_string;
263  std::getline(rotation_invariance_file_handle, rotation_invariance_string);
264  rotation_invariant = boost::lexical_cast<bool>(rotation_invariance_string);
265 
266  // close file.
267  rotation_invariance_file_handle.close();
268  }
269  }
270  return rotation_invariant;
271 }
272 }
273 
d
#define ROS_WARN(...)
ROS_DEPRECATED ObjectDatabaseEntry(ObjectDatabaseRecognizer *recognizerPtr, const std::string uniqueName, const fs::path path, const fs::path rvizMeshResourcePath, const fs::path normalVectorResourcePath)
static std::vector< geometry_msgs::Point > getNormalVectors(double angle_x, double angle_y, double angle_z, const std::string &savePath=std::string())
#define ROS_INFO(...)
std::vector< geometry_msgs::Point > getNormalVectors()
ObjectDatabaseRecognizer * getRecognizer()
::xsd::cxx::tree::string< char, simple_type > string
#define ROS_INFO_STREAM(args)
ObjectDatabaseRecognizer * mRecognizerPtr
#define ROS_ERROR_STREAM(args)
std::vector< geometry_msgs::Point > mNormalVectors
#define ROS_DEBUG(...)


asr_object_database
Author(s): Allgeyer Tobias, Aumann Florian, Borella Jocelyn, Braun Kai, Heizmann Heinrich, Heller Florian, Kasper Alexander, Marek Felix, Mehlhaus Jonas, Meißner Pascal, Schleicher Ralf, Stöckle Patrick, Walter Milena
autogenerated on Mon Feb 28 2022 21:49:21