Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

mitk::PlanarPolygon Class Reference

Implementation of PlanarFigure representing a polygon with two or more control points. More...

#include <mitkPlanarPolygon.h>

Inheritance diagram for mitk::PlanarPolygon:
Inheritance graph
[legend]
Collaboration diagram for mitk::PlanarPolygon:
Collaboration graph
[legend]

List of all members.

Public Types

typedef PlanarPolygon Self
typedef PlanarFigure Superclass
typedef itk::SmartPointer< SelfPointer
typedef itk::SmartPointer
< const Self
ConstPointer

Public Member Functions

virtual const char * GetClassName () const
virtual void SetClosed (bool closed)
 Set whether the polygon should be closed between first and last control point or not.
virtual void ClosedOn ()
virtual void ClosedOff ()
unsigned int GetMinimumNumberOfControlPoints () const
 Place figure in its minimal configuration (a point at least) onto the given 2D geometry.
unsigned int GetMaximumNumberOfControlPoints () const
 Polygon maximum number of control points is principally not limited.

Static Public Member Functions

static Pointer New ()

Protected Member Functions

 PlanarPolygon ()
virtual ~PlanarPolygon ()
virtual void GeneratePolyLine ()
 Generates the poly-line representation of the planar figure.
virtual void GenerateHelperPolyLine (double mmPerDisplayUnit, unsigned int displayHeight)
 Generates the poly-lines that should be drawn the same size regardless of zoom.
virtual void EvaluateFeaturesInternal ()
 Calculates feature quantities of the planar figure.
bool CheckForLineIntersection (Point2D p1, Point2D p2, Point2D p3, Point2D p4)
virtual void PrintSelf (std::ostream &os, itk::Indent indent) const

Protected Attributes

const unsigned int FEATURE_ID_CIRCUMFERENCE
const unsigned int FEATURE_ID_AREA

Detailed Description

Implementation of PlanarFigure representing a polygon with two or more control points.

Definition at line 35 of file mitkPlanarPolygon.h.


Member Typedef Documentation

typedef itk::SmartPointer<const Self> mitk::PlanarPolygon::ConstPointer

Reimplemented from mitk::PlanarFigure.

Definition at line 38 of file mitkPlanarPolygon.h.

typedef itk::SmartPointer<Self> mitk::PlanarPolygon::Pointer

Reimplemented from mitk::PlanarFigure.

Definition at line 38 of file mitkPlanarPolygon.h.

Reimplemented from mitk::PlanarFigure.

Definition at line 38 of file mitkPlanarPolygon.h.

Reimplemented from mitk::PlanarFigure.

Definition at line 38 of file mitkPlanarPolygon.h.


Constructor & Destructor Documentation

mitk::PlanarPolygon::PlanarPolygon (  ) [protected]

Definition at line 27 of file mitkPlanarPolygon.cpp.

References mitk::PlanarFigure::m_PolyLines, mitk::BoolProperty::New(), mitk::PlanarFigure::ResetNumberOfControlPoints(), and mitk::BaseData::SetProperty().

: FEATURE_ID_CIRCUMFERENCE( this->AddFeature( "Circumference", "mm" ) ),
  FEATURE_ID_AREA( this->AddFeature( "Area", "mm^2" ) )
{
  // Polygon has at least two control points
  this->ResetNumberOfControlPoints( 2 );

  // Polygon is closed by default
  this->SetProperty( "closed", mitk::BoolProperty::New( true ) );

  m_PolyLines->InsertElement( 0, VertexContainerType::New());
}
mitk::PlanarPolygon::~PlanarPolygon (  ) [protected, virtual]

Definition at line 41 of file mitkPlanarPolygon.cpp.

{
}

Member Function Documentation

bool mitk::PlanarPolygon::CheckForLineIntersection ( Point2D  p1,
Point2D  p2,
Point2D  p3,
Point2D  p4 
) [protected]

Definition at line 213 of file mitkPlanarPolygon.cpp.

References QuadProgPP::max(), and min.

