文本文件读取
//read timestamps and poses equal to timestamps
std::ifstream infile;
infile.open(file_path);
while (!infile.eof() && infile.good())
{
std::string line;
std::getline(infile, line);
if (!line.empty())
{
Eigen::Matrix4f pose;
double time_stamp;
ParseLine(line, pose, time_stamp);
}
}
从字符串中解析出double.
格式: 1.1,2.1,3.1
bool PoseGenerator::ParseLine(const std::string &line, Eigen::Matrix4f &pose, double &time_stamp)
{
if (line.size() < 1U) {
return false;
}
if (line[0] == '#') {
return false;
}
// 1403715274302142976,0.878612,2.142470,0.947262,0.060514,-0.828459,-0.058956,-0.553641,0.009474,-0.014009,-0.002145,-0.002229,0.020700,0.076350,-0.012492,0.547666,0.069073
Eigen::Quaternionf q;
Eigen::Vector3f p;
std::istringstream iss(line);
char dotc = ',';
iss >> time_stamp >> dotc >> p.x() >> dotc >> p.y() >> dotc >> p.z() >> dotc >> q.w() >> dotc >> q.x() >> dotc >> q.y() >> dotc >> q.z();
// std::cout << time_stamp << ", " << p.y() << ", " << q.z() << "\n"; // "\n";
pose = Eigen::Matrix4f::Identity();
pose.block<3, 3>(0, 0) = q.toRotationMatrix();
pose.block<3, 1>(0, 3) = p;
time_stamp = time_stamp / 1e9; // transform to seconds
}
以上方式处理不了如下格式
1.1 2.1 3.1
可以使用下述方式读取:
Eigen::Quaternionf q;
Eigen::Vector3f p;
std::istringstream iss(line);
if (data_set_type_ == DATATYPE::EUROC) {
char dotc = ',';
iss >> time_stamp >> dotc >> p.x() >> dotc >> p.y() >> dotc >> p.z() >> dotc >> q.w() >> dotc >> q.x() >> dotc >> q.y() >> dotc >> q.z();
time_stamp = time_stamp / 1e9; // transform to seconds
} else if (data_set_type_ == DATATYPE::VR) {
std::vector<std::string> strs;
std::string spacestr;
while (iss >> spacestr) {
strs.emplace_back(spacestr);
}
time_stamp = std::atof(strs[0].c_str());
p.x() = std::atof(strs[1].c_str());
p.y() = std::atof(strs[2].c_str());
p.z() = std::atof(strs[3].c_str());
q.x() = std::atof(strs[4].c_str());
q.y() = std::atof(strs[5].c_str());
q.z() = std::atof(strs[6].c_str());
q.w() = std::atof(strs[7].c_str());
// iss >> time_stamp >> spacestr >> p.x() >> spacestr >> p.y() >> spacestr >> p.z() >> spacestr >> q.x() >> spacestr >> q.y() >> spacestr >> q.z() >> spacestr >> q.w();
}
C++ ofstream和ifstream详细用法_Happinesspills的博客-优快云博客_c++ ofstream