本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:VTK读取一个TXT文档中的三维点坐标显示三维点云
原文链接:https://www.stubbornhuang.com/279/
发布于:2019年11月08日 22:58:29
修改于:2020年01月07日 14:21:04
1 txt文档点云数据格式
将三维点云坐标按 X Y Z的方式存入到txt文档中,如下图所示,
2 Vtk5.10版本非智能指针代码
#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();
}
2 Vtk5.10版本智能指针代码
#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();
}
4 示例结果
当前分类随机文章推荐
- VTK以批量三维点坐标为中心(点云)绘制球体,可用于标识特征点或者是化学分子 阅读4827次,点赞0次
- VTK能干什么?VTK大部分功能的细节简介,VTK能打开的文件格式 阅读5846次,点赞3次
- VTK读取序列的Dicom医学图片,用Marchingcube进行重建,并保存为obj文件 阅读5894次,点赞2次
- VTK读取一个TXT文档中的三维点坐标显示三维点云 阅读6268次,点赞4次
- VTK - 冠脉重建点匹配坐标数据下载 阅读3901次,点赞5次
全站随机文章推荐
- 资源分享 - Computer Graphics Programming in OpenGL with C++, Second Edition 英文高清PDF下载 阅读2800次,点赞0次
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明 阅读5848次,点赞2次
- C++ - std::map - 存储动态指针时正确释放内存 阅读4260次,点赞1次
- C++STL容器 - std::map查找元素与判断键值是否存在方法总结 count,find,contains,equal_range,lower_bound,upper_bound 阅读1091次,点赞0次
- WordPress - home_url()函数,获取网站主页url链接 阅读915次,点赞0次
- 资源分享 - Digital Image Processing , Fourth Edition 英文高清PDF下载 阅读2852次,点赞1次
- C++ - 我的代码风格/代码规范备忘 阅读902次,点赞0次
- 资源分享 - Game Engine Architecture (Second Edition)英文高清PDF下载 阅读2441次,点赞0次
- 资源分享 - Ray Tracing - The Rest of Your Life英文高清PDF下载 阅读2446次,点赞0次
- 三维重建 - 基于RBF的三维网格重建 阅读5283次,点赞7次
楼主可以给我私发一份吗,谢谢!