GSLAM  3.0.0
Dataset.h
1 // GSLAM - A general SLAM framework and benchmark
2 // Copyright 2018 PILAB Inc. All rights reserved.
3 // https://github.com/zdzhaoyong/GSLAM
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: zd5945@126.com (Yong Zhao)
30 //
31 // Dataset is used to provide source frames for SLAM plugins
32 
33 #ifndef GSLAM_DATASET_H
34 #define GSLAM_DATASET_H
35 #include "Svar.h"
36 #include "Registry.h"
37 #include "Messenger.h"
38 #include "Timer.h"
39 #include "Map.h"
40 
41 namespace GSLAM{
42 
43 class Dataset;
44 typedef std::shared_ptr<Dataset> DatasetPtr;
45 
46 /**
47  @brief The Dataset class
48 
49  Here is a little demo to implement an OpenCV monocular Dataset
50  @code
51 #include <opencv2/opencv.hpp>
52 #include <GSLAM/core/GSLAM.h>
53 
54 using namespace GSLAM;
55 using namespace std;
56 
57 class DatasetOpenCVMono : public GSLAM::Dataset
58 {
59 public:
60  DatasetOpenCVMono(){}
61  virtual std::string type() const{return "DatasetOpenCVMono";}
62 
63  virtual bool open(const std::string& dataset){
64  var.parseFile(dataset.c_str());
65 
66  if(!video.open(var.get<std::string>("video","")) return false;
67  camera=Camera(var.get<std::vector<double>>("camera_paras",{}));
68  if(!camera.isValid()) return false;
69  _name=dataset;
70  frameId=1;
71  return true;
72  }
73 
74  virtual bool isOpened(){return video.isOpened()&&camera.isValid();}
75 
76  virtual FramePtr grabFrame(){
77  double timestamp=GSLAM::TicToc::timestamp();
78  cv::Mat img;
79  video>>img;
80  return FramePtr(new FrameMono(frameId++,timestamp,gimg,camera,IMAGE_BGRA));
81  }
82 
83  GSLAM::FrameID frameId;
84  cv::VideoCapture video;
85  GSLAM::Camera camera;
86 };
87 
88 GSLAM_REGISTER_DATASET(DatasetOpenCVMono,cvmono)
89  @endcode
90 
91  Compile this file as a shared library with name "gslamDB_cvmono", or compile this file along with the excutable file.
92  It would be very easy to use the dataset:
93  @code
94  Dataset dataset("file_path.cvmono");
95  if(dataset.isOpened())
96  auto fr=dataset.grabFrame();// obtain a frame
97  @endcode
98 */
99 
100 
101 class Dataset : public GSLAM::GObject
102 {
103 public:
104  typedef std::vector<std::string> StrVec;
105  Dataset():_name("Untitled"){}
106  Dataset(const std::string& dataset){open(dataset);}
107  virtual ~Dataset(){}
108 
109  std::string name() const{if(_impl) return _impl->type();else return _name;}
110  virtual std::string type() const{if(_impl) return _impl->type();else return "Dataset";}
111  virtual bool isOpened(){if(_impl) return _impl->isOpened();else return false;}
112  virtual FramePtr grabFrame(){if(_impl) return _impl->grabFrame();else return FramePtr();}
113 
114  virtual bool open(const std::string& dataset);
115  virtual bool isLive()const{
116  if(_impl) return _impl->isLive();
117  return false;
118  }
119 protected:
120  std::string _name;
121  DatasetPtr _impl;
122 };
123 
124 inline bool Dataset::open(const std::string& dataset){
125  DatasetPtr impl;
126  std::string extension;
127  // The input path could be dataset configuration file or just a folder path
128  size_t dotPosition=dataset.find_last_of('.');
129  if(dotPosition!=std::string::npos)// call by extension
130  {
131  extension=dataset.substr(dotPosition+1);
132  }
133  if(extension.empty()) {
134  LOG(ERROR)<<"Can't open dataset without extension.";
135  return false;
136  }
137 
138  Svar localImpl=svar["gslam"]["datasets"][extension];
139  if(localImpl.isFunction()){
140  impl=DatasetPtr(localImpl().castAs<Dataset*>());
141  }
142 
143  if(!impl)
144  {
145  Svar plugin=Registry::load("gslamDB_"+extension);
146  if(plugin.isUndefined()){
147  LOG(ERROR)<<"Can't load plugin gslamDB_"<<extension<<".";
148  return false;
149  }
150  Svar pluginImpl=plugin["gslam"]["datasets"][extension];
151  if(!pluginImpl.isFunction())
152  {
153  LOG(ERROR)<<"Plugin gslamDB_"<<extension
154  <<" has no function gslam.datasets."<<extension;
155  return false;
156  }
157  impl=DatasetPtr(pluginImpl().castAs<Dataset*>());
158  }
159 
160  if(impl) {_impl=impl;return _impl->open(dataset);}
161  return false;
162 }
163 
164 }
165 
166 #endif // VIDEOREADER_H
Definition: Map.h:102
Definition: Camera.h:45
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages.
Definition: Svar.h:561
The Dataset class.
Definition: Dataset.h:101