PointCloudPlyWriter.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
20 // -- END LICENSE BLOCK ------------------------------------------------
21 
24 
25 #include <fstream>
26 #include <iostream>
27 
28 namespace visionary {
29 
30 bool PointCloudPlyWriter::WriteFormatPLY(const char* filename,
31  const std::vector<PointXYZ>& points,
32  bool useBinary)
33 {
34  return WriteFormatPLY(
35  filename, points, std::vector<uint32_t>(), std::vector<uint16_t>(), useBinary);
36 }
37 
38 bool PointCloudPlyWriter::WriteFormatPLY(const char* filename,
39  const std::vector<PointXYZ>& points,
40  const std::vector<uint32_t>& rgbaMap,
41  bool useBinary)
42 {
43  return WriteFormatPLY(filename, points, rgbaMap, std::vector<uint16_t>(), useBinary);
44 }
45 
46 bool PointCloudPlyWriter::WriteFormatPLY(const char* filename,
47  const std::vector<PointXYZ>& points,
48  const std::vector<uint16_t>& intensityMap,
49  bool useBinary)
50 {
51  return WriteFormatPLY(filename, points, std::vector<uint32_t>(), intensityMap, useBinary);
52 }
53 
54 bool PointCloudPlyWriter::WriteFormatPLY(const char* filename,
55  const std::vector<PointXYZ>& points,
56  const std::vector<uint32_t>& rgbaMap,
57  const std::vector<uint16_t>& intensityMap,
58  bool useBinary)
59 {
60  bool success = true;
61 
62  bool hasColors = points.size() == rgbaMap.size();
63  bool hasIntensities = points.size() == intensityMap.size();
64 
65  std::ofstream stream;
66 
67  // Open file
68  stream.open(filename,
69  useBinary ? (std::ios_base::out | std::ios_base::binary) : std::ios_base::out);
70 
71  if (stream.is_open())
72  {
73  // Write header
74  stream << "ply\n";
75  stream << "format " << (useBinary ? "binary_little_endian" : "ascii") << " 1.0\n";
76  stream << "element vertex " << points.size() << "\n";
77  stream << "property float x\n";
78  stream << "property float y\n";
79  stream << "property float z\n";
80  if (hasColors)
81  {
82  stream << "property uchar red\n";
83  stream << "property uchar green\n";
84  stream << "property uchar blue\n";
85  }
86  if (hasIntensities)
87  {
88  stream << "property float intensity\n";
89  }
90  stream << "end_header\n";
91 
92  if (useBinary == false)
93  {
94  // Write all points
95  for (int i = 0; i < static_cast<int>(points.size()); i++)
96  {
97  PointXYZ point = points.at(i);
98  stream << point.x << " " << point.y << " " << point.z;
99 
100  if (hasColors)
101  {
102  const uint8_t* rgba = reinterpret_cast<const uint8_t*>(&rgbaMap.at(i));
103  stream << " " << static_cast<uint32_t>(rgba[0]) << " " << static_cast<uint32_t>(rgba[1])
104  << " " << static_cast<uint32_t>(rgba[2]);
105  }
106  if (hasIntensities)
107  {
108  float intensity = static_cast<float>(intensityMap.at(i)) / 65535.0f;
109  stream << " " << intensity;
110  }
111 
112  stream << "\n";
113  }
114  }
115  else
116  {
117  // Write all points
118  for (int i = 0; i < static_cast<int>(points.size()); i++)
119  {
120  PointXYZ point = points.at(i);
121  float x = nativeToLittleEndian(point.x);
122  float y = nativeToLittleEndian(point.y);
123  float z = nativeToLittleEndian(point.z);
124 
125  stream.write(reinterpret_cast<char*>(&x), 4);
126  stream.write(reinterpret_cast<char*>(&y), 4);
127  stream.write(reinterpret_cast<char*>(&z), 4);
128 
129  if (hasColors)
130  {
131  stream.write(reinterpret_cast<const char*>(&rgbaMap.at(i)), 3);
132  }
133  if (hasIntensities)
134  {
135  float intensity = static_cast<float>(intensityMap.at(i)) / 65535.0f;
136  stream.write(reinterpret_cast<const char*>(&intensity), 4);
137  }
138  }
139  }
140  }
141  else
142  {
143  success = false;
144  }
145 
146  // Close file
147  stream.close();
148 
149  return success;
150 }
151 
153 
155 
156 } // namespace visionary
VisionaryEndian.h
visionary::PointXYZ::x
float x
Definition: PointXYZ.h:28
visionary::PointCloudPlyWriter::~PointCloudPlyWriter
virtual ~PointCloudPlyWriter()
Definition: PointCloudPlyWriter.cpp:154
visionary
Definition: AuthenticationLegacy.h:25
visionary::PointXYZ
Definition: PointXYZ.h:26
visionary::PointXYZ::z
float z
Definition: PointXYZ.h:30
PointCloudPlyWriter.h
visionary::nativeToLittleEndian
T nativeToLittleEndian(T x)
Definition: VisionaryEndian.h:152
visionary::PointCloudPlyWriter::PointCloudPlyWriter
PointCloudPlyWriter()
Definition: PointCloudPlyWriter.cpp:152
visionary::PointXYZ::y
float y
Definition: PointXYZ.h:29
visionary::PointCloudPlyWriter::WriteFormatPLY
static bool WriteFormatPLY(const char *filename, const std::vector< PointXYZ > &points, bool useBinary)
Save a point cloud to a file in Polygon File Format (PLY), see: https://en.wikipedia....
Definition: PointCloudPlyWriter.cpp:30


sick_safevisionary_base
Author(s):
autogenerated on Sat Oct 21 2023 02:24:26