[Insight-users] itk::Mesh and itk::QuadEdgeMesh converter

Gao, Yi gaoyi.cn at gmail.com
Fri Sep 9 10:41:05 EDT 2011


Hi Alex and Arnaud,

Here is the converter I used. It was the convert from vtkpolydata to
itkMesh I got in 2006 (did I get this from you? Or download it
somewhere? Or I wrote it myself? I really forgot... ).

I just change everything to QEmesh.

Although I define qemesh <double, 3>, in the compilation error, i get
its type as:
itk::QuadEdgeMeshPoint<float, 3u>. Then I can't use a double[3] to
init the point.

(in fact it's itk::QuadEdgeMeshPoint<float, 3u,
itk::GeometricalQuadEdge<long unsigned int, long unsigned int, bool,
bool, true> >::QuadEdgeMeshPoint(double [3])’)

I could change everything I have to float, I will try that also.


Here is the converter function:

typedef itk::QuadEdgeMesh< double, 3 > QEMeshType;

QEMeshType::Pointer vtkPolyDataToITKQEMesh(
vtkSmartPointer<vtkPolyData> polyData)
  {
    /// Create a new mesh
    QEMeshType::Pointer mesh = QEMeshType::New();

    /// Transfer the points from the vtkPolyData into the itk::Mesh
    const unsigned int numberOfPoints = polyData->GetNumberOfPoints();

    vtkSmartPointer<vtkPoints> vtkpoints = polyData->GetPoints();

    mesh->GetPoints()->Reserve( numberOfPoints );

    {
      double apoint[3];

      for(unsigned int p =0; p < numberOfPoints; p++)
        {
          vtkpoints->GetPoint( p, apoint );
          mesh->SetPoint( p, QEMeshType::PointType( apoint ));
        }
    }

    /// Transfer the cells from the vtkPolyData into the itk::Mesh
    vtkSmartPointer<vtkCellArray> triangleStrips = polyData->GetStrips();


    vtkIdType* cellPoints;
    vtkIdType numberOfCellPoints;

    /// First count the total number of triangles from all the triangle strips.
    unsigned int numberOfTriangles = 0;

    triangleStrips->InitTraversal();

    while( triangleStrips->GetNextCell( numberOfCellPoints, cellPoints ) )
      {
        numberOfTriangles += numberOfCellPoints-2;
      }

    vtkSmartPointer<vtkCellArray> polygons = polyData->GetPolys();

    polygons->InitTraversal();

    while( polygons->GetNextCell( numberOfCellPoints, cellPoints ) )
      {
        if( numberOfCellPoints == 3 )
          {
            numberOfTriangles ++;
          }
      }


    /// Reserve memory in the itk::Mesh for all those triangles
    mesh->GetCells()->Reserve( numberOfTriangles );


    /// Copy the triangles from vtkPolyData into the itk::Mesh
    typedef QEMeshType::CellType CellType;
    typedef itk::TriangleCell< CellType > TriangleCellType;

    int cellId = 0;

    /// first copy the triangle strips
    triangleStrips->InitTraversal();
    while( triangleStrips->GetNextCell( numberOfCellPoints, cellPoints ) )
      {

        unsigned int numberOfTrianglesInStrip = numberOfCellPoints - 2;

        unsigned long pointIds[3];
        pointIds[0] = cellPoints[0];
        pointIds[1] = cellPoints[1];
        pointIds[2] = cellPoints[2];

        for( unsigned int t=0; t < numberOfTrianglesInStrip; t++ )
          {
            QEMeshType::CellAutoPointer c;
            TriangleCellType * tcell = new TriangleCellType;
            tcell->SetPointIds( pointIds );
            c.TakeOwnership( tcell );
            mesh->SetCell( cellId, c );
            cellId++;
            pointIds[0] = pointIds[1];
            pointIds[1] = pointIds[2];
            pointIds[2] = cellPoints[t+3];
          }
      }

    /// then copy the normal triangles
    polygons->InitTraversal();
    while( polygons->GetNextCell( numberOfCellPoints, cellPoints ) )
      {
        if( numberOfCellPoints !=3 ) // skip any non-triangle.
          {
            continue;
          }
        QEMeshType::CellAutoPointer c;
        TriangleCellType * t = new TriangleCellType;
        //t->SetPointIds( static_cast<unsigned long*>(cellPoints) );
        t->SetPointIds( (unsigned long*)cellPoints );
        c.TakeOwnership( t );
        mesh->SetCell( cellId, c );
        cellId++;
      }

    return mesh;
  }

Thank you for any hint!

Best,
yi

On Fri, Sep 9, 2011 at 9:57 AM, Alexandre GOUAILLARD
<agouaillard at gmail.com> wrote:
> hum,
>
> what is you "converter" between polydata and itk:mesh?
> you shoudl be able to use the old vtkPolydatawriter templated over a QEMesh ...
>
> alex.
>
>
> On Fri, Sep 9, 2011 at 9:32 PM, Gao, Yi <gaoyi.cn at gmail.com> wrote:
>> Dear Alex,
>>
>> Thank you for the reply!
>>
>> I'm trying to use the smoothing filter Arnaud and you contributed. The
>> triangles it gives are much better shaped than the vtk's windowed sinc
>> filter!
>>
>> But my main pipeline is in VTK so in fact I want to convert from
>> vtkPolyData with QE mesh. Since I have converter between vtkPolyData
>> <--> itk::Mesh, then the missing step is itk::Mesh <-->QE mesh. That
>> is where the problem comes from. :)
>>
>> So I'll just get an itk::Mesh and feed it to the smoothing filter for
>> QE mesh, I will try that out!
>>
>> Thanks again! :)
>>
>> Best,
>> yi
>>
>> On Fri, Sep 9, 2011 at 8:47 AM, Alexandre GOUAILLARD
>> <agouaillard at gmail.com> wrote:
>>> dear gao yi,
>>>
>>> an itk:QuadEdgeMesh is an itk:Mesh. You can pass the pointer to the
>>> itk:Pointset directly (sharing memory), then you can just make one
>>> loop on the cell container (and equivalent data) to feed your itk:Mesh
>>> with the cells. There is a little bit of magic to do as the cells
>>> types are a little bit different but nothing fancy. Namely, in a
>>> QEMesh cells are polygons and do not differentiate from triangles,
>>> quads or others. In your case, if you know it s triangular, then it s
>>> easier.
>>>
>>> Any reason you want to change it to itk:Mesh by the way? QEMesh API is
>>> supposedly 100% compatible with Mesh, so if there is a limitation,
>>> please let me know.
>>>
>>> What is your specific problem? If you re at miccai, we can take a look
>>> at it together, otherwise I can give a hand by e-mail.
>>>
>>> alex.
>>>
>>>
>>> On Fri, Sep 9, 2011 at 8:29 PM, Gao, Yi <gaoyi.cn at gmail.com> wrote:
>>>> Dear list,
>>>>
>>>> I'm trying to convert a triangular 2D mesh (all in memory, not in
>>>> file) from itk::QuadEdgeMesh to itk::Mesh (or to VTK polydata).
>>>>
>>>> The itkVTKPolyDataReader and writer can do this... but I will need to
>>>> write to file and then load, which does seem to be the optimal
>>>> solution. :)
>>>>
>>>> I googled but didn't find such a solution, could I get any hint?
>>>>
>>>> Thank you in advance for the help!
>>>>
>>>> Best,
>>>> yi
>>>> _____________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Kitware offers ITK Training Courses, for more information visit:
>>>> http://www.kitware.com/products/protraining.html
>>>>
>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>
>>>
>>
>


More information about the Insight-users mailing list