vgl_rtree_c.h
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_rtree_c.h
2 #ifndef vgl_rtree_c_h_
3 #define vgl_rtree_c_h_
4 //:
5 // \file
6 // \brief C helper classes for vgl_rtree
7 // \author J.L. Mundy
8 // \date November 14, 2008
9 // \verbatim
10 // Modifications
11 // <None yet>
12 // \endverbatim
13 //
14 // vgl_rtree stores elements of type V with regions described by
15 // bounds type B. The C helper class implements the bounding predicates
16 // between V and B. Thus V and B remain independent of each other.
17 //
18 #include <vgl/vgl_point_2d.h>
19 #include <vgl/vgl_box_2d.h>
20 #include <vgl/vgl_polygon.h>
21 #include <vgl/vgl_intersection.h>
22 #include <vgl/vgl_area.h>
23 template <class V, class B, class C> class vgl_rtree_probe;
24 
25 //: vgl_rtree Class C for V=vgl_point_2d<T>, B = vgl_box_2d<T>
26 template <class T>
28 {
29  // only static methods
30  vgl_rtree_point_box_2d() = delete;
31  ~vgl_rtree_point_box_2d() = delete;
32 
33  public:
36  typedef T t_type;
37  // Operations------
38  static void init (vgl_box_2d<T>& b, vgl_point_2d<T> const& p)
39  { b = vgl_box_2d<T>(); b.add(p); }
40 
41  static void update(vgl_box_2d<T>& b, vgl_point_2d<T> const& p)
42  { b.add(p); }
43 
44  static void update(vgl_box_2d<T>& b0, vgl_box_2d<T> const &b1)
45  { b0.add(b1.min_point()); b0.add(b1.max_point()); }
46 
47  static bool meet(vgl_box_2d<T> const& b, vgl_point_2d<T> const& p)
48  { return b.contains(p); }
49 
50  static bool meet(vgl_box_2d<T> const& b0, vgl_box_2d<T> const& b1) {
51  vgl_box_2d<T> bint = vgl_intersection<T>(b0, b1);
52  return !bint.is_empty();
53  }
54 
55  static float volume(vgl_box_2d<T> const& b)
56  { return static_cast<float>(vgl_area(b)); }
57 
58  // point meets for a polygon, used by generic rtree probe
59  static bool meets(vgl_point_2d<T> const& v, vgl_polygon<T> poly)
60  { return poly.contains(v); }
61 
62  // box meets for a polygon, used by generic rtree probe
63  static bool meets(vgl_box_2d<T> const& b, vgl_polygon<T> poly)
64  { return vgl_intersection<T>(b, poly); }
65 };
66 
67 
68 //: vgl_rtree Class C for V=vgl_box_2d<T>, B = vgl_rbox_2d<T>
69 // Need to distinguish bounds type from stored element type,
70 // so create minimal subclass of vgl_box_2d
71 template <class Type>
72 class vgl_bbox_2d : public vgl_box_2d<Type>
73 {
74  public:
75  //: Default constructor (creates empty box)
76  vgl_bbox_2d() = default;
77 
78  //: Construct using two corner points
79  vgl_bbox_2d(Type const min_position[2],
80  Type const max_position[2])
81  : vgl_box_2d<Type>(min_position[2], max_position[2]) {}
82 
83  //: Construct using two corner points
86  : vgl_box_2d<Type>(min_pos, max_pos) {}
87 
88  //: Construct using ranges in \a x (first two args) and \a y (last two)
89  vgl_bbox_2d(Type xmin, Type xmax, Type ymin, Type ymax)
90  : vgl_box_2d<Type>(xmin, xmax, ymin, ymax) {}
91 
92  //: Equality test
93  inline bool operator==(vgl_bbox_2d<Type> const& b) const {
94  // All empty boxes are equal:
95  if (b.is_empty()) return this->is_empty();
96  return this->min_x() == b.min_x() && this->min_y() == b.min_y()
97  && this->max_x() == b.max_x() && this->max_y() == b.max_y();
98  }
99 };
100 
101 template <class T>
103 {
104  // only static methods
105  vgl_rtree_box_box_2d() = delete;
106  ~vgl_rtree_box_box_2d() = delete;
107 
108  public:
111  typedef T t_type;
112  // Operations------
113  static void init (vgl_bbox_2d<T>& b, vgl_box_2d<T> const& v)
114  { b = vgl_bbox_2d<T>(); b.add(v.min_point()); b.add(v.max_point()); }
115 
116  static void update(vgl_bbox_2d<T>& b, vgl_box_2d<T> const& v)
117  { b.add(v.min_point()); b.add(v.max_point()); }
118 
119  static void update(vgl_bbox_2d<T>& b0, vgl_bbox_2d<T> const &b1)
120  { b0.add(b1.min_point()); b0.add(b1.max_point()); }
121 
122  static bool meet(vgl_bbox_2d<T> const& b0, vgl_box_2d<T> const& v) {
123  bool resultf =(b0.contains(v.min_point()) || b0.contains(v.max_point()));
124  bool resultr =(v.contains(b0.min_point()) || v.contains(b0.max_point()));
125  return resultf||resultr;
126  }
127 
128  static bool meet(vgl_bbox_2d<T> const& b0, vgl_bbox_2d<T> const& b1) {
129  bool resultf =(b0.contains(b1.min_point()) || b0.contains(b1.max_point()));
130  bool resultr =(b1.contains(b0.min_point()) || b1.contains(b0.max_point()));
131  return resultf||resultr;
132  }
133 
134  static float volume(vgl_box_2d<T> const& b)
135  { return static_cast<float>(vgl_area(b)); }
136 
137  // box_2d meets for a polygon, used by generic rtree probe
138  static bool meets(vgl_box_2d<T> const& b, vgl_polygon<T> poly)
139  { return vgl_rtree_point_box_2d<T>::meets(b, poly); }
140 
141  static bool meets(vgl_bbox_2d<T> const& b, vgl_polygon<T> poly)
142  { return vgl_rtree_point_box_2d<T>::meets(b, poly); }
143 };
144 
145 template <class V, class B, class C>
147 {
148  typedef typename C::t_type T;
150  public:
152 
153  //: return true if the probe "meets" the given object.
154  bool meets(V const &v) const override {return C::meets(v, poly_); }
155  bool meets(B const &b) const override {return C::meets(b, poly_); }
156 };
157 
158 #endif // vgl_rtree_c_h_
Set of intersection functions.
static float volume(vgl_box_2d< T > const &b)
Definition: vgl_rtree_c.h:55
vgl_bbox_2d(vgl_point_2d< Type > const &min_pos, vgl_point_2d< Type > const &max_pos)
Construct using two corner points.
Definition: vgl_rtree_c.h:84
bool operator==(vgl_bbox_2d< Type > const &b) const
Equality test.
Definition: vgl_rtree_c.h:93
a point in 2D nonhomogeneous space
static float volume(vgl_box_2d< T > const &b)
Definition: vgl_rtree_c.h:134
bool contains(point_t const &p) const
Returns true if p(x,y) is inside the polygon, else false.
Definition: vgl_polygon.h:81
Type min_x() const
Get min x.
Definition: vgl_box_2d.h:132
T vgl_area(vgl_polygon< T > const &poly)
The area of a polygon.
Definition: vgl_area.hxx:49
static void update(vgl_box_2d< T > &b, vgl_point_2d< T > const &p)
Definition: vgl_rtree_c.h:41
vgl_rtree Class C for V=vgl_point_2d<T>, B = vgl_box_2d<T>.
Definition: vgl_rtree_c.h:27
bool meets(B const &b) const override
Definition: vgl_rtree_c.h:155
vgl_point_2d< Type > max_point() const
Return upper right corner of box.
Definition: vgl_box_2d.hxx:147
vgl_rtree Class C for V=vgl_box_2d<T>, B = vgl_rbox_2d<T>.
Definition: vgl_rtree_c.h:72
static bool meets(vgl_point_2d< T > const &v, vgl_polygon< T > poly)
Definition: vgl_rtree_c.h:59
~vgl_rtree_point_box_2d()=delete
~vgl_rtree_box_box_2d()=delete
bool contains(vgl_point_2d< Type > const &p) const
Return true iff the point p is inside this box.
Definition: vgl_box_2d.hxx:347
vgl_bbox_2d()=default
Default constructor (creates empty box).
vgl_polygon< T > poly_
Definition: vgl_rtree_c.h:149
#define v
Definition: vgl_vector_2d.h:74
static void update(vgl_box_2d< T > &b0, vgl_box_2d< T > const &b1)
Definition: vgl_rtree_c.h:44
static void update(vgl_bbox_2d< T > &b0, vgl_bbox_2d< T > const &b1)
Definition: vgl_rtree_c.h:119
static bool meets(vgl_box_2d< T > const &b, vgl_polygon< T > poly)
Definition: vgl_rtree_c.h:63
static void init(vgl_bbox_2d< T > &b, vgl_box_2d< T > const &v)
Definition: vgl_rtree_c.h:113
vgl_box_2d< T > v_type
Definition: vgl_rtree_c.h:109
vgl_box_2d< T > b_type
Definition: vgl_rtree_c.h:35
vgl_rtree_box_box_2d()=delete
vgl_bbox_2d< T > b_type
Definition: vgl_rtree_c.h:110
static bool meet(vgl_box_2d< T > const &b0, vgl_box_2d< T > const &b1)
Definition: vgl_rtree_c.h:50
static bool meet(vgl_bbox_2d< T > const &b0, vgl_box_2d< T > const &v)
Definition: vgl_rtree_c.h:122
Contains class to represent a cartesian 2D bounding box.
vgl_bbox_2d(Type xmin, Type xmax, Type ymin, Type ymax)
Construct using ranges in x (first two args) and y (last two).
Definition: vgl_rtree_c.h:89
static bool meet(vgl_bbox_2d< T > const &b0, vgl_bbox_2d< T > const &b1)
Definition: vgl_rtree_c.h:128
vgl_bbox_2d(Type const min_position[2], Type const max_position[2])
Construct using two corner points.
Definition: vgl_rtree_c.h:79
Type min_y() const
Get min y.
Definition: vgl_box_2d.h:134
bool meets(V const &v) const override
return true if the probe "meets" the given object.
Definition: vgl_rtree_c.h:154
static bool meet(vgl_box_2d< T > const &b, vgl_point_2d< T > const &p)
Definition: vgl_rtree_c.h:47
static bool meets(vgl_box_2d< T > const &b, vgl_polygon< T > poly)
Definition: vgl_rtree_c.h:138
void add(vgl_point_2d< Type > const &p)
Add a point to this box.
Definition: vgl_box_2d.hxx:317
static bool meets(vgl_bbox_2d< T > const &b, vgl_polygon< T > poly)
Definition: vgl_rtree_c.h:141
vgl_point_2d< Type > min_point() const
Return lower left corner of box.
Definition: vgl_box_2d.hxx:140
vgl_rtree_point_box_2d()=delete
static void init(vgl_box_2d< T > &b, vgl_point_2d< T > const &p)
Definition: vgl_rtree_c.h:38
Type max_y() const
Get max y.
Definition: vgl_box_2d.h:138
Store a polygon.
Definition: vgl_area.h:6
Type max_x() const
Get max x.
Definition: vgl_box_2d.h:136
Function predicate object for querying the tree.
Definition: vgl_algo_fwd.h:30
static void update(vgl_bbox_2d< T > &b, vgl_box_2d< T > const &v)
Definition: vgl_rtree_c.h:116
vgl_rtree_polygon_probe(vgl_polygon< T > const &poly)
Definition: vgl_rtree_c.h:151
bool is_empty() const
Return true if this box is empty.
Definition: vgl_box_2d.h:156
vgl_point_2d< T > v_type
Definition: vgl_rtree_c.h:34