ed_sensor_integration
snapshot.h
Go to the documentation of this file.
1 #ifndef ED_SENSOR_INTEGRATION_TOOLS_SNAPSHOT_H_
2 #define ED_SENSOR_INTEGRATION_TOOLS_SNAPSHOT_H_
3 
5 #include <ed/world_model.h>
6 #include <ed/update_request.h>
8 #include <ed/io/json_reader.h>
9 
10 #include <exception>
11 
12 #include <tue/filesystem/crawler.h>
13 
14 #include <tue/config/read.h>
15 #include <tue/config/reader.h>
17 
18 #include <rgbd/image.h>
19 #include <rgbd/serialization.h>
20 
21 #include <vector>
22 
23 namespace ed
24 {
25 
32 
33 public:
34  ModelNotFoundException(const std::string model_name, const std::string message)
35  {
36  model_name_ = model_name;
37  message_ = message;
38  }
39  const char * what () const throw () {
40  return message_.c_str();
41  }
42 };
43 
47 struct Snapshot
48 {
51 };
52 
61 bool readImage(const std::string& filename, rgbd::ImagePtr& image, geo::Pose3D& sensor_pose)
62 {
63  tue::config::DataPointer meta_data;
64 
65  try
66  {
67  meta_data = tue::config::fromFile(filename);
68  }
70  {
71  std::cerr << "Could not open '" << filename << "'.\n\n" << e.what() << std::endl;
72  return false;
73  }
74 
75  tue::config::Reader r(meta_data);
76 
77  // Read image
78  std::string rgbd_filename;
79  if (r.value("rgbd_filename", rgbd_filename))
80  {
81  tue::filesystem::Path abs_rgbd_filename = tue::filesystem::Path(filename).parentPath().join(rgbd_filename);
82 
83  std::ifstream f_rgbd;
84  f_rgbd.open(abs_rgbd_filename.string().c_str(), std::ifstream::binary);
85 
86  if (!f_rgbd.is_open())
87  {
88  std::cerr << "Could not open '" << filename << "'." << std::endl;
89  return false;
90  }
91 
92  image.reset(new rgbd::Image);
93 
95  rgbd::deserialize(a_in, *image);
96  }
97 
98  // Read sensor pose
99  if (!ed::deserialize(r, "sensor_pose", sensor_pose))
100  {
101  std::cerr << "No field 'sensor_pose' specified." << std::endl;
102  return false;
103  }
104 
105  return true;
106 }
107 
116 {
118 
119  ed::models::ModelLoader model_loader;
120 
122  if (!model_loader.create("_root", model_name, req, error, true))
123  {
124  std::string message = "loadWorldModel: Model '" + model_name +
125  "' could not be loaded. ModelLoader error: " + error.str();
126  throw ModelNotFoundException(model_name.c_str(), message.c_str());
127  }
128 
129  ed::WorldModelPtr world_model_ptr = ed::make_shared<ed::WorldModel>();
130 
131  // Update world
132  world_model_ptr->update(req);
133 
134  return world_model_ptr;
135 }
136 
138 {
139 
140 public:
141 
143  {
144  if (path.isDirectory())
145  crawler.setRootPath(path);
146  else
148 
149  // load first snapshot
150  if (path.isRegularFile())
151  loadSnapshot(path);
152  else
153  loadNewSnapshot();
154  }
155 
156  inline Snapshot& current() { return snapshots[i_current]; }
157  inline Snapshot& getSnapshot(unsigned int i) { return snapshots[i]; }
158 
159  void previous()
160  {
161  if (i_current > 0)
162  --i_current;
163  }
164 
168  void next()
169  {
170  ++i_current;
171 
172  //load new image if size of vector is exceeded
173  if (i_current >= snapshots.size())
174  {
175  if (!loadNewSnapshot())
176  {
177  i_current = snapshots.size() - 1;
178  }
179  }
180  }
181 
187  {
188  bool file_found = false;
189  tue::filesystem::Path filename;
190 
191  while (crawler.nextPath(filename))
192  {
193  if (filename.extension() == ".json")
194  {
195  file_found = true;
196  break;
197  }
198  }
199 
200  if (!file_found)
201  return false;
202 
203  return loadSnapshot(filename);
204  }
205 
212  {
213  i_current = snapshots.size();
214  snapshots.push_back(Snapshot());
215  Snapshot& snapshot = snapshots.back();
216 
217  std::cout << "Loading " << filename << std::endl;
218  if (!readImage(filename.string(), snapshot.image, snapshot.sensor_pose))
219  {
220  std::cerr << "Could not read: " << filename << std::endl;
221  snapshots.pop_back();
222  return false;
223  }
224  return true;
225  }
226 
227 private:
228  uint i_current;
230 
232 };
233 
234 }
235 #endif // ED_SENSOR_INTEGRATION_TOOLS_SNAPSHOT_H_
read.h
ed::SnapshotCrawler::loadSnapshot
bool loadSnapshot(tue::filesystem::Path filename)
loadSnapshot, load snapshot data and extend the snapshots list.
Definition: snapshot.h:211
ed::UpdateRequest
std::string
std::shared_ptr< Image >
exception
ed::Snapshot
The Snapshot struct, of a camera image taken at a single point in time.
Definition: snapshot.h:47
ed::Snapshot::sensor_pose
geo::Pose3D sensor_pose
Definition: snapshot.h:50
update_request.h
tue::config::Reader::value
bool value(const std::string &name, T &value, const RequiredOrOptional=REQUIRED) const
tue::filesystem::Path
vector
tue::filesystem::Path::extension
std::string extension() const
tue::config::DataPointer
rgbd::deserialize
bool deserialize(tue::serialization::InputArchive &a, Image &image)
std::stringstream
ed::models::ModelLoader
tue::filesystem::Crawler::nextPath
bool nextPath(Path &path)
reader.h
std::cerr
ed::SnapshotCrawler::SnapshotCrawler
SnapshotCrawler(tue::filesystem::Path path)
Definition: snapshot.h:142
ed::SnapshotCrawler::crawler
tue::filesystem::Crawler crawler
Definition: snapshot.h:231
geo::Transform3T
tue::config::ParseException
crawler.h
ed::loadWorldModel
ed::WorldModelPtr loadWorldModel(const std::string &model_name)
Definition: snapshot.h:115
ed::SnapshotCrawler::snapshots
std::vector< Snapshot > snapshots
Definition: snapshot.h:229
tue::filesystem::Path::isDirectory
bool isDirectory() const
tue::filesystem::Crawler::setRootPath
void setRootPath(const Path &root_path)
tue::filesystem::Crawler
image
cv::Mat image
std::cout
ed::ModelNotFoundException::ModelNotFoundException
ModelNotFoundException(const std::string model_name, const std::string message)
Definition: snapshot.h:34
rgbd::Image
image.h
ed::SnapshotCrawler
Definition: snapshot.h:137
error
std::ostream & error()
tue::config::Reader
ed::SnapshotCrawler::next
void next()
next, proceed to next index of the snapshot list. If index is out of bounds, try to load a new snapsh...
Definition: snapshot.h:168
std::string::c_str
T c_str(T... args)
serialization.h
ed::SnapshotCrawler::previous
void previous()
Definition: snapshot.h:159
std::ifstream::open
T open(T... args)
ed::deserialize
bool deserialize(ed::io::Reader &r, ConvexHull &ch)
req
string req
tue::filesystem::Path::isRegularFile
bool isRegularFile() const
data_pointer.h
ed::ModelNotFoundException::message_
std::string message_
Definition: snapshot.h:31
ed::models::ModelLoader::create
bool create(const tue::config::DataConstPointer &data, const UUID &id_opt, const UUID &parent_id, UpdateRequest &req, std::stringstream &error, const std::string &model_path="", const geo::Pose3D &pose_offset=geo::Pose3D::identity())
ed::WorldModelPtr
shared_ptr< WorldModel > WorldModelPtr
ed::Snapshot::image
rgbd::ImagePtr image
Definition: snapshot.h:49
tue::serialization::InputArchive
world_model.h
std::endl
T endl(T... args)
ed::ModelNotFoundException::model_name_
std::string model_name_
Definition: snapshot.h:30
ed::readImage
bool readImage(const std::string &filename, rgbd::ImagePtr &image, geo::Pose3D &sensor_pose)
Definition: snapshot.h:61
ed::SnapshotCrawler::loadNewSnapshot
bool loadNewSnapshot()
loadNewSnapshot, find next file to load snapshot from and load snapshot.
Definition: snapshot.h:186
tue::config::ParseException::what
const char * what() const
ed::SnapshotCrawler::current
Snapshot & current()
Definition: snapshot.h:156
tue::config::fromFile
DataPointer fromFile(const char *filename, const ResolveConfig &resolve_config=ResolveConfig::defaultConfig())
ed
tue::filesystem::Path::string
const std::string & string() const
ed::SnapshotCrawler::getSnapshot
Snapshot & getSnapshot(unsigned int i)
Definition: snapshot.h:157
tue::filesystem::Path::parentPath
Path parentPath() const
ed::ModelNotFoundException
Definition: snapshot.h:29
std::ifstream::is_open
T is_open(T... args)
model_loader.h
ed::SnapshotCrawler::i_current
uint i_current
Definition: snapshot.h:228
tue::filesystem::Path::join
Path join(const Path &path) const
json_reader.h
ed::ModelNotFoundException::what
const char * what() const
Definition: snapshot.h:39
std::ifstream