TableHelper.cpp
Go to the documentation of this file.
1 
18 #include "TableHelper.hpp"
19 #include <set>
20 #include <iostream>
21 #include <vector>
22 #include <map>
23 #include <boost/foreach.hpp>
24 #include <boost/filesystem.hpp>
25 
26 namespace ISM {
27 
28 /*******************
29  UTIL
30  *******************/
31 
32 TableHelper::TableHelper(std::string dbfilename) {
33  this->sqlite.reset(new session(sqlite3, dbfilename.c_str()));
36  objectIdMap_.clear();
37 }
38 
40  (*this->sqlite).close();
41 }
42 
44  std::map<std::string, std::vector<std::pair<std::string,std::string>> > tableNamesToColumns;
45  tableNamesToColumns["model_votes"].push_back(std::make_pair("weight", "INTEGER"));
46  for(const std::pair<std::string, std::vector<std::pair<std::string,std::string>> >& tablesToColumns : tableNamesToColumns)
47  {
48  std::vector<std::string> columnsAlreadyExisting;
49  std::stringstream infoSs;
50  infoSs << "pragma table_info('" << tablesToColumns.first << "');";
51  rowset<row> rs = (*sqlite).prepare << infoSs.str();
52  for(soci::row& row : rs)
53  {
54  columnsAlreadyExisting.push_back(row.get<std::string>(1));
55  }
56  for(const std::pair<std::string,std::string>& col : tablesToColumns.second)
57  {
58  if(std::find(columnsAlreadyExisting.begin(), columnsAlreadyExisting.end(), col.first)
59  == columnsAlreadyExisting.end())
60  {
61  std::stringstream alterSs;
62  alterSs << "ALTER TABLE " << tablesToColumns.first << " ADD " << col.first <<
63  " " << col.second << ";";
64  (*sqlite) << alterSs.str();
65  }
66  }
67  }
68 }
69 
70 const std::set<std::string> TableHelper::getObjectsInPattern(std::string pattern_name){
71  rowset<row> rs = ((*sqlite).prepare<<"SELECT DISTINCT ro.type, ro.observedID FROM recorded_objects ro JOIN recorded_poses rpo on ro.id = rpo.objectId JOIN recorded_sets rs on rpo.setId = rs.id JOIN recorded_patterns rpa on rs.patternId = rpa.id where rpa.name = '" << pattern_name << "';");
72 
73  std::set<std::string> types;
74  BOOST_FOREACH(row const& row, rs) {
75  types.insert(row.get<std::string>(0) + row.get<std::string>(1));
76  }
77  return types;
78 }
79 
81  std::set<std::string> tables;
82 
83  rowset<row> rs = (*sqlite).prepare<< "SELECT tbl_name FROM sqlite_master WHERE type = 'table';";
84  for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it) {
85  row const& row = *it;
86  tables.insert(row.get<std::string>(0));
87  }
88 
89  std::map<std::string, std::string> tableDefs;
90  tableDefs["recorded_objects"] = "id INTEGER PRIMARY KEY, type TEXT, observedId TEXT, resourcePath TEXT";
91  tableDefs["recorded_poses"] = "id INTEGER PRIMARY KEY, objectId INTEGER, setId INTEGER, px FLOAT, py FLOAT, pz FLOAT, qw FLOAT, qx FLOAT, qy FLOAT, qz FLOAT";
92  tableDefs["recorded_sets"] = "id INTEGER PRIMARY KEY, patternId INTEGER";
93  tableDefs["recorded_patterns"] = "id INTEGER PRIMARY KEY, name TEXT UNIQUE";
94  tableDefs["model_objects"] = "id INTEGER PRIMARY KEY, type TEXT UNIQUE";
95  tableDefs["model_votes"] =
96  "id INTEGER PRIMARY KEY, objectId INTEGER, patternId INTEGER, observedId TEXT, radius FLOAT"
97  ", qw FLOAT, qx FLOAT, qy FLOAT, qz FLOAT"
98  ", qw2 FLOAT, qx2 FLOAT, qy2 FLOAT, qz2 FLOAT"
99  ", qpw FLOAT, qpx FLOAT, qpy FLOAT, qpz FLOAT"
100  ", qpw2 FLOAT, qpx2 FLOAT, qpy2 FLOAT, qpz2 FLOAT"
101  ", trackIndex INTEGER, weight INTEGER";
102  tableDefs["model_patterns"] = "id INTEGER PRIMARY KEY, name TEXT UNIQUE, expectedMaxWeight INTEGER";
103 
104  typedef std::pair<std::string, std::string> pair_type;
105  BOOST_FOREACH(pair_type p, tableDefs) {
106  if (tables.find(p.first) == tables.end()) {
107  this->createTable(p.first, p.second);
108  }
109  }
110 }
111 
112 void TableHelper::createTable(const std::string& tablename, const std::string& sql) const {
113  try {
114  (*sqlite).once<<"CREATE TABLE `"<<tablename<<"` ("<<sql<<");";
115  } catch (soci_error e) {
116  std::cerr<<"SQL error "<<e.what()<<std::endl;
117  throw e;
118  }
119 }
120 
121 void TableHelper::dropTable(const std::string& tablename) const {
122  try {
123  (*sqlite).once<<"DROP TABLE `"<<tablename<<"`;";
124  } catch (soci_error e) {
125  std::cerr<<"SQL error "<<e.what()<<std::endl;
126  throw e;
127  }
128 }
129 
131 {
132  this->dropTable("recorded_objects");
133  this->dropTable("recorded_poses");
134  this->dropTable("recorded_patterns");
135  this->dropTable("recorded_sets");
136 }
137 
139 {
140  this->dropTable("recorded_objects");
141  this->dropTable("recorded_poses");
142  this->dropTable("recorded_patterns");
143  this->dropTable("recorded_sets");
144  this->dropTable("model_votes");
145  this->dropTable("model_patterns");
146  this->dropTable("model_objects");
147 }
148 
150  this->dropTable("model_votes");
151  this->dropTable("model_patterns");
152  this->dropTable("model_objects");
153 }
154 
156 {
157  int count;
158  (*sqlite) << "SELECT COUNT(*) FROM `recorded_patterns`;", into(count);
159 
160  return count;
161 }
162 
164 {
165  int count;
166  (*sqlite) << "SELECT COUNT(*) FROM `model_patterns`;", into(count);
167 
168  return count;
169 }
170 
171 int TableHelper::getLastInsertId(const std::string& tablename) const {
172  int id = 0;
173  try {
174  (*sqlite)<<"SELECT id FROM `"<<tablename<<"` ORDER BY ID DESC LIMIT 1;", into(id);
175  } catch (soci_error e) {
176  std::cerr<<"SQL error "<<e.what()<<std::endl;
177  throw e;
178  }
179 
180  return id;
181 }
182 
183 /*******************
184  Recorded
185  *******************/
187 {
188  std::pair<std::string, std::string> objectIdentifier = std::make_pair(object->type, object->observedId);
189  if(objectIdMap_.find(objectIdentifier) != objectIdMap_.end())
190  {
191  return objectIdMap_[objectIdentifier];
192  }
193 
194  int id;
195  indicator ind;
196  (*sqlite) << "SELECT id FROM `recorded_objects` where type = :type and observedId = :observedId;", into(id, ind), use(object->type),use(object->observedId);
197  if ((*sqlite).got_data() && ind == i_ok) {
198  objectIdMap_[objectIdentifier] = id;
199  return id;
200  } else {
201  int lastInsertId = getLastInsertId("recorded_objects");
202  (*sqlite) << "INSERT INTO `recorded_objects` (id, type, observedId, resourcePath) values (:id, :type, :oid, :resourcePath);",
203  use(lastInsertId+1),
204  use(object->type),
205  use(object->observedId),
206  use(object->ressourcePath.string());
207  objectIdMap_[objectIdentifier] = lastInsertId + 1;
208  return lastInsertId + 1;
209  }
210 }
211 
212 
213 int TableHelper::insertRecordedObject(const boost::shared_ptr<Object>& o, int setId) {
214  int objectId = this->insertRecordedObjectIfNecessary(o);
215  (*sqlite) << "INSERT INTO `recorded_poses` (objectId, setId, px, py, pz, qw, qx, qy, qz) values (:objectId, :setId, :px, :py, :pz, :qw, :qx, :qy, :qz);",
216  use(objectId),
217  use(setId),
218  use(o->pose->point->eigen.x()),
219  use(o->pose->point->eigen.y()),
220  use(o->pose->point->eigen.z()),
221  use(o->pose->quat->eigen.w()),
222  use(o->pose->quat->eigen.x()),
223  use(o->pose->quat->eigen.y()),
224  use(o->pose->quat->eigen.z());
225 
226  return this->getLastInsertId("recorded_objects");
227 }
228 
230  std::vector<boost::shared_ptr<Object> > objects = os->objects;
231 
232  transaction trans(*sqlite);
233  int patternId = this->ensureRecordedPatternName(patternName);
234 
235  (*sqlite) << "INSERT INTO `recorded_sets` (patternId) VALUES (:patternId);", use(patternId);
236  int setId = this->getLastInsertId("recorded_sets");
237  for (size_t i = 0; i < objects.size(); i++) {
238  this->insertRecordedObject(objects[i], setId);
239  }
240 
241  trans.commit();
242 
243  return setId;
244 }
245 
246 int TableHelper::insertRecordedPattern(const std::string& patternName) const {
247  (*sqlite) << "INSERT INTO `recorded_patterns` (name) VALUES (:patternName);", use(patternName);
248  return this->getLastInsertId("recorded_patterns");
249 }
250 
252  int id = this->getRecordedPatternId(patternName);
253  return id == 0 ? this->insertRecordedPattern(patternName) : id;
254 }
255 
256 std::vector<std::string> TableHelper::getRecordedPatternNames() const {
257  std::vector<std::string> names;
258  rowset<std::string> rs = ((*sqlite).prepare<< "SELECT name FROM `recorded_patterns`;");
259  for (std::string& name : rs) {
260  names.push_back(name);;
261  }
262  return names;
263 }
264 
265 int TableHelper::getRecordedPatternId(const std::string& patternName) const {
266  int id;
267  indicator ind;
268  (*sqlite) << "SELECT id FROM `recorded_patterns` WHERE name = :name;", into(id, ind), use(patternName);
269  if ((*sqlite).got_data() && ind == i_ok) {
270  return id;
271  } else {
272  return 0;
273  }
274 }
275 
277  boost::shared_ptr<RecordedPattern> pattern;
278  int patternId = this->getRecordedPatternId(patternName);
279 
280  if (patternId != 0) {
281  pattern.reset(new RecordedPattern(patternName));
282 
283  rowset<int> rs = ((*sqlite).prepare<< "SELECT id FROM `recorded_sets` WHERE patternId = :patternId;", use(patternId));
284  for (rowset<int>::const_iterator it = rs.begin(); it != rs.end(); ++it) {
285  pattern->addObjectSet(this->getRecordedObjectSet(*it));
286  }
287  }
288 
289  return pattern;
290 }
291 
292 const std::vector<int> TableHelper::getSetIds() const {
293  std::vector<int> setIds;
294  rowset<int> rs = ((*sqlite).prepare<< "SELECT DISTINCT setId FROM `recorded_poses`;");
295 
296  for (rowset<int>::const_iterator it = rs.begin(); it != rs.end(); ++it) {
297  setIds.push_back(*it);
298  }
299  return setIds;
300 }
301 
303  boost::shared_ptr<ObjectSet> s(new ObjectSet());
304 
305  rowset<row> rs = ((*sqlite).prepare<<
306  "SELECT ro.type, ro.observedId, rp.px, rp.py, rp.pz, rp.qw, rp.qx, rp.qy, rp.qz, ro.resourcePath FROM `recorded_objects` ro JOIN `recorded_poses` rp WHERE ro.id = rp.objectId AND setId = :setId;",
307  use(setId)
308  );
309 
310  for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it) {
311  row const& row = *it;
312  boost::filesystem::path meshPath = row.get<std::string>(9,"");
313  ObjectPtr object(
314  new Object(
315  row.get<std::string>(0, ""),
316  PosePtr(new Pose(
317  new Point(
318  row.get<double>(2, 0.0),
319  row.get<double>(3, 0.0),
320  row.get<double>(4, 0.0)
321  ),
322  new Quaternion(
323  row.get<double>(5, 0.0),
324  row.get<double>(6, 0.0),
325  row.get<double>(7, 0.0),
326  row.get<double>(8, 0.0)
327  )
328  )
329  ),
330  row.get<std::string>(1, "")
331  )
332  );
333  object->ressourcePath = meshPath;
334  s->insert(object);
335  }
336  return s;
337 }
338 
339 /*******************
340  Model
341  *******************/
342 
344  int patternId = this->ensureModelPatternName(vote->patternName);
345  int objectId = this->ensureModelObjectType(vote->objectType);
346 
347  (*sqlite) << "INSERT INTO `model_votes` "<<
348  "(objectId, patternId, observedId, radius, qw, qx, qy, qz, qw2, qx2, qy2, qz2, qpw, qpx, qpy, qpz, qpw2, qpx2, qpy2, qpz2, trackIndex, weight) values "<<
349  "(:objectId, :patternId, :observedId, :radius, :qw, :qx, :qy, :qz, :qw2, :qx2, :qy2, :qz2, :qpw, :qpx, :qpy, :qpz, :qpw2, :qpx2, :qpy2, :qpz2, :trackIndex, :weight);",
350  use(objectId),
351  use(patternId),
352  use(vote->observedId),
353  use(vote->radius),
354  use(vote->objectToRefQuat->eigen.w()),
355  use(vote->objectToRefQuat->eigen.x()),
356  use(vote->objectToRefQuat->eigen.y()),
357  use(vote->objectToRefQuat->eigen.z()),
358  use(vote->objectToRefPoseQuat->eigen.w()),
359  use(vote->objectToRefPoseQuat->eigen.x()),
360  use(vote->objectToRefPoseQuat->eigen.y()),
361  use(vote->objectToRefPoseQuat->eigen.z()),
362  use(vote->refToObjectQuat->eigen.w()),
363  use(vote->refToObjectQuat->eigen.x()),
364  use(vote->refToObjectQuat->eigen.y()),
365  use(vote->refToObjectQuat->eigen.z()),
366  use(vote->refToObjectPoseQuat->eigen.w()),
367  use(vote->refToObjectPoseQuat->eigen.x()),
368  use(vote->refToObjectPoseQuat->eigen.y()),
369  use(vote->refToObjectPoseQuat->eigen.z()),
370  use(vote->trackIndex),
371  use(vote->weight);
372 
373  return this->getLastInsertId("model_votes");
374 }
375 
376 int TableHelper::ensureModelPatternName(const std::string& patternName) const {
377  int id = this->getModelPatternId(patternName);
378  return id == 0 ? this->insertModelPattern(patternName) : id;
379 }
380 
381 int TableHelper::ensureModelObjectType(const std::string& objectType) const {
382  int id = this->getModelObjectTypeId(objectType);
383  return id == 0 ? this->insertModelObjectType(objectType) : id;
384 }
385 
386 int TableHelper::insertModelPattern(const std::string& patternName) const {
387  (*sqlite) << "INSERT INTO `model_patterns` (name) VALUES (:patternName);", use(patternName);
388  return this->getLastInsertId("model_patterns");
389 }
390 
391 int TableHelper::upsertModelPattern(const std::string& patternName, int expectedMaxWeight) const {
392  int patternId = this->getModelPatternId(patternName);
393  if (patternId == 0) {
394  (*sqlite) << "INSERT INTO `model_patterns` (name, expectedMaxWeight) VALUES (:patternName, :expectedMaxWeight);",
395  use(patternName),
396  use(expectedMaxWeight);
397  } else {
398  (*sqlite) << "REPLACE INTO `model_patterns` (id, name, expectedMaxWeight) VALUES (:id, :patternName, :expectedMaxWeight);",
399  use(patternId),
400  use(patternName),
401  use(expectedMaxWeight);
402  }
403  return this->getLastInsertId("model_patterns");
404 }
405 
406 int TableHelper::insertModelObjectType(const std::string& objectType) const {
407  (*sqlite) << "INSERT INTO `model_objects` (type) VALUES (:objectType);", use(objectType);
408  return this->getLastInsertId("model_objects");
409 }
410 
411 int TableHelper::getModelPatternId(const std::string& patternName) const {
412  int id;
413  indicator ind;
414  (*sqlite) << "SELECT id FROM `model_patterns` WHERE name = :name;", into(id, ind), use(patternName);
415  if ((*sqlite).got_data() && ind == i_ok) {
416  return id;
417  } else {
418  return 0;
419  }
420 }
421 
422 int TableHelper::getModelObjectTypeId(const std::string& objectType) const {
423  int id;
424  indicator ind;
425  (*sqlite) << "SELECT id FROM `model_objects` WHERE type = :objectType;", into(id, ind), use(objectType);
426  if ((*sqlite).got_data() && ind == i_ok) {
427  return id;
428  } else {
429  return 0;
430  }
431 }
432 
433 std::vector<std::string> TableHelper::getModelPatternNames() const {
434  std::vector<std::string> names;
435  rowset<std::string> rs = ((*sqlite).prepare<< "SELECT name FROM `model_patterns`;");
436  for (std::string& name : rs) {
437  names.push_back(name);;
438  }
439 
440  return names;
441 }
442 
443 const PatternNameToPatternMap TableHelper::getPatternDefinitionsByName(const std::set<std::string>& patternNames) const {
444  PatternNameToPatternMap patterns;
445  BOOST_FOREACH(std::string patternName, patternNames) {
446  int expectedMaxWeight;
447  indicator indEOC;
448  (*sqlite) <<
449  "SELECT expectedMaxWeight "<<
450  "FROM `model_patterns` "<<
451  "WHERE name = :patternName LIMIT 1;",
452  into(expectedMaxWeight, indEOC),
453  use(patternName);
454  if ((*sqlite).got_data() && indEOC == i_ok) {
455  patterns[patternName] = PatternPtr(new Pattern(patternName, expectedMaxWeight));
456  }
457  }
458 
459  return patterns;
460 }
461 
462 const std::set<std::string> TableHelper::getObjectTypes() const {
463  rowset<row> rs = ((*sqlite).prepare<<"SELECT type FROM `model_objects`;");
464  std::set<std::string> types;
465  BOOST_FOREACH(row const& row, rs) {
466  types.insert(row.get<std::string>(0));
467  }
468  return types;
469 }
470 
471 const std::map<std::string, boost::filesystem::path> TableHelper::getRessourcePaths() const {
472  rowset<row> rs = ((*sqlite).prepare<<"SELECT DISTINCT type, resourcePath FROM `recorded_objects`;");
473  std::map<std::string, boost::filesystem::path> types_to_ressource_paths_map;
474  BOOST_FOREACH(row const& row, rs) {
475  std::string type = row.get<std::string>(0);
476  boost::filesystem::path meshPath = row.get<std::string>(1,"");
477  types_to_ressource_paths_map[type] = meshPath;
478  }
479  return types_to_ressource_paths_map;
480 }
481 
482 const std::map<std::string, std::map<std::string, std::string>> TableHelper::getModelWeightsPerTypeAndId() const {
483  std::map<std::string, std::map<std::string, std::string>> modelWeightsPerTypeAndId;
484  try {
485  rowset<row> rs = ((*sqlite).prepare<<"SELECT type, observedId, weight FROM `model_weight`;");
486  BOOST_FOREACH(row const& row, rs) {
487  modelWeightsPerTypeAndId[row.get<std::string>(0)][row.get<std::string>(1)] = row.get<std::string>(2);
488  }
489  } catch (soci::sqlite3_soci_error e) {
490  if (std::string(e.what()).find("no such table: model_weight") != std::string::npos) {
491  // the table model_weight will be added by intermediate_object_generator; if not present return empty map
492  } else {
493  throw e;
494  }
495  }
496  return modelWeightsPerTypeAndId;
497 }
498 
499 const std::set<std::pair<std::string, std::string> > TableHelper::getObjectTypesAndIdsFromModelObjects() const {
500  rowset<row> rs = ((*sqlite).prepare<<"SELECT DISTINCT type, observedId FROM model_votes v JOIN model_objects o on o.id = v.objectId");
501  std::set<std::pair<std::string, std::string> > typesAndIds;
502  BOOST_FOREACH(row const& row, rs) {
503  typesAndIds.insert(std::make_pair(row.get<std::string>(0), row.get<std::string>(1)));
504  }
505  return typesAndIds;
506 }
507 
508 const std::set<std::pair<std::string, std::string> > TableHelper::getObjectTypesAndIdsFromRecordedObjects() const {
509  rowset<row> rs = ((*sqlite).prepare<<"SELECT DISTINCT type, observedId FROM `recorded_objects`;");
510  std::set<std::pair<std::string, std::string> > typesAndIds;
511  BOOST_FOREACH(row const& row, rs) {
512  typesAndIds.insert(std::make_pair(row.get<std::string>(0), row.get<std::string>(1)));
513  }
514  return typesAndIds;
515 }
516 
517 const ObjectTypeToVoteMap TableHelper::getVoteSpecifiersForObjectTypes(const std::set<std::string>& objectTypes) const {
518  ObjectTypeToVoteMap voteSpecifierMap;
519  BOOST_FOREACH(std::string objectType, objectTypes) {
520  int objectId = getModelObjectTypeId(objectType);
521  rowset<row> rs = ((*sqlite).prepare<<
522  "SELECT radius, name, type, observedId, qw, qx, qy, qz, qw2, qx2, qy2, qz2, qpw, qpx, qpy, qpz, qpw2, qpx2, qpy2, qpz2, trackIndex, v.weight "<<
523  "FROM `model_votes` AS v "<<
524  "JOIN `model_objects` AS o ON v.objectId = o.id "<<
525  "JOIN `model_patterns` AS p ON v.patternId = p.id "<<
526  "WHERE objectId = :objectId;",
527  use(objectId)
528  );
529  std::vector<VoteSpecifierPtr> specifiers;
530  BOOST_FOREACH(row const& row, rs) {
531  VoteSpecifierPtr vote(
532  new VoteSpecifier(
534  new Quaternion(
535  row.get<double>(4, 0.0),
536  row.get<double>(5, 0.0),
537  row.get<double>(6, 0.0),
538  row.get<double>(7, 0.0)
539  )
540  ),
542  new Quaternion(
543  row.get<double>(8, 0.0),
544  row.get<double>(9, 0.0),
545  row.get<double>(10, 0.0),
546  row.get<double>(11, 0.0)
547  )
548  ),
550  new Quaternion(
551  row.get<double>(12, 0.0),
552  row.get<double>(13, 0.0),
553  row.get<double>(14, 0.0),
554  row.get<double>(15, 0.0)
555  )
556  ),
558  new Quaternion(
559  row.get<double>(16, 0.0),
560  row.get<double>(17, 0.0),
561  row.get<double>(18, 0.0),
562  row.get<double>(19, 0.0)
563  )
564  ),
565  row.get<double>(0, 0.0),
566  row.get<std::string>(1, ""),
567  row.get<std::string>(2, ""),
568  row.get<std::string>(3, ""),
569  row.get<int>(20, 0)
570  )
571  );
572  vote->weight = row.get<int>(21, 1);
573  specifiers.push_back(vote);
574  }
575  voteSpecifierMap[objectType] = specifiers;
576  };
577 
578  return voteSpecifierMap;
579 }
580 
581 const ObjectToVoteMap TableHelper::getVoteSpecifiersForPatternAndObjects(const std::string& patternName, std::set<std::pair<std::string, std::string> > objects) const{
582 
583  ObjectToVoteMap voteSpecifierMap;
584  int patternId = getModelPatternId(patternName);
585 
586  for(std::pair<std::string, std::string> object : objects) {
587 
588  int objectId = getModelObjectTypeId(object.first);
589  std::string observedId = object.second;
590 
591  rowset<row> rs = ((*sqlite).prepare<<
592  "SELECT radius, name, type, observedId, qw, qx, qy, qz, qw2, qx2, qy2, qz2, qpw, qpx, qpy, qpz, qpw2, qpx2, qpy2, qpz2, trackIndex, v.weight "<<
593  "FROM `model_votes` AS v "<<
594  "JOIN `model_objects` AS o ON v.objectId = o.id "<<
595  "JOIN `model_patterns` AS p ON v.patternId = p.id "<<
596  "WHERE objectId = :objectId AND patternId = :patternId AND observedId = :observedId;",
597  use(objectId), use(patternId), use(observedId)
598  );
599  std::vector<VoteSpecifierPtr> specifiers;
600  BOOST_FOREACH(row const& row, rs) {
601  VoteSpecifierPtr vote(
602  new VoteSpecifier(
604  new Quaternion(
605  row.get<double>(4, 0.0),
606  row.get<double>(5, 0.0),
607  row.get<double>(6, 0.0),
608  row.get<double>(7, 0.0)
609  )
610  ),
612  new Quaternion(
613  row.get<double>(8, 0.0),
614  row.get<double>(9, 0.0),
615  row.get<double>(10, 0.0),
616  row.get<double>(11, 0.0)
617  )
618  ),
620  new Quaternion(
621  row.get<double>(12, 0.0),
622  row.get<double>(13, 0.0),
623  row.get<double>(14, 0.0),
624  row.get<double>(15, 0.0)
625  )
626  ),
628  new Quaternion(
629  row.get<double>(16, 0.0),
630  row.get<double>(17, 0.0),
631  row.get<double>(18, 0.0),
632  row.get<double>(19, 0.0)
633  )
634  ),
635  row.get<double>(0, 0.0),
636  row.get<std::string>(1, ""),
637  row.get<std::string>(2, ""),
638  row.get<std::string>(3, ""),
639  row.get<int>(20, 0)
640  )
641  );
642 
643  vote->weight = row.get<int>(21, 1);
644  specifiers.push_back(vote);
645  }
646 
647  if(!specifiers.empty())
648  voteSpecifierMap[object.first][object.second] = specifiers;
649  else
650  std::cout << "Object (" << object.first << "," << object.second << ") and pattern " << patternName << " is an invalid combination." << std::endl;
651 
652  }
653 
654  return voteSpecifierMap;
655 
656 }
657 
658 const std::set<std::pair<std::string, std::string> > TableHelper::getObjectTypesAndIdsBelongingToPattern(const std::string& patternName) const {
659 
660  int patternId = getModelPatternId(patternName);
661 
662  rowset<row> rs = ((*sqlite).prepare<<"SELECT DISTINCT type, observedId " <<
663  "FROM `model_votes` AS v "<<
664  "JOIN `model_objects` AS o ON v.objectId = o.id "<<
665  "JOIN `model_patterns` AS p ON v.patternId = p.id "<<
666  "WHERE patternId = :patternId;",
667  use(patternId));
668 
669  std::set<std::pair<std::string, std::string> > typesAndIds;
670  BOOST_FOREACH(row const& row, rs) {
671  typesAndIds.insert(std::make_pair(row.get<std::string>(0), row.get<std::string>(1)));
672  }
673  return typesAndIds;
674 }
675 
676 const std::vector<std::pair<int, ObjectPtr>> TableHelper::getAllMarkerObjects() {
677  rowset<row> rs = ((*sqlite).prepare << "SELECT type, observedId, px, py, pz, qw, qx, qy, qz, resourcePath, id FROM `recorded_objects` where type like 'marker_%'");
678  std::vector<std::pair<int, ObjectPtr>> result;
679  for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it) {
680  row const& row = *it;
681  boost::filesystem::path meshPath = row.get<std::string>(9,"");
682  ObjectPtr object(
683  new Object(
684  row.get<std::string>(0, ""),
685  PosePtr(new Pose(
686  new Point(
687  row.get<double>(2, 0.0),
688  row.get<double>(3, 0.0),
689  row.get<double>(4, 0.0)
690  ),
691  new Quaternion(
692  row.get<double>(5, 0.0),
693  row.get<double>(6, 0.0),
694  row.get<double>(7, 0.0),
695  row.get<double>(8, 0.0)
696  )
697  )
698  ),
699  row.get<std::string>(1, "")
700  )
701  );
702  object->ressourcePath = meshPath;
703  result.push_back(std::make_pair(row.get<int>(10, 0), object));
704 }
705 return result;
706 }
707 
709  QuaternionPtr quat = object->pose->quat;
710  double qw = quat->eigen.w();
711  double qx = quat->eigen.x();
712  double qy = quat->eigen.y();
713  double qz = quat->eigen.z();
714  transaction trans(*sqlite);
715  (*sqlite)<< "UPDATE `recorded_poses` SET qw = :qw , qx = :qx , qy = :qy , qz = :qz WHERE id = :dbId;",
716  use(qw), use(qx), use(qy), use(qz), use(dbId);
717  trans.commit();
718  return dbId;
719 
720 }
721 }
722 
std::vector< std::string > getModelPatternNames() const
const_iterator begin() const
Definition: rowset.h:220
T get(std::size_t pos) const
Definition: row.h:66
const ObjectSetPtr getRecordedObjectSet(int setId) const
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
boost::shared_ptr< Quaternion > QuaternionPtr
Definition: Quaternion.hpp:39
int ensureModelPatternName(const std::string &patternName) const
const std::set< std::pair< std::string, std::string > > getObjectTypesAndIdsBelongingToPattern(const std::string &patternName) const
int insertModelPattern(const std::string &patternName) const
boost::unordered_map< std::pair< std::string, std::string >, int > objectIdMap_
int insertRecordedObjectIfNecessary(ObjectPtr object)
Definition: row.h:41
const std::set< std::pair< std::string, std::string > > getObjectTypesAndIdsFromRecordedObjects() const
SOCI_SQLITE3_DECL sqlite3_backend_factory const sqlite3
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
std::string patternName
boost::shared_ptr< RecordedPattern > RecordedPatternPtr
const std::set< std::pair< std::string, std::string > > getObjectTypesAndIdsFromModelObjects() const
const std::vector< std::pair< int, ObjectPtr > > getAllMarkerObjects()
const ObjectToVoteMap getVoteSpecifiersForPatternAndObjects(const std::string &patternName, std::set< std::pair< std::string, std::string > > objects) const
TableHelper(std::string dbfilename="record.sqlite")
Definition: TableHelper.cpp:32
const_iterator end() const
Definition: rowset.h:227
int ensureModelObjectType(const std::string &objectType) const
void dropModelTables() const
int ensureRecordedPatternName(const std::string &patternName) const
int getLastInsertId(const std::string &tablename) const
const std::set< std::string > getObjectTypes() const
void createTable(const std::string &tablename, const std::string &sql) const
std::map< std::string, std::map< std::string, std::vector< VoteSpecifierPtr > > > ObjectToVoteMap
Definition: TableHelper.hpp:39
void dropTable(const std::string &tablename) const
int insertRecordedObject(const boost::shared_ptr< Object > &o, int setId)
const PatternNameToPatternMap getPatternDefinitionsByName(const std::set< std::string > &patternNames) const
boost::shared_ptr< Pattern > PatternPtr
Definition: Pattern.hpp:36
const std::map< std::string, std::map< std::string, std::string > > getModelWeightsPerTypeAndId() const
boost::shared_ptr< VoteSpecifier > VoteSpecifierPtr
int getModelObjectTypeId(const std::string &objectType) const
int insertModelVoteSpecifier(const VoteSpecifierPtr &vote) const
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
void dropTables() const
boost::shared_ptr< session > sqlite
Definition: TableHelper.hpp:47
boost::shared_ptr< Pose > PosePtr
Definition: Pose.hpp:79
const std::map< std::string, boost::filesystem::path > getRessourcePaths() const
int getModelPatternId(const std::string &patternName) const
std::map< std::string, PatternPtr > PatternNameToPatternMap
Definition: TableHelper.hpp:37
const std::vector< int > getSetIds() const
void dropRecordTables() const
int insertModelObjectType(const std::string &objectType) const
std::vector< std::string > getRecordedPatternNames() const
bool recordDataExists() const
void createTablesIfNecessary() const
Definition: TableHelper.cpp:80
int insertRecordedPattern(const std::string &patternName) const
this namespace contains all generally usable classes.
bool modelDataExists() const
const std::set< std::string > getObjectsInPattern(std::string pattern_name)
Definition: TableHelper.cpp:70
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
Definition: use.h:43
int upsertModelPattern(const std::string &patternName, int expectedMaxWeight) const
std::map< std::string, std::vector< VoteSpecifierPtr > > ObjectTypeToVoteMap
Definition: TableHelper.hpp:36
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82
int getRecordedPatternId(const std::string &patternName) const
const ObjectTypeToVoteMap getVoteSpecifiersForObjectTypes(const std::set< std::string > &objectTypes) const
int updateObjectQuaternion(int dbId, ObjectPtr object)
const RecordedPatternPtr getRecordedPattern(const std::string &patternName) const
void createColumnsIfNecessary()
Definition: TableHelper.cpp:43


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:41