VTK
vtkLabelHierarchyPrivate.h
Go to the documentation of this file.
1 #ifndef __vtkLabelHierarchyPrivate_h
2 #define __vtkLabelHierarchyPrivate_h
3 
4 #include "vtkObject.h" // for vtkstd
5 #include <vtkstd/set>
6 
7 #include "octree/octree"
8 
9 //----------------------------------------------------------------------------
10 // vtkLabelHierarchy::Implementation
11 
13 {
14 public:
16  {
17  this->Hierarchy2 = 0;
18  this->Hierarchy3 = 0;
19  this->ActualDepth = 5;
20  this->Z2 = 0.;
21  }
22 
24  {
25  if ( this->Hierarchy2 )
26  {
27  delete this->Hierarchy2;
28  }
29  if ( this->Hierarchy3 )
30  {
31  delete this->Hierarchy3;
32  }
33 
34  }
35 
37  {
38  vtkDataArray* priorities = this->Husk->GetPriorities();
39  return priorities ?
40  priorities->GetTuple1( a ) > priorities->GetTuple1( b ) :
41  a < b;
42  }
43 
45  {
47 
49  {
50  // See comment near declaration of Current for more info:
52  }
53 
55  {
56  this->Hierarchy = h;
57  }
58 
60  {
61  this->Hierarchy = src.Hierarchy;
62  }
63 
65  {
66  if (this != &rhs)
67  {
68  this->Hierarchy = rhs.Hierarchy;
69  }
70  return *this;
71  }
72 
74  {
75  }
76 
77  bool operator () ( const vtkIdType& a, const vtkIdType& b )
78  {
79  if (0 == this->Hierarchy)
80  {
81  vtkGenericWarningMacro( "error: NULL this->Hierarchy in PriorityComparator" );
82  return a < b;
83  }
84 
85  if (0 == this->Hierarchy->GetImplementation())
86  {
87  vtkGenericWarningMacro( "error: NULL this->Hierarchy->GetImplementation() in PriorityComparator" );
88  return a < b;
89  }
90 
91  return this->Hierarchy->GetImplementation()->ComparePriorities( a, b );
92  }
93  };
94 
95  class LabelSet : public vtkstd::multiset<vtkIdType,PriorityComparator>
96  {
97  public:
99  : vtkstd::multiset<vtkIdType,PriorityComparator>( PriorityComparator(hierarchy) )
100  {
101  this->TotalAnchors = 0;
102  this->Size = 1.;
103  for ( int i = 0; i < 3; ++ i )
104  {
105  this->Center[i] = 0.;
106  }
107  }
108 
109  LabelSet( const LabelSet& src )
110  : vtkstd::multiset<vtkIdType,PriorityComparator>( src )
111  {
112  this->TotalAnchors = src.TotalAnchors;
113  this->Size = src.Size;
114  for ( int i = 0; i < 3; ++ i )
115  {
116  this->Center[i] = src.Center[i];
117  }
118  }
119 
121  : vtkstd::multiset<vtkIdType,PriorityComparator>()
122  {
123  this->TotalAnchors = 0;
124  this->Size = 1.;
125  for ( int i = 0; i < 3; ++ i )
126  {
127  this->Center[i] = 0.;
128  }
129  }
130 
132  {
133  if ( this != &rhs )
134  {
135 #if ! ( defined(_MSC_VER) && (_MSC_VER < 1300) )
137 #endif
138  this->TotalAnchors = rhs.TotalAnchors;
139  this->Size = rhs.Size;
140  for ( int i = 0; i < 3; ++ i )
141  {
142  this->Center[i] = rhs.Center[i];
143  }
144  }
145  return *this;
146  }
147  const double* GetCenter() const { return this->Center; }
148  double GetSize() const { return this->Size; }
149  void SetGeometry( const double center[3], double length );
150  void SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self );
151  void SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self );
152  void AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode );
153  void AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode );
154  void Insert( vtkIdType anchor )
155  {
156  this->insert( anchor );
157  ++ this->TotalAnchors;
158  }
159  void Increment() { ++ this->TotalAnchors; }
160  vtkIdType GetLocalAnchorCount() const { return this->size(); }
161  vtkIdType GetTotalAnchorCount() const { return this->TotalAnchors; }
162 
163  vtkIdType TotalAnchors; // Count of all anchors stored in this node and its children.
164  double Center[3]; // Geometric coordinates of this node's center.
165  double Size; // Length of each edge of this node.
166  };
167 
168  typedef octree<LabelSet,2> HierarchyType2;
169  typedef octree<LabelSet,2>::cursor HierarchyCursor2;
170  typedef octree<LabelSet,2>::iterator HierarchyIterator2;
171 
172  typedef octree<LabelSet> HierarchyType3;
173  typedef octree<LabelSet>::cursor HierarchyCursor3;
174  typedef octree<LabelSet>::iterator HierarchyIterator3;
175 
176  //typedef vtkstd::map<Coord,vtkstd::pair<int,vtkstd::set<vtkIdType> > >::iterator MapCoordIter;
177 
178  // Description:
179  // Computes the depth of the generated hierarchy.
180  //void ComputeActualDepth();
181 
182  // Description:
183  // Routines called by ComputeHierarchy()
184  void BinAnchorsToLevel( int level );
185  void PromoteAnchors();
186  void DemoteAnchors( int level );
187  void RecursiveNodeDivide( HierarchyCursor2& cursor );
188  void RecursiveNodeDivide( HierarchyCursor3& cursor );
189 
190  // Description:
191  // Routines called by ComputeHierarchy()
192  void PrepareSortedAnchors( LabelSet& anchors );
193  void FillHierarchyRoot( LabelSet& anchors );
194  void DropAnchor2( vtkIdType anchor );
195  void DropAnchor3( vtkIdType anchor );
196  void SmudgeAnchor2( HierarchyCursor2& cursor, vtkIdType anchor, double* x );
197  void SmudgeAnchor3( HierarchyCursor3& cursor, vtkIdType anchor, double* x );
198 
199  double Z2; // common z-coordinate of all label anchors when quadtree (Hierarchy2) is used.
200  HierarchyType2* Hierarchy2; // 2-D quadtree of label anchors (all input points have same z coord)
201  HierarchyType3* Hierarchy3; // 3-D octree of label anchors (input point bounds have non-zero z range)
203  HierarchyType3::size_type ActualDepth;
205 
207 };
208 
210 {
211  for ( int i = 0; i < 3; ++ i )
212  {
213  this->Center[i] = center[i];
214  }
215  this->Size = length;
216 }
217 
218 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,2>::octree_node_pointer self )
219 {
220  double sz2 = this->Size / 2.;
221  double x[3];
222  for ( int i = 0; i < self->num_children(); ++ i )
223  {
224  for ( int j = 0; j < 2; ++ j )
225  {
226  x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
227  }
228  x[2] = this->Center[2];
229  (*self)[i].value().SetGeometry( x, sz2 );
230  }
231 }
232 
233 inline void vtkLabelHierarchy::Implementation::LabelSet::SetChildGeometry( octree<LabelSet,3>::octree_node_pointer self )
234 {
235  double sz2 = this->Size / 2.;
236  double x[3];
237  for ( int i = 0; i < self->num_children(); ++ i )
238  {
239  for ( int j = 0; j < 3; ++ j )
240  {
241  x[j] = this->Center[j] + ( ( i & (1<<j) ) ? 0.5 : -0.5 ) * sz2 ;
242  }
243  (*self)[i].value().SetGeometry( x, sz2 );
244  }
245 }
246 
247 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,2>::octree_node_pointer self, LabelSet& emptyNode )
248 {
249  self->add_children( emptyNode );
250  this->SetChildGeometry( self );
251 }
252 
253 inline void vtkLabelHierarchy::Implementation::LabelSet::AddChildren( octree<LabelSet,3>::octree_node_pointer self, LabelSet& emptyNode )
254 {
255  self->add_children( emptyNode );
256  this->SetChildGeometry( self );
257 }
258 
259 #endif // __vtkLabelHierarchyPrivate_h