-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathply.cpp
315 lines (277 loc) · 10.8 KB
/
ply.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <tuple>
#include <unordered_map>
#include <stdexcept>
#include <cstring> // For memcpy
#include <type_traits>
// 示例:用户自定义的点结构体
struct CustomVertex {
float x, y, z; // 必须的坐标属性
unsigned char r, g, b; // 自定义属性
};
// 重载输出操作符 <<
std::ostream& operator<<(std::ostream& os, const CustomVertex& vertex) {
os << "Position: (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ") ";
os << "Color: (" << static_cast<int>(vertex.r) << ", " << static_cast<int>(vertex.g) << ", " << static_cast<int>(vertex.b) << ")";
return os;
}
// 重载输入操作符 >>
std::istream& operator>>(std::istream& is, CustomVertex& vertex) {
int r, g, b;
is >> vertex.x >> vertex.y >> vertex.z >> r >> g >> b;
vertex.r = static_cast<unsigned char>(r);
vertex.g = static_cast<unsigned char>(g);
vertex.b = static_cast<unsigned char>(b);
return is;
}
// 属性类型映射
enum class PropertyType {
CHAR,
UCHAR,
SHORT,
USHORT,
INT,
UINT,
FLOAT,
DOUBLE
};
// 定义属性类型的大小和属性名映射
template<typename T>
struct TypeInfo;
template<>
struct TypeInfo<char> {
static constexpr PropertyType type = PropertyType::CHAR;
static constexpr const char* name = "char";
};
template<>
struct TypeInfo<unsigned char> {
static constexpr PropertyType type = PropertyType::UCHAR;
static constexpr const char* name = "uchar";
};
template<>
struct TypeInfo<short> {
static constexpr PropertyType type = PropertyType::SHORT;
static constexpr const char* name = "short";
};
template<>
struct TypeInfo<unsigned short> {
static constexpr PropertyType type = PropertyType::USHORT;
static constexpr const char* name = "ushort";
};
template<>
struct TypeInfo<int> {
static constexpr PropertyType type = PropertyType::INT;
static constexpr const char* name = "int";
};
template<>
struct TypeInfo<unsigned int> {
static constexpr PropertyType type = PropertyType::UINT;
static constexpr const char* name = "uint";
};
template<>
struct TypeInfo<float> {
static constexpr PropertyType type = PropertyType::FLOAT;
static constexpr const char* name = "float";
};
template<>
struct TypeInfo<double> {
static constexpr PropertyType type = PropertyType::DOUBLE;
static constexpr const char* name = "double";
};
// 提取结构体成员属性名、偏移量和类型
#define DEFINE_PROPERTY_INFO(Type, Member) {#Member, offsetof(Type, Member), TypeInfo<decltype(Type::Member)>::type}
class PlyBinaryIO {
public:
// 构造函数传入文件名
PlyBinaryIO(const std::string& filename) : filename_(filename) {}
// 读取 PLY 文件
template<typename VertexType>
void read(std::vector<VertexType>& vertices) {
std::ifstream file(filename_, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file for reading: " + filename_);
}
// 读取头部
readHeader<VertexType>(file);
// 读取顶点数据
vertices.resize(vertex_count_);
for (auto& vertex : vertices) {
// 读取 xyz 坐标
//file.read(reinterpret_cast<char*>(&vertex.x), sizeof(vertex.x));
//file.read(reinterpret_cast<char*>(&vertex.y), sizeof(vertex.y));
//file.read(reinterpret_cast<char*>(&vertex.z), sizeof(vertex.z));
// 读取其他属性
for (const auto& property : custom_properties_) {
char* vertex_ptr = reinterpret_cast<char*>(&vertex);
file.read(vertex_ptr + property.offset, property.size);
}
}
file.close();
}
// 写入 PLY 文件
template<typename VertexType>
void write(const std::vector<VertexType>& vertices) {
std::ofstream file(filename_, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file for writing: " + filename_);
}
vertex_count_ = vertices.size();
// 写入头部
writeHeader<VertexType>(file);
// 写入顶点数据
for (const auto& vertex : vertices) {
// 写入 xyz 坐标
//file.write(reinterpret_cast<const char*>(&vertex.x), sizeof(vertex.x));
//file.write(reinterpret_cast<const char*>(&vertex.y), sizeof(vertex.y));
//file.write(reinterpret_cast<const char*>(&vertex.z), sizeof(vertex.z));
// 写入其他属性
for (const auto& property : custom_properties_) {
const char* vertex_ptr = reinterpret_cast<const char*>(&vertex);
file.write(vertex_ptr + property.offset, property.size);
}
}
file.close();
}
private:
struct Property {
std::string name;
size_t offset;
size_t size;
PropertyType type;
};
std::string filename_;
size_t vertex_count_ = 0;
std::vector<Property> custom_properties_;
// 读取头部,自动识别属性
template<typename VertexType>
void readHeader(std::ifstream& file) {
std::string line;
while (std::getline(file, line)) {
if (line == "end_header") break;
if (line.find("element vertex") != std::string::npos) {
vertex_count_ = std::stoi(line.substr(line.find_last_of(' ') + 1));
}
custom_properties_.clear();
custom_properties_.emplace_back(Property{"x",0,4,PropertyType::FLOAT});
custom_properties_.emplace_back(Property{"y",4,4,PropertyType::FLOAT});
custom_properties_.emplace_back(Property{"z",8,4,PropertyType::FLOAT});
size_t offset = 12;
if (line.find("property") != std::string::npos) {
auto tokens = tokenize(line);
if (tokens.size() >= 3) {
std::string property_type = tokens[1];
std::string property_name = tokens[2];
if (property_type == TypeInfo<unsigned char>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(unsigned char), PropertyType::UCHAR});
offset += sizeof(unsigned char);
}
else if (property_type == TypeInfo<char>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(char), PropertyType::CHAR});
offset += sizeof(char);
}
else if (property_type == TypeInfo<short>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(short), PropertyType::SHORT});
offset += sizeof(short);
}
else if (property_type == TypeInfo<ushort>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(ushort), PropertyType::USHORT});
offset += sizeof(ushort);
}
else if (property_type == TypeInfo<int>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(int), PropertyType::INT});
offset += sizeof(int);
}
else if (property_type == TypeInfo<uint>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(uint), PropertyType::UINT});
offset += sizeof(uint);
}
else if (property_type == TypeInfo<float>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(float), PropertyType::FLOAT});
offset += sizeof(float);
}
else if (property_type == TypeInfo<double>::name) {
custom_properties_.emplace_back(Property{property_name, offset, sizeof(double), PropertyType::DOUBLE});
offset += sizeof(double);
}
}
}
}
}
// 写入头部
template<typename VertexType>
void writeHeader(std::ofstream& file) const {
file << "ply\n";
file << "format binary_little_endian 1.0\n";
file << "element vertex " << vertex_count_ << "\n";
//file << "property float x\n";
//file << "property float y\n";
//file << "property float z\n";
for (const auto& property : custom_properties_) {
switch (property.type) {
case PropertyType::UCHAR:
file << "property uchar " << property.name << "\n";
break;
case PropertyType::CHAR:
file << "property char " << property.name << "\n";
break;
case PropertyType::SHORT:
file << "property short " << property.name << "\n";
break;
case PropertyType::USHORT:
file << "property ushort " << property.name << "\n";
break;
case PropertyType::INT:
file << "property int " << property.name << "\n";
break;
case PropertyType::UINT:
file << "property uint " << property.name << "\n";
break;
case PropertyType::FLOAT:
file << "property float " << property.name << "\n";
break;
case PropertyType::DOUBLE:
file << "property double " << property.name << "\n";
break;
default:
break;
}
}
file << "end_header\n";
}
// 将字符串按空格拆分为 tokens
std::vector<std::string> tokenize(const std::string& line) const {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(line);
while (std::getline(tokenStream, token, ' ')) {
if (!token.empty()) {
tokens.push_back(token);
}
}
return tokens;
}
};
int main() {
PlyBinaryIO ply("example.ply");
// 自定义的顶点数据
std::vector<CustomVertex> vertices = {
{0, 22, 1, 255, 0, 0},
{1, 33, 20, 0, 255, 0},
{2, 44, 3, 0, 0, 255}
};
// 写入 PLY 文件
ply.write(vertices);
// 读取 PLY 文件
std::vector<CustomVertex> readVertices;
ply.read(readVertices);
// 输出读取的顶点数据
for (const auto& v : readVertices) {
std::cout << v << std::endl;
//std::cout << "Vertex: " << v.x << ", " << v.y << ", " << v.z << ", " << v.r<< ", " << v.g<< ", " << v.b <<"\n";
}
return 0;
}