OpenWalnut  1.5.0dev
WHierarchicalTree.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WHIERARCHICALTREE_H
26 #define WHIERARCHICALTREE_H
27 
28 #include <list>
29 #include <memory>
30 #include <queue>
31 #include <utility>
32 #include <vector>
33 
34 
35 #include "WColor.h"
36 
37 
38 /**
39  * base class for hierarchical tree implementations
40  */
41 class WHierarchicalTree // NOLINT
42 {
43 public:
44  /**
45  * standard constructor
46  */
48 
49  /**
50  * destructor
51  */
52  virtual ~WHierarchicalTree();
53 
54  /**
55  * A leaf is at the very bottom of the tree, it represents a single fiber or voxel, for several purposes
56  * a leaf also counts as a cluster
57  */
58  virtual void addLeaf() = 0;
59 
60  /**
61  * getter
62  * \return the number of leafes
63  */
64  size_t getLeafCount() const;
65 
66  /**
67  * getter
68  * \return the number of clusters
69  */
70  size_t getClusterCount() const;
71 
72  /**
73  * getter
74  * \return maxlevel, i.e. the level of the root cluster
75  */
76  size_t getMaxLevel() const;
77 
78  /**
79  * getter
80  * \param cluster
81  * \return the level of the selected cluster
82  */
83  size_t getLevel( size_t cluster ) const;
84 
85  /**
86  * getter
87  * \param cluster the cluster in question
88  * \return the parent for the selected cluster
89  */
90  size_t getParent( size_t cluster ) const;
91 
92  /**
93  * getter
94  * \param cluster
95  * \return the custom data for the selected cluster
96  */
97  float getCustomData( size_t cluster ) const;
98 
99  /**
100  * setter sets the color for a single cluster
101  * \param color
102  * \param cluster
103  */
104  void setColor( WColor color, size_t cluster );
105 
106  /**
107  * getter
108  * \param cluster
109  * \return the color for the selected cluster
110  */
111  WColor getColor( size_t cluster ) const;
112 
113  /**
114  * getter
115  * \param cluster
116  * \return children for the selected cluster
117  */
118  std::pair<size_t, size_t>getChildren( size_t cluster ) const;
119 
120  /**
121  * getter
122  * \param cluster
123  * \return the leafes contained in the selected cluster
124  */
125  std::vector<size_t>getLeafesForCluster( size_t cluster ) const;
126 
127  /**
128  * getter
129  * \param cluster
130  * \return number of leafes for the selected cluster
131  */
132  size_t size( size_t cluster ) const;
133 
134  /**
135  * checks if a cluster is a leaf or a cluster
136  * \param cluster
137  * \return true if it is a leaf
138  */
139  bool isLeaf( size_t cluster ) const;
140 
141  /**
142  * returns a number of clusters at a certain level down from top cluster
143  * \param level how many levels to go down
144  * \param hideOutliers true if clusters of size 1 should be ignored
145  * \return vector containing the cluster id's
146  */
147  std::vector< size_t >downXLevelsFromTop( size_t level, bool hideOutliers = false ) const;
148 
149  /**
150  * finds the X biggest clusters for a given cluster
151  * \param cluster
152  * \param number of sub clusters
153  *
154  * \return the biggest clusters
155  */
156  std::vector< size_t >findXBiggestClusters( size_t cluster, size_t number = 10 ) const;
157 
158  /**
159  * sets the color for a selected cluster and all sub clusters
160  * \param cluster
161  * \param color
162  */
163  void colorCluster( size_t cluster, WColor color );
164 
165 
166 protected:
167  /**
168  * overall number of cluster, counts both leafes ( clusters of size one ) and real clusters
169  */
171 
172  /**
173  * number of leaf nodes
174  */
175  size_t m_leafCount;
176 
177  /**
178  * the maximum level, naturally the level of the root node
179  */
180  size_t m_maxLevel;
181 
182  /**
183  * to enforce valid datastructures there will be no leaf with an id higher than a cluster, thus when
184  * the first cluster is inserted, no leafes may be added
185  */
187 
188  /**
189  * vector that stores the level of each cluster, the level is the maximum of the levels of the child clusters +1
190  */
191  std::vector<size_t>m_level;
192 
193  /**
194  * vector that stores the parent cluster for each cluster
195  */
196  std::vector<size_t>m_parents;
197 
198  /**
199  * vector that stores the 2 children of each cluster, contains an empty pair for leafes
200  */
201  std::vector< std::pair< size_t, size_t> >m_children;
202 
203  /**
204  * custom data for each cluster, this may some energy or similarity level generated by the clustering algorithm
205  * or something completely different
206  */
207  std::vector<float>m_customData;
208 
209  /**
210  * a color value for each cluster
211  */
212  std::vector<WColor>m_colors;
213 
214  /**
215  *vector that stores the leaf id's for each cluster, this is quite memory intensive but speeds up selection
216  * of leafes for nodes at higher levels
217  */
218  std::vector< std::vector<size_t> >m_containsLeafes;
219 
220 private:
221 };
222 
223 inline size_t WHierarchicalTree::getLeafCount() const
224 {
225  return m_leafCount;
226 }
227 
229 {
230  return m_clusterCount;
231 }
232 
233 inline size_t WHierarchicalTree::getMaxLevel() const
234 {
235  return m_maxLevel;
236 }
237 
238 inline size_t WHierarchicalTree::getLevel( size_t cluster ) const
239 {
240  return m_level[cluster];
241 }
242 
243 inline size_t WHierarchicalTree::getParent( size_t cluster ) const
244 {
245  if( m_level[cluster] < m_maxLevel )
246  {
247  return m_parents[cluster];
248  }
249  else
250  {
251  // this is just to quiet the compiler, this branch should never be reached
252  return cluster;
253  }
254 }
255 
256 inline std::pair<size_t, size_t>WHierarchicalTree::getChildren( size_t cluster ) const
257 {
258  if( m_level[cluster] > 0 )
259  {
260  return m_children[cluster];
261  }
262  else
263  {
264  // this is just to quiet the compiler, this branch should never be reached
265  return std::pair<size_t, size_t>( cluster, cluster );
266  }
267 }
268 
269 inline float WHierarchicalTree::getCustomData( size_t cluster ) const
270 {
271  return m_customData[cluster];
272 }
273 
274 inline size_t WHierarchicalTree::size( size_t cluster ) const
275 {
276  return getLeafesForCluster( cluster ).size();
277 }
278 
279 inline void WHierarchicalTree::setColor( WColor color, size_t cluster )
280 {
281  m_colors[cluster] = color;
282 }
283 
284 inline WColor WHierarchicalTree::getColor( size_t cluster ) const
285 {
286  return m_colors[cluster];
287 }
288 
289 inline bool WHierarchicalTree::isLeaf( size_t cluster ) const
290 {
291  return ( cluster < m_leafCount ) ? true : false;
292 }
293 
294 inline std::vector<size_t>WHierarchicalTree::getLeafesForCluster( size_t cluster ) const
295 {
296  return m_containsLeafes[cluster];
297 }
298 /**
299  * implements a compare operator for clusters, depending on leaf count
300  */
301 struct compSize
302 {
303  const WHierarchicalTree* m_tree; //!< stores pointer to tree we work on
304 
305  /**
306  * constructor
307  * \param tree
308  */
309  explicit compSize( const WHierarchicalTree* tree ) :
310  m_tree( tree )
311  {
312  }
313 
314  /**
315  * compares two clusters
316  * \param lhs
317  * \param rhs
318  * \return bool
319  */
320  bool operator()( const size_t lhs, const size_t rhs ) const //NOLINT
321  {
322  return m_tree->size( lhs ) > m_tree->size( rhs ); //NOLINT
323  }
324 };
325 /**
326  * implements a compare operator for clusters, depending on custom value of the cluster
327  */
328 struct compValue
329 {
330  WHierarchicalTree* m_tree; //!< stores pointer to tree we work on
331 
332  /**
333  * constructor
334  * \param tree
335  */
336  explicit compValue( WHierarchicalTree* tree ) :
337  m_tree( tree )
338  {
339  }
340  /**
341  * compares two clusters
342  * \param lhs
343  * \param rhs
344  * \return bool
345  */
346  bool operator()( const size_t lhs, const size_t rhs ) const
347  {
348  return m_tree->getCustomData( lhs ) > m_tree->getCustomData( rhs );
349  }
350 };
351 
352 #endif // WHIERARCHICALTREE_H
base class for hierarchical tree implementations
std::vector< float > m_customData
custom data for each cluster, this may some energy or similarity level generated by the clustering al...
size_t size(size_t cluster) const
getter
std::vector< WColor > m_colors
a color value for each cluster
bool isLeaf(size_t cluster) const
checks if a cluster is a leaf or a cluster
size_t m_clusterCount
overall number of cluster, counts both leafes ( clusters of size one ) and real clusters
std::vector< std::pair< size_t, size_t > > m_children
vector that stores the 2 children of each cluster, contains an empty pair for leafes
std::pair< size_t, size_t > getChildren(size_t cluster) const
getter
WColor getColor(size_t cluster) const
getter
size_t getParent(size_t cluster) const
getter
std::vector< size_t > m_parents
vector that stores the parent cluster for each cluster
std::vector< size_t > downXLevelsFromTop(size_t level, bool hideOutliers=false) const
returns a number of clusters at a certain level down from top cluster
void colorCluster(size_t cluster, WColor color)
sets the color for a selected cluster and all sub clusters
float getCustomData(size_t cluster) const
getter
size_t m_leafCount
number of leaf nodes
size_t m_maxLevel
the maximum level, naturally the level of the root node
size_t getLevel(size_t cluster) const
getter
std::vector< size_t > getLeafesForCluster(size_t cluster) const
getter
virtual void addLeaf()=0
A leaf is at the very bottom of the tree, it represents a single fiber or voxel, for several purposes...
std::vector< size_t > findXBiggestClusters(size_t cluster, size_t number=10) const
finds the X biggest clusters for a given cluster
std::vector< size_t > m_level
vector that stores the level of each cluster, the level is the maximum of the levels of the child clu...
size_t getLeafCount() const
getter
virtual ~WHierarchicalTree()
destructor
bool m_leafesLocked
to enforce valid datastructures there will be no leaf with an id higher than a cluster,...
std::vector< std::vector< size_t > > m_containsLeafes
vector that stores the leaf id's for each cluster, this is quite memory intensive but speeds up selec...
size_t getClusterCount() const
getter
void setColor(WColor color, size_t cluster)
setter sets the color for a single cluster
size_t getMaxLevel() const
getter
WHierarchicalTree()
standard constructor
implements a compare operator for clusters, depending on leaf count
bool operator()(const size_t lhs, const size_t rhs) const
compares two clusters
compSize(const WHierarchicalTree *tree)
constructor
const WHierarchicalTree * m_tree
stores pointer to tree we work on
implements a compare operator for clusters, depending on custom value of the cluster
compValue(WHierarchicalTree *tree)
constructor
bool operator()(const size_t lhs, const size_t rhs) const
compares two clusters
WHierarchicalTree * m_tree
stores pointer to tree we work on