[Insight-users] Re: VTK polydata

Luis Ibanez luis.ibanez@kitware.com
Fri May 21 23:18:59 EDT 2004


Hi Alam,

There is no built-in functionality in ITK for
computing the normals of a surface.

If you obtained this Mesh from an ITK image,
an option is to write a while loop for visiting
all the Mesh nodes, take their coordinates and
use them to place a CentralDifferenceImageFunction
on the closest pixel.
http://www.itk.org/Insight/Doxygen/html/classitk_1_1CentralDifferenceImageFunction.html

The Gradient returnded by this ImageFunction could
then be used as normal for the surface. Note that
Normals to surfaces and function gradients are
CovariantVectors, not Vectors.  This is particularly
important if you are transforming the Image or
the Mesh using transforms involving anisotropic
scaling, or skews. (e.g. AffineTransforms).


   Regards,


      Luis


----------------------
Alam, Mohammed wrote:

> Hi Luis,
> 
> Thanks for your help. I was able to finally remove the line out of the
> vtk object. This object was made from a 3D mesh in ITK. Is there a way
> to calculate the normal of these vertices/triangle_strips in ITK, so as
> to smoothen the vtk object?
> 
> Attached is the code from ITK, to save this object as VTKpolydata.
> Please let me know of any functions that maybe added to calculate the
> normals.
> 
> Thanks
> Alam
> 
> 
> void MySegmentation::SaveVTKResult()
> {
>  const char * filename = fl_file_chooser("Image Filename","*.*","");
>   if( !filename )
>     {
>       return;
>     }
> 
> 
>   //DMesh::PointsContainer::Iterator  normalsit;
>   DMesh::PointsContainerPointer     points;
>   DMesh::PointsContainer::Iterator  pointsit;
>   DMesh::CellsContainerPointer      cells;
>   DMesh::CellsContainer::Iterator   cellsit;
>   DMesh::PointType                  node;
>   const unsigned long *tp;
> 
>   FILE *outputfile = fopen(filename, "wb");
> 
>   points = m_dfilter->GetOutput()->GetPoints();
>   pointsit = points->Begin();
>   cells = m_dfilter->GetOutput()->GetCells();
> 
>  
>   int i = 0;
>   int num_points=0;
>   num_points = m_dfilter->GetOutput()->GetNumberOfPoints();
>   fprintf(outputfile, "# vtk DataFile Version 3.0\n");
>   fprintf(outputfile, "vtk output\n");
>   fprintf(outputfile, "ASCII\n");
>   fprintf(outputfile, "DATASET POLYDATA\n");
>   fprintf(outputfile, "POINTS %d float\n", num_points);
>   
>   
>   while ( pointsit != points->End() ) {
>     node = pointsit.Value();
> 	/*to change the origin 256,256,15
> 	   minus it from 0 to flip it on x and y axis*/
> 	node[0]= 0 -(node[0]-315);	//x
> 	node[1]= 0-(node[1]-256);	//y
> 	node[2]= node[2]-63;		//z
> 
> 
>     fprintf(outputfile, "%f %f %f\n", node[0], node[1], node[2]);
>     ++pointsit;
>     i++;
>   }
> 
>   
> 
>   i=0;
>   cellsit = cells->Begin();
>   while(  cellsit != cells->End() ) {
>     ++cellsit;
>     i++;
>   }
>   int cell_size= i*4;
>   fprintf(outputfile, "\nPOLYGONS %d %d\n", i, cell_size);
> 
> 
> //  int num_cell = m_dfilter->GetOutput()->GetNumberOfCells();
>   i=0;
>   cellsit = cells->Begin();
> 	while( /*i < m_dfilter->GetOutput()->GetNumberOfCells()*/
> cellsit != cells->End() ) {
> 		tp = cellsit.Value()->GetPointIds();
> 		fprintf(outputfile, "3 %d %d %d\n", tp[0], tp[1],
> tp[2]);
> 		++cellsit;
> 		i++;
> 	}
> 
> 
> 
>   fclose(outputfile);
> 
> 
> }//save VTK
> 
> 
> 
> -----Original Message-----
> From: Luis Ibanez [mailto:luis.ibanez@kitware.com] 
> Sent: Tuesday, May 18, 2004 2:03 PM
> To: Alam, Mohammed
> Cc: Insight-users@itk.org
> Subject: Re: VTK polydata
> 
> 
> Hi Alam,
> 
> Please read the desciption of the VTK file format in the
> VTK Users Guide, page 287.
> 
> The problem in your file is that in your POLYGONS section
> you are numbering the points from 1 to 8267. You should
> be using numbers from 0 to 8266.
> 
> 
> 
> E.g. the first cell should be
> 
>      POLYGONS 16374 65496
>      3 0 2 1
> 
> and not the current
> 
>       POLYGONS 16374 65496
>       3 1 3 2
> 
> 
> 
>    Regards,
> 
> 
>        Luis
> 
> 
> ----------------------
> Alam, Mohammed wrote:
> 
> 
>>Hello Luis
>>
>>I have been working on 3D segmentation. The result of 3D segmentation
>>can be saved as a *.pss / *.vtk (vtkpolydata format). When the vtk
>>object was visualized (using 3D Slicer), we see an extra line attached
>>to the object.
>>
>>For your reference I am attaching the vtk file generated by the
> 
> program
> 
>>(which was similar to pss file except for the header format of vtk
>>polydata files). I would highly appreciate if you could point out a
>>solution to the problem and any ideas to avoid such problems.
>>
>>Thank you
>>Alam
>>
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> void MySegmentation::SaveVTKResult()
> {
>  const char * filename = fl_file_chooser("Image Filename","*.*","");
>   if( !filename )
>     {
>       return;
>     }
> 
> 
>   //DMesh::PointsContainer::Iterator  normalsit;
>   DMesh::PointsContainerPointer     points;
>   DMesh::PointsContainer::Iterator  pointsit;
>   DMesh::CellsContainerPointer      cells;
>   DMesh::CellsContainer::Iterator   cellsit;
>   DMesh::PointType                  node;
>   const unsigned long *tp;
> 
>   FILE *outputfile = fopen(filename, "wb");
> 
>   points = m_dfilter->GetOutput()->GetPoints();
>   pointsit = points->Begin();
>   cells = m_dfilter->GetOutput()->GetCells();
> 
>  
>   int i = 0;
>   int num_points=0;
>   num_points = m_dfilter->GetOutput()->GetNumberOfPoints();
>   fprintf(outputfile, "# vtk DataFile Version 3.0\n");
>   fprintf(outputfile, "vtk output\n");
>   fprintf(outputfile, "ASCII\n");
>   fprintf(outputfile, "DATASET POLYDATA\n");
>   fprintf(outputfile, "POINTS %d float\n", num_points);
>   
>   
>   while ( pointsit != points->End() ) {
>     node = pointsit.Value();
> 	/*to change the origin 256,256,15
> 	   minus it from 0 to flip it on x and y axis*/
> 	node[0]= 0 -(node[0]-315);	//x
> 	node[1]= 0-(node[1]-256);	//y
> 	node[2]= node[2]-63;		//z
> 
> 
>     fprintf(outputfile, "%f %f %f\n", node[0], node[1], node[2]);
>     ++pointsit;
>     i++;
>   }
> 
>   
> 
>   i=0;
>   cellsit = cells->Begin();
>   while(  cellsit != cells->End() ) {
>     ++cellsit;
>     i++;
>   }
>   int cell_size= i*4;
>   fprintf(outputfile, "\nPOLYGONS %d %d\n", i, cell_size);
> 
> 
> //  int num_cell = m_dfilter->GetOutput()->GetNumberOfCells();
>   i=0;
>   cellsit = cells->Begin();
> 	while( /*i < m_dfilter->GetOutput()->GetNumberOfCells()*/ cellsit != cells->End() ) {
> 		tp = cellsit.Value()->GetPointIds();
> 		fprintf(outputfile, "3 %d %d %d\n", tp[0], tp[1], tp[2]);
> 		++cellsit;
> 		i++;
> 	}
> 
> 
> 
>   fclose(outputfile);
> 
> 
> }//save VTK






More information about the Insight-users mailing list