[vtkusers] Surface reconstruction from a point cloud

Dr Zeto dr_zeato at yahoo.com
Mon Mar 7 15:41:03 EST 2005


Hi all,
I am doing a very simple thing that is not working,
which  I dont know why. Given a cloud of points, I
want to generate a surface. VTK documentation shows
that there are classes vtkSurfaceReconstructionFilter
and vtkContourFilter for this purpose. Following is my
code which is not working, which is a direct
conversion of 
the example @
http://public.kitware.com/VTK/doc/release/4.0/html/c2_vtk_e_1.html#c2_vtk_e_vtkSurfaceReconstructionFilter.

The problem appears to be somewhere between the usage
of above mentioned classes. I have used
vtkSurfaceReconstructionFilter to generate the
volumetric distance field. But transferring the output
of vtkSurfaceReconstructionFilter to vtkContourFilter
doesnt seem to be as simple as given in the TCL
example  as vtkSurfaceReconstructionFilter::GetOutput
does not return vtkPolyData*, while
vtkContourFilter::SetInput requires vtkPolyData* as an
input. To fix this I manually transfer points and
faces through function DataSet2PolyData given below.
But when I get the output of vtkContourFilter, it is
completely empty. I am totally lost where I am messing
up.
Any help hint would be greatly appreciated.
Thanks!
Zeato
----------------------------------------

vtkPolyData* DataSet2PolyData( vtkDataSet* pvtkDataSet
)
{
	vtkPolyData* pvtkPolyData = vtkPolyData::New();
	vtkPoints* pvtkPoints = vtkPoints::New();

	int nPoints = pvtkDataSet->GetNumberOfPoints();
	for( int i = 0; i < nPoints; i++ )
	{
		pvtkPoints->InsertPoint( i, pvtkDataSet->GetPoint( i
) );
	}
	pvtkPolyData->SetPoints( pvtkPoints );
	
	int nFaces = pvtkDataSet->GetNumberOfCells();
	vtkCellArray* pvtkFaces = vtkCellArray::New();
	for( int j = 0; j < nFaces; j++ )
	{
		pvtkFaces->InsertNextCell( pvtkDataSet->GetCell( j )
);
	}

	pvtkPolyData->SetPolys( pvtkFaces );
	return pvtkPolyData;
}


int GenerateSurface()
{
	int nVertex = 0;
	float pfVertex[ 3 ];

	int nFileStatus = _access( "EightPointCloud.txt",
READ_PERMISSION );
	if( nFileStatus == -1 )
	{
		cout << "File does not exist or access denied.\n";
		exit( 1 );
	}
	
	vtkPoints* pvtkPoints = vtkPoints::New();

	CFile oFile( "EightPointCloud.txt", CFile::typeBinary
| CFile::modeRead );
	while( oFile.Read( pfVertex, sizeof( pfVertex ) ) &&
nVertex < 5000 )
	{
		pvtkPoints->InsertPoint( nVertex++, pfVertex );
	}
	oFile.Close();
	if( !nVertex )
	{
		cout << "No points read." << endl;
		exit( 1 );
	}

	vtkPolyData* pvtkPolyData = vtkPolyData::New();
	pvtkPolyData->SetPoints( pvtkPoints );

	vtkSurfaceReconstructionFilter* pvtkSurfaceGenerator
= vtkSurfaceReconstructionFilter::New();
	pvtkSurfaceGenerator->SetInput( pvtkPolyData );
	pvtkSurfaceGenerator->Update();

	vtkPolyData* pvtkPolyData2 = DataSet2PolyData(
pvtkSurfaceGenerator->GetOutput() );
	vtkContourFilter* pvtkContourFilter =
vtkContourFilter::New();
	pvtkContourFilter->SetInput( pvtkPolyData2 );
	pvtkContourFilter->SetValue( 0, 0.0 );
	pvtkContourFilter->Update();

	cout <<
pvtkContourFilter->GetOutput()->GetNumberOfCells() <<
endl;
	cout <<
pvtkContourFilter->GetOutput()->GetNumberOfPoints() <<
endl;

	vtkReverseSense* pvtkReverser =
vtkReverseSense::New();

		pvtkReverser->SetInput(
pvtkContourFilter->GetOutput() );
				pvtkReverser->ReverseCellsOn();
				pvtkReverser->ReverseNormalsOn();
				pvtkReverser->Update();
				
				cout <<
pvtkReverser->GetOutput()->GetNumberOfCells() << endl;
				cout <<
pvtkReverser->GetOutput()->GetNumberOfPoints() <<
endl;
	

	vtkFloatArray* pvtkColor = vtkFloatArray::New();
	vtkPolyData* pvtkSurface = pvtkReverser->GetOutput();
	/*
	for( int i = 0; i < pvtkSurface->GetNumberOfCells();
i++ )
		{
			pvtkColor->InsertNextTuple3( .5, .7, .6 );
		}*/
	
	cout << pvtkSurface->GetNumberOfCells() << endl;
	cout << pvtkSurface->GetNumberOfPoints() << endl;

	//pvtkSurface->GetPointData()->SetScalars( pvtkColor
);
	vtkPolyDataMapper* pvtkMapper =
vtkPolyDataMapper::New();
	pvtkMapper->SetInput( pvtkSurface );
	pvtkMapper->ScalarVisibilityOff();


	vtkActor *pvtkActor = vtkActor::New();
	pvtkActor->SetMapper( pvtkMapper );
//	pvtkActor->GetProperty()->SetDiffuseColor( 1.0000,
0.3882, 0.2784 );
//	pvtkActor->GetProperty()->SetSpecularColor( .6, .6,
.4 );
//	pvtkActor->GetProperty()->SetSpecular( .4 );
//	pvtkActor->GetProperty()->SetSpecularPower( 50 );
	pvtkActor->GetProperty()->SetOpacity( .4 );

//	vtkPolyDataMapper *objectMapper =
vtkPolyDataMapper::New();
//		objectMapper->SetInput(normals->GetOutput());
//object
//		objectMapper->SetScalarRange(0,8);
//	vtkActor *objectActor = vtkActor::New();
//		objectActor->SetMapper(objectMapper);
//		objectActor->GetProperty()->SetOpacity(0.3);;
	

	vtkRenderer *renderer = vtkRenderer::New();
	vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(renderer);

	vtkRenderWindowInteractor *iren =
vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);

	renderer->AddActor( pvtkActor );
	renderer->SetBackground( 1, 1, 1 );

	renWin->SetSize(560,560);	

	// interact with data	
	renWin->Render();
	iren->Start();

	// Clean up
	pvtkPolyData2->Delete();
	pvtkActor->Delete();
	pvtkMapper->Delete();
	pvtkPoints->Delete();
	pvtkContourFilter->Delete();
	pvtkReverser->Delete();
	pvtkSurfaceGenerator->Delete();
	renWin->Delete();
	renderer->Delete();
	iren->Delete();
	pvtkPolyData->Delete();
	pvtkColor->Delete();

	return 0;
}


	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/



More information about the vtkusers mailing list