[vtkusers] Exporting vtkVolume to STL/PLY

Ievgen Petrashchuk vaper at ukr.net
Fri Jul 31 11:16:19 EDT 2015


Hello, I've been playing with VTK examples and got the tool that renders 3D mesh, based on DICOM files. 
How can I export the mesh to .ply or .stl file? 

My code: 
#include <vtkVersion.h> #include <vtkSmartPointer.h> #include <vtkSphere.h> #include <vtkSampleFunction.h> #include <vtkSmartVolumeMapper.h> #include <vtkColorTransferFunction.h> #include <vtkPiecewiseFunction.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkVolumeProperty.h> #include <vtkCamera.h> #include <vtkImageShiftScale.h> #include <vtkImageData.h> #include <vtkPointData.h> #include <vtkDataArray.h> #include <vtkXMLImageDataReader.h> #include <vtkPLYWriter.h> #include <vtkDICOMImageReader.h> #include <vtkVolumeOutlineSource.h> 
int main(int argc, char* argv[]) {     // Verify input arguments     if ( argc != 3 )     {         std::cout << "Usage: " << argv[0]         << " FolderName" << "Export.ply" << std::endl;         return EXIT_FAILURE;     } 
    std::string folder = argv[1];     std::string output = argv[2]; 
    // Read all the DICOM files in the specified directory.     vtkSmartPointer<vtkDICOMImageReader> reader =             vtkSmartPointer<vtkDICOMImageReader>::New();     reader->SetDirectoryName(folder.c_str());     reader->Update(); 
    //Create vtkImageData     vtkSmartPointer<vtkImageData> imageData =             vtkSmartPointer<vtkImageData>::New(); 
    imageData->ShallowCopy(reader->GetOutput()); 
    //renderer     vtkSmartPointer<vtkRenderWindow> renWin =             vtkSmartPointer<vtkRenderWindow>::New();     vtkSmartPointer<vtkRenderer> ren1 =             vtkSmartPointer<vtkRenderer>::New();     ren1->SetBackground(0,0,0); 
    renWin->AddRenderer(ren1); 
    renWin->SetSize(301,300); // intentional odd and NPOT  width/height 
    vtkSmartPointer<vtkRenderWindowInteractor> iren =             vtkSmartPointer<vtkRenderWindowInteractor>::New();     iren->SetRenderWindow(renWin); 
    renWin->Render(); // make sure we have an OpenGL context. 
    vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper =             vtkSmartPointer<vtkSmartVolumeMapper>::New();     volumeMapper->SetBlendModeToComposite(); // composite first #if VTK_MAJOR_VERSION <= 5     volumeMapper->SetInputConnection(imageData->GetProducerPort()); #else   volumeMapper->SetInputData(imageData); #endif     vtkSmartPointer<vtkVolumeProperty> volumeProperty =             vtkSmartPointer<vtkVolumeProperty>::New();     volumeProperty->ShadeOff();     volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION); 
    vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =             vtkSmartPointer<vtkPiecewiseFunction>::New();     compositeOpacity->AddPoint(-1000.0,0.0);     compositeOpacity->AddPoint(0.0,0.0);     compositeOpacity->AddPoint(1000.0,0.89);     volumeProperty->SetScalarOpacity(compositeOpacity); // composite first. 
    vtkSmartPointer<vtkColorTransferFunction> color =             vtkSmartPointer<vtkColorTransferFunction>::New(); 
    color->AddRGBPoint(0,    0.0, 0.0, 0.0);     color->AddRGBPoint(1000,  1.0, 1, 1);     color->AddRGBPoint(-1000,    0.0, 0.0, 0.0);     volumeProperty->SetColor(color); 
    volumeProperty->SetInterpolationTypeToLinear();     volumeProperty->ShadeOff(); 
    vtkSmartPointer<vtkVolume> volume =             vtkSmartPointer<vtkVolume>::New();     volume->SetMapper(volumeMapper);     volume->SetProperty(volumeProperty); 

    ren1->AddViewProp(volume);     ren1->ResetCamera(); 
    // Render composite. In default mode. For coverage.     renWin->Render(); 
    // 3D texture mode. For coverage.     volumeMapper->SetRequestedRenderModeToDefault();     renWin->Render(); 
    // Software mode, for coverage. It also makes sure we will get the same     // regression image on all platforms.     volumeMapper->SetRequestedRenderModeToRayCast();     renWin->Render(); 
    iren->Start(); 
    return EXIT_SUCCESS; } 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150731/653a116f/attachment.html>


More information about the vtkusers mailing list