cgv
Loading...
Searching...
No Matches
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
7namespace cgv {
8namespace media {
9
13template<typename T, cgv::type::uint32_type N>
15{
16public:
23protected:
24 fpnt_type minp;
25 fpnt_type maxp;
26public:
30 template <typename S>
31 axis_aligned_box(const axis_aligned_box<S, N>& other) : minp(other.get_min_pnt()), maxp(other.get_max_pnt()) {}
33 axis_aligned_box(const fpnt_type& minp, const fpnt_type& maxp) : minp(minp), maxp(maxp) {}
35 axis_aligned_box(const pnt_type& _minp, const pnt_type& _maxp)
36 {
37 invalidate();
38 unsigned i;
39 for (i=0; i<_minp.size(); ++i) {
40 if (i == N)
41 break;
42 minp(i) = _minp(i);
43 }
44 for (i=0; i<_maxp.size(); ++i) {
45 if (i == N)
46 break;
47 maxp(i) = _maxp(i);
48 }
49 }
51 void invalidate() {
52 minp = T(1);
53 maxp = T(0);
54 }
56 const fpnt_type& get_min_pnt() const { return minp; }
58 const fpnt_type& get_max_pnt() const { return maxp; }
60 fpnt_type& ref_min_pnt() { return minp; }
62 fpnt_type& ref_max_pnt() { return maxp; }
64 fpnt_type get_corner(int i) const
65 {
66 fpnt_type c = minp;
67 int bit = 1;
68 for (unsigned int dim=0; dim < N; ++dim, bit *= 2)
69 if (i & bit)
70 c(dim) = maxp(dim);
71 return c;
72 }
74 fvec_type get_alpha(const fvec_type& p) const { return (p-minp) / (maxp-minp); }
76 fvec_type get_extent() const { return maxp-minp; }
78 fpnt_type get_center() const { return (minp+maxp)/2; }
80 bool is_valid() const { return minp[0] <= maxp[0]; }
82 bool inside(const fpnt_type& p) const {
83 for (unsigned int c = 0; c<N; ++c) {
84 if (p(c) < minp(c)) return false;
85 if (p(c) >= maxp(c)) return false;
86 }
87 return true;
88 }
90 void add_point(const fpnt_type& p) {
91 if (is_valid()) {
92 for (unsigned int c = 0; c<N; ++c) {
93 if (p(c) > maxp(c)) maxp(c) = p(c);
94 if (p(c) < minp(c)) minp(c) = p(c);
95 }
96 }
97 else
98 minp = maxp = p;
99 }
101 void add_point(const pnt_type& p) {
102 if (is_valid()) {
103 for (unsigned int c = 0; p.size(); ++c) {
104 if (c == N)
105 break;
106 if (p(c) > maxp(c)) maxp(c) = p(c);
107 if (p(c) < minp(c)) minp(c) = p(c);
108 }
109 }
110 else
111 *this = axis_aligned_box<T,N>(p,p);
112 }
115 if (!aab.is_valid())
116 return;
117 add_point(aab.minp);
118 add_point(aab.maxp);
119 }
122 fpnt_type min_p = minp;
123 fpnt_type max_p = maxp;
124 if (!aab.is_valid())
125 return axis_aligned_box<T, N>(min_p, max_p);
126
127 for (unsigned int c = 0; c<N; ++c) {
128 if (aab.maxp(c) < maxp(c)) max_p(c) = aab.maxp(c);
129 if (aab.minp(c) > minp(c)) min_p(c) = aab.minp(c);
130 }
131 return axis_aligned_box<T, N>(min_p, max_p);
132 }
134 void scale(const T& f)
135 {
136 if (!is_valid())
137 return;
138 for (unsigned int c = 0; c<N; ++c) {
139 maxp(c) *= f;
140 minp(c) *= f;
141 }
142 }
144 void translate(const fpnt_type& v)
145 {
146 if (!is_valid())
147 return;
148 ref_min_pnt() += v;
149 ref_max_pnt() += v;
150 }
153 {
154 fvec_type e = get_extent();
155 unsigned j = 0;
156 for (unsigned i=1; i<N; ++i)
157 if (e(i) > e(j))
158 j = i;
159 return j;
160 }
161};
162
164template<typename T, cgv::type::uint32_type N>
165std::ostream& operator<<(std::ostream& out, const axis_aligned_box<T, N>& box)
166{
167 return out << box.get_min_pnt() << "->" << box.get_max_pnt();
168}
169
170} // namespace media
171
174
181
188
201
214
227
229
230} // namespace cgv
A vector with zero based index.
Definition fvec.h:26
A column vector class.
Definition vec.h:28
unsigned size() const
number of elements
Definition vec.h:59
An axis aligned box, defined by to points: min and max.
void invalidate()
set to invalid min and max points
fpnt_type get_center() const
return the center of the box
axis_aligned_box(const pnt_type &_minp, const pnt_type &_maxp)
construct from min point and max point
void add_point(const fpnt_type &p)
extent box to include given point
cgv::math::vec< T > pnt_type
the interface allows also to work with variable sized points
void add_point(const pnt_type &p)
extent box to include given point
fpnt_type & ref_max_pnt()
return a reference to corner 7
fpnt_type & ref_min_pnt()
return a reference to corner 0
bool inside(const fpnt_type &p) const
check whether a point is inside the aabb
unsigned get_max_extent_coord_index() const
return the index of the coordinte with maximum extend
axis_aligned_box()
standard constructor does not initialize
axis_aligned_box(const fpnt_type &minp, const fpnt_type &maxp)
construct from min point and max point
axis_aligned_box(const axis_aligned_box< S, N > &other)
type conversion copy constructor
void add_axis_aligned_box(const axis_aligned_box< T, N > &aab)
extent box to include given axis alinged box
void translate(const fpnt_type &v)
translate box by vector
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 (...
void scale(const T &f)
scale the complete box with respect to the world coordinate origin
fvec_type get_extent() const
return a vector with the extents in the different dimensions
fvec_type get_alpha(const fvec_type &p) const
return a vector containing linear interpolation parameter values in the different dimensions
const fpnt_type & get_max_pnt() const
return a const reference to corner 7
const fpnt_type & get_min_pnt() const
return a const reference to corner 0
axis_aligned_box< T, N > intersect(const axis_aligned_box< T, N > &aab)
returns intersection of this aab with the provided aab
bool is_valid() const
check if aab is valid
cgv::math::fvec< T, N > fpnt_type
internally fixed sized points and vectors are used
the cgv namespace
Definition print.h:11
cgv::media::axis_aligned_box< float, 3 > box3
declare type of 3d single precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< uint16_t, 3 > usbox3
declare type of 3d 16 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< uint16_t, 4 > usbox4
declare type of 4d 16 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< int16_t, 3 > sbox3
declare type of 3d 16 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< double, 3 > dbox3
declare type of 3d double precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< int16_t, 2 > sbox2
declare type of 2d 16 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< uint32_t, 2 > ubox2
declare type of 2d 32 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< uint16_t, 2 > usbox2
declare type of 2d 16 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< uint64_t, 3 > ulbox3
declare type of 3d 64 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< uint64_t, 2 > ulbox2
declare type of 2d 64 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< int32_t, 2 > ibox2
declare type of 2d 32 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< int32_t, 3 > ibox3
declare type of 3d 32 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< int64_t, 2 > lbox2
declare type of 2d 64 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< float, 2 > box2
declare type of 2d single precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< double, 2 > dbox2
declare type of 2d double precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< uint32_t, 3 > ubox3
declare type of 3d 32 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< int64_t, 3 > lbox3
declare type of 3d 64 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< int16_t, 4 > sbox4
declare type of 4d 16 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< float, 4 > box4
declare type of 4d single precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< uint64_t, 4 > ulbox4
declare type of 4d 64 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< int32_t, 4 > ibox4
declare type of 4d 32 bit integer axis-aligned boxes
cgv::media::axis_aligned_box< double, 4 > dbox4
declare type of 4d double precision floating point axis-aligned boxes
cgv::media::axis_aligned_box< uint32_t, 4 > ubox4
declare type of 4d 32 bit unsigned integer axis-aligned boxes
cgv::media::axis_aligned_box< int64_t, 4 > lbox4
declare type of 4d 64 bit integer axis-aligned boxes