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 ];
87 // -------------------------------------------------------------------------
88 cpPlugins::DataObjects::BoundingBox::
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( );
98 // -------------------------------------------------------------------------
99 cpPlugins::DataObjects::BoundingBox::
104 // -------------------------------------------------------------------------
105 void cpPlugins::DataObjects::BoundingBox::
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 )
115 bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
116 bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
120 // Update vtk objects
121 this->m_Outline->SetBounds( bounds );
122 this->m_Outline->Update( );
123 this->m_VTK = this->m_Outline->GetOutput( );
126 // -------------------------------------------------------------------------
127 template< unsigned int _NDim >
128 bool cpPlugins::DataObjects::BoundingBox::
129 _ITKImage( itk::LightObject* o )
131 auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
135 auto region = image->GetLargestPossibleRegion( );
136 auto i0 = region.GetIndex( );
137 auto i1 = i0 + region.GetSize( );
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( );
145 for( unsigned int d = 0; d < _NDim; ++d )
147 this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
148 this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
155 // -------------------------------------------------------------------------
156 template< class _TScalar, unsigned int _NDim >
157 bool cpPlugins::DataObjects::BoundingBox::
158 _ITKPointSet( itk::LightObject* o )
160 typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
161 typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
163 auto ps = dynamic_cast< _TPointSet* >( o );
167 this->m_Points[ 0 ].clear( );
168 this->m_Points[ 1 ].clear( );
170 typename _TBBox::Pointer bb = _TBBox::New( );
171 bb->SetPoints( ps->GetPoints( ) );
172 if( bb->ComputeBoundingBox( ) )
174 auto p0 = bb->GetMinimum( );
175 auto p1 = bb->GetMaximum( );
176 for( unsigned int d = 0; d < _NDim; ++d )
178 this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
179 this->m_Points[ 1 ].push_back( double( p1[ d ] ) );