//统计文件中的行数,即数据文件中数据数目
int count(string file)
{
cout << "数据计数中..." << endl;
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
vector<string> ve;
int num = 0;
while (getline(infile, s))
{
num++;
}
infile.close(); //关闭文件输入流
cout << "文件计数完毕..." << endl;
return num;
}
//从一个数据文件中复制length个数据到新文件中
void copyData(string file, int length)
{
std::cout << "读取数据中..." << endl;
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
//char str;
vector<string> ve;
vector<Point> num;
int aa = length;
while (aa)
{
Point p;
int i = 0;
string num_s = "";
getline(infile, s);
while (s[i] != ' ')
{
num_s += s[i];
i++;
}
p.x = str2num(num_s);
i++;
num_s = "";
while (s[i] != ' ')
{
num_s += s[i];
i++;
}
p.y = str2num(num_s);
i++;
num_s = "";
while (i <= s.size())
{
num_s += s[i];
i++;
}
p.z = str2num(num_s);
num.push_back(p);
aa--;
}
infile.close(); //关闭文件输入流
std::cout << "读取文件完毕..." << endl;
std::cout << "写入数据..." << endl;
ofstream fout("data.txt");
for (int i = 0; i < length; i++)
{
fout << num[i].x << " " << num[i].y << " " << num[i].z << endl;
}
std::cout << "写入数据完毕..." << endl;
fout.close();
}
//新建一个文件,写入数据
//格式为: x y z
void write_txt(vector<Point>num)
{
cout << "写入数据..." << endl;
ofstream fout("data.txt");
for (int i = 0; i <num.size(); i++)
{
fout << num[i].x << " " << num[i].y << " " << num[i].z << endl;
}
std::cout << "写入数据完毕..." << endl;
fout.close();
}
//分别找出点云数据x、y、z的最大和最小值
void Maximum_value(vector<Point>num)
{
Xmax = num[0].x; Xmin = num[0].x;
Ymax = num[0].y; Ymin = num[0].y;
Zmax = num[0].z; Zmin = num[0].z;
for (int i = 0; i < num.size(); i++)
{
if (Xmax < num[i].x) Xmax = num[i].x;
if (Xmin > num[i].x) Xmin = num[i].x;
if (Ymax < num[i].y) Ymax = num[i].y;
if (Ymin > num[i].y) Ymin = num[i].y;
if (Zmax < num[i].z) Zmax = num[i].z;
if (Zmin > num[i].z) Zmin = num[i].z;
}
std::cout << "Xmax: " << Xmax << " Xmin: " << Xmin << endl;
std::cout << "Ymax: " << Ymax << " Ymin: " << Ymin << endl;
std::cout << "Zmax: " << Zmax << " Zmin: " << Zmin << endl;
}
// 字符串转换为数字,需要有文件 #include <sstream>
int str2num(const string str)
{
stringstream ss(str);
int num;
ss >> num;
return num;
}