Debian Codegraph @

vcg/vcg/space/box.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
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 /****************************************************************************
24 History
25
26 $Log: box.h,v $
27 Revision 1.2 2005/01/12 11:23:46 ganovelli
28 correction on point<3
29
30 Revision 1.1 2004/03/16 03:07:38 tarini
31 "dimensionally unified" version: first commit
32
33 Revision 1.5 2004/03/05 17:51:28 tarini
34 Errorino "ScalarType" -> "S"
35
36 Revision 1.4 2004/03/03 14:32:13 cignoni
37 Yet another cr lf mismatch
38
39 Revision 1.3 2004/02/23 23:44:21 cignoni
40 cr lf mismatch
41
42 Revision 1.2 2004/02/19 15:40:56 cignoni
43 Added doxygen groups
44
45 Revision 1.1 2004/02/13 02:16:22 cignoni
46 First working release.
47
48
49 ****************************************************************************/
50
51
52 #ifndef __VCGLIB_BOX
53 #define __VCGLIB_BOX
54
55 #include <vcg/space/point.h>
56 #include <vcg/space/space.h>
57 #include <vcg/math/linear.h>
58
59 namespace vcg {
60
61 /** \addtogroup space */
62 /*@{*/
63 /**
64 Templated class for 3D boxes.
65 This is the class for definition of a axis aligned box in 2D or 3D space.
66 Typically used as bounding boxes.
67 It is stored just as two Points (at the opposite vertices).
68 @param S (template parameter) Specifies the type of scalar used to represent coords.
69 */
70 template <int N, class S>
71 class Box : public Space<N,S> , Linear<Box>
72 {
73 public:
74 typedef S ScalarType;
75 typedef Point<N,S> ParamType;
76 typedef Point<N,S> PointType;
77 enum {Dimension=N};
78
79 /// The scalar type
80 protected:
81 /// _min coordinate point
82 Point3<S> _min;
83 /// _max coordinate point
84 Point3<S> _max;
85
86 public:
87
88 inline const PointType &Max() const { return _max; }
89 inline PointType &Max() { return _max; }
90 inline const PointType &Min() const { return _min; }
91 inline PointType &Min() { return _min; }
92
93 /// The box constructor
94 inline Box() {
95 _min.X()= 1;_max.X()= -1;
96 _min.Y()= 1;_max.Y()= -1;
97 if (N>2) {_min.Z()= 1;_max.Z()= -1;}
98 }
99 /// Min Max constructor
100 inline Box( const PointType & mi, const PointType & ma ) { _min = mi; _max = ma; }
101 /// The box distructor
102 inline ~Box() { }
103 /// Operator to compare two boxes
104 inline bool operator == ( Box const & p ) const
105 {
106 return _min==p._min && _max==p._max;
107 }
108 /// Operator to dispare two boxes
109 inline bool operator != ( Box const & p ) const
110 {
111 return _min!=p._min || _max!=p._max;
112 }
113 /** Infaltes the box of a percentage..
114 @param s Scalar value. E.g if s=0.1 the box enlarges of 10% in every direction
115 if S==0.5 box doubles (+50% in every direction)
116 if S < 0 box shrinks
117 if S==0.5 box reduces to a point
118 */
119 void Inflate( const S s )
120 {
121 Inflate( (_max-_min)*s );
122 }
123 /** Enlarges the box dimensions by k in every direction, with k = bbox.diag*s
124 */
125 void InflateFix( const S s )
126 {
127 S k = Diag()*s;
128 if (N==2) Inflate( PointType (k,k));
129 if (N==3) Inflate( PointType (k,k,k));
130 }
131 /** Enlarges the box dimensions by a fixed delta.
132 @param delta Point in D space. If delta > 0 box enlarges. If delta < 0 box reduces.
133 */
134 void Inflate( const PointType & delta )
135 {
136 _min -= delta;
137 _max += delta;
138 }
139 /// Initializing the box
140 void Set( const PointType & p )
141 {
142 _min = _max = p;
143 }
144 /// Set the box to a null value
145 void SetNull()
146 {
147 _min.X()= 1; _max.X()= -1;
148 _min.Y()= 1; _max.Y()= -1;
149 _min.Z()= 1; _max.Z()= -1;
150 }
151 /** Add two boxex:
152 Returns minimal box that contains both operands.
153 @param b The box to add
154 */
155 void Add( Box const & b )
156 {
157 if(IsNull()) *this=b;
158 else
159 Add(_min); Add(_max);
160 }
161 /** Add a point to a box.
162 The box is modified is the added point is aoutside it.
163 @param p The point to add
164 */
165 void Add( const PointType & p )
166 {
167 if(IsNull()) Set(p);
168 else
169 {
170 if(_min.X() > p.X()) _min.X() = p.X();
171 else if(_max.X() < p.X()) _max.X() = p.X();
172 if(_min.Y() > p.Y()) _min.Y() = p.Y();
173 else if(_max.Y() < p.Y()) _max.Y() = p.Y();
174 if (N>2) {
175 if(_min.Z() > p.Z()) _min.Z() = p.Z();
176 else if(_max.Z() < p.Z()) _max.Z() = p.Z();
177 };
178 }
179 }
180 /** Coputes intersection of Boxes: the minimal box containing both operands.
181 @param b The other operand
182 */
183 void Intersect( const Box & b )
184 {
185 if(_min.X() < b._min.X()) _min.X() = b._min.X();
186 if(_min.Y() < b._min.Y()) _min.Y() = b._min.Y();
187 if (N>2) if(_min.Z() < b._min.Z()) _min.Z() = b._min.Z();
188
189 if(_max.X() > b._max.X()) _max.X() = b._max.X();
190 if(_max.Y() > b._max.Y()) _max.Y() = b._max.Y();
191 if (N>2) if(_max.Z() > b._max.Z()) _max.Z() = b._max.Z();
192
193 if(_min.X()>_max.X() || _min.Y()>_max.Y() ) SetNull();
194 else if (N>2) if (_min.Z()>_max.Z()) SetNull();
195 }
196 /** Traslalate the box.
197 @param p: the translation vector
198 */
199 void Translate( const PointType & p )
200 {
201 _min += p;
202 _max += p;
203 }
204 /** Check wheter a point is inside box.
205 @param p The point
206 @returns True if inside, false otherwise
207 */
208 bool IsIn( PointType const & p ) const
209 {
210 if (N==2) return (
211 _min.X() <= p.X() && p.X() <= _max.X() &&
212 _min.Y() <= p.Y() && p.Y() <= _max.Y()
213 );
214 if (N==3) return (
215 _min.X() <= p.X() && p.X() <= _max.X() &&
216 _min.Y() <= p.Y() && p.Y() <= _max.Y() &&
217 _min.Z() <= p.Z() && p.Z() <= _max.Z()
218 );
219 }
220 /** Check wheter a point is inside box, open at left and closed at right [min..max)
221 @param p The point 3D
222 @returns True if inside, false otherwise
223 */
224 bool IsInEx( PointType const & p ) const
225 {
226 if (N==2) return (
227 _min.X() <= p.X() && p.X() < _max.X() &&
228 _min.Y() <= p.Y() && p.Y() < _max.Y()
229 );
230 if (N==3) return (
231 _min.X() <= p.X() && p.X() < _max.X() &&
232 _min.Y() <= p.Y() && p.Y() < _max.Y() &&
233 _min.Z() <= p.Z() && p.Z() < _max.Z()
234 );
235 }
236 /**
237 TODO: Move TO COLLIDE!!!
238 Verifica se due box collidono cioe' se hanno una intersezione non vuota. Per esempio
239 due box adiacenti non collidono.
240 @param b A box
241 @return True se collidoo, false altrimenti
242 */
243
244 bool Collide(Box const &b)
245 {
246 return b._min.X()<_max.X() && b._max.X()>_min.X() &&
247 b._min.Y()<_max.Y() && b._max.Y()>_min.Y() &&
248 b._min.Z()<_max.Z() && b._max.Z()>_min.Z() ;
249 }
250 /** Controlla se il box e' nullo.
251 @return True se il box e' nullo, false altrimenti
252 */
253 bool IsNull() const { return _min.X()>_max.X() || _min.Y()>_max.Y() || _min.Z()>_max.Z(); }
254 /** Controlla se il box e' vuoto.
255 @return True se il box e' vuoto, false altrimenti
256 */
257 bool IsEmpty() const { return _min==_max; }
258 /// Restituisce la lunghezza della diagonale del box.
259 S Diag() const
260 {
261 return Distance(_min,_max);
262 }
263 /// Calcola il quadrato della diagonale del box.
264 S SquaredDiag() const
265 {
266 return SquaredDistance(_min,_max);
267 }
268 /// Calcola il centro del box.
269 PointType Center() const
270 {
271 return (_min+_max)/2;
272 }
273
274 /// Returns global coords of a local point expressed in [0..1]^3
275 PointType LocalToGlobal(PointType const & p) const{
276 return PointType(
277 _min[0] + p[0]*(_max[0]-_min[0]),
278 _min[1] + p[1]*(_max[1]-_min[1]),
279 _min[2] + p[2]*(_max[2]-_min[2]));
280 }
281 /// Returns local coords expressed in [0..1]^3 of a point in 3D
282 PointType GlobalToLocal(PointType const & p) const{
283 return PointType(
284 (p[0]-_min[0])/(_max[0]-_min[0]),
285 (p[1]-_min[1])/(_max[1]-_min[1]),
286 (p[2]-_min[2])/(_max[2]-_min[2])
287 );
288 }
289 /// Computes the Volume for the box.
290 inline S Volume() const
291 {
292 if (N==2) return (_max.X()-_min.X())*(_max.Y()-_min.Y());
293 if (N==3) return (_max.X()-_min.X())*(_max.Y()-_min.Y())*(_max.Z()-_min.Z());
294 }
295 /// Area() and Volume() are sinonims (a
296 inline S Area() const {
297 return Volume();
298 };
299 /// Compute box size.
300 PointType Size() const
301 {
302 return (_max-_min);
303 }
304 /// Compute box size X.
305 inline S SizeX() const { return _max.X()-_min.X();}
306 /// Compute box size Y.
307 inline S SizeY() const { return _max.Y()-_min.Y();}
308 /// Compute box size Z.
309 inline S SizeZ() const { static_assert(N>2); return _max.Z()-_min.Z();}
310
311 /** @name Linearity for boxes
312 **/
313
314 /// sets a point to Zero
315 inline void Zero()
316 {
317 _min.Zero();
318 _max.Zero();
319 }
320 inline Box operator + ( Box const & p) const
321 {
322 return Box(_min+p._min,_max+p._max);
323 }
324 inline Box operator - ( Box const & p) const
325 {
326 return Box(_min-p._min,_max-p._max);
327 }
328 inline Box operator * ( const S s ) const
329 {
330 return Box(_min*s,_max*s);
331 }
332 inline Box operator / ( const S s ) const
333 {
334 S inv=S(1.0)/s;
335 return Box(_min*inv,_max*inv);
336 }
337 inline Box & operator += ( Box const & p)
338 {
339 _min+=p._min; _max+=p._max; return *this;
340 }
341 inline Box & operator -= ( Box const & p)
342 {
343 _min-=p._min; _max-=p._max; return *this;
344 }
345 inline Box & operator *= ( const S s )
346 {
347 _min*=s; _max*=s; return *this;
348 }
349 inline Box & operator /= ( const S s )
350 {
351 S inv=S(1.0)/s;
352 _min*=s; _max*=s; return *this;
353 return *this;
354 }
355 inline Box operator - () const
356 {
357 return Box(-_min,-_max);
358 }
359 //@}
360
361 //@{
362 /** @name Iporters (for boxes in different spaces and with different scalar types)
363 **/
364
365 /// imports the box
366 template <int N0, class S0>
367 inline void Import( const Box<N0,S0> & b )
368 { _max.Import( b._max );_min.Import( b._min );
369 }
370 template <int N0, class S0>
371 /// constructs a new ray importing it from an existing one
372 static Box Construct( const Box<N0,S0> & b )
373 {
374 return Box(PointType::Construct(b._min),PointType::Construct(b._max));
375 }
376
377
378 }; // end class definition
379
380
381
382
383 typedef Box<3,short> Box3s;
384 typedef Box<3,int> Box3i;
385 typedef Box<3,float> Box3f;
386 typedef Box<3,double> Box3d;
387
388 typedef Box<2,short> Box2s;
389 typedef Box<2,int> Box2i;
390 typedef Box<2,float> Box2f;
391 typedef Box<2,double> Box2d;
392
393
394 /*@}*/
395
396 } // end namespace
397 #endif