]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/BoundingBox.cxx
0a0480c3dab4f2792c83af00de90b8772d72850b
[cpPlugins.git] / lib / cpPlugins / DataObjects / BoundingBox.cxx
1 #include <cpPlugins/DataObjects/BoundingBox.h>
2 #include <limits>
3 #include <vtkDataSet.h>
4
5 // -------------------------------------------------------------------------
6 void cpPlugins::DataObjects::BoundingBox::
7 SetDataObject( DataObject* o )
8 {
9   auto i = o->GetITK< itk::LightObject >( );
10   auto v = o->GetVTK< vtkObjectBase >( );
11   if( v != NULL )      this->SetVTK( v );
12   else if( i != NULL ) this->SetITK( i );
13 }
14
15 // -------------------------------------------------------------------------
16 void cpPlugins::DataObjects::BoundingBox::
17 SetITK( itk::LightObject* o )
18 {
19   bool     r = this->_ITKImage< 1 >( o );
20   if( !r ) r = this->_ITKImage< 2 >( o );
21   if( !r ) r = this->_ITKImage< 3 >( o );
22   if( !r ) r = this->_ITKImage< 4 >( o );
23   if( !r ) r = this->_ITKPointSet< float, 2 >( o );
24   if( !r ) r = this->_ITKPointSet< double, 2 >( o );
25   if( !r ) r = this->_ITKPointSet< float, 3 >( o );
26   if( !r ) r = this->_ITKPointSet< double, 3 >( o );
27   if( r )
28     this->_UpdateVTK( );
29 }
30
31 // -------------------------------------------------------------------------
32 void cpPlugins::DataObjects::BoundingBox::
33 SetVTK( vtkObjectBase* o )
34 {
35   auto ds = dynamic_cast< vtkDataSet* >( o );
36   if( ds != NULL )
37   {
38     double bounds[ 6 ];
39     ds->GetBounds( bounds );
40     this->m_Points[ 0 ].clear( );
41     this->m_Points[ 1 ].clear( );
42     this->m_Points[ 0 ].push_back( bounds[ 0 ] );
43     this->m_Points[ 1 ].push_back( bounds[ 1 ] );
44     this->m_Points[ 0 ].push_back( bounds[ 2 ] );
45     this->m_Points[ 1 ].push_back( bounds[ 3 ] );
46     this->m_Points[ 0 ].push_back( bounds[ 4 ] );
47     this->m_Points[ 1 ].push_back( bounds[ 5 ] );
48     this->_UpdateVTK( );
49
50   } // fi
51 }
52
53 // -------------------------------------------------------------------------
54 void cpPlugins::DataObjects::BoundingBox::
55 Copy( Self* other )
56 {
57   this->m_Points[ 0 ] = other->m_Points[ 0 ];
58   this->m_Points[ 1 ] = other->m_Points[ 1 ];
59   this->Modified( );
60 }
61
62 // -------------------------------------------------------------------------
63 void cpPlugins::DataObjects::BoundingBox::
64 Blend( Self* other )
65 {
66   if( this->m_Points[ 0 ].size( ) < other->m_Points[ 0 ].size( ) )
67     this->m_Points[ 0 ].resize(
68       other->m_Points[ 0 ].size( ),
69       std::numeric_limits< double >::max( )
70       );
71   if( this->m_Points[ 1 ].size( ) < other->m_Points[ 1 ].size( ) )
72     this->m_Points[ 1 ].resize(
73       other->m_Points[ 1 ].size( ),
74       -std::numeric_limits< double >::max( )
75       );
76   for( unsigned int d = 0; d < this->m_Points[ 0 ].size( ); ++d )
77     if( other->m_Points[ 0 ][ d ] < this->m_Points[ 0 ][ d ] )
78       this->m_Points[ 0 ][ d ] = other->m_Points[ 0 ][ d ];
79   for( unsigned int d = 0; d < this->m_Points[ 1 ].size( ); ++d )
80     if( other->m_Points[ 1 ][ d ] > this->m_Points[ 1 ][ d ] )
81       this->m_Points[ 1 ][ d ] = other->m_Points[ 1 ][ d ];
82   this->Modified( );
83 }
84
85 // -------------------------------------------------------------------------
86 cpPlugins::DataObjects::BoundingBox::
87 BoundingBox( )
88   : Superclass( )
89 {
90   this->m_Points[ 0 ].push_back( double( 0 ) );
91   this->m_Points[ 1 ].push_back( double( 0 ) );
92   this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
93   this->_UpdateVTK( );
94 }
95
96 // -------------------------------------------------------------------------
97 cpPlugins::DataObjects::BoundingBox::
98 ~BoundingBox( )
99 {
100 }
101
102 // -------------------------------------------------------------------------
103 void cpPlugins::DataObjects::BoundingBox::
104 _UpdateVTK( )
105 {
106   // Get bounds
107   double bounds[ 6 ] = { 0 };
108   unsigned int dim = this->m_Points[ 0 ].size( );
109   dim = ( this->m_Points[ 1 ].size( ) < dim )? this->m_Points[ 1 ].size( ): dim;
110   dim = ( dim < 3 )? dim: 3;
111   for( unsigned int d = 0; d < dim; ++d )
112   {
113     bounds[ d << 1 ] = this->m_Points[ 0 ][ d ];
114     bounds[ ( d << 1 ) + 1 ] = this->m_Points[ 1 ][ d ];
115
116   } // rof
117
118   // Update vtk objects
119   this->m_Outline->SetBounds( bounds );
120   this->m_Outline->Update( );
121   this->m_VTK = this->m_Outline->GetOutput( );
122 }
123
124 // eof - $RCSfile$