30 #include <math/vector2d.h> 33 #include <boost/optional.hpp> 48 typedef typename Vec::coord_type coord_type;
49 typedef typename Vec::extended_type ecoord_type;
50 typedef typename std::numeric_limits<coord_type> coord_limits;
54 BOX2(
const Vec& aPos,
const Vec& aSize ) :
63 m_Pos.x = m_Pos.y = coord_limits::min() / 2 + coord_limits::epsilon();
64 m_Size.x = m_Size.y = coord_limits::max() - coord_limits::epsilon();
69 return Vec( m_Pos.x + ( m_Size.x / 2 ),
70 m_Pos.y + ( m_Size.y / 2 ) );
78 template <
class Container>
79 void Compute(
const Container& aPointList )
83 typename Container::const_iterator i;
85 if( !aPointList.size() )
88 vmin = vmax = aPointList[0];
90 for( i = aPointList.begin(); i != aPointList.end(); ++i )
93 vmin.x = std::min( vmin.x, p.x );
94 vmin.y = std::min( vmin.y, p.y );
95 vmax.x = std::max( vmax.x, p.x );
96 vmax.y = std::max( vmax.y, p.y );
100 SetSize( vmax - vmin );
108 void Move(
const Vec& aMoveVector )
110 m_Pos += aMoveVector;
121 m_Size.y = -m_Size.y;
127 m_Size.x = -m_Size.x;
141 Vec rel_pos = aPoint - m_Pos;
156 return ( rel_pos.x >= 0 ) && ( rel_pos.y >= 0 ) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
177 const Vec& GetSize()
const {
return m_Size; }
178 coord_type GetX()
const {
return m_Pos.x; }
179 coord_type GetY()
const {
return m_Pos.y; }
181 const Vec& GetOrigin()
const {
return m_Pos; }
182 const Vec& GetPosition()
const {
return m_Pos; }
183 const Vec GetEnd()
const {
return Vec( GetRight(), GetBottom() ); }
185 coord_type GetWidth()
const {
return m_Size.x; }
186 coord_type GetHeight()
const {
return m_Size.y; }
187 coord_type GetRight()
const {
return m_Pos.x + m_Size.x; }
188 coord_type GetBottom()
const {
return m_Pos.y + m_Size.y; }
191 coord_type GetLeft()
const {
return GetX(); }
192 coord_type GetTop()
const {
return GetY(); }
193 void MoveTopTo( coord_type aTop ) { m_Pos.y = aTop; }
194 void MoveBottomTo( coord_type aBottom ) { m_Size.y = aBottom - m_Pos.y; }
195 void MoveLeftTo( coord_type aLeft ) { m_Pos.x = aLeft; }
196 void MoveRightTo( coord_type aRight ) { m_Size.x = aRight - m_Pos.x; }
198 void SetOrigin(
const Vec& pos ) { m_Pos = pos; }
199 void SetOrigin( coord_type x, coord_type y ) { m_Pos.x = x; m_Pos.y = y; }
200 void SetSize(
const Vec& size ) { m_Size = size; }
201 void SetSize( coord_type w, coord_type h ) { m_Size.x = w; m_Size.y = h; }
202 void Offset( coord_type dx, coord_type dy ) { m_Pos.x += dx; m_Pos.y += dy; }
203 void Offset(
const Vec& offset )
205 m_Pos.x += offset.x; m_Pos.y +=
209 void SetX( coord_type val ) { m_Pos.x = val; }
210 void SetY( coord_type val ) { m_Pos.y = val; }
211 void SetWidth( coord_type val ) { m_Size.x = val; }
212 void SetHeight( coord_type val ) { m_Size.y = val; }
213 void SetEnd( coord_type x, coord_type y ) { SetEnd( Vec( x, y ) ); }
214 void SetEnd(
const Vec& pos )
216 m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
235 int left = std::max( me.m_Pos.x, rect.m_Pos.x );
237 int right = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
239 int top = std::max( me.m_Pos.y, aRect.m_Pos.y );
241 int bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
244 if( left <= right && top <= bottom )
252 const std::string Format()
const 254 std::stringstream ss;
256 ss <<
"( box corner " << m_Pos.Format() <<
" w " << m_Size.x <<
" h " << m_Size.y <<
" )";
270 if( m_Size.x < -2 * dx )
273 m_Pos.x += m_Size.x / 2;
285 if( m_Size.x > -2 * dx )
288 m_Pos.x -= m_Size.x / 2;
301 if( m_Size.y < -2 * dy )
304 m_Pos.y += m_Size.y / 2;
316 if( m_Size.y > 2 * dy )
319 m_Pos.y -= m_Size.y / 2;
356 Vec rect_end = rect.GetEnd();
359 m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
360 m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
361 end.x = std::max( end.x, rect_end.x );
362 end.y = std::max( end.y, rect_end.y );
378 m_Pos.x = std::min( m_Pos.x, aPoint.x );
379 m_Pos.y = std::min( m_Pos.y, aPoint.y );
380 end.x = std::max( end.x, aPoint.x );
381 end.y = std::max( end.y, aPoint.y );
393 return (ecoord_type) GetWidth() * (ecoord_type) GetHeight();
403 return m_Size.EuclideanNorm();
406 ecoord_type SquaredDistance(
const Vec& aP )
const 408 ecoord_type x2 = m_Pos.x + m_Size.x;
409 ecoord_type y2 = m_Pos.y + m_Size.y;
410 ecoord_type xdiff = std::max( aP.x < m_Pos.x ? m_Pos.x - aP.x : m_Pos.x - x2, (ecoord_type) 0 );
411 ecoord_type ydiff = std::max( aP.y < m_Pos.y ? m_Pos.y - aP.y : m_Pos.y - y2, (ecoord_type) 0 );
412 return xdiff * xdiff + ydiff * ydiff;
415 ecoord_type Distance(
const Vec& aP )
const 417 return sqrt( SquaredDistance( aP ) );
430 if( aBox.m_Pos.x + aBox.m_Size.x < m_Pos.x )
432 ecoord_type d = aBox.m_Pos.x + aBox.m_Size.x - m_Pos.x;
435 else if( aBox.m_Pos.x > m_Pos.x + m_Size.x )
437 ecoord_type d = aBox.m_Pos.x - m_Size.x - m_Pos.x;
441 if( aBox.m_Pos.y + aBox.m_Size.y < m_Pos.y )
443 ecoord_type d = aBox.m_Pos.y + aBox.m_Size.y - m_Pos.y;
446 else if( aBox.m_Pos.y > m_Pos.y + m_Size.y )
448 ecoord_type d = aBox.m_Pos.y - m_Size.y - m_Pos.y;
463 return sqrt( SquaredDistance( aBox ) );
471 typedef boost::optional<BOX2I> OPT_BOX2I;
ecoord_type Diagonal() const
Function GetArea returns the length of the diagonal of the rectangle.
Definition: box2.h:401
void Move(const Vec &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
Definition: box2.h:108
BOX2< Vec > & Inflate(int aDelta)
Function Inflate inflates the rectangle horizontally and vertically by aDelta.
Definition: box2.h:338
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:79
Class BOX2 handles a 2-D bounding box, built on top of an origin point and size vector, both of templated class Vec.
Definition: box2.h:41
bool Intersects(const BOX2< Vec > &aRect) const
Function Intersects.
Definition: box2.h:224
bool Contains(coord_type x, coord_type y) const
Function Contains.
Definition: box2.h:165
ecoord_type SquaredDistance(const BOX2< Vec > &aBox) const
Function SquaredDistance returns the square of the minimum distance between self and box aBox...
Definition: box2.h:426
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:117
bool Contains(const Vec &aPoint) const
Function Contains.
Definition: box2.h:139
bool Contains(const BOX2< Vec > &aRect) const
Function Contains.
Definition: box2.h:172
ecoord_type GetArea() const
Function GetArea returns the area of the rectangle.
Definition: box2.h:391
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
Definition: box2.h:350
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:266
BOX2< Vec > & Merge(const Vec &aPoint)
Function Merge modifies the position and size of the rectangle in order to contain the given point...
Definition: box2.h:372
ecoord_type Distance(const BOX2< Vec > &aBox) const
Function Distance returns the minimum distance between self and box aBox.
Definition: box2.h:461