用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 *oColors = new vector;
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(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(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(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();