1 #include <cpPlugins/DataObjects/BoundingBox.h>
3 #include <vtkDataSet.h>
4 #include <cpInstances_SimpleImages.h>
5 #include <cpInstances_Meshes.h>
7 // -------------------------------------------------------------------------
8 void cpPlugins::DataObjects::BoundingBox::
9 SetDataObject( DataObject* o )
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 );
17 // -------------------------------------------------------------------------
18 void cpPlugins::DataObjects::BoundingBox::
19 SetITK( itk::LightObject* o )
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 );
33 // -------------------------------------------------------------------------
34 void cpPlugins::DataObjects::BoundingBox::
35 SetVTK( vtkObjectBase* o )
37 auto ds = dynamic_cast< vtkDataSet* >( o );
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 ] );
55 // -------------------------------------------------------------------------
56 void cpPlugins::DataObjects::BoundingBox::
59 this->m_Points[ 0 ] = other->m_Points[ 0 ];
60 this->m_Points[ 1 ] = other->m_Points[ 1 ];
64 // -------------------------------------------------------------------------
65 void cpPlugins::DataObjects::BoundingBox::
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( )
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( )
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 ];
88 // -------------------------------------------------------------------------
89 cpPlugins::DataObjects::BoundingBox::
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( );
99 // -------------------------------------------------------------------------
100 cpPlugins::DataObjects::BoundingBox::
105 // -------------------------------------------------------------------------
106 void cpPlugins::DataObjects::BoundingBox::
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 )
116 bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
117 bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
121 // Update vtk objects
122 this->m_Outline->SetBounds( bounds );
123 this->m_Outline->Update( );
124 this->m_VTK = this->m_Outline->GetOutput( );
127 // -------------------------------------------------------------------------
128 template< unsigned int _NDim >
129 bool cpPlugins::DataObjects::BoundingBox::
130 _ITKImage( itk::LightObject* o )
132 auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
136 auto region = image->GetLargestPossibleRegion( );
137 auto i0 = region.GetIndex( );
138 auto i1 = i0 + region.GetSize( );
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( );
146 for( unsigned int d = 0; d < _NDim; ++d )
148 this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
149 this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
156 // -------------------------------------------------------------------------
157 template< class _TScalar, unsigned int _NDim >
158 bool cpPlugins::DataObjects::BoundingBox::
159 _ITKPointSet( itk::LightObject* o )
161 typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
162 typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
164 auto ps = dynamic_cast< _TPointSet* >( o );
168 this->m_Points[ 0 ].clear( );
169 this->m_Points[ 1 ].clear( );
171 typename _TBBox::Pointer bb = _TBBox::New( );
172 bb->SetPoints( ps->GetPoints( ) );
173 if( bb->ComputeBoundingBox( ) )
175 auto p0 = bb->GetMinimum( );
176 auto p1 = bb->GetMaximum( );
177 for( unsigned int d = 0; d < _NDim; ++d )
179 this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
180 this->m_Points[ 1 ].push_back( double( p1[ d ] ) );