GrabBag/Tools/CloudView/Inc/PointCloudConverter.h

144 lines
4.0 KiB
C
Raw Normal View History

2026-01-16 01:04:43 +08:00
#ifndef POINT_CLOUD_CONVERTER_H
#define POINT_CLOUD_CONVERTER_H
#include <string>
#include <vector>
#include <memory>
/**
* @brief 3D
*/
struct Point3D
{
float x, y, z;
Point3D() : x(0), y(0), z(0) {}
Point3D(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
};
/**
* @brief 3D
*/
struct Point3DRGB
{
float x, y, z;
uint8_t r, g, b;
Point3DRGB() : x(0), y(0), z(0), r(255), g(255), b(255) {}
Point3DRGB(float _x, float _y, float _z, uint8_t _r = 255, uint8_t _g = 255, uint8_t _b = 255)
: x(_x), y(_y), z(_z), r(_r), g(_g), b(_b) {}
};
/**
* @brief
*/
template<typename PointT>
class SimplePointCloud
{
public:
std::vector<PointT> points;
std::vector<int> lineIndices; // 每个点所属的线索引
void clear() { points.clear(); lineIndices.clear(); }
size_t size() const { return points.size(); }
bool empty() const { return points.empty(); }
void reserve(size_t n) { points.reserve(n); lineIndices.reserve(n); }
void push_back(const PointT& pt) { points.push_back(pt); }
void push_back(const PointT& pt, int lineIdx) {
points.push_back(pt);
lineIndices.push_back(lineIdx);
}
};
using PointCloudXYZ = SimplePointCloud<Point3D>;
using PointCloudXYZRGB = SimplePointCloud<Point3DRGB>;
/**
* @brief
* txt pcd
*/
class PointCloudConverter
{
public:
PointCloudConverter();
~PointCloudConverter();
/**
* @brief txt 使 CloudUtils
*/
int loadFromTxt(const std::string& fileName, PointCloudXYZ& cloud);
int loadFromTxt(const std::string& fileName, PointCloudXYZRGB& cloud);
/**
* @brief pcd ASCII/Binary
*/
int loadFromPcd(const std::string& fileName, PointCloudXYZ& cloud);
int loadFromPcd(const std::string& fileName, PointCloudXYZRGB& cloud);
/**
* @brief
*/
int loadFromFile(const std::string& fileName, PointCloudXYZ& cloud);
int loadFromFile(const std::string& fileName, PointCloudXYZRGB& cloud);
/**
* @brief txt 使 CloudUtils
*/
int saveToTxt(const std::string& fileName, const PointCloudXYZ& cloud, int lineNum, int linePtNum);
/**
* @brief + XY
* @param cloud
* @param rotatedCloud
* @param lineNum 线
* @param linePtNum 线
* @param newLineNum 线
* @param newLinePtNum 线
*/
int rotateCloud(const PointCloudXYZ& cloud, PointCloudXYZ& rotatedCloud,
int lineNum, int linePtNum, int& newLineNum, int& newLinePtNum);
/**
* @brief
*/
std::string getLastError() const { return m_lastError; }
/**
* @brief
*/
size_t getLoadedPointCount() const { return m_loadedPointCount; }
/**
* @brief 线
*/
int getLoadedLineCount() const { return m_loadedLineCount; }
private:
/**
* @brief
*/
std::string getFileExtension(const std::string& fileName);
/**
* @brief PCD
*/
struct PcdHeader
{
int width = 0;
int height = 1;
int points = 0;
bool isBinary = false;
bool hasRgb = false;
std::vector<std::string> fields;
std::vector<int> fieldSizes;
std::vector<char> fieldTypes;
int pointSize = 0;
};
bool parsePcdHeader(std::ifstream& file, PcdHeader& header);
std::string m_lastError;
size_t m_loadedPointCount;
int m_loadedLineCount;
};
#endif // POINT_CLOUD_CONVERTER_H