C++读入"N,X,Y,Z"格式文本文件到Eigen3 Matrix

所属分类: 软件编程 / C 语言 阅读数: 84
收藏 0 赞 0 分享

C++读入"N,X,Y,Z"格式文本文件到Eigen3 Matrix,以及相同格式输出方法

很多数据资料的格式类似这样:

1,-2085738.7757,5503702.8697,2892977.6829
2,-2071267.5135,5520926.7235,2883341.8135
3,-2079412.5535,5512450.8800,2879771.2119
4,-2093693.1744,5511218.2651,2869861.8947
5,-2113681.5062,5491864.0382,2896934.4852
6,-2100573.2849,5496675.0138,2894377.6030

其中数据按照N(点号),X,Y,Z(三维坐标)排序。

这里提供一种C++读入"N,X,Y,Z"格式文本文件到Eigen3 Matrix的方法,以及对应的同格式输出方法

#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <Eigen/Dense>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;
using namespace Eigen;

// 字符串分割
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
 std::string::size_type pos1, pos2;
 pos2 = s.find(c);
 pos1 = 0;
 while (std::string::npos != pos2)
 {
 v.push_back(s.substr(pos1, pos2 - pos1));

 pos1 = pos2 + c.size();
 pos2 = s.find(c, pos1);
 }
 if (pos1 != s.length())
 v.push_back(s.substr(pos1));
}
// 读入相应格式的xyz文件
void ReadXYZFile(string filepath, MatrixXd& origin_data)
{
 ifstream infile;
 infile.open(filepath);
 cout << "Reading XYZ File: " << filepath << endl;
 if (!infile.is_open())
 {
 cout << "File Cannot Open" << endl;
 exit(1);
 }

 int r = 0; // 逐行加载数据
 char buffer[100];
 while (!infile.eof())
 {
 // getline只能读成char*,
 // 而SplitString只能切割string,
 // 而atof又只能转化char*到double
 infile.getline(buffer, 100);
 // cout << buffer << endl;
 string line = buffer;
 if (line == "")
 {
 continue;
 }
 vector<string> vector_data;
 SplitString(line, vector_data, ",");
 for (int c = 0; c < origin_data.cols(); c++)
 {
 origin_data(r, c) = atof(vector_data[c].c_str());
 }
 r++;

 }
 return;
}
// 将矩阵按读入的相同格式保存至相应路径
void WriteXYZFile(string filepath, MatrixXd& trans_data)
{
 ofstream outfile;
 outfile.open(filepath, ios::out | ios::trunc);
 for (int r = 0; r < trans_data.rows(); r++)
 {
 for (int c = 0; c < trans_data.cols(); c++)
 {
 if (c < trans_data.cols() - 1)
 {
 outfile << trans_data(r, c) << ',';
 }
 if (c == trans_data.cols() - 1)
 {
 outfile << trans_data(r, c);
 }

 }
 outfile << endl;
 }
 cout << "Write XYZ File: " << filepath << endl;
 outfile.close();
 return;
}
更多精彩内容其他人还在看

利用C语言来求最大连续子序列乘积的方法

这篇文章主要介绍了利用C语言来求最大连续子序列乘积的方法,基本的思路以外文中还附有相关ACM题目,需要的朋友可以参考下
收藏 0 赞 0 分享

用C语言判断一个二叉树是否为另一个的子结构

这篇文章主要介绍了用C语言判断一个二叉树是否为另一个的子结构,是数据结构学习当中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的阶乘,排列和组合实例

这篇文章主要介绍了C语言实现的阶乘,排列和组合的方法,涉及C语言数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言查找数组里数字重复次数的方法

这篇文章主要介绍了C语言查找数组里数字重复次数的方法,涉及C语言针对数组的遍历与判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言简单实现计算字符个数的方法

这篇文章主要介绍了C语言简单实现计算字符个数的方法,涉及C语言针对字符串的简单遍历与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

c实现linux下的数据库备份

本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

C++获得文件状态信息的方法

这篇文章主要介绍了C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言按关键字搜索文件夹中文件的方法

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现BMP转换JPG的方法

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多