[Insight-users] CellInterface & PolygonCell & Region

Luis Ibanez luis.ibanez@kitware.com
Wed, 27 Nov 2002 15:20:18 -0500


Hi Ofri,

You got it right.

The Mesh assumes the responsibility for
releasing the memory allocated for the
cells.

There is no need to call "delete pcell"
after mesh->SetCell(). Calling delete will
render invalid the cell pointer stored in
the Mesh.

There are some dark details here that you
may want to ignore at this point...

but since you asked    :-)

The itk::Mesh is intended to facilitate
dynamic management of cells. It was designed
with deformable models in mind.

For this reason most of the examples you see
in the toolkit use this approach of allocating
individual cells and assigning them to the Mesh.

There is however a drawback in this way of managing
cells. If you allocate thousands of cells this
becomes a slow process (since "new" is slow in itself)
and produced memory fragmentation.

There are two alternative ways of allocating cells
for the Mesh. They are selected with the method:

void SetCellsAllocationMethod (CellsAllocationMethodType _arg)

Which accept as argument one of the following enum symbols:

1 - CellsAllocationMethodUndefined
2 - CellsAllocatedAsStaticArray
3 - CellsAllocatedAsADynamicArray
4 - CellsAllocatedDynamicallyCellByCell

By default the Mesh is set to number (4) which means that
the user will allocate cell by cell calling "new" and then
SetCell(). Just as you did in your example.


Option (2) is what you want if your Mesh is not dynamic.
(e.g. cells are no created/destroyed) and you have a large
number of cells.  In this case you allocate an array of cells
like:

mesh->SetCellsAllocationMethod(
        MeshType::CellsAllocatedAsADynamicArray );

const int numberOfCells = 10000;
LineType  * lines = new LineType[ numberOfCells ];

for(unsigned int i=0; i<numberOfCells; i++)
{
   CellAutoPointer cell.TakeOwnership( &(lines[i]) );
   mesh->SetCell( i, cell );
}

// don't call delete on "lines"


The mesh will assume that cells are never changed
and that the pointer to the first cell is the pointer
to the full array. Upon mesh destruction the cell array
will be deleted.


Option (1) will look like :

mesh->SetCellsAllocationMethod(
    MeshType::CellsAllocatedAsStatictArray );

const int numberOfCells = 10000;
LineType lines[ numberOfCells ];
for(unsigned int i=0; i<numberOfCells; i++)
{
   CellAutoPointer cell.TakeOwnership( &(lines[i]) );
   mesh->SetCell( i, cell );
}


The mesh will not attempt to release the array's
memory at destruction time. This last approach
make impossible to use the mesh outside of the
scope where the "lines" static array has been
defined, since the array is destroyed when the
program leaves the scope.


----------

Unless you start encountering memory problems
or performance difficulties, it is better to
stick to mode (4) which is the default: Cell
by cell allocation.


Please let us know if you have further questions.


   Thanks


    Luis


====================================================
Ofri Sadowsky wrote:

>    PolygonType  *  pcell = new PolygonType;
>    pcell->AdPointId( pp );  // many times...
>    _polygonCellPtr.TakeOwnership( pcell );
>    _mesh->SetCell(0, cellptr);
> 
> Just to see that I understand it right - I should not try to delete pcell
> while getting rid of _mesh. _mesh takes ownership of pcell in the SetCell
> operation.
> 
>