$darkmode
VCG Library
box3.h
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #ifndef __VCGLIB_BOX3
24 #define __VCGLIB_BOX3
25 
26 #include <vcg/space/point3.h>
27 #include <vcg/math/matrix44.h>
28 #include <vcg/space/line3.h>
29 #include <vcg/space/plane3.h>
30 
31 namespace vcg {
32 
40 template <class BoxScalarType>
41 class Box3
42 {
43 public:
44 
46  typedef BoxScalarType ScalarType;
47 
53  inline Box3() { this->SetNull(); }
55  inline Box3( const Point3<BoxScalarType> & mi, const Point3<BoxScalarType> & ma ) { min = mi; max = ma; }
57  inline Box3(const Point3<BoxScalarType> & center, const BoxScalarType & radius) {
58  min = center-Point3<BoxScalarType>(radius,radius,radius);
59  max = center+Point3<BoxScalarType>(radius,radius,radius);
60  }
62  inline ~Box3() { }
64  inline bool operator == ( const Box3<BoxScalarType> & p ) const
65  {
66  return min==p.min && max==p.max;
67  }
69  inline bool operator != ( const Box3<BoxScalarType> & p ) const
70  {
71  return min!=p.min || max!=p.max;
72  }
75  void Offset( const BoxScalarType s )
76  {
77  Offset( Point3<BoxScalarType> (s,s,s));
78  }
83  void Offset( const Point3<BoxScalarType> & delta )
84  {
85  min -= delta;
86  max += delta;
87  }
89  void Set( const Point3<BoxScalarType> & p )
90  {
91  min = max = p;
92  }
93 
95  void SetNull()
96  {
97  min.X()= 1; max.X()= -1;
98  min.Y()= 1; max.Y()= -1;
99  min.Z()= 1; max.Z()= -1;
100  }
104  void Add( const Box3<BoxScalarType> & b )
105  {
106  if(b.IsNull()) return; // Adding a null bbox should do nothing
107  if(IsNull()) *this=b;
108  else
109  {
110  if(min.X() > b.min.X()) min.X() = b.min.X();
111  if(min.Y() > b.min.Y()) min.Y() = b.min.Y();
112  if(min.Z() > b.min.Z()) min.Z() = b.min.Z();
113 
114  if(max.X() < b.max.X()) max.X() = b.max.X();
115  if(max.Y() < b.max.Y()) max.Y() = b.max.Y();
116  if(max.Z() < b.max.Z()) max.Z() = b.max.Z();
117  }
118  }
121  void Add( const Point3<BoxScalarType> & p )
122  {
123  if(IsNull()) Set(p);
124  else
125  {
126  if(min.X() > p.X()) min.X() = p.X();
127  if(min.Y() > p.Y()) min.Y() = p.Y();
128  if(min.Z() > p.Z()) min.Z() = p.Z();
129 
130  if(max.X() < p.X()) max.X() = p.X();
131  if(max.Y() < p.Y()) max.Y() = p.Y();
132  if(max.Z() < p.Z()) max.Z() = p.Z();
133  }
134  }
135 
138  void Add( const Point3<BoxScalarType> & p, const BoxScalarType radius )
139  {
140  if(IsNull()) Set(p);
141  else
142  {
143  min.X() = std::min(min.X(),p.X()-radius);
144  min.Y() = std::min(min.Y(),p.Y()-radius);
145  min.Z() = std::min(min.Z(),p.Z()-radius);
146 
147  max.X() = std::max(max.X(),p.X()+radius);
148  max.Y() = std::max(max.Y(),p.Y()+radius);
149  max.Z() = std::max(max.Z(),p.Z()+radius);
150  }
151  }
154  void Add( const Matrix44<BoxScalarType> &m, const Box3<BoxScalarType> & b )
155  {
156  if(b.IsNull()) return; // Adding a null bbox should do nothing
157 
158  const Point3<BoxScalarType> &mn= b.min;
159  const Point3<BoxScalarType> &mx= b.max;
160  Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mn[2])));
161  Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mn[2])));
162  Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mn[2])));
163  Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mn[2])));
164  Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mx[2])));
165  Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mx[2])));
166  Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mx[2])));
167  Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mx[2])));
168  }
172  void Intersect( const Box3<BoxScalarType> & b )
173  {
174  if(min.X() < b.min.X()) min.X() = b.min.X();
175  if(min.Y() < b.min.Y()) min.Y() = b.min.Y();
176  if(min.Z() < b.min.Z()) min.Z() = b.min.Z();
177 
178  if(max.X() > b.max.X()) max.X() = b.max.X();
179  if(max.Y() > b.max.Y()) max.Y() = b.max.Y();
180  if(max.Z() > b.max.Z()) max.Z() = b.max.Z();
181 
182  if(min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z()) SetNull();
183  }
188  {
189  min += p;
190  max += p;
191  }
194  bool IsIn( const Point3<BoxScalarType> & p ) const
195  {
196  return (
197  min.X() <= p.X() && p.X() <= max.X() &&
198  min.Y() <= p.Y() && p.Y() <= max.Y() &&
199  min.Z() <= p.Z() && p.Z() <= max.Z()
200  );
201  }
205  bool IsInEx( const Point3<BoxScalarType> & p ) const
206  {
207  return (
208  min.X() <= p.X() && p.X() < max.X() &&
209  min.Y() <= p.Y() && p.Y() < max.Y() &&
210  min.Z() <= p.Z() && p.Z() < max.Z()
211  );
212  }
218  /* old version
219  bool Collide(Box3<BoxScalarType> const &b)
220  {
221  Box3<BoxScalarType> bb=*this;
222  bb.Intersect(b);
223  return bb.IsValid();
224  }
225  */
226  bool Collide( const Box3<BoxScalarType> &b) const
227  {
228  return b.min.X()<max.X() && b.max.X()>min.X() &&
229  b.min.Y()<max.Y() && b.max.Y()>min.Y() &&
230  b.min.Z()<max.Z() && b.max.Z()>min.Z() ;
231  }
235  bool IsNull() const { return min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z(); }
238  bool IsEmpty() const { return min==max; }
240  BoxScalarType Diag() const
241  {
242  return Distance(min,max);
243  }
245  BoxScalarType SquaredDiag() const
246  {
247  return SquaredDistance(min,max);
248  }
251  {
252  return (min+max)/2;
253  }
256  {
257  return (max-min);
258  }
261  return Point3<BoxScalarType>(
262  min[0] + p[0]*(max[0]-min[0]),
263  min[1] + p[1]*(max[1]-min[1]),
264  min[2] + p[2]*(max[2]-min[2]));
265  }
268  return Point3<BoxScalarType>(
269  (p[0]-min[0])/(max[0]-min[0]),
270  (p[1]-min[1])/(max[1]-min[1]),
271  (p[2]-min[2])/(max[2]-min[2])
272  );
273  }
275  BoxScalarType Volume() const
276  {
277  return (max.X()-min.X())*(max.Y()-min.Y())*(max.Z()-min.Z());
278  }
280  inline BoxScalarType DimX() const { return max.X()-min.X();}
282  inline BoxScalarType DimY() const { return max.Y()-min.Y();}
284  inline BoxScalarType DimZ() const { return max.Z()-min.Z();}
286  inline unsigned char MaxDim() const {
287  int i;
289  if(diag[0]>diag[1]) i=0; else i=1;
290  return (diag[i]>diag[2])? i: 2;
291  }
293  inline unsigned char MinDim() const {
294  int i;
296  if(diag[0]<diag[1]) i=0; else i=1;
297  return (diag[i]<diag[2])? i: 2;
298  }
299 
300  template <class Q>
301  inline void Import( const Box3<Q> & b )
302  {
303  min.Import(b.min);
304  max.Import(b.max);
305  }
306 
307  template <class Q>
308  static inline Box3 Construct( const Box3<Q> & b )
309  {
310  return Box3(Point3<BoxScalarType>::Construct(b.min),Point3<BoxScalarType>::Construct(b.max));
311  }
312 
314  Point3<BoxScalarType> P(int i) const {
315  return Point3<BoxScalarType>(
316  min[0]+ (i%2) * DimX(),
317  min[1]+ ((i / 2)%2) * DimY(),
318  min[2]+ (i>3)* DimZ());
319  }
320 }; // end class definition
321 
322 template <class T> Box3<T> Point3<T>::GetBBox(Box3<T> &bb) const {
323  bb.Set( *this );
324  return bb;
325 }
326 
327 
328 typedef Box3<short> Box3s;
329 typedef Box3<int> Box3i;
330 typedef Box3<float> Box3f;
331 typedef Box3<double> Box3d;
332 
333 
336 } // end namespace
337 #endif
338 
Definition: box3.h:42
Point3< BoxScalarType > GlobalToLocal(const Point3< BoxScalarType > &p) const
Returns local coords expressed in [0..1]^3 of a point in 3D.
Definition: box3.h:267
Box3()
The bounding box constructor.
Definition: box3.h:53
void Add(const Point3< BoxScalarType > &p, const BoxScalarType radius)
Definition: box3.h:138
bool IsNull() const
Definition: box3.h:235
BoxScalarType DimY() const
Calcola la dimensione del bounding box sulla y.
Definition: box3.h:282
Point3< BoxScalarType > P(int i) const
gives the ith box vertex in order: (x,y,z),(X,y,z),(x,Y,z),(X,Y,z),(x,y,Z),(X,y,Z),...
Definition: box3.h:314
bool IsIn(const Point3< BoxScalarType > &p) const
Definition: box3.h:194
bool IsEmpty() const
Definition: box3.h:238
bool IsInEx(const Point3< BoxScalarType > &p) const
Definition: box3.h:205
void Offset(const Point3< BoxScalarType > &delta)
Definition: box3.h:83
BoxScalarType SquaredDiag() const
Calcola il quadrato della diagonale del bounding box.
Definition: box3.h:245
void Set(const Point3< BoxScalarType > &p)
Initializing the bounding box.
Definition: box3.h:89
Point3< BoxScalarType > max
max coordinate point
Definition: box3.h:51
Box3(const Point3< BoxScalarType > &center, const BoxScalarType &radius)
Point Radius Constructor.
Definition: box3.h:57
void Add(const Box3< BoxScalarType > &b)
Definition: box3.h:104
bool operator!=(const Box3< BoxScalarType > &p) const
Operator to dispare two bounding box.
Definition: box3.h:69
Point3< BoxScalarType > Dim() const
Compute bounding box size.
Definition: box3.h:255
void Translate(const Point3< BoxScalarType > &p)
Definition: box3.h:187
~Box3()
The bounding box distructor.
Definition: box3.h:62
Point3< BoxScalarType > Center() const
Return the center of the box.
Definition: box3.h:250
void Intersect(const Box3< BoxScalarType > &b)
Definition: box3.h:172
Box3(const Point3< BoxScalarType > &mi, const Point3< BoxScalarType > &ma)
Min Max constructor.
Definition: box3.h:55
BoxScalarType DimZ() const
Calcola la dimensione del bounding box sulla z.
Definition: box3.h:284
bool operator==(const Box3< BoxScalarType > &p) const
Operator to compare two bounding box.
Definition: box3.h:64
void Offset(const BoxScalarType s)
Definition: box3.h:75
void SetNull()
Set the bounding box to a null value.
Definition: box3.h:95
BoxScalarType Volume() const
Return the volume of the box.
Definition: box3.h:275
bool Collide(const Box3< BoxScalarType > &b) const
Definition: box3.h:226
Point3< BoxScalarType > LocalToGlobal(const Point3< BoxScalarType > &p) const
Returns global coords of a local point expressed in [0..1]^3.
Definition: box3.h:260
void Add(const Point3< BoxScalarType > &p)
Definition: box3.h:121
BoxScalarType DimX() const
Calcola la dimensione del bounding box sulla x.
Definition: box3.h:280
BoxScalarType ScalarType
The scalar type.
Definition: box3.h:46
void Add(const Matrix44< BoxScalarType > &m, const Box3< BoxScalarType > &b)
Definition: box3.h:154
unsigned char MaxDim() const
Calcola il lato di lunghezza maggiore.
Definition: box3.h:286
Point3< BoxScalarType > min
min coordinate point
Definition: box3.h:49
BoxScalarType Diag() const
Return the lenght of the diagonal of the box .
Definition: box3.h:240
unsigned char MinDim() const
Calcola il lato di lunghezza minore.
Definition: box3.h:293
Definition: namespaces.dox:6