cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
pcd.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint> // For fixed-width integers
5#include <cstring> // For memcpy
6#include <iosfwd> // Forward declare std::istream
7#include <map>
8#include <memory> // unique_ptr
9#include <optional>
10#include <string>
11#include <vector>
12
13#include <cpp-toolbox/container/string.hpp> // For string utilities like ends_with
14#include <cpp-toolbox/cpp-toolbox_export.hpp>
15#include <cpp-toolbox/file/file.hpp> // Assuming MemoryMappedFile is here
17#include <cpp-toolbox/io/formats/base.hpp> // base_file_format_t, base_file_data_t
19#include <cpp-toolbox/macro.hpp> // For platform checks if needed directly
20#include <cpp-toolbox/types/point.hpp> // point_cloud_t (inherits base_file_data_t)
21
22namespace toolbox::io
23{
24
25// Forward declare the concrete data type we'll work with
27using toolbox::types::point_t; // Assuming point_t is also in types namespace
28
71class CPP_TOOLBOX_EXPORT pcd_format_t final : public base_file_format_t
72{
73private:
74 // --- Internal Helper Structs/Enums ---
75
80 struct pcd_header_t
81 {
82 std::string version;
83 std::vector<std::string> fields;
85 std::vector<char>
86 types;
88 std::vector<size_t> sizes;
90 std::vector<size_t> counts;
92 size_t width =
93 0;
95 size_t height =
96 0;
98 std::array<double, 7> viewpoint {
101 0,
102 0,
103 0,
104 1,
105 0,
106 0,
107 0};
108 size_t points = 0;
113 enum class data_type_t : uint8_t
114 {
115 ASCII,
116 BINARY,
117 BINARY_COMPRESSED,
118 UNKNOWN
119 } data_type = data_type_t::UNKNOWN;
120 size_t point_step = 0;
122 size_t header_length = 0;
129 std::map<std::string, size_t> m_field_indices;
130
136 {
137 char type;
138 size_t size;
139 size_t count;
140 size_t offset;
141 };
142
151 bool parse_line(const std::string& line);
152
158 void calculate_point_step_and_indices();
159
168 [[nodiscard]] std::optional<field_info_t> get_field_info(
169 const std::string& field_name) const;
170
179 [[nodiscard]] bool validate(size_t file_size_bytes) const;
180 };
181
182public:
186 explicit pcd_format_t() = default;
187
191 ~pcd_format_t() override = default;
192
193 // 禁用复制,继承移动操作/Disable copy, inherit move operations
194 pcd_format_t(const pcd_format_t&) = delete;
196 // pcd_format_t(pcd_format_t&&) = default; // 继承的足够了/Inherited is fine
197 // pcd_format_t& operator=(pcd_format_t&&) = default; //
198 // 继承的足够了/Inherited is fine
199
216 [[nodiscard]] auto can_read(const std::string& path) const -> bool override;
217
231 [[nodiscard]] auto get_supported_extensions() const
232 -> std::vector<std::string> override;
233
264 auto read(const std::string& path, std::unique_ptr<base_file_data_t>& data)
265 -> bool override;
266
312 [[nodiscard]] auto write(const std::string& path,
313 const std::unique_ptr<base_file_data_t>& data,
314 bool binary) const -> bool override;
315
316private:
317 // --- Internal Helper Functions ---
318
330 template<typename T>
331 bool write_internal(const std::string& path,
332 const point_cloud_t<T>& cloud,
333 bool binary) const;
334
347 static bool parse_header_stream(std::istream& stream,
348 pcd_header_t& header,
349 size_t& header_end_pos);
350
362 template<typename T>
363 static bool read_ascii_data(std::istream& stream,
364 const pcd_header_t& header,
365 point_cloud_t<T>& cloud);
366
378 template<typename T>
379 static bool read_binary_data(const std::string& path,
380 const pcd_header_t& header,
381 point_cloud_t<T>& cloud);
382
398 template<typename PointStorageT>
399 static bool parse_ascii_point_line(const std::string& line,
400 const pcd_header_t& header,
401 point_t<PointStorageT>& point,
402 point_t<PointStorageT>* normal = nullptr,
403 point_t<PointStorageT>* color = nullptr);
404
424 template<typename DestT, typename PCDT>
425 static bool read_binary_field_value(const unsigned char* point_ptr,
426 size_t offset,
427 DestT& out_val);
428
429}; // class pcd_format_t
430
453template<typename T>
454CPP_TOOLBOX_EXPORT std::unique_ptr<toolbox::types::point_cloud_t<T>> read_pcd(
455 const std::string& path);
456
484template<typename T>
485CPP_TOOLBOX_EXPORT bool write_pcd(const std::string& path,
486 const toolbox::types::point_cloud_t<T>& cloud,
487 bool binary);
488
489} // namespace toolbox::io
490
491#include <cpp-toolbox/io/formats/detail/pcd_impl.hpp>
文件数据的基类 / Base class for data loaded from files
Definition base.hpp:33
文件格式读写器的基类 / Base class for file format readers/writers
Definition base.hpp:85
点云数据(.pcd)文件格式处理器。/File format handler for Point Cloud Data (.pcd) files.
Definition pcd.hpp:72
pcd_format_t()=default
构造函数。/Constructor.
pcd_format_t(const pcd_format_t &)=delete
auto get_supported_extensions() const -> std::vector< std::string > override
返回支持的文件扩展名。/Returns the supported file extension.
~pcd_format_t() override=default
析构函数。/Destructor.
auto can_read(const std::string &path) const -> bool override
检查文件扩展名是否为".pcd"。/Checks if the file extension is ".pcd".
pcd_format_t & operator=(const pcd_format_t &)=delete
包含点和相关数据的点云类 / A point cloud class containing points and associated data
Definition point.hpp:268
通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macro...
< 用于列出目录下的文件/For listing files in a directory
Definition dataloader.hpp:15
包含偏移量的字段详细信息结构体。/Helper struct for field details including offset.
Definition pcd.hpp:136
3D点/向量模板类 / A 3D point/vector template class
Definition point.hpp:48