VTK
dox/Rendering/Label/vtkLabelHierarchyPrivate.h
Go to the documentation of this file.
00001 #ifndef __vtkLabelHierarchyPrivate_h
00002 #define __vtkLabelHierarchyPrivate_h
00003 
00004 #include "vtkObject.h" // for vtkstd
00005 #include <set>
00006 
00007 #include "octree/octree"
00008 
00009 //----------------------------------------------------------------------------
00010 // vtkLabelHierarchy::Implementation
00011 
00012 class vtkLabelHierarchy::Implementation
00013 {
00014 public:
00015   Implementation()
00016     {
00017     this->Hierarchy2 = 0;
00018     this->Hierarchy3 = 0;
00019     this->ActualDepth = 5;
00020     this->Z2 = 0.;
00021     }
00022 
00023   ~Implementation()
00024     {
00025     if ( this->Hierarchy2 )
00026       {
00027       delete this->Hierarchy2;
00028       }
00029     if ( this->Hierarchy3 )
00030       {
00031       delete this->Hierarchy3;
00032       }
00033 
00034     }
00035 
00036   bool ComparePriorities( vtkIdType a, vtkIdType b )
00037     {
00038     vtkDataArray* priorities = this->Husk->GetPriorities();
00039     return priorities ?
00040       priorities->GetTuple1( a ) > priorities->GetTuple1( b ) :
00041       a < b;
00042     }
00043 
00044   struct PriorityComparator
00045   {
00046     vtkLabelHierarchy* Hierarchy;
00047 
00048     PriorityComparator()
00049       {
00050       // See comment near declaration of Current for more info:
00051       this->Hierarchy = vtkLabelHierarchy::Implementation::Current;
00052       }
00053 
00054     PriorityComparator( vtkLabelHierarchy* h )
00055       {
00056       this->Hierarchy = h;
00057       }
00058 
00059     PriorityComparator( const PriorityComparator& src )
00060       {
00061       this->Hierarchy = src.Hierarchy;
00062       }
00063 
00064     PriorityComparator& operator=(const PriorityComparator& rhs)
00065       {
00066       if (this != &rhs)
00067         {
00068         this->Hierarchy = rhs.Hierarchy;
00069         }
00070       return *this;
00071       }
00072 
00073     ~PriorityComparator()
00074       {
00075       }
00076 
00077     bool operator () ( const vtkIdType& a, const vtkIdType& b )
00078       {
00079       if (0 == this->Hierarchy)
00080         {
00081         vtkGenericWarningMacro( "error: NULL this->Hierarchy in PriorityComparator" );
00082         return a < b;
00083         }
00084 
00085       if (0 == this->Hierarchy->GetImplementation())
00086         {
00087         vtkGenericWarningMacro( "error: NULL this->Hierarchy->GetImplementation() in PriorityComparator" );
00088         return a < b;
00089         }
00090 
00091       return this->Hierarchy->GetImplementation()->ComparePriorities( a, b );
00092       }
00093   };
00094 
00095   class LabelSet : public std::multiset<vtkIdType,PriorityComparator>
00096   {
00097   public:
00098     LabelSet( vtkLabelHierarchy* hierarchy )
00099       : std::multiset<vtkIdType,PriorityComparator>( PriorityComparator(hierarchy) )
00100       {
00101       this->TotalAnchors = 0;
00102       this->Size = 1.;
00103       for ( int i = 0; i < 3; ++ i )
00104         {
00105         this->Center[i] = 0.;
00106         }
00107       }
00108 
00109     LabelSet( const LabelSet& src )
00110       : std::multiset<vtkIdType,PriorityComparator>( src )
00111       {
00112       this->TotalAnchors = src.TotalAnchors;
00113       this->Size = src.Size;
00114       for ( int i = 0; i < 3; ++ i )
00115         {
00116         this->Center[i] = src.Center[i];
00117         }
00118       }
00119 
00120     LabelSet()
00121       : std::multiset<vtkIdType,PriorityComparator>()
00122       {
00123       this->TotalAnchors = 0;
00124       this->Size = 1.;
00125       for ( int i = 0; i < 3; ++ i )
00126         {
00127         this->Center[i] = 0.;
00128         }
00129       }
00130 
00131     LabelSet& operator = ( const LabelSet& rhs )
00132       {
00133       if ( this != &rhs )
00134         {
00135         std::multiset<vtkIdType,PriorityComparator>::operator = ( rhs );
00136         this->TotalAnchors = rhs.TotalAnchors;
00137         this->Size = rhs.Size;
00138         for ( int i = 0; i < 3; ++ i )
00139           {
00140           this->Center[i] = rhs.Center[i];
00141           }
00142         }
00143       return *this;
00144       }
00145     const double* GetCenter() const { return this->Center; }
00146     double GetSize() const { return this->Size; }
00147     void SetGeometry( const double center[3], double length );
00148     void SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self );
00149     void SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self );
00150     void AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode );
00151     void AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode );
00152     void Insert( vtkIdType anchor )
00153       {
00154       this->insert( anchor );
00155       ++ this->TotalAnchors;
00156       }
00157     void Increment() { ++ this->TotalAnchors; }
00158     vtkIdType GetLocalAnchorCount() const { return this->size(); }
00159     vtkIdType GetTotalAnchorCount() const { return this->TotalAnchors; }
00160 
00161     vtkIdType TotalAnchors; // Count of all anchors stored in this node and its children.
00162     double Center[3]; // Geometric coordinates of this node's center.
00163     double Size; // Length of each edge of this node.
00164   };
00165 
00166   typedef octree<LabelSet,2> HierarchyType2;
00167   typedef octree<LabelSet,2>::cursor HierarchyCursor2;
00168   typedef octree<LabelSet,2>::iterator HierarchyIterator2;
00169 
00170   typedef octree<LabelSet> HierarchyType3;
00171   typedef octree<LabelSet>::cursor HierarchyCursor3;
00172   typedef octree<LabelSet>::iterator HierarchyIterator3;
00173 
00174   //typedef std::map<Coord,std::pair<int,std::set<vtkIdType> > >::iterator MapCoordIter;
00175 
00176   // Description:
00177   // Computes the depth of the generated hierarchy.
00178   //void ComputeActualDepth();
00179 
00180   // Description:
00181   // Routines called by ComputeHierarchy()
00182   void BinAnchorsToLevel( int level );
00183   void PromoteAnchors();
00184   void DemoteAnchors( int level );
00185   void RecursiveNodeDivide( HierarchyCursor2& cursor );
00186   void RecursiveNodeDivide( HierarchyCursor3& cursor );
00187 
00188   // Description:
00189   // Routines called by ComputeHierarchy()
00190   void PrepareSortedAnchors( LabelSet& anchors );
00191   void FillHierarchyRoot( LabelSet& anchors );
00192   void DropAnchor2( vtkIdType anchor );
00193   void DropAnchor3( vtkIdType anchor );
00194   void SmudgeAnchor2( HierarchyCursor2& cursor, vtkIdType anchor, double* x );
00195   void SmudgeAnchor3( HierarchyCursor3& cursor, vtkIdType anchor, double* x );
00196 
00197   double Z2; // common z-coordinate of all label anchors when quadtree (Hierarchy2) is used.
00198   HierarchyType2* Hierarchy2; // 2-D quadtree of label anchors (all input points have same z coord)
00199   HierarchyType3* Hierarchy3; // 3-D octree of label anchors (input point bounds have non-zero z range)
00200   vtkTimeStamp HierarchyTime;
00201   HierarchyType3::size_type ActualDepth;
00202   vtkLabelHierarchy* Husk;
00203 
00204   static vtkLabelHierarchy* Current;
00205 };
00206 
00207 inline void vtkLabelHierarchy::Implementation::LabelSet::SetGeometry( const double center[3], double length )
00208 {
00209   for ( int i = 0; i < 3; ++ i )
00210     {
00211     this->Center[i] = center[i];
00212     }
00213   this->Size = length;
00214 }
00215 
00216 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self )
00217 {
00218   double sz2 = this->Size / 2.;
00219   double x[3];
00220   for ( int i = 0; i < self->num_children(); ++ i )
00221     {
00222     for ( int j = 0; j < 2; ++ j )
00223       {
00224       x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
00225       }
00226     x[2] = this->Center[2];
00227     (*self)[i].value().SetGeometry( x, sz2 );
00228     }
00229 }
00230 
00231 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self )
00232 {
00233   double sz2 = this->Size / 2.;
00234   double x[3];
00235   for ( int i = 0; i < self->num_children(); ++ i )
00236     {
00237     for ( int j = 0; j < 3; ++ j )
00238       {
00239       x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
00240       }
00241     (*self)[i].value().SetGeometry( x, sz2 );
00242     }
00243 }
00244 
00245 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode )
00246 {
00247   self->add_children( emptyNode );
00248   this->SetChildGeometry( self );
00249 }
00250 
00251 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode )
00252 {
00253   self->add_children( emptyNode );
00254   this->SetChildGeometry( self );
00255 }
00256 
00257 #endif // __vtkLabelHierarchyPrivate_h
00258 // VTK-HeaderTest-Exclude: vtkLabelHierarchyPrivate.h