File: box3.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (340 lines) | stat: -rw-r--r-- 12,331 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef __VCGLIB_BOX3
#define __VCGLIB_BOX3

#include <vcg/space/point3.h>
#include <vcg/math/matrix44.h>
#include <vcg/space/line3.h>
#include <vcg/space/plane3.h>

namespace vcg {

/** \addtogroup space */
/*@{*/
/**
Templated class for 3D boxes.
  This is the class for definition of a axis aligned bounding box in 3D space. It is stored just as two Point3
    @param BoxScalarType (template parameter) Specifies the type of scalar used to represent coords.
*/
template <class BoxScalarType>
class Box3
{
public:

    /// The scalar type
    typedef BoxScalarType ScalarType;

    /// min coordinate point
  Point3<BoxScalarType> min;
    /// max coordinate point
    Point3<BoxScalarType> max;
        /// The bounding box constructor
    inline  Box3() { this->SetNull(); }
        /// Copy constructor
    //inline  Box3( const Box3 & b ) { min=b.min; max=b.max; }
        /// Min Max constructor
    inline  Box3( const Point3<BoxScalarType> & mi, const Point3<BoxScalarType> & ma ) { min = mi; max = ma; }
    /// Point Radius Constructor
  inline Box3(const Point3<BoxScalarType> & center, const BoxScalarType & radius) {
    min = center-Point3<BoxScalarType>(radius,radius,radius);
    max = center+Point3<BoxScalarType>(radius,radius,radius);
  }
        /// The bounding box distructor
    inline ~Box3() { }
        /// Operator to compare two bounding box
    inline bool operator == ( Box3<BoxScalarType> const & p ) const
    {
        return min==p.min && max==p.max;
    }
        /// Operator to dispare two bounding box
    inline bool operator != ( Box3<BoxScalarType> const & p ) const
    {
        return min!=p.min || max!=p.max;
    }
        /** Offset of a vector (s,s,s)
        */
    void Offset( const BoxScalarType s )
    {
        Offset( Point3<BoxScalarType> (s,s,s));
    }
        /** Offset the two corner of the box of a vector delta. 
         *  adding delta to max and -delta to min. 
            @param delta offset vector
        */
    void Offset( const Point3<BoxScalarType> & delta )
    {
        min -= delta;
        max += delta;
    }
    /// Initializing the bounding box
    void Set( const Point3<BoxScalarType> & p )
    {
        min = max = p;
    }
    
    /// Set the bounding box to a null value
    void SetNull()
    {
        min.X()= 1; max.X()= -1;
        min.Y()= 1; max.Y()= -1;
        min.Z()= 1; max.Z()= -1;
    }
        /** Modify the current bbox to contain also the passed box.
         *  Adding a null bounding box does nothing
        */
    void Add( Box3<BoxScalarType> const & b )
    {
        if(b.IsNull()) return; // Adding a null bbox should do nothing
        if(IsNull()) *this=b;
        else
        {
            if(min.X() > b.min.X()) min.X() = b.min.X();
            if(min.Y() > b.min.Y()) min.Y() = b.min.Y();
            if(min.Z() > b.min.Z()) min.Z() = b.min.Z();

            if(max.X() < b.max.X()) max.X() = b.max.X();
            if(max.Y() < b.max.Y()) max.Y() = b.max.Y();
            if(max.Z() < b.max.Z()) max.Z() = b.max.Z();
        }
    }
        /** Modify the current bbox to contain also the passed point
        */
    void Add( const Point3<BoxScalarType> & p )
    {
        if(IsNull()) Set(p);
        else
        {
            if(min.X() > p.X()) min.X() = p.X();
            if(min.Y() > p.Y()) min.Y() = p.Y();
            if(min.Z() > p.Z()) min.Z() = p.Z();

            if(max.X() < p.X()) max.X() = p.X();
            if(max.Y() < p.Y()) max.Y() = p.Y();
            if(max.Z() < p.Z()) max.Z() = p.Z();
        }
    }

    /** Modify the current bbox to contain also the passed sphere
    */
void Add( const Point3<BoxScalarType> & p, const BoxScalarType radius )
{
    if(IsNull()) Set(p);
    else
    {
      min.X() = std::min(min.X(),p.X()-radius);
      min.Y() = std::min(min.Y(),p.Y()-radius);
      min.Z() = std::min(min.Z(),p.Z()-radius);

      max.X() = std::max(max.X(),p.X()+radius);
      max.Y() = std::max(max.Y(),p.Y()+radius);
      max.Z() = std::max(max.Z(),p.Z()+radius);
    }
}
  /** Modify the current bbox to contain also the box b transformed according to the matrix m
  */
    void Add( const Matrix44<BoxScalarType> &m, const Box3<BoxScalarType> & b )
    {
      if(b.IsNull()) return; // Adding a null bbox should do nothing
      
      const Point3<BoxScalarType> &mn= b.min;
      const Point3<BoxScalarType> &mx= b.max;
      Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mn[2])));
      Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mn[2])));
      Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mn[2])));
      Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mn[2])));
      Add(m*(Point3<BoxScalarType>(mn[0],mn[1],mx[2])));
      Add(m*(Point3<BoxScalarType>(mx[0],mn[1],mx[2])));
      Add(m*(Point3<BoxScalarType>(mn[0],mx[1],mx[2])));
      Add(m*(Point3<BoxScalarType>(mx[0],mx[1],mx[2])));
    }
        /** Calcola l'intersezione tra due bounding box. Al bounding box viene assegnato il valore risultante.
            @param b Il bounding box con il quale si vuole effettuare l'intersezione
        */
    void Intersect( const Box3<BoxScalarType> & b )
    {
        if(min.X() < b.min.X()) min.X() = b.min.X();
        if(min.Y() < b.min.Y()) min.Y() = b.min.Y();
        if(min.Z() < b.min.Z()) min.Z() = b.min.Z();

        if(max.X() > b.max.X()) max.X() = b.max.X();
        if(max.Y() > b.max.Y()) max.Y() = b.max.Y();
        if(max.Z() > b.max.Z()) max.Z() = b.max.Z();

        if(min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z()) SetNull();
    }
        /** Trasla il bounding box di un valore definito dal parametro.
            @param p Il bounding box trasla sulla x e sulla y in base alle coordinate del parametro
        */
    void Translate( const Point3<BoxScalarType> & p )
    {
        min += p;
        max += p;
    }
        /** true if the point belong to the closed box 
        */
    bool IsIn( Point3<BoxScalarType> const & p ) const
    {
        return (
            min.X() <= p.X() && p.X() <= max.X() &&
            min.Y() <= p.Y() && p.Y() <= max.Y() &&
            min.Z() <= p.Z() && p.Z() <= max.Z()
        );
    }
        /** true if the point belong to the open box (open on the max side) 
         * e.g. if p in [min,max)
        */
    bool IsInEx( Point3<BoxScalarType> const & p ) const
    {
        return (
            min.X() <= p.X() && p.X() < max.X() &&
            min.Y() <= p.Y() && p.Y() < max.Y() &&
            min.Z() <= p.Z() && p.Z() < max.Z()
        );
    }
        /** Verifica se due bounding box collidono cioe' se hanno una intersezione non vuota. Per esempio
            due bounding box adiacenti non collidono.
            @param b A bounding box
            @return True se collidoo, false altrimenti
        */
    /* old version
    bool Collide(Box3<BoxScalarType> const &b)
    {
        Box3<BoxScalarType> bb=*this;
        bb.Intersect(b);
        return bb.IsValid();
    }
    */
    bool Collide(Box3<BoxScalarType> const &b) const
    {
        return b.min.X()<max.X() && b.max.X()>min.X() &&
               b.min.Y()<max.Y() && b.max.Y()>min.Y() &&
               b.min.Z()<max.Z() && b.max.Z()>min.Z() ;
    }
        /** 
          return true if the box is null (e.g. invalid or not initialized);
        */
    bool IsNull() const { return min.X()>max.X() || min.Y()>max.Y() || min.Z()>max.Z(); }
        /** return true if the box is empty (e.g. if min == max)
        */
    bool IsEmpty() const { return min==max; }
        /// Return the lenght of the diagonal of the box .
    BoxScalarType Diag() const
    {
        return Distance(min,max);
    }
        /// Calcola il quadrato della diagonale del bounding box.
    BoxScalarType SquaredDiag() const
    {
        return SquaredDistance(min,max);
    }
        /// Return the center of the box.
    Point3<BoxScalarType> Center() const
    {
        return (min+max)/2;
    }
        /// Compute bounding box size.
    Point3<BoxScalarType> Dim() const
    {
        return (max-min);
    }
      /// Returns global coords of a local point expressed in [0..1]^3
    Point3<BoxScalarType> LocalToGlobal(Point3<BoxScalarType> const & p) const{
        return Point3<BoxScalarType>(
            min[0] + p[0]*(max[0]-min[0]),
            min[1] + p[1]*(max[1]-min[1]),
            min[2] + p[2]*(max[2]-min[2]));
    }
      /// Returns local coords expressed in [0..1]^3 of a point in 3D
    Point3<BoxScalarType> GlobalToLocal(Point3<BoxScalarType> const & p) const{
        return Point3<BoxScalarType>(
          (p[0]-min[0])/(max[0]-min[0]),
          (p[1]-min[1])/(max[1]-min[1]),
          (p[2]-min[2])/(max[2]-min[2])
            );
    }
        /// Return the volume of the box.
    BoxScalarType Volume() const
    {
        return (max.X()-min.X())*(max.Y()-min.Y())*(max.Z()-min.Z());
    }
        /// Calcola la dimensione del bounding box sulla x.
    inline BoxScalarType DimX() const { return max.X()-min.X();}
        /// Calcola la dimensione del bounding box sulla y.
    inline BoxScalarType DimY() const { return max.Y()-min.Y();}
        /// Calcola la dimensione del bounding box sulla z.
    inline BoxScalarType DimZ() const { return max.Z()-min.Z();}
        /// Calcola il lato di lunghezza maggiore
    inline unsigned char MaxDim() const {
        int i;
        Point3<BoxScalarType> diag = max-min;
        if(diag[0]>diag[1]) i=0; else i=1;
        return (diag[i]>diag[2])? i: 2;
    }
        /// Calcola il lato di lunghezza minore
    inline unsigned char MinDim() const {
        int i;
        Point3<BoxScalarType> diag =  max-min;
        if(diag[0]<diag[1]) i=0; else i=1;
        return (diag[i]<diag[2])? i: 2;
    }

    template <class Q>
    inline void Import( const Box3<Q> & b )
    {
        min.Import(b.min);
        max.Import(b.max);
    }

    template <class Q>
    static inline Box3 Construct( const Box3<Q> & b )
    {
    return Box3(Point3<BoxScalarType>::Construct(b.min),Point3<BoxScalarType>::Construct(b.max));
    }

        /// 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)
    Point3<BoxScalarType> P(const int & i) const {
            return Point3<BoxScalarType>(
                min[0]+ (i%2) * DimX(),
                min[1]+ ((i / 2)%2) * DimY(),
                min[2]+ (i>3)* DimZ());
    }
}; // end class definition

template <class T> Box3<T> Point3<T>::GetBBox(Box3<T> &bb) const {
 bb.Set( *this );
 return bb;
}


typedef Box3<short>  Box3s;
typedef Box3<int>	 Box3i;
typedef Box3<float>  Box3f;
typedef Box3<double> Box3d;


/*@}*/

} // end namespace
#endif