]> Creatis software - cpMesh.git/blob - lib/cpm/Algorithms/QuadEdge/MeshToDualFilter.hxx
First commit
[cpMesh.git] / lib / cpm / Algorithms / QuadEdge / MeshToDualFilter.hxx
1 #ifndef __CPM__ALGORITHMS__QUADEDGEMESHTO__DUALFILTER__HXX__
2 #define __CPM__ALGORITHMS__QUADEDGEMESHTO__DUALFILTER__HXX__
3
4 #include <queue>
5 #include <map>
6 #include <itkPolygonCell.h>
7
8 // -------------------------------------------------------------------------
9 template< class I, class O >
10 cpm::Algorithms::QuadEdge::MeshToDualFilter< I, O >::
11 MeshToDualFilter( )
12   : Superclass( )
13 {
14 }
15
16 // -------------------------------------------------------------------------
17 template< class I, class O >
18 cpm::Algorithms::QuadEdge::MeshToDualFilter< I, O >::
19 ~MeshToDualFilter( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 template< class I, class O >
25 void cpm::Algorithms::QuadEdge::MeshToDualFilter< I, O >::
26 GenerateData( )
27 {
28   typedef itk::PolygonCell< typename O::CellType > TPolygon;
29   typedef typename I::TPrimalEdge                  TInPrimalEdge;
30   typedef typename I::PixelType                    TInScalar;
31   typedef typename O::PixelType                    TOutScalar;
32   typedef typename I::PointType                    TInPoint;
33   typedef typename O::PointType                    TOutPoint;
34   typedef typename TInPoint::VectorType            TInVector;
35   typedef std::map< TPointId, TPointId >          _TBorderColumn;
36   typedef std::map< TPointId, _TBorderColumn >    _TBorderMatrix;
37
38   const I* in = this->GetInput( );
39   O* out = this->GetOutput( );
40   // TODO: this->m_Function->SetInputMesh( in );
41
42   // Convert faces to points
43   unsigned long nCells = in->GetNumberOfCells( );
44   out->GetPoints( )->Reserve( nCells );
45   for( unsigned long cId = 0; cId < nCells; cId++ )
46   {
47     const typename I::TPrimalEdge* e = in->FindEntryEdge( cId );
48     if( e == NULL )
49       continue;
50     if( !( e->IsLeftSet( ) ) )
51       continue;
52
53     // Compute new point
54     TInVector bc( TInScalar( 0 ) );
55     typename TInPrimalEdge::PointConstIterator pIt = e->BeginPointLnext( );
56     unsigned long pCount = 0;
57     for( ; pIt != e->EndPointLnext( ); pIt++ )
58     {
59       bc += in->GetPoint( *pIt ).GetVectorFromOrigin( );
60       pCount++;
61
62     } // rof
63     if( pCount != 0 )
64       bc /= TInScalar( pCount );
65
66     TOutPoint pnt;
67     for( unsigned int d = 0; d < I::PointDimension; d++ )
68       pnt[ d ] = TOutScalar( bc[ d ] );
69     out->SetPoint( e->GetLeft( ), pnt );
70
71   } // rof
72
73   _TBorderMatrix borderIds;
74   unsigned long nPoints = in->GetNumberOfPoints( );
75   for( unsigned long pId = 0; pId < nPoints; pId++ )
76   {
77     const TInPrimalEdge* e = in->FindEdge( pId );
78
79     // Add dual faces: each point becomes a face
80     typename O::CellAutoPointer cell;
81     TPolygon* face = new TPolygon( );
82     typename TInPrimalEdge::ConstIterator eIt = e->BeginOnext( );
83     for( ; eIt != e->EndOnext( ); eIt++ )
84     {
85       if( ( *eIt )->IsAtBorder( ) )
86       {
87         // Keep track of created border points
88         TPointId ori = ( *eIt )->GetOrigin( );
89         TPointId des = ( *eIt )->GetDestination( );
90         typename _TBorderMatrix::const_iterator bmIt =
91           borderIds.find( ori );
92         bool found = false;
93         if( bmIt != borderIds.end( ) )
94           found = ( bmIt->second.find( des ) != bmIt->second.end( ) );
95         if( !found )
96         {
97           // Compute border point
98           TInVector bc = in->GetPoint( ( *eIt )->GetOrigin( ) ).
99             GetVectorFromOrigin( );
100           bc += in->GetPoint( ( *eIt )->GetDestination( ) ).
101             GetVectorFromOrigin( );
102           bc /= TInScalar( 2 );
103
104           TOutPoint pnt;
105           for( unsigned int d = 0; d < I::PointDimension; d++ )
106             pnt[ d ] = TOutScalar( bc[ d ] );
107
108           unsigned long nOutPoints = out->GetNumberOfPoints( );
109           out->SetPoint( nOutPoints, pnt );
110           borderIds[ ori ][ des ] = nOutPoints;
111           borderIds[ des ][ ori ] = nOutPoints;
112
113         } // fi
114         face->AddPointId( borderIds[ ori ][ des ] );
115
116       } // fi
117       if( ( *eIt )->IsLeftSet( ) )
118         face->AddPointId( ( *eIt )->GetLeft( ) );
119
120     } // rof
121     cell.TakeOwnership( face );
122     out->SetCell( out->GetNumberOfCells( ), cell );
123
124   } // rof
125 }
126
127 #endif // __CPM__ALGORITHMS__QUADEDGEMESHTO__DUALFILTER__HXX__
128
129 // eof - $RCSfile$