Skip to content

Commit

Permalink
removed vector from split function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Husenap authored Jul 10, 2021
1 parent a577138 commit ab0a6f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
11 changes: 7 additions & 4 deletions dubu_rect_pack/src/dubu_rect_pack/packer/Packer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ std::optional<Rect> Packer::Pack(Size rectangle) {
continue;
}

const auto [occupied, newSpaces] = space.Split(rectangle);
const auto [occupied, newSpace1, newSpace2] = space.Split(rectangle);

mSpaces.erase(it.base() - 1);

std::move(std::begin(newSpaces),
std::end(newSpaces),
std::back_inserter(mSpaces));
if (newSpace1) {
mSpaces.emplace_back(std::move(*newSpace1));
if (newSpace2) {
mSpaces.emplace_back(std::move(*newSpace2));
}
}

std::sort(std::begin(mSpaces),
std::end(mSpaces),
Expand Down
43 changes: 23 additions & 20 deletions dubu_rect_pack/src/dubu_rect_pack/packer/Space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ bool Space::CanFitRect(Size rectangle) const {
return (rectangle.width <= mWidth) && (rectangle.height <= mHeight);
}

std::pair<Space, std::vector<Space>> Space::Split(Size rectangle) const {
std::tuple<Space, std::optional<Space>, std::optional<Space>> Space::Split(
Size rectangle) const {
assert(rectangle.width <= mWidth);
assert(rectangle.height <= mHeight);

if (rectangle.width == mWidth && rectangle.height == mHeight) {
return {*this, {}};
return {*this, std::nullopt, std::nullopt};
} else if (rectangle.width == mWidth) {
return {Space(mLeft, mTop, mWidth, rectangle.height),
{Space(mLeft,
mTop + rectangle.height,
mWidth,
mHeight - rectangle.height)}};
Space(mLeft,
mTop + rectangle.height,
mWidth,
mHeight - rectangle.height),
std::nullopt};
} else if (rectangle.height == mHeight) {
return {Space(mLeft, mTop, rectangle.width, mHeight),
{Space(mLeft + rectangle.width,
mTop,
mWidth - rectangle.width,
mHeight)}};
Space(mLeft + rectangle.width,
mTop,
mWidth - rectangle.width,
mHeight),
std::nullopt};
} else {
Space newSpace(mLeft, mTop, rectangle.width, rectangle.height);
Space bottomSpace(
Expand All @@ -43,18 +46,18 @@ std::pair<Space, std::vector<Space>> Space::Split(Size rectangle) const {

if (bottomSpace.Area() >= rightSpace.Area()) {
return {newSpace,
{bottomSpace,
Space(mLeft + rectangle.width,
mTop,
mWidth - rectangle.width,
rectangle.height)}};
bottomSpace,
Space(mLeft + rectangle.width,
mTop,
mWidth - rectangle.width,
rectangle.height)};
} else {
return {newSpace,
{rightSpace,
Space(mLeft,
mTop + rectangle.height,
rectangle.width,
mHeight - rectangle.height)}};
rightSpace,
Space(mLeft,
mTop + rectangle.height,
rectangle.width,
mHeight - rectangle.height)};
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion dubu_rect_pack/src/dubu_rect_pack/packer/Space.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Space {

bool CanFitRect(Size rectangle) const;

std::pair<Space, std::vector<Space>> Split(Size rectangle) const;
std::tuple<Space, std::optional<Space>, std::optional<Space>> Split(
Size rectangle) const;

std::uint32_t Area() const { return mArea; }

Expand Down

0 comments on commit ab0a6f9

Please sign in to comment.