]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/BoundingBox.cxx
c10b8a2453aebcca88bb52a7f48c553e49c62496
[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 }
86
87 // -------------------------------------------------------------------------
88 cpPlugins::DataObjects::BoundingBox::
89 BoundingBox( )
90   : Superclass( )
91 {
92   this->m_Points[ 0 ].push_back( double( 0 ) );
93   this->m_Points[ 1 ].push_back( double( 0 ) );
94   this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
95   this->_UpdateVTK( );
96 }
97
98 // -------------------------------------------------------------------------
99 cpPlugins::DataObjects::BoundingBox::
100 ~BoundingBox( )
101 {
102 }
103
104 // -------------------------------------------------------------------------
105 void cpPlugins::DataObjects::BoundingBox::
106 _UpdateVTK( )
107 {
108   // Get bounds
109   double bounds[ 6 ] = { 0 };
110   unsigned int dim = this->m_Points[ 0 ].size( );
111   dim = ( this->m_Points[ 1 ].size( ) < dim )? this->m_Points[ 1 ].size( ): dim;
112   dim = ( dim < 3 )? dim: 3;
113   for( unsigned int d = 0; d < dim; ++d )
114   {
115     bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
116     bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
117
118   } // rof
119
120   // Update vtk objects
121   this->m_Outline->SetBounds( bounds );
122   this->m_Outline->Update( );
123   this->m_VTK = this->m_Outline->GetOutput( );
124 }
125
126 // -------------------------------------------------------------------------
127 template< unsigned int _NDim >
128 bool cpPlugins::DataObjects::BoundingBox::
129 _ITKImage( itk::LightObject* o )
130 {
131   auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
132   if( image == NULL )
133     return( false );
134
135   auto region = image->GetLargestPossibleRegion( );
136   auto i0 = region.GetIndex( );
137   auto i1 = i0 + region.GetSize( );
138
139   typename itk::ImageBase< _NDim >::PointType p0, p1;
140   image->TransformIndexToPhysicalPoint( i0, p0 );
141   image->TransformIndexToPhysicalPoint( i1, p1 );
142   this->m_Points[ 0 ].clear( );
143   this->m_Points[ 1 ].clear( );
144
145   for( unsigned int d = 0; d < _NDim; ++d )
146   {
147     this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
148     this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
149
150   } // rof
151   this->Modified( );
152   return( true );
153 }
154
155 // -------------------------------------------------------------------------
156 template< class _TScalar, unsigned int _NDim >
157 bool cpPlugins::DataObjects::BoundingBox::
158 _ITKPointSet( itk::LightObject* o )
159 {
160   typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
161   typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
162
163   auto ps = dynamic_cast< _TPointSet* >( o );
164   if( ps == NULL )
165     return( false );
166
167   this->m_Points[ 0 ].clear( );
168   this->m_Points[ 1 ].clear( );
169
170   typename _TBBox::Pointer bb = _TBBox::New( );
171   bb->SetPoints( ps->GetPoints( ) );
172   if( bb->ComputeBoundingBox( ) )
173   {
174     auto p0 = bb->GetMinimum( );
175     auto p1 = bb->GetMaximum( );
176     for( unsigned int d = 0; d < _NDim; ++d )
177     {
178       this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
179       this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
180
181     } // rof
182     this->Modified( );
183     return( true );
184   }
185   else
186     return( false );
187 }
188
189 // eof - $RCSfile$