从文本文件(txt)中读取点云,每一行的坐标格式为:x,y,z

本文介绍了一种使用C++从TXT文件中读取点云数据的方法,包括读取文件、解析每一行数据为浮点数并存储为点云的详细过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在三维重建,点云处理中,经常需要读取txt格式的点云,下面就是对点云的读取的c++代码,

void readFromTxt(const string fileName,vector<Point3f>& pointCloud)

{

ifstream myfile(fileName);

if(!myfile.is_open())

{

cout<<"open file error"<<endl;exit(0);

}

while(getline(myfile,temp))

{

Point3f p;

vector<string> strs = split(str,",");

p.x = stringToNum<float>(strs.at(0));

p.y = stringToNum<float>(strs.at(1));

p.z = stringToNum<float>(strs.at(2));

pointCloud.push_back(p);

}

myfile.close();

}

template<class Type>

Type stringToNum(const string& str)

{

istringstream iss(str);

Type num;iss >> num;

return num;

}

 

vector<string> split(const string &str, const string &pattern)
{
    char * strc = new char[strlen(str.c_str()) + 1];
    strcpy(strc, str.c_str());
    vector<string> resultVec;
    char* tmpStr = strtok(strc, pattern.c_str());
    while (tmpStr != NULL)
    {
        resultVec.push_back(string(tmpStr));
        tmpStr = strtok(NULL, pattern.c_str());
    }
    delete[] strc;
    return resultVec;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值