VTK
dox/Rendering/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 #if ! ( defined(_MSC_VER) && (_MSC_VER < 1300) )
00136         std::multiset<vtkIdType,PriorityComparator>::operator = ( rhs );
00137 #endif
00138         this->TotalAnchors = rhs.TotalAnchors;
00139         this->Size = rhs.Size;
00140         for ( int i = 0; i < 3; ++ i )
00141           {
00142           this->Center[i] = rhs.Center[i];
00143           }
00144         }
00145       return *this;
00146       }
00147     const double* GetCenter() const { return this->Center; }
00148     double GetSize() const { return this->Size; }
00149     void SetGeometry( const double center[3], double length );
00150     void SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self );
00151     void SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self );
00152     void AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode );
00153     void AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode );
00154     void Insert( vtkIdType anchor )
00155       {
00156       this->insert( anchor );
00157       ++ this->TotalAnchors;
00158       }
00159     void Increment() { ++ this->TotalAnchors; }
00160     vtkIdType GetLocalAnchorCount() const { return this->size(); }
00161     vtkIdType GetTotalAnchorCount() const { return this->TotalAnchors; }
00162 
00163     vtkIdType TotalAnchors; // Count of all anchors stored in this node and its children.
00164     double Center[3]; // Geometric coordinates of this node's center.
00165     double Size; // Length of each edge of this node.
00166   };
00167 
00168   typedef octree<LabelSet,2> HierarchyType2;
00169   typedef octree<LabelSet,2>::cursor HierarchyCursor2;
00170   typedef octree<LabelSet,2>::iterator HierarchyIterator2;
00171 
00172   typedef octree<LabelSet> HierarchyType3;
00173   typedef octree<LabelSet>::cursor HierarchyCursor3;
00174   typedef octree<LabelSet>::iterator HierarchyIterator3;
00175 
00176   //typedef std::map<Coord,std::pair<int,std::set<vtkIdType> > >::iterator MapCoordIter;
00177 
00178   // Description:
00179   // Computes the depth of the generated hierarchy.
00180   //void ComputeActualDepth();
00181 
00182   // Description:
00183   // Routines called by ComputeHierarchy()
00184   void BinAnchorsToLevel( int level );
00185   void PromoteAnchors();
00186   void DemoteAnchors( int level );
00187   void RecursiveNodeDivide( HierarchyCursor2& cursor );
00188   void RecursiveNodeDivide( HierarchyCursor3& cursor );
00189 
00190   // Description:
00191   // Routines called by ComputeHierarchy()
00192   void PrepareSortedAnchors( LabelSet& anchors );
00193   void FillHierarchyRoot( LabelSet& anchors );
00194   void DropAnchor2( vtkIdType anchor );
00195   void DropAnchor3( vtkIdType anchor );
00196   void SmudgeAnchor2( HierarchyCursor2& cursor, vtkIdType anchor, double* x );
00197   void SmudgeAnchor3( HierarchyCursor3& cursor, vtkIdType anchor, double* x );
00198 
00199   double Z2; // common z-coordinate of all label anchors when quadtree (Hierarchy2) is used.
00200   HierarchyType2* Hierarchy2; // 2-D quadtree of label anchors (all input points have same z coord)
00201   HierarchyType3* Hierarchy3; // 3-D octree of label anchors (input point bounds have non-zero z range)
00202   vtkTimeStamp HierarchyTime;
00203   HierarchyType3::size_type ActualDepth;
00204   vtkLabelHierarchy* Husk;
00205 
00206   static vtkLabelHierarchy* Current;
00207 };
00208 
00209 inline void vtkLabelHierarchy::Implementation::LabelSet::SetGeometry( const double center[3], double length )
00210 {
00211   for ( int i = 0; i < 3; ++ i )
00212     {
00213     this->Center[i] = center[i];
00214     }
00215   this->Size = length;
00216 }
00217 
00218 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self )
00219 {
00220   double sz2 = this->Size / 2.;
00221   double x[3];
00222   for ( int i = 0; i < self->num_children(); ++ i )
00223     {
00224     for ( int j = 0; j < 2; ++ j )
00225       {
00226       x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
00227       }
00228     x[2] = this->Center[2];
00229     (*self)[i].value().SetGeometry( x, sz2 );
00230     }
00231 }
00232 
00233 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self )
00234 {
00235   double sz2 = this->Size / 2.;
00236   double x[3];
00237   for ( int i = 0; i < self->num_children(); ++ i )
00238     {
00239     for ( int j = 0; j < 3; ++ j )
00240       {
00241       x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
00242       }
00243     (*self)[i].value().SetGeometry( x, sz2 );
00244     }
00245 }
00246 
00247 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode )
00248 {
00249   self->add_children( emptyNode );
00250   this->SetChildGeometry( self );
00251 }
00252 
00253 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode )
00254 {
00255   self->add_children( emptyNode );
00256   this->SetChildGeometry( self );
00257 }
00258 
00259 #endif // __vtkLabelHierarchyPrivate_h