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

在三维重建,点云处理中,经常需要读取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;
}

猜你喜欢

转载自blog.csdn.net/qq_29441995/article/details/81188781