dutren的个人博客分享 http://blog.sciencenet.cn/u/dutren

博文

VC读取TXT数据样例以及曲面绘制实现

已有 3142 次阅读 2011-11-20 20:17 |系统分类:科研笔记

/此小段函数是当初用来读取txt文件中的三维坐标数据。
//数据是由双目视觉定位空间标志点得到的。
//文件的格式为列格式(如下所示):
//ID
//x
//y
//z
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
void main()
{
fstream fs;//("file.txt",ios::in,filebuf::sh_read);
fs.setmode();
fs.open("file.txt",ios::in);
char str[10];
double pix[3];
int count=0;
while(1)
{
if(fs.getline(str,10,'n'))
{
if(strlen(str)!=0)
{
count=count%4;
if(count!=0)
{
pix[count-1]=atof(str);
if(count==3)
{
cout<<"x="<<pix[0]<<";y="<<pix[1]<<";z="<<pix[2]<<endl;
}
}
count++;

}
}
else break;
}
fs.close();
}
//感谢常伟同学的热心指点

=======================以下是三维曲面的绘制简单实现============================
fstream fs;//("file.txt",ios::in,filebuf::sh_read);
fs.setmode();
fs.open("rebuild3D.txt",ios::in);
char str[10];
//pixel3D point3D[140];
//double pix[3];
CVector3 point3D[140];
int count=0;
int i=0;
while(1)
{
if(fs.getline(str,10,'n'))
{
if(strlen(str)!=0)
{   
count=count%4;
if(count==1)
{
point3D[i].x=atof(str)/250+0.5;
}
if(count==2)
{
point3D[i].y=atof(str)/250+0.5;
}
if(count==3)
{
point3D[i].z=atof(str)/250-1.4;
i++;
}
//i++;
count++;
}
}
else break;
}
DrawAxis();
int column=9;
int row=14;
for(  i=0;i<column-1;i++)
for(int j=0;j<row-1;j++)
{ GLfloat color=0.0;
glColor3f(color+float(j)/14,0.12f+float(i)/9,0.3f);
glBegin(GL_QUADS); //  绘制正方形,注意点事顺时针
glVertex3f(point3D[j+i*14].x,point3D[j+i*14].y,point3D[j+i*14].z);
glVertex3f( point3D[j+1+i*14].x,point3D[j+1+i*14].y,point3D[j+1+i*14].z);
glVertex3f(point3D[j+15+i*14].x,point3D[j+15+i*14].y,point3D[j+15+i*14].z);
glVertex3f( point3D[j+14+i*14].x,point3D[j+14+i*14].y,point3D[j+14+i*14].z);
glEnd(); // 正方形绘制结束
}


glFlush();


https://blog.sciencenet.cn/blog-347289-510014.html


下一篇:量变决定质变
收藏 IP: 202.118.77.*| 热度|

1 梁满仓

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-25 10:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部