]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/BoundingBox.hxx
e1618d74897017af10e9de599f4bff5dbd8b70b1
[cpPlugins.git] / lib / cpPlugins / DataObjects / BoundingBox.hxx
1 #ifndef __cpPlugins__DataObjects__BoundingBox__hxx__
2 #define __cpPlugins__DataObjects__BoundingBox__hxx__
3
4 #include <cpPlugins/DataObjects/Image.h>
5 #include <cpPlugins/DataObjects/Mesh.h>
6 #include <itkBoundingBox.h>
7 #include <itkPointSet.h>
8
9 // -------------------------------------------------------------------------
10 template< class _TPoint >
11 void cpPlugins::DataObjects::BoundingBox::
12 SetMinimum( const _TPoint& p )
13 {
14   this->_SetPoint( 0, p );
15 }
16
17 // -------------------------------------------------------------------------
18 template< class _TPoint >
19 void cpPlugins::DataObjects::BoundingBox::
20 SetMaximum( const _TPoint& p )
21 {
22   this->_SetPoint( 1, p );
23 }
24
25 // -------------------------------------------------------------------------
26 template< class _TPoint >
27 _TPoint cpPlugins::DataObjects::BoundingBox::
28 GetMinimum( ) const
29 {
30   return( this->_GetPoint< _TPoint >( 0 ) );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class _TPoint >
35 _TPoint cpPlugins::DataObjects::BoundingBox::
36 GetMaximum( ) const
37 {
38   return( this->_GetPoint< _TPoint >( 1 ) );
39 }
40
41 // -------------------------------------------------------------------------
42 template< class _TPoint >
43 void cpPlugins::DataObjects::BoundingBox::
44 _SetPoint( unsigned int m, const _TPoint& p )
45 {
46   this->m_Points[ m ].clear( );
47   for( unsigned int d = 0; d < _TPoint::PointDimension; ++d )
48     this->m_Points[ m ].push_back( double( p[ d ] ) );
49   this->_UpdateVTK( );
50   this->Modified( );
51 }
52
53 // -------------------------------------------------------------------------
54 template< class _TPoint >
55 _TPoint cpPlugins::DataObjects::BoundingBox::
56 _GetPoint( unsigned int m ) const
57 {
58   unsigned int dim = this->m_Points[ m ].size( );
59   dim = ( _TPoint::PointDimension < dim )? _TPoint::PointDimension: dim;
60   _TPoint p;
61   p.Fill( 0 );
62   for( unsigned int d = 0; d < dim; ++d )
63     p[ d ] = this->m_Points[ m ][ d ];
64   return( p );
65 }
66
67 // -------------------------------------------------------------------------
68 template< unsigned int _NDim >
69 bool cpPlugins::DataObjects::BoundingBox::
70 _ITKImage( itk::LightObject* o )
71 {
72   auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
73   if( image == NULL )
74     return( false );
75
76   auto region = image->GetLargestPossibleRegion( );
77   auto i0 = region.GetIndex( );
78   auto i1 = i0 + region.GetSize( );
79
80   typename itk::ImageBase< _NDim >::PointType p0, p1;
81   image->TransformIndexToPhysicalPoint( i0, p0 );
82   image->TransformIndexToPhysicalPoint( i1, p1 );
83   this->m_Points[ 0 ].clear( );
84   this->m_Points[ 1 ].clear( );
85
86   for( unsigned int d = 0; d < _NDim; ++d )
87   {
88     this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
89     this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
90
91   } // rof
92   this->Modified( );
93   return( true );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TScalar, unsigned int _NDim >
98 bool cpPlugins::DataObjects::BoundingBox::
99 _ITKPointSet( itk::LightObject* o )
100 {
101   typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
102   typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
103
104   auto ps = dynamic_cast< _TPointSet* >( o );
105   if( ps == NULL )
106     return( false );
107
108   this->m_Points[ 0 ].clear( );
109   this->m_Points[ 1 ].clear( );
110
111   typename _TBBox::Pointer bb = _TBBox::New( );
112   bb->SetPoints( ps->GetPoints( ) );
113   if( bb->ComputeBoundingBox( ) )
114   {
115     auto p0 = bb->GetMinimum( );
116     auto p1 = bb->GetMaximum( );
117     for( unsigned int d = 0; d < _NDim; ++d )
118     {
119       this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
120       this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
121
122     } // rof
123     this->Modified( );
124     return( true );
125   }
126   else
127     return( false );
128 }
129
130 #endif // __cpPlugins__DataObjects__BoundingBox__hxx__
131
132 // eof - $RCSfile$