26 #include "glog/logging.h" 35 void WriteBinaryPlyHeader(
const bool has_color,
const bool has_intensities,
36 const int64 num_points,
37 FileWriter*
const file_writer) {
38 const std::string color_header = !has_color ?
"" 39 :
"property uchar red\n" 40 "property uchar green\n" 41 "property uchar blue\n";
42 const std::string intensity_header =
43 !has_intensities ?
"" :
"property float intensity\n";
44 std::ostringstream stream;
46 <<
"format binary_little_endian 1.0\n" 47 <<
"comment generated by Cartographer\n" 48 <<
"element vertex " << std::setw(15) << std::setfill(
'0')
50 <<
"property float x\n" 51 <<
"property float y\n" 52 <<
"property float z\n" 53 << color_header << intensity_header <<
"end_header\n";
54 const std::string out = stream.str();
55 CHECK(file_writer->WriteHeader(out.data(), out.size()));
58 void WriteBinaryPlyPointCoordinate(
const Eigen::Vector3f& point,
59 FileWriter*
const file_writer) {
62 memcpy(buffer, &point[0],
sizeof(
float));
63 memcpy(buffer + 4, &point[1],
sizeof(
float));
64 memcpy(buffer + 8, &point[2],
sizeof(
float));
65 CHECK(file_writer->Write(buffer, 12));
68 void WriteBinaryIntensity(
const float intensity,
69 FileWriter*
const file_writer) {
71 CHECK(file_writer->Write(reinterpret_cast<const char*>(&intensity),
75 void WriteBinaryPlyPointColor(
const Uint8Color& color,
76 FileWriter*
const file_writer) {
77 CHECK(file_writer->Write(reinterpret_cast<const char*>(color.data()),
83 std::unique_ptr<PlyWritingPointsProcessor>
88 return common::make_unique<PlyWritingPointsProcessor>(
89 file_writer_factory(dictionary->
GetString(
"filename")), next);
97 file_(std::move(file_writer)) {}
101 CHECK(
file_->Close()) <<
"Closing PLY file_writer failed.";
108 LOG(FATAL) <<
"PLY generation must be configured to occur after any " 109 "stages that require multiple passes.";
115 if (batch->points.empty()) {
126 CHECK_EQ(batch->points.size(), batch->colors.size())
127 <<
"First PointsBatch had colors, but encountered one without. " 132 CHECK_EQ(batch->points.size(), batch->intensities.size())
133 <<
"First PointsBatch had intensities, but encountered one without. " 138 for (
size_t i = 0; i < batch->points.size(); ++i) {
139 WriteBinaryPlyPointCoordinate(batch->points[i],
file_.get());
144 WriteBinaryIntensity(batch->intensities[i],
file_.get());
virtual void Process(std::unique_ptr< PointsBatch > points_batch)=0
PointsProcessor *const next_
std::unique_ptr< FileWriter > file_
std::function< std::unique_ptr< FileWriter >(const std::string &filename)> FileWriterFactory
std::string GetString(const std::string &key)
PlyWritingPointsProcessor(std::unique_ptr< FileWriter > file_writer, PointsProcessor *next)
Uint8Color ToUint8Color(const FloatColor &color)
void Process(std::unique_ptr< PointsBatch > batch) override
std::array< uint8, 3 > Uint8Color
virtual FlushResult Flush()=0
FlushResult Flush() override
static std::unique_ptr< PlyWritingPointsProcessor > FromDictionary(const FileWriterFactory &file_writer_factory, common::LuaParameterDictionary *dictionary, PointsProcessor *next)