vgl_oriented_box_2d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_oriented_box_2d.h
2 #ifndef vgl_oriented_box_2d_h_
3 #define vgl_oriented_box_2d_h_
4 
5 #include <iostream>
6 #include <vector>
7 #include <map>
9 #include <vgl/vgl_polygon.h>
10 #include <vgl/vgl_point_2d.h>
11 #include <vgl/vgl_tolerance.h>
12 //:
13 // \file
14 // \brief An orienteded box in 2-d
15 // \author J.L. Mundy
16 // \date April 18, 2017
17 //
18 
19 template <class T>
20 class vgl_oriented_box_2d {
21  public:
22  //:default constructor
24 
25  //:construct from center, width (major axis), height and orientedation with respect to x-axis
26  // rotation is around the center point. width must be greater than height!!
28 
29  //:construct from major axis and height
30  // the center is the midpoint of the major axis
31  vgl_oriented_box_2d(vgl_point_2d<T> const& maj_p1, vgl_point_2d<T> const& maj_p2, T height){
32  major_axis_.set(maj_p1, maj_p2); half_height_ = height/T(2);}
33 
35 
36  //: the minor axis line need not be orthogonal to the major axis, and
37  // the major axis passes through the midpoint of the minor line segment
38  // box height is defined by the projection of the minor line segment onto the perpendicular to the major axis
39  vgl_oriented_box_2d(vgl_point_2d<T> const& maj_p1, vgl_point_2d<T> const& maj_p2,
40  vgl_point_2d<T> const& min_p1, vgl_point_2d<T> const& min_p2);
41 
43  *this = vgl_oriented_box_2d<T>(maj.point1(), maj.point2(), min.point1(), min.point2());
44  }
45 
46  //: construct from an axis aligned box
48 
49  //: construct from an axis aligned box with orientedation
50  // the major axis of the box is rotated by angle_in_rad around the centroid
52 
53  //: constructor from three corner points.
54  // The two directions from p0 to the two other points must be
55  // mutually orthogonal, i.e., a local box coordinate frame centered on p0
56  vgl_oriented_box_2d(vgl_point_2d<T> const& p0, vgl_point_2d<T> const& p1, vgl_point_2d<T> const& p2);
57 
58  //: major axis
60 
61  //: center (midpoint of major axis)
62  vgl_point_2d<T> centroid() const;
63 
64  //: return width (first) and height (second)
65  std::pair<T, T> width_height() const;
66 
67  //: width - length of major axis
68  T width() const {std::pair<T, T> p = this->width_height(); return p.first;}
69 
70  //: height - length of minor axis
71  T height() const {std::pair<T, T> p = this->width_height(); return p.second;}
72 
73  //: aspsect ratio width/height
74  T aspect_ratio() const {return width()/height();}
75 
76  //:: orientedation of major axis on the range +- pi (as returned by atan2)
77  T angle_in_rad() const;
78 
79  bool operator==(vgl_oriented_box_2d<T> const& ob) const {
80  return (this == &ob) || (ob.major_axis_ == this->major_axis_ && ob.half_height_ == this->half_height_);
81  }
82  bool near_equal(vgl_oriented_box_2d<T> const& ob, T tol = vgl_tolerance<T>::position) const;
83 
84  //: Return true if (x,y) is inside this box
85  bool contains(T const& x, T const& y) const{return this->contains(vgl_point_2d<T>(x, y));}
86 
87  //: Return true if point is inside this box
88  bool contains(vgl_point_2d<T> const& p) const;
89 
90 
91  void set(vgl_line_segment_2d<T> const& major, T half_height){
92  major_axis_ = major;
93  half_height_ = half_height;
94  }
95 
96  //: axis-aligned bounding box for *this
98 
99  //: corners of the orienteded rectangle
100  std::vector<vgl_point_2d<T> > corners() const;
101 
102  //: polygon corresponding to obox boundary
104  std::vector<vgl_point_2d<T> > corns = this->corners();
105  return vgl_polygon<T>(corns);
106  }
107  //: map a point to the (u, v) coordinate system of the orienteded box
109 
110  //:rotate by angle in radians
111  vgl_oriented_box_2d<T> rotate(vgl_point_2d<T> const& rot_center,T angle_rad) const;
112 
113  //:translate by displacement (tx, ty)
114  vgl_oriented_box_2d<T> translate(T tx, T ty) const;
115  private:
118 };
119 template <class T>
120 std::ostream& operator<<(std::ostream& os, const vgl_oriented_box_2d<T>& obox);
121 template <class T>
122 std::istream& operator>>(std::istream& is, vgl_oriented_box_2d<T>& obox);
123 
124 #define VGL_ORIENTED_BOX_2D_INSTANTIATE(T) extern "please include vgl/vgl_point_2d.hxx first"
125 
126 #endif // vgl_oriented_box_2d_h_
T angle_in_rad() const
: orientedation of major axis on the range +- pi (as returned by atan2).
a point in 2D nonhomogeneous space
vgl_oriented_box_2d(vgl_line_segment_2d< T > const &maj, T height)
vgl_oriented_box_2d()
default constructor.
vgl_oriented_box_2d< T > translate(T tx, T ty) const
translate by displacement (tx, ty).
vgl_point_2d< Type > point2() const
The other end-point of the line segment.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
std::pair< T, T > width_height() const
return width (first) and height (second).
vgl_box_2d< T > enclosing_box() const
axis-aligned bounding box for *this.
T width() const
width - length of major axis.
vgl_oriented_box_2d(vgl_point_2d< T > const &maj_p1, vgl_point_2d< T > const &maj_p2, T height)
construct from major axis and height.
T height() const
height - length of minor axis.
bool contains(T const &x, T const &y) const
Return true if (x,y) is inside this box.
void set(vgl_line_segment_2d< T > const &major, T half_height)
vgl_point_2d< T > centroid() const
center (midpoint of major axis).
std::vector< vgl_point_2d< T > > corners() const
corners of the orienteded rectangle.
vgl_line_segment_2d< T > major_axis_
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
T aspect_ratio() const
aspsect ratio width/height.
vgl_line_segment_2d< T > major_axis() const
major axis.
bool near_equal(vgl_oriented_box_2d< T > const &ob, T tol=vgl_tolerance< T >::position) const
void set(vgl_point_2d< Type > const &p1, vgl_point_2d< Type > const &p2)
Assignment.
vgl_oriented_box_2d(vgl_line_segment_2d< T > const &maj, vgl_line_segment_2d< T > const &min)
vgl_point_2d< T > transform_to_obox(vgl_point_2d< T > const &p) const
map a point to the (u, v) coordinate system of the orienteded box.
Store a polygon.
Definition: vgl_area.h:6
vgl_point_2d< Type > point1() const
One end-point of the line segment.
vgl_polygon< T > obox_boundary() const
polygon corresponding to obox boundary.
vgl_oriented_box_2d< T > rotate(vgl_point_2d< T > const &rot_center, T angle_rad) const
rotate by angle in radians.
bool operator==(vgl_oriented_box_2d< T > const &ob) const