[vtkusers] Coverting volume data into an stl file

Rhys Thomas rgt at informatics.bangor.ac.uk
Wed Dec 6 11:37:56 EST 2006


Greetings vtkusers,

I was wondering if anyone has any suggestions that can help me convert a 
volume dataset in an stl file. I have made an attempt a coding a 
solution, which is included below, however I am not getting the results 
I expected. The volume that I am using is read from a file with a .vtk 
extension. I am quite sure that this file is not causing the problem 
because I have been rendering the data in vtk for quite a while now. I 
have previously loaded the contents of the file into 
vtkStructuredPoints, as I have done in my solution here.

I chose the classes and methods that I used by fairly randomly looking 
through the documentation and picking out "useful" looking classes. I 
have tried several different ways of converting from vtkStructuredPoints 
into vtkPolyData, and have left them all in my solution, with most of 
them commented out.

When I enable the vtkDataSetSurfaceFilter and run the program I get a a 
cuboid appearing in my stl viewing software, and I appear to have no 
vertices, but nearly a million polygons. When I enable the 
vtkImageDataGeometryFilter the output suggests that I have many vertices 
but no polygons, and nothing appears to be rendered in my stl viewer. 
The file output is as follows:

solid ascii
endsolid

I have tried using a threshold value when I am using the 
vtkImageDataGeometryFilter, as there is a large amount of air that I do 
not wish to be present in the surface, however after trying several 
values for the threshold I have not yet achieved any meaningful results. 
I should probably note that the number of vertices changes slightly when 
I adjust the threshold value.

void main(int argc, char **argv)
{
    vtkOutputWindow::GetInstance()->PromptUserOn();
    if(!(argc == 3))
    {
        printf("Usage: VolumeToSurface input.vtk output.stl");
        return;
    }

    vtkStructuredPoints *inputVolume = vtkStructuredPoints::New();
    vtkStructuredPointsReader *volumeReader = 
vtkStructuredPointsReader::New();
    volumeReader->SetFileName(argv[1]);
    volumeReader->Update();
    inputVolume = volumeReader->GetOutput();
    inputVolume->Update();

//    vtkDataSetSurfaceFilter *volumeToSurface = 
vtkDataSetSurfaceFilter::New();
    vtkImageDataGeometryFilter *volumeToSurface = 
vtkImageDataGeometryFilter::New();
//    vtkExtractUnstructuredGrid *volumeToSurface = 
vtkExtractUnstructuredGrid::New();
//    vtkRectilinearGridGeometryFilter *volumeToSurface = 
vtkRectilinearGridGeometryFilter::New();
//    volumeToSurface->SetPieceInvariant(2);
    volumeToSurface->SetInput(inputVolume);
    volumeToSurface->SetExtent(0, 512, 0, 512, 0, 512);
    volumeToSurface->ThresholdValueOn();
    volumeToSurface->SetThresholdValue(0.0);
    vtkPolyData *outputSurface = volumeToSurface->GetOutput();
    outputSurface->Update();

    vtkTriangleFilter *triangleOnly = vtkTriangleFilter::New();
    triangleOnly->SetInput(outputSurface);
    vtkPolyData *outputTriangles = triangleOnly->GetOutput();
    outputTriangles->Update();
    printf("Vertices: %d\tPolys: %d\n", 
outputTriangles->GetNumberOfVerts(),
        outputTriangles->GetNumberOfPolys());

    vtkSTLWriter *surfaceWriter = vtkSTLWriter::New();
    surfaceWriter->SetInput(outputTriangles);
    surfaceWriter->SetFileName(argv[2]);
    surfaceWriter->Update();
}

Please excuse all of the Update() calls, I tried them out of 
semi-desperation, and not really expecting them to do anything meaningful.

Thanks

Rhys Thomas



More information about the vtkusers mailing list