23 #include <boost/foreach.hpp> 24 #include <boost/filesystem.hpp> 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)
48 std::vector<std::string> columnsAlreadyExisting;
49 std::stringstream infoSs;
50 infoSs <<
"pragma table_info('" << tablesToColumns.first <<
"');";
54 columnsAlreadyExisting.push_back(
row.
get<std::string>(1));
56 for(
const std::pair<std::string,std::string>& col : tablesToColumns.second)
58 if(std::find(columnsAlreadyExisting.begin(), columnsAlreadyExisting.end(), col.first)
59 == columnsAlreadyExisting.end())
61 std::stringstream alterSs;
62 alterSs <<
"ALTER TABLE " << tablesToColumns.first <<
" ADD " << col.first <<
63 " " << col.second <<
";";
64 (*sqlite) << alterSs.str();
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 <<
"';");
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));
81 std::set<std::string> tables;
83 rowset<row> rs = (*sqlite).prepare<<
"SELECT tbl_name FROM sqlite_master WHERE type = 'table';";
86 tables.insert(row.
get<std::string>(0));
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";
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()) {
114 (*sqlite).once<<
"CREATE TABLE `"<<tablename<<
"` ("<<sql<<
");";
116 std::cerr<<
"SQL error "<<e.what()<<std::endl;
123 (*sqlite).once<<
"DROP TABLE `"<<tablename<<
"`;";
125 std::cerr<<
"SQL error "<<e.what()<<std::endl;
158 (*sqlite) <<
"SELECT COUNT(*) FROM `recorded_patterns`;",
into(count);
166 (*sqlite) <<
"SELECT COUNT(*) FROM `model_patterns`;",
into(count);
174 (*sqlite)<<
"SELECT id FROM `"<<tablename<<
"` ORDER BY ID DESC LIMIT 1;",
into(
id);
176 std::cerr<<
"SQL error "<<e.what()<<std::endl;
188 std::pair<std::string, std::string> objectIdentifier = std::make_pair(object->type, object->observedId);
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) {
202 (*sqlite) <<
"INSERT INTO `recorded_objects` (id, type, observedId, resourcePath) values (:id, :type, :oid, :resourcePath);",
205 use(object->observedId),
206 use(object->ressourcePath.string());
208 return lastInsertId + 1;
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);",
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());
230 std::vector<boost::shared_ptr<Object> > objects = os->objects;
235 (*sqlite) <<
"INSERT INTO `recorded_sets` (patternId) VALUES (:patternId);",
use(patternId);
237 for (
size_t i = 0; i < objects.size(); i++) {
247 (*sqlite) <<
"INSERT INTO `recorded_patterns` (name) VALUES (:patternName);",
use(patternName);
257 std::vector<std::string> names;
259 for (std::string& name : rs) {
260 names.push_back(name);;
268 (*sqlite) <<
"SELECT id FROM `recorded_patterns` WHERE name = :name;",
into(
id, ind),
use(patternName);
269 if ((*sqlite).got_data() && ind ==
i_ok) {
277 boost::shared_ptr<RecordedPattern> pattern;
280 if (patternId != 0) {
283 rowset<int> rs = ((*sqlite).prepare<<
"SELECT id FROM `recorded_sets` WHERE patternId = :patternId;",
use(patternId));
293 std::vector<int> setIds;
294 rowset<int> rs = ((*sqlite).prepare<<
"SELECT DISTINCT setId FROM `recorded_poses`;");
297 setIds.push_back(*it);
303 boost::shared_ptr<ObjectSet> s(
new ObjectSet());
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;",
312 boost::filesystem::path meshPath = row.
get<std::string>(9,
"");
315 row.
get<std::string>(0,
""),
318 row.
get<
double>(2, 0.0),
319 row.
get<
double>(3, 0.0),
320 row.
get<
double>(4, 0.0)
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)
330 row.
get<std::string>(1,
"")
333 object->ressourcePath = meshPath;
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);",
352 use(vote->observedId),
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),
387 (*sqlite) <<
"INSERT INTO `model_patterns` (name) VALUES (:patternName);",
use(patternName);
393 if (patternId == 0) {
394 (*sqlite) <<
"INSERT INTO `model_patterns` (name, expectedMaxWeight) VALUES (:patternName, :expectedMaxWeight);",
396 use(expectedMaxWeight);
398 (*sqlite) <<
"REPLACE INTO `model_patterns` (id, name, expectedMaxWeight) VALUES (:id, :patternName, :expectedMaxWeight);",
401 use(expectedMaxWeight);
407 (*sqlite) <<
"INSERT INTO `model_objects` (type) VALUES (:objectType);",
use(objectType);
414 (*sqlite) <<
"SELECT id FROM `model_patterns` WHERE name = :name;",
into(
id, ind),
use(patternName);
415 if ((*sqlite).got_data() && ind ==
i_ok) {
425 (*sqlite) <<
"SELECT id FROM `model_objects` WHERE type = :objectType;",
into(
id, ind),
use(objectType);
426 if ((*sqlite).got_data() && ind ==
i_ok) {
434 std::vector<std::string> names;
436 for (std::string& name : rs) {
437 names.push_back(name);;
445 BOOST_FOREACH(std::string
patternName, patternNames) {
446 int expectedMaxWeight;
449 "SELECT expectedMaxWeight "<<
450 "FROM `model_patterns` "<<
451 "WHERE name = :patternName LIMIT 1;",
452 into(expectedMaxWeight, indEOC),
454 if ((*sqlite).got_data() && indEOC ==
i_ok) {
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));
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;
479 return types_to_ressource_paths_map;
483 std::map<std::string, std::map<std::string, std::string>> modelWeightsPerTypeAndId;
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);
490 if (std::string(e.what()).find(
"no such table: model_weight") != std::string::npos) {
496 return modelWeightsPerTypeAndId;
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)));
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)));
519 BOOST_FOREACH(std::string objectType, objectTypes) {
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;",
529 std::vector<VoteSpecifierPtr> specifiers;
530 BOOST_FOREACH(
row const&
row, rs) {
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)
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)
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)
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)
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,
""),
572 vote->weight = row.
get<
int>(21, 1);
573 specifiers.push_back(vote);
575 voteSpecifierMap[objectType] = specifiers;
578 return voteSpecifierMap;
586 for(std::pair<std::string, std::string>
object : objects) {
589 std::string observedId =
object.second;
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)
599 std::vector<VoteSpecifierPtr> specifiers;
600 BOOST_FOREACH(
row const&
row, rs) {
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)
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)
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)
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)
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,
""),
643 vote->weight = row.
get<
int>(21, 1);
644 specifiers.push_back(vote);
647 if(!specifiers.empty())
648 voteSpecifierMap[
object.first][
object.second] = specifiers;
650 std::cout <<
"Object (" <<
object.first <<
"," <<
object.second <<
") and pattern " << patternName <<
" is an invalid combination." << std::endl;
654 return voteSpecifierMap;
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;",
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)));
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;
681 boost::filesystem::path meshPath = row.
get<std::string>(9,
"");
684 row.
get<std::string>(0,
""),
687 row.
get<
double>(2, 0.0),
688 row.
get<
double>(3, 0.0),
689 row.
get<
double>(4, 0.0)
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)
699 row.
get<std::string>(1,
"")
702 object->ressourcePath = meshPath;
703 result.push_back(std::make_pair(row.
get<
int>(10, 0),
object));
710 double qw = quat->eigen.w();
711 double qx = quat->eigen.x();
712 double qy = quat->eigen.y();
713 double qz = quat->eigen.z();
715 (*sqlite)<<
"UPDATE `recorded_poses` SET qw = :qw , qx = :qx , qy = :qy , qz = :qz WHERE id = :dbId;",
std::vector< std::string > getModelPatternNames() const
const_iterator begin() const
T get(std::size_t pos) const
const ObjectSetPtr getRecordedObjectSet(int setId) const
details::into_container< T, details::no_indicator > into(T &t)
boost::shared_ptr< Quaternion > QuaternionPtr
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)
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)
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")
const_iterator end() const
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
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
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
boost::shared_ptr< session > sqlite
boost::shared_ptr< Pose > PosePtr
const std::map< std::string, boost::filesystem::path > getRessourcePaths() const
int getModelPatternId(const std::string &patternName) const
std::map< std::string, PatternPtr > PatternNameToPatternMap
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
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)
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
int upsertModelPattern(const std::string &patternName, int expectedMaxWeight) const
std::map< std::string, std::vector< VoteSpecifierPtr > > ObjectTypeToVoteMap
boost::shared_ptr< Object > ObjectPtr
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()