| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkTreeIteratorBase.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 __itkTreeContainerBase_txx |
| 18 |
DEF |
#define __itkTreeContainerBase_txx |
| 19 |
|
|
| 20 |
|
#include <itkTreeIteratorBase.h> |
| 21 |
|
#include "itkTreeChangeEvent.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
/** Constructor */ |
| 27 |
|
template <class TTreeType> |
| 28 |
LEN |
TreeIteratorBase<TTreeType>::TreeIteratorBase( TTreeType* tree, const TreeNodeType* start) |
| 29 |
|
{ |
| 30 |
|
if(start) |
| 31 |
|
{ |
| 32 |
|
m_Root = start; |
| 33 |
|
} |
| 34 |
|
else |
| 35 |
|
{ |
| 36 |
|
m_Root = dynamic_cast<const TreeNodeType*>(tree->GetRoot()); |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
m_Position = const_cast<TreeNodeType*>(m_Root); |
| 40 |
|
m_Tree = tree; |
| 41 |
|
m_Begin = m_Position; |
| 42 |
|
m_End = NULL; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
/** Constructor */ |
| 46 |
|
template <class TTreeType> |
| 47 |
LEN |
TreeIteratorBase<TTreeType>::TreeIteratorBase( const TTreeType* tree, const TreeNodeType* start) |
| 48 |
|
{ |
| 49 |
|
if(start) |
| 50 |
|
{ |
| 51 |
|
m_Root = start; |
| 52 |
|
} |
| 53 |
|
else |
| 54 |
|
{ |
| 55 |
LEN |
m_Root = const_cast<TreeNodeType*>(dynamic_cast<const TreeNodeType*>(tree->GetRoot())); |
| 56 |
|
} |
| 57 |
|
m_Position = const_cast<TreeNodeType*>(m_Root); |
| 58 |
|
m_Tree = const_cast<TTreeType*>(tree); |
| 59 |
|
m_Begin = m_Position; |
| 60 |
|
m_End = NULL; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
/** Return the current value of the node */ |
| 64 |
|
template <class TTreeType> |
| 65 |
|
const typename TreeIteratorBase<TTreeType>::ValueType& |
| 66 |
|
TreeIteratorBase<TTreeType>::Get() const |
| 67 |
|
{ |
| 68 |
|
return m_Position->Get(); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
/** Set the current value of the node */ |
| 72 |
|
template <class TTreeType> |
| 73 |
|
typename TreeIteratorBase<TTreeType>::ValueType& |
| 74 |
|
TreeIteratorBase<TTreeType>::Set(ValueType element) |
| 75 |
|
{ |
| 76 |
|
ValueType oldValue = m_Position->Get(); |
| 77 |
|
m_Position->Set(element); |
| 78 |
|
return oldValue; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
/** Add a value to the node. This create a new child node */ |
| 82 |
|
template <class TTreeType> |
| 83 |
|
bool |
| 84 |
|
TreeIteratorBase<TTreeType>::Add(ValueType element) |
| 85 |
|
{ |
| 86 |
|
if ( m_Position == NULL && m_Root == NULL ) |
| 87 |
|
{ |
| 88 |
|
bool returnValue; |
| 89 |
|
if ( m_Tree ) |
| 90 |
|
{ |
| 91 |
|
returnValue = const_cast<TTreeType*>(m_Tree)->SetRoot( element ); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
LEN,IND |
******m_Root = dynamic_cast<const TreeNodeType*>(const_cast<TTreeType*>(m_Tree)->GetRoot()); |
| 95 |
IND |
******m_Position = const_cast<TreeNodeType*>(m_Root); |
| 96 |
IND |
******m_Tree->Modified(); |
| 97 |
IND |
******m_Tree->InvokeEvent( TreeAddEvent<TTreeType>(*this) ); |
| 98 |
IND |
******return returnValue; |
| 99 |
|
} |
| 100 |
|
else if ( m_Position == NULL ) |
| 101 |
|
{ |
| 102 |
|
return false; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
typename TreeNodeType::Pointer node = TreeNodeType::New(); |
| 106 |
|
node->Set(element); |
| 107 |
|
node->SetParent(m_Position); |
| 108 |
|
m_Position->AddChild( node); |
| 109 |
|
m_Tree->Modified(); |
| 110 |
|
m_Tree->InvokeEvent( TreeAddEvent<TTreeType>(*this) ); |
| 111 |
|
return true; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
/** Add a new element at a given position */ |
| 115 |
|
template <class TTreeType> |
| 116 |
|
bool |
| 117 |
LEN |
TreeIteratorBase<TTreeType>::Add( int itkNotUsed(childPosition), ValueType element ) |
| 118 |
|
{ |
| 119 |
|
if ( m_Position ) |
| 120 |
|
{ |
| 121 |
|
typename TreeNodeType::Pointer node = TreeNodeType::New(); |
| 122 |
|
node->Set(element); |
| 123 |
|
node->SetParent(m_Position); |
| 124 |
SEM |
m_Position->AddChild(node) ; |
| 125 |
|
m_Tree->Modified(); |
| 126 |
|
m_Tree->InvokeEvent( TreeAddEvent<TTreeType>(*this) ); |
| 127 |
|
return true; |
| 128 |
|
} |
| 129 |
|
return false; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
/** Return true if the current pointed node is a leaf */ |
| 133 |
|
template <class TTreeType> |
| 134 |
|
bool |
| 135 |
|
TreeIteratorBase<TTreeType>::IsLeaf() const |
| 136 |
|
{ |
| 137 |
|
return !(m_Position->HasChildren()); |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
/** Return true if the current pointed node is a root */ |
| 141 |
|
template <class TTreeType> |
| 142 |
|
bool |
| 143 |
|
TreeIteratorBase<TTreeType>::IsRoot( ) const |
| 144 |
|
{ |
| 145 |
|
if ( m_Root == NULL ) |
| 146 |
|
{ |
| 147 |
|
return false; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
if ( m_Position == m_Root ) |
| 151 |
|
{ |
| 152 |
|
return true; |
| 153 |
|
} |
| 154 |
|
return false; |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
/** Add a subtree */ |
| 158 |
|
template <class TTreeType> |
| 159 |
|
bool |
| 160 |
|
TreeIteratorBase<TTreeType>::Add( TTreeType& subTree ) |
| 161 |
|
{ |
| 162 |
|
if ( subTree.Count() == 0 ) |
| 163 |
|
{ |
| 164 |
|
return false; |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
if(!subTree.GetRoot()) |
| 168 |
|
{ |
| 169 |
|
return false; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
if ( m_Root == NULL ) |
| 173 |
|
{ |
| 174 |
|
m_Root = static_cast<const TreeNodeType*>(subTree.GetRoot()); |
| 175 |
|
} |
| 176 |
|
else |
| 177 |
|
{ |
| 178 |
|
if ( m_Position == NULL ) |
| 179 |
|
{ |
| 180 |
|
return false; |
| 181 |
|
} |
| 182 |
LEN |
m_Position->AddChild( const_cast<TreeNodeType*>(static_cast<const TreeNodeType*>(subTree.GetRoot())) ); |
| 183 |
|
} |
| 184 |
|
return true; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
/** Return the subtree */ |
| 188 |
|
template <class TTreeType> |
| 189 |
|
TTreeType* |
| 190 |
|
TreeIteratorBase<TTreeType>::GetSubTree() const |
| 191 |
|
{ |
| 192 |
|
typename TTreeType::Pointer tree = TTreeType::New(); |
| 193 |
|
tree->SetSubtree(true); |
| 194 |
|
tree->SetRoot(m_Position); |
| 195 |
|
return tree; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
/** Return true of the current node has a child */ |
| 199 |
|
template <class TTreeType> |
| 200 |
|
bool |
| 201 |
|
TreeIteratorBase<TTreeType>::HasChild( int number ) const |
| 202 |
|
{ |
| 203 |
|
if ( m_Position == NULL ) |
| 204 |
|
{ |
| 205 |
|
return false; |
| 206 |
|
} |
| 207 |
IND |
*if ( m_Position->GetChild( number ) != NULL ) |
| 208 |
IND |
***{ |
| 209 |
IND |
***return true; |
| 210 |
IND |
***} |
| 211 |
|
return false; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
/** Return the current position of the child */ |
| 215 |
|
template <class TTreeType> |
| 216 |
|
int |
| 217 |
|
TreeIteratorBase<TTreeType>::ChildPosition( ValueType element ) const |
| 218 |
|
{ |
| 219 |
|
if ( !m_Position ) |
| 220 |
|
{ |
| 221 |
|
return -1; |
| 222 |
|
} |
| 223 |
|
return m_Position->ChildPosition( element ); |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
/** Remove a child */ |
| 227 |
|
template <class TTreeType> |
| 228 |
|
bool |
| 229 |
|
TreeIteratorBase<TTreeType>::RemoveChild( int number ) |
| 230 |
|
{ |
| 231 |
|
if( !HasChild( number ) ) |
| 232 |
|
{ |
| 233 |
|
return false; |
| 234 |
|
} |
| 235 |
LEN |
TreeNodeType* child = dynamic_cast<TreeNodeType*>(m_Position->GetChild( number )); |
| 236 |
|
|
| 237 |
|
if( child != NULL ) |
| 238 |
|
{ |
| 239 |
|
TreeNodeType* help = m_Position; |
| 240 |
|
m_Position = dynamic_cast<TreeNodeType*>(child); |
| 241 |
|
m_Tree->InvokeEvent( TreeRemoveEvent<TTreeType>( *this ) ); |
| 242 |
|
m_Position = dynamic_cast<TreeNodeType*>(help); |
| 243 |
|
const_cast<TreeNodeType*>(m_Position)->Remove( child ); |
| 244 |
|
m_Tree->Modified(); |
| 245 |
|
return true; |
| 246 |
|
} |
| 247 |
|
return false; |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
/** Count the number of children */ |
| 251 |
|
template <class TTreeType> |
| 252 |
|
int |
| 253 |
|
TreeIteratorBase<TTreeType>::CountChildren() const |
| 254 |
|
{ |
| 255 |
|
if ( m_Position == NULL ) |
| 256 |
|
{ |
| 257 |
|
return -1; |
| 258 |
|
} |
| 259 |
|
return m_Position->CountChildren( ); |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
/** Return true of the pointed node has a parent */ |
| 263 |
|
template <class TTreeType> |
| 264 |
|
bool |
| 265 |
|
TreeIteratorBase<TTreeType>::HasParent( ) const |
| 266 |
|
{ |
| 267 |
|
if ( m_Position == NULL && m_Position->GetParent() == NULL ) |
| 268 |
|
{ |
| 269 |
|
return false; |
| 270 |
|
} |
| 271 |
|
return true; |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
/** Disconnect the tree */ |
| 275 |
|
template <class TTreeType> |
| 276 |
|
bool |
| 277 |
|
TreeIteratorBase<TTreeType>::Disconnect() |
| 278 |
|
{ |
| 279 |
|
if ( m_Position == NULL ) |
| 280 |
|
{ |
| 281 |
|
return false; |
| 282 |
|
} |
| 283 |
|
|
| 284 |
|
if( m_Position->HasParent() == false ) |
| 285 |
|
{ |
| 286 |
|
return false; |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
TreeNodeType* parent = dynamic_cast<TreeNodeType*>(m_Position->GetParent()); |
| 290 |
|
m_Tree->InvokeEvent( TreeRemoveEvent<TTreeType>( *this ) ); |
| 291 |
|
parent->Remove( const_cast<TreeNodeType*>(m_Position) ); |
| 292 |
|
m_Tree->Modified(); |
| 293 |
|
int size = m_Position->CountChildren(); |
| 294 |
|
|
| 295 |
|
for( int i=0; i< size; i++ ) |
| 296 |
|
{ |
| 297 |
|
TreeNodeType* child = dynamic_cast<TreeNodeType*>(m_Position->GetChild(i)); |
| 298 |
|
parent->AddChild( child ); |
| 299 |
|
child->SetParent( parent ); |
| 300 |
|
} |
| 301 |
|
|
| 302 |
|
m_Position = NULL; |
| 303 |
|
return true; |
| 304 |
|
} |
| 305 |
|
|
| 306 |
|
/** Return the children list */ |
| 307 |
|
template <class TTreeType> |
| 308 |
|
TreeIteratorBase<TTreeType>* |
| 309 |
|
TreeIteratorBase<TTreeType>::Children() |
| 310 |
|
{ |
| 311 |
|
itkGenericOutputMacro("Not implemented yet"); |
| 312 |
LEN |
::itk::ExceptionObject e_(__FILE__, __LINE__, "Not implemented yet", ITK_LOCATION); |
| 313 |
|
throw e_; /* Explicit naming to work around Intel compiler bug. */ |
| 314 |
|
return 0; |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
|
| 318 |
|
/** Return the first parent found */ |
| 319 |
|
template <class TTreeType> |
| 320 |
|
const typename TreeIteratorBase<TTreeType>::TreeNodeType* |
| 321 |
|
TreeIteratorBase<TTreeType>::GetParent() const |
| 322 |
|
{ |
| 323 |
|
if(m_Position == NULL) |
| 324 |
|
{ |
| 325 |
|
return NULL; |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
return m_Position->GetParent(); |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
/** Return the list of parents */ |
| 332 |
|
template <class TTreeType> |
| 333 |
|
TreeIteratorBase<TTreeType>* TreeIteratorBase<TTreeType>::Parents() |
| 334 |
|
{ |
| 335 |
|
itkGenericOutputMacro("Not implemented yet"); |
| 336 |
LEN |
::itk::ExceptionObject e_(__FILE__, __LINE__, "Not implemented yet",ITK_LOCATION); |
| 337 |
|
throw e_; /* Explicit naming to work around Intel compiler bug. */ |
| 338 |
|
return 0; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
/** Go to a child */ |
| 342 |
|
template <class TTreeType> |
| 343 |
|
bool TreeIteratorBase<TTreeType>::GoToChild( int number ) |
| 344 |
|
{ |
| 345 |
|
if ( m_Position == NULL ) |
| 346 |
|
{ |
| 347 |
|
return false; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
LEN |
TreeNodeType* next = dynamic_cast<TreeNodeType*>(m_Position->GetChild( number )); |
| 351 |
|
|
| 352 |
|
if ( next == NULL ) |
| 353 |
|
{ |
| 354 |
|
return false; |
| 355 |
|
} |
| 356 |
|
m_Position = next; |
| 357 |
|
return true; |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
/** Go to a parent */ |
| 361 |
|
template <class TTreeType> |
| 362 |
|
bool TreeIteratorBase<TTreeType>::GoToParent( ) |
| 363 |
|
{ |
| 364 |
|
if ( m_Position == NULL ) |
| 365 |
|
{ |
| 366 |
|
return false; |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
if( !m_Position->HasParent() ) |
| 370 |
|
{ |
| 371 |
|
return false; |
| 372 |
|
} |
| 373 |
|
|
| 374 |
|
m_Position = dynamic_cast<TreeNodeType*>(m_Position->GetParent()); |
| 375 |
|
return true; |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
/** Get a child given a number */ |
| 379 |
|
template <class TTreeType> |
| 380 |
LEN |
TreeIteratorBase<TTreeType>* TreeIteratorBase<TTreeType>::GetChild( int number ) const |
| 381 |
|
{ |
| 382 |
|
if ( !m_Position ) |
| 383 |
|
{ |
| 384 |
|
return NULL; |
| 385 |
|
} |
| 386 |
|
|
| 387 |
LEN |
TreeNodeType* child = dynamic_cast<TreeNodeType*>(m_Position->GetChild( number )); |
| 388 |
|
|
| 389 |
|
if ( !child ) |
| 390 |
|
{ |
| 391 |
|
return NULL; |
| 392 |
|
} |
| 393 |
LEN,IND |
// return new WalkTreeIterator<ValueType,P>( child, m_Root, m_Tree, getType() ); |
| 394 |
|
return NULL; |
| 395 |
|
} |
| 396 |
|
|
| 397 |
|
/** Count the number of nodes from the beginning */ |
| 398 |
|
template <class TTreeType> |
| 399 |
|
int TreeIteratorBase<TTreeType>::Count() |
| 400 |
|
{ |
| 401 |
|
int size = 0; |
| 402 |
|
this->GoToBegin(); |
| 403 |
|
if ( !m_Position->HasChildren() ) |
| 404 |
|
{ |
| 405 |
|
return 0; |
| 406 |
|
} |
| 407 |
|
while (this->Next() ) |
| 408 |
|
{ |
| 409 |
|
size++; |
| 410 |
|
} |
| 411 |
|
return size; |
| 412 |
|
} |
| 413 |
|
|
| 414 |
|
/** Count the number of nodes */ |
| 415 |
|
template <class TTreeType> |
| 416 |
|
int TreeIteratorBase<TTreeType>::Count(TreeNodeType* node ) |
| 417 |
|
{ |
| 418 |
|
int size = 0; |
| 419 |
|
|
| 420 |
|
if ( !node->hasChildren() ) |
| 421 |
|
{ |
| 422 |
|
return 0; |
| 423 |
|
} |
| 424 |
|
|
| 425 |
|
TreeIteratorBase<TTreeType> it(this,node); |
| 426 |
|
|
| 427 |
|
while ( !it.IsAtEnd() ) |
| 428 |
IND |
***{ |
| 429 |
IND |
***++it; |
| 430 |
IND |
***size++; |
| 431 |
IND |
***} |
| 432 |
|
return size; |
| 433 |
|
} |
| 434 |
|
|
| 435 |
|
/** Get the node pointed by the iterator */ |
| 436 |
|
template <class TTreeType> |
| 437 |
|
typename TreeIteratorBase<TTreeType>::TreeNodeType* |
| 438 |
|
TreeIteratorBase<TTreeType>::GetNode() |
| 439 |
|
{ |
| 440 |
|
return const_cast<TreeNodeType*>(m_Position); |
| 441 |
|
} |
| 442 |
|
|
| 443 |
|
/** Get the node pointed by the iterator */ |
| 444 |
|
template <class TTreeType> |
| 445 |
|
const typename TreeIteratorBase<TTreeType>::TreeNodeType* |
| 446 |
|
TreeIteratorBase<TTreeType>::GetNode() const |
| 447 |
|
{ |
| 448 |
|
return m_Position; |
| 449 |
|
} |
| 450 |
|
|
| 451 |
|
/** Get the root */ |
| 452 |
|
template <class TTreeType> |
| 453 |
|
typename TreeIteratorBase<TTreeType>::TreeNodeType* & |
| 454 |
|
TreeIteratorBase<TTreeType>::GetRoot() |
| 455 |
|
{ |
| 456 |
|
return const_cast<TreeNodeType*>(m_Root); |
| 457 |
|
} |
| 458 |
|
|
| 459 |
|
/** Get the root (const) */ |
| 460 |
|
template <class TTreeType> |
| 461 |
|
const typename TreeIteratorBase<TTreeType>::TreeNodeType* & |
| 462 |
|
TreeIteratorBase<TTreeType>::GetRoot() const |
| 463 |
|
{ |
| 464 |
|
return m_Root; |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
/** Remove a specific node */ |
| 468 |
|
template <class TTreeType> |
| 469 |
|
bool |
| 470 |
|
TreeIteratorBase<TTreeType>::Remove() |
| 471 |
|
{ |
| 472 |
|
if ( m_Position == NULL ) |
| 473 |
|
{ |
| 474 |
|
return false; |
| 475 |
|
} |
| 476 |
|
|
| 477 |
|
if ( m_Position->HasParent() ) |
| 478 |
|
{ |
| 479 |
|
m_Tree->InvokeEvent( TreeRemoveEvent<TTreeType>(*this) ); |
| 480 |
|
TreeNodeType* parent = m_Position->GetParent(); |
| 481 |
|
parent->Remove( m_Position ); |
| 482 |
|
m_Tree->Modified(); |
| 483 |
|
} |
| 484 |
|
else if (m_Root == m_Position) |
| 485 |
|
{ |
| 486 |
|
m_Tree->InvokeEvent( TreeRemoveEvent<TTreeType>(*this) ); |
| 487 |
|
m_Root = NULL; |
| 488 |
|
m_Tree->Modified(); |
| 489 |
|
} |
| 490 |
|
|
| 491 |
|
m_Position = NULL; |
| 492 |
|
return true; |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
/** Return the tree */ |
| 496 |
|
template <class TTreeType> |
| 497 |
|
TTreeType* |
| 498 |
|
TreeIteratorBase<TTreeType>::GetTree() const |
| 499 |
|
{ |
| 500 |
|
return m_Tree; |
| 501 |
|
} |
| 502 |
|
|
| 503 |
|
|
| 504 |
|
} // namespace itk |
| 505 |
|
|
| 506 |
|
#endif |
| 507 |
|
|
| 508 |
EOF |
|