[vtkusers] Possible Bug in vtkPolyDataToImageStencil

David Gobbi dgobbi at atamai.com
Thu Mar 22 12:39:45 EDT 2007


Hi Jesus,

The vtkPolyDataToImageStencil does use a scanline approach, and the way 
it works is that it uses vtkOBBTree to find out where the scanline 
intersects the polydata.  Let's just say that this isn't the best way to 
rasterize a polydata, and it doesn't work very well with sharp points.  
I'm the original author of the class, so I take responsibility for its 
less-than-robust implementation.

My company has another vtkPolyDataToImageStencil class that uses a 
different algorithm, I will send it to you so that you can try it out.  
It is definitely more robust than the current VTK one.

 - David


Jesús Spí­nola wrote:
> Hello,
>
> I'm using the vtkPolyDataToImageStencil class to cut parts of a 
> dataset. It works fine for several polygons but I have a case with a 
> star-shaped polygon with a strange result. I've attached several 
> images with the resuts ( http://hades.udg.es/~chus/images.tar.bz2 
> <http://hades.udg.es/%7Echus/images.tar.bz2> )
>
> For example, when I make the stencil with an hexahedron the result is 
> ok ( see noStencilHexahedron.png and stencilHexahedron.png, that's 
> before cutting and after cutting with the stencil, respectively ).
>
> But when constructing another kind of polygon I get a strange result. 
> First I thought the coordinates I was giving were wrong, so I created 
> and actor with the same polydata to see if it was wrong, but the actor 
> is rendered as I expected, so the polydata is ok. You can see the 
> original image before cutting is noStencilstarShape.png. After cutting 
> with the stencil I get stencilOnlyStarShape.png. Then you can see the 
> polygon created for the stencil in this image 
> stencilWithPolyDataStarShape.png.
>
> The original dicom images i used for this examples are: 
> http://www.creatis.insa-lyon.fr/~jpr/PUBLIC/gdcm/gdcmSampleData/imgdisplay_testcases/dish_p05.dcm 
> <http://www.creatis.insa-lyon.fr/%7Ejpr/PUBLIC/gdcm/gdcmSampleData/imgdisplay_testcases/dish_p05.dcm> 
> for the hexahedron and 
> http://www.creatis.insa-lyon.fr/~jpr/PUBLIC/gdcm/gdcmSampleData/imgdisplay_testcases/dish_p09.dcm 
> <http://www.creatis.insa-lyon.fr/%7Ejpr/PUBLIC/gdcm/gdcmSampleData/imgdisplay_testcases/dish_p09.dcm> 
> for the star shape.
>
> The coordinates for the star shape ( refering to pixels coordinates, 
> where 1,1 is the first pixel ) are
> (133, 257)
> (199, 233)
> (169, 169)
> (233, 199)
> (257, 133)
> (281, 199)
> (345, 169)
> (315, 233)
> (381, 257)
> (315, 281)
> (345, 345)
> (281, 315)
> (257, 381)
> (233, 315)
> (169, 345)
> (199, 281)
>
> My code looks like this ( it's the same for the hexahedron and for the 
> star shape )
>
> int vertices = getNumberOfVerticesOfThePolygon();
> vtkPoints *points = vtkPoints::New();
> vtkCellArray *vertexs = vtkCellArray::New();
> vertexs->InsertNextCell( vertices + 1 );
> int polyX, polyY;
> double x, y;
> for( int i = 0; i < vertices; i++ )
> {
>       getPolygonVertex( i, polyX, polyY ) )
>      
>       // to transform the pixel coordinates to world points coordinates
>       x = origin[0] + (polyX-1.0)*spacing[0];
>       y = origin[1] + ( polyY-1.0)*spacing[1];
>
>       points->InsertPoint( i , x, y, 0.0 );
>       vertexs->InsertCellPoint( i );
> }
> // to close the polygon
> vertexs->InsertCellPoint( 0 );
> vtkPolyData *polyData = vtkPolyData::New();
> polyData->SetPoints( points );
> polyData->SetLines( vertexs );
>
> vtkLinearExtrusionFilter *extruder = vtkLinearExtrusionFilter::New();
> extruder->SetScaleFactor(1);
> extruder->SetExtrusionTypeToNormalExtrusion();
> extruder->SetVector( 0 , 0 , 1 );
> extruder->SetInput( polyData );
>
> m_polygonalPolyDataStencil = vtkPolyDataToImageStencil::New();
> m_polygonalPolyDataStencil->SetInput( extruder->GetOutput() );
> extruder->Delete();
>
> stencil = vtkImageStencil::New();
> stencil->SetInput( m_imageData ); // m_imageData is of type 
> vtkImageData, contains the original dicom dataset
> stencil->ReverseStencilOn();
> stencil->SetBackgroundValue( m_backgroundValue );
> stencil->SetStencil( m_rectangularPolyDataStencil->GetOutput() );
> ....
>
> m_imageViewer->SetInput( stencil->GetOutput() ); // m_imageViewer is 
> of type vtkImageViewer2
>
> I want to know if this is because of an error of my code or if it's 
> due to some bug in the implicated classes.
>
> As a side note, other workmates that are using this class had also 
> problems like this on the sharpen vertexs of the polygons. Maybe it's 
> because the algorithm is a kind of scanline method and when the same 
> pixel in the polygon is the begin and the end of the region that it 
> encloses the algorithm fails.
>
> Thanks in advance!
> ------------------------------------------------------------------------
>
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>   




More information about the vtkusers mailing list