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>
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 }
126 fpnt_type min_p = minp;
127 fpnt_type max_p = maxp;
128 if (!aab.is_valid())
129 return axis_aligned_box<T, N>(min_p, max_p);
130
131 for (unsigned int c = 0; c<N; ++c) {
132 if (aab.maxp(c) < maxp(c)) max_p(c) = aab.maxp(c);
133 if (aab.minp(c) > minp(c)) min_p(c) = aab.minp(c);
134 }
135 return axis_aligned_box<T, N>(min_p, max_p);
136 }
138 void scale(const T& f)
139 {
140 if (!is_valid())
141 return;
142 for (unsigned int c = 0; c<N; ++c) {
143 maxp(c) *= f;
144 minp(c) *= f;
145 }
146 }
148 void translate(const fpnt_type& v)
149 {
150 if (!is_valid())
151 return;
152 ref_min_pnt() += v;
153 ref_max_pnt() += v;
154 }
157 {
158 fvec_type e = get_extent();
159 unsigned j = 0;
160 for (unsigned i=1; i<N; ++i)
161 if (e(i) > e(j))
162 j = i;
163 return j;
164 }
165};
166
168template<typename T, cgv::type::uint32_type N>
169std::ostream& operator<<(std::ostream& out, const axis_aligned_box<T, N>& box)
170{
171 return out << box.get_min_pnt() << "->" << box.get_max_pnt();
172}
173
174} // namespace media
175
178
185
192
205
218
231
233
234} // 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
axis_aligned_box(const fpnt_type &_minp, const fpnt_type &_maxp)
construct from min point and max point
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
axis_aligned_box(const axis_aligned_box< S, N > &B)
type conversion copy constructor
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
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