-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlyWriter.hpp
106 lines (96 loc) · 2.9 KB
/
PlyWriter.hpp
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
#pragma once
#include "TriangleMesh.hpp"
#include "Vector.hpp"
#include <fstream>
namespace Pvl {
class PlyWriter {
std::ostream& out_;
std::size_t vertexPos_;
std::size_t facePos_;
bool closed_ = false;
std::size_t vertexCnt_ = 0;
std::size_t faceCnt_ = 0;
public:
PlyWriter(std::ostream& out)
: out_(out) {
out << "ply\n";
out << "format ascii 1.0\n";
out << "comment Created by PVL library\n";
out << "element vertex ";
vertexPos_ = out.tellp();
out << " \n";
out << "property float x\n";
out << "property float y\n";
out << "property float z\n";
out << "property float nx\n";
out << "property float ny\n";
out << "property float nz\n"; /// \todo
out << "element face ";
facePos_ = out.tellp();
out << " \n";
out << "property list uchar int vertex_index\n";
out << "end_header\n";
}
template <typename T, int Dim>
PlyWriter& operator<<(const Vector<T, Dim>& p) {
for (int i = 0; i < Dim; ++i) {
out_ << p[i] << " ";
}
if (Dim == 3) {
out_ << "0 0 0\n";
}
++vertexCnt_;
return *this;
}
template <typename T, int Dim>
PlyWriter& operator<<(const std::vector<Vector<T, Dim>>& cloud) {
for (auto& p : cloud) {
*this << p;
}
return *this;
}
template <typename T, int Dim>
PlyWriter& write(const std::vector<Vector<T, Dim>>& cloud,
const std::vector<Vector<T, Dim>>& normals) {
for (std::size_t i = 0; i < cloud.size(); ++i) {
const auto& p = cloud[i];
const auto& n = normals[i];
out_ << p[0] << " " << p[1] << " " << p[2] << " " << n[0] << " " << n[1] << " "
<< n[2] << "\n";
}
vertexCnt_ += cloud.size();
return *this;
}
template <typename Vec>
PlyWriter& operator<<(const TriangleMesh<Vec>& mesh) {
for (std::uint32_t i = 0; i < mesh.numVertices(); ++i) {
const Vec& p = mesh.points[i];
out_ << p[0] << " " << p[1] << " " << p[2] << " 0 0 0\n";
}
std::size_t validCnt = 0;
for (std::uint32_t i = 0; i < mesh.numFaces(); ++i) {
if (!mesh.valid(FaceHandle(i))) {
continue;
}
auto f = mesh.faceVertices(FaceHandle(i));
out_ << "3 " << f[0] << " " << f[1] << " " << f[2] << "\n";
validCnt++;
}
vertexCnt_ += mesh.numVertices();
faceCnt_ += validCnt;
return *this;
}
void close() {
if (!closed_) {
out_.seekp(vertexPos_);
out_ << vertexCnt_;
out_.seekp(facePos_);
out_ << faceCnt_;
}
closed_ = true;
}
~PlyWriter() {
close();
}
};
} // namespace Pvl