Commit 0409804b authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[MERGE]

parents a9a1abb7 fc1d5788
......@@ -36,24 +36,288 @@ WHierarchicalTree::~WHierarchicalTree()
{
}
void WHierarchicalTree::addCluster( size_t cluster1, size_t cluster2, float customData )
std::vector< size_t > WHierarchicalTree::findXBiggestClusters( size_t cluster, size_t number )
{
m_leafesLocked = true;
//std::cout << number << " largest clusters for cluster: " << cluster << std::endl;
size_t level = std::max( m_level[cluster1], m_level[cluster1] ) + 1;
m_level.push_back( level );
if ( number > m_containsLeafes[cluster].size() )
{
number = m_containsLeafes[cluster].size();
}
m_maxLevel = std::max( m_maxLevel, level );
// init
std::list<size_t>worklist;
worklist.push_back( cluster );
m_parents.push_back( m_clusterCount );
m_customData.push_back( customData );
m_colors.push_back( WColor( 0.3, 0.3, 0.3, 1.0 ) );
while ( worklist.size() < number )
{
size_t current = worklist.front();
worklist.pop_front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
worklist.push_back( left );
worklist.push_back( right );
}
else
{
worklist.push_back( current );
}
}
sortList( worklist );
std::pair<size_t, size_t>childs( cluster1, cluster2 );
m_children.push_back( childs );
bool newSplit = true;
m_parents[cluster1] = m_clusterCount;
m_parents[cluster2] = m_clusterCount;
while ( newSplit )
{
newSplit = false;
size_t current = worklist.front();
++m_clusterCount;
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
size_t last = worklist.back();
if ( m_containsLeafes[left].size() > m_containsLeafes[last].size() )
{
worklist.pop_front();
worklist.push_back( left );
sortList( worklist );
newSplit = true;
}
last = worklist.back();
if ( m_containsLeafes[right].size() > m_containsLeafes[last].size() )
{
if ( !newSplit )
{
worklist.pop_front();
}
if ( worklist.size() == number )
{
worklist.pop_back();
}
worklist.push_back( right );
sortList( worklist );
newSplit = true;
}
}
sortList( worklist );
}
std::vector<size_t>returnVector;
std::list<size_t>::iterator it;
for ( it = worklist.begin(); it != worklist.end(); ++it )
{
size_t current = *it;
//std::cout << "cluster:" << current << " size:" << m_containsLeafes[current].size() << std::endl;
returnVector.push_back( current );
}
return returnVector;
}
std::vector< size_t > WHierarchicalTree::findXBiggestClusters2( size_t cluster, size_t number )
{
//std::cout << number << " largest clusters for cluster: " << cluster << std::endl;
if ( number > m_containsLeafes[cluster].size() )
{
number = m_containsLeafes[cluster].size();
}
// init
std::list<size_t>worklist;
worklist.push_back( cluster );
while ( worklist.size() < number )
{
size_t current = worklist.front();
worklist.pop_front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
worklist.push_back( left );
worklist.push_back( right );
}
else
{
worklist.push_back( current );
}
}
sortList( worklist );
bool newSplit = true;
while ( newSplit )
{
newSplit = false;
size_t current = worklist.front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
size_t last = worklist.back();
if ( ( m_containsLeafes[left].size() > m_containsLeafes[last].size() ) &&
( m_containsLeafes[right].size() > m_containsLeafes[last].size() ) )
{
if ( m_containsLeafes[left].size() > m_containsLeafes[last].size() )
{
worklist.pop_front();
worklist.push_back( left );
sortList( worklist );
newSplit = true;
}
last = worklist.back();
if ( m_containsLeafes[right].size() > m_containsLeafes[last].size() )
{
if ( !newSplit )
{
worklist.pop_front();
}
if ( worklist.size() == number )
{
worklist.pop_back();
}
worklist.push_back( right );
sortList( worklist );
newSplit = true;
}
}
}
sortList( worklist );
}
std::vector<size_t>returnVector;
std::list<size_t>::iterator it;
for ( it = worklist.begin(); it != worklist.end(); ++it )
{
size_t current = *it;
//std::cout << "cluster:" << current << " size:" << m_containsLeafes[current].size() << std::endl;
returnVector.push_back( current );
}
return returnVector;
}
std::vector< size_t > WHierarchicalTree::downXLevelsFromTop( size_t level, bool hideOutliers )
{
if ( level > m_maxLevel )
{
level = m_maxLevel -1;
}
std::vector<size_t>returnVector;
std::list<size_t>worklist;
worklist.push_back( m_clusterCount - 1 );
for ( size_t i = 0; i < level; ++i )
{
std::list<size_t>newChildList;
while ( !worklist.empty() )
{
size_t current = worklist.front();
worklist.pop_front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
if ( hideOutliers )
{
if ( m_containsLeafes[left].size() > 1 )
{
newChildList.push_back( left );
}
if ( m_containsLeafes[right].size() > 1 )
{
newChildList.push_back( right );
}
}
else
{
newChildList.push_back( left );
newChildList.push_back( right );
}
}
}
worklist = newChildList;
}
sortList( worklist );
std::list<size_t>::iterator it;
for ( it = worklist.begin(); it != worklist.end(); ++it )
{
size_t current = *it;
returnVector.push_back( current );
}
return returnVector;
}
void WHierarchicalTree::sortList( std::list<size_t> &input ) //NOLINT
{
if ( input.size() == 0 )
{
return;
}
std::vector<size_t>vec;
std::list<size_t>::iterator it;
for ( it = input.begin(); it != input.end(); ++it )
{
vec.push_back( *it );
}
for ( size_t n = vec.size() - 1; n > 0; --n )
{
for ( size_t i = 0; i < n; ++i )
{
if ( m_containsLeafes[vec[i]].size() < m_containsLeafes[vec[i+1]].size() )
{
size_t tmp = vec[i];
vec[i] = vec[i+1];
vec[i+1] = tmp;
}
}
}
input.clear();
for ( size_t k = 0; k < vec.size(); ++k )
{
input.push_back( vec[k] );
}
}
void WHierarchicalTree::colorCluster( size_t cluster, WColor color )
{
std::list<size_t>worklist;
worklist.push_back( cluster );
while ( !worklist.empty() )
{
size_t current = worklist.front();
worklist.pop_front();
m_colors[current] = color;
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
worklist.push_back( left );
worklist.push_back( right );
}
}
}
......@@ -57,15 +57,6 @@ public:
*/
virtual void addLeaf() = 0;
/**
* adds a cluster to the set, it combines 2 already existing clusters
*
* \param cluster1 first cluster to add
* \param cluster2 second cluster to add
* \param customData some arbitrary data stored with the cluster
*/
void addCluster( size_t cluster1, size_t cluster2, float customData );
/**
* getter
* \return the number of leafes
......@@ -126,19 +117,12 @@ public:
*/
std::pair<size_t, size_t>getChildren( size_t cluster );
/**
* sets the color for a selected cluster and all sub clusters
* \param cluster
* \param color
*/
virtual void colorCluster( size_t cluster, WColor color ) = 0;
/**
* getter
* \param cluster
* \return the leafes contained in the selected cluster
*/
virtual std::vector<size_t>getLeafesForCluster( size_t cluster ) = 0;
std::vector<size_t>getLeafesForCluster( size_t cluster );
/**
* getter
......@@ -154,6 +138,45 @@ public:
*/
bool isLeaf( size_t cluster );
/**
* returns a number of clusters at a certain level down from top cluster
* \param level how many levels to go down
* \param hideOutliers true if clusters of size 1 should be ignored
* \return vector containing the cluster id's
*/
std::vector< size_t >downXLevelsFromTop( size_t level, bool hideOutliers = false );
/**
* finds the X biggest clusters for a given cluster
* \param cluster
* \param number of sub clusters
*/
std::vector< size_t >findXBiggestClusters( size_t cluster, size_t number = 10 );
// TODO(schurade): merge these two function
/**
* finds the X biggest clusters for a given cluster
* \param cluster
* \param number of sub clusters
*/
std::vector< size_t >findXBiggestClusters2( size_t cluster, size_t number = 10 );
/**
* helper function to sort a list of clusters depending on the number of leafes in them
* \param input reference to the list to be sorted
*/
void sortList( std::list<size_t> &input ); //NOLINT
/**
* sets the color for a selected cluster and all sub clusters
* \param cluster
* \param color
*/
void colorCluster( size_t cluster, WColor color );
protected:
/**
* overall number of cluster, counts both leafes ( clusters of size one ) and real clusters
......@@ -201,6 +224,13 @@ protected:
* a color value for each cluster
*/
std::vector<WColor>m_colors;
/**
*vector that stores the leaf id's for each cluster, this is quite memory intensive but speeds up selection
* of leafes for nodes at higher levels
*/
std::vector< std::vector<size_t> >m_containsLeafes;
private:
};
......@@ -275,4 +305,9 @@ inline bool WHierarchicalTree::isLeaf( size_t cluster )
return ( cluster < m_leafCount ) ? true : false;
}
inline std::vector<size_t>WHierarchicalTree::getLeafesForCluster( size_t cluster )
{
return m_containsLeafes[cluster];
}
#endif // WHIERARCHICALTREE_H
......@@ -204,143 +204,3 @@ float WHierarchicalTreeFibers::getRatio( size_t cluster )
}
return static_cast<float>( fibersFromClusterActive ) / static_cast<float>( countFibersInCluster );
}
std::vector< size_t > WHierarchicalTreeFibers::findXBiggestClusters( size_t cluster, size_t number )
{
//std::cout << number << " largest clusters for cluster: " << cluster << std::endl;
if ( number > m_containsLeafes[cluster].size() )
{
number = m_containsLeafes[cluster].size();
}
// init
std::list<size_t>worklist;
worklist.push_back( cluster );
while ( worklist.size() < number )
{
size_t current = worklist.front();
worklist.pop_front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
worklist.push_back( left );
worklist.push_back( right );
}
else
{
worklist.push_back( current );
}
}
sortList( worklist );
bool newSplit = true;
while ( newSplit )
{
newSplit = false;
size_t current = worklist.front();
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
size_t last = worklist.back();
if ( m_containsLeafes[left].size() > m_containsLeafes[last].size() )
{
worklist.pop_front();
worklist.push_back( left );
sortList( worklist );
newSplit = true;
}
last = worklist.back();
if ( m_containsLeafes[right].size() > m_containsLeafes[last].size() )
{
if ( !newSplit )
{
worklist.pop_front();
}
if ( worklist.size() == number )
{
worklist.pop_back();
}
worklist.push_back( right );
sortList( worklist );
newSplit = true;
}
}
sortList( worklist );
}
std::vector<size_t>returnVector;
std::list<size_t>::iterator it;
for ( it = worklist.begin(); it != worklist.end(); ++it )
{
size_t current = *it;
//std::cout << "cluster:" << current << " size:" << m_containsLeafes[current].size() << std::endl;
returnVector.push_back( current );
}
return returnVector;
}
void WHierarchicalTreeFibers::sortList( std::list<size_t> &input ) //NOLINT
{
if ( input.size() == 0 )
{
return;
}
std::vector<size_t>vec;
std::list<size_t>::iterator it;
for ( it = input.begin(); it != input.end(); ++it )
{
vec.push_back( *it );
}
for ( size_t n = vec.size() - 1; n > 0; --n )
{
for ( size_t i = 0; i < n; ++i )
{
if ( m_containsLeafes[vec[i]].size() < m_containsLeafes[vec[i+1]].size() )
{
size_t tmp = vec[i];
vec[i] = vec[i+1];
vec[i+1] = tmp;
}
}
}
input.clear();
for ( size_t k = 0; k < vec.size(); ++k )
{
input.push_back( vec[k] );
}
}
void WHierarchicalTreeFibers::colorCluster( size_t cluster, WColor color )
{
std::list<size_t>worklist;
worklist.push_back( cluster );
while ( !worklist.empty() )
{
size_t current = worklist.front();
worklist.pop_front();
m_colors[current] = color;
if ( m_containsLeafes[current].size() > 1 )
{
size_t left = m_children[current].first;
size_t right = m_children[current].second;
worklist.push_back( left );
worklist.push_back( right );
}
}
}
......@@ -107,46 +107,12 @@ public:
*/
void setRoiBitField( boost::shared_ptr< std::vector<bool> > bitfield );
/**
* finds the X biggest clusters for a given cluster
* \param cluster
* \param number of sub clusters
*/
std::vector< size_t >findXBiggestClusters( size_t cluster, size_t number = 10 );
/**
* helper function to sort a list of clusters depending on the number of leafes in them
* \param input reference to the list to be sorted
*/
void sortList( std::list<size_t> &input ); //NOLINT
/**
* sets the color for a selected cluster and all sub clusters
* \param cluster
* \param color
*/
void colorCluster( size_t cluster, WColor color );
/**
* getter
* \param cluster
* \return the leafes contained in the selected cluster
*/
std::vector<size_t>getLeafesForCluster( size_t cluster );
protected:
private:
/**
* stores a pointer to the bitfield by the current roi setting
*/
boost::shared_ptr< std::vector<bool> > m_roiSelection;
/**
*vector that stores the leaf id's for each cluster, this is quite memory intensive but speeds up selection
* of leafes for nodes at higher levels
*/
std::vector< std::vector<size_t> >m_containsLeafes;
};
......@@ -155,9 +121,4 @@ inline void WHierarchicalTreeFibers::setRoiBitField( boost::shared_ptr< std::vec
m_roiSelection = bitfield;
}
inline std::vector<size_t>WHierarchicalTreeFibers::getLeafesForCluster( size_t cluster )
{
return m_containsLeafes[cluster];