cgv
axis_aligned_box.h
1 #pragma once
2 
3 #include <cgv/type/standard_types.h>
4 #include <cgv/math/vec.h>
5 #include <cgv/math/fvec.h>
6 
7 namespace cgv {
8  namespace media {
9 
13 template<typename T, cgv::type::uint32_type N>
15 {
16 public:
23 protected:
24  fpnt_type minp;
25  fpnt_type maxp;
26 public:
30  template <typename S>
32  minp(T(B.get_min_pnt()(0)), T(B.get_min_pnt()(1)), T(B.get_min_pnt()(2))),
33  maxp(T(B.get_max_pnt()(0)), T(B.get_max_pnt()(1)), T(B.get_max_pnt()(2))) {}
35  axis_aligned_box(const fpnt_type& _minp, const fpnt_type& _maxp) : minp(_minp), maxp(_maxp) {}
37  axis_aligned_box(const pnt_type& _minp, const pnt_type& _maxp)
38  {
39  invalidate();
40  unsigned i;
41  for (i=0; i<_minp.size(); ++i) {
42  if (i == N)
43  break;
44  minp(i) = _minp(i);
45  }
46  for (i=0; i<_maxp.size(); ++i) {
47  if (i == N)
48  break;
49  maxp(i) = _maxp(i);
50  }
51  }
53  void invalidate() {
54  for (unsigned int c = 0; c<N; ++c) {
55  minp(c) = 1;
56  maxp(c) = 0;
57  }
58  }
60  const fpnt_type& get_min_pnt() const { return minp; }
62  const fpnt_type& get_max_pnt() const { return maxp; }
64  fpnt_type& ref_min_pnt() { return minp; }
66  fpnt_type& ref_max_pnt() { return maxp; }
68  fpnt_type get_corner(int i) const
69  {
70  fpnt_type c = minp;
71  int bit = 1;
72  for (unsigned int dim=0; dim < N; ++dim, bit *= 2)
73  if (i & bit)
74  c(dim) = maxp(dim);
75  return c;
76  }
78  fvec_type get_alpha(const fvec_type& p) const { return (p-minp) / (maxp-minp); }
80  fvec_type get_extent() const { return maxp-minp; }
82  fpnt_type get_center() const { return (minp+maxp)/2; }
84  bool is_valid() const { return minp[0] <= maxp[0]; }
86  bool inside(const fpnt_type& p) const {
87  for (unsigned int c = 0; c<N; ++c) {
88  if (p(c) < minp(c)) return false;
89  if (p(c) >= maxp(c)) return false;
90  }
91  return true;
92  }
94  void add_point(const fpnt_type& p) {
95  if (is_valid()) {
96  for (unsigned int c = 0; c<N; ++c) {
97  if (p(c) > maxp(c)) maxp(c) = p(c);
98  if (p(c) < minp(c)) minp(c) = p(c);
99  }
100  }
101  else
102  minp = maxp = p;
103  }
105  void add_point(const pnt_type& p) {
106  if (is_valid()) {
107  for (unsigned int c = 0; p.size(); ++c) {
108  if (c == N)
109  break;
110  if (p(c) > maxp(c)) maxp(c) = p(c);
111  if (p(c) < minp(c)) minp(c) = p(c);
112  }
113  }
114  else
115  *this = axis_aligned_box<T,N>(p,p);
116  }
119  if (!aab.is_valid())
120  return;
121  add_point(aab.minp);
122  add_point(aab.maxp);
123  }
125  void scale(const T& f)
126  {
127  if (!is_valid())
128  return;
129  for (unsigned int c = 0; c<N; ++c) {
130  maxp(c) *= f;
131  minp(c) *= f;
132  }
133  }
135  void translate(const fpnt_type& v)
136  {
137  if (!is_valid())
138  return;
139  ref_min_pnt() += v;
140  ref_max_pnt() += v;
141  }
143  unsigned get_max_extent_coord_index() const
144  {
145  fvec_type e = get_extent();
146  unsigned j = 0;
147  for (unsigned i=1; i<N; ++i)
148  if (e(i) > e(j))
149  j = i;
150  return j;
151  }
152 };
153 
155 template<typename T, cgv::type::uint32_type N>
156 std::ostream& operator<<(std::ostream& out, const axis_aligned_box<T, N>& box)
157 {
158  return out << box.get_min_pnt() << "->" << box.get_max_pnt();
159 }
160 
161 
162  }
163 }
cgv::media::axis_aligned_box::axis_aligned_box
axis_aligned_box(const fpnt_type &_minp, const fpnt_type &_maxp)
construct from min point and max point
Definition: axis_aligned_box.h:35
cgv::media::axis_aligned_box::axis_aligned_box
axis_aligned_box()
standard constructor does not initialize
Definition: axis_aligned_box.h:28
cgv::media::axis_aligned_box::add_point
void add_point(const pnt_type &p)
extent box to include given point
Definition: axis_aligned_box.h:105
cgv::media::axis_aligned_box::pnt_type
cgv::math::vec< T > pnt_type
the interface allows also to work with variable sized points
Definition: axis_aligned_box.h:21
cgv::math::fvec
Definition: fvec.h:18
cgv::math::vec
A column vector class.
Definition: fvec.h:13
cgv::media::axis_aligned_box::ref_max_pnt
fpnt_type & ref_max_pnt()
return a reference to corner 7
Definition: axis_aligned_box.h:66
cgv::math::vec::size
unsigned size() const
number of elements
Definition: vec.h:97
cgv::media::axis_aligned_box::axis_aligned_box
axis_aligned_box(const pnt_type &_minp, const pnt_type &_maxp)
construct from min point and max point
Definition: axis_aligned_box.h:37
cgv::media::axis_aligned_box::is_valid
bool is_valid() const
check if aab is valid
Definition: axis_aligned_box.h:84
cgv::media::axis_aligned_box::get_extent
fvec_type get_extent() const
return a vector with the extents in the different dimensions
Definition: axis_aligned_box.h:80
cgv::media::axis_aligned_box
Definition: axis_aligned_box.h:15
cgv::media::axis_aligned_box::invalidate
void invalidate()
set to invalid min and max points
Definition: axis_aligned_box.h:53
cgv::media::axis_aligned_box::add_axis_aligned_box
void add_axis_aligned_box(const axis_aligned_box< T, N > &aab)
extent box to include given axis alinged box
Definition: axis_aligned_box.h:118
cgv::media::axis_aligned_box::get_max_extent_coord_index
unsigned get_max_extent_coord_index() const
return the index of the coordinte with maximum extend
Definition: axis_aligned_box.h:143
cgv::media::axis_aligned_box::ref_min_pnt
fpnt_type & ref_min_pnt()
return a reference to corner 0
Definition: axis_aligned_box.h:64
cgv::media::axis_aligned_box::get_max_pnt
const fpnt_type & get_max_pnt() const
return a const reference to corner 7
Definition: axis_aligned_box.h:62
cgv::media::axis_aligned_box::get_min_pnt
const fpnt_type & get_min_pnt() const
return a const reference to corner 0
Definition: axis_aligned_box.h:60
cgv::media::axis_aligned_box::get_center
fpnt_type get_center() const
return the center of the box
Definition: axis_aligned_box.h:82
cgv::media::axis_aligned_box::scale
void scale(const T &f)
scale the complete box with respect to the world coordinate origin
Definition: axis_aligned_box.h:125
cgv::media::axis_aligned_box::inside
bool inside(const fpnt_type &p) const
check whether a point is inside the aabb
Definition: axis_aligned_box.h:86
cgv::media::axis_aligned_box::get_corner
fpnt_type get_corner(int i) const
return the i-th corner where the lower N bits of i are used to select between min (bit = 0) and max (...
Definition: axis_aligned_box.h:68
cgv::media::axis_aligned_box::get_alpha
fvec_type get_alpha(const fvec_type &p) const
return a vector containing linear interpolation parameter values in the different dimensions
Definition: axis_aligned_box.h:78
cgv::media::axis_aligned_box::translate
void translate(const fpnt_type &v)
translate box by vector
Definition: axis_aligned_box.h:135
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::media::axis_aligned_box::axis_aligned_box
axis_aligned_box(const axis_aligned_box< S, N > &B)
type conversion copy constructor
Definition: axis_aligned_box.h:31
cgv::media::axis_aligned_box::add_point
void add_point(const fpnt_type &p)
extent box to include given point
Definition: axis_aligned_box.h:94
cgv::media::axis_aligned_box::fpnt_type
cgv::math::fvec< T, N > fpnt_type
internally fixed sized points and vectors are used
Definition: axis_aligned_box.h:18