-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcd2ptx.cpp
64 lines (50 loc) · 2.06 KB
/
pcd2ptx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
void writePTXHeader(std::ofstream &file, const int width, const int height)
{
file << width << std::endl
<< height << std::endl;
file << 0.f << " " << 0.f << " " << 0.f << std::endl;
file << 1.f << " " << 0.f << " " << 0.f << std::endl;
file << 0.f << " " << 1.f << " " << 0.f << std::endl;
file << 0.f << " " << 0.f << " " << 1.f << std::endl;
file << 1.f << " " << 0.f << " " << 0.f << " " << 0.f << std::endl;
file << 0.f << " " << 1.f << " " << 0.f << " " << 0.f << std::endl;
file << 0.f << " " << 0.f << " " << 1.f << " " << 0.f << std::endl;
file << 0.f << " " << 0.f << " " << 0.f << " " << 1.f << std::endl;
}
void writePTXFileXYZ(const std::string &filename, const pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
std::ofstream file(filename.c_str());
writePTXHeader(file, cloud->width, cloud->height);
for (size_t i = 0; i < cloud->points.size(); ++i)
{
file << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << " " << 1 << "\n";
}
file.close();
}
void writePTXFileXYZRGB(const std::string &filename, const pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
std::ofstream file(filename.c_str());
writePTXHeader(file, cloud->width, cloud->height);
for (size_t i = 0; i < cloud->points.size(); ++i)
{
std::uint32_t rgb = *reinterpret_cast<int *>(&(cloud->points[i].rgb));
std::uint8_t r = (rgb >> 16) & 0x0000ff;
std::uint8_t g = (rgb >> 8) & 0x0000ff;
std::uint8_t b = (rgb) & 0x0000ff;
file << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << " " << 1 << " " << (int)r << " " << (int)g << " " << (int)b << "\n";
}
file.close();
}
int main()
{
// Load the point cloud data
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::io::loadPCDFile("pc.pcd", *cloud);
// Save to PTX format
writePTXFileXYZRGB("pc.ptx", cloud);
return 0;
}