Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
GuillotineBinPack.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 // Implémentation originale de Jukka Jylänki (Merci de sa contribution au domaine public)
6 // http://clb.demon.fi/projects/even-more-rectangle-bin-packing
7 
8 #pragma once
9 
10 #ifndef NAZARA_GUILLOTINEBINPACK_HPP
11 #define NAZARA_GUILLOTINEBINPACK_HPP
12 
13 #include <Nazara/Prerequesites.hpp>
14 #include <Nazara/Math/Rect.hpp>
15 #include <vector>
16 
17 namespace Nz
18 {
19  class NAZARA_CORE_API GuillotineBinPack
20  {
21  public:
22  enum FreeRectChoiceHeuristic : int;
23  enum GuillotineSplitHeuristic : int;
24 
26  GuillotineBinPack(unsigned int width, unsigned int height);
27  GuillotineBinPack(const Vector2ui& size);
28  GuillotineBinPack(const GuillotineBinPack&) = default;
30  ~GuillotineBinPack() = default;
31 
32  void Clear();
33 
34  void Expand(unsigned int newWidth, unsigned newHeight);
35  void Expand(const Vector2ui& newSize);
36 
37  void FreeRectangle(const Rectui& rect);
38 
39  unsigned int GetHeight() const;
40  float GetOccupancy() const;
41  Vector2ui GetSize() const;
42  unsigned int GetWidth() const;
43 
44  bool Insert(Rectui* rects, unsigned int count, bool merge, FreeRectChoiceHeuristic rectChoice, GuillotineSplitHeuristic splitMethod);
45  bool Insert(Rectui* rects, bool* flipped, unsigned int count, bool merge, FreeRectChoiceHeuristic rectChoice, GuillotineSplitHeuristic splitMethod);
46  bool Insert(Rectui* rects, bool* flipped, bool* inserted, unsigned int count, bool merge, FreeRectChoiceHeuristic rectChoice, GuillotineSplitHeuristic splitMethod);
47 
48  bool MergeFreeRectangles();
49 
50  void Reset();
51  void Reset(unsigned int width, unsigned int height);
52  void Reset(const Vector2ui& size);
53 
54  GuillotineBinPack& operator=(const GuillotineBinPack&) = default;
55  GuillotineBinPack& operator=(GuillotineBinPack&&) = default;
56 
57  enum FreeRectChoiceHeuristic : int
58  {
59  RectBestAreaFit,
60  RectBestLongSideFit,
61  RectBestShortSideFit,
62  RectWorstAreaFit,
63  RectWorstLongSideFit,
64  RectWorstShortSideFit
65  };
66 
67  enum GuillotineSplitHeuristic : int
68  {
69  SplitLongerAxis,
70  SplitLongerLeftoverAxis,
71  SplitMaximizeArea,
72  SplitMinimizeArea,
73  SplitShorterAxis,
74  SplitShorterLeftoverAxis
75  };
76 
77  private:
78  void SplitFreeRectAlongAxis(const Rectui& freeRect, const Rectui& placedRect, bool splitHorizontal);
79  void SplitFreeRectByHeuristic(const Rectui& freeRect, const Rectui& placedRect, GuillotineSplitHeuristic method);
80 
81  static int ScoreByHeuristic(int width, int height, const Rectui& freeRect, FreeRectChoiceHeuristic rectChoice);
82 
83  std::vector<Rectui> m_freeRectangles;
84  unsigned int m_height;
85  unsigned int m_usedArea;
86  unsigned int m_width;
87  };
88 }
89 
90 #endif // NAZARA_GUILLOTINEBINPACK_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Core class that represents the "Guillotine problem", combination of the "Bin packing problem" and the...
Definition: GuillotineBinPack.hpp:19