00001 #ifndef KIDNAPPED_ROBOT_PLACE_DATABASE_H
00002 #define KIDNAPPED_ROBOT_PLACE_DATABASE_H
00003
00004 #include <sqlite3.h>
00005 #include <vocabulary_tree/generic_tree.h>
00006 #include <vocabulary_tree/database.h>
00007 #include <frame_common/frame.h>
00008 #include <tf/transform_datatypes.h>
00009 #include <limits>
00010
00011 namespace kidnapped_robot {
00012
00013 class PlaceDatabase
00014 {
00015 sqlite3 *persistent_db_;
00016 vt::GenericTree voctree_;
00017 vt::Database document_db_;
00018 typedef std::map<vt::DocId, int64_t> IdMap;
00019 IdMap doc_to_place_id_;
00020
00021 sqlite3_stmt *insert_stmt_;
00022 int map_pose_param_index_, optical_transform_param_index_, camera_param_index_;
00023 int disparities_param_index_, keypoints_param_index_;
00024 mutable sqlite3_stmt *select_transforms_stmt_, *select_frame_stmt_, *select_spatial_stmt_, *select_images_stmt_;
00025
00026 public:
00027 PlaceDatabase();
00028
00029 PlaceDatabase(const std::string& db_file, const std::string& tree_file, const std::string& weights_file);
00030
00031 ~PlaceDatabase();
00032
00033 void load(const std::string& db_file, const std::string& tree_file, const std::string& weights_file);
00034
00035 static const int64_t AUTO_ID = LONG_MIN;
00036
00037 int64_t add(ros::Time stamp, const tf::Pose& pose_in_map, const tf::Transform& optical_transform,
00038 const frame_common::Frame& frame, int64_t id = AUTO_ID);
00039
00040 void findMatching(const frame_common::Frame& query_frame, size_t N, vt::Matches& matches) const;
00041
00042 void findInRegion(cv::Rect_<double> bounding_box, std::vector<int64_t>& ids) const;
00043
00044 void getFrame(int64_t id, frame_common::Frame& frame) const;
00045
00046 void getTransforms(int64_t id, tf::Pose& pose_in_map, tf::Transform& optical_transform) const;
00047
00048 void getImages(int64_t id, cv::Mat& left, cv::Mat& right);
00049
00050 sqlite3* getSqlite() { return persistent_db_; }
00051
00052
00053
00054 protected:
00055 void checkErr(int returned_code, const char* msg, int expected_code = SQLITE_OK) const;
00056
00057 void loadPersistentDatabase(const std::string& db_file);
00058
00059 void loadDocumentDatabase(const std::string& weights_file);
00060
00061 void prepareReusedStatements();
00062
00063 void extractTransform(btTransform& xfm, int start_index) const;
00064
00065 void extractImage(cv::Mat& image, int col) const;
00066
00067 void bindTransform(const btTransform& xfm, int param_index);
00068
00069 void bindCameraParams(const frame_common::CamParams& cam);
00070
00071 void bindImage(int col, const cv::Mat& image);
00072
00073 template<typename T> void bindVector(sqlite3_stmt *stmt, int col, const std::vector<T>& vec);
00074
00075
00076 };
00077
00078 }
00079
00080 #endif