[Insight-users] How to clear the nodes in a NodeContainer?

Luis Ibanez luis.ibanez@kitware.com
Thu, 26 Sep 2002 08:59:38 -0400


Hi Zhao,

The default PointsContainer in the PointSet & Mesh
classes is the itk::VectorContainer.

If you just want to clear the nodes the method

            Initalize()

will do it.

For example, from the pointSet or mesh you can do:

      pointSet->GetPoints()->Initialize();

However, this doesn't release the memory allocated
for the nodes.

The reason is that this class derives from the
STL vector and inherits its behavior.

   http://www.sgi.com/tech/stl/Vector.html

The std::vector class has a mecanism for reserving memory
(by preallocating a block of memory) and then manage the
preallocated block by filling it with elements as you
call "push_back". The preallocation is done with

          "reserve()"

You can check the size of the current preallocated memory
block by calling:

           "capacity()"

If the preallocated memory is filled up. The vector internally
allocates a new block of memory double in size of the previous
one. That has the advantage of preventing the vector from
overflowing the allocated block.

The elements on the std::vector can be removed by calling

              "clear()"

or more selectively by calling

     "erase(iterator)"
     "erase(iterator begin, iterator end)"

However the std::vector silently keep the preallocated memory.
This is due to the fact that allocating memory is a slow
process, and the consideration that if your vector reached a
certain maximum size it will probably do it again along the
process.


Most of the std::vector methods have been masked inside
itk::VectorContainer methods. You can see the details
in    "Insight/Code/Common/itkVectorContainer.h" or in
http://public.kitware.com/Insight/Doxygen/html/classitk_1_1VectorContainer.html


----


In practice the only way to make sure that the allocated
memory is release is to destroy the Container itself.

This is easily done in the itk::PointSet and itk::Mesh
classes just by plugging in a newly created PointContainer.

Something like

pointSet->SetPoints(  PointSetType::PointsContainer::New() );


The previous container will be deleted when its reference
count reaches zero.


Please let us know if you have further questions,


   Thanks


     Luis




==========================================================

zhao wrote:

> Hi all,
> 
>  
> 
>     How to clear the nodes in a NodeContainer? I can't find any member 
> function that can do this job.
> 
>     Thanks!
> 
>  
> 
> Zhao
> 
>  
> 
>  
>