2004-02-16 00:34:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-02-16 00:34:04 +01:00
|
|
|
* 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 <assert.h>
|
2011-10-05 17:04:40 +02:00
|
|
|
#include <algorithm>
|
2004-02-16 00:34:04 +01:00
|
|
|
#include <vcg/math/base.h>
|
2005-05-05 12:20:24 +02:00
|
|
|
#include <vcg/space/point2.h>
|
2010-05-20 15:58:08 +02:00
|
|
|
|
2004-02-16 00:34:04 +01:00
|
|
|
namespace vcg {
|
|
|
|
|
2010-05-24 10:25:26 +02:00
|
|
|
|
|
|
|
// needed prototype;
|
|
|
|
template <class SegScalarType> class Segment2;
|
|
|
|
|
2004-03-10 22:38:40 +01:00
|
|
|
/** \addtogroup space */
|
|
|
|
/*@{*/
|
|
|
|
/**
|
|
|
|
Templated class for a 2D bounding box. It is stored just as two Point2
|
|
|
|
@param BoxScalarType (Template Parameter) Specifies the scalar field.
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2004-03-10 22:38:40 +01:00
|
|
|
template <class BoxScalarType>
|
2004-02-16 00:34:04 +01:00
|
|
|
class Box2
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// The scalar type
|
2004-03-10 22:38:40 +01:00
|
|
|
typedef BoxScalarType ScalarType;
|
2010-07-25 15:31:57 +02:00
|
|
|
typedef Point2<BoxScalarType> PointType ;
|
2004-02-16 00:34:04 +01:00
|
|
|
|
|
|
|
/// min coordinate point
|
2010-07-24 17:29:20 +02:00
|
|
|
PointType min;
|
2004-02-16 00:34:04 +01:00
|
|
|
/// max coordinate point
|
2010-07-24 17:29:20 +02:00
|
|
|
PointType max;
|
2004-02-16 00:34:04 +01:00
|
|
|
/// Standard constructor
|
2005-05-05 14:22:08 +02:00
|
|
|
inline Box2() { min.X()= 1; max.X()= -1; min.Y()= 1; max.Y()= -1; }
|
2004-02-16 00:34:04 +01:00
|
|
|
/// Copy constructor
|
|
|
|
inline Box2( const Box2 & b ) { min=b.min; max=b.max; }
|
2010-12-13 00:57:39 +01:00
|
|
|
/// Min Max constructor
|
|
|
|
inline Box2( const Point2<BoxScalarType> & mi, const Point2<BoxScalarType> & ma ) { min = mi; max = ma; }
|
2014-08-01 02:15:51 +02:00
|
|
|
|
|
|
|
inline Box2(const Point2<BoxScalarType> & center, const BoxScalarType & radius) {
|
|
|
|
min = center-Point2<BoxScalarType>(radius,radius);
|
|
|
|
max = center+Point2<BoxScalarType>(radius,radius);
|
|
|
|
}
|
|
|
|
|
2004-02-16 00:34:04 +01:00
|
|
|
/// Distructor
|
|
|
|
inline ~Box2() { }
|
|
|
|
/// Operator to compare two bounding box
|
|
|
|
inline bool operator == ( Box2 const & p ) const
|
|
|
|
{
|
|
|
|
return min==p.min && max==p.max;
|
|
|
|
}
|
2004-03-10 22:38:40 +01:00
|
|
|
/// Initializing the bounding box with a point
|
2010-07-24 17:29:20 +02:00
|
|
|
void Set( const PointType & p )
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
min = max = p;
|
|
|
|
}
|
2010-11-05 17:18:51 +01:00
|
|
|
|
|
|
|
Point2<BoxScalarType> P(const int & i) const
|
|
|
|
{
|
|
|
|
return Point2<BoxScalarType>(
|
|
|
|
min[0]+ (i%2) * DimX(),
|
|
|
|
min[1]+ ((i / 2)%2) * DimY());
|
|
|
|
}
|
|
|
|
|
2004-02-16 00:34:04 +01:00
|
|
|
// Initializing with the values
|
2010-07-24 17:29:20 +02:00
|
|
|
inline void Set( ScalarType minx, ScalarType miny, ScalarType maxx, ScalarType maxy )
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
min[0] = minx;
|
|
|
|
min[1] = miny;
|
|
|
|
max[0] = maxx;
|
|
|
|
max[1] = maxy;
|
|
|
|
}
|
|
|
|
/// Set the bounding box to a null value
|
|
|
|
void SetNull()
|
|
|
|
{
|
2005-05-06 16:02:37 +02:00
|
|
|
min.X()= 1; max.X()= -1; min.Y()= 1; max.Y()= -1;
|
2004-02-16 00:34:04 +01:00
|
|
|
}
|
|
|
|
/** Function to add two bounding box
|
2010-07-24 17:29:20 +02:00
|
|
|
the bounding box expand to include the other bounding box (if necessary)
|
|
|
|
@param b The bounding box to be added
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
|
|
|
void Add( Box2 const & b )
|
|
|
|
{
|
|
|
|
if(IsNull())
|
|
|
|
{
|
|
|
|
min=b.min;
|
|
|
|
max=b.max;
|
|
|
|
}
|
2005-05-06 16:02:37 +02:00
|
|
|
else
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
2005-05-06 16:02:37 +02:00
|
|
|
if(min.X() > b.min.X()) min.X() = b.min.X();
|
|
|
|
if(min.Y() > b.min.Y()) min.Y() = b.min.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2005-05-06 16:02:37 +02:00
|
|
|
if(max.X() < b.max.X()) max.X() = b.max.X();
|
|
|
|
if(max.Y() < b.max.Y()) max.Y() = b.max.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Adds a point to the bouning box.
|
|
|
|
the bounding box expand to include the new point (if necessary)
|
2004-02-16 00:34:04 +01:00
|
|
|
@param p The point 2D
|
|
|
|
*/
|
2010-07-24 17:29:20 +02:00
|
|
|
void Add( const PointType & p )
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
if(IsNull()) Set(p);
|
|
|
|
else
|
|
|
|
{
|
2005-05-06 16:02:37 +02:00
|
|
|
if(min.X() > p.X()) min.X() = p.X();
|
|
|
|
if(min.Y() > p.Y()) min.Y() = p.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2005-05-06 16:02:37 +02:00
|
|
|
if(max.X() < p.X()) max.X() = p.X();
|
|
|
|
if(max.Y() < p.Y()) max.Y() = p.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
}
|
|
|
|
}
|
2009-06-05 11:32:47 +02:00
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Varies the dimension of the bounding box.
|
|
|
|
@param delta The size delta (if positive, box is enlarged)
|
|
|
|
*/
|
|
|
|
void Offset(const ScalarType s)
|
2009-06-05 11:32:47 +02:00
|
|
|
{
|
2010-07-24 17:29:20 +02:00
|
|
|
Offset(PointType(s, s));
|
2009-06-05 11:32:47 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Varies the dimension of the bounding box.
|
2010-07-25 15:31:57 +02:00
|
|
|
@param delta The size delta per dimension (if positive, box is enlarged)
|
2009-06-05 11:32:47 +02:00
|
|
|
*/
|
2010-07-24 17:29:20 +02:00
|
|
|
void Offset(const PointType & delta)
|
2009-06-05 11:32:47 +02:00
|
|
|
{
|
|
|
|
min -= delta;
|
|
|
|
max += delta;
|
|
|
|
}
|
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Computes intersection between this and another bounding box
|
|
|
|
@param b The other bounding box
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
|
|
|
void Intersect( const Box2 & b )
|
|
|
|
{
|
2005-05-06 16:02:37 +02:00
|
|
|
if(min.X() < b.min.X()) min.X() = b.min.X();
|
|
|
|
if(min.Y() < b.min.Y()) min.Y() = b.min.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2005-05-06 16:02:37 +02:00
|
|
|
if(max.X() > b.max.X()) max.X() = b.max.X();
|
|
|
|
if(max.Y() > b.max.Y()) max.Y() = b.max.Y();
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2005-05-06 16:02:37 +02:00
|
|
|
if(min.X()>max.X() || min.Y()>max.Y()) SetNull();
|
2004-02-16 00:34:04 +01:00
|
|
|
}
|
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Traslate the bounding box by a vectore
|
|
|
|
@param p The transolation vector
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2010-07-24 17:29:20 +02:00
|
|
|
void Translate( const PointType & p )
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
min += p;
|
|
|
|
max += p;
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Checks whether a 2D point p is inside the box
|
2004-02-16 00:34:04 +01:00
|
|
|
@param p The point 2D
|
2010-07-24 17:29:20 +02:00
|
|
|
@return True iff p inside
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2010-07-24 17:29:20 +02:00
|
|
|
bool IsIn( PointType const & p ) const
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
return (
|
2005-05-06 16:02:37 +02:00
|
|
|
min.X() <= p.X() && p.X() <= max.X() &&
|
|
|
|
min.Y() <= p.Y() && p.Y() <= max.Y()
|
2004-02-16 00:34:04 +01:00
|
|
|
);
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/** 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
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2010-07-24 17:29:20 +02:00
|
|
|
bool IsInEx( PointType const & p ) const
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
return (
|
2005-05-06 16:02:37 +02:00
|
|
|
min.X() <= p.X() && p.X() < max.X() &&
|
|
|
|
min.Y() <= p.Y() && p.Y() < max.Y()
|
2004-02-16 00:34:04 +01:00
|
|
|
);
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Check bbox collision.
|
|
|
|
Note: just adjiacent bbox won't collide
|
2004-02-16 00:34:04 +01:00
|
|
|
@param b A bounding box
|
2010-07-24 17:29:20 +02:00
|
|
|
@return True iff collision
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
|
|
|
bool Collide( Box2 const &b )
|
|
|
|
{
|
2005-06-14 15:46:20 +02:00
|
|
|
Box2 bb=*this;
|
2004-02-16 00:34:04 +01:00
|
|
|
bb.Intersect(b);
|
|
|
|
return bb.IsValid();
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/** Check if emptry.
|
|
|
|
@return True iff empty
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2005-05-06 16:02:37 +02:00
|
|
|
inline bool IsNull() const { return min.X()>max.X() || min.Y()>max.Y(); }
|
2010-07-24 17:29:20 +02:00
|
|
|
|
|
|
|
/** Check consistency.
|
|
|
|
@return True iff consistent
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
2005-05-06 16:02:37 +02:00
|
|
|
inline bool IsValid() const { return min.X()<max.X() && min.Y()<max.Y(); }
|
2010-07-24 17:29:20 +02:00
|
|
|
|
|
|
|
/** Check if emptry.
|
|
|
|
@return True iff empty
|
2004-02-16 00:34:04 +01:00
|
|
|
*/
|
|
|
|
inline bool IsEmpty() const { return min==max; }
|
2010-07-24 17:29:20 +02:00
|
|
|
|
2012-10-08 11:03:25 +02:00
|
|
|
/// Computes length of diagonal
|
2010-07-24 17:29:20 +02:00
|
|
|
ScalarType Diag() const
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
return Distance(min,max);
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/// Computes box center
|
|
|
|
PointType Center() const
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
return (min+max)/2;
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/// Computes area
|
|
|
|
inline ScalarType Area() const
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
2007-07-02 12:01:00 +02:00
|
|
|
return (max[0]-min[0])*(max[1]-min[1]);
|
2004-02-16 00:34:04 +01:00
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
/// 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(); }
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/// Computes sizes (as a vector)
|
|
|
|
inline PointType Dim() const { return max-min; }
|
2006-10-07 18:50:26 +02:00
|
|
|
|
2010-07-24 17:29:20 +02:00
|
|
|
/// Deprecated: use GlobalToLocal
|
|
|
|
inline void Normalize( PointType & p )
|
2004-02-16 00:34:04 +01:00
|
|
|
{
|
|
|
|
p -= min;
|
|
|
|
p[0] /= max[0]-min[0];
|
|
|
|
p[1] /= max[1]-min[1];
|
|
|
|
}
|
2010-07-24 17:29:20 +02:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2004-02-16 00:34:04 +01:00
|
|
|
}; // end class definition
|
|
|
|
|
2008-03-05 12:45:36 +01:00
|
|
|
template <class ScalarType>
|
|
|
|
ScalarType DistancePoint2Box2(const Point2<ScalarType> &test,
|
|
|
|
const Box2<ScalarType> &bbox)
|
|
|
|
{
|
|
|
|
///test possible position respect to bounding box
|
2009-06-30 18:07:13 +02:00
|
|
|
if (!bbox.IsIn(test)){
|
2008-03-05 12:45:36 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2004-02-16 00:34:04 +01:00
|
|
|
|
2010-05-20 15:58:08 +02:00
|
|
|
template <class ScalarType>
|
|
|
|
Point2<ScalarType> ClosestPoint2Box2(const Point2<ScalarType> &test,
|
2011-03-21 18:03:34 +01:00
|
|
|
const Box2<ScalarType> &bbox)
|
2010-05-20 15:58:08 +02:00
|
|
|
{
|
|
|
|
Segment2<ScalarType> Segs[4];
|
2011-03-21 18:03:34 +01:00
|
|
|
Segs[0].P0() = bbox.min;
|
|
|
|
Segs[0].P1() = vcg::Point2<ScalarType>(bbox.max.X(), bbox.min.Y());
|
2010-05-20 15:58:08 +02:00
|
|
|
|
2013-11-28 17:34:20 +01:00
|
|
|
Segs[1].P0() = Segs[0].P1();
|
2011-03-21 18:03:34 +01:00
|
|
|
Segs[1].P1() = bbox.max;
|
2010-05-20 15:58:08 +02:00
|
|
|
|
2013-11-28 17:34:20 +01:00
|
|
|
Segs[2].P0() = Segs[1].P1();
|
2011-03-21 18:03:34 +01:00
|
|
|
Segs[2].P1() = vcg::Point2<ScalarType>(bbox.min.X(), bbox.max.Y());
|
2010-05-20 15:58:08 +02:00
|
|
|
|
2013-11-28 17:34:20 +01:00
|
|
|
Segs[3].P0() = Segs[2].P1();
|
2011-03-21 18:03:34 +01:00
|
|
|
Segs[3].P1() = bbox.min;
|
2010-05-20 15:58:08 +02:00
|
|
|
|
2011-03-21 18:03:34 +01:00
|
|
|
Point2<ScalarType> closest = ClosestPoint(Segs[0], test);
|
|
|
|
ScalarType minDist = (closest-test).Norm();
|
|
|
|
for (int i = 1; i < 4; i++)
|
2010-05-20 15:58:08 +02:00
|
|
|
{
|
2011-03-21 18:03:34 +01:00
|
|
|
Point2<ScalarType> point = ClosestPoint(Segs[i], test);
|
|
|
|
ScalarType dist = (test - point).Norm();
|
|
|
|
if (dist < minDist)
|
2010-05-20 15:58:08 +02:00
|
|
|
{
|
2011-03-21 18:03:34 +01:00
|
|
|
minDist = dist;
|
|
|
|
closest = point;
|
2010-05-20 15:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2004-02-16 00:34:04 +01:00
|
|
|
/// 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;
|
|
|
|
|
2004-03-10 22:38:40 +01:00
|
|
|
/*@}*/
|
2004-02-16 00:34:04 +01:00
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|