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

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

void readFromTxt(const string fileName,vector& pointCloud)

{

ifstream myfile(fileName);

if(!myfile.is_open())

{

cout<<"open file error"<

}

while(getline(myfile,temp))

{

Point3f p;

vector strs = split(str,",");

p.x = stringToNum(strs.at(0));

p.y = stringToNum(strs.at(1));

p.z = stringToNum(strs.at(2));

pointCloud.push_back(p);

}

myfile.close();

}

template

Type stringToNum(const string& str)

{

istringstream iss(str);

Type num;iss >> num;

return num;

}

 

vector split(const string &str, const string &pattern)
{
    char * strc = new char[strlen(str.c_str()) + 1];
    strcpy(strc, str.c_str());
    vector 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;
}

 

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