-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
147 lines (125 loc) · 3.97 KB
/
main.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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <istream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
std::string join(const std::vector<std::string>& strings,
const std::string& joinWith)
{
const size_t numStrings = strings.size();
if (numStrings == 0)
return "";
std::stringstream ss;
ss << strings[0];
for (size_t i = 1; i < numStrings; i++)
ss << joinWith << strings[i];
return ss.str();
}
std::vector<std::string> split(const std::string& s)
{
std::stringstream ss(s);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
return vstrings;
}
void merge_plys(std::vector<std::string> files, std::string& vertices_tot,
std::string& faces_tot, size_t& num_vertices_tot,
size_t& num_faces_tot)
{
size_t ctr = 1;
for (auto filename : files)
{
std::ifstream infile(filename);
const size_t vertices_offset = num_vertices_tot;
bool header_done = false;
std::string line;
while (std::getline(infile, line))
{
if (!header_done)
{
header_done = line == "end_header";
continue;
}
const auto num_spaces = std::count(line.begin(), line.end(), ' ');
// Vertex
if (num_spaces == 5)
{
num_vertices_tot += 1;
vertices_tot += line;
vertices_tot += '\n';
continue;
}
assert(num_spaces == 3 || num_spaces == 4);
// Face
const auto nums_str = split(line);
std::vector<std::string> adjusted_nums;
adjusted_nums.reserve(4);
adjusted_nums.push_back(nums_str[0]);
for (size_t i = 1; i < num_spaces + 1; ++i)
{
const auto v = std::stoul(nums_str[i]);
adjusted_nums.push_back(std::to_string(v + vertices_offset));
}
const std::string face_adj = join(adjusted_nums, " ");
faces_tot += face_adj;
faces_tot += '\n';
num_faces_tot += 1;
}
std::cout << ctr++ << "/" << files.size() << std::endl;
}
}
std::vector<std::string> read_filelist(const std::string input)
{
std::vector<std::string> output;
std::ifstream infile(input);
std::string line;
while (std::getline(infile, line))
output.push_back(line);
return output;
}
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cout << "Usage: ./plymerge inputfile output.ply" << std::endl;
return 1;
}
std::string input = argv[1];
std::string output = argv[2];
std::cout << "Reading file list from " << input << std::endl;
std::vector<std::string> infiles = read_filelist(input);
std::cout << "Merging " << infiles.size() << " files" << std::endl;
std::string vertices_tot;
std::string faces_tot;
size_t num_vertices_tot = 0;
size_t num_faces_tot = 0;
merge_plys(infiles, vertices_tot, faces_tot, num_vertices_tot,
num_faces_tot);
std::cout << "Saving merged ply to " << output << "\n";
std::ofstream out(output);
out << "ply\n";
out << "format ascii 1.0\n";
out << "comment merged with plymerge\n";
out << "element vertex " + std::to_string(num_vertices_tot) + "\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";
out << "element face " + std::to_string(num_faces_tot) + "\n";
out << "property list uchar uint vertex_indices\n";
out << "end_header\n";
out << vertices_tot;
vertices_tot.clear();
out << faces_tot;
faces_tot.clear();
out.close();
return 0;
}