Page MenuHomePhabricator

Planar figures cannot be cloned/deep copied safely
Closed, ResolvedPublic

Description

A very good example why this is a problem can be found in mitkPlanarFigureIOTest.cpp:

static PlanarFigureList CreateDeepCopiedPlanarFigures(PlanarFigureList original)
{
  PlanarFigureList copiedPlanarFigures;

  PlanarFigureList::iterator it1;

  for ( it1 = original.begin(); it1 != original.end(); ++it1 )
  {
    mitk::PlanarFigure::Pointer copiedFigure;
    if(strcmp((*it1)->GetNameOfClass(), "PlanarAngle") == 0)
    {
      copiedFigure    = mitk::PlanarAngle::New();
    }
    // ...
    // ...
    // ...
    if(strcmp((*it1)->GetNameOfClass(), "PlanarFourPointAngle") == 0)
    {
      copiedFigure    = mitk::PlanarFourPointAngle::New();
    }

    copiedFigure->DeepCopy((*it1));
    copiedPlanarFigures.push_back(copiedFigure.GetPointer());
  }
  return copiedPlanarFigures;
}

The function should look more like:

static PlanarFigureList CreateClonedPlanarFigures(PlanarFigureList original)
{
  PlanarFigureList clonedPlanarFigures;
  clonedPlanarFigures.resize(original.size());
  std::transform(original.begin(), original.end(), clonedPlanarFigures.begin(), Clone);
  return clonedPlanarFigures;
}

We should use the newly introduced clone API of ITK since PlanarFigure is a BaseData.

Solution: Add mitkCloneMacro(Self) in the protected sections of all classes that derive from PlanarFigure (forced by base class). If something has to be deep copied, explicitly implement the copy constructor in addition.

Event Timeline

New remote branch pushed: bug-16221-ClonePlanarFigures

[bd2421]: Merge branch 'bug-16221-ClonePlanarFigures'

Merged commits:

2013-10-02 15:39:09 Stefan Kislinskiy [9f0838]
Implemented Clone method for planar figures and declared DeepCopy method deprecated.

[78619f]: Merge branch 'bug-16221-DiffusionFix'

Merged commits:

2013-10-04 14:21:42 Stefan Kislinskiy [577573]
COMP: Made PlanarFigureComposite cloneable.