Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
berryGeometry.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "berryGeometry.h"
14 #include "berryConstants.h"
15 
16 #include <QPoint>
17 #include <QRect>
18 #include <QWidget>
19 
20 #include <limits>
21 
22 namespace berry
23 {
24 
25 int Geometry::GetDimension(const QRect& toMeasure, bool width)
26 {
27  if (width)
28  {
29  return toMeasure.width();
30  }
31  return toMeasure.height();
32 }
33 
34 bool Geometry::IsHorizontal(int berrySideConstant)
35 {
36  return !(berrySideConstant == Constants::LEFT || berrySideConstant
37  == Constants::RIGHT);
38 }
39 
40 QRect Geometry::GetExtrudedEdge(const QRect& toExtrude, int size,
41  int orientation)
42 {
43  QRect bounds(toExtrude);
44 
45  if (!IsHorizontal(orientation))
46  {
47  bounds.setWidth(size);
48  }
49  else
50  {
51  bounds.setHeight(size);
52  }
53 
54  if (orientation == Constants::RIGHT)
55  {
56  bounds.moveLeft(toExtrude.x() + toExtrude.width() - bounds.width());
57  }
58  else if (orientation == Constants::BOTTOM)
59  {
60  bounds.moveTop(toExtrude.y() + toExtrude.height() - bounds.height());
61 
62  }
63 
64  Normalize(bounds);
65 
66  return bounds;
67 }
68 
69 void Geometry::Normalize(QRect& rect)
70 {
71  if (rect.width() < 0)
72  {
73  rect.setWidth(-rect.width());
74  rect.setX(-rect.width());
75  }
76 
77  if (rect.height() < 0)
78  {
79  rect.setHeight(-rect.height());
80  rect.setY(-rect.height());
81  }
82 }
83 
84 int Geometry::GetClosestSide(const QRect& boundary, const QPoint& toTest)
85 {
86  int sides[] =
88 
89  int closestSide = Constants::LEFT;
90  int closestDistance = std::numeric_limits<int>::max();
91 
92  for (auto side : sides)
93  {
94 
95 
96  int distance = GetDistanceFromEdge(boundary, toTest, side);
97 
98  if (distance < closestDistance)
99  {
100  closestDistance = distance;
101  closestSide = side;
102  }
103  }
104 
105  return closestSide;
106 }
107 
108 int Geometry::GetDistanceFromEdge(const QRect& rectangle,
109  const QPoint& testPoint, int edgeOfInterest)
110 {
111  if (edgeOfInterest == Constants::TOP)
112  return testPoint.y() - rectangle.y();
113  else if (edgeOfInterest == Constants::BOTTOM)
114  return rectangle.y() + rectangle.height() - testPoint.y();
115  else if (edgeOfInterest == Constants::LEFT)
116  return testPoint.x() - rectangle.x();
117  else if (edgeOfInterest == Constants::RIGHT)
118  return rectangle.x() + rectangle.width() - testPoint.x();
119 
120  return 0;
121 }
122 
123 int Geometry::GetOppositeSide(int directionConstant)
124 {
125  if (directionConstant == Constants::TOP)
126  return Constants::BOTTOM;
127  else if (directionConstant == Constants::BOTTOM)
128  return Constants::TOP;
129  else if (directionConstant == Constants::LEFT)
130  return Constants::RIGHT;
131  else if (directionConstant == Constants::RIGHT)
132  return Constants::LEFT;
133 
134  return directionConstant;
135 }
136 
137 QRect Geometry::ToControl(QWidget* coordinateSystem,
138  const QRect& toConvert)
139 {
140  return QRect(coordinateSystem->mapFromGlobal(toConvert.topLeft()),
141  coordinateSystem->mapFromGlobal(toConvert.bottomRight()));
142 }
143 
144 QPoint Geometry::ToControl(QWidget* coordinateSystem, const QPoint& toConvert)
145 {
146  return coordinateSystem->mapFromGlobal(toConvert);
147 }
148 
149 QRect Geometry::ToDisplay(QWidget* coordinateSystem,
150  const QRect& toConvert)
151 {
152  return QRect(coordinateSystem->mapToGlobal(toConvert.topLeft()),
153  coordinateSystem->mapToGlobal(toConvert.bottomRight()));
154 }
155 
156 QPoint Geometry::ToDisplay(QWidget* coordinateSystem, const QPoint& toConvert)
157 {
158  return coordinateSystem->mapToGlobal(toConvert);
159 }
160 
161 }
162 
static const int LEFT
static int GetDimension(const QRect &toMeasure, bool width)
static const int RIGHT
static const int BOTTOM
static int GetDistanceFromEdge(const QRect &rectangle, const QPoint &testPoint, int edgeOfInterest)
static QRect ToDisplay(QWidget *coordinateSystem, const QRect &toConvert)
static QRect ToControl(QWidget *coordinateSystem, const QRect &toConvert)
static int GetClosestSide(const QRect &boundary, const QPoint &toTest)
static QRect GetExtrudedEdge(const QRect &toExtrude, int size, int orientation)
static int GetOppositeSide(int directionConstant)
static T max(T x, T y)
Definition: svm.cpp:56
static void Normalize(QRect &rect)
static const int TOP
static bool IsHorizontal(int berrySideConstant)