OpenWalnut  1.5.0dev
WHierarchicalTreeVoxels.cpp
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 #include <algorithm>
26 #include <iostream>
27 #include <utility>
28 #include <vector>
29 #include <queue>
30 #include <list>
31 
32 #include "WHierarchicalTreeVoxels.h"
33 
36 {
37 }
38 
40 {
41 }
42 
44 {
45 }
46 
47 void WHierarchicalTreeVoxels::addLeaf( size_t voxelnum )
48 {
49  // after a cluster was added no more leafes may be inserted
50  if( m_leafesLocked )
51  {
52  return;
53  }
54  m_level.push_back( 0 );
55  m_parents.push_back( m_clusterCount );
56  std::vector<size_t> tmp( 1, m_clusterCount );
57  m_containsLeafes.push_back( tmp );
58  std::pair<size_t, size_t>tmp2;
59  m_children.push_back( tmp2 );
60  m_customData.push_back( 0.0 );
61  m_colors.push_back( WColor( 0.3, 0.3, 0.3, 1.0 ) );
62  m_voxelnums.push_back( voxelnum );
63 
64  ++m_leafCount;
66 }
67 
68 void WHierarchicalTreeVoxels::addCluster( size_t cluster1, size_t cluster2, float customData )
69 {
70  m_leafesLocked = true;
71 
72  size_t level = std::max( m_level[cluster1], m_level[cluster2] ) + 1;
73  m_level.push_back( level );
74 
75  m_maxLevel = std::max( m_maxLevel, level );
76 
77  m_parents.push_back( m_clusterCount );
78  m_customData.push_back( customData );
79  m_colors.push_back( WColor( 0.3, 0.3, 0.3, 1.0 ) );
80 
81  std::pair<size_t, size_t>childs( cluster1, cluster2 );
82  m_children.push_back( childs );
83 
84  std::vector<size_t>leafes;
85  leafes.reserve( m_containsLeafes[cluster1].size() + m_containsLeafes[cluster2].size() );
86  leafes.insert( leafes.end(), m_containsLeafes[cluster1].begin(), m_containsLeafes[cluster1].end() );
87  leafes.insert( leafes.end(), m_containsLeafes[cluster2].begin(), m_containsLeafes[cluster2].end() );
88  m_containsLeafes.push_back( leafes );
89 
90  m_parents[cluster1] = m_clusterCount;
91  m_parents[cluster2] = m_clusterCount;
92 
94 }
95 
96 std::vector<size_t> WHierarchicalTreeVoxels::getVoxelsForCluster( size_t cluster )
97 {
98  std::vector<size_t>returnVec = getLeafesForCluster( cluster );
99 
100  for( size_t i = 0; i < returnVec.size(); ++i )
101  {
102  returnVec[i] = m_voxelnums[returnVec[i]];
103  }
104  return returnVec;
105 }
106 
107 std::vector< size_t > WHierarchicalTreeVoxels::findClustersForValue( float value )
108 {
109  // init
110  std::list<size_t>worklist;
111  std::vector<size_t>returnVector;
112 
113  if( value < getCustomData( m_clusterCount - 1 ) )
114  {
115  worklist.push_back( m_clusterCount - 1 );
116  }
117 
118  while( !worklist.empty() )
119  {
120  size_t current = worklist.front();
121  worklist.pop_front();
122  if( m_containsLeafes[current].size() > 1 )
123  {
124  size_t left = m_children[current].first;
125  size_t right = m_children[current].second;
126 
127  if( value < getCustomData( left ) )
128  {
129  worklist.push_back( left );
130  }
131  else
132  {
133  returnVector.push_back( left );
134  }
135  if( value < getCustomData( right ) )
136  {
137  worklist.push_back( right );
138  }
139  else
140  {
141  returnVector.push_back( right );
142  }
143  }
144  }
145  return returnVector;
146 }
147 
148 std::vector< size_t >WHierarchicalTreeVoxels::findClustersForBranchLength( float value, size_t minSize )
149 {
150  // init
151  std::list<size_t>worklist;
152  std::vector<size_t>returnVector;
153 
154  std::vector<int>distanceCheckVector( m_clusterCount, 0 );
155 
156  for( size_t i = m_leafCount; i < m_clusterCount; ++i )
157  {
158  if( ( distanceCheckVector[m_children[i].first] > 0 ) || ( distanceCheckVector[m_children[i].second] > 0 ) )
159  {
160  distanceCheckVector[i] = 2;
161  }
162  else if( ( ( m_containsLeafes[i].size() >= minSize ) && ( ( m_customData[m_parents[i]] - m_customData[i] ) > value ) ) )
163  {
164  distanceCheckVector[i] = 1;
165  returnVector.push_back( i );
166  }
167  }
168  return returnVector;
169 }
170 
171 std::vector< size_t >WHierarchicalTreeVoxels::findXClusters( size_t root, size_t number )
172 {
173  std::vector<size_t>returnVector;
174 
175  std::list<size_t>worklist;
176  worklist.push_back( root );
177 
178  while( worklist.size() < number )
179  {
180  size_t current = worklist.front();
181  worklist.pop_front();
182 
183  size_t left = m_children[current].first;
184  size_t right = m_children[current].second;
185 
186  worklist.push_back( left );
187  worklist.push_back( right );
188 
189  worklist.sort( compValue( this ) );
190  }
191  std::cout << number << " clusters at " << m_customData[worklist.front()] << " energy" << std::endl;
192 
193  std::list<size_t>::iterator it;
194  for( it = worklist.begin(); it != worklist.end(); ++it )
195  {
196  size_t current = *it;
197  returnVector.push_back( current );
198  }
199 
200  return returnVector;
201 }
std::vector< size_t > getVoxelsForCluster(size_t cluster)
getter
std::vector< size_t > findXClusters(size_t root, size_t number)
finds a number of clusters, the algorithm travers down the tree and will always split the cluster wit...
std::vector< size_t > findClustersForBranchLength(float value, size_t minSize=100)
finds the clusters for a given similarity value
void addLeaf()
A leaf is at the very bottom of the tree, it represents a single fiber or voxel, for several purposes...
std::vector< size_t > m_voxelnums
stores the voxel id of each leaf node
WHierarchicalTreeVoxels()
standard constructor
std::vector< size_t > findClustersForValue(float value)
finds the clusters for a given similarity value
void addCluster(size_t cluster1, size_t cluster2, float customData)
adds a cluster to the set, it combines 2 already existing clusters
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
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::vector< size_t > m_parents
vector that stores the parent cluster for each cluster
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
std::vector< size_t > getLeafesForCluster(size_t cluster) const
getter
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...
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...
implements a compare operator for clusters, depending on custom value of the cluster