Free Electron
Box.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __math_Box_h__
8 #define __math_Box_h__
9 
10 namespace fe
11 {
12 
13 /**************************************************************************//**
14  @brief N-dimensional axis-aligned bounding-box
15 
16  @ingroup math
17 *//***************************************************************************/
18 template<int N,class T> class Box: public Vector<N,T>
19 {
20  public:
21  Box(void) {}
22 
23  Box(const Vector<N,T>& location):
24  Vector<N,T>(location) {}
25 
26  Box(const Vector<N,T>& location,
27  const Vector<N,T>& size):
28  Vector<N,T>(location), m_size(size) {}
29 
30  Box(const Box<N,T>& rhs):
31  Vector<N,T>(rhs)
32  { m_size=rhs.m_size; }
33 
34  Box<N,T>& operator=(const Box<N,T>& rhs)
36  m_size=rhs.m_size;
37  return *this; }
38 
39  Vector<N,T>& size(void) { return m_size; }
40 const Vector<N,T>& size(void) const { return m_size; }
41 
42  private:
43  Vector<N,T> m_size;
44 };
45 
46 /** Test equality
47  @relates Box
48  */
49 template<int N,class T,class U>
50 inline bool operator==(const Box<N,T> &lhs, const Box<N,U> &rhs)
51 {
52  return Vector<N,T>(lhs).operator==(rhs) && lhs.size()==rhs.size();
53 }
54 
55 /** Test inequality
56  @relates Box
57  */
58 template<int N,class T,class U>
59 inline bool operator!=(const Box<N,T> &lhs, const Box<N,U> &rhs)
60 {
61  return Vector<N,T>(lhs).operator!=(rhs) || lhs.size()!=rhs.size();
62 }
63 
64 /** Change the size
65  @relates Box
66  */
67 template<int N,class T,class U>
68 inline Box<N,T>& resize(Box<N,T>& lhs,const Vector<N,U> &size)
69 {
70  lhs.size()=size;
71  return lhs;
72 }
73 
74 /** Redefine the spatial domain
75  @relates Box
76  */
77 template<int N,class T,class U,class V>
78 inline Box<N,T>& set(Box<N,T>& lhs,const Vector<N,U> &location,
79  const Vector<N,V> &size)
80 {
81  lhs=location;
82  lhs.size()=size;
83  return lhs;
84 }
85 
86 /** Set the box to encapsulate the given sphere
87  @relates Box
88  */
89 template<class T,class U,class V>
90 inline Box<3,T>& set(Box<3,T>& lhs,const Vector<3,U> &center,V radius)
91 {
92  const Vector<3,U> corner(radius,radius,radius);
93  const Vector<3,U> diff=center-corner;
94  const Vector<3,U> corner2=2.0f*corner;
95  lhs=diff;
96  resize(lhs,corner2);
97  return lhs;
98 }
99 
100 /** Get the box width
101  @relates Box
102  */
103 template<int N,class T>
104 inline T width(const Box<N,T>& lhs)
105 {
106  FEASSERT(N>0);
107  return lhs.size()[0];
108 }
109 
110 /** Get the box height
111  @relates Box
112  */
113 template<int N,class T>
114 inline T height(const Box<N,T>& lhs)
115 {
116  FEASSERT(N>1);
117  return lhs.size()[1];
118 }
119 
120 /** Get the box depth
121  @relates Box
122  */
123 template<int N,class T>
124 inline T depth(const Box<N,T>& lhs)
125 {
126  FEASSERT(N>2);
127  return lhs.size()[2];
128 }
129 
130 /** Set by explicit elements (2D)
131  @relates Box
132  */
133 template<class T,class U,class V,class W,class X>
134 inline Box<2,T>& set(Box<2,T>& lhs,U x,V y,W w,X h)
135 {
136  set(lhs,x,y);
137  set(lhs.size(),w,h);
138  return lhs;
139 }
140 
141 /** Set by explicit elements (3D)
142  @relates Box
143  */
144 template<class T,class U,class V,class W,class X,class Y,class Z>
145 inline Box<3,T>& set(Box<3,T>& lhs,U x,V y,W z,X w,Y h,Z d)
146 {
147  set(lhs,x,y,z);
148  set(lhs.size(),w,h,d);
149  return lhs;
150 }
151 
152 /** Resize by explicit elements (2D)
153  @relates Box
154  */
155 template<class T,class U,class V>
156 inline Box<2,T>& resize(Box<2,T>& lhs,U w,V h)
157 {
158  set(lhs.size(),w,h);
159  return lhs;
160 }
161 
162 /** Resize by explicit elements (3D)
163  @relates Box
164  */
165 template<class T,class U,class V,class W>
166 inline Box<3,T>& resize(Box<3,T>& lhs,U w,V h,W d)
167 {
168  set(lhs.size(),w,h,d);
169  return lhs;
170 }
171 
172 /** Returns true if the @em box contains the @em point
173  @relates Box
174  */
175 template<class T,class U>
176 inline bool intersecting(const Box<2,T>& box,const Vector<2,U>& point)
177 {
178  return point[0]>box[0] && point[0]<box[0]+box.size()[0] &&
179  point[1]>box[1] && point[1]<box[1]+box.size()[1];
180 }
181 
182 /** Returns true if the boxes overlap
183  @relates Box
184  */
185 template<class T,class U>
186 inline bool intersecting(const Box<2,T>& box1,const Box<2,U>& box2)
187 {
188  return box1[0]+box1.size()[0]>box2[0] && box1[0]<box2[0]+box2.size()[0] &&
189  box1[1]+box1.size()[1]>box2[1] && box1[1]<box2[1]+box2.size()[1];
190 }
191 
192 /** Returns true if the @em box contains the @em point
193  @relates Box
194  */
195 template<class T,class U>
196 inline bool intersecting(const Box<3,T>& box,const Vector<3,U>& point)
197 {
198  return point[0]>box[0] && point[0]<box[0]+box.size()[0] &&
199  point[1]>box[1] && point[1]<box[1]+box.size()[1] &&
200  point[2]>box[2] && point[2]<box[2]+box.size()[2];
201 }
202 
203 /** Returns true if the boxes overlap
204  @relates Box
205  */
206 template<class T,class U>
207 inline bool intersecting(const Box<3,T>& box1,const Box<3,U>& box2)
208 {
209  return box1[0]+box1.size()[0]>box2[0] && box1[0]<box2[0]+box2.size()[0] &&
210  box1[1]+box1.size()[1]>box2[1] && box1[1]<box2[1]+box2.size()[1] &&
211  box1[2]+box1.size()[2]>box2[2] && box1[2]<box2[2]+box2.size()[2];
212 }
213 
214 typedef Box<2,I32> Box2i;
215 typedef Box<2,F32> Box2f;
216 typedef Box<2,F64> Box2d;
217 typedef Box<2,Real> Box2;
218 
219 typedef Box<3,F32> Box3f;
220 typedef Box<3,F64> Box3d;
221 typedef Box<3,Real> Box3;
222 typedef Box<3,Real> SpatialBox;
223 
224 /** Returns Box enveloping a tapered cylinder
225  @relates Box
226  */
227 FE_DL_EXPORT const Box3 aabb(const Vector3f& location,const Vector3f& span,
228  F32 baseRadius,F32 endRadius);
229 
230 /** Returns Box enveloping a sphere
231  @relates Box
232  */
233 FE_DL_EXPORT const Box3 aabb(const Vector3f& location,F32 radius);
234 
235 /** Print to a string
236  @relates Box
237  */
238 template<int N, class T>
239 inline String print(const Box<N,T> &box)
240 {
241  return print((Vector<N,T>&)box) + " " + print(box.size());
242 }
243 
244 } /* namespace */
245 
246 #endif /* __math_Box_h__ */
Box< 2, T > & resize(Box< 2, T > &lhs, U w, V h)
Resize by explicit elements (2D)
Definition: Box.h:156
FE_DL_EXPORT const Box3 aabb(const Vector3f &location, const Vector3f &span, F32 baseRadius, F32 endRadius)
Returns Box enveloping a tapered cylinder.
Definition: Box.cc:13
bool intersecting(const Box< 3, T > &box, const Vector< 3, U > &point)
Returns true if the box contains the point.
Definition: Box.h:196
bool operator!=(const Box< N, T > &lhs, const Box< N, U > &rhs)
Test inequality.
Definition: Box.h:59
T height(const Box< N, T > &lhs)
Get the box height.
Definition: Box.h:114
kernel
Definition: namespace.dox:3
Box< N, T > & resize(Box< N, T > &lhs, const Vector< N, U > &size)
Change the size.
Definition: Box.h:68
bool intersecting(const Box< 2, T > &box, const Vector< 2, U > &point)
Returns true if the box contains the point.
Definition: Box.h:176
T width(const Box< N, T > &lhs)
Get the box width.
Definition: Box.h:104
bool intersecting(const Box< 2, T > &box1, const Box< 2, U > &box2)
Returns true if the boxes overlap.
Definition: Box.h:186
N-dimensional axis-aligned bounding-box.
Definition: Box.h:18
String print(const Box< N, T > &box)
Print to a string.
Definition: Box.h:239
Dense vector - size fixed by template.
Definition: Vector.h:19
T depth(const Box< N, T > &lhs)
Get the box depth.
Definition: Box.h:124
bool intersecting(const Box< 3, T > &box1, const Box< 3, U > &box2)
Returns true if the boxes overlap.
Definition: Box.h:207
Automatically reference-counted string container.
Definition: String.h:128
Box< 3, T > & resize(Box< 3, T > &lhs, U w, V h, W d)
Resize by explicit elements (3D)
Definition: Box.h:166
U32 size(const Vector< N, T > &lhs)
Return the number of elements.
Definition: Vector.h:635
bool operator==(const Box< N, T > &lhs, const Box< N, U > &rhs)
Test equality.
Definition: Box.h:50