Code-Eli  0.3.6
bounding_box.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_geom_general_bounding_box_hpp
14 #define eli_geom_general_bounding_box_hpp
15 
16 #include "eli/code_eli.hpp"
17 
18 #include "eli/util/tolerance.hpp"
19 
20 namespace eli
21 {
22  namespace geom
23  {
24  namespace general
25  {
26  template<typename data__, unsigned short dim__, typename tol__=eli::util::tolerance<data__> >
28  {
29  public:
30  typedef data__ data_type;
31  typedef unsigned short dimension_type;
32  typedef Eigen::Matrix<data_type, 1, dim__> point_type;
33  typedef typename point_type::Index index_type;
34  typedef tol__ tolerance_type;
35 
36  public:
37  bounding_box() : empty(true) {}
38  bounding_box(const point_type &p) : empty(false), pmin(p), pmax(p) {}
41 
43  {
44  if (this!=&bb)
45  {
46  empty=bb.empty;
47  pmin=bb.pmin;
48  pmax=bb.pmax;
49  }
50 
51  return (*this);
52  }
53 
55  {
56  // if compared against self then true
57  if (this==&bb)
58  return true;
59 
60  // if both are empty sets then true
61  if (empty && bb.empty)
62  return true;
63 
64  // start comparing member data
65  if (empty!=bb.empty)
66  return false;
67  if (pmin!=bb.pmin)
68  return false;
69  if (pmax!=bb.pmax)
70  return false;
71 
72  return true;
73  }
74 
76  {
77  return !operator==(bb);
78  }
79 
80  static dimension_type dimension() {return dim__;}
81 
82  bool empty_set() const {return empty;}
83 
84  void set_min(const point_type &pm)
85  {
86  if (empty_set())
87  {
88  pmax=pm;
89  empty=false;
90  }
91  pmin=pm;
92  }
93  point_type get_min() const {return pmin;}
94 
95  void set_max(const point_type &pm)
96  {
97  if (empty_set())
98  {
99  pmin=pm;
100  empty=false;
101  }
102  pmax=pm;
103  }
104  point_type get_max() const {return pmax;}
105 
106  void clear()
107  {
108  empty=true;
109  pmin.setZero();
110  pmax.setZero();
111  }
112 
113  bool add(const point_type &p)
114  {
115  index_type i;
116  bool changed(false);
117 
118  // handle special case of no bounding box
119  if (empty_set())
120  {
121  set_min(p);
122  set_max(p);
123  return true;
124  }
125 
126  for (i=0; i<dim__; ++i)
127  {
128  if (p(i)<pmin(i))
129  {
130  changed=true;
131  pmin(i)=p(i);
132  }
133  if (p(i)>pmax(i))
134  {
135  changed=true;
136  pmax(i)=p(i);
137  }
138  }
139 
140  return changed;
141  }
142 
144  {
145  bool rtn1=add(bb.pmin), rtn2=add(bb.pmax);
146  return (rtn1 || rtn2);
147  }
148 
149  bool inside(const point_type &p) const
150  {
151  for (index_type i=0; i<dim__; ++i)
152  {
153  if ( (p(i)<pmin(i)) || (p(i)>pmax(i)) )
154  return false;
155  }
156 
157  return true;
158  }
159 
161  {
162  const index_type nc(1<<dim__);
163  point_type c[nc], bb_min(bb.get_min()), bb_max(bb.get_max());
164 
165  // set the first and last corner
166  c[0]=bb_min;
167  c[nc-1]=bb_max;
168 
169  // set the corners for 2 or 3 dimensional cases
170  if (dim__>1)
171  {
172  c[1].x()=bb_max.x();
173  c[1].y()=bb_min.y();
174  c[2].x()=bb_min.x();
175  c[2].y()=bb_max.y();
176  if (dim__==3)
177  {
178  c[1].z()=bb_min.z();
179  c[2].z()=bb_min.z();
180  }
181  }
182 
183  // set the remaining corners for 3 dimensional cases
184  if (dim__>2)
185  {
186  c[3] << bb_max.x(), bb_max.y(), bb_min.z();
187  c[4] << bb_min.x(), bb_min.y(), bb_max.z();
188  c[5] << bb_max.x(), bb_min.y(), bb_max.z();
189  c[6] << bb_min.x(), bb_max.y(), bb_max.z();
190  }
191 
192  // if any corner is inside this bbox, then they intersect
193  for (index_type i=0; i<nc; ++i)
194  {
195  if (inside(c[i]))
196  return true;
197  }
198 
199  // at this point know that no bb edge crosses this bbox, but need
200  // to check if this bbox in entirely inside bb
201  return bb.inside(pmin);
202  }
203 
204  private:
205  bool empty;
206  point_type pmin, pmax;
207  };
208  }
209  }
210 }
211 #endif
bounding_box()
Definition: bounding_box.hpp:37
bool empty
Definition: bounding_box.hpp:205
Definition: math.hpp:20
void clear()
Definition: bounding_box.hpp:106
~bounding_box()
Definition: bounding_box.hpp:40
point_type::Index index_type
Definition: bounding_box.hpp:33
Definition: bounding_box.hpp:27
bounding_box(const bounding_box< data_type, dim__ > &bb)
Definition: bounding_box.hpp:39
unsigned short dimension_type
Definition: bounding_box.hpp:31
Eigen::Matrix< data_type, 1, dim__ > point_type
Definition: bounding_box.hpp:32
point_type get_max() const
Definition: bounding_box.hpp:104
bool intersect(const bounding_box< data_type, dim__ > &bb) const
Definition: bounding_box.hpp:160
bounding_box< data_type, dim__ > & operator=(const bounding_box< data_type, dim__ > &bb)
Definition: bounding_box.hpp:42
tol__ tolerance_type
Definition: bounding_box.hpp:34
bool operator!=(const bounding_box< data_type, dim__ > &bb)
Definition: bounding_box.hpp:75
point_type pmin
Definition: bounding_box.hpp:206
bounding_box(const point_type &p)
Definition: bounding_box.hpp:38
bool add(const point_type &p)
Definition: bounding_box.hpp:113
void set_min(const point_type &pm)
Definition: bounding_box.hpp:84
bool inside(const point_type &p) const
Definition: bounding_box.hpp:149
bool empty_set() const
Definition: bounding_box.hpp:82
bool operator==(const bounding_box< data_type, dim__ > &bb)
Definition: bounding_box.hpp:54
point_type pmax
Definition: bounding_box.hpp:206
point_type get_min() const
Definition: bounding_box.hpp:93
data__ data_type
Definition: bounding_box.hpp:30
void set_max(const point_type &pm)
Definition: bounding_box.hpp:95
static dimension_type dimension()
Definition: bounding_box.hpp:80
bool add(const bounding_box< data_type, dim__ > &bb)
Definition: bounding_box.hpp:143