{
  // do not check for intersections with control points
  if(p1 == p2 || p1 == p3 || p1 == p4 ||
     p2 == p3 || p2 == p4 ||
     p3 == p4)
    return false;
  

  // Store the values for fast access and easy
  // equations-to-code conversion
  float x1 = p1[0], x2 = p2[0], x3 = p3[0], x4 = p4[0];
  float y1 = p1[1], y2 = p2[1], y3 = p3[1], y4 = p4[1];
   
  float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  // If d is zero, there is no intersection
  //if (d < mitk::eps) return false;
  if (d == 0) return false;
   
  // Get the x and y
  float pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
  float x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
  float y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;
   
  // Check if the x and y coordinates are within both lines
  if ( x < std::min(x1, x2) || x > std::max(x1, x2) ||
  x < std::min(x3, x4) || x > std::max(x3, x4) ) return false;
  if ( y < std::min(y1, y2) || y > std::max(y1, y2) ||
  y < std::min(y3, y4) || y > std::max(y3, y4) ) return false;
   
  // point of intersection
  Point2D ret; ret[0] = x; ret[1] = y;
  return true;
}
virtual void mitk::PlanarPolygon::ClosedOff (  ) [virtual]
virtual void mitk::PlanarPolygon::ClosedOn (  ) [virtual]
void mitk::PlanarPolygon::EvaluateFeaturesInternal (  ) [protected, virtual]

Calculates feature quantities of the planar figure.

Implements mitk::PlanarFigure.

Definition at line 121 of file mitkPlanarPolygon.cpp.

References int().

{
  // Calculate circumference
  double circumference = 0.0;
  unsigned int i,j;
  for ( i = 0; i < this->GetNumberOfControlPoints() - 1; ++i )
  {
    circumference += this->GetWorldControlPoint( i ).EuclideanDistanceTo( 
      this->GetWorldControlPoint( i + 1 ) );
  }

  if ( this->IsClosed() )
  {
    circumference += this->GetWorldControlPoint( i ).EuclideanDistanceTo(
      this->GetWorldControlPoint( 0 ) );
  }

  this->SetQuantity( FEATURE_ID_CIRCUMFERENCE, circumference );


  // Calculate polygon area (if closed)
  double area       = 0.0;
  bool intersection = false;
  
  if ( this->IsClosed() && (this->GetGeometry2D() != NULL) )
  {
    // does PlanarPolygon overlap/intersect itself?
    unsigned int numberOfPoints = (unsigned int)GetNumberOfControlPoints();
    if( numberOfPoints >= 4)
    {
      for ( i = 0; i < (numberOfPoints - 1); ++i )
      {
        // line 1
        Point2D p0 = this->GetControlPoint( i );
        Point2D p1 = this->GetControlPoint(i + 1);

        // check for intersection with all other lines
        for (j = i+1; j < (numberOfPoints - 1); ++j )
        {
          Point2D p2 = this->GetControlPoint(j);
          Point2D p3 = this->GetControlPoint(j + 1);
          intersection = CheckForLineIntersection(p0,p1,p2,p3);
          if (intersection) break;
        }
        if (intersection) break; // only because the inner loop might have changed "intersection"
      
        // last line from p_x to p_0
        Point2D p2 = this->GetControlPoint(0);
        Point2D p3 = this->GetControlPoint(numberOfPoints - 1);
       
        intersection = CheckForLineIntersection(p0,p1,p2,p3);
        if (intersection) break;
      }
   }
      
    // calculate area
    for ( i = 0; i < this->GetNumberOfControlPoints(); ++i )
    {
      Point2D p0 = this->GetControlPoint( i );
      Point2D p1 = this->GetControlPoint( (i + 1) % this->GetNumberOfControlPoints() );

      area += p0[0] * p1[1] - p1[0] * p0[1];
    }
    area /= 2.0;
  }
  
  // set area if appropiate (i.e. closed and not intersected)
  if(this->IsClosed() && !intersection)
  {
    SetQuantity( FEATURE_ID_AREA, fabs( area ) );
    this->ActivateFeature( FEATURE_ID_AREA );
  }
  else
  {    
    SetQuantity( FEATURE_ID_AREA,  0  );
    this->DeactivateFeature( FEATURE_ID_AREA );
  }
}
void mitk::PlanarPolygon::GenerateHelperPolyLine ( double  mmPerDisplayUnit,
unsigned int  displayHeight 
) [protected, virtual]

Generates the poly-lines that should be drawn the same size regardless of zoom.

Implements mitk::PlanarFigure.

Definition at line 116 of file mitkPlanarPolygon.cpp.

{
  // A polygon does not require helper objects
}
void mitk::PlanarPolygon::GeneratePolyLine (  ) [protected, virtual]

Generates the poly-line representation of the planar figure.

Implements mitk::PlanarFigure.

Definition at line 94 of file mitkPlanarPolygon.cpp.

{
  // if more elements are needed that have been reserved -> reserve
  if ( m_PolyLines->ElementAt( 0 )->size() < this->GetNumberOfControlPoints() )
  {
    m_PolyLines->ElementAt( 0 )->Reserve( this->GetNumberOfControlPoints() );
  }
  // if more elements have been reserved/set before than are needed now -> clear vector
  else if (m_PolyLines->ElementAt( 0 )->size() > this->GetNumberOfControlPoints())
  {
    m_PolyLines->ElementAt( 0 )->clear();
  }


  // TODO: start polygon at specified initalize point...
  m_PolyLines->ElementAt( 0 )->Reserve( this->GetNumberOfControlPoints() );
  for ( unsigned int i = 0; i < this->GetNumberOfControlPoints(); ++i )
  {
    m_PolyLines->ElementAt( 0 )->ElementAt( i ) = m_ControlPoints->ElementAt( i );  
  }
}
virtual const char* mitk::PlanarPolygon::GetClassName (  ) const [virtual]

Reimplemented from mitk::PlanarFigure.

unsigned int mitk::PlanarPolygon::GetMaximumNumberOfControlPoints (  ) const [inline, virtual]

Polygon maximum number of control points is principally not limited.

Implements mitk::PlanarFigure.

Definition at line 65 of file mitkPlanarPolygon.h.

  {
    return 1000;
  }
unsigned int mitk::PlanarPolygon::GetMinimumNumberOfControlPoints (  ) const [inline, virtual]

Place figure in its minimal configuration (a point at least) onto the given 2D geometry.

Must be implemented in sub-classes.Polygon has 2 control points per definition.

Implements mitk::PlanarFigure.

Definition at line 58 of file mitkPlanarPolygon.h.

  {
    return 2;
  }
static Pointer mitk::PlanarPolygon::New (  ) [static]
void mitk::PlanarPolygon::PrintSelf ( std::ostream &  os,
itk::Indent  indent 
) const [protected, virtual]

Reimplemented from mitk::PlanarFigure.

Definition at line 201 of file mitkPlanarPolygon.cpp.

{
  Superclass::PrintSelf( os, indent );

  if (this->IsClosed())
    os << indent << "Polygon is closed\n";
  else
    os << indent << "Polygon is not closed\n";
}
void mitk::PlanarPolygon::SetClosed ( bool  closed ) [virtual]

Set whether the polygon should be closed between first and last control point or not.

Definition at line 46 of file mitkPlanarPolygon.cpp.

References mitk::BoolProperty::New().

{
  this->SetProperty( "closed", mitk::BoolProperty::New( closed ) );

  if ( !closed )
  {
    // For non-closed polygons: use "Length" as feature name; disable area
    this->SetFeatureName( FEATURE_ID_CIRCUMFERENCE, "Length" );
    this->DeactivateFeature( FEATURE_ID_AREA );
  }
  else
  {
    // For closed polygons: use "Circumference" as feature name; enable area
    this->SetFeatureName( FEATURE_ID_CIRCUMFERENCE, "Circumference" );
    this->ActivateFeature( FEATURE_ID_AREA );
  }

  this->Modified();
}

Member Data Documentation

const unsigned int mitk::PlanarPolygon::FEATURE_ID_AREA [protected]

Definition at line 89 of file mitkPlanarPolygon.h.

const unsigned int mitk::PlanarPolygon::FEATURE_ID_CIRCUMFERENCE [protected]

Definition at line 88 of file mitkPlanarPolygon.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines