-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfutils.h
164 lines (153 loc) · 4.44 KB
/
futils.h
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
#pragma once
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include "vertex.h"
#include <qprogressdialog.h>
#include <qapplication.h>
/*
// 读取从matlab导出的txt文件,每行四个数x y z w,只留xyz
inline std::vector<Vertex> readTxt(const std::string& fpath,bool hasColors = false) {
std::ifstream infile(fpath);
assert(!infile.fail());
std::vector<Vertex> res;
std::string line, str;
int i = 0;
while (std::getline(infile, line))
{
float x, y, z, w;
std::istringstream iss(line);
if (!(iss >> x >> y >> z >> w)) {
qDebug() << "error";
break;
}
float t = (float)i / 100000.0f;
res.push_back(Vertex({ x, y, z }, { t, 1.0f, 0.0f }));
i++;
if (i % 2000 == 0) {
QApplication::processEvents();
}
}
qDebug() << "read vertices: " << i;
progress.close();
infile.close();
return res;
}
*/
// 读取从matlab导出的txt文件,每行四个数x y z r g b w,只留xyz rgb
inline std::vector<Vertex> readTxt(const std::string& fpath) {
std::ifstream infile(fpath);
assert(!infile.fail());
std::vector<Vertex> res;
std::string line, str;
// 开始读取
QProgressDialog progress;
progress.setWindowTitle(QStringLiteral("加载中"));
progress.setMaximum(0);
progress.setMinimum(0);
progress.show();
int i = 0;
int num = 0;
float a;
std::getline(infile, line);
std::istringstream iss0(line);
while (iss0 >> a)
num++;
num--;
if (num == 3) {
while (std::getline(infile, line))
{
float x, y, z, w;
std::istringstream iss(line);
if (!(iss >> x >> y >> z >> w)) {
qDebug() << "error";
break;
}
float t = (float)i / 100000.0f;
res.push_back(Vertex({ x, y, z }, { t, 1.0f, 0.0f }));
i++;
if (i % 2000 == 0) {
QApplication::processEvents();
}
}
}
else if(num == 6){
while (std::getline(infile, line))
{
float x, y, z, w;
int r, g, b;
std::istringstream iss(line);
if (iss)
if (!(iss >> x >> y >> z >> r >> g >> b >> w)) {
qDebug() << "error";
break;
}
float t = (float)i / 100000.0f;
res.push_back(Vertex({ x, y, z }, { r / 255.0f, g / 255.0f, b / 255.0f }));
i++;
if (i % 2000 == 0) {
QApplication::processEvents();
}
}
}
qDebug() << "read vertices: " << i;
progress.close();
infile.close();
return res;
}
// 忽略文件头的一堆信息,只读取所有点的坐标
inline std::vector<Vertex> readPly(const std::string& fpath) {
std::ifstream infile(fpath);
assert(!infile.fail());
std::vector<Vertex> res;
std::string line, str;
int numVertices;
// 找到顶点数所在行
while (std::getline(infile, line))
{
if (std::string::npos != line.find("element vertex")) {
std::istringstream iss(line);
// 读出顶点数量
if (!(iss >> str >> str >> numVertices)) {
qDebug() << "error reading element vertex";
// error
}
qDebug() << numVertices << " | " << line.c_str();
break;
}
}
// 找到顶点数据开始的行
while (std::getline(infile, line))
{
if (std::string::npos != line.find("end_header")) {
break;
}
}
// 开始读取
QProgressDialog progress;
progress.setWindowTitle(QStringLiteral("加载中"));
progress.setMaximum(100);
progress.setMinimum(0);
progress.setValue(0);
progress.show();
float x, y, z;
for (int i = 0; i < numVertices; i++) {
float t = (float)i / numVertices;
std::getline(infile, line);
std::istringstream iss(line);
if (!(iss >> x >> y >> z)) {
qDebug() << "error reading vertex: " << line.c_str();
}
else {
res.push_back(Vertex(QVector3D(x, y, z), { t, 1.0f, 0.0f }));
}
if (i % 2000 == 0) {
progress.setValue((100 * i) / numVertices);
QApplication::processEvents();
}
}
progress.close();
infile.close();
return res;
}