[vtkusers] Converting PolyData into ImageData / Voxelizer

Reinhold Füreder R.Fureder at exeter.ac.uk
Tue Mar 25 08:40:11 EST 2003


Hi,

According to the e-mails
http://public.kitware.com/pipermail/vtkusers/2002-April/011020.html and
http://public.kitware.com/pipermail/vtkusers/2002-March/010409.html I
tried to convert STL data into image data or in other words, tried to
voxelize it. But the code below does not generate a single voxel. What
is the proplem???

(Attached are the test STL file "unit_cube.stl", the CMake build file
and for the sake of simplicity the source code file from below.)

Thanks a lot,
	Reinhold


--- Code ----
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInterActor.h"
#include "vtkRenderer.h"
#include "vtkSTLReader.h"
#include "vtkPolyDataMapper.h"
#include "vtkImageGridSource.h"
#include "vtkPolyDataToImageStencil.h"
#include "vtkImageStencil.h"
#include "vtkImageStencilData.h"
#include "vtkImageMarchingCubes.h"
#include "vtkPolyDataNormals.h"
#include "vtkPolyData.h"
#include "vtkImageData.h"


int main (int argc, char *argv[])
{
    // Read STL file:
    vtkSTLReader *stlReader = vtkSTLReader::New ();
    stlReader->SetFileName ("unit_cube.stl");

    //-----------------------------------------------------
    // Sampling:
    //-----------------------------------------------------
    vtkPolyData *stlData;
    vtkImageData *imageData;
    float bounds[6], length, center[3];
    vtkIdType nmbOfPolys;
    float spacings[3];

    // Define spacing:
    spacings[0] = 0.5;
    spacings[1] = 0.5;
    spacings[2] = 0.5;

    stlData = stlReader->GetOutput ();
    stlReader->Update ();
    stlData->GetBounds (bounds);
    length = stlData->GetLength ();
    stlData->GetCenter (center);
    nmbOfPolys = stlData->GetNumberOfPolys ();

    // Create grid source for sampling:
    vtkImageGridSource *imageGrid = vtkImageGridSource::New ();
    imageGrid->SetDataScalarTypeToUnsignedChar ();
/*
    imageGrid->SetDataExtent (bounds[0], bounds[1] / spacings[0],
            bounds[2], bounds[3] / spacings[1],
            bounds[4], bounds[5] / spacings[2]);
*/
    imageGrid->SetDataExtent (-bounds[1] / spacings[0] + bounds[0], 3*
bounds[1] / spacings[0],
            -bounds[3] / spacings[1] + bounds[2], 3 * bounds[3] /
spacings[1],
            -bounds[5] / spacings[2] + bounds[4], 3 * bounds[5] /
spacings[2]);

    imageGrid->SetDataSpacing (spacings);
    imageGrid->SetDataOrigin (0.0, 0.0, 0.0);
    imageGrid->SetLineValue (0);
    imageGrid->SetFillValue (0);

    // Convert STL polydata to image stencil data:
    vtkPolyDataToImageStencil *dataToStencil =
vtkPolyDataToImageStencil::New ();
    dataToStencil->SetInput (stlReader->GetOutput ());

    // Create image stencil with image stencil data and grid source:
    vtkImageStencil *imageStencil = vtkImageStencil::New ();
    imageStencil->SetInput (imageGrid->GetOutput ());
    imageStencil->SetStencil (dataToStencil->GetOutput ());
    imageStencil->SetBackgroundValue (0);
    
    //-----------------------------------------------------
    // Rendering:
    //-----------------------------------------------------

    // Use ContourFilter (MC):
    //vtkKitwareContourFilter contour
    //vtkMarchingCubes contour
    vtkImageMarchingCubes *contour = vtkImageMarchingCubes::New ();
    contour->SetInput (imageStencil->GetOutput ());
    contour->SetValue (0, 0.5);
    
    vtkPolyDataNormals *normals = vtkPolyDataNormals::New ();
    normals->SetInput (contour->GetOutput ());
    normals->SetFeatureAngle (60.0);

    vtkPolyDataMapper *sampledMapper = vtkPolyDataMapper::New ();
    sampledMapper->SetInput (normals->GetOutput ());
    sampledMapper->ScalarVisibilityOff ();

    vtkActor *sampledActor = vtkActor::New ();
    sampledActor->SetMapper (sampledMapper);

    // Visualize STL file:
    vtkPolyDataMapper *stlMapper = vtkPolyDataMapper::New ();
    stlMapper->SetInput (stlReader->GetOutput ());

    vtkActor *stlActor = vtkActor::New ();
    stlActor->SetMapper (stlMapper);

    vtkRenderer *ren1= vtkRenderer::New ();
    ren1->AddActor (stlActor);
    ren1->SetViewport (0, 0, 0.5, 1);
    ren1->SetBackground ( 0.1, 0.2, 0.4);

    vtkRenderer *ren2= vtkRenderer::New ();
    ren2->AddActor (sampledActor);
    ren2->SetViewport (0.5, 0, 1, 1);
    ren2->SetBackground (0.1, 0.2, 0.4);

    vtkRenderWindow *renWin = vtkRenderWindow::New ();
    renWin->AddRenderer (ren1);
    renWin->AddRenderer (ren2);
    renWin->SetSize (1000, 500);

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

    renWin->Render ();
    interActor->Start ();

    //-----------------------------------------------------
    // Free up any objects we created:
    //-----------------------------------------------------
    stlReader->Delete();
    stlMapper->Delete();
    stlActor->Delete();
    ren1->Delete();
    ren2->Delete();
    renWin->Delete();
    // and so on...

    return 0;
}
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CMakeLists.txt
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030325/a5539a22/attachment.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unit_cube.stl
Type: application/vnd.ms-pki.stl
Size: 6792 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030325/a5539a22/attachment.stl>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Sampling.cpp
Type: application/octet-stream
Size: 4336 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030325/a5539a22/attachment.obj>


More information about the vtkusers mailing list