| 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 |
#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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 80 |
protected: |
| 81 |
|
| 82 |
Point3<S> _min; |
| 83 |
|
| 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 |
|
| 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 |
|
| 100 |
inline Box( const PointType & mi, const PointType & ma ) { _min = mi; _max = ma; } |
| 101 |
|
| 102 |
inline ~Box() { } |
| 103 |
|
| 104 |
inline bool operator == ( Box const & p ) const |
| 105 |
{ |
| 106 |
return _min==p._min && _max==p._max; |
| 107 |
} |
| 108 |
|
| 109 |
inline bool operator != ( Box const & p ) const |
| 110 |
{ |
| 111 |
return _min!=p._min || _max!=p._max; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void Inflate( const S s ) |
| 120 |
{ |
| 121 |
Inflate( (_max-_min)*s ); |
| 122 |
} |
| 123 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
void Inflate( const PointType & delta ) |
| 135 |
{ |
| 136 |
_min -= delta; |
| 137 |
_max += delta; |
| 138 |
} |
| 139 |
|
| 140 |
void Set( const PointType & p ) |
| 141 |
{ |
| 142 |
_min = _max = p; |
| 143 |
} |
| 144 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
void Add( Box const & b ) |
| 156 |
{ |
| 157 |
if(IsNull()) *this=b; |
| 158 |
else |
| 159 |
Add(_min); Add(_max); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 181 |
|
| 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 |
|
| 197 |
|
| 198 |
|
| 199 |
void Translate( const PointType & p ) |
| 200 |
{ |
| 201 |
_min += p; |
| 202 |
_max += p; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|
| 251 |
|
| 252 |
|
| 253 |
bool IsNull() const { return _min.X()>_max.X() || _min.Y()>_max.Y() || _min.Z()>_max.Z(); } |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
bool IsEmpty() const { return _min==_max; } |
| 258 |
|
| 259 |
S Diag() const |
| 260 |
{ |
| 261 |
return Distance(_min,_max); |
| 262 |
} |
| 263 |
|
| 264 |
S SquaredDiag() const |
| 265 |
{ |
| 266 |
return SquaredDistance(_min,_max); |
| 267 |
} |
| 268 |
|
| 269 |
PointType Center() const |
| 270 |
{ |
| 271 |
return (_min+_max)/2; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 296 |
inline S Area() const { |
| 297 |
return Volume(); |
| 298 |
}; |
| 299 |
|
| 300 |
PointType Size() const |
| 301 |
{ |
| 302 |
return (_max-_min); |
| 303 |
} |
| 304 |
|
| 305 |
inline S SizeX() const { return _max.X()-_min.X();} |
| 306 |
|
| 307 |
inline S SizeY() const { return _max.Y()-_min.Y();} |
| 308 |
|
| 309 |
inline S SizeZ() const { static_assert(N>2); return _max.Z()-_min.Z();} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 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 |
|
| 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 |
}; |
| 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 |
} |
| 397 |
#endif |