-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.cpp
More file actions
171 lines (147 loc) · 4.53 KB
/
Copy pathutils.cpp
File metadata and controls
171 lines (147 loc) · 4.53 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "utils.h"
#include <iostream>
#include <fstream>
#include <sstream>
#define PLY_START_HEADER "ply"
#define PLY_END_HEADER "end_header"
#define PLY_ASCII "format ascii 1.0"
#define PLY_ELEMENT_VERTEX "element vertex"
BufferDepth::BufferDepth() {
m_data = NULL;
m_rows = 0;
m_cols = 0;
m_stride = 0;
}
BufferDepth::BufferDepth(uint16_t* data, size_t rows, size_t cols, size_t stride) :
m_data(data), m_rows(rows), m_cols(cols), m_stride(stride) {
}
BufferDepth::~BufferDepth(){}
uint16_t* BufferDepth::data() {
return m_data.get();
}
size_t BufferDepth::rows() const {
return m_rows;
}
size_t BufferDepth::cols() const {
return m_cols;
}
size_t BufferDepth::stride() const {
return m_stride;
}
BufferColor::BufferColor() {
m_data = NULL;
m_rows = 0;
m_cols = 0;
m_stride = 0;
}
BufferColor::BufferColor(uint8_t* data, size_t rows, size_t cols, size_t stride) :
m_data(data), m_rows(rows), m_cols(cols), m_stride(stride) {
}
BufferColor::~BufferColor(){}
uint8_t* BufferColor::data() {
return m_data.get();
}
size_t BufferColor::rows() const {
return m_rows;
}
size_t BufferColor::cols() const {
return m_cols;
}
size_t BufferColor::stride() const {
return m_stride;
}
BufferBodyIndex::BufferBodyIndex() {
m_data = NULL;
m_rows = 0;
m_cols = 0;
m_stride = 0;
}
BufferBodyIndex::BufferBodyIndex(uint8_t* data, size_t rows, size_t cols, size_t stride) :
m_data(data), m_rows(rows), m_cols(cols), m_stride(stride) {
}
BufferBodyIndex::~BufferBodyIndex(){}
uint8_t* BufferBodyIndex::data() {
return m_data.get();
}
size_t BufferBodyIndex::rows() const {
return m_rows;
}
size_t BufferBodyIndex::cols() const {
return m_cols;
}
size_t BufferBodyIndex::stride() const {
return m_stride;
}
BufferPointCloud::BufferPointCloud(int16_t* data, size_t rows, size_t cols, size_t stride) :
m_data(data), m_rows(rows), m_cols(cols), m_stride(stride) {
}
BufferPointCloud::~BufferPointCloud(){}
int16_t *BufferPointCloud::data() {
return m_data.get();
}
size_t BufferPointCloud::rows() const {
return m_rows;
}
size_t BufferPointCloud::cols() const {
return m_cols;
}
size_t BufferPointCloud::stride() const {
return m_stride;
}
struct color_point_t
{
int16_t xyz[3];
uint8_t rgb[3];
};
void write_point_cloud(const k4a_image_t point_cloud_image,
const k4a_image_t color_image,
const char *file_name) {
std::vector<color_point_t> points;
int width = k4a_image_get_width_pixels(point_cloud_image);
int height = k4a_image_get_height_pixels(color_image);
int16_t *point_cloud_image_data = (int16_t *)(void *)k4a_image_get_buffer(point_cloud_image);
uint8_t *color_image_data = k4a_image_get_buffer(color_image);
for (int i = 0; i < width * height; i++)
{
color_point_t point;
point.xyz[0] = point_cloud_image_data[3 * i + 0];
point.xyz[1] = point_cloud_image_data[3 * i + 1];
point.xyz[2] = point_cloud_image_data[3 * i + 2];
if (point.xyz[2] == 0)
{
continue;
}
point.rgb[0] = color_image_data[4 * i + 0];
point.rgb[1] = color_image_data[4 * i + 1];
point.rgb[2] = color_image_data[4 * i + 2];
uint8_t alpha = color_image_data[4 * i + 3];
if (point.rgb[0] == 0 && point.rgb[1] == 0 && point.rgb[2] == 0 && alpha == 0)
{
continue;
}
points.push_back(point);
}
// save to the ply file
std::ofstream ofs(file_name); // text mode first
ofs << PLY_START_HEADER << std::endl;
ofs << PLY_ASCII << std::endl;
ofs << PLY_ELEMENT_VERTEX << " " << points.size() << std::endl;
ofs << "property float x" << std::endl;
ofs << "property float y" << std::endl;
ofs << "property float z" << std::endl;
ofs << "property uchar red" << std::endl;
ofs << "property uchar green" << std::endl;
ofs << "property uchar blue" << std::endl;
ofs << PLY_END_HEADER << std::endl;
ofs.close();
std::stringstream ss;
for (size_t i = 0; i < points.size(); ++i)
{
// image data is BGR
ss << (float)points[i].xyz[0] << " " << (float)points[i].xyz[1] << " " << (float)points[i].xyz[2];
ss << " " << (float)points[i].rgb[2] << " " << (float)points[i].rgb[1] << " " << (float)points[i].rgb[0];
ss << std::endl;
}
std::ofstream ofs_text(file_name, std::ios::out | std::ios::app);
ofs_text.write(ss.str().c_str(), (std::streamsize)ss.str().length());
}