KiCad PCB EDA Suite
rect_placement.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------------
2 // Name : rect_placement.h
3 // Description : A class that allocates subrectangles into power-of-2 rectangles
4 // (C) Copyright 2000-2002 by Javier Arevalo
5 // This code is free to use and modify for all purposes
6 // --------------------------------------------------------------------------------
7 
12 #ifndef _RECT_PLACEMENT_H_
13 #define _RECT_PLACEMENT_H_
14 
15 #include <vector>
16 
17 // --------------------------------------------------------------------------------
18 // --------------------------------------------------------------------------------
19 
21 {
22 public:
23 
24  // Helper classes
25  struct TPos
26  {
27  int x, y;
28 
29  TPos() : x( 0 ), y( 0 ) { }
30  TPos( int _x, int _y ) : x( _x ), y( _y ) { }
31 
32  bool operator ==( const TPos& p ) const { return x == p.x && y == p.y; }
33  };
34 
35  struct TRect : public TPos
36  {
37  int w, h;
38 
39  TRect() : w( 0 ), h( 0 ) { }
40  TRect( int _x, int _y, int _w, int _h ) : TPos( _x, _y ), w( _w > 0 ? _w : 0 ), h(
41  _h > 0 ? _h : 0 ) { }
42 
43  bool Contains( const TPos& p ) const
44  {
45  return p.x >= x && p.y >= y && p.x < (x + w) && p.y < (y + h);
46  }
47  bool Contains( const TRect& r ) const
48  {
49  return r.x >= x && r.y >= y &&
50  (r.x + r.w) <= (x + w) && (r.y + r.h) <= (y + h);
51  }
52  bool Intersects( const TRect& r ) const
53  {
54  return w > 0 && h > 0 && r.w > 0 && r.h > 0
55  && ( (r.x + r.w) > x && r.x < (x + w) && (r.y + r.h) > y && r.y < (y + h) );
56  }
57 
58  // static bool Greater(const TRect &a, const TRect &b)
59  // { return a.w*a.h > b.w*b.h; }
60  // Greater rect area. Not as good as the next heuristic:
61  // Greater size in at least one dim.
62  static bool Greater( const TRect& a, const TRect& b )
63  {
64  return (a.w > b.w && a.w > b.h) || (a.h > b.w && a.h > b.h);
65  }
66  };
67 
68  // ---------------------
69 
70  typedef std::vector<TPos> CPosArray;
71  typedef std::vector<TRect> CRectArray;
72 
73  // ---------------------
74 
77 
78  void Init( int w = 1, int h = 1 );
79  void End();
80 
81  bool IsOk() const { return m_size.w > 0; }
82 
83  int GetW() const { return m_size.w; }
84  int GetH() const { return m_size.h; }
85  double GetArea() const { return m_area; }
86  double GetTotalArea() const { return (double)m_size.w * m_size.h; }
87 
88  bool AddAtEmptySpotAutoGrow( TRect* pRect, int maxW, int maxH );
89 
90 private:
92  CRectArray m_vRects;
93  CPosArray m_vPositions;
94  double m_area;
95 
96  // ---------------------
97 
98  bool IsFree( const TRect& r ) const;
99  void AddPosition( const TPos& p );
100  void AddRect( const TRect& r );
101  bool AddAtEmptySpot( TRect& r );
102 };
103 
104 #endif // _RECT_PLACEMENT_H_
int GetH() const
bool Contains(const TPos &p) const
void Init(int w=1, int h=1)
std::vector< TRect > CRectArray
bool AddAtEmptySpot(TRect &r)
std::vector< TPos > CPosArray
CPosArray m_vPositions
double GetTotalArea() const
CRectArray m_vRects
double GetArea() const
bool Intersects(const TRect &r) const
static bool Greater(const TRect &a, const TRect &b)
bool operator==(const TPos &p) const
void AddPosition(const TPos &p)
bool IsFree(const TRect &r) const
int GetW() const
bool Contains(const TRect &r) const
void AddRect(const TRect &r)
bool IsOk() const
TRect(int _x, int _y, int _w, int _h)
bool AddAtEmptySpotAutoGrow(TRect *pRect, int maxW, int maxH)
TPos(int _x, int _y)