-#include <cpPlugins/DataObjects/BoundingBox.h>
-#include <limits>
-#include <vtkDataSet.h>
-#include <cpInstances_SimpleImages.h>
-#include <cpInstances_Meshes.h>
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-SetDataObject( DataObject* o )
-{
- auto i = o->GetITK< itk::LightObject >( );
- auto v = o->GetVTK< vtkObjectBase >( );
- if( v != NULL ) this->SetVTK( v );
- else if( i != NULL ) this->SetITK( i );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-SetITK( itk::LightObject* o )
-{
- bool r = this->_ITKImage< 1 >( o );
- if( !r ) r = this->_ITKImage< 2 >( o );
- if( !r ) r = this->_ITKImage< 3 >( o );
- if( !r ) r = this->_ITKImage< 4 >( o );
- if( !r ) r = this->_ITKPointSet< float, 2 >( o );
- if( !r ) r = this->_ITKPointSet< double, 2 >( o );
- if( !r ) r = this->_ITKPointSet< float, 3 >( o );
- if( !r ) r = this->_ITKPointSet< double, 3 >( o );
- if( r )
- this->_UpdateVTK( );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-SetVTK( vtkObjectBase* o )
-{
- auto ds = dynamic_cast< vtkDataSet* >( o );
- if( ds != NULL )
- {
- double bounds[ 6 ];
- ds->GetBounds( bounds );
- this->m_Points[ 0 ].clear( );
- this->m_Points[ 1 ].clear( );
- this->m_Points[ 0 ].push_back( bounds[ 0 ] );
- this->m_Points[ 1 ].push_back( bounds[ 1 ] );
- this->m_Points[ 0 ].push_back( bounds[ 2 ] );
- this->m_Points[ 1 ].push_back( bounds[ 3 ] );
- this->m_Points[ 0 ].push_back( bounds[ 4 ] );
- this->m_Points[ 1 ].push_back( bounds[ 5 ] );
- this->_UpdateVTK( );
-
- } // fi
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-Copy( Self* other )
-{
- this->m_Points[ 0 ] = other->m_Points[ 0 ];
- this->m_Points[ 1 ] = other->m_Points[ 1 ];
- this->Modified( );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-Blend( Self* other )
-{
- if( this->m_Points[ 0 ].size( ) < other->m_Points[ 0 ].size( ) )
- this->m_Points[ 0 ].resize(
- other->m_Points[ 0 ].size( ),
- std::numeric_limits< double >::max( )
- );
- if( this->m_Points[ 1 ].size( ) < other->m_Points[ 1 ].size( ) )
- this->m_Points[ 1 ].resize(
- other->m_Points[ 1 ].size( ),
- -std::numeric_limits< double >::max( )
- );
- for( unsigned int d = 0; d < this->m_Points[ 0 ].size( ); ++d )
- if( other->m_Points[ 0 ][ d ] < this->m_Points[ 0 ][ d ] )
- this->m_Points[ 0 ][ d ] = other->m_Points[ 0 ][ d ];
- for( unsigned int d = 0; d < this->m_Points[ 1 ].size( ); ++d )
- if( other->m_Points[ 1 ][ d ] > this->m_Points[ 1 ][ d ] )
- this->m_Points[ 1 ][ d ] = other->m_Points[ 1 ][ d ];
- this->Modified( );
- this->_UpdateVTK( );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::DataObjects::BoundingBox::
-BoundingBox( )
- : Superclass( )
-{
- this->m_Points[ 0 ].push_back( double( 0 ) );
- this->m_Points[ 1 ].push_back( double( 0 ) );
- this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
- this->_UpdateVTK( );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::DataObjects::BoundingBox::
-~BoundingBox( )
-{
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::DataObjects::BoundingBox::
-_UpdateVTK( )
-{
- // Get bounds
- double bounds[ 6 ] = { 0 };
- unsigned int dim = this->m_Points[ 0 ].size( );
- dim = ( this->m_Points[ 1 ].size( ) < dim )? this->m_Points[ 1 ].size( ): dim;
- dim = ( dim < 3 )? dim: 3;
- for( unsigned int d = 0; d < dim; ++d )
- {
- bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
- bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
-
- } // rof
-
- // Update vtk objects
- this->m_Outline->SetBounds( bounds );
- this->m_Outline->Update( );
- this->m_VTK = this->m_Outline->GetOutput( );
-}
-
-// -------------------------------------------------------------------------
-template< unsigned int _NDim >
-bool cpPlugins::DataObjects::BoundingBox::
-_ITKImage( itk::LightObject* o )
-{
- auto image = dynamic_cast< itk::ImageBase< _NDim >* >( o );
- if( image == NULL )
- return( false );
-
- auto region = image->GetLargestPossibleRegion( );
- auto i0 = region.GetIndex( );
- auto i1 = i0 + region.GetSize( );
-
- typename itk::ImageBase< _NDim >::PointType p0, p1;
- image->TransformIndexToPhysicalPoint( i0, p0 );
- image->TransformIndexToPhysicalPoint( i1, p1 );
- this->m_Points[ 0 ].clear( );
- this->m_Points[ 1 ].clear( );
-
- for( unsigned int d = 0; d < _NDim; ++d )
- {
- this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
- this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
-
- } // rof
- this->Modified( );
- return( true );
-}
-
-// -------------------------------------------------------------------------
-template< class _TScalar, unsigned int _NDim >
-bool cpPlugins::DataObjects::BoundingBox::
-_ITKPointSet( itk::LightObject* o )
-{
- typedef itk::PointSet< _TScalar, _NDim > _TPointSet;
- typedef itk::BoundingBox< typename _TPointSet::PointIdentifier, _NDim, _TScalar, typename _TPointSet::PointsContainer > _TBBox;
-
- auto ps = dynamic_cast< _TPointSet* >( o );
- if( ps == NULL )
- return( false );
-
- this->m_Points[ 0 ].clear( );
- this->m_Points[ 1 ].clear( );
-
- typename _TBBox::Pointer bb = _TBBox::New( );
- bb->SetPoints( ps->GetPoints( ) );
- if( bb->ComputeBoundingBox( ) )
- {
- auto p0 = bb->GetMinimum( );
- auto p1 = bb->GetMaximum( );
- for( unsigned int d = 0; d < _NDim; ++d )
- {
- this->m_Points[ 0 ].push_back( double( p0[ d ] ) );
- this->m_Points[ 1 ].push_back( double( p1[ d ] ) );
-
- } // rof
- this->Modified( );
- return( true );
- }
- else
- return( false );
-}
-
-// eof - $RCSfile$