[Insight-users] Mesh issues...Q3 solved

Mathieu Malaterre malat@free.fr
Mon, 17 Feb 2003 20:02:15 +0100


Sorry to answer myslef. But I have been able to answer at least on eof 
my question (n°3). About reading vtkPolyData.
Here is my solution (heavily based on vtk2itk.cxx). This seems to work 
at least for me, but this is not a good solution.

By the way if you give this function the output of a vtkPolyDataReader 
you need to first UpdateWholeExtent, but I don't know why :-/

floatMesh::Pointer MeshFromPolyData(vtkPolyData* polydata)
{
  // Create a new mesh
  floatMesh::Pointer mesh(floatMesh::New());
  // Get the points from vtk
  vtkPoints* vtkpoints = polydata->GetPoints();
  int numPoints = vtkpoints->GetNumberOfPoints();
  // Create a compatible point container for the mesh
  // the mesh is created with a null points container
  floatMesh::PointsContainer::Pointer points =
    floatMesh::PointsContainer::New();
  // Resize the point container to be able to fit the vtk points
  points->Reserve(numPoints);
  // Set the point container on the mesh
  mesh->SetPoints(points);
  for(int i =0; i < numPoints; i++)
    {
    float* apoint = vtkpoints->GetPoint(i);
    mesh->SetPoint(i, floatMesh::PointType(apoint));
    }

  vtkTriangleFilter *triangle = vtkTriangleFilter::New();
  triangle->SetInput(polydata);
  triangle->UpdateWholeExtent();  //important

  vtkCellArray* vtkcells = triangle->GetOutput()->GetPolys();  //very 
bad hack

  floatMesh::CellsContainerPointer cells = floatMesh::CellsContainer::New();
  mesh->SetCells(cells);
  // extract the cell id's from the vtkPolyData

  int numcells = vtkcells->GetNumberOfCells();
 
  int* vtkCellTypes = new int[numcells];
  int cellId =0;
  for(; cellId < numcells; cellId++)
    {
    vtkCellTypes[cellId] = triangle->GetOutput()->GetCellType(cellId);
    }
 
  cells->Reserve(numcells);
  vtkIdType npts;
  vtkIdType* pts;
  cellId = 0;
  for(vtkcells->InitTraversal(); vtkcells->GetNextCell(npts, pts); cellId++)
    {
    floatMesh::CellAutoPointer c;
    switch(vtkCellTypes[cellId])
      {
      case VTK_TRIANGLE:
        {
        typedef itk::CellInterface<float, floatMesh::CellTraits> 
CellInterfaceType;
        typedef itk::TriangleCell<CellInterfaceType> TriangleCellType;
        TriangleCellType * t = new TriangleCellType;
        t->SetPointIds((unsigned long*)pts);
        c.TakeOwnership( t );
        break;
        } 
      case VTK_QUAD:
        {
        typedef itk::CellInterface<float, floatMesh::CellTraits> 
CellInterfaceType;
        typedef itk::QuadrilateralCell<CellInterfaceType> 
QuadrilateralCellType;
        QuadrilateralCellType * t = new QuadrilateralCellType;
        t->SetPointIds((unsigned long*)pts);
        c.TakeOwnership( t );
        break;
        } 
      case VTK_EMPTY_CELL:
      case VTK_VERTEX:
      case VTK_POLY_VERTEX:
      case VTK_LINE:
      case VTK_POLY_LINE:
      case VTK_TRIANGLE_STRIP:
      case VTK_POLYGON:
      case VTK_PIXEL:
      case VTK_TETRA:
      case VTK_VOXEL:
      case VTK_HEXAHEDRON:
      case VTK_WEDGE:
      case VTK_PYRAMID:
      case VTK_PARAMETRIC_CURVE:
      case VTK_PARAMETRIC_SURFACE:
      default:
        std::cerr << "Warning unhandled cell type "
                  << vtkCellTypes[cellId] << std::endl;
        ;
      }
    mesh->SetCell(cellId, c);
    }

  mesh->Print(std::cout);
  triangle->Delete();
 
  return mesh;
}



mathieu


Mathieu Malaterre wrote:

> Hi all,
>
>   I have been playing around this time with DeformableMeshFilter and 
> as I wanted to save my result I discover there was no MeshFileWriter 
> (nor MeshFileReader). I have browse the mailing list archive and found 
> only:
>
> http://www.itk.org/pipermail/insight-users/2002-September/001158.html
>
> So there seems to be an equivalence between UnstructuredGrid and Mesh.
>
>  Nevertheless my questions are
>
> 1. Is there any plan to add a real MeshFileReader/Writer to the toolkit ?
>
> 2. Can I use without any problem vtk2itk.cxx as a replacement ?
>
> 3. When using VTK I would rather use vtkPolyData for my mesh type. 
> What is the simpliest way to read a vtkPolyData into ITK ?
>
> 4. By the way if anyone could explain me what is the main difference 
> between those two types of data (vtkUnstructuredGrid and vtkPolyData).
>
>
> Well I guess that's enough,
>
> thanks for your time
> mathieu
>