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
|
// --------------------------------------------------------------------------------
// Name : rect_placement.h
// Description : A class that allocates subrectangles into power-of-2 rectangles
// (C) Copyright 2000-2002 by Javier Arevalo
// This code is free to use and modify for all purposes
// --------------------------------------------------------------------------------
/**
* @file rect_placement.h
*/
#ifndef _RECT_PLACEMENT_H_
#define _RECT_PLACEMENT_H_
#include <vector>
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
class CRectPlacement
{
public:
// Helper classes
struct TPos
{
int x, y;
TPos() : x( 0 ), y( 0 ) { }
TPos( int _x, int _y ) : x( _x ), y( _y ) { }
bool operator ==( const TPos& p ) const { return x == p.x && y == p.y; }
};
struct TRect : public TPos
{
int w, h;
TRect() : w( 0 ), h( 0 ) { }
TRect( int _x, int _y, int _w, int _h ) : TPos( _x, _y ), w( _w > 0 ? _w : 0 ), h(
_h > 0 ? _h : 0 ) { }
bool Contains( const TPos& p ) const
{
return p.x >= x && p.y >= y && p.x < (x + w) && p.y < (y + h);
}
bool Contains( const TRect& r ) const
{
return r.x >= x && r.y >= y &&
(r.x + r.w) <= (x + w) && (r.y + r.h) <= (y + h);
}
bool Intersects( const TRect& r ) const
{
return w > 0 && h > 0 && r.w > 0 && r.h > 0
&& ( (r.x + r.w) > x && r.x < (x + w) && (r.y + r.h) > y && r.y < (y + h) );
}
// static bool Greater(const TRect &a, const TRect &b)
// { return a.w*a.h > b.w*b.h; }
// Greater rect area. Not as good as the next heuristic:
// Greater size in at least one dim.
static bool Greater( const TRect& a, const TRect& b )
{
return (a.w > b.w && a.w > b.h) || (a.h > b.w && a.h > b.h);
}
};
// ---------------------
typedef std::vector<TPos> CPosArray;
typedef std::vector<TRect> CRectArray;
// ---------------------
CRectPlacement() { Init(); }
~CRectPlacement() { End(); }
void Init( int w = 1, int h = 1 );
void End();
bool IsOk() const { return m_size.w > 0; }
int GetW() const { return m_size.w; }
int GetH() const { return m_size.h; }
double GetArea() const { return m_area; }
double GetTotalArea() const { return (double)m_size.w * m_size.h; }
bool AddAtEmptySpotAutoGrow( TRect* pRect, int maxW, int maxH );
private:
TRect m_size;
CRectArray m_vRects;
CPosArray m_vPositions;
double m_area;
// ---------------------
bool IsFree( const TRect& r ) const;
void AddPosition( const TPos& p );
void AddRect( const TRect& r );
bool AddAtEmptySpot( TRect& r );
};
#endif // _RECT_PLACEMENT_H_
|