您的位置:首页 > 其它

VTK读取三维点云图并显示

2013-12-31 10:30 330 查看
用VTK读取三维点云图并显示的程序遇到问题,求帮助

该程序是读取txt里的三维点云数据然后显示出来

程序如下:

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkPoints.h"

#include "vtkUnsignedCharArray.h"

#include "vtkCamera.h"

#include "vtkPolyVertex.h"

#include "vtkUnstructuredGrid.h"

#include "vtkDataSetMapper.h"

#include "vtkCellLinks.h"

#include "vector"

#include "cstdlib"

#include "stdlib.h"

//#include "stdafx.h"

#include "string"

#include "string.h"

#include "vtkPolyDataMapper.h"

#include "vtkActor.h"

#include "vtkProperty.h"

#include "iostream"

#include "fstream"

using namespace std;

int main()

{

vtkRenderer *ren = vtkRenderer::New();

vtkPoints *points = vtkPoints::New();

vtkUnsignedCharArray *Colors = vtkUnsignedCharArray :: New();

unsigned char Color[] = { 0, 0, 0 };

vector<unsigned char> *oColors = new vector<unsigned char>;

string line;

int iPts = 0;

double x = 0, y = 0, z = 0;

int iStart = 0, iEnd = 0;

ifstream fin("D:\\VTKstudy\\vtkpoint1\\bin\\leaf2.txt");

if(!fin){

cout << "Unable to open myfile";

exit(1); // terminate with error

}

string str;

while(getline(fin,str)){ //一行一行地读,直到失败为止

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

x= atof(line.c_str());

iStart = iEnd + 1;

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

y= atof(line.c_str());//strncpy(char_num,str+2+k,3);

iStart = iEnd + 1;

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

z= atof(line.c_str());

points->InsertNextPoint(x, y, z);

iStart = iEnd + 1;

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

Color[0]= reinterpret_cast<unsigned char>(line.c_str());

oColors->push_back(Color[0]);

iStart = iEnd + 1;

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

Color[1]= reinterpret_cast<unsigned char>(line.c_str());

oColors->push_back(Color[1]);

iStart = iEnd + 1;

iEnd = str.find_first_of(' ', iStart );

line.assign(str,iStart, iEnd - iStart);

Color[2]= reinterpret_cast<unsigned char>(line.c_str());

oColors->push_back(Color[2]);

iStart = 0;

iPts++;

}

return 0;

vtkPolyVertex *polyvertex =vtkPolyVertex ::New();

polyvertex->GetPointIds()->SetNumberOfIds(iPts);

for(int i = 0; i < iPts; i++)

{

polyvertex->GetPointIds()->SetId(i, i);

}

vtkUnstructuredGrid * grid = vtkUnstructuredGrid:: New();

grid->SetPoints(points);

grid->InsertNextCell(polyvertex->GetCellType(),polyvertex->GetPointIds());

vtkDataSetMapper *map1 = vtkDataSetMapper :: New();

map1->SetInput(grid);

map1->SetColorModeToDefault();

map1->SetScalarModeToUsePointFieldData();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: