Skip to content

Commit 5586157

Browse files
PranjalSahuthewtex
authored andcommitted
ENH: Adding changes for itk::PolyLineCell
1 parent f2140a3 commit 5586157

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

include/itkMeshToPolyDataFilter.hxx

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "itkVertexCell.h"
2424
#include "itkLineCell.h"
25+
#include "itkPolyLineCell.h"
2526
#include "itkMesh.h"
2627
#include "itkTriangleCell.h"
2728
#include "itkQuadrilateralCell.h"
@@ -45,6 +46,7 @@ public:
4546

4647
using VertexCellType = itk::VertexCell<CellInterfaceType>;
4748
using LineCellType = itk::LineCell<CellInterfaceType>;
49+
using PolyLineCellType = itk::PolyLineCell<CellInterfaceType>;
4850
using TriangleCellType = itk::TriangleCell<CellInterfaceType>;
4951
using QuadrilateralCellType = itk::QuadrilateralCell<CellInterfaceType>;
5052
using PolygonCellType = itk::PolygonCell<CellInterfaceType>;
@@ -115,9 +117,22 @@ public:
115117
m_Lines->push_back( numberOfPoints );
116118
const typename LineCellType::PointIdConstIterator pointIdEnd = cell->PointIdsEnd();
117119
for( typename LineCellType::PointIdConstIterator pointIdIt = cell->PointIdsBegin(); pointIdIt != pointIdEnd; ++pointIdIt )
118-
{
120+
{
119121
m_Lines->push_back( *pointIdIt );
120-
}
122+
}
123+
m_LinesCellIds->push_back( static_cast< unsigned int >( cellId ) );
124+
}
125+
126+
// Visit a polyline and create a polyline in the output
127+
void Visit(unsigned long cellId, PolyLineCellType* cell)
128+
{
129+
int numberOfPoints = cell->GetNumberOfPoints();
130+
m_Lines->push_back( numberOfPoints );
131+
const typename PolyLineCellType::PointIdConstIterator pointIdEnd = cell->PointIdsEnd();
132+
for( typename PolyLineCellType::PointIdConstIterator pointIdIt = cell->PointIdsBegin(); pointIdIt != pointIdEnd; ++pointIdIt )
133+
{
134+
m_Lines->push_back( *pointIdIt );
135+
}
121136
m_LinesCellIds->push_back( static_cast< unsigned int >( cellId ) );
122137
}
123138

@@ -430,6 +445,8 @@ MeshToPolyDataFilter< TInputMesh >
430445
vertices->reserve( numberOfCells / 4 + 1 );
431446
typename CellsContainerType::Pointer lines = CellsContainerType::New();
432447
lines->reserve( numberOfCells / 4 + 1 );
448+
typename CellsContainerType::Pointer polylines = CellsContainerType::New();
449+
polylines->reserve( numberOfCells / 4 + 1 );
433450
typename CellsContainerType::Pointer polygons = CellsContainerType::New();
434451
polygons->reserve( numberOfCells / 4 + 1 );
435452
//typename CellsContainerType::Pointer triangleStrips = CellsContainerType::New();
@@ -465,6 +482,21 @@ MeshToPolyDataFilter< TInputMesh >
465482
vertexVisitor->SetPolygonsCellIds( polygonsCellIds );
466483
//vertexVisitor->SetTriangleStripsCellIds( triangleStripsCellIds );
467484

485+
// Setup the poly line visitor
486+
typedef CellInterfaceVisitorImplementation<
487+
PixelType, CellTraits,
488+
PolyLineCell< CellInterface< PixelType, CellTraits > >,
489+
VisitCellsClass< InputMeshType, PolyDataType > > PolyLineVisitor;
490+
typename PolyLineVisitor::Pointer polyLineVisitor = PolyLineVisitor::New();
491+
polyLineVisitor->SetVertices( vertices );
492+
polyLineVisitor->SetLines( polylines );
493+
polyLineVisitor->SetPolygons( polygons );
494+
//lineVisitor->SetTriangleStrips( triangleStrips );
495+
polyLineVisitor->SetVerticesCellIds( verticesCellIds );
496+
polyLineVisitor->SetLinesCellIds( linesCellIds );
497+
polyLineVisitor->SetPolygonsCellIds( polygonsCellIds );
498+
//lineVisitor->SetTriangleStripsCellIds( triangleStripsCellIds );
499+
468500
// Setup the line visitor
469501
typedef CellInterfaceVisitorImplementation<
470502
PixelType, CellTraits,
@@ -545,6 +577,7 @@ MeshToPolyDataFilter< TInputMesh >
545577
typename InputMeshType::CellType::MultiVisitor::Pointer multiVisitor = InputMeshType::CellType::MultiVisitor::New();
546578
multiVisitor->AddVisitor(vertexVisitor.GetPointer());
547579
multiVisitor->AddVisitor(lineVisitor.GetPointer());
580+
multiVisitor->AddVisitor(polyLineVisitor.GetPointer());
548581
multiVisitor->AddVisitor(triangleVisitor.GetPointer());
549582
multiVisitor->AddVisitor(quadrilateralVisitor.GetPointer());
550583
multiVisitor->AddVisitor(polygonVisitor.GetPointer());
@@ -554,14 +587,23 @@ MeshToPolyDataFilter< TInputMesh >
554587
// will Call Visit for each cell in the mesh that matches the
555588
// cell types of the visitors added to the MultiVisitor
556589
if( numberOfCells )
557-
{
590+
{
558591
inputMesh->Accept(multiVisitor);
559-
}
592+
}
560593

561594
vertices->shrink_to_fit();
562595
outputPolyData->SetVertices( vertices );
596+
597+
// Append lines to polylines before calling SetLines
563598
lines->shrink_to_fit();
564-
outputPolyData->SetLines( lines );
599+
polylines->shrink_to_fit();
600+
601+
auto iterator_start_lines = lines->begin();
602+
auto iterator_end_lines = lines->end();
603+
auto iterator_end_polylines = polylines->end();
604+
polylines->insert(iterator_end_polylines, iterator_start_lines, iterator_end_lines);
605+
606+
outputPolyData->SetLines( polylines);
565607
polygons->shrink_to_fit();
566608
outputPolyData->SetPolygons( polygons );
567609
//triangleStrips->shrink_to_fit();
@@ -570,7 +612,7 @@ MeshToPolyDataFilter< TInputMesh >
570612
using CellDataContainerType = typename PolyDataType::CellDataContainer;
571613
const CellDataContainerType * inputCellData = inputMesh->GetCellData();
572614
if( inputCellData && inputCellData->Size() )
573-
{
615+
{
574616
typename CellDataContainerType::Pointer outputCellData = CellDataContainerType::New();
575617
outputCellData->Reserve( inputCellData->Size() );
576618
SizeValueType offset = 0;

include/itkPolyData.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ PolyData< TPixelType, TCellPixel >
141141
itkDebugMacro("setting Lines container to " << lines);
142142
if ( m_LinesContainer != lines )
143143
{
144-
m_LinesContainer = lines;
145-
this->Modified();
144+
m_LinesContainer = lines;
145+
this->Modified();
146146
}
147147
}
148148

include/itkPolyDataToMeshFilter.hxx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "itkVertexCell.h"
2424
#include "itkLineCell.h"
25+
#include "itkPolyLineCell.h"
2526
#include "itkMesh.h"
2627
#include "itkTriangleCell.h"
2728
#include "itkQuadrilateralCell.h"
@@ -205,9 +206,9 @@ PolyDataToMeshFilter<TInputPolyData>::GenerateData()
205206

206207
while (inputCellItr != inputCellEnd)
207208
{
208-
#ifndef NDEBUG
209+
#ifndef NDEBUG
209210
auto numPoints = inputCellItr.Value();
210-
#endif
211+
#endif
211212
++inputCellItr;
212213

213214
// Verify vertex contains exactly one point ID
@@ -227,6 +228,8 @@ PolyDataToMeshFilter<TInputPolyData>::GenerateData()
227228

228229
// Set line cells
229230
using LineCellType = itk::LineCell<CellType>;
231+
using PolyLineCellType = itk::PolyLineCell<CellType>;
232+
230233
if (inputPolyData->GetLines() != nullptr && !inputPolyData->GetLines()->empty())
231234
{
232235
const CellContainerType * inputLines = inputPolyData->GetLines();
@@ -238,12 +241,17 @@ PolyDataToMeshFilter<TInputPolyData>::GenerateData()
238241
auto numPoints = inputCellItr.Value();
239242
++inputCellItr;
240243

241-
// Verify lines contain exactly two point IDs
242-
itkAssertInDebugAndIgnoreInReleaseMacro(numPoints == LineCellType::NumberOfPoints);
243-
244-
// Create cell
245244
typename CellType::CellAutoPointer cell;
246-
cell.TakeOwnership(new LineCellType);
245+
// Use PolyLineCell Type
246+
if (numPoints > LineCellType::NumberOfPoints)
247+
{
248+
cell.TakeOwnership(new PolyLineCellType);
249+
}
250+
// Use LineCell Type
251+
else
252+
{
253+
cell.TakeOwnership(new LineCellType);
254+
}
247255

248256
for (unsigned int i = 0; i < numPoints; i++)
249257
{

0 commit comments

Comments
 (0)