KiCad PCB EDA Suite
box2.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6  * Copyright (C) 2013 CERN
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef __BOX2_H
28 #define __BOX2_H
29 
30 #include <math/vector2d.h>
31 #include <limits>
32 
33 // Needed for the OPT definition
34 #include <core/optional.h>
35 
41 template <class Vec>
42 class BOX2
43 {
44 private:
45  Vec m_Pos; // Rectangle Origin
46  Vec m_Size; // Rectangle Size
47 
48 public:
49  typedef typename Vec::coord_type coord_type;
50  typedef typename Vec::extended_type ecoord_type;
51  typedef std::numeric_limits<coord_type> coord_limits;
52 
53  BOX2() {};
54 
55  BOX2( const Vec& aPos, const Vec& aSize ) :
56  m_Pos( aPos ),
57  m_Size( aSize )
58  {
59  Normalize();
60  }
61 
62 #ifdef WX_COMPATIBILITY
63  BOX2( const wxRect& aRect ) :
65  m_Pos( aRect.GetPosition() ),
66  m_Size( aRect.GetSize() )
67  {
68  Normalize();
69  }
70 #endif
71 
72  void SetMaximum()
73  {
74  m_Pos.x = m_Pos.y = coord_limits::lowest() / 2 + coord_limits::epsilon();
75  m_Size.x = m_Size.y = coord_limits::max() - coord_limits::epsilon();
76  }
77 
78  Vec Centre() const
79  {
80  return Vec( m_Pos.x + ( m_Size.x / 2 ),
81  m_Pos.y + ( m_Size.y / 2 ) );
82  }
83 
89  template <class Container>
90  void Compute( const Container& aPointList )
91  {
92  Vec vmin, vmax;
93 
94  typename Container::const_iterator i;
95 
96  if( !aPointList.size() )
97  return;
98 
99  vmin = vmax = aPointList[0];
100 
101  for( i = aPointList.begin(); i != aPointList.end(); ++i )
102  {
103  Vec p( *i );
104  vmin.x = std::min( vmin.x, p.x );
105  vmin.y = std::min( vmin.y, p.y );
106  vmax.x = std::max( vmax.x, p.x );
107  vmax.y = std::max( vmax.y, p.y );
108  }
109 
110  SetOrigin( vmin );
111  SetSize( vmax - vmin );
112  }
113 
119  void Move( const Vec& aMoveVector )
120  {
121  m_Pos += aMoveVector;
122  }
123 
129  {
130  if( m_Size.y < 0 )
131  {
132  m_Size.y = -m_Size.y;
133  m_Pos.y -= m_Size.y;
134  }
135 
136  if( m_Size.x < 0 )
137  {
138  m_Size.x = -m_Size.x;
139  m_Pos.x -= m_Size.x;
140  }
141 
142  return *this;
143  }
144 
150  bool Contains( const Vec& aPoint ) const
151  {
152  Vec rel_pos = aPoint - m_Pos;
153  Vec size = m_Size;
154 
155  if( size.x < 0 )
156  {
157  size.x = -size.x;
158  rel_pos.x += size.x;
159  }
160 
161  if( size.y < 0 )
162  {
163  size.y = -size.y;
164  rel_pos.y += size.y;
165  }
166 
167  return ( rel_pos.x >= 0 ) && ( rel_pos.y >= 0 ) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
168  }
169 
176  bool Contains( coord_type x, coord_type y ) const { return Contains( Vec( x, y ) ); }
177 
183  bool Contains( const BOX2<Vec>& aRect ) const
184  {
185  return Contains( aRect.GetOrigin() ) && Contains( aRect.GetEnd() );
186  }
187 
188  const Vec& GetSize() const { return m_Size; }
189  coord_type GetX() const { return m_Pos.x; }
190  coord_type GetY() const { return m_Pos.y; }
191 
192  const Vec& GetOrigin() const { return m_Pos; }
193  const Vec& GetPosition() const { return m_Pos; }
194  const Vec GetEnd() const { return Vec( GetRight(), GetBottom() ); }
195 
196  coord_type GetWidth() const { return m_Size.x; }
197  coord_type GetHeight() const { return m_Size.y; }
198  coord_type GetRight() const { return m_Pos.x + m_Size.x; }
199  coord_type GetBottom() const { return m_Pos.y + m_Size.y; }
200 
201  // Compatibility aliases
202  coord_type GetLeft() const { return GetX(); }
203  coord_type GetTop() const { return GetY(); }
204  void MoveTopTo( coord_type aTop ) { m_Pos.y = aTop; }
205  void MoveBottomTo( coord_type aBottom ) { m_Size.y = aBottom - m_Pos.y; }
206  void MoveLeftTo( coord_type aLeft ) { m_Pos.x = aLeft; }
207  void MoveRightTo( coord_type aRight ) { m_Size.x = aRight - m_Pos.x; }
208 
209  void SetOrigin( const Vec& pos ) { m_Pos = pos; }
210  void SetOrigin( coord_type x, coord_type y ) { m_Pos.x = x; m_Pos.y = y; }
211  void SetSize( const Vec& size ) { m_Size = size; }
212  void SetSize( coord_type w, coord_type h ) { m_Size.x = w; m_Size.y = h; }
213  void Offset( coord_type dx, coord_type dy ) { m_Pos.x += dx; m_Pos.y += dy; }
214  void Offset( const Vec& offset )
215  {
216  m_Pos.x += offset.x; m_Pos.y +=
217  offset.y;
218  }
219 
220  void SetX( coord_type val ) { m_Pos.x = val; }
221  void SetY( coord_type val ) { m_Pos.y = val; }
222  void SetWidth( coord_type val ) { m_Size.x = val; }
223  void SetHeight( coord_type val ) { m_Size.y = val; }
224  void SetEnd( coord_type x, coord_type y ) { SetEnd( Vec( x, y ) ); }
225  void SetEnd( const Vec& pos )
226  {
227  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
228  }
229 
235  bool Intersects( const BOX2<Vec>& aRect ) const
236  {
237  // this logic taken from wxWidgets' geometry.cpp file:
238  bool rc;
239 
240  BOX2<Vec> me( *this );
241  BOX2<Vec> rect( aRect );
242  me.Normalize(); // ensure size is >= 0
243  rect.Normalize(); // ensure size is >= 0
244 
245  // calculate the left common area coordinate:
246  int left = std::max( me.m_Pos.x, rect.m_Pos.x );
247  // calculate the right common area coordinate:
248  int right = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
249  // calculate the upper common area coordinate:
250  int top = std::max( me.m_Pos.y, aRect.m_Pos.y );
251  // calculate the lower common area coordinate:
252  int bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
253 
254  // if a common area exists, it must have a positive (null accepted) size
255  if( left <= right && top <= bottom )
256  rc = true;
257  else
258  rc = false;
259 
260  return rc;
261  }
262 
267  BOX2<Vec> Intersect( const BOX2<Vec>& aRect )
268  {
269  BOX2<Vec> me( *this );
270  BOX2<Vec> rect( aRect );
271  me.Normalize(); // ensure size is >= 0
272  rect.Normalize(); // ensure size is >= 0
273 
274  Vec topLeft, bottomRight;
275 
276  topLeft.x = std::max( me.m_Pos.x, rect.m_Pos.x );
277  bottomRight.x = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
278  topLeft.y = std::max( me.m_Pos.y, rect.m_Pos.y );
279  bottomRight.y = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
280 
281  if ( topLeft.x < bottomRight.x && topLeft.y < bottomRight.y )
282  return BOX2<Vec>( topLeft, bottomRight - topLeft );
283  else
284  return BOX2<Vec>( Vec( 0, 0 ), Vec( 0, 0 ) );
285  }
286 
287  const std::string Format() const
288  {
289  std::stringstream ss;
290 
291  ss << "( box corner " << m_Pos.Format() << " w " << m_Size.x << " h " << m_Size.y << " )";
292 
293  return ss.str();
294  }
295 
302  {
303  if( m_Size.x >= 0 )
304  {
305  if( m_Size.x < -2 * dx )
306  {
307  // Don't allow deflate to eat more width than we have,
308  m_Pos.x += m_Size.x / 2;
309  m_Size.x = 0;
310  }
311  else
312  {
313  // The inflate is valid.
314  m_Pos.x -= dx;
315  m_Size.x += 2 * dx;
316  }
317  }
318  else // size.x < 0:
319  {
320  if( m_Size.x > -2 * dx )
321  {
322  // Don't allow deflate to eat more width than we have,
323  m_Pos.x -= m_Size.x / 2;
324  m_Size.x = 0;
325  }
326  else
327  {
328  // The inflate is valid.
329  m_Pos.x += dx;
330  m_Size.x -= 2 * dx; // m_Size.x <0: inflate when dx > 0
331  }
332  }
333 
334  if( m_Size.y >= 0 )
335  {
336  if( m_Size.y < -2 * dy )
337  {
338  // Don't allow deflate to eat more height than we have,
339  m_Pos.y += m_Size.y / 2;
340  m_Size.y = 0;
341  }
342  else
343  {
344  // The inflate is valid.
345  m_Pos.y -= dy;
346  m_Size.y += 2 * dy;
347  }
348  }
349  else // size.y < 0:
350  {
351  if( m_Size.y > 2 * dy )
352  {
353  // Don't allow deflate to eat more height than we have,
354  m_Pos.y -= m_Size.y / 2;
355  m_Size.y = 0;
356  }
357  else
358  {
359  // The inflate is valid.
360  m_Pos.y += dy;
361  m_Size.y -= 2 * dy; // m_Size.y <0: inflate when dy > 0
362  }
363  }
364 
365  return *this;
366  }
367 
373  BOX2<Vec>& Inflate( int aDelta )
374  {
375  Inflate( aDelta, aDelta );
376  return *this;
377  }
378 
385  BOX2<Vec>& Merge( const BOX2<Vec>& aRect )
386  {
387  Normalize(); // ensure width and height >= 0
388  BOX2<Vec> rect = aRect;
389  rect.Normalize(); // ensure width and height >= 0
390  Vec end = GetEnd();
391  Vec rect_end = rect.GetEnd();
392 
393  // Change origin and size in order to contain the given rect
394  m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
395  m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
396  end.x = std::max( end.x, rect_end.x );
397  end.y = std::max( end.y, rect_end.y );
398  SetEnd( end );
399  return *this;
400  }
401 
407  BOX2<Vec>& Merge( const Vec& aPoint )
408  {
409  Normalize(); // ensure width and height >= 0
410 
411  Vec end = GetEnd();
412  // Change origin and size in order to contain the given rect
413  m_Pos.x = std::min( m_Pos.x, aPoint.x );
414  m_Pos.y = std::min( m_Pos.y, aPoint.y );
415  end.x = std::max( end.x, aPoint.x );
416  end.y = std::max( end.y, aPoint.y );
417  SetEnd( end );
418  return *this;
419  }
420 
427  {
428  return (ecoord_type) GetWidth() * (ecoord_type) GetHeight();
429  }
430 
437  {
438  return m_Size.EuclideanNorm();
439  }
440 
441  ecoord_type SquaredDistance( const Vec& aP ) const
442  {
443  ecoord_type x2 = m_Pos.x + m_Size.x;
444  ecoord_type y2 = m_Pos.y + m_Size.y;
445  ecoord_type xdiff = std::max( aP.x < m_Pos.x ? m_Pos.x - aP.x : m_Pos.x - x2, (ecoord_type) 0 );
446  ecoord_type ydiff = std::max( aP.y < m_Pos.y ? m_Pos.y - aP.y : m_Pos.y - y2, (ecoord_type) 0 );
447  return xdiff * xdiff + ydiff * ydiff;
448  }
449 
450  ecoord_type Distance( const Vec& aP ) const
451  {
452  return sqrt( SquaredDistance( aP ) );
453  }
454 
461  ecoord_type SquaredDistance( const BOX2<Vec>& aBox ) const
462  {
463  ecoord_type s = 0;
464 
465  if( aBox.m_Pos.x + aBox.m_Size.x < m_Pos.x )
466  {
467  ecoord_type d = aBox.m_Pos.x + aBox.m_Size.x - m_Pos.x;
468  s += d * d;
469  }
470  else if( aBox.m_Pos.x > m_Pos.x + m_Size.x )
471  {
472  ecoord_type d = aBox.m_Pos.x - m_Size.x - m_Pos.x;
473  s += d * d;
474  }
475 
476  if( aBox.m_Pos.y + aBox.m_Size.y < m_Pos.y )
477  {
478  ecoord_type d = aBox.m_Pos.y + aBox.m_Size.y - m_Pos.y;
479  s += d * d;
480  }
481  else if( aBox.m_Pos.y > m_Pos.y + m_Size.y )
482  {
483  ecoord_type d = aBox.m_Pos.y - m_Size.y - m_Pos.y;
484  s += d * d;
485  }
486 
487  return s;
488  }
489 
496  ecoord_type Distance( const BOX2<Vec>& aBox ) const
497  {
498  return sqrt( SquaredDistance( aBox ) );
499  }
500 
501  bool operator==( const BOX2<Vec>& aOther ) const
502  {
503  auto t1 ( *this );
504  auto t2 ( aOther );
505  t1.Normalize();
506  t2.Normalize();
507  return ( t1.m_Pos == t2.m_Pos && t1.m_Size == t2.m_Size );
508  }
509 
510  bool operator!=( const BOX2<Vec>& aOther ) const
511  {
512  auto t1 ( *this );
513  auto t2 ( aOther );
514  t1.Normalize();
515  t2.Normalize();
516  return ( t1.m_Pos != t2.m_Pos || t1.m_Size != t2.m_Size );
517  }
518 };
519 
520 /* Default specializations */
523 
525 
526 // FIXME should be removed to avoid multiple typedefs for the same type
527 typedef BOX2D DBOX;
528 
529 #endif
ecoord_type Diagonal() const
Function GetArea returns the length of the diagonal of the rectangle.
Definition: box2.h:436
void Move(const Vec &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
Definition: box2.h:119
BOX2< VECTOR2I > BOX2I
Definition: box2.h:521
void Offset(const Vec &offset)
Definition: box2.h:214
BOX2()
Definition: box2.h:53
bool operator!=(const BOX2< Vec > &aOther) const
Definition: box2.h:510
const Vec GetEnd() const
Definition: box2.h:194
coord_type GetX() const
Definition: box2.h:189
coord_type GetTop() const
Definition: box2.h:203
BOX2D DBOX
Definition: box2.h:527
void MoveBottomTo(coord_type aBottom)
Definition: box2.h:205
BOX2< Vec > & Inflate(int aDelta)
Function Inflate inflates the rectangle horizontally and vertically by aDelta.
Definition: box2.h:373
BOX2< VECTOR2D > BOX2D
Definition: box2.h:522
coord_type GetRight() const
Definition: box2.h:198
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:90
ecoord_type SquaredDistance(const Vec &aP) const
Definition: box2.h:441
void SetSize(const Vec &size)
Definition: box2.h:211
coord_type GetBottom() const
Definition: box2.h:199
void SetX(coord_type val)
Definition: box2.h:220
BOX2 handles a 2-D bounding box, built on top of an origin point and size vector, both of templated c...
Definition: box2.h:42
bool operator==(const BOX2< Vec > &aOther) const
Definition: box2.h:501
bool Intersects(const BOX2< Vec > &aRect) const
Function Intersects.
Definition: box2.h:235
bool Contains(coord_type x, coord_type y) const
Function Contains.
Definition: box2.h:176
void MoveLeftTo(coord_type aLeft)
Definition: box2.h:206
void SetOrigin(coord_type x, coord_type y)
Definition: box2.h:210
Vec::extended_type ecoord_type
Definition: box2.h:50
coord_type GetWidth() const
Definition: box2.h:196
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:461
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:128
bool Contains(const Vec &aPoint) const
Function Contains.
Definition: box2.h:150
void SetMaximum()
Definition: box2.h:72
void SetSize(coord_type w, coord_type h)
Definition: box2.h:212
bool Contains(const BOX2< Vec > &aRect) const
Function Contains.
Definition: box2.h:183
ecoord_type GetArea() const
Function GetArea returns the area of the rectangle.
Definition: box2.h:426
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:385
void Offset(coord_type dx, coord_type dy)
Definition: box2.h:213
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:224
const Vec & GetPosition() const
Definition: box2.h:193
BOX2(const Vec &aPos, const Vec &aSize)
Definition: box2.h:55
coord_type GetY() const
Definition: box2.h:190
void SetHeight(coord_type val)
Definition: box2.h:223
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:301
Vec Centre() const
Definition: box2.h:78
void SetEnd(const Vec &pos)
Definition: box2.h:225
void SetY(coord_type val)
Definition: box2.h:221
Vec m_Pos
Definition: box2.h:45
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:407
void SetOrigin(const Vec &pos)
Definition: box2.h:209
coord_type GetHeight() const
Definition: box2.h:197
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:524
boost::optional< T > OPT
Definition: optional.h:7
const Vec & GetSize() const
Definition: box2.h:188
coord_type GetLeft() const
Definition: box2.h:202
Vec m_Size
Definition: box2.h:46
const Vec & GetOrigin() const
Definition: box2.h:192
void MoveTopTo(coord_type aTop)
Definition: box2.h:204
ecoord_type Distance(const Vec &aP) const
Definition: box2.h:450
std::numeric_limits< coord_type > coord_limits
Definition: box2.h:51
ecoord_type Distance(const BOX2< Vec > &aBox) const
Function Distance returns the minimum distance between self and box aBox.
Definition: box2.h:496
Vec::coord_type coord_type
Definition: box2.h:49
const std::string Format() const
Definition: box2.h:287
void SetWidth(coord_type val)
Definition: box2.h:222
BOX2< Vec > Intersect(const BOX2< Vec > &aRect)
Function Intersect Returns the intersection of this with another rectangle.
Definition: box2.h:267
void MoveRightTo(coord_type aRight)
Definition: box2.h:207