• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c++ 搞代码 4年前 (2022-01-06) 39次浏览 已收录 0个评论

这篇文章主要介绍了C++读入”N,X,Y,Z”格式文本文件到Eigen3 Matrix,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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  #include  #include  #include  #include  #include  #include  using namespace std; using namespace Eigen; // 字符串分割 void SplitString(const std::string& s, std::vector& 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 vector_data; SplitString(line, vector_data, ","); for (int c = 0; c <origin_data.cols(); c++) { origin_data(r, c<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>) = 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++读入”N,X,Y,Z”格式文本文件到Eigen3 Matrix的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++读入”N,X,Y,Z”格式文本文件到Eigen3 Matrix

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址