-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFileIO.cpp
222 lines (154 loc) · 5 KB
/
FileIO.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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string>
#include "FileIO.h"
#include "PointCollection.h"
#include "TreeCollection.h"
using namespace std;
FileIO::FileParts FileIO::GetFileParts(const string& s)
{
FileIO::FileParts file;
string path, name, extension;
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
path = s.substr(0, i + 1);
extension = s.substr(s.rfind("."), s.length() - s.rfind("."));
name = s.substr(i + 1, s.length() - (path.length() + extension.length()));
file.path = path;
file.name = name;
file.extension = extension;
return(file);
}
string FileIO::GetInputFilepath()
{
return i_filepath_;
}
string FileIO::GetPointOutputFilepath()
{
return o_filepath_points_;
}
string FileIO::GetTreeOutputFilepath()
{
return o_filepath_trees_;
}
// Read a point cloud from a .csv file into a vector of Points
PointCollection FileIO::ReadCsvPoints()
{
ifstream i_file(i_filepath_);
if(i_file){
cout << "Reading " << i_filepath_ << "...";
// Read line by line to determine the number of lines in the input file
string line;
unsigned int counter = 0;
while ( getline(i_file, line) ) {
counter++;
}
// Return to the beginning of the file
i_file.clear();
i_file.seekg(0, ios::beg);
PointCollection point_collection;
PointCollection::Point initial_val = {0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 2, false, {0,0,0}};
point_collection.points_.resize(counter, initial_val);
unsigned int k = 0;
string field;
PointCollection::Point point;
while(!i_file.eof()){
getline(i_file, field,',');
//point.x = stod(field);
stringstream str1(field);
str1 >> point.x;
getline(i_file, field, ',');
//point.y = stod(field);
stringstream str2(field);
str2 >> point.y;
getline(i_file, field, ',');
//point.z = stod(field);
stringstream str3(field);
str3 >> point.z;
getline(i_file, field);
//point.classification = stoi(field);
stringstream str4(field);
str4 >> point.classification;
// Set additional attribute values
point.tree_idx = 0;
point.segmentation_status = false;
point.local_maxima_status = 2;
// Add Point to PointCollection
point_collection.points_.push_back(point);
k++;
i_file >> ws;
}
i_file.close();
cout << "Done!" << endl;
return point_collection;
} else {
cerr << "FAILURE: unable to open input file " << i_filepath_ << endl;
exit(1);
}
}
// Write a vector of segmented Points to .csv file
void FileIO::WritePointsToCSV(PointCollection& point_collection, unsigned int precision)
{
// Create output file name
FileParts current_file_parts = GetFileParts(i_filepath_);
o_filepath_points_ = current_file_parts.path + current_file_parts.name + "_seg.csv";
ofstream o_file(o_filepath_points_);
if(o_file){
// Set decimal precision
o_file << fixed << setprecision(precision);
cout << "Writing points to " << o_filepath_points_ << endl;
// Print header
o_file << "X, Y, H, ID, R, G, B" << endl;
// Print content
for(unsigned int j(0); j < point_collection.points_.size(); j++){
o_file << point_collection.points_[j].x << ", " << point_collection.points_[j].y << ", " << point_collection.points_[j].z << ", " << point_collection.points_[j].tree_idx << ", " << point_collection.points_[j].rgb_color[0] << ", " << point_collection.points_[j].rgb_color[1] << ", " << point_collection.points_[j].rgb_color[2] << endl;
}
} else{
cerr << "FAILURE: unable to open output file ";
exit(1);
}
}
// Write a TreeCollection to .csv file
void FileIO::WriteTreesToCSV(TreeCollection& tree_collection, unsigned int precision)
{
// Create output file name
FileParts current_file_parts = GetFileParts(i_filepath_);
o_filepath_trees_ = current_file_parts.path + current_file_parts.name + "_trees.csv";
ofstream o_file(o_filepath_trees_);
if(o_file){
// Set decimal precision
o_file << fixed << setprecision(precision);
cout << "Writing trees to " << o_filepath_trees_ << endl;
// Print header
o_file << "ID, X_TOP, Y_TOP, H_TOP, X_BARYCENTER, Y_BARYCENTER, H_BARYCENTER, H_REL_BARYCENTER, N_POINTS" << endl;
// Print content
for(unsigned int j(0); j < tree_collection.trees_.size(); j++){
o_file
<< tree_collection.trees_[j].tree_idx << ", "
<< tree_collection.trees_[j].x_top << ", "
<< tree_collection.trees_[j].y_top << ", "
<< tree_collection.trees_[j].h_top << ", "
<< tree_collection.trees_[j].x_barycenter << ", "
<< tree_collection.trees_[j].y_barycenter << ", "
<< tree_collection.trees_[j].h_barycenter << ", "
<< tree_collection.trees_[j].rel_h_barycenter << ", "
<< tree_collection.trees_[j].n_points
<< endl;
}
} else{
cerr << "FAILURE: unable to open output file ";
exit(1);
}
}
// Constructor
FileIO::FileIO(std::string i_filepath)
{
i_filepath_ = i_filepath;
}