| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkTreeNode.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:48 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef _itkTreeNode_txx |
| 18 |
DEF |
#define _itkTreeNode_txx |
| 19 |
|
|
| 20 |
|
#include "itkTreeNode.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
/** Constructor */ |
| 26 |
|
template <class TValueType> |
| 27 |
|
TreeNode<TValueType>::TreeNode() |
| 28 |
|
{ |
| 29 |
|
m_Parent = NULL; |
| 30 |
|
} |
| 31 |
|
|
| 32 |
|
/** Destructor */ |
| 33 |
|
template <class TValueType> |
| 34 |
|
TreeNode<TValueType>::~TreeNode() |
| 35 |
|
{ |
| 36 |
|
if ( m_Parent ) |
| 37 |
|
{ |
| 38 |
|
m_Parent->Remove(this); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
SEM,IND |
*for ( int i=m_Children.size() ; i > 0; i-- ) |
| 42 |
IND |
***{ |
| 43 |
IND |
***m_Children[i-1]->SetParent(NULL); |
| 44 |
IND |
***m_Children[i-1] = 0; |
| 45 |
IND |
***} |
| 46 |
|
m_Children.clear(); |
| 47 |
|
m_Parent = NULL; |
| 48 |
|
m_Data = 0; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/** Return the parent node */ |
| 52 |
|
template <class TValueType> |
| 53 |
|
TreeNode<TValueType>* |
| 54 |
|
TreeNode<TValueType> |
| 55 |
|
::GetParent( ) const |
| 56 |
|
{ |
| 57 |
|
return m_Parent; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
/** Get a child */ |
| 61 |
|
template <class TValueType> |
| 62 |
|
TreeNode<TValueType>* |
| 63 |
|
TreeNode<TValueType> |
| 64 |
|
::GetChild( int number ) const |
| 65 |
|
{ |
| 66 |
|
if ( (unsigned int)number < m_Children.size() ) |
| 67 |
|
{ |
| 68 |
|
return m_Children[number]; |
| 69 |
|
} |
| 70 |
|
else |
| 71 |
|
{ |
| 72 |
|
return NULL; |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
/** Set the value of a node */ |
| 77 |
|
template <class TValueType> |
| 78 |
|
TValueType TreeNode<TValueType>::Set(const TValueType data) |
| 79 |
|
{ |
| 80 |
|
TValueType help = m_Data; |
| 81 |
|
m_Data = data; |
| 82 |
|
return help; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
/** Get the data of node */ |
| 86 |
|
template <class TValueType> |
| 87 |
|
const TValueType& TreeNode<TValueType>::Get() const |
| 88 |
|
{ |
| 89 |
|
return m_Data; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
/** Return true if has a parent */ |
| 93 |
|
template <class TValueType> |
| 94 |
|
bool |
| 95 |
|
TreeNode<TValueType>::HasParent() const |
| 96 |
|
{ |
| 97 |
|
return (m_Parent)?true:false; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
/** Set the parent node */ |
| 101 |
|
template <class TValueType> |
| 102 |
|
void |
| 103 |
|
TreeNode<TValueType>::SetParent( TreeNode<TValueType>* node) |
| 104 |
|
{ |
| 105 |
|
m_Parent = node; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
/** Return true if the node has children */ |
| 109 |
|
template <class TValueType> |
| 110 |
|
bool TreeNode<TValueType>::HasChildren() const |
| 111 |
|
{ |
| 112 |
|
return (m_Children.size()>0)?true:false; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
/** Return the number of children */ |
| 116 |
|
template <class TValueType> |
| 117 |
|
int |
| 118 |
|
TreeNode<TValueType> |
| 119 |
|
::CountChildren( ) const |
| 120 |
|
{ |
| 121 |
|
return m_Children.size(); |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
/** Remove a child node from the current node */ |
| 125 |
|
template <class TValueType> |
| 126 |
|
bool |
| 127 |
|
TreeNode<TValueType> |
| 128 |
|
::Remove( TreeNode<TValueType> *n ) |
| 129 |
|
{ |
| 130 |
|
typename std::vector<Pointer>::iterator pos; |
| 131 |
|
pos = std::find(m_Children.begin(), m_Children.end(), n ); |
| 132 |
|
if ( pos != m_Children.end() ) |
| 133 |
|
{ |
| 134 |
|
m_Children.erase(pos); |
| 135 |
|
n->SetParent(NULL); |
| 136 |
|
return true; |
| 137 |
|
} |
| 138 |
|
return false; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/** Replace a child by a new one */ |
| 142 |
|
template <class TValueType> |
| 143 |
LEN |
bool TreeNode<TValueType>::ReplaceChild( TreeNode<TValueType> *oldChild, TreeNode<TValueType> *newChild ) |
| 144 |
|
{ |
| 145 |
|
int size = m_Children.size(); |
| 146 |
|
|
| 147 |
|
for ( int i=0; i<size; i++ ) |
| 148 |
|
{ |
| 149 |
|
if ( m_Children[i] == oldChild ) |
| 150 |
|
{ |
| 151 |
|
m_Children[i] = newChild; |
| 152 |
|
return true; |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
return false; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/** Return the child position given a node */ |
| 159 |
|
template <class TValueType> |
| 160 |
LEN |
int TreeNode<TValueType>::ChildPosition( const TreeNode<TValueType> *node ) const |
| 161 |
|
{ |
| 162 |
|
for ( unsigned int i=0; i < m_Children.size(); i++ ) |
| 163 |
|
{ |
| 164 |
|
if ( m_Children[i] == node ) |
| 165 |
|
{ |
| 166 |
|
return i; |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
return -1; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
/** Return the child position given an element, the first child found. */ |
| 173 |
|
template <class TValueType> |
| 174 |
|
int TreeNode<TValueType>::ChildPosition( TValueType element ) const |
| 175 |
|
{ |
| 176 |
|
for ( unsigned int i=0; i < m_Children.size(); i++ ) |
| 177 |
|
{ |
| 178 |
|
if ( m_Children[i]->Get() == element ) |
| 179 |
|
return i; |
| 180 |
|
} |
| 181 |
|
return -1; |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
/** Add a child node */ |
| 185 |
|
template <class TValueType> |
| 186 |
|
void TreeNode<TValueType>::AddChild( TreeNode<TValueType> *node ) |
| 187 |
|
{ |
| 188 |
|
node->SetParent(this); |
| 189 |
|
m_Children.push_back(node); |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
/** Add a child at a specific position in the children list */ |
| 193 |
|
template <class TValueType> |
| 194 |
|
void |
| 195 |
|
TreeNode<TValueType> |
| 196 |
|
::AddChild( int number, TreeNode<TValueType> *node ) |
| 197 |
|
{ |
| 198 |
|
int size = m_Children.size(); |
| 199 |
|
|
| 200 |
|
if ( number > size ) |
| 201 |
|
{ |
| 202 |
|
for ( int i=size; i <= number; i++ ) |
| 203 |
|
{ |
| 204 |
|
m_Children[i] = NULL; |
| 205 |
|
} |
| 206 |
|
m_Children[number] = node; |
| 207 |
|
return; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
TreeNode<TValueType>* old = m_Children[number]; |
| 211 |
|
if ( old != NULL ) |
| 212 |
|
{ |
| 213 |
|
delete old; |
| 214 |
|
} |
| 215 |
|
m_Children[number] = node; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
/** Get the number of children given a name and a depth */ |
| 219 |
|
template <class TValueType> |
| 220 |
|
unsigned int |
| 221 |
|
TreeNode<TValueType> |
| 222 |
|
::GetNumberOfChildren( unsigned int depth, char * name ) const |
| 223 |
|
{ |
| 224 |
|
typename ChildrenListType::const_iterator it = m_Children.begin(); |
| 225 |
|
typename ChildrenListType::const_iterator itEnd = m_Children.end(); |
| 226 |
|
|
| 227 |
|
unsigned int cnt = 0; |
| 228 |
|
while(it != itEnd) |
| 229 |
|
{ |
| 230 |
|
if(name == NULL || strstr(typeid(**it).name(), name)) |
| 231 |
|
{ |
| 232 |
|
cnt++; |
| 233 |
|
} |
| 234 |
|
it++; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
it = m_Children.begin(); |
| 238 |
|
itEnd = m_Children.end(); |
| 239 |
|
if( depth > 0 ) |
| 240 |
|
{ |
| 241 |
|
while(it != itEnd) |
| 242 |
|
{ |
| 243 |
|
cnt += (*it)->GetNumberOfChildren( depth-1, name ); |
| 244 |
|
it++; |
| 245 |
|
} |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
return cnt; |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
/** Get children given a name and a depth */ |
| 252 |
|
template <class TValueType> |
| 253 |
|
typename TreeNode<TValueType>::ChildrenListType* |
| 254 |
|
TreeNode<TValueType> |
| 255 |
|
::GetChildren( unsigned int depth, char * name) const |
| 256 |
|
{ |
| 257 |
|
ChildrenListType * children = new ChildrenListType; |
| 258 |
|
|
| 259 |
|
typename ChildrenListType::const_iterator childrenListIt = |
| 260 |
IND |
****m_Children.begin(); |
| 261 |
|
typename ChildrenListType::const_iterator childrenListEnd = |
| 262 |
IND |
****m_Children.end(); |
| 263 |
|
|
| 264 |
|
while( childrenListIt != childrenListEnd ) |
| 265 |
|
{ |
| 266 |
|
if( name == NULL || strstr(typeid(**childrenListIt).name(), name) ) |
| 267 |
|
{ |
| 268 |
|
children->push_back(*childrenListIt); |
| 269 |
|
} |
| 270 |
|
if( depth > 0 ) |
| 271 |
|
{ |
| 272 |
|
ChildrenListType * nextchildren = (**childrenListIt).GetChildren(depth-1, |
| 273 |
|
name); |
| 274 |
|
// Add the child to the current list |
| 275 |
|
typename ChildrenListType::const_iterator nextIt = nextchildren->begin(); |
| 276 |
|
while(nextIt != nextchildren->end()) |
| 277 |
|
{ |
| 278 |
|
children->push_back(*nextIt); |
| 279 |
|
nextIt++; |
| 280 |
|
} |
| 281 |
|
delete nextchildren; |
| 282 |
|
} |
| 283 |
|
childrenListIt++; |
| 284 |
|
} |
| 285 |
|
|
| 286 |
|
return children; |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 290 |
|
} // namespace itk |
| 291 |
|
|
| 292 |
|
#endif |
| 293 |
|
|