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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/****************************************************************************
* 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_BOX2
#define __VCGLIB_BOX2
#include <vcg/math/base.h>
#include <vcg/space/point2.h>
namespace vcg {
// needed prototype;
template <class SegScalarType> class Segment2;
/** \addtogroup space */
/*@{*/
/**
Templated class for a 2D bounding box. It is stored just as two Point2
@param BoxScalarType (Template Parameter) Specifies the scalar field.
*/
template <class BoxScalarType>
class Box2
{
public:
/// The scalar type
typedef BoxScalarType ScalarType;
typedef Point2<BoxScalarType> PointType ;
/// min coordinate point
PointType min;
/// max coordinate point
PointType max;
/// Standard constructor
inline Box2() { min.X()= 1; max.X()= -1; min.Y()= 1; max.Y()= -1; }
/// Copy constructor
inline Box2( const Box2 & b ) { min=b.min; max=b.max; }
/// Min Max constructor
inline Box2( const Point2<BoxScalarType> & mi, const Point2<BoxScalarType> & ma ) { min = mi; max = ma; }
inline Box2(const Point2<BoxScalarType> & center, const BoxScalarType & radius) {
min = center-Point2<BoxScalarType>(radius,radius);
max = center+Point2<BoxScalarType>(radius,radius);
}
/// Distructor
inline ~Box2() { }
/// Operator to compare two bounding box
inline bool operator == ( Box2 const & p ) const
{
return min==p.min && max==p.max;
}
/// Initializing the bounding box with a point
void Set( const PointType & p )
{
min = max = p;
}
Point2<BoxScalarType> P(const int & i) const
{
return Point2<BoxScalarType>(
min[0]+ (i%2) * DimX(),
min[1]+ ((i / 2)%2) * DimY());
}
// Initializing with the values
inline void Set( ScalarType minx, ScalarType miny, ScalarType maxx, ScalarType maxy )
{
min[0] = minx;
min[1] = miny;
max[0] = maxx;
max[1] = maxy;
}
/// Set the bounding box to a null value
void SetNull()
{
min.X()= 1; max.X()= -1; min.Y()= 1; max.Y()= -1;
}
/** Function to add two bounding box
the bounding box expand to include the other bounding box (if necessary)
@param b The bounding box to be added
*/
void Add( Box2 const & b )
{
if(IsNull())
{
min=b.min;
max=b.max;
}
else
{
if(min.X() > b.min.X()) min.X() = b.min.X();
if(min.Y() > b.min.Y()) min.Y() = b.min.Y();
if(max.X() < b.max.X()) max.X() = b.max.X();
if(max.Y() < b.max.Y()) max.Y() = b.max.Y();
}
}
/** Adds a point to the bouning box.
the bounding box expand to include the new point (if necessary)
@param p The point 2D
*/
void Add( const PointType & 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(max.X() < p.X()) max.X() = p.X();
if(max.Y() < p.Y()) max.Y() = p.Y();
}
}
/** Varies the dimension of the bounding box.
@param delta The size delta (if positive, box is enlarged)
*/
void Offset(const ScalarType s)
{
Offset(PointType(s, s));
}
/** Varies the dimension of the bounding box.
@param delta The size delta per dimension (if positive, box is enlarged)
*/
void Offset(const PointType & delta)
{
min -= delta;
max += delta;
}
/** Computes intersection between this and another bounding box
@param b The other bounding box
*/
void Intersect( const Box2 & b )
{
if(min.X() < b.min.X()) min.X() = b.min.X();
if(min.Y() < b.min.Y()) min.Y() = b.min.Y();
if(max.X() > b.max.X()) max.X() = b.max.X();
if(max.Y() > b.max.Y()) max.Y() = b.max.Y();
if(min.X()>max.X() || min.Y()>max.Y()) SetNull();
}
/** Traslate the bounding box by a vectore
@param p The transolation vector
*/
void Translate( const PointType & p )
{
min += p;
max += p;
}
/** Checks whether a 2D point p is inside the box
@param p The point 2D
@return True iff p inside
*/
bool IsIn( PointType const & p ) const
{
return (
min.X() <= p.X() && p.X() <= max.X() &&
min.Y() <= p.Y() && p.Y() <= max.Y()
);
}
/** Checks whether a 2D point p is inside the box, closed at min but open at max
@param p The point in 2D
@return True iff p inside
*/
bool IsInEx( PointType const & p ) const
{
return (
min.X() <= p.X() && p.X() < max.X() &&
min.Y() <= p.Y() && p.Y() < max.Y()
);
}
/** Check bbox collision.
Note: just adjiacent bbox won't collide
@param b A bounding box
@return True iff collision
*/
bool Collide( Box2 const &b )
{
Box2 bb=*this;
bb.Intersect(b);
return bb.IsValid();
}
/** Check if emptry.
@return True iff empty
*/
inline bool IsNull() const { return min.X()>max.X() || min.Y()>max.Y(); }
/** Check consistency.
@return True iff consistent
*/
inline bool IsValid() const { return min.X()<max.X() && min.Y()<max.Y(); }
/** Check if emptry.
@return True iff empty
*/
inline bool IsEmpty() const { return min==max; }
/// Computes length of diagonal
ScalarType Diag() const
{
return Distance(min,max);
}
/// Computes box center
PointType Center() const
{
return (min+max)/2;
}
/// Computes area
inline ScalarType Area() const
{
return (max[0]-min[0])*(max[1]-min[1]);
}
/// computes dimension on X axis.
inline ScalarType DimX() const { return max.X()-min.X(); }
/// computes dimension on Y axis.
inline ScalarType DimY() const { return max.Y()-min.Y(); }
/// Computes sizes (as a vector)
inline PointType Dim() const { return max-min; }
/// Deprecated: use GlobalToLocal
inline void Normalize( PointType & p )
{
p -= min;
p[0] /= max[0]-min[0];
p[1] /= max[1]-min[1];
}
/// Returns global coords of a local point expressed in [0..1]^2
PointType LocalToGlobal(PointType const & p) const{
return PointType(
min[0] + p[0]*(max[0]-min[0]),
min[1] + p[1]*(max[1]-min[1]));
}
/// Returns local coords expressed in [0..1]^2 of a point in R^2
PointType GlobalToLocal(PointType const & p) const{
return PointType(
(p[0]-min[0])/(max[0]-min[0]),
(p[1]-min[1])/(max[1]-min[1])
);
}
/// Turns the bounding box into a square (conservatively)
void MakeSquare(){
ScalarType radius = max( DimX(), DimY() ) / 2;
PointType c = Center();
max = c + PointType(radius, radius);
min = c - PointType(radius, radius);
}
inline unsigned char MaxDim() const {
int i=1;
PointType diag = max-min;
if (diag[0] > diag[1])
i=0;
return i;
}
inline unsigned char MinDim() const {
int i=1;
PointType diag = max-min;
if (diag[0] < diag[1])
i=0;
return i;
}
}; // end class definition
template <class ScalarType>
ScalarType DistancePoint2Box2(const Point2<ScalarType> &test,
const Box2<ScalarType> &bbox)
{
///test possible position respect to bounding box
if (!bbox.IsIn(test)){
if ((test.X()<=bbox.min.X())&&(test.Y()<=bbox.min.Y()))
return ((test-bbox.min).Norm());
else
if ((test.X()>=bbox.min.X())&&
(test.X()<=bbox.max.X())&&
(test.Y()<=bbox.min.Y()))
return (bbox.min.Y()-test.Y());
else
if ((test.X()>=bbox.max.X())&&
(test.Y()<=bbox.min.Y()))
return ((test-vcg::Point2<ScalarType>(bbox.max.X(),bbox.min.Y())).Norm());
else
if ((test.Y()>=bbox.min.Y())&&
(test.Y()<=bbox.max.Y())&&
(test.X()>=bbox.max.X()))
return (test.X()-bbox.max.X());
else
if ((test.X()>=bbox.max.X())&&(test.Y()>=bbox.max.Y()))
return ((test-bbox.max).Norm());
else
if ((test.X()>=bbox.min.X())&&
(test.X()<=bbox.max.X())&&
(test.Y()>=bbox.max.Y()))
return (test.Y()-bbox.max.Y());
else
if ((test.X()<=bbox.min.X())&&
(test.Y()>=bbox.max.Y()))
return ((test-vcg::Point2<ScalarType>(bbox.min.X(),bbox.max.Y())).Norm());
else
if ((test.X()<=bbox.min.X())&&
(test.Y()<=bbox.max.Y())&&
(test.Y()>=bbox.min.Y()))
return (bbox.min.X()-test.X());
}
else
{
//return minimum distance
ScalarType dx=std::min<ScalarType>(fabs(test.X()-bbox.min.X()),fabs(bbox.max.X()-test.X()));
ScalarType dy=std::min<ScalarType>(fabs(test.Y()-bbox.min.Y()),fabs(bbox.max.Y()-test.Y()));
return(std::min<ScalarType>(dx,dy));
}
}
template <class ScalarType>
Point2<ScalarType> ClosestPoint2Box2(const Point2<ScalarType> &test,
const Box2<ScalarType> &bbox)
{
Segment2<ScalarType> Segs[4];
Segs[0].P0() = bbox.min;
Segs[0].P1() = vcg::Point2<ScalarType>(bbox.max.X(), bbox.min.Y());
Segs[1].P0() = Segs[0].P1();
Segs[1].P1() = bbox.max;
Segs[2].P0() = Segs[1].P1();
Segs[2].P1() = vcg::Point2<ScalarType>(bbox.min.X(), bbox.max.Y());
Segs[3].P0() = Segs[2].P1();
Segs[3].P1() = bbox.min;
Point2<ScalarType> closest = ClosestPoint(Segs[0], test);
ScalarType minDist = (closest-test).Norm();
for (int i = 1; i < 4; i++)
{
Point2<ScalarType> point = ClosestPoint(Segs[i], test);
ScalarType dist = (test - point).Norm();
if (dist < minDist)
{
minDist = dist;
closest = point;
}
}
return closest;
}
/// Specificazione di box of short
typedef Box2<short> Box2s;
/// Specificazione di box of int
typedef Box2<int> Box2i;
/// Specificazione di box of float
typedef Box2<float> Box2f;
/// Specificazione di box of double
typedef Box2<double> Box2d;
/*@}*/
} // end namespace
#endif
|