KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_2D_bounding_box.h
Go to the documentation of this file.
1 /* =========================================================================== *
2 |
3 | Copyright (c) 1994-2008 by Kobus Barnard (author).
4 |
5 | Personal and educational use of this code is granted, provided that this
6 | header is kept intact, and that the authorship is not misrepresented, that
7 | its use is acknowledged in publications, and relevant papers are cited.
8 |
9 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10 |
11 | Please note that the code in this file has not necessarily been adequately
12 | tested. Naturally, there is no guarantee of performance, support, or fitness
13 | for any particular task. Nonetheless, I am interested in hearing about
14 | problems that you encounter.
15 |
16 | Authors:
17 | Joseph Schlecht, Luca Del Pero, Ernesto Brau
18 |
19 * =========================================================================== */
20 
30 #ifndef KJB_AA_RECTANGLE_2D_H
31 #define KJB_AA_RECTANGLE_2D_H
32 
33 #include <m_cpp/m_vector.h>
34 #include <l_cpp/l_functors.h>
35 
36 #include <iosfwd>
37 
38 #include <algorithm>
39 #include <vector>
40 
41 namespace kjb {
42 class Base_gl_interface;
43 class Image;
44 
52 {
54 public:
55 
57  Axis_aligned_rectangle_2d(const Vector& center = Vector(2, 0.0), double width = 1.0, double height = 1.0) :
58  m_center(center),
59  m_width(width),
60  m_height(height)
61  {}
62 
64  Axis_aligned_rectangle_2d(const Vector& p1, const Vector& p2) :
65  m_center(0.5 * (p1 + p2)),
66  m_width(fabs(p1[0] - p2[0])),
67  m_height(fabs(p1[1] - p2[1]))
68  {}
69 
72  {
73  return new Axis_aligned_rectangle_2d(*this);
74  }
75 
78 
80  const Vector& get_center() const
81  {
82  return m_center;
83  }
84 
86  double get_width() const
87  {
88  return m_width;
89  }
90 
92  double get_height() const
93  {
94  return m_height;
95  }
96 
97  double get_left() const { return m_center[0] - m_width / 2.0; }
98  double get_right() const { return m_center[0] + m_width / 2.0; }
99  double get_bottom() const { return m_center[1] - m_height / 2.0; }
100  double get_top() const { return m_center[1] + m_height/ 2.0; }
101 
102  Vector get_top_left() const { return m_center + Vector(-m_width / 2.0, m_height / 2.0); }
103  Vector get_bottom_right() const { return m_center + Vector(m_width / 2.0, -m_height / 2.0); }
104  Vector get_top_center() const { return m_center + Vector(0.0, m_height / 2.0); }
105  Vector get_bottom_center() const { return m_center + Vector(0.0, -m_height / 2.0); }
106 
107 
109  void set_center(const Vector& center)
110  {
111  m_center = center;
112  }
113 
115  void set_width(double width)
116  {
117  m_width = width;
118  }
119 
121  void set_height(double height)
122  {
123  m_height = height;
124  }
125 
126  /*===================================================================== *
127  * RENDERING *
128  *===================================================================== */
129 
130  void wire_render() const;
131 
132  /*===================================================================== *
133  * TESTS *
134  *===================================================================== */
135 
136 
138  bool contains(const kjb::Vector& pt) const
139  {
140  assert(pt.size() == 2);
141 
142  return pt[0] >= get_left() && pt[0] <= get_right() &&
143  pt[1] >= get_bottom() && pt[1] <= get_top();
144  }
145 
146  bool intersects(const Self& other) const
147  {
148  // probably could rewrite this for quicker short-circuiting...
149  return !(other.get_left() >= get_right() ||
150  other.get_right() <= get_left() ||
151  other.get_bottom() >= get_top() ||
152  other.get_top() <= get_bottom());
153  }
154 
155  /*===================================================================== *
156  * OTHER *
157  *===================================================================== */
158 
159  void draw(kjb::Image & img, double ir = 255, double ig = 0, double ib = 0, double iwidth = 1.0) const;
160 
161  void write_corners_on(std::ostream& ofs);
162 
167  {
168  using std::swap;
169  swap(m_center, r.m_center);
170  swap(m_width, r.m_width);
171  swap(m_height, r.m_height);
172  }
173 
174 
175  /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
176  /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
177  /*===================================================================== *
178  * SUPERFLUOUS METHODS -- MIGHT DISAPPEAR *
179  *===================================================================== */
180  /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
181  /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
182 
183 
185  // Superflous...should disappear.
186  Axis_aligned_rectangle_2d(double center_x, double center_y, double iwidth, double iheight) :
187  m_center(Vector().set(center_x, center_y)),
188  m_width(iwidth),
189  m_height(iheight)
190  {}
191 
193  const Vector& get_centre() const
194  {
195  return get_center();
196  }
197 
199  double get_centre_x() const
200  {
201  return m_center(0);
202  }
203 
205  double get_centre_y() const
206  {
207  return m_center(1);
208  }
209 
211  double get_area() const
212  {
213  return m_width*m_height;
214  }
215 
217  void set_centre(const Vector & icenter)
218  {
219  set_center(icenter);
220  }
221 
223  void set_centre_x(double center_x)
224  {
225  m_center(0) = center_x;
226  }
227 
229  void set_centre_y(double center_y)
230  {
231  m_center(1) = center_y;
232  }
233 
234 friend std::ostream& operator<<(std::ostream& ost, const Axis_aligned_rectangle_2d& box);
235 friend std::istream& operator>>(std::istream& ist, Axis_aligned_rectangle_2d& box);
236 
237 private:
238  Vector m_center;
239  double m_width;
240  double m_height;
241 
242 };
243 
244 std::ostream& operator<<(std::ostream& ost, const Axis_aligned_rectangle_2d& box);
245 
246 std::istream& operator>>(std::istream& ist, Axis_aligned_rectangle_2d& box);
247 
249 
254 
259 {
260  kjb::Vector c = box.get_center();
261  box.set_center(c + t);
262 }
263 
272 
279 (
280  Axis_aligned_rectangle_2d& bb,
281  const std::vector<Vector>& points,
282  const Base_gl_interface& camera,
283  double img_width,
284  double img_height
285 );
286 
296 template<class Iterator>
297 inline
298 Axis_aligned_rectangle_2d compute_bounding_box(Iterator first, Iterator last)
299 {
300  Vector left = *std::min_element(first, last, Index_less_than<Vector>(0));
301  Vector right = *std::max_element(first, last, Index_less_than<Vector>(0));
302  Vector bottom = *std::min_element(first, last, Index_less_than<Vector>(1));
303  Vector top = *std::max_element(first, last, Index_less_than<Vector>(1));
304 
305  return Axis_aligned_rectangle_2d(Vector().set((left[0] + right[0]) / 2.0, (bottom[1] + top[1]) / 2.0), right[0] - left[0], top[1] - bottom[1]);
306 }
307 
312 (
313  const kjb::Bounding_Box2D& b1,
314  const kjb::Bounding_Box2D& b2
315 );
316 
320 inline
322 {
323  r1.swap(r2);
324 }
325 
326 } // namespace kjb
327 
328 #endif
329 
double get_width() const
returns the width of this bounding box
Definition: gr_2D_bounding_box.h:86
const Vector & get_centre() const
returns the center of this Axis_aligned_rectangle_2d
Definition: gr_2D_bounding_box.h:193
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
Class that represents an axis-aligned 2D rectangle. It is defined in terms of its (2D) center...
Definition: gr_2D_bounding_box.h:51
void translate(kjb::Axis_aligned_rectangle_2d &box, const kjb::Vector &t)
Definition: gr_2D_bounding_box.h:258
bool contains(const kjb::Vector &pt) const
Definition: gr_2D_bounding_box.h:138
Axis_aligned_rectangle_2d(const Vector &p1, const Vector &p2)
Constructs a Axis_aligned_rectangle_2d.
Definition: gr_2D_bounding_box.h:64
double get_centre_x() const
returns the x-coordinate of the center of this Axis_aligned_rectangle_2d
Definition: gr_2D_bounding_box.h:199
void set_centre_y(double center_y)
sets the y-coordinate of the center of this bounding box
Definition: gr_2D_bounding_box.h:229
Axis_aligned_rectangle_2d Bounding_Box2D
Definition: gr_2D_bounding_box.h:248
void set_centre(const Vector &icenter)
sets the center of this bounding box
Definition: gr_2D_bounding_box.h:217
Predicate that compares the kth element of a indexable type.
Definition: l_functors.h:94
double get_height() const
returns the height of this bounding box
Definition: gr_2D_bounding_box.h:92
double get_bottom() const
Definition: gr_2D_bounding_box.h:99
double get_area() const
returns the area of the box
Definition: gr_2D_bounding_box.h:211
double get_right() const
Definition: gr_2D_bounding_box.h:98
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
height
Definition: APPgetLargeConnectedEdges.m:33
r
Definition: APPgetLargeConnectedEdges.m:127
friend std::istream & operator>>(std::istream &ist, Axis_aligned_rectangle_2d &box)
Definition: gr_2D_bounding_box.cpp:249
Vector get_top_center() const
Definition: gr_2D_bounding_box.h:104
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void draw(kjb::Image &img, double ir=255, double ig=0, double ib=0, double iwidth=1.0) const
Definition: gr_2D_bounding_box.cpp:57
Bounding_Box2D intersect(const Bounding_Box2D &b1, const Bounding_Box2D &b2)
Definition: gr_2D_bounding_box.cpp:263
std::istream & operator>>(std::istream &ist, Detection_type &type)
Stream in an detection.
Definition: d_type.cpp:91
void set_height(double height)
sets the height of this bounding box
Definition: gr_2D_bounding_box.h:121
bool intersects(const Self &other) const
Definition: gr_2D_bounding_box.h:146
const Vector & get_center() const
returns the center of this Axis_aligned_rectangle_2d
Definition: gr_2D_bounding_box.h:80
Axis_aligned_rectangle_2d * clone() const
Clones this Axis_aligned_rectangle_2d.
Definition: gr_2D_bounding_box.h:71
void wire_render() const
Definition: gr_2D_bounding_box.cpp:37
void set_centre_x(double center_x)
sets the x-coordinate of the center of this bounding box
Definition: gr_2D_bounding_box.h:223
std::ofstream & operator<<(std::ofstream &out, const Quaternion &q)
Definition: turntable_camera.cpp:77
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
double get_left() const
Definition: gr_2D_bounding_box.h:97
Vector get_bottom_right() const
Definition: gr_2D_bounding_box.h:103
void write_corners_on(std::ostream &ofs)
Definition: gr_2D_bounding_box.cpp:103
double get_top() const
Definition: gr_2D_bounding_box.h:100
void set_width(double width)
sets the width of this bounding box
Definition: gr_2D_bounding_box.h:115
Axis_aligned_rectangle_2d compute_bounding_box(Iterator first, Iterator last)
Computes the 2D bounding box of a range of 2D points.
Definition: gr_2D_bounding_box.h:298
Vector get_top_left() const
Definition: gr_2D_bounding_box.h:102
~Axis_aligned_rectangle_2d()
Deletes this Axis_aligned_rectangle_2d.
Definition: gr_2D_bounding_box.h:77
double get_centre_y() const
returns the y-coordinate of the center of this Axis_aligned_rectangle_2d
Definition: gr_2D_bounding_box.h:205
double get_rectangle_intersection(const kjb::Bounding_Box2D &b1, const kjb::Bounding_Box2D &b2)
Compute area of intersection of two rectangles.
Definition: gr_2D_bounding_box.cpp:282
friend std::ostream & operator<<(std::ostream &ost, const Axis_aligned_rectangle_2d &box)
Definition: gr_2D_bounding_box.cpp:232
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
Axis_aligned_rectangle_2d(double center_x, double center_y, double iwidth, double iheight)
Constructs a Axis_aligned_rectangle_2d.
Definition: gr_2D_bounding_box.h:186
void swap(Axis_aligned_rectangle_2d &r)
Swaps this rectangle with another.
Definition: gr_2D_bounding_box.h:166
void scale(kjb::Axis_aligned_rectangle_2d &box, const kjb::Vector &s)
Definition: gr_2D_bounding_box.cpp:108
struct memorypool points
Definition: triangle.c:637
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void get_projected_bbox_from_3Dpoints(Bounding_Box2D &bb, const std::vector< Vector > &points, const Base_gl_interface &camera, double img_width, double img_height)
Projects a set of 3D points onto the image plane, and finds a bounding box (aligned with the image ax...
Definition: gr_2D_bounding_box.cpp:138
Vector get_bottom_center() const
Definition: gr_2D_bounding_box.h:105
void set_center(const Vector &center)
sets the center of this bounding box
Definition: gr_2D_bounding_box.h:109
Axis_aligned_rectangle_2d(const Vector &center=Vector(2, 0.0), double width=1.0, double height=1.0)
Constructs a Axis_aligned_rectangle_2d.
Definition: gr_2D_bounding_box.h:57