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 
33 /** \addtogroup space */
34 /*@{*/
35 /**
36 Templated class for 3D boxes.
37   This is the class for definition of a axis aligned bounding box in 3D space. It is stored just as two Point3
38     @param BoxScalarType (template parameter) Specifies the type of scalar used to represent coords.
39 */
40 template <class BoxScalarType>
41 class Box3
42 {
43 public:
44 
45     /// The scalar type
46     typedef BoxScalarType ScalarType;
47 
48     /// min coordinate point
49   Point3<BoxScalarType> min;
50     /// max coordinate point
51     Point3<BoxScalarType> max;
52         /// The bounding box constructor
Box3()53     inline  Box3() { min.X()= 1;max.X()= -1;min.Y()= 1;max.Y()= -1;min.Z()= 1;max.Z()= -1;}
54         /// Copy constructor
Box3(const Box3 & b)55     inline  Box3( const Box3 & b ) { min=b.min; max=b.max; }
56         /// Min Max constructor
Box3(const Point3<BoxScalarType> & mi,const Point3<BoxScalarType> & ma)57     inline  Box3( const Point3<BoxScalarType> & mi, const Point3<BoxScalarType> & ma ) { min = mi; max = ma; }
58     /// Point Radius Constructor
Box3(const Point3<BoxScalarType> & center,const BoxScalarType & radius)59   inline Box3(const Point3<BoxScalarType> & center, const BoxScalarType & radius) {
60     min = center-Point3<BoxScalarType>(radius,radius,radius);
61     max = center+Point3<BoxScalarType>(radius,radius,radius);
62   }
63         /// The bounding box distructor
~Box3()64     inline ~Box3() { }
65         /// Operator to compare two bounding box
66     inline bool operator == ( Box3<BoxScalarType> const & p ) const
67     {
68         return min==p.min && max==p.max;
69     }
70         /// Operator to dispare two bounding box
71     inline bool operator != ( Box3<BoxScalarType> const & p ) const
72     {
73         return min!=p.min || max!=p.max;
74     }
75         /** Offset of a vector (s,s,s)
76         */
Offset(const BoxScalarType s)77     void Offset( const BoxScalarType s )
78     {
79         Offset( Point3<BoxScalarType> (s,s,s));
80     }
81         /** Offset the two corner of the box of a vector delta.
82          *  adding delta to max and -delta to min.
83             @param delta offset vector
84         */
Offset(const Point3<BoxScalarType> & delta)85     void Offset( const Point3<BoxScalarType> & delta )
86     {
87         min -= delta;
88         max += delta;
89     }
90     /// Initializing the bounding box
Set(const Point3<BoxScalarType> & p)91     void Set( const Point3<BoxScalarType> & p )
92     {
93         min = max = p;
94     }
95 
96     /// Set the bounding box to a null value
SetNull()97     void SetNull()
98     {
99         min.X()= 1; max.X()= -1;
100         min.Y()= 1; max.Y()= -1;
101         min.Z()= 1; max.Z()= -1;
102     }
103         /** Modify the current bbox to contain also the passed box.
104          *  Adding a null bounding box does nothing
105         */
Add(Box3<BoxScalarType> const & b)106     void Add( Box3<BoxScalarType> const & b )
107     {
108         if(b.IsNull()) return; // Adding a null bbox should do nothing
109         if(IsNull()) *this=b;
110         else
111         {
112             if(min.X() > b.min.X()) min.X() = b.min.X();
113             if(min.Y() > b.min.Y()) min.Y() = b.min.Y();
114             if(min.Z() > b.min.Z()) min.Z() = b.min.Z();
115 
116             if(max.X() < b.max.X()) max.X() = b.max.X();
117             if(max.Y() < b.max.Y()) max.Y() = b.max.Y();
118             if(max.Z() < b.max.Z()) max.Z() = b.max.Z();
119         }
120     }
121         /** Modify the current bbox to contain also the passed point
122         */
Add(const Point3<BoxScalarType> & p)123     void Add( const Point3<BoxScalarType> & p )
124     {
125         if(IsNull()) Set(p);
126         else
127         {
128             if(min.X() > p.X()) min.X() = p.X();
129             if(min.Y() > p.Y()) min.Y() = p.Y();
130             if(min.Z() > p.Z()) min.Z() = p.Z();
131 
132             if(max.X() < p.X()) max.X() = p.X();
133             if(max.Y() < p.Y()) max.Y() = p.Y();
134             if(max.Z() < p.Z()) max.Z() = p.Z();
135         }
136     }
137 
138     /** Modify the current bbox to contain also the passed sphere
139     */
Add(const Point3<BoxScalarType> & p,const BoxScalarType radius)140 void Add( const Point3<BoxScalarType> & p, const BoxScalarType radius )
141 {
142     if(IsNull()) Set(p);
143     else
144     {
145       min.X() = std::min(min.X(),p.X()-radius);
146       min.Y() = std::min(min.Y(),p.Y()-radius);
147       min.Z() = std::min(min.Z(),p.Z()-radius);
148 
149       max.X() = std::max(max.X(),p.X()+radius);
150       max.Y() = std::max(max.Y(),p.Y()+radius);
151       max.Z() = std::max(max.Z(),p.Z()+radius);
152     }
153 }
154   /** Modify the current bbox to contain also the box b trasformed according to the matrix m
155   */
Add(const Matrix44<BoxScalarType> & m,const Box3<BoxScalarType> & b)156     void Add( const Matrix44<BoxScalarType> &m, const Box3<BoxScalarType> & b )
157     {
158       if(b.IsNull()) return; // Adding a null bbox should do nothing
159 
160       const Point3<BoxScalarType> &mn= b.min;
161       const Point3<BoxScalarType> &mx= b.max;
162       Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mn[2])));
163       Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mn[2])));
164       Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mn[2])));
165       Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mn[2])));
166       Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mx[2])));
167       Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mx[2])));
168       Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mx[2])));
169       Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mx[2])));
170     }
171         /** Calcola l'intersezione tra due bounding box. Al bounding box viene assegnato il valore risultante.
172             @param b Il bounding box con il quale si vuole effettuare l'intersezione
173         */
Intersect(const Box3<BoxScalarType> & b)174     void Intersect( const Box3<BoxScalarType> & b )
175     {
176         if(min.X() < b.min.X()) min.X() = b.min.X();
177         if(min.Y() < b.min.Y()) min.Y() = b.min.Y();
178         if(min.Z() < b.min.Z()) min.Z() = b.min.Z();
179 
180         if(max.X() > b.max.X()) max.X() = b.max.X();
181         if(max.Y() > b.max.Y()) max.Y() = b.max.Y();
182         if(max.Z() > b.max.Z()) max.Z() = b.max.Z();
183 
184         if(min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z()) SetNull();
185     }
186         /** Trasla il bounding box di un valore definito dal parametro.
187             @param p Il bounding box trasla sulla x e sulla y in base alle coordinate del parametro
188         */
Translate(const Point3<BoxScalarType> & p)189     void Translate( const Point3<BoxScalarType> & p )
190     {
191         min += p;
192         max += p;
193     }
194         /** true if the point belong to the closed box
195         */
IsIn(Point3<BoxScalarType> const & p)196     bool IsIn( Point3<BoxScalarType> const & p ) const
197     {
198         return (
199             min.X() <= p.X() && p.X() <= max.X() &&
200             min.Y() <= p.Y() && p.Y() <= max.Y() &&
201             min.Z() <= p.Z() && p.Z() <= max.Z()
202         );
203     }
204         /** true if the point belong to the open box (open on the max side)
205          * e.g. if p in [min,max)
206         */
IsInEx(Point3<BoxScalarType> const & p)207     bool IsInEx( Point3<BoxScalarType> const & p ) const
208     {
209         return (
210             min.X() <= p.X() && p.X() < max.X() &&
211             min.Y() <= p.Y() && p.Y() < max.Y() &&
212             min.Z() <= p.Z() && p.Z() < max.Z()
213         );
214     }
215         /** Verifica se due bounding box collidono cioe' se hanno una intersezione non vuota. Per esempio
216             due bounding box adiacenti non collidono.
217             @param b A bounding box
218             @return True se collidoo, false altrimenti
219         */
220     /* old version
221     bool Collide(Box3<BoxScalarType> const &b)
222     {
223         Box3<BoxScalarType> bb=*this;
224         bb.Intersect(b);
225         return bb.IsValid();
226     }
227     */
Collide(Box3<BoxScalarType> const & b)228     bool Collide(Box3<BoxScalarType> const &b) const
229     {
230         return b.min.X()<max.X() && b.max.X()>min.X() &&
231                b.min.Y()<max.Y() && b.max.Y()>min.Y() &&
232                b.min.Z()<max.Z() && b.max.Z()>min.Z() ;
233     }
234         /**
235           return true if the box is null (e.g. invalid or not initialized);
236         */
IsNull()237     bool IsNull() const { return min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z(); }
238         /** return true if the box is empty (e.g. if min == max)
239         */
IsEmpty()240     bool IsEmpty() const { return min==max; }
241         /// Return the lenght of the diagonal of the box .
Diag()242     BoxScalarType Diag() const
243     {
244         return Distance(min,max);
245     }
246         /// Calcola il quadrato della diagonale del bounding box.
SquaredDiag()247     BoxScalarType SquaredDiag() const
248     {
249         return SquaredDistance(min,max);
250     }
251         /// Return the center of the box.
Center()252     Point3<BoxScalarType> Center() const
253     {
254         return (min+max)/2;
255     }
256         /// Compute bounding box size.
Dim()257     Point3<BoxScalarType> Dim() const
258     {
259         return (max-min);
260     }
261       /// Returns global coords of a local point expressed in [0..1]^3
LocalToGlobal(Point3<BoxScalarType> const & p)262     Point3<BoxScalarType> LocalToGlobal(Point3<BoxScalarType> const & p) const{
263         return Point3<BoxScalarType>(
264             min[0] + p[0]*(max[0]-min[0]),
265             min[1] + p[1]*(max[1]-min[1]),
266             min[2] + p[2]*(max[2]-min[2]));
267     }
268       /// Returns local coords expressed in [0..1]^3 of a point in 3D
GlobalToLocal(Point3<BoxScalarType> const & p)269     Point3<BoxScalarType> GlobalToLocal(Point3<BoxScalarType> const & p) const{
270         return Point3<BoxScalarType>(
271           (p[0]-min[0])/(max[0]-min[0]),
272           (p[1]-min[1])/(max[1]-min[1]),
273           (p[2]-min[2])/(max[2]-min[2])
274             );
275     }
276         /// Return the volume of the box.
Volume()277     BoxScalarType Volume() const
278     {
279         return (max.X()-min.X())*(max.Y()-min.Y())*(max.Z()-min.Z());
280     }
281         /// Calcola la dimensione del bounding box sulla x.
DimX()282     inline BoxScalarType DimX() const { return max.X()-min.X();}
283         /// Calcola la dimensione del bounding box sulla y.
DimY()284     inline BoxScalarType DimY() const { return max.Y()-min.Y();}
285         /// Calcola la dimensione del bounding box sulla z.
DimZ()286     inline BoxScalarType DimZ() const { return max.Z()-min.Z();}
287         /// Calcola il lato di lunghezza maggiore
MaxDim()288     inline unsigned char MaxDim() const {
289         int i;
290         Point3<BoxScalarType> diag = max-min;
291         if(diag[0]>diag[1]) i=0; else i=1;
292         return (diag[i]>diag[2])? i: 2;
293     }
294         /// Calcola il lato di lunghezza minore
MinDim()295     inline unsigned char MinDim() const {
296         int i;
297         Point3<BoxScalarType> diag =  max-min;
298         if(diag[0]<diag[1]) i=0; else i=1;
299         return (diag[i]<diag[2])? i: 2;
300     }
301 
302     template <class Q>
Import(const Box3<Q> & b)303     inline void Import( const Box3<Q> & b )
304     {
305         min.Import(b.min);
306         max.Import(b.max);
307     }
308 
309     template <class Q>
Construct(const Box3<Q> & b)310     static inline Box3 Construct( const Box3<Q> & b )
311     {
312     return Box3(Point3<BoxScalarType>::Construct(b.min),Point3<BoxScalarType>::Construct(b.max));
313     }
314 
315         /// 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),(x,Y,Z),(X,Y,Z)
P(const int & i)316     Point3<BoxScalarType> P(const int & i) const {
317             return Point3<BoxScalarType>(
318                 min[0]+ (i%2) * DimX(),
319                 min[1]+ ((i / 2)%2) * DimY(),
320                 min[2]+ (i>3)* DimZ());
321     }
322 }; // end class definition
323 
GetBBox(Box3<T> & bb)324 template <class T> Box3<T> Point3<T>::GetBBox(Box3<T> &bb) const {
325  bb.Set( *this );
326 }
327 
328 
329 typedef Box3<short>  Box3s;
330 typedef Box3<int>	 Box3i;
331 typedef Box3<float>  Box3f;
332 typedef Box3<double> Box3d;
333 
334 
335 /*@}*/
336 
337 } // end namespace
338 #endif
339 
340