gldemo
futils.h
1 #pragma once
2 
3 #include <vector>
4 #include <fstream>
5 #include <sstream>
6 #include <string>
7 #include "vertex.h"
8 
9 #include <qprogressdialog.h>
10 #include <qapplication.h>
11 /*
12 // 读取从matlab导出的txt文件,每行四个数x y z w,只留xyz
13 inline std::vector<Vertex> readTxt(const std::string& fpath,bool hasColors = false) {
14  std::ifstream infile(fpath);
15  assert(!infile.fail());
16  std::vector<Vertex> res;
17  std::string line, str;
18  int i = 0;
19 
20  while (std::getline(infile, line))
21  {
22  float x, y, z, w;
23  std::istringstream iss(line);
24  if (!(iss >> x >> y >> z >> w)) {
25  qDebug() << "error";
26  break;
27  }
28  float t = (float)i / 100000.0f;
29  res.push_back(Vertex({ x, y, z }, { t, 1.0f, 0.0f }));
30  i++;
31  if (i % 2000 == 0) {
32  QApplication::processEvents();
33  }
34  }
35  qDebug() << "read vertices: " << i;
36 
37  progress.close();
38 
39  infile.close();
40  return res;
41 }
42 */
43 // 读取从matlab导出的txt文件,每行四个数x y z r g b w,只留xyz rgb
44 inline std::vector<Vertex> readTxt(const std::string& fpath) {
45  std::ifstream infile(fpath);
46  assert(!infile.fail());
47  std::vector<Vertex> res;
48  std::string line, str;
49 
50  // 开始读取
51  QProgressDialog progress;
52  progress.setWindowTitle(QStringLiteral("加载中"));
53  progress.setMaximum(0);
54  progress.setMinimum(0);
55  progress.show();
56  int i = 0;
57  int num = 0;
58  float a;
59  std::getline(infile, line);
60  std::istringstream iss0(line);
61  while (iss0 >> a)
62  num++;
63  num--;
64  if (num == 3) {
65  while (std::getline(infile, line))
66  {
67  float x, y, z, w;
68  std::istringstream iss(line);
69  if (!(iss >> x >> y >> z >> w)) {
70  qDebug() << "error";
71  break;
72  }
73  float t = (float)i / 100000.0f;
74  res.push_back(Vertex({ x, y, z }, { t, 1.0f, 0.0f }));
75  i++;
76  if (i % 2000 == 0) {
77  QApplication::processEvents();
78  }
79  }
80  }
81  else if(num == 6){
82  while (std::getline(infile, line))
83  {
84  float x, y, z, w;
85  int r, g, b;
86  std::istringstream iss(line);
87  if (iss)
88  if (!(iss >> x >> y >> z >> r >> g >> b >> w)) {
89  qDebug() << "error";
90  break;
91  }
92  float t = (float)i / 100000.0f;
93  res.push_back(Vertex({ x, y, z }, { r / 255.0f, g / 255.0f, b / 255.0f }));
94  i++;
95  if (i % 2000 == 0) {
96  QApplication::processEvents();
97  }
98  }
99  }
100  qDebug() << "read vertices: " << i;
101  progress.close();
102 
103  infile.close();
104  return res;
105 }
106 
107 
108 // 忽略文件头的一堆信息,只读取所有点的坐标
109 inline std::vector<Vertex> readPly(const std::string& fpath) {
110  std::ifstream infile(fpath);
111  assert(!infile.fail());
112  std::vector<Vertex> res;
113  std::string line, str;
114  int numVertices;
115  // 找到顶点数所在行
116  while (std::getline(infile, line))
117  {
118  if (std::string::npos != line.find("element vertex")) {
119  std::istringstream iss(line);
120  // 读出顶点数量
121  if (!(iss >> str >> str >> numVertices)) {
122  qDebug() << "error reading element vertex";
123  // error
124  }
125  qDebug() << numVertices << " | " << line.c_str();
126  break;
127  }
128  }
129  // 找到顶点数据开始的行
130  while (std::getline(infile, line))
131  {
132  if (std::string::npos != line.find("end_header")) {
133  break;
134  }
135  }
136  // 开始读取
137  QProgressDialog progress;
138  progress.setWindowTitle(QStringLiteral("加载中"));
139  progress.setMaximum(100);
140  progress.setMinimum(0);
141  progress.setValue(0);
142  progress.show();
143  float x, y, z;
144  for (int i = 0; i < numVertices; i++) {
145  float t = (float)i / numVertices;
146  std::getline(infile, line);
147  std::istringstream iss(line);
148  if (!(iss >> x >> y >> z)) {
149  qDebug() << "error reading vertex: " << line.c_str();
150  }
151  else {
152  res.push_back(Vertex(QVector3D(x, y, z), { t, 1.0f, 0.0f }));
153  }
154  if (i % 2000 == 0) {
155  progress.setValue((100 * i) / numVertices);
156  QApplication::processEvents();
157  }
158  }
159  progress.close();
160 
161  infile.close();
162  return res;
163 }
164 
Definition: vertex.h:8