Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#8 from aylward/Points
Browse files Browse the repository at this point in the history
ENH: Refactored SpatialObjectPoints and its derivatives
  • Loading branch information
aylward authored Feb 11, 2019
2 parents e135401 + ce6be6e commit 33b245f
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 350 deletions.
2 changes: 2 additions & 0 deletions Modules/Core/SpatialObjects/include/itkEllipseSpatialObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class ITK_TEMPLATE_EXPORT EllipseSpatialObject:
/** world-space property getters */
itkGetConstReferenceMacro( Center, PointType );

void Update() override;

protected:
EllipseSpatialObject();
~EllipseSpatialObject() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ITK_TEMPLATE_EXPORT PointBasedSpatialObject:
{ return static_cast<SizeValueType>( m_Points.size() ); }

/** Method returns the Point closest to the given point */
SpatialObjectPointType * ClosestPoint(const PointType & worldPoint) const;
TSpatialObjectPointType & ClosestPoint(const PointType & worldPoint) const;

/** Returns true if the point is inside the Blob, false otherwise. */
bool IsInside(const PointType & worldPoint, unsigned int depth,
Expand Down
19 changes: 10 additions & 9 deletions Modules/Core/SpatialObjects/include/itkPointBasedSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >

/** Determine closest point in world space */
template< unsigned int TDimension, class TSpatialObjectPointType >
SpatialObjectPointType *
TSpatialObjectPointType &
PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
::ClosestPoint( const PointType & point )
::ClosestPoint( const PointType & point ) const
{
auto it = m_Points.begin();
auto itend = m_Points.end();
Expand All @@ -80,7 +80,8 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
while ( it != itend )
{
typename SpatialObjectPoint< TDimension >::PointType curpos =
this->GetObjectToWorldTransform()->TransformPoint( ( *it ).GetPosition() );
this->GetObjectToWorldTransform()->TransformPoint(
( *it ).GetPositionInObjectSpace() );
double curdistance = curpos.EuclideanDistanceTo(curPoint);
if ( curdistance < closestPointDistance )
{
Expand All @@ -95,9 +96,9 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >

/** Compute bounding box of just this object in world space */
template< unsigned int TDimension, class TSpatialObjectPointType >
void
bool
PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
::ComputeMyBoundingBox()
::ComputeMyBoundingBox() const
{
itkDebugMacro("Computing blob bounding box");

Expand All @@ -109,7 +110,7 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
return false;
}

PointType pt = ( *it ).GetPosition();
PointType pt = ( *it ).GetPositionInObjectSpace();

// Compute a bounding box in object space
typename BoundingBoxType::Pointer bb = BoundingBoxType::New();
Expand All @@ -119,10 +120,10 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
it++;
while ( it != end )
{
bb->ConsiderPoint( ( *it ).GetPosition() );
bb->ConsiderPoint( ( *it ).GetPositionInObjectSpace() );
it++;
}
bb->ComputeBOundingBox();
bb->ComputeBoundingBox();

// Next Transform the corners of the bounding box into world space
using PointsContainer = typename BoundingBoxType::PointsContainer;
Expand Down Expand Up @@ -176,7 +177,7 @@ PointBasedSpatialObject< TDimension, TSpatialObjectPointType >
for( unsigned int i=0; i<ObjectDimension; ++i )
{
if( ! Math::AlmostEquals( transformedPoint[i],
it->GetPosition()[i] ) )
it->GetPositionInObjectSpace()[i] ) )
{
equals = false;
break;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/SpatialObjects/include/itkSpatialObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ITK_TEMPLATE_EXPORT SpatialObject:

using RegionType = ImageRegion< VDimension >;

using PropertyType = SpatialObjectProperty< ScalarType >;
using PropertyType = SpatialObjectProperty;
using PropertyPointer = typename PropertyType::Pointer;

/* These are needed to participate in a Pipeline */
Expand Down
27 changes: 17 additions & 10 deletions Modules/Core/SpatialObjects/include/itkSpatialObjectPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef itkSpatialObjectPoint_h
#define itkSpatialObjectPoint_h

#include "itkSpatialObject.h"

#include "itkPoint.h"
#include "vnl/vnl_vector_fixed.h"
#include "itkRGBAPixel.h"
Expand All @@ -34,7 +36,7 @@ namespace itk
*/

template< unsigned int TPointDimension = 3,
class TSpatialObjectType >
class TSpatialObjectType = SpatialObject< TPointDimension> >
class ITK_TEMPLATE_EXPORT SpatialObjectPoint
{
public:
Expand All @@ -59,9 +61,9 @@ class ITK_TEMPLATE_EXPORT SpatialObjectPoint
/** Get the SpatialObjectPoint Id. */
itkGetMacro( Id, int );

/** Set the point object. Couldn't use macros for these methods. */
void SetPositionInObjectSpace(const PointType & newX)
{ m_X = newX; }
/** Set the point object. */
void SetPositionInObjectSpace(const PointType & newPositionInObjectSpace)
{ m_PositionInObjectSpace = newPositionInObjectSpace; }

template <typename... TCoordinate>
void SetPositionInObjectSpace(
Expand All @@ -74,24 +76,29 @@ class ITK_TEMPLATE_EXPORT SpatialObjectPoint
{
firstCoordinate, static_cast<double>(otherCoordinate)...
};
m_X = coordinates;
m_PositionInObjectSpace = coordinates;
}

/** Return a pointer to the point object. */
const PointType & GetPositionInObjectSpace() const
{ return m_X; }
{ return m_PositionInObjectSpace; }

itkSetConstObjectMacro( SpatialObject, SpatialObjectType );

/** Set the position in world coordinates, using the
* spatialObject's objectToWorld transform, inverse */
void SetPosition( const PointType & point );

/** Returns the position in world coordinates, using the
* spatialObject's objectToWorld transform */
PointType GetPosition() const;

/** Copy one SpatialObjectPoint to another */
SpatialObjectPoint & operator=(const SpatialObjectPoint & ) = default;
Self & operator=(const SpatialObjectPoint & rhs );

/** Set/Get color of the point */
itkSetConstReferenceMacro( Color, ColorType );
itkSetMacro( Color, ColorType );
itkGetMacro( Color, ColorType );
itkGetConstReferenceMacro( Color, ColorType );

/** Set the color */
Expand Down Expand Up @@ -138,12 +145,12 @@ class ITK_TEMPLATE_EXPORT SpatialObjectPoint
int m_ID;

/** Position of the point */
PointType m_X;
PointType m_PositionInObjectSpace;

/** Color of the point */
ColorType m_Color;

const SpatialObjectType::Pointer m_SpatialObject;
const typename SpatialObjectType::Pointer m_SpatialObject;
};

} // end of namespace itk
Expand Down
62 changes: 48 additions & 14 deletions Modules/Core/SpatialObjects/include/itkSpatialObjectPoint.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
namespace itk
{
/** Constructor */
template< unsigned int TPointDimension >
SpatialObjectPoint< TPointDimension >
template< unsigned int TPointDimension, class TSpatialObjectType >
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::SpatialObjectPoint()
{
m_Id = -1;

m_X.Fill( 0 );
m_PositionInObjectSpace.Fill( 0 );

m_Color.SetRed(1.0); // red by default
m_Color.SetGreen(0);
Expand All @@ -39,10 +39,27 @@ SpatialObjectPoint< TPointDimension >
m_SpatialObject = nullptr;
}

template< unsigned int TPointDimension >
PointType
SpatialObjectPoint< TPointDimension >
::GetPosition( )
template< unsigned int TPointDimension, class TSpatialObjectType >
void
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::SetPosition( const PointType & point )
{
if( m_SpatialObject == nullptr )
{
ExceptionObject e(__FILE__);
e.SetLocation( "SpatialObjectPoint:SetPosition" );
e.SetDescription( "The SpatialObject must be set prior to calling." );
throw e;
}

m_PositionInObjectSpace = m_SpatialObject->GetObjectToWorldTransform()->
GetInverseTransform()->TransformPoint( point );
}

template< unsigned int TPointDimension, class TSpatialObjectType >
typename SpatialObjectPoint< TPointDimension, TSpatialObjectType >::PointType
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::GetPosition() const
{
if( m_SpatialObject == nullptr )
{
Expand All @@ -52,13 +69,14 @@ SpatialObjectPoint< TPointDimension >
throw e;
}

return m_SpatialObject->GetObjectToWorldTransform()->TransformPoint( m_X );
return m_SpatialObject->GetObjectToWorldTransform()->TransformPoint(
m_PositionInObjectSpace );
}

/** Set the color of the point */
template< unsigned int TPointDimension >
template< unsigned int TPointDimension, class TSpatialObjectType >
void
SpatialObjectPoint< TPointDimension >
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::SetColor(float r, float g, float b, float a)
{
m_Color.SetRed(r);
Expand All @@ -67,22 +85,38 @@ SpatialObjectPoint< TPointDimension >
m_Color.SetAlpha(a);
}

template< unsigned int TPointDimension, class TSpatialObjectType >
typename SpatialObjectPoint< TPointDimension, TSpatialObjectType >::Self &
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::operator=(const SpatialObjectPoint & rhs)
{
if(this != &rhs)
{
this->SetId( rhs.GetId() );
this->SetPositionInObjectSpace( rhs.GetPositionInObjectSpace() );
this->SetColor( rhs.GetColor() );
}
return *this;
}


/** PrintSelfMethod */
template< unsigned int TPointDimension >
template< unsigned int TPointDimension, class TSpatialObjectType >
void
SpatialObjectPoint< TPointDimension >
SpatialObjectPoint< TPointDimension, TSpatialObjectType >
::PrintSelf(std::ostream & os, Indent indent) const
{
os << indent << "Id: " << m_Id << std::endl;
os << indent << "RGBA: " << m_Color.GetRed() << " ";
os << m_Color.GetGreen() << " ";
os << m_Color.GetBlue() << " ";
os << m_Color.GetAlpha() << std::endl;
os << indent << "Position: ";
for ( unsigned int i = 1; i < TPointDimension; i++ )
{
os << m_X[i - 1] << ",";
os << m_PositionInObjectSpace[i - 1] << ",";
}
os << m_X[TPointDimension - 1] << std::endl;
os << m_PositionInObjectSpace[TPointDimension - 1] << std::endl;
}
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SpatialObjectProperty
void SetTagStringDictionary( const std::map< std::string,
std::string > & dict );

SpatialObjectProperty & operator=(const SpatialObjectProperty * rhs );
Self & operator=(const SpatialObjectProperty * rhs );

itkGetConstMacro( MTime, ModifiedTimeType );

Expand Down
19 changes: 10 additions & 9 deletions Modules/Core/SpatialObjects/include/itkTubeSpatialObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ITK_TEMPLATE_EXPORT TubeSpatialObject:
using ScalarType = double;

using TubePointType = TSpatialObjectPointType;
using TubePointListType = std::vector< TubePointType >;
using TubePointListType = std::list< TubePointType >;

using PointType = typename Superclass::PointType;
using TransformType = typename Superclass::TransformType;
Expand All @@ -79,14 +79,8 @@ class ITK_TEMPLATE_EXPORT TubeSpatialObject:
bool ComputeTangentAndNormals();

/** Remove duplicate points */
unsigned int RemoveDuplicatePoints(unsigned int step = 1);

/** Returns true if the point is inside the tube, false otherwise. */
bool IsInside(const PointType & point, unsigned int depth = 0,
const std::string & name) const override;

/** Compute the boundaries of the tube. */
bool ComputeMyBoundingBox() const override;
unsigned int RemoveDuplicatePointsInObjectSpace(
double minSpacingInObjectSpace = 0 );

/** Set the parent point which corresponds to the
* position of the point in the parent's points list */
Expand All @@ -104,6 +98,13 @@ class ITK_TEMPLATE_EXPORT TubeSpatialObject:
* tube network in the scene */
itkGetConstMacro(Root, bool);

/** Compute the boundaries of the tube. */
bool ComputeMyBoundingBox() const override;

/** Returns true if the point is inside the tube, false otherwise. */
bool IsInside(const PointType & point, unsigned int depth = 0,
const std::string & name) const override;

void DeepCopy( const TubeSpatialObject * tube );

protected:
Expand Down
Loading

0 comments on commit 33b245f

Please sign in to comment.