ed
read.cpp
Go to the documentation of this file.
2 
3 #include "ed/measurement.h"
5 
6 #include <rgbd/image.h>
7 #include <rgbd/serialization.h>
8 
10 
11 #include <fstream>
12 
13 #include "ed/io/json_reader.h"
14 
15 #include "ed/entity.h"
16 #include "ed/update_request.h"
17 #include "ed/logging.h"
18 #include "ed/convex_hull_calc.h"
19 
20 #include <tue/filesystem/path.h>
21 
22 namespace ed
23 {
24 
25 namespace
26 {
27 
28 rgbd::ImagePtr readRGBDImage(const std::string& filename)
29 {
31 
32  std::ifstream f_in;
33  f_in.open(filename.c_str(), std::ifstream::binary);
34 
35  if (!f_in.is_open())
36  {
37  std::cout << "Could not open '" << filename << "'." << std::endl;
38  return rgbd::ImagePtr();
39  }
40 
42  rgbd::deserialize(a_in, *image);
43 
44  return image;
45 }
46 
47 bool readImageMask(const std::string& filename, ed::ImageMask& mask)
48 {
49  std::ifstream f_in;
50  f_in.open(filename.c_str(), std::ifstream::binary);
51 
52  if (!f_in.is_open())
53  {
54  std::cout << "Could not open '" << filename << "'." << std::endl;
55  return false;
56  }
57 
59  ed::deserialize(a_in, mask);
60 
61  return true;
62 }
63 
64 }
65 
66 // ----------------------------------------------------------------------------------------------------
67 
68 bool read(const std::string& filename, Measurement& msr)
69 {
70  // Read image
71  rgbd::ImagePtr image = readRGBDImage(filename + ".rgbd");
72 
73  // Read mask
74  ed::ImageMask mask;
75  readImageMask(filename + ".mask", mask);
76 
77  msr = Measurement(image, mask, geo::Pose3D::identity()); // TODO: read pose
78 
79  return true;
80 }
81 
82 // ----------------------------------------------------------------------------------------------------
83 
84 bool readEntity(const std::string& filename, UpdateRequest& req)
85 {
86  std::ifstream f_in;
87  f_in.open(filename.c_str());
88 
89  if (!f_in.is_open())
90  {
91  std::cout << "Could not open '" << filename << "'." << std::endl;
92  return false;
93  }
94 
95  std::stringstream buffer;
96  buffer << f_in.rdbuf();
97  std::string str = buffer.str();
98 
99  io::JSONReader r(str.c_str());
100 
101  // ID
102  ed::UUID id;
103  std::string id_str;
104  if (r.readValue("id", id_str))
105  id = id_str;
106  else
107  id = Entity::generateID();
108 
109  // Type
110  std::string type;
111  if (r.readValue("type", type))
112  req.setType(id, type);
113 
114  // Pose
115  geo::Pose3D pose;
116  if (r.readGroup("pose"))
117  {
118  if (ed::deserialize(r, pose))
119  req.setPose(id, pose);
120  r.endGroup();
121  }
122 
123  // Convex hull
124  if (r.readGroup("convex_hull"))
125  {
126  ConvexHull chull;
127  r.readValue("z_min", chull.z_min);
128  r.readValue("z_max", chull.z_max);
129 
130  if (r.readArray("points"))
131  {
132  while(r.nextArrayItem())
133  {
134  chull.points.push_back(geo::Vec2f());
135  geo::Vec2f& p = chull.points.back();
136  r.readValue("x", p.x);
137  r.readValue("y", p.y);
138  }
139  r.endArray();
140  }
141 
144 
145  ed::log::warning() << "ed::readEntity: convex hull timestamp is set to 0." << std::endl;
146  req.setConvexHullNew(id, chull, pose, 0);
147 
148  r.endGroup();
149  }
150 
151  // RGBD measurement
152  if (r.readGroup("rgbd_measurement"))
153  {
154  std::string rgbd_filename, mask_filename;
155  if (r.readValue("image_file", rgbd_filename) && r.readValue("mask_file", mask_filename))
156  {
157  std::string base_path = tue::filesystem::Path(filename).parentPath().string();
158 
159  rgbd::ImagePtr image = readRGBDImage(base_path + "/" + rgbd_filename);
160 
161  // Read mask
162  ed::ImageMask mask;
163  readImageMask(base_path + "/" + mask_filename, mask);
164 
165  geo::Pose3D sensor_pose;
166  if (r.readGroup("sensor_pose"))
167  {
168  ed::deserialize(r, sensor_pose);
169  r.endGroup();
170  }
171  else
172  {
173  log::error() << "Could not read sensor pose from rgbd measurement" << std::endl;
174  sensor_pose = geo::Pose3D::identity();
175  }
176 
177  MeasurementPtr msr(new Measurement(image, mask, sensor_pose));
178 
179  req.addMeasurement(id, msr);
180  }
181 
182  r.endGroup();
183  }
184 
185  return true;
186 }
187 
188 }
convex_hull_calc.h
ed::ConvexHull::z_min
float z_min
Definition: convex_hull.h:16
std::ifstream::rdbuf
T rdbuf(T... args)
ed::UpdateRequest
Definition: update_request.h:24
input_archive.h
fstream
std::string
std::shared_ptr
ed::ConvexHull
Definition: convex_hull.h:11
ed::convex_hull::calculateEdgesAndNormals
void calculateEdgesAndNormals(ConvexHull &chull)
Definition: convex_hull_calc.cpp:111
ed::MeasurementPtr
shared_ptr< Measurement > MeasurementPtr
Definition: types.h:31
entity.h
ed::deserialize
bool deserialize(io::Reader &r, UpdateRequest &req)
Definition: serialization.cpp:37
geo::Vec2T::y
T y
tue::filesystem::Path
geo::Transform3T::identity
static Transform3T identity()
ed::io::JSONReader
Definition: json_reader.h:16
rgbd::deserialize
bool deserialize(tue::serialization::InputArchive &a, Image &image)
std::stringstream
ed::io::JSONReader::nextArrayItem
bool nextArrayItem()
Definition: json_reader.cpp:185
ed::io::JSONReader::readValue
bool readValue(const std::string &, float &f)
Definition: json_reader.cpp:214
geo::Vec2T::x
T x
ed::io::JSONReader::readArray
bool readArray(const std::string &name)
Definition: json_reader.cpp:150
geo::Transform3T
ed::io::JSONReader::readGroup
bool readGroup(const std::string &name)
Definition: json_reader.cpp:128
image
cv::Mat image
Definition: view_model.cpp:42
std::cout
rgbd::Image
update_request.h
image.h
ed::log::error
std::ostream & error()
Definition: logging.cpp:74
measurement.h
json_reader.h
std::string::c_str
T c_str(T... args)
serialization.h
ed::log::warning
std::ostream & warning()
Definition: logging.cpp:52
ed::UpdateRequest::setPose
void setPose(const UUID &id, const geo::Pose3D &pose)
Definition: update_request.h:132
ed::Measurement
Definition: measurement.h:11
ed::UUID
Definition: uuid.h:10
std::ifstream::open
T open(T... args)
ed::UpdateRequest::setConvexHullNew
void setConvexHullNew(const UUID &id, const ed::ConvexHull &convex_hull, const geo::Pose3D &pose, double time, std::string source="")
Definition: update_request.h:87
ed::UpdateRequest::setType
void setType(const UUID &id, const std::string &type)
Definition: update_request.h:106
tue::serialization::InputArchive
ed::Entity::generateID
static UUID generateID()
Definition: entity.cpp:198
rgbd::ImagePtr
std::shared_ptr< Image > ImagePtr
ed::io::JSONReader::endArray
bool endArray()
Definition: json_reader.cpp:165
path.h
ed::io::JSONReader::endGroup
bool endGroup()
Definition: json_reader.cpp:141
std::endl
T endl(T... args)
logging.h
ed::readEntity
bool readEntity(const std::string &filename, UpdateRequest &req)
Definition: read.cpp:84
ed::UpdateRequest::addMeasurement
void addMeasurement(const UUID &id, const MeasurementConstPtr &m)
Definition: update_request.h:35
ed::ImageMask
Definition: mask.h:17
serialization.h
ed
Definition: convex_hull.h:8
std::stringstream::str
T str(T... args)
tue::filesystem::Path::string
const std::string & string() const
tue::filesystem::Path::parentPath
Path parentPath() const
ed::ConvexHull::z_max
float z_max
Definition: convex_hull.h:16
ed::read
bool read(const std::string &filename, Measurement &msr)
Definition: read.cpp:68
ed::convex_hull::calculateArea
void calculateArea(ConvexHull &c)
calculateArea calculate the area of the ConvexHull in xy-plane.
Definition: convex_hull_calc.cpp:221
std::ifstream::is_open
T is_open(T... args)
geo::Vec2T
read.h
std::ifstream
ed::ConvexHull::points
std::vector< geo::Vec2f > points
Definition: convex_hull.h:13