Flutter Impeller
impeller::SkylineRectanglePacker Class Referencefinal
Inheritance diagram for impeller::SkylineRectanglePacker:
impeller::RectanglePacker

Public Member Functions

 SkylineRectanglePacker (int w, int h)
 
 ~SkylineRectanglePacker () final
 
void Reset () final
 Empty out all previously added rectangles. More...
 
bool AddRect (int w, int h, IPoint16 *loc) final
 Attempt to add a rect without moving already placed rectangles. More...
 
Scalar PercentFull () const final
 Returns how much area has been filled with rectangles. More...
 
- Public Member Functions inherited from impeller::RectanglePacker
virtual ~RectanglePacker ()
 

Additional Inherited Members

- Static Public Member Functions inherited from impeller::RectanglePacker
static std::shared_ptr< RectanglePackerFactory (int width, int height)
 Return an empty packer with area specified by width and height. More...
 
- Protected Member Functions inherited from impeller::RectanglePacker
 RectanglePacker (int width, int height)
 
int width () const
 
int height () const
 

Detailed Description

Definition at line 18 of file rectangle_packer.cc.

Constructor & Destructor Documentation

◆ SkylineRectanglePacker()

impeller::SkylineRectanglePacker::SkylineRectanglePacker ( int  w,
int  h 
)
inline

Definition at line 20 of file rectangle_packer.cc.

20 : RectanglePacker(w, h) { Reset(); }

References Reset().

◆ ~SkylineRectanglePacker()

impeller::SkylineRectanglePacker::~SkylineRectanglePacker ( )
inlinefinal

Definition at line 22 of file rectangle_packer.cc.

22 {}

Member Function Documentation

◆ AddRect()

bool impeller::SkylineRectanglePacker::AddRect ( int  width,
int  height,
IPoint16 loc 
)
finalvirtual

Attempt to add a rect without moving already placed rectangles.

Parameters
[in]widthThe width of the rectangle to add.
[in]heightThe height of the rectangle to add.
[out]locIf successful, will be set to the position of the upper-left corner of the rectangle.
Returns
Return true on success; false on failure.

Implements impeller::RectanglePacker.

Definition at line 61 of file rectangle_packer.cc.

61  {
62  if ((unsigned)p_width > (unsigned)width() ||
63  (unsigned)p_height > (unsigned)height()) {
64  return false;
65  }
66 
67  // find position for new rectangle
68  int bestWidth = width() + 1;
69  int bestX = 0;
70  int bestY = height() + 1;
71  int bestIndex = -1;
72  for (auto i = 0u; i < skyline_.size(); ++i) {
73  int y;
74  if (RectangleFits(i, p_width, p_height, &y)) {
75  // minimize y position first, then width of skyline
76  if (y < bestY || (y == bestY && skyline_[i].width_ < bestWidth)) {
77  bestIndex = i;
78  bestWidth = skyline_[i].width_;
79  bestX = skyline_[i].x_;
80  bestY = y;
81  }
82  }
83  }
84 
85  // add rectangle to skyline
86  if (-1 != bestIndex) {
87  AddSkylineLevel(bestIndex, bestX, bestY, p_width, p_height);
88  loc->x_ = bestX;
89  loc->y_ = bestY;
90 
91  area_so_far_ += p_width * p_height;
92  return true;
93  }
94 
95  loc->x_ = 0;
96  loc->y_ = 0;
97  return false;
98 }

References impeller::RectanglePacker::height(), impeller::RectanglePacker::width(), impeller::IPoint16::x_, and impeller::IPoint16::y_.

◆ PercentFull()

Scalar impeller::SkylineRectanglePacker::PercentFull ( ) const
inlinefinalvirtual

Returns how much area has been filled with rectangles.

Returns
Percentage as a decimal between 0.0 and 1.0

Implements impeller::RectanglePacker.

Definition at line 32 of file rectangle_packer.cc.

32  {
33  return area_so_far_ / ((float)width() * height());
34  }

References impeller::RectanglePacker::height(), and impeller::RectanglePacker::width().

◆ Reset()

void impeller::SkylineRectanglePacker::Reset ( )
inlinefinalvirtual

Empty out all previously added rectangles.

Implements impeller::RectanglePacker.

Definition at line 24 of file rectangle_packer.cc.

24  {
25  area_so_far_ = 0;
26  skyline_.clear();
27  skyline_.push_back(SkylineSegment{0, 0, width()});
28  }

References impeller::RectanglePacker::width().

Referenced by SkylineRectanglePacker().


The documentation for this class was generated from the following file:
impeller::RectanglePacker::width
int width() const
Definition: rectangle_packer.h:65
impeller::SkylineRectanglePacker::Reset
void Reset() final
Empty out all previously added rectangles.
Definition: rectangle_packer.cc:24
impeller::RectanglePacker::height
int height() const
Definition: rectangle_packer.h:66
impeller::RectanglePacker::RectanglePacker
RectanglePacker(int width, int height)
Definition: rectangle_packer.h:60