ed
write.cpp
Go to the documentation of this file.
2 
3 #include "ed/measurement.h"
4 #include "ed/entity.h"
6 #include "ed/io/json_writer.h"
7 #include "ed/logging.h"
8 
10 #include <tue/filesystem/path.h>
11 
12 #include <rgbd/serialization.h>
13 
14 #include <fstream>
15 
16 namespace ed
17 {
18 
19 // ----------------------------------------------------------------------------------------------------
20 
21 bool write(const std::string& filename, const Measurement& msr)
22 {
23  // save image
24  {
25  std::string filename_image = filename + ".rgbd";
26  std::ofstream f_out;
27  f_out.open(filename_image.c_str(), std::ifstream::binary);
28  if (f_out.is_open())
29  {
31  rgbd::serialize(*msr.image(), a_out);
32  }
33  else
34  {
35  std::cout << "Could not save to " << filename_image << std::endl;
36  }
37  }
38 
39  // save mask
40  {
41  std::string filename_mask = filename + ".mask";
42  std::ofstream f_out;
43  f_out.open(filename_mask.c_str(), std::ifstream::binary);
44  if (f_out.is_open())
45  {
47  ed::serialize(msr.imageMask(), a_out);
48  }
49  else
50  {
51  std::cout << "Could not save to " << filename_mask << std::endl;
52  }
53  }
54 
55  return true;
56 }
57 
58 // ----------------------------------------------------------------------------------------------------
59 
60 bool write(const std::string& filename, const Entity& e)
61 {
62  std::string filename_ext = filename + ".json";
63  std::ofstream f_out;
64  f_out.open(filename_ext.c_str());
65 
66  if (!f_out.is_open())
67  {
68  ed::log::error() << "Could not save to '" << filename_ext << "'" << std::endl;
69  return false;
70  }
71 
72  ed::io::JSONWriter w(f_out);
73 
74  w.writeValue("id", e.id().str());
75  w.writeValue("type", e.type());
76 
77  // Convex hull
78  const ed::ConvexHull& chull = e.convexHull();
79  if (!chull.points.empty())
80  {
81  w.writeGroup("convex_hull");
82  ed::serialize(chull, w);
83  w.endGroup();
84  }
85 
86  // Pose
87  if (e.has_pose())
88  {
89  w.writeGroup("pose");
90  ed::serialize(e.pose(), w);
91  w.endGroup();
92  }
93 
94  // RGBD Measurement
96  if (msr)
97  {
98  w.writeGroup("rgbd_measurement");
99 
100  // Get filename without path
101  std::string base_filename = tue::filesystem::Path(filename).filename();
102 
103  w.writeValue("image_file", base_filename + ".rgbd");
104  w.writeValue("mask_file", base_filename + ".mask");
105 
106  w.writeGroup("sensor_pose");
107  ed::serialize(msr->sensorPose(), w);
108  w.endGroup();
109 
110  write(filename, *msr);
111 
112  w.endGroup();
113  }
114 
115  w.finish();
116 
117  return true;
118 }
119 
120 }
ed::Entity::has_pose
bool has_pose() const
Definition: entity.h:115
fstream
std::string
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, float f)
Definition: json_writer.h:47
ed::Measurement::image
rgbd::ImageConstPtr image() const
Definition: measurement.h:23
ed::ConvexHull
Definition: convex_hull.h:11
entity.h
tue::filesystem::Path
ed::Entity::convexHull
const ConvexHull & convexHull() const
Definition: entity.h:75
ed::io::JSONWriter::writeGroup
void writeGroup(const std::string &name)
Definition: json_writer.h:27
tue::serialization::OutputArchive
std::cout
rgbd::serialize
bool serialize(const Image &image, tue::serialization::OutputArchive &a, RGBStorageType rgb_type=RGB_STORAGE_JPG, DepthStorageType depth_type=DEPTH_STORAGE_PNG)
ed::Entity::type
const TYPE & type() const
Definition: entity.h:40
ed::log::error
std::ostream & error()
Definition: logging.cpp:74
std::ofstream
measurement.h
std::string::c_str
T c_str(T... args)
ed::Entity
Definition: entity.h:30
serialization.h
ed::write
bool write(const std::string &filename, const Measurement &msr)
Definition: write.cpp:21
ed::io::JSONWriter::endGroup
void endGroup()
Definition: json_writer.h:37
ed::Measurement
Definition: measurement.h:11
ed::Entity::pose
const geo::Pose3D & pose() const
Definition: entity.h:97
std::ofstream::open
T open(T... args)
json_writer.h
tue::filesystem::Path::filename
std::string filename() const
ed::MeasurementConstPtr
shared_ptr< const Measurement > MeasurementConstPtr
Definition: types.h:33
ed::Measurement::imageMask
const ImageMask & imageMask() const
Definition: measurement.h:25
ed::io::JSONWriter::finish
void finish()
Definition: json_writer.h:170
path.h
std::endl
T endl(T... args)
ed::io::JSONWriter
Definition: json_writer.h:15
logging.h
serialization.h
ed
Definition: convex_hull.h:8
output_archive.h
ed::Entity::id
const UUID & id() const
Definition: entity.h:38
ed::serialize
void serialize(const geo::Pose3D &pose, ed::io::Writer &w)
Definition: serialization.cpp:150
write.h
std::ofstream::is_open
T is_open(T... args)
ed::Entity::lastMeasurement
MeasurementConstPtr lastMeasurement() const
Definition: entity.cpp:188
ed::UUID::str
const std::string & str() const
Definition: uuid.h:27
ed::ConvexHull::points
std::vector< geo::Vec2f > points
Definition: convex_hull.h:13