Visual Servoing Platform  version 3.6.1 under development (2023-11-30)
vpRectOriented.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Defines a (possibly oriented) rectangle in the plane.
33  *
34 *****************************************************************************/
35 #include <visp3/core/vpRectOriented.h>
36 
37 #include <cmath>
38 
41  : m_center(), m_width(), m_height(), m_theta(), m_topLeft(), m_topRight(), m_bottomLeft(), m_bottomRight()
42 { }
43 
50 vpRectOriented::vpRectOriented(const vpImagePoint &center, double width, double height, double theta)
51 {
52  m_center = center;
53  m_width = width;
54  m_height = height;
55  m_theta = theta;
56  m_topLeft.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2.0);
57  m_topLeft.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2.0);
58  m_bottomLeft.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2.0);
59  m_bottomLeft.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2.0);
60  m_bottomRight.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2.0);
61  m_bottomRight.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2.0);
62  m_topRight.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2.0);
63  m_topRight.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2.0);
64 }
65 
70 {
71  m_center = rect.getCenter();
72  m_width = rect.getWidth();
73  m_height = rect.getHeight();
74  m_theta = .0;
75  m_topLeft.set_i(m_center.get_i() - m_height / 2.0);
76  m_topLeft.set_j(m_center.get_j() - m_width / 2.0);
77  m_bottomLeft.set_i(m_center.get_i() + m_height / 2.0);
78  m_bottomLeft.set_j(m_center.get_j() - m_width / 2.0);
79  m_bottomRight.set_i(m_center.get_i() + m_height / 2.0);
80  m_bottomRight.set_j(m_center.get_j() + m_width / 2.0);
81  m_topRight.set_i(m_center.get_i() - m_height / 2.0);
82  m_topRight.set_j(m_center.get_j() + m_width / 2.0);
83 }
84 
89 {
90  m_center = rect.getCenter();
91  m_width = rect.getWidth();
92  m_height = rect.getHeight();
93  m_theta = .0;
94  m_topLeft.set_i(m_center.get_i() - m_height / 2.0);
95  m_topLeft.set_j(m_center.get_j() - m_width / 2.0);
96  m_bottomLeft.set_i(m_center.get_i() + m_height / 2.0);
97  m_bottomLeft.set_j(m_center.get_j() - m_width / 2.0);
98  m_bottomRight.set_i(m_center.get_i() + m_height / 2.0);
99  m_bottomRight.set_j(m_center.get_j() + m_width / 2.0);
100  m_topRight.set_i(m_center.get_i() - m_height / 2.0);
101  m_topRight.set_j(m_center.get_j() + m_width / 2.0);
102  return *this;
103 }
104 
107 vpRectOriented::operator vpRect()
108 {
109  if (std::fabs(m_theta) > std::numeric_limits<double>::epsilon())
110  throw(vpException(vpException::badValue, "Cannot convert a vpRectOriented with non-zero orientation to a vpRect"));
111 
112  return vpRect(m_topLeft, m_bottomRight);
113 }
114 
119 void vpRectOriented::setPoints(const vpImagePoint &topLeft, const vpImagePoint &topRight,
120  const vpImagePoint &bottomLeft, const vpImagePoint &bottomRight)
121 {
122  m_topLeft = topLeft;
123  m_bottomLeft = bottomLeft;
124  m_bottomRight = bottomRight;
125  m_topRight = topRight;
126  m_center.set_i((m_topLeft.get_i() + m_bottomLeft.get_i() + m_bottomRight.get_i() + m_topRight.get_i()) / 4.0);
127  m_center.set_j((m_topLeft.get_j() + m_bottomLeft.get_j() + m_bottomRight.get_j() + m_topRight.get_j()) / 4.0);
128  m_width = sqrt((m_topRight.get_i() - m_topLeft.get_i()) * (m_topRight.get_i() - m_topLeft.get_i()) +
129  (m_topRight.get_j() - m_topLeft.get_j()) * (m_topRight.get_j() - m_topLeft.get_j()));
130  m_height = sqrt((m_bottomLeft.get_i() - m_topLeft.get_i()) * (m_bottomLeft.get_i() - m_topLeft.get_i()) +
131  (m_bottomLeft.get_j() - m_topLeft.get_j()) * (m_bottomLeft.get_j() - m_topLeft.get_j()));
132  m_theta = atan2(m_topRight.get_i() - m_topLeft.get_i(), m_topRight.get_j() - m_topLeft.get_j());
133 }
134 
137 {
138  m_topLeft += center - m_center;
139  m_bottomLeft += center - m_center;
140  m_bottomRight += center - m_center;
141  m_topRight += center - m_center;
142  m_center = center;
143 }
144 
146 vpImagePoint vpRectOriented::getCenter() const { return m_center; }
147 
149 vpImagePoint vpRectOriented::getTopLeft() const { return m_topLeft; }
150 
152 vpImagePoint vpRectOriented::getTopRight() const { return m_topRight; }
153 
155 vpImagePoint vpRectOriented::getBottomLeft() const { return m_bottomLeft; }
156 
158 vpImagePoint vpRectOriented::getBottomRight() const { return m_bottomRight; }
159 
161 void vpRectOriented::setSize(double width, double height)
162 {
163  m_width = width;
164  m_height = height;
165  m_topLeft.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2);
166  m_topLeft.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2);
167  m_bottomLeft.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2);
168  m_bottomLeft.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2);
169  m_bottomRight.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2);
170  m_bottomRight.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2);
171  m_topRight.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2);
172  m_topRight.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2);
173 }
174 
176 double vpRectOriented::getWidth() const { return m_width; }
177 
179 double vpRectOriented::getHeight() const { return m_height; }
180 
183 {
184  m_theta = theta;
185  m_topLeft.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2);
186  m_topLeft.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2);
187  m_bottomLeft.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 - m_width * sin(m_theta) / 2);
188  m_bottomLeft.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 - m_width * cos(m_theta) / 2);
189  m_bottomRight.set_i(m_center.get_i() + m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2);
190  m_bottomRight.set_j(m_center.get_j() - m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2);
191  m_topRight.set_i(m_center.get_i() - m_height * cos(m_theta) / 2.0 + m_width * sin(m_theta) / 2);
192  m_topRight.set_j(m_center.get_j() + m_height * sin(m_theta) / 2.0 + m_width * cos(m_theta) / 2);
193 }
194 
196 double vpRectOriented::getOrientation() const { return m_theta; }
197 
199 bool vpRectOriented::isInside(const vpImagePoint &point) const
200 {
201  if (!isLeft(point, m_topLeft, m_bottomLeft))
202  return false;
203  if (!isLeft(point, m_bottomLeft, m_bottomRight))
204  return false;
205  if (!isLeft(point, m_bottomRight, m_topRight))
206  return false;
207  if (!isLeft(point, m_topRight, m_topLeft))
208  return false;
209  return true;
210 }
211 
212 bool vpRectOriented::isLeft(const vpImagePoint &pointToTest, const vpImagePoint &point1,
213  const vpImagePoint &point2) const
214 {
215  double a = point1.get_j() - point2.get_j();
216  double b = point2.get_i() - point1.get_i();
217  double c = -(a * point1.get_i() + b * point1.get_j());
218  double d = a * pointToTest.get_i() + b * pointToTest.get_j() + c;
219  return (d > 0);
220 }
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:294
double get_j() const
Definition: vpImagePoint.h:125
void set_i(double ii)
Definition: vpImagePoint.h:283
double get_i() const
Definition: vpImagePoint.h:114
Defines an oriented rectangle in the plane.
double getHeight() const
Get the rectangle height.
vpImagePoint getBottomLeft() const
Get the bottom-left corner.
double getOrientation() const
Get the rectangle orientation (rad).
vpImagePoint getBottomRight() const
Get the bottom-right corner.
void setPoints(const vpImagePoint &topLeft, const vpImagePoint &topRight, const vpImagePoint &bottomLeft, const vpImagePoint &bottomRight)
void setCenter(const vpImagePoint &center)
Set the center of the rectangle.
double getWidth() const
Get the rectangle width.
bool isInside(const vpImagePoint &point) const
Check whether the point is inside the rectangle.
vpRectOriented()
Default constructor.
void setSize(double width, double height)
Set the size of the rectangle : performs a homothety relatively to the rectangle center.
vpImagePoint getTopRight() const
Get the top-right corner.
vpImagePoint getTopLeft() const
Get the top-left corner.
vpImagePoint getCenter() const
Get the rectangle center point.
vpRectOriented & operator=(const vpRectOriented &rect)=default
void setOrientation(double theta)
Set the rectangle orientation (rad).
Defines a rectangle in the plane.
Definition: vpRect.h:76
void getCenter(double &x, double &y) const
Definition: vpRect.h:133
double getWidth() const
Definition: vpRect.h:224
double getHeight() const
Definition: vpRect.h:163