Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPlanarRectangle.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkProperties.h"
18 
19 #include "mitkPlanarRectangle.h"
20 #include "mitkPlaneGeometry.h"
21 
23  : FEATURE_ID_CIRCUMFERENCE(this->AddFeature("Circumference", "mm")), FEATURE_ID_AREA(this->AddFeature("Area", "mm2"))
24 {
25  // Rectangle has four control points
27  this->SetProperty("closed", mitk::BoolProperty::New(true));
28  this->SetNumberOfPolyLines(1);
29 }
30 
31 bool mitk::PlanarRectangle::SetControlPoint(unsigned int index, const Point2D &point, bool createIfDoesNotExist)
32 {
33  // heres the deal with the rectangle:
34  // when a point is moved all corresponding corner points are moved with him
35  // e.g. if the lower right point (index=3) is moved the upper right point (index=1)
36  // is moved in the same x direction
37  // and the lower left point (index=2) is moved in the same y direction
38  // the upper left point (index=0) is left untouched
39  bool set = PlanarFigure::SetControlPoint(index, point, createIfDoesNotExist);
40 
41  if (set)
42  {
43  // can be made better ...
44  unsigned int horizontalCorrespondingPointIndex = 1;
45  unsigned int verticalCorrespondingPointIndex = 3;
46  if (index == 1)
47  {
48  horizontalCorrespondingPointIndex = 0;
49  verticalCorrespondingPointIndex = 2;
50  }
51  else if (index == 2)
52  {
53  horizontalCorrespondingPointIndex = 3;
54  verticalCorrespondingPointIndex = 1;
55  }
56  else if (index == 3)
57  {
58  horizontalCorrespondingPointIndex = 2;
59  verticalCorrespondingPointIndex = 0;
60  }
61 
62  Point2D verticalCorrespondingPoint = GetControlPoint(verticalCorrespondingPointIndex);
63  verticalCorrespondingPoint[0] = point[0];
64  PlanarFigure::SetControlPoint(verticalCorrespondingPointIndex, verticalCorrespondingPoint);
65 
66  Point2D horizontalCorrespondingPoint = GetControlPoint(horizontalCorrespondingPointIndex);
67  horizontalCorrespondingPoint[1] = point[1];
68  PlanarFigure::SetControlPoint(horizontalCorrespondingPointIndex, horizontalCorrespondingPoint);
69  }
70 
71  return set;
72 }
73 
75 {
77  m_SelectedControlPoint = 3;
78 }
79 
81 {
82  this->ClearPolyLines();
83 
84  for (unsigned int i = 0; i < this->GetNumberOfControlPoints(); ++i)
85  this->AppendPointToPolyLine(0, this->GetControlPoint(i));
86 }
87 
88 void mitk::PlanarRectangle::GenerateHelperPolyLine(double /*mmPerDisplayUnit*/, unsigned int /*displayHeight*/)
89 {
90  // A polygon does not require helper objects
91 }
92 
94 {
95  // Calculate circumference
96  double circumference = 0.0;
97  unsigned int i;
98  for (i = 0; i < this->GetNumberOfControlPoints(); ++i)
99  {
100  circumference += this->GetWorldControlPoint(i).EuclideanDistanceTo(
101  this->GetWorldControlPoint((i + 1) % this->GetNumberOfControlPoints()));
102  }
103 
104  this->SetQuantity(FEATURE_ID_CIRCUMFERENCE, circumference);
105 
106  // Calculate rectangle area (well, done a bit clumsy...)
107  double area = 0.0;
108  if (this->GetPlaneGeometry() != nullptr)
109  {
110  for (i = 0; i < this->GetNumberOfControlPoints(); ++i)
111  {
112  Point2D p0 = this->GetControlPoint(i);
113  Point2D p1 = this->GetControlPoint((i + 1) % this->GetNumberOfControlPoints());
114 
115  area += p0[0] * p1[1] - p1[0] * p0[1];
116  }
117  area /= 2.0;
118  }
119 
120  this->SetQuantity(FEATURE_ID_AREA, fabs(area));
121 }
122 
123 void mitk::PlanarRectangle::PrintSelf(std::ostream &os, itk::Indent indent) const
124 {
125  Superclass::PrintSelf(os, indent);
126 
127  os << indent << "Number of control points: " << this->GetNumberOfControlPoints() << std::endl;
128 
129  os << indent << "Control points:" << std::endl;
130 
131  for (unsigned int i = 0; i < this->GetNumberOfControlPoints(); ++i)
132  {
133  os << indent << indent << i << ": " << GetControlPoint(i) << std::endl;
134  }
135 }
136 
138 {
139  const mitk::PlanarRectangle *otherRectangle = dynamic_cast<const mitk::PlanarRectangle *>(&other);
140  if (otherRectangle)
141  {
142  return Superclass::Equals(other);
143  }
144  else
145  {
146  return false;
147  }
148 }
virtual void GeneratePolyLine() override
Generates the poly-line representation of the planar figure.
Implementation of PlanarFigure representing a polygon with two or more control points.
void ResetNumberOfControlPoints(int numberOfControlPoints)
Set the initial number of control points of the planar figure.
static Pointer New()
virtual void EvaluateFeaturesInternal() override
Calculates feature quantities of the planar figure.
void SetProperty(const char *propertyKey, BaseProperty *property)
virtual void PlaceFigure(const Point2D &point)
Place figure at the given point (in 2D index coordinates) onto the given 2D geometry.
virtual void GenerateHelperPolyLine(double mmPerDisplayUnit, unsigned int displayHeight) override
Generates the poly-lines that should be drawn the same size regardless of zoom.
virtual void PrintSelf(std::ostream &os, itk::Indent indent) const override
void SetNumberOfPolyLines(unsigned int numberOfPolyLines)
defines the number of PolyLines that will be available
virtual bool SetControlPoint(unsigned int index, const Point2D &point, bool createIfDoesNotExist=false)
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
virtual bool SetControlPoint(unsigned int index, const Point2D &point, bool createIfDoesNotExist=false) override
virtual bool Equals(const mitk::PlanarFigure &other) const override
Compare two PlanarFigure objects Note: all subclasses have to implement the method on their own...
virtual void PlaceFigure(const Point2D &point) override
Place figure in its minimal configuration (a point at least) onto the given 2D geometry.