Skip to content

Commit

Permalink
ENH: Add Clone() and MakeDeepCopy() to PointSetBase
Browse files Browse the repository at this point in the history
Allows making a "deep copy" of a point set (rather than the "shallow copy" made
by `PointSet::Graft`).
  • Loading branch information
N-Dekker committed Oct 29, 2024
1 parent 4b7886a commit 3bed9db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Modules/Core/Common/include/itkPointSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ class ITK_TEMPLATE_EXPORT PointSet : public PointSetBase<typename TMeshTraits::P
void
PrintSelf(std::ostream & os, Indent indent) const override;

private:
void
DetachPointData() override
{
if (m_PointDataContainer)
{
// Make a new copy of the point data, detached from the original one.
const auto pointData = PointDataContainer::New();
pointData->CastToSTLContainer() = m_PointDataContainer->CastToSTLConstContainer();
m_PointDataContainer = pointData;
}
}
}; // End Class: PointSet
} // end namespace itk

Expand Down
25 changes: 25 additions & 0 deletions Modules/Core/Common/include/itkPointSetBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class ITK_TEMPLATE_EXPORT PointSetBase : public DataObject
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(PointSetBase);

itkCloneMacro(Self);

/** Convenient type alias obtained from TPointsContainer template parameter. */
using PointType = typename TPointsContainer::Element;
using CoordRepType = typename PointType::CoordRepType;
Expand Down Expand Up @@ -190,6 +192,25 @@ class ITK_TEMPLATE_EXPORT PointSetBase : public DataObject

itkGetConstMacro(BufferedRegion, RegionType);


/** Returns a "deep copy" of this point set, having copied its points and point data. */
Pointer
MakeDeepCopy() const
{
const auto clone = this->Clone();
clone->Graft(this);

if (m_PointsContainer)
{
// Make a new copy of the points, detached from the original one.
const auto points = TPointsContainer::New();
points->CastToSTLContainer() = m_PointsContainer->CastToSTLConstContainer();
clone->m_PointsContainer = points;
}
clone->DetachPointData();
return clone;
}

protected:
/** Default-constructor, to be used by derived classes. */
PointSetBase() = default;
Expand All @@ -215,6 +236,10 @@ class ITK_TEMPLATE_EXPORT PointSetBase : public DataObject
RegionType m_RequestedNumberOfRegions{};
RegionType m_BufferedRegion{ -1 };
RegionType m_RequestedRegion{ -1 };

private:
virtual void
DetachPointData() = 0;
};
} // end namespace itk

Expand Down

0 comments on commit 3bed9db

Please sign in to comment.