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