VTK读取一个TXT文档中的三维点坐标显示三维点云
2017-02-20 20:27
543 查看
VTK读取一个TXT文档中的三维点坐标就可以显示三维点云,txt文档中的格式为
X坐标 Y坐标
Z坐标
如下所示:
附上代码如下:
智能指针版本
#include <iostream>
#include <vector>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
using namespace std;
void main(int argc, char* argv[])
{
vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> vertices =vtkSmartPointer<vtkCellArray>::New(); //_存放细胞顶点,用于渲染(显示点云所必须的)
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer< vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
//_读进点云数据信息
FILE*fp = NULL; fp=fopen("E:\\斯坦福兔子3000点.txt","r"); //2DpointDatas.txt
if(!fp)
{
printf("打开文件失败!!\n");
int m;
cin>>m;
exit(0);
}
double x=0,y=0,z=0;
int i = 0;
while (!feof(fp))
{
fscanf(fp,"%lf %lf %lf",&x,&y,&z);
m_Points->InsertPoint(i,x,y,z); //_加入点信息
vertices->InsertNextCell(1); //_加入细胞顶点信息----用于渲染点集
vertices->InsertCellPoint(i);
i ++;
}
fclose(fp);
//_创建待显示数据源
polyData->SetPoints(m_Points); //_设置点集
polyData->SetVerts(vertices); //_设置渲染顶点
pointMapper->SetInput( polyData );
pointActor->SetMapper( pointMapper );
pointActor->GetProperty()->SetColor(0.0,0.1,1.0);
pointActor->GetProperty()->SetAmbient(0.5);
pointActor->GetProperty()->SetPointSize(2);
//pointActor->GetProperty()->SetRepresentationToWireframe();
//pointActor->GetProperty()->SetRepresentationToSurface();
ren1->AddActor( pointActor );
ren1->SetBackground( 0, 0, 0);
renWin->AddRenderer( ren1 );
renWin->SetSize(800,800);
iren->SetInteractorStyle(istyle);
iren->SetRenderWindow(renWin); //交互
renWin->Render();
iren->Start();
}
附上结果图片
X坐标 Y坐标
Z坐标
如下所示:
附上代码如下:
#include <iostream> #include <vector> #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkProperty.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkPoints.h" #include "vtkPolyVertex.h" #include "vtkUnstructuredGrid.h" #include "vtkDataSetMapper.h" #include "vtkPolyData.h" #include "vtkCellArray.h" #include "vtkInteractorStyleTrackball.h" #include "vtkPolyDataMapper.h" using namespace std; void main(int argc, char* argv[]) { vtkPoints *m_Points = vtkPoints::New(); vtkCellArray *vertices = vtkCellArray::New(); //_存放细胞顶点,用于渲染(显示点云所必须的) vtkPolyData *polyData = vtkPolyData::New(); vtkPolyDataMapper *pointMapper = vtkPolyDataMapper::New(); vtkActor *pointActor = vtkActor::New(); vtkRenderer *ren1= vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); vtkInteractorStyleTrackball *istyle = vtkInteractorStyleTrackball::New(); //_读进点云数据信息 FILE*fp = NULL; fp=fopen("point.txt","r"); //2DpointDatas.txt if(!fp) { printf("打开文件失败!!\n"); int m; cin>>m; exit(0); } double x=0,y=0,z=0; int i = 0; while (!feof(fp)) { fscanf(fp,"%lf %lf %lf",&x,&y,&z); m_Points->InsertPoint(i,x,y,z); //_加入点信息 vertices->InsertNextCell(1); //_加入细胞顶点信息----用于渲染点集 vertices->InsertCellPoint(i); i ++; } fclose(fp); //_创建待显示数据源 polyData->SetPoints(m_Points); //_设置点集 polyData->SetVerts(vertices); //_设置渲染顶点 pointMapper->SetInput( polyData ); pointActor->SetMapper( pointMapper ); pointActor->GetProperty()->SetColor(0.0,0.1,1.0); pointActor->GetProperty()->SetAmbient(0.5); pointActor->GetProperty()->SetPointSize(2); //pointActor->GetProperty()->SetRepresentationToWireframe(); //pointActor->GetProperty()->SetRepresentationToSurface(); ren1->AddActor( pointActor ); ren1->SetBackground( 0, 0, 0); renWin->AddRenderer( ren1 ); renWin->SetSize(800,800); iren->SetInteractorStyle(istyle); iren->SetRenderWindow(renWin); //交互 renWin->Render(); iren->Start(); //删除各指针 m_Points->Delete(); vertices->Delete(); polyData->Delete(); pointMapper->Delete(); pointActor->Delete(); ren1->Delete(); renWin->Delete(); iren->Delete(); istyle->Delete(); }
智能指针版本
#include <iostream>
#include <vector>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
using namespace std;
void main(int argc, char* argv[])
{
vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> vertices =vtkSmartPointer<vtkCellArray>::New(); //_存放细胞顶点,用于渲染(显示点云所必须的)
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer< vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
//_读进点云数据信息
FILE*fp = NULL; fp=fopen("E:\\斯坦福兔子3000点.txt","r"); //2DpointDatas.txt
if(!fp)
{
printf("打开文件失败!!\n");
int m;
cin>>m;
exit(0);
}
double x=0,y=0,z=0;
int i = 0;
while (!feof(fp))
{
fscanf(fp,"%lf %lf %lf",&x,&y,&z);
m_Points->InsertPoint(i,x,y,z); //_加入点信息
vertices->InsertNextCell(1); //_加入细胞顶点信息----用于渲染点集
vertices->InsertCellPoint(i);
i ++;
}
fclose(fp);
//_创建待显示数据源
polyData->SetPoints(m_Points); //_设置点集
polyData->SetVerts(vertices); //_设置渲染顶点
pointMapper->SetInput( polyData );
pointActor->SetMapper( pointMapper );
pointActor->GetProperty()->SetColor(0.0,0.1,1.0);
pointActor->GetProperty()->SetAmbient(0.5);
pointActor->GetProperty()->SetPointSize(2);
//pointActor->GetProperty()->SetRepresentationToWireframe();
//pointActor->GetProperty()->SetRepresentationToSurface();
ren1->AddActor( pointActor );
ren1->SetBackground( 0, 0, 0);
renWin->AddRenderer( ren1 );
renWin->SetSize(800,800);
iren->SetInteractorStyle(istyle);
iren->SetRenderWindow(renWin); //交互
renWin->Render();
iren->Start();
}
附上结果图片
相关文章推荐
- VTK读取一个TXT文档中的三维点坐标显示三维点云
- VTK 点云重建和读取.txt文档显示三维点云
- VTK生成分布在球内部或者表面的三维点云坐标,将点云坐标保存为txt文本文档
- 汇编语言:请写一个程序把当前目录下的TEXT.TXT文件中的字符读出来显示在屏幕上,当遇到结束符时就停止读取。
- 第二周第四天(解析/过滤文件/找d盘里的所有txt文件/FileInputStream/FileOutputStream/拷贝文件/readLine读取一个文本行/在一个文档里写入内容)
- Android中读取一个txt文本文件,然后显示到界面上的操作
- 如何打开一个已存在的txt格式的文本文档 ,并显示在QTextEdit中
- PYTHON读取三维点云球坐标数据并动态生成三维图像与着色
- OSG读取显示原始三维数据_txt格式
- openGL三维网格坐标,旋转,缩放,灯光设置,纹理读取,模型读取(MFC单文档)
- 读取一个txt文档中的内容
- 将数据库内表中的数据导出到txt文档中,并且显示一个对话框,提示用户保存文件的位置
- javaSE之如何将一个文档显示出来(,txt,.doc,.....)
- vtk实战(九)—读取三维点坐标2
- openGL三维网格坐标,旋转,缩放,灯光设置,纹理读取,模型读取(MFC单文档)
- 如何matlab 读取同一个文件中的多个txt 文档
- vtk实战(七)—读取三维点坐标
- 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来
- VTK读取三维点云图并显示
- 将txt文档中内容导入到数据库,并且显示一个对话框,提示用户选择文件的位置