iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RotatedRectangle.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_GEOMETRY_ROTATEDRECTANGLE_H
37 #define INCLUDE_NRT_CORE_GEOMETRY_ROTATEDRECTANGLE_H
38 
39 #include <nrt/Core/Geometry/Dims.H>
41 #include <nrt/External/cereal/access.hpp>
42 
43 namespace nrt
44 {
45  //! A 2D rotated rectangle containing a center point, dimensions, and an angle
46  /*! \ingroup geometry */
47  template <class T>
49  {
50  public:
51  //! Default constructor
52  explicit RotatedRectangle() = default;
53 
54  //! Construct a RotatedRectangle from a center point, dimensions, and an angle (in radians)
55  explicit RotatedRectangle(Point2D<T> const & center, Dims<T> const & dims, double const & angle);
56 
57  //! Copy Constructor
58  RotatedRectangle(RotatedRectangle<T> const & other) = default;
59 
60  //! Move constructor
61  RotatedRectangle(RotatedRectangle<T> && other) = default;
62 
63  //! Copy from a RotatedRectangle of a different datatype
64  /*! This will internally use nrt::clamped_convert<T,U> on the center and dims */
65  template <class U>
66  explicit RotatedRectangle(RotatedRectangle<U> const & other);
67 
68  //! Assignment
69  RotatedRectangle<T> & operator=(RotatedRectangle<T> const & other) = default;
70 
71  //! Move assignment
73 
74  //! Get the center point
75  Point2D<T> center() const;
76 
77  //! Get the dimensions
78  Dims<T> dims() const;
79 
80  //! Get the angle (in radians)
81  double angle() const;
82 
83  //! Compute the four vertices
84  std::vector<Point2D<T> > vertices() const;
85 
86  //! Find the RotatedRectangle with the minimum area which encloses a convex Polygon
87  /*! This method uses the algorithm outlined in "A Linear Time Algorithm for the Minimum Area Rectangle Enclosing a
88  Convex Polygon" by Arnon & Gieselmann, 1983. The algorithm works by using the "Rotating Calipers" method,
89  which exploits a proof showing that the minimum area enclosing rectangle will have one edge which meets an
90  edge of the Polygon.
91 
92  The returned rectangle will have an angle between 0-pi, and it's width will always be at least as large as
93  it's height.
94 
95  \note The given Polygon <i>must</i> be convex, otherwise the calculated RotatedRectangle will be incorrect. */
96  static RotatedRectangle<T> minEnclosingRectangle(Polygon<T> const & convexPoly);
97 
98  private:
99  Point2D<T> itsCenter;
100  Dims<T> itsDims;
101  double itsAngle;
102 
103  //! Serialization
104  template <class Archive>
105  void serialize(Archive& ar)
106  {
107  ar( itsCenter, itsDims, itsAngle );
108  }
109  friend class cereal::access;
110  };
111 
112 
113  //! Human-readable output to a stream: outputs [Center: xxx, Dims: xxx, Amgle: xxx]
114  /*! \relates RotatedRectangle */
115  template <class T>
116  std::ostream& operator<<(std::ostream &out, RotatedRectangle<T> const & r);
117 
118  //! Return a scaled version of the source object
119  /*! \note This does \e not have the same effect as the multiplication or division operators, which scale everything,
120  while here we only scale the object size without scaling its center coordinates.
121  \relates RotatedRectangle */
122  template <class T>
123  RotatedRectangle<T> scale(RotatedRectangle<T> const & src, double const factor);
124 
125  //! Return a rotated version of the source object, about its center by a given angle in radians
126  /*! \relates RotatedRectangle */
127  template <class T>
128  RotatedRectangle<T> rotate(RotatedRectangle<T> const & src, double const angle);
129 
130  //! Return a rotated version of the source object, about the given point and by a given angle in radians
131  /*! \relates RotatedRectangle */
132  template <class T>
133  RotatedRectangle<T> rotateAbout(RotatedRectangle<T> const & src, Point2D<T> const & p, double const angle);
134 
135  //! Operator overload for RotatedRectangle<T> == RotatedRectangle<T>
136  /*! \relates RotatedRectangle */
137  template <class T>
138  bool operator==(RotatedRectangle<T> const & lhs, RotatedRectangle<T> const & rhs);
139 
140  //! Operator overload for RotatedRectangle<T> != RotatedRectangle<T>
141  /*! \relates RotatedRectangle */
142  template <class T>
143  bool operator!=(RotatedRectangle<T> const & lhs, RotatedRectangle<T> const & rhs);
144 
145  //! Operator overload for RotatedRectangle<T> + Point2D<T>
146  /*! \relates RotatedRectangle */
147  template <class T>
148  RotatedRectangle<T> operator+(RotatedRectangle<T> const & lhs, Point2D<T> const & rhs);
149 
150  //! Operator overload for Point2D<T> + RotatedRectangle<T>
151  /*! \relates RotatedRectangle */
152  template <class T>
153  RotatedRectangle<T> operator+(Point2D<T> const & lhs, RotatedRectangle<T> const & rhs);
154 
155  //! Operator overload for RotatedRectangle<T> - Point2D<T>
156  /*! \relates RotatedRectangle */
157  template <class T>
158  RotatedRectangle<T> operator-(RotatedRectangle<T> const & lhs, nrt::Point2D<T> const & rhs);
159 
160  //! Operator overload for Point2D<T> - RotatedRectangle<T>
161  /*! \relates RotatedRectangle */
162  template <class T>
163  RotatedRectangle<T> operator-(Point2D<T> const & lhs, RotatedRectangle<T> const & rhs);
164 
165  //! Operator overload for RotatedRectangle<T> * double
166  /*! \relates RotatedRectangle */
167  template <class T>
168  RotatedRectangle<T> operator*(RotatedRectangle<T> const & lhs, double const rhs);
169 
170  //! Operator overload for double * RotatedRectangle<T>
171  /*! \relates RotatedRectangle */
172  template <class T>
173  RotatedRectangle<T> operator*(double const lhs, RotatedRectangle<T> const & rhs);
174 
175  //! Operator overload for RotatedRectangle<T> / double
176  /*! \relates RotatedRectangle */
177  template <class T>
178  RotatedRectangle<T> operator/(RotatedRectangle<T> const & lhs, double const rhs);
179 
180  //! Operator overload for RotatedRectangle<T> *= double
181  /*! \relates RotatedRectangle */
182  template <class T>
183  RotatedRectangle<T> & operator*=(RotatedRectangle<T> & lhs, double const rhs);
184 
185  //! Operator overload for RotatedRectangle<T> /= double
186  /*! \relates RotatedRectangle */
187  template <class T>
188  RotatedRectangle<T> & operator/=(RotatedRectangle<T> & lhs, double const rhs);
189 
190  //! Operator overload for RotatedRectangle<T> += Point2D<T>
191  /*! \relates RotatedRectangle */
192  template <class T>
193  RotatedRectangle<T> & operator+=(RotatedRectangle<T> & lhs, Point2D<T> const & rhs);
194 
195  //! Operator overload for RotatedRectangle<T> -= Point2D<T>
196  /*! \relates RotatedRectangle */
197  template <class T>
198  RotatedRectangle<T> & operator-=(RotatedRectangle<T> & lhs, Point2D<T> const & rhs);
199 
200 }
201 
203 
204 #endif // INCLUDE_NRT_CORE_GEOMETRY_ROTATEDRECTANGLE_H