点云数据处理

点云数据处理
点云数据处理

c++对txt文件的读取与写入/* 这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用~

*/ #include

#include

#include

using namespace std; i

nt main(){ char buffer[256];

ifstream myfile ("c:\\a.txt");

ofstream outfile("c:\\b.txt");

if(!myfile){ cout << "Unable to open myfile";

exit(1); // terminate with error }

if(!outfile){

cout << "Unable to open otfile";

exit(1); // terminate with error } int a,b; int i=0,j=0; int data[6][2]; while (! myfile.eof() ) { myfile.getline (buffer,10); sscanf(buffer,"%d %d",&a,&b); cout<

tob_id_3208

cout<头文件读:从外部文件中将数据读到程序中来处理对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:int a,b; ifstream infile; infile.open("myfile.txt"); //注意文件的路径infile>>a>>b; //两行数据可以连续读出到变量里infile.close() 如果是个很大的多行存储的文本型文件可以这么读:char buf[1024]; //临时保存读取出来的文件内容string message; ifstream infile; infile.open("myfile.js"); if(infile.is_open()) //文件打开成功,说明曾经写入过东西{ while(infile.good() && !infile.eof()) { memset(buf,0,1024); infile.getline(buf,1204); message = buf; ...... //这里可能对message做一些操作cout<

outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名if(outfile.is_open()) { outfile< #i nclude #i nclude using namespace std; //////////////从键盘上读取字符的函数void read_save(){ char c[80]; ofstream outfile("f1.dat");//以输出方工打开文件if(!outfile){ cerr<<"open error!"<=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符

是字符outfile.put(c[i]);//将字母字符存入磁盘文件cout<

} ofstream outfile("f3.dat");//定义输出流f3.dat文件if(!outfile){ cerr<<"open error!"<=97) ch=ch-32; outfile.put(ch); cout< #include using namespace std; typedef struct node{ int data; struct node *next; } node; node *creat(ifstream &ifp)

{ node *h=NULL,*p=NULL,*q=NULL; int data; while (ifp>>data) { p=new node; p->data=data; p->next=NULL; if (!h) h=p; else q->next=p; q=p; } return h; } void prt(node *h) { if (h) { cout<data<next); } } int _tmain(int argc, _TCHAR* argv[]) { //--file1 open ifstream fp("d:\\data.txt",ifstream::in); node *hst=creat(fp); fp.close(); prt(hst); //--file2 open ifstream r("d:\\test.txt",ifstream::in); if(!r) { cout<<"打开文件出错!"<>line) { cout<

函数用于读取.xyz的点云文件,点云的格式为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

17.371559 -6.531680 -8.080792 0.242422 0.419118 0.874970

15.640106 -16.101347 -9.550241 -0.543610 -0.382877 0.746922

17.750742 -6.395478 -8.307115 0.333093 0.494766 0.802655

15.432834 -15.947010 -9.587061 -0.548083 -0.385148 0.742473

23.626318 -7.729815 -13.608750 0.081697 0.502976 0.860431

15.300377 -15.610346 -9.547507 -0.569658 -0.341132 0.747743

23.975805 -7.512131 -13.775388 0.082388 0.564137 0.821561

24.251831 -7.345085 -13.949208 0.099309 0.574142 0.812711

14.999881 -15.463743 -9.748975 -0.629676 -0.333713 0.701530

14.804974 -15.162496 -9.758424 -0.616575 -0.334426 0.712737

27.607445 -6.731058 -16.160894 0.387612 0.713240 0.583991

14.560967 -14.955154 -9.909436 -0.638394 -0.335827 0.692584

27.938255 -6.707172 -16.443462 0.379390 0.740941 0.554139

14.290791 -14.852806 -10.137550 -0.692395 -0.381273 0.612552

14.386531 -15.114174 -10.178914 -0.719801 -0.337913 0.606384

14.001437 -14.247000 -10.103112 -0.735267 -0.343587 0.584235

13.762934 -13.909647 -10.200064 -0.752330 -0.295280 0.588906

前面3个数字是坐标,后面3个数字是法向量,有多少行就代表有多少个点。

代码:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

struct Point3d

{

Point3d(){x=y=z=0.0f;}

float x;

float y;

float z;

bool operator<(const Point3d& p2 ) const

{

if (x == p2.x)

{

if (y == p2.y)

{

return (z < p2.z);

}

else return (y < p2.y);

}

else return (x < p2.x);

}

};

int ReadPointCloudFile(const std::string& strPath)

{

FILE *ifp = fopen(strPath.c_str(),"r");

if(ifp == NULL)

{

printf("Cannot open point cloud file.\n");

return -1;

}

//获取行数,即点的数量,用于设置std::vector的容量

const int BUFSIZE = 512;

char buf[BUFSIZE];

int rowNumber = 0;

while(fgets(buf,BUFSIZE,ifp) != NULL)

{

++rowNumber;

}

fclose(ifp);

ifp = 0;

//重新打开文件,读取所有点的信息

ifp = fopen(strPath.c_str(),"r");

Point3d pointCoor;

Point3d pointNormal;

std::vector vecPtCoor; //点坐标

std::vector vecPtNormal; //法向量

vecPtCoor.reserve(rowNumber);

vecPtNormal.reserve(rowNumber);

while(fgets(buf,BUFSIZE,ifp) != NULL) // read info line by line

{

sscanf(buf, "%f %f %f %f %f %f",

&(pointCoor.x), &(pointCoor.y), &(pointCoor.z),

&(pointNormal.x), &(pointNormal.y), &(pointNormal.z));

vecPtCoor.push_back(pointCoor);

vecPtNormal.push_back(pointNormal);

}

fclose(ifp);

ifp = 0;

return 0;

相关主题
相关文档
最新文档