Classes | Public Types | Static Public Member Functions

MeshUtil< MeshType, ScalarAccessor > Class Template Reference

The class provides mehtods for ITK - VTK mesh conversion. More...

#include <mitkMeshUtil.h>

List of all members.

Classes

class  DistributeInsertImplementation
 Implementation of the InsertImplementation interface of SwitchByCellType to define a visitor that distributes cells according to their types (for vtkPolyData construction)
class  ExactSwitchByCellType
 A visitor similar to SwitchByCellType, but with exact matching of cell types.
class  SingleCellArrayInsertImplementation
 Implementation of the InsertImplementation interface of SwitchByCellType to define a visitor that create cells according to their types and put them in a single vtkCellArray (for vtkUnstructuredGrid construction)
class  SwitchByCellType
 A visitor to create VTK cells by means of a class defining the InsertImplementation interface.

Public Types

typedef
itk::MatrixOffsetTransformBase
< typename
MeshType::CoordRepType, 3, 3 > 
ITKTransformType
typedef
itk::MatrixOffsetTransformBase
< mitk::ScalarType, 3, 3 > 
MITKTransformType

Static Public Member Functions

static void ConvertTransformToItk (const MITKTransformType *mitkTransform, ITKTransformType *itkTransform)
static MeshType::Pointer MeshFromPolyData (vtkPolyData *poly, mitk::Geometry3D *geometryFrame=NULL, mitk::Geometry3D *polyDataGeometryFrame=NULL)
static MeshType::Pointer MeshFromSurface (mitk::Surface *surface, mitk::Geometry3D *geometryFrame=NULL)
static vtkUnstructuredGrid * MeshToUnstructuredGrid (MeshType *mesh, bool usePointScalarAccessor=false, bool useCellScalarAccessor=false, unsigned int pointDataType=0, mitk::Geometry3D *geometryFrame=NULL)
static vtkPolyData * MeshToPolyData (MeshType *mesh, bool onlyTriangles=false, bool useScalarAccessor=false, unsigned int pointDataType=0, mitk::Geometry3D *geometryFrame=NULL, vtkPolyData *polydata=NULL)
static MeshType::Pointer CreateRegularSphereMesh (typename MeshType::PointType center, typename MeshType::PointType::VectorType scale, int resolution)
static MeshType::Pointer CreateSphereMesh (typename MeshType::PointType center, typename MeshType::PointType scale, int *resolution)
static MeshType::Pointer CreateRegularSphereMesh2 (typename MeshType::PointType center, typename MeshType::PointType scale, int resolution)

Detailed Description

template<typename MeshType, class ScalarAccessor = NullScalarAccessor<MeshType>>
class MeshUtil< MeshType, ScalarAccessor >

The class provides mehtods for ITK - VTK mesh conversion.

Definition at line 150 of file mitkMeshUtil.h.


Member Typedef Documentation

template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
typedef itk::MatrixOffsetTransformBase<typename MeshType::CoordRepType,3,3> MeshUtil< MeshType, ScalarAccessor >::ITKTransformType

Definition at line 647 of file mitkMeshUtil.h.

template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
typedef itk::MatrixOffsetTransformBase<mitk::ScalarType,3,3> MeshUtil< MeshType, ScalarAccessor >::MITKTransformType

Definition at line 648 of file mitkMeshUtil.h.


Member Function Documentation

template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static void MeshUtil< MeshType, ScalarAccessor >::ConvertTransformToItk ( const MITKTransformType mitkTransform,
ITKTransformType itkTransform 
) [inline, static]

Convert a MITK transformation to an ITK transformation Necessary because ITK uses double and MITK uses float values

Definition at line 654 of file mitkMeshUtil.h.

  {
    typename MITKTransformType::MatrixType mitkM = mitkTransform->GetMatrix();
    typename ITKTransformType::MatrixType itkM;

    typename MITKTransformType::OffsetType mitkO = mitkTransform->GetOffset();
    typename ITKTransformType::OffsetType itkO;

    for(short i = 0; i < 3; ++i)
    {
      for(short j = 0; j<3; ++j)
      {
        itkM[i][j] = (double)mitkM[i][j];
      }
      itkO[i] = (double)mitkO[i];
    }

    itkTransform->SetMatrix(itkM);
    itkTransform->SetOffset(itkO);
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static MeshType::Pointer MeshUtil< MeshType, ScalarAccessor >::CreateRegularSphereMesh ( typename MeshType::PointType  center,
typename MeshType::PointType::VectorType  scale,
int  resolution 
) [inline, static]

Definition at line 1400 of file mitkMeshUtil.h.

  {
    typedef itk::RegularSphereMeshSource<MeshType> SphereSourceType;
    typename SphereSourceType::Pointer mySphereSource = SphereSourceType::New();

    mySphereSource->SetCenter(center);
    mySphereSource->SetScale(scale);
    mySphereSource->SetResolution( resolution );
    mySphereSource->Update();

    typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
    resultMesh->Register(); // necessary ???? 
    return resultMesh;
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static MeshType::Pointer MeshUtil< MeshType, ScalarAccessor >::CreateRegularSphereMesh2 ( typename MeshType::PointType  center,
typename MeshType::PointType  scale,
int  resolution 
) [inline, static]

Definition at line 1482 of file mitkMeshUtil.h.

References QuadProgPP::sqrt().

  {
    typedef typename itk::AutomaticTopologyMeshSource<MeshType> MeshSourceType;
    typename MeshSourceType::Pointer mySphereSource = MeshSourceType::New();

    typename MeshType::PointType pnt0, pnt1, pnt2, pnt3, pnt4, pnt5, pnt6, pnt7, pnt8, pnt9, pnt10, pnt11;
    double c1= 0.5 * (1.0 + sqrt(5.0));
    double c2= 1.0;
    double len = sqrt( c1*c1 + c2*c2 );
    c1 /= len;  c2 /= len;

    pnt0[0] = center[0] - c1*scale[0];    pnt0[1] = center[1];                pnt0[2] = center[2] + c2*scale[2];
    pnt1[0] = center[0];                  pnt1[1] = center[1] + c2*scale[1];  pnt1[2] = center[2] - c1*scale[2];
    pnt2[0] = center[0];                  pnt2[1] = center[1] + c2*scale[1];  pnt2[2] = center[2] + c1*scale[2];
    pnt3[0] = center[0] + c1*scale[0];    pnt3[1] = center[1];                pnt3[2] = center[2] - c2*scale[2];
    pnt4[0] = center[0] - c2*scale[0];    pnt4[1] = center[1] - c1*scale[1];  pnt4[2] = center[2];
    pnt5[0] = center[0] - c2*scale[0];    pnt5[1] = center[1] + c1*scale[1];  pnt5[2] = center[2];
    pnt6[0] = center[0];                  pnt6[1] = center[1] - c2*scale[1];  pnt6[2] = center[2] + c1*scale[2];
    pnt7[0] = center[0] + c2*scale[0];    pnt7[1] = center[1] + c1*scale[1];  pnt7[2] = center[2];
    pnt8[0] = center[0];                  pnt8[1] = center[1] - c2*scale[1];  pnt8[2] = center[2] - c1*scale[2];
    pnt9[0] = center[0] + c1*scale[0];    pnt9[1] = center[1];                pnt9[2] = center[2] + c2*scale[2];
    pnt10[0]= center[0] + c2*scale[0];    pnt10[1]= center[1] - c1*scale[1];  pnt10[2]= center[2];
    pnt11[0]= center[0] - c1*scale[0];    pnt11[1]= center[1];                pnt11[2]= center[2] - c2*scale[2];

    addTriangle( mySphereSource, scale, pnt9, pnt2, pnt6, resolution );
    addTriangle( mySphereSource, scale, pnt1, pnt11, pnt5, resolution );
    addTriangle( mySphereSource, scale, pnt11, pnt1, pnt8, resolution );
    addTriangle( mySphereSource, scale, pnt0, pnt11, pnt4, resolution );
    addTriangle( mySphereSource, scale, pnt3, pnt1, pnt7, resolution );
    addTriangle( mySphereSource, scale, pnt3, pnt8, pnt1, resolution );
    addTriangle( mySphereSource, scale, pnt9, pnt3, pnt7, resolution );
    addTriangle( mySphereSource, scale, pnt0, pnt6, pnt2, resolution );
    addTriangle( mySphereSource, scale, pnt4, pnt10, pnt6, resolution );
    addTriangle( mySphereSource, scale, pnt1, pnt5, pnt7, resolution );
    addTriangle( mySphereSource, scale, pnt7, pnt5, pnt2, resolution );
    addTriangle( mySphereSource, scale, pnt8, pnt3, pnt10, resolution );
    addTriangle( mySphereSource, scale, pnt4, pnt11, pnt8, resolution );
    addTriangle( mySphereSource, scale, pnt9, pnt7, pnt2, resolution );
    addTriangle( mySphereSource, scale, pnt10, pnt9, pnt6, resolution );
    addTriangle( mySphereSource, scale, pnt0, pnt5, pnt11, resolution );
    addTriangle( mySphereSource, scale, pnt0, pnt2, pnt5, resolution );
    addTriangle( mySphereSource, scale, pnt8, pnt10, pnt4, resolution );
    addTriangle( mySphereSource, scale, pnt3, pnt9, pnt10, resolution );
    addTriangle( mySphereSource, scale, pnt6, pnt0, pnt4, resolution );

    return mySphereSource->GetOutput();
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static MeshType::Pointer MeshUtil< MeshType, ScalarAccessor >::CreateSphereMesh ( typename MeshType::PointType  center,
typename MeshType::PointType  scale,
int *  resolution 
) [inline, static]

Definition at line 1415 of file mitkMeshUtil.h.

  {
    typedef typename itk::SphereMeshSource<MeshType> SphereSource;

    typename SphereSource::Pointer mySphereSource = SphereSource::New();

    mySphereSource->SetCenter(center);
    mySphereSource->SetScale(scale);
    mySphereSource->SetResolutionX(resolution[0]);
    mySphereSource->SetResolutionY(resolution[1]);
    mySphereSource->SetSquareness1(1);
    mySphereSource->SetSquareness2(1);
    mySphereSource->Update();
    mySphereSource->GetOutput();

    typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
    resultMesh->Register();

    return resultMesh;
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static MeshType::Pointer MeshUtil< MeshType, ScalarAccessor >::MeshFromPolyData ( vtkPolyData *  poly,
mitk::Geometry3D geometryFrame = NULL,
mitk::Geometry3D polyDataGeometryFrame = NULL 
) [inline, static]

create an itkMesh object from a vtkPolyData

Definition at line 678 of file mitkMeshUtil.h.

References MITK_ERROR, MITK_WARN, and QuadProgPP::t().

Referenced by MeshUtil< MeshType, ScalarAccessor >::MeshFromSurface().

  {
    // Create a new mesh
    typename MeshType::Pointer output = MeshType::New();
    output->SetCellsAllocationMethod( MeshType::CellsAllocatedDynamicallyCellByCell );  

    typedef typename MeshType::CellDataContainer MeshCellDataContainerType;

    output->SetCellData(MeshCellDataContainerType::New());

    // Get the points from vtk
    vtkPoints* vtkpoints = poly->GetPoints();
    const unsigned int numPoints = poly->GetNumberOfPoints();

    // Create a compatible point container for the mesh
    // the mesh is created with a null points container
    // MeshType::PointsContainer::Pointer points = 
    //   MeshType::PointsContainer::New();
    // // Resize the point container to be able to fit the vtk points
    // points->Reserve(numPoints);
    // // Set the point container on the mesh
    //output->SetPoints(points);
    vtkFloatingPointType vtkpoint[3];
    typename MeshType::PointType itkPhysicalPoint;
    if(geometryFrame==NULL)
    {
      if(polyDataGeometryFrame==NULL)
      {
        for(unsigned int i=0; i < numPoints; ++i)
        {
          vtkpoints->GetPoint(i, vtkpoint);
          //MITK_INFO << "next point: " << test[0]<< "," << test[1] << "," << test[2] << std::endl;
          //typename MeshType::PixelType* apoint = (typename MeshType::PixelType*) vtkpoints->GetPoint(i);
          mitk::vtk2itk(vtkpoint, itkPhysicalPoint);
          output->SetPoint( i, itkPhysicalPoint );
        }
      }
      else
      {
        for(unsigned int i=0; i < numPoints; ++i)
        {
          vtkpoints->GetPoint(i, vtkpoint);
          //MITK_INFO << "next point: " << test[0]<< "," << test[1] << "," << test[2] << std::endl;
          //typename MeshType::PixelType* apoint = (typename MeshType::PixelType*) vtkpoints->GetPoint(i);
          mitk::Point3D mitkWorldPoint;
          mitk::vtk2itk(vtkpoint, mitkWorldPoint);
          polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
          mitk::vtk2itk(mitkWorldPoint, itkPhysicalPoint);
          output->SetPoint( i, itkPhysicalPoint );
        }
      }
    }
    else
    {
      mitk::Point3D mitkWorldPoint;
      if(polyDataGeometryFrame==NULL)
      {
        for(unsigned int i=0; i < numPoints; ++i)
        {
          vtkpoints->GetPoint(i, vtkpoint);
          //MITK_INFO << "next point: " << test[0]<< "," << test[1] << "," << test[2] << std::endl;
          //typename MeshType::PixelType* apoint = (typename MeshType::PixelType*) vtkpoints->GetPoint(i);
          mitk::vtk2itk(vtkpoint, mitkWorldPoint);
          geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
          output->SetPoint( i, itkPhysicalPoint );
        }
      }
      else
      {
        for(unsigned int i=0; i < numPoints; ++i)
        {
          vtkpoints->GetPoint(i, vtkpoint);
          //MITK_INFO << "next point: " << test[0]<< "," << test[1] << "," << test[2] << std::endl;
          //typename MeshType::PixelType* apoint = (typename MeshType::PixelType*) vtkpoints->GetPoint(i);
          mitk::vtk2itk(vtkpoint, mitkWorldPoint);
          polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
          geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
          output->SetPoint( i, itkPhysicalPoint );
        }
      }
    }

    vtkCellArray* vtkcells = poly->GetPolys();
    //    vtkCellArray* vtkcells = poly->GetStrips();
    //MeshType::CellsContainerPointer cells = MeshType::CellsContainer::New();
    //output->SetCells(cells);
    // extract the cell id's from the vtkUnstructuredGrid
    int numcells = vtkcells->GetNumberOfCells();
    int* vtkCellTypes = new int[numcells];
    int cellId = 0;
    // poly ids start after verts and lines!
    int cellIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines();
    for(; cellId < numcells; cellId++)
    {
      vtkCellTypes[cellId] = poly->GetCellType( cellId+cellIdOfs );
    }

    // cells->Reserve(numcells);
    vtkIdType npts;
    vtkIdType* pts;
    cellId = 0;

    typedef typename MeshType::MeshTraits   OMeshTraits;
    typedef typename OMeshTraits::PixelType       OPixelType; 
    typedef typename MeshType::CellTraits          CellTraits;
    typedef typename itk::CellInterface<OPixelType, CellTraits>   CellInterfaceType;
    typedef typename itk::TriangleCell<CellInterfaceType>         TriCellType;
    typedef typename TriCellType::CellAutoPointer             TriCellPointer;

    TriCellPointer newCell;
    output->GetCells()->Reserve( poly->GetNumberOfPolys() + poly->GetNumberOfStrips() );
    output->GetCellData()->Reserve( poly->GetNumberOfPolys() + poly->GetNumberOfStrips() );

    for(vtkcells->InitTraversal(); vtkcells->GetNextCell(npts, pts); cellId++)
    {
      switch(vtkCellTypes[cellId])
      {
      case VTK_TRIANGLE:
        {
          if (npts != 3) continue; // skip non-triangles;
          unsigned long pointIds[3];
          pointIds[0] = (unsigned long) pts[0];
          pointIds[1] = (unsigned long) pts[1];
          pointIds[2] = (unsigned long) pts[2];

          newCell.TakeOwnership( new TriCellType );
          newCell->SetPointIds(pointIds);//(unsigned long*)pts);
          output->SetCell(cellId, newCell );
          output->SetCellData(cellId, (typename MeshType::PixelType)3);
          break;    
        } 

      case VTK_QUAD:
        {
          if (npts != 4 ) continue; // skip non-quadrilateral
          unsigned long pointIds[3];

          pointIds[0] = (unsigned long) pts[0];
          pointIds[1] = (unsigned long) pts[1];
          pointIds[2] = (unsigned long) pts[2];
          newCell.TakeOwnership( new TriCellType );
          newCell->SetPointIds(pointIds);
          output->SetCell(cellId, newCell );
          output->SetCellData(cellId, (typename MeshType::PixelType)3);
          cellId++;

          pointIds[0] = (unsigned long) pts[2];
          pointIds[1] = (unsigned long) pts[3];
          pointIds[2] = (unsigned long) pts[0];
          newCell.TakeOwnership( new TriCellType );
          newCell->SetPointIds(pointIds);
          output->SetCell(cellId, newCell );
          output->SetCellData(cellId, (typename MeshType::PixelType)3);
          break;
        }

      case VTK_EMPTY_CELL:
        {
          if (npts != 3) 
          {
            MITK_ERROR << "Only empty triangle cell supported by now..." << std::endl; // skip non-triangle empty cells;
            continue;
          }
          unsigned long pointIds[3];
          pointIds[0] = (unsigned long) pts[0];
          pointIds[1] = (unsigned long) pts[1];
          pointIds[2] = (unsigned long) pts[2];

          newCell.TakeOwnership( new TriCellType );
          newCell->SetPointIds(pointIds);
          output->SetCell(cellId, newCell );
          output->SetCellData(cellId, (typename MeshType::PixelType)3);
          break;    
        } 

        //case VTK_VERTEX:              // If need to implement use 
        //case VTK_POLY_VERTEX:         // the poly->GetVerts() and 
        //case VTK_LINE:                // poly->GetLines() routines 
        //case VTK_POLY_LINE:           // outside of the switch..case.
      case VTK_POLYGON:
      case VTK_PIXEL:
        {
          if (npts != 4 ) continue;// skip non-quadrilateral
          unsigned long pointIds[3];
          for ( unsigned int idx = 0; idx <= 1; idx++ )
          {
            pointIds[0] = (unsigned long) pts[idx];
            pointIds[1] = (unsigned long) pts[idx+1];
            pointIds[2] = (unsigned long) pts[idx+2];
            newCell.TakeOwnership( new TriCellType );
            newCell->SetPointIds(pointIds);
            output->SetCell(cellId+idx, newCell );
            output->SetCellData(cellId+idx, (typename MeshType::PixelType)3);
          }
          cellId++;
          break;
        }

      case VTK_TETRA:
      case VTK_VOXEL:
      case VTK_HEXAHEDRON:
      case VTK_WEDGE:
      case VTK_PYRAMID:
      case VTK_PARAMETRIC_CURVE:
      case VTK_PARAMETRIC_SURFACE:
      default:
        MITK_WARN << "Warning, unhandled cell type " 
          << vtkCellTypes[cellId] << std::endl;
      }
    }

    if (poly->GetNumberOfStrips() != 0) 
    {
      vtkcells = poly->GetStrips();
      numcells = vtkcells->GetNumberOfCells();
      vtkCellTypes = new int[numcells];
      int stripId = 0;
      // strip ids start after verts, lines and polys!
      int stripIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines() + poly->GetNumberOfPolys(); 
      for(; stripId < numcells; stripId++)
      {
        vtkCellTypes[stripId] = poly->GetCellType( stripId+stripIdOfs );
      }
      stripId = 0;

      vtkcells->InitTraversal();
      while( vtkcells->GetNextCell(npts, pts) )
      {
        if (vtkCellTypes[stripId] != VTK_TRIANGLE_STRIP) 
        {
          MITK_ERROR << "Only triangle strips supported!" << std::endl;
          continue;
        }
        stripId++;

        unsigned int numberOfTrianglesInStrip = npts - 2;
        unsigned long pointIds[3];
        pointIds[0] = (unsigned long) pts[0];
        pointIds[1] = (unsigned long) pts[1];
        pointIds[2] = (unsigned long) pts[2];

        for( unsigned int t=0; t < numberOfTrianglesInStrip; t++ )
        {
          newCell.TakeOwnership( new TriCellType );
          newCell->SetPointIds(pointIds);
          output->SetCell(cellId, newCell );
          output->SetCellData(cellId, (typename MeshType::PixelType)3);
          cellId++;
          pointIds[0] = pointIds[1];
          pointIds[1] = pointIds[2];
          pointIds[2] = pts[t+3];
        }
      }
    }   
    //output->Print(std::cout);
    output->BuildCellLinks();
    delete[] vtkCellTypes;
    return output;
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static MeshType::Pointer MeshUtil< MeshType, ScalarAccessor >::MeshFromSurface ( mitk::Surface surface,
mitk::Geometry3D geometryFrame = NULL 
) [inline, static]

create an itkMesh object from an mitk::Surface

Definition at line 941 of file mitkMeshUtil.h.

References mitk::BaseData::GetGeometry(), mitk::Surface::GetVtkPolyData(), and MeshUtil< MeshType, ScalarAccessor >::MeshFromPolyData().

  {
    if(surface == NULL)
      return NULL;
    return MeshFromPolyData(surface->GetVtkPolyData(), geometryFrame, surface->GetGeometry());
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static vtkPolyData* MeshUtil< MeshType, ScalarAccessor >::MeshToPolyData ( MeshType *  mesh,
bool  onlyTriangles = false,
bool  useScalarAccessor = false,
unsigned int  pointDataType = 0,
mitk::Geometry3D geometryFrame = NULL,
vtkPolyData *  polydata = NULL 
) [inline, static]

create a vtkPolyData object from an itkMesh

default Distribute line cell visitior definition

default Distribute polygon cell visitior definition

default Distribute triangle cell visitior definition

default Distribute quad cell visitior definition

default Distribute triangle cell visitior definition

Definition at line 1181 of file mitkMeshUtil.h.

References MITK_ERROR.

  {
    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::LineCell< typename MeshType::CellType >, 
      DistributeUserVisitorType>                DistributeLineVisitor;

    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::PolygonCell< typename MeshType::CellType >, 
      DistributeUserVisitorType>                DistributePolygonVisitor;

    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      DistributeUserVisitorType>                DistributeTriangleVisitor;


    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
      itk::QuadrilateralCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      DistributeUserVisitorType>               DistributeQuadrilateralVisitor;


    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      ExactUserVisitorType>                    ExactTriangleVisitor;


    // Get the number of points in the mesh
    int numPoints = mesh->GetNumberOfPoints();
    if(numPoints == 0)
    {
      //mesh->Print(std::cerr);
      MITK_ERROR << "no points in Grid " << std::endl;
    }
    // Create a vtkPolyData
    if(polydata == NULL)
      polydata = vtkPolyData::New();
    else
      polydata->Initialize();

    // Create the vtkPoints object and set the number of points
#if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) ))
    vtkPoints* vpoints = vtkPoints::New( VTK_DOUBLE );
#else
    vtkPoints* vpoints = vtkPoints::New();
#endif
    vtkFloatArray * scalars = vtkFloatArray::New();
    scalars->SetNumberOfComponents(1);

    typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
    typename MeshType::PointsContainer::Iterator i;

    // iterate over all the points in the itk mesh to find
    // the maximal index
    unsigned int maxIndex = 0;
    for(i = points->Begin(); i != points->End(); ++i)
    {
      if(maxIndex < i->Index())
        maxIndex = i->Index();
    }

    // initialize vtk-classes for points and scalars
    vpoints->SetNumberOfPoints(maxIndex+1);
    scalars->SetNumberOfTuples(maxIndex+1);

    // iterate over all the points in the itk mesh filling in
    // the vtkPoints object as we go

    vtkFloatingPointType vtkpoint[3];
    typename MeshType::PointType itkPhysicalPoint;
    if(geometryFrame==NULL)
    {
      for(i = points->Begin(); i != points->End(); ++i)
      {
        // Get the point index from the point container iterator
        int idx = i->Index();

        itkPhysicalPoint = i->Value();
        mitk::itk2vtk(itkPhysicalPoint, vtkpoint);
        // Set the vtk point at the index with the the coord array from itk
        // itk returns a const pointer, but vtk is not const correct, so
        // we have to use a const cast to get rid of the const
        //      vpoints->SetPoint(idx, const_cast<DATATYPE*>(i->Value().GetDataPointer()));
        vpoints->SetPoint(idx, vtkpoint);

        if(useScalarAccessor)
        {   
          scalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
        }
      }
    }
    else
    {
      mitk::Point3D mitkWorldPoint;
      for(i = points->Begin(); i != points->End(); ++i)
      {
        // Get the point index from the point container iterator
        int idx = i->Index();

        itkPhysicalPoint = i->Value();
        geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
        mitk::itk2vtk(mitkWorldPoint, vtkpoint);
        // Set the vtk point at the index with the the coord array from itk
        // itk returns a const pointer, but vtk is not const correct, so
        // we have to use a const cast to get rid of the const
        //      vpoints->SetPoint(idx, const_cast<DATATYPE*>(i->Value().GetDataPointer()));
        vpoints->SetPoint(idx, vtkpoint);

        if(useScalarAccessor)
        {   
          scalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
        }
      }
    }

    // Set the points on the vtk grid
    polydata->SetPoints(vpoints);
    if (useScalarAccessor)
      polydata->GetPointData()->SetScalars(scalars);
    polydata->GetPointData()->CopyAllOn();


    // Now create the cells using the MulitVisitor
    // 1. Create a MultiVisitor
    typedef typename MeshType::CellType::MultiVisitor MeshMV;
    typename MeshMV::Pointer mv = MeshMV::New();

    int numCells = mesh->GetNumberOfCells();

    if (onlyTriangles)
    {
      // create vtk cells and allocate
      vtkCellArray* trianglecells = vtkCellArray::New();
      trianglecells->Allocate(numCells);

      // 2. Create a triangle visitor and add it to the multivisitor
      typename ExactTriangleVisitor::Pointer tv = ExactTriangleVisitor::New();
      tv->SetCellArrays(NULL, trianglecells, NULL, NULL);
      mv->AddVisitor(tv);
      // 3. Now ask the mesh to accept the multivisitor which
      // will Call Visit for each cell in the mesh that matches the
      // cell types of the visitors added to the MultiVisitor
      mesh->Accept(mv);

      // 4. Set the result into our vtkPolyData
      if(trianglecells->GetNumberOfCells()>0)
        polydata->SetStrips(trianglecells); 

      // 5. Clean up vtk objects (no vtkSmartPointer ... )
      trianglecells->Delete();
    }
    else 
    {
      // create vtk cells and allocate
      vtkCellArray* linecells = vtkCellArray::New();
      vtkCellArray* trianglecells = vtkCellArray::New();
      vtkCellArray* polygoncells = vtkCellArray::New();
      linecells->Allocate(numCells);
      trianglecells->Allocate(numCells);
      polygoncells->Allocate(numCells);

      // 2. Create visitors
      typename DistributeLineVisitor::Pointer lv          = DistributeLineVisitor::New();
      typename DistributePolygonVisitor::Pointer pv       = DistributePolygonVisitor::New();
      typename DistributeTriangleVisitor::Pointer tv      = DistributeTriangleVisitor::New();
      typename DistributeQuadrilateralVisitor::Pointer qv = DistributeQuadrilateralVisitor::New();

      lv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
      pv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
      tv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
      qv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);

      // add the visitors to the multivisitor
      mv->AddVisitor(tv);
      mv->AddVisitor(lv);
      mv->AddVisitor(pv);
      mv->AddVisitor(qv);
      // 3. Now ask the mesh to accept the multivisitor which
      // will Call Visit for each cell in the mesh that matches the
      // cell types of the visitors added to the MultiVisitor
      mesh->Accept(mv);

      // 4. Set the result into our vtkPolyData
      if(linecells->GetNumberOfCells()>0)
        polydata->SetLines(linecells);  
      if(trianglecells->GetNumberOfCells()>0)
        polydata->SetStrips(trianglecells); 
      if(polygoncells->GetNumberOfCells()>0)
        polydata->SetPolys(polygoncells);

      // 5. Clean up vtk objects (no vtkSmartPointer ... )
      linecells->Delete();
      trianglecells->Delete();
      polygoncells->Delete();
    }
    vpoints->Delete();
    scalars->Delete();

    //MITK_INFO << "meshToPolyData end" << std::endl;
    return polydata;
  }
template<typename MeshType , class ScalarAccessor = NullScalarAccessor<MeshType>>
static vtkUnstructuredGrid* MeshUtil< MeshType, ScalarAccessor >::MeshToUnstructuredGrid ( MeshType *  mesh,
bool  usePointScalarAccessor = false,
bool  useCellScalarAccessor = false,
unsigned int  pointDataType = 0,
mitk::Geometry3D geometryFrame = NULL 
) [inline, static]

create an vtkUnstructuredGrid object from an itkMesh

default SingleCellArray line cell visitior definition

default SingleCellArray polygon cell visitior definition

default SingleCellArray triangle cell visitior definition

default SingleCellArray quad cell visitior definition

default SingleCellArray tetra cell visitior definition

default SingleCellArray hex cell visitior definition

Definition at line 951 of file mitkMeshUtil.h.

References MITK_FATAL.

  {
    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::LineCell< typename MeshType::CellType >, 
      SingleCellArrayUserVisitorType>                SingleCellArrayLineVisitor;

    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::PolygonCell< typename MeshType::CellType >, 
      SingleCellArrayUserVisitorType>                SingleCellArrayPolygonVisitor;

    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
      typename MeshType::CellTraits,
      itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      SingleCellArrayUserVisitorType>                SingleCellArrayTriangleVisitor;


    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
      itk::QuadrilateralCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      SingleCellArrayUserVisitorType>               SingleCellArrayQuadrilateralVisitor;


    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
      itk::TetrahedronCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      SingleCellArrayUserVisitorType>               SingleCellArrayTetrahedronVisitor;


    typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
      itk::HexahedronCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >, 
      SingleCellArrayUserVisitorType>               SingleCellArrayHexahedronVisitor;

    // Get the number of points in the mesh
    int numPoints = mesh->GetNumberOfPoints();
    if(numPoints == 0)
    {
      //mesh->Print(std::cerr);
      MITK_FATAL << "no points in Grid " << std::endl;
      exit(-1);
    }
    // Create a vtkUnstructuredGrid
    vtkUnstructuredGrid* vgrid = vtkUnstructuredGrid::New();
    // Create the vtkPoints object and set the number of points
#if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) ))
    vtkPoints* vpoints = vtkPoints::New( VTK_DOUBLE );
#else
    vtkPoints* vpoints = vtkPoints::New();
#endif
    vtkFloatArray* pointScalars = vtkFloatArray::New();
    vtkFloatArray* cellScalars = vtkFloatArray::New();
    pointScalars->SetNumberOfComponents(1);
    cellScalars->SetNumberOfComponents(1);

    typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
    typename MeshType::PointsContainer::Iterator i;

    // iterate over all the points in the itk mesh to find
    // the maximal index
    unsigned int maxIndex = 0;
    for(i = points->Begin(); i != points->End(); ++i)
    {
      if(maxIndex < i->Index())
        maxIndex = i->Index();
    }

    // initialize vtk-classes for points and scalars
    vpoints->SetNumberOfPoints(maxIndex+1);
    pointScalars->SetNumberOfTuples(maxIndex+1);
    cellScalars->SetNumberOfTuples(mesh->GetNumberOfCells());

    vtkFloatingPointType vtkpoint[3];
    typename MeshType::PointType itkPhysicalPoint;
    if (geometryFrame == 0)
    {
      for(i = points->Begin(); i != points->End(); ++i)
      {
        // Get the point index from the point container iterator
        int idx = i->Index();

        itkPhysicalPoint = i->Value();
        mitk::itk2vtk(itkPhysicalPoint, vtkpoint);
        // Set the vtk point at the index with the the coord array from itk
        vpoints->SetPoint(idx, vtkpoint);

        if(usePointScalarAccessor)
        {   
          pointScalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
        }
      }
    }
    else
    {
      mitk::Point3D mitkWorldPoint;
      for(i = points->Begin(); i != points->End(); ++i)
      {
        // Get the point index from the point container iterator
        int idx = i->Index();

        itkPhysicalPoint = i->Value();
        geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
        mitk::itk2vtk(mitkWorldPoint, vtkpoint);
        // Set the vtk point at the index with the the coord array from itk
        vpoints->SetPoint(idx, vtkpoint);

        if(usePointScalarAccessor)
        {   
          pointScalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
        }
      }
    }
    // Set the points on the vtk grid
    vgrid->SetPoints(vpoints);
    if (usePointScalarAccessor)
      vgrid->GetPointData()->SetScalars(pointScalars);

    // Now create the cells using the MultiVisitor
    // 1. Create a MultiVisitor
    typename MeshType::CellType::MultiVisitor::Pointer mv =
      MeshType::CellType::MultiVisitor::New();
    // 2. Create visitors
    typename SingleCellArrayLineVisitor::Pointer      lv = SingleCellArrayLineVisitor::New();
    typename SingleCellArrayPolygonVisitor::Pointer      pv = SingleCellArrayPolygonVisitor::New();
    typename SingleCellArrayTriangleVisitor::Pointer      tv = SingleCellArrayTriangleVisitor::New();
    typename SingleCellArrayQuadrilateralVisitor::Pointer qv = SingleCellArrayQuadrilateralVisitor::New();
    typename SingleCellArrayTetrahedronVisitor::Pointer tetv = SingleCellArrayTetrahedronVisitor::New();
    typename SingleCellArrayHexahedronVisitor::Pointer hv = SingleCellArrayHexahedronVisitor::New();
    // 3. Set up the visitors
    //int vtkCellCount = 0; // running counter for current cell being inserted into vtk
    int numCells = mesh->GetNumberOfCells();
    int *types = new int[numCells]; // type array for vtk 
    // create vtk cells and estimate the size
    vtkCellArray* cells = vtkCellArray::New();
    cells->Allocate(numCells);
    // Set the TypeArray CellCount and CellArray for the visitors
    lv->SetTypeArray(types);
    lv->SetCellArray(cells);
    pv->SetTypeArray(types);
    pv->SetCellArray(cells);
    tv->SetTypeArray(types);
    //tv->SetCellCounter(&vtkCellCount);
    tv->SetCellArray(cells);
    qv->SetTypeArray(types);
    //qv->SetCellCounter(&vtkCellCount);
    qv->SetCellArray(cells);
    tetv->SetTypeArray(types);
    tetv->SetCellArray(cells);
    hv->SetTypeArray(types);
    hv->SetCellArray(cells);

    if (useCellScalarAccessor)
    {
      lv->SetUseCellScalarAccessor(true);
      lv->SetCellScalars(cellScalars);
      lv->SetMeshCellData(mesh->GetCellData());

      pv->SetUseCellScalarAccessor(true);
      pv->SetCellScalars(cellScalars);
      pv->SetMeshCellData(mesh->GetCellData());

      tv->SetUseCellScalarAccessor(true);
      tv->SetCellScalars(cellScalars);
      tv->SetMeshCellData(mesh->GetCellData());

      qv->SetUseCellScalarAccessor(true);
      qv->SetCellScalars(cellScalars);
      qv->SetMeshCellData(mesh->GetCellData());

      tetv->SetUseCellScalarAccessor(true);
      tetv->SetCellScalars(cellScalars);
      tetv->SetMeshCellData(mesh->GetCellData());

      hv->SetUseCellScalarAccessor(true);
      hv->SetCellScalars(cellScalars);
      hv->SetMeshCellData(mesh->GetCellData());
    }

    // add the visitors to the multivisitor
    mv->AddVisitor(lv);
    mv->AddVisitor(pv);
    mv->AddVisitor(tv);
    mv->AddVisitor(qv);
    mv->AddVisitor(tetv);
    mv->AddVisitor(hv);
    // Now ask the mesh to accept the multivisitor which
    // will Call Visit for each cell in the mesh that matches the
    // cell types of the visitors added to the MultiVisitor
    mesh->Accept(mv);
    // Now set the cells on the vtk grid with the type array and cell array

    vgrid->SetCells(types, cells);
    vgrid->GetCellData()->SetScalars(cellScalars);

    // Clean up vtk objects (no vtkSmartPointer ... )
    cells->Delete();
    vpoints->Delete();
    delete[] types;

    pointScalars->Delete();
    cellScalars->Delete();
    //MITK_INFO << "meshToUnstructuredGrid end" << std::endl;
    return vgrid;
  }

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines