Skip to content

Commit 2836aac

Browse files
authored
Merge pull request #44 from PranjalSahu/pranjal
ENH: Adding changes for itk::PolyLineCell
2 parents f2140a3 + 2558bd4 commit 2836aac

File tree

9 files changed

+128
-32
lines changed

9 files changed

+128
-32
lines changed

.github/workflows/build-test-package.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Build, test, package
33
on: [push,pull_request]
44

55
env:
6-
itk-git-tag: "835dc01388d22c4b4c9a46b01dbdfe394ec23511"
6+
itk-git-tag: "v5.3rc04"
77
itk-wheel-tag: "v5.3rc04.post2"
88

99
jobs:
@@ -17,14 +17,17 @@ jobs:
1717
- os: ubuntu-18.04
1818
c-compiler: "gcc"
1919
cxx-compiler: "g++"
20+
itk-git-tag: "v5.3rc04"
2021
cmake-build-type: "MinSizeRel"
2122
- os: windows-2019
2223
c-compiler: "cl.exe"
2324
cxx-compiler: "cl.exe"
25+
itk-git-tag: "v5.3rc04"
2426
cmake-build-type: "Release"
2527
- os: macos-10.15
2628
c-compiler: "clang"
2729
cxx-compiler: "clang++"
30+
itk-git-tag: "v5.3rc04"
2831
cmake-build-type: "MinSizeRel"
2932

3033
steps:
@@ -169,9 +172,6 @@ jobs:
169172
runs-on: macos-10.15
170173
strategy:
171174
max-parallel: 2
172-
matrix:
173-
include:
174-
- itk-python-git-tag: "v5.3rc04"
175175

176176
steps:
177177
- uses: actions/checkout@v2
@@ -206,8 +206,6 @@ jobs:
206206
max-parallel: 2
207207
matrix:
208208
python-version-minor: [7, 8, 9, 10]
209-
include:
210-
- itk-python-git-tag: "v5.3rc04"
211209

212210
steps:
213211
- name: Get specific version of CMake, Ninja

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
{

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name='itk-meshtopolydata',
16-
version='0.8.4',
16+
version='0.9.0',
1717
author='Insight Software Consortium',
1818
author_email='[email protected]',
1919
packages=['itk'],

test/itkMeshToPolyDataFilterTest.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ int itkMeshToPolyDataFilterTest( int argc, char * argv[] )
100100
ITK_TEST_EXPECT_TRUE( itk::Math::FloatAlmostEqual< float >( points->GetElement( 0 )[2], 0.0, 10, 1e-4 ) );
101101

102102
ITK_TEST_EXPECT_EQUAL( polyData->GetVertices()->size(), 0 );
103-
104103
ITK_TEST_EXPECT_EQUAL( polyData->GetLines()->size(), 0 );
105104

106105
ITK_TEST_EXPECT_EQUAL( polyData->GetPolygons()->size(), 15593 );

test/itkPolyDataToMeshFilterTest.cxx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ itkPolyDataToMeshFilterTest(int, char *[])
7070

7171
// Verify line cells
7272
meshResult->GetCell(2, cellPtr);
73-
ITK_TEST_EXPECT_EQUAL(cellPtr->GetNumberOfPoints(), 2);
74-
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[0], 4);
75-
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[1], 5);
73+
ITK_TEST_EXPECT_EQUAL(cellPtr->GetNumberOfPoints(), 3);
74+
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[0], 3);
75+
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[1], 4);
76+
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[2], 5);
7677
meshResult->GetCell(3, cellPtr);
7778
ITK_TEST_EXPECT_EQUAL(cellPtr->GetNumberOfPoints(), 2);
7879
ITK_TEST_EXPECT_EQUAL(cellPtr->GetPointIdsContainer()[0], 7);
@@ -142,12 +143,14 @@ MakePolyDataSample(itk::PolyData<TPixelType> * polyData)
142143
polyData->SetVertices(vertices);
143144

144145
typename CellContainerType::Pointer lines = CellContainerType::New();
145-
lines->InsertElement(0, 2);
146-
lines->InsertElement(1, 4);
147-
lines->InsertElement(2, 5);
148-
lines->InsertElement(3, 2);
149-
lines->InsertElement(4, 7);
150-
lines->InsertElement(5, 8);
146+
lines->InsertElement(0, 3);
147+
lines->InsertElement(1, 3);
148+
lines->InsertElement(2, 4);
149+
lines->InsertElement(3, 5);
150+
151+
lines->InsertElement(4, 2);
152+
lines->InsertElement(5, 7);
153+
lines->InsertElement(6, 8);
151154
polyData->SetLines(lines);
152155

153156
typename CellContainerType::Pointer strips = CellContainerType::New();

wrapping/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ itk_python_expression_add_test(NAME itkMeshToPolyDataFilterPythonTest
22
EXPRESSION "filt = itk.MeshToPolyDataFilter.New()")
33
itk_python_add_test(NAME itkMeshToPolyDataFilterPythonTest2
44
COMMAND itkMeshToPolyDataFilterTest2.py)
5+
itk_python_add_test(NAME itkPolyLineCellTest
6+
COMMAND itkPolyLineCellTest.py)
57
itk_python_expression_add_test(NAME itkPolyDataPythonTest
68
EXPRESSION "poly_data = itk.PolyData.New()")
79

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import itk
2+
import numpy as np
3+
import os
4+
5+
# Create a test mesh
6+
mesh_input = itk.Mesh[itk.D, 3].New()
7+
8+
# Inserting 5 points in the Mesh
9+
points_arr = np.array([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 1, 2, 3]).astype('float32')
10+
points_vc = itk.vector_container_from_array(points_arr.flatten())
11+
mesh_input.SetPoints(points_vc)
12+
13+
# Inserting 3 cells comprising 1 PolyLine Cell and 2 Line Cells
14+
# LINES 3 10
15+
# 2 0 1
16+
# 3 0 2 5
17+
# 2 3 4
18+
before_cells_array = np.array([1, 2, 0, 1, 10, 3, 0, 2, 5, 1, 2, 3, 4]).astype('uint64')
19+
mesh_input.SetCellsArray(itk.vector_container_from_array(before_cells_array))
20+
21+
# Convert Mesh to PolyData
22+
filter = itk.MeshToPolyDataFilter[type(mesh_input)].New(Input=mesh_input)
23+
filter.Update()
24+
poly_data = filter.GetOutput()
25+
assert(type(poly_data) == itk.PolyData[itk.D])
26+
lines = poly_data.GetLines()
27+
28+
# Check the count of points in line cells of polydata
29+
assert(lines.Size() == 10)
30+
31+
filter = itk.PolyDataToMeshFilter[type(poly_data)].New(Input=poly_data)
32+
filter.Update()
33+
mesh_output = filter.GetOutput()
34+
35+
assert(mesh_output.GetNumberOfPoints() == mesh_input.GetNumberOfPoints())
36+
assert(mesh_output.GetNumberOfCells() == mesh_input.GetNumberOfCells())
37+
38+
# Check if points are same
39+
for i in range(0, mesh_output.GetNumberOfPoints()):
40+
assert(mesh_output.GetPoint(i) == mesh_input.GetPoint(i))
41+
42+
# Check if cells are same
43+
after_cells_array = itk.array_from_vector_container(mesh_output.GetCellsArray())
44+
assert(np.array_equal(before_cells_array, after_cells_array))

0 commit comments

Comments
 (0)