]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/BoundingBox.cxx
6ecf3f224d83c9ad9b2f41a6676875a2e6f5874e
[cpPlugins.git] / lib / cpPlugins / DataObjects / BoundingBox.cxx
1 #include <cpPlugins/DataObjects/BoundingBox.h>
2 #include <limits>
3 #include <vtkDataSet.h>
4 #include <cpInstances_SimpleImages.h>
5 #include <cpInstances_Meshes.h>
6
7 // -------------------------------------------------------------------------
8 void cpPlugins::DataObjects::BoundingBox::
9 SetDataObject( DataObject* o )
10 {
11   auto i = o->GetITK< itk::LightObject >( );
12   auto v = o->GetVTK< vtkObjectBase >( );
13   if( v != NULL )      this->SetVTK( v );
14   else if( i != NULL ) this->SetITK( i );
15 }
16
17 // -------------------------------------------------------------------------
18 void cpPlugins::DataObjects::BoundingBox::
19 SetITK( itk::LightObject* o )
20 {
21   bool     r = this->_ITKImage< 1 >( o );
22   if( !r ) r = this->_ITKImage< 2 >( o );
23   if( !r ) r = this->_ITKImage< 3 >( o );
24   if( !r ) r = this->_ITKImage< 4 >( o );
25   if( !r ) r = this->_ITKPointSet< float, 2 >( o );
26   if( !r ) r = this->_ITKPointSet< double, 2 >( o );
27   if( !r ) r = this->_ITKPointSet< float, 3 >( o );
28   if( !r ) r = this->_ITKPointSet< double, 3 >( o );
29   if( r )
30     this->_UpdateVTK( );
31 }
32
33 // -------------------------------------------------------------------------
34 void cpPlugins::DataObjects::BoundingBox::
35 SetVTK( vtkObjectBase* o )
36 {
37   auto ds = dynamic_cast< vtkDataSet* >( o );
38   if( ds != NULL )
39   {
40     double bounds[ 6 ];
41     ds->GetBounds( bounds );
42     this->m_Points[ 0 ].clear( );
43     this->m_Points[ 1 ].clear( );
44     this->m_Points[ 0 ].push_back( bounds[ 0 ] );
45     this->m_Points[ 1 ].push_back( bounds[ 1 ] );
46     this->m_Points[ 0 ].push_back( bounds[ 2 ] );
47     this->m_Points[ 1 ].push_back( bounds[ 3 ] );
48     this->m_Points[ 0 ].push_back( bounds[ 4 ] );
49     this->m_Points[ 1 ].push_back( bounds[ 5 ] );
50     this->_UpdateVTK( );
51
52   } // fi
53 }
54
55 // -------------------------------------------------------------------------
56 void cpPlugins::DataObjects::BoundingBox::
57 Copy( Self* other )
58 {
59   this->m_Points[ 0 ] = other->m_Points[ 0 ];
60   this->m_Points[ 1 ] = other->m_Points[ 1 ];
61   this->Modified( );
62 }
63
64 // -------------------------------------------------------------------------
65 void cpPlugins::DataObjects::BoundingBox::
66 Blend( Self* other )
67 {
68   if( this->m_Points[ 0 ].size( ) < other->m_Points[ 0 ].size( ) )
69     this->m_Points[ 0 ].resize(
70       other->m_Points[ 0 ].size( ),
71       std::numeric_limits< double >::max( )
72       );
73   if( this->m_Points[ 1 ].size( ) < other->m_Points[ 1 ].size( ) )
74     this->m_Points[ 1 ].resize(
75       other->m_Points[ 1 ].size( ),
76       -std::numeric_limits< double >::max( )
77       );
78   for( unsigned int d = 0; d < this->m_Points[ 0 ].size( ); ++d )
79     if( other->m_Points[ 0 ][ d ] < this->m_Points[ 0 ][ d ] )
80       this->m_Points[ 0 ][ d ] = other->m_Points[ 0 ][ d ];
81   for( unsigned int d = 0; d < this->m_Points[ 1 ].size( ); ++d )
82     if( other->m_Points[ 1 ][ d ] > this->m_Points[ 1 ][ d ] )
83       this->m_Points[ 1 ][ d ] = other->m_Points[ 1 ][ d ];
84   this->Modified( );
85   this->_UpdateVTK( );
86 }
87
88 // -------------------------------------------------------------------------
89 cpPlugins::DataObjects::BoundingBox::
90 BoundingBox( )
91   : Superclass( )
92 {
93   this->m_Points[ 0 ].push_back( double( 0 ) );
94   this->m_Points[ 1 ].push_back( double( 0 ) );
95   this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
96   this->_UpdateVTK( );
97 }
98
99 // -------------------------------------------------------------------------
100 cpPlugins::DataObjects::BoundingBox::
101 ~BoundingBox( )
102 {
103 }
104
105 // -------------------------------------------------------------------------
106 void cpPlugins::DataObjects::BoundingBox::
107 _UpdateVTK( )
108 {
109   // Get bounds
110   double bounds[ 6 ] = { 0 };
111   unsigned int dim = this->m_Points[ 0 ].size( );
112   dim = ( this->m_Points[ 1 ].size( ) < dim )? this->m_Points[ 1 ].size( ): dim;
113   dim = ( dim < 3 )? dim: 3;
114   for( unsigned int d = 0; d < dim; ++d )
115   {
116     bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
117     bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
118
119   } // rof
120
121   // Update vtk objects
122   this->m_Outline->SetBounds( bounds );
123   this->m_Outline->Update( );
124   this->m_VTK = this->m_Outline->GetOutput( );
125 }
126
127 // -------------------------------------------------------------------------
128 template< unsigned int _NDim >
129 bool cpPlugins::DataObjects::BoundingBox::
130 _ITKImage( itk::LightObject* o )
131 {
132   auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
133   if( image == NULL )
134     return( false );
135
136   auto region = image->GetLargestPossibleRegion( );
137   auto i0 = region.GetIndex( );
138   auto i1 = i0 + region.GetSize( );
139
140   typename itk::ImageBase< _NDim >::PointType p0, p1;
141   image->TransformIndexToPhysicalPoint( i0, p0 );
142   image->TransformIndexToPhysicalPoint( i1, p1 );
143   this->m_Points[ 0 ].clear( );
144   this->m_Points[ 1 ].clear( );
145
146   for( unsigned int d = 0; d < _NDim; ++d )
147   {
148     this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
149     this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
150
151   } // rof
152   this->Modified( );
153   return( true );
154 }
155
156 // -------------------------------------------------------------------------
157 template< class _TScalar, unsigned int _NDim >
158 bool cpPlugins::DataObjects::BoundingBox::
159 _ITKPointSet( itk::LightObject* o )
160 {
161   typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
162   typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
163
164   auto ps = dynamic_cast< _TPointSet* >( o );
165   if( ps == NULL )
166     return( false );
167
168   this->m_Points[ 0 ].clear( );
169   this->m_Points[ 1 ].clear( );
170
171   typename _TBBox::Pointer bb = _TBBox::New( );
172   bb->SetPoints( ps->GetPoints( ) );
173   if( bb->ComputeBoundingBox( ) )
174   {
175     auto p0 = bb->GetMinimum( );
176     auto p1 = bb->GetMaximum( );
177     for( unsigned int d = 0; d < _NDim; ++d )
178     {
179       this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
180       this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
181
182     } // rof
183     this->Modified( );
184     return( true );
185   }
186   else
187     return( false );
188 }
189
190 // eof - $RCSfile$