Ocean
BinPacking.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_SEGMENTATION_BIN_PACKING_H
9 #define META_OCEAN_CV_SEGMENTATION_BIN_PACKING_H
10 
12 
14 #include "ocean/cv/PixelPosition.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Segmentation
23 {
24 
25 /**
26  * This class implements bin packing algorithms.
27  * @ingroup cvsegmentation
28  */
29 class OCEAN_CV_SEGMENTATION_EXPORT BinPacking
30 {
31  public:
32 
33  /**
34  * This class provides the relevant packing information for a 2D box with pixel precision.
35  */
36  class Packing
37  {
38  public:
39 
40  /**
41  * Default constructor creating an invalid packing information.
42  */
43  Packing() = default;
44 
45  /**
46  * Creates a new packing information for a 2D box with pixel precision.
47  * @param boxIndex The index of the box
48  * @param topLeft The location of the top left corner of the packed 2D box
49  * @param transposed True, if the box needs to be transposed; False, if the box is packed as given
50  */
51  inline Packing(const Index32 boxIndex, const CV::PixelPosition& topLeft, const bool transposed);
52 
53  /**
54  * Returns the index of the packed 2D box.
55  * @return The index of the corresponding box
56  */
57  inline Index32 boxIndex() const;
58 
59  /**
60  * Returns the location of the top left corner of the packed 2D box.
61  * @return The top left corner of the corresponding box
62  */
63  inline const CV::PixelPosition topLeft() const;
64 
65  /**
66  * Returns whether the packed 2D box needs to be transposed.
67  * @return True, if so
68  */
69  inline bool transposed() const;
70 
71  protected:
72 
73  /// The index of the box.
74  const Index32 boxIndex_ = Index32(-1);
75 
76  /// The location of the top left corner of the packed 2D box.
78 
79  /// True, if the box needs to be transposed; False, if the box is packed as given.
80  const bool transposed_ = false;
81  };
82 
83  /**
84  * Definition of a vector holding Packing objects.
85  */
86  typedef std::vector<Packing> Packings;
87 
88  protected:
89 
90  /**
91  * Definition of a pair combining a 2D box with an id.
92  */
93  typedef std::pair<CV::PixelBoundingBox, Index32> BoundingBoxIdPair;
94 
95  /**
96  * Definition of a vector holding box id pairs.
97  */
98  typedef std::vector<BoundingBoxIdPair> BoundingBoxIdPairs;
99 
100  /**
101  * Definition of an ordered set holding 2D boxes with pixel accuracy.
102  */
103  typedef std::set<CV::PixelBoundingBox, BinPacking> BoxSet;
104 
105  public:
106 
107  /**
108  * Invokes a bin packing for given 2D boxes with pixel precision.
109  * @param boxes The 2D boxed to be packed
110  * @param allowTransposed True, if boxes can be transposed during packing; False, to not allow transposing boxes
111  * @param width Optional resulting width the packed area, in pixel, nullptr if not of interest
112  * @param height Optional resulting height the packed area, in pixel, nullptr if not of interest
113  * @return The resulting packing information, one packing for each box
114  */
115  static Packings binPacking(const CV::PixelBoundingBoxes& boxes, const bool allowTransposed, unsigned int* width = nullptr, unsigned int* height = nullptr);
116 
117  /**
118  * Compares the size of two boxes and returns whether the first box is smaller than the second box.
119  * In case both boxes have the same size, the location of the box is used for comparison as well.
120  * @param boxA The first box to compare
121  * @param boxB The second box to compare
122  * @return True, if the first box is smaller than the second box
123  */
124  inline bool operator()(const CV::PixelBoundingBox& boxA, const CV::PixelBoundingBox& boxB) const;
125 
126  protected:
127 
128  /**
129  * Compares two pairs of bounding boxes with ids and returns whether the left pair as a lower id than the second pair.
130  * @param boundingBoxIdPairA The first box to compare
131  * @param boundingBoxIdPairB The second box to compare
132  * @return True, if so
133  */
134  static inline bool sortBoundingBoxIdPair(const BoundingBoxIdPair& boundingBoxIdPairA, const BoundingBoxIdPair& boundingBoxIdPairB);
135 };
136 
137 inline BinPacking::Packing::Packing(const Index32 boxIndex, const CV::PixelPosition& topLeft, const bool transposed) :
138  boxIndex_(boxIndex),
139  topLeft_(topLeft),
140  transposed_(transposed)
141 {
142  // nothing to do here
143 }
144 
146 {
147  return boxIndex_;
148 }
149 
151 {
152  return topLeft_;
153 }
154 
156 {
157  return transposed_;
158 }
159 
160 inline bool BinPacking::operator()(const CV::PixelBoundingBox& boxA, const CV::PixelBoundingBox& boxB) const
161 {
162  const uint64_t sizeA = uint64_t(boxA.width()) * uint64_t(boxA.height());
163  const uint64_t sizeB = uint64_t(boxB.width()) * uint64_t(boxB.height());
164 
165  if (sizeA == sizeB)
166  {
167  return boxA.left() < boxB.left() || (boxA.left() == boxB.left() && boxA.top() < boxB.top());
168  }
169 
170  return sizeA < sizeB;
171 }
172 
173 inline bool BinPacking::sortBoundingBoxIdPair(const BoundingBoxIdPair& boundingBoxIdPairA, const BoundingBoxIdPair& boundingBoxIdPairB)
174 {
175  return boundingBoxIdPairA.first.size() < boundingBoxIdPairB.first.size();
176 }
177 
178 }
179 
180 }
181 
182 }
183 
184 #endif // META_OCEAN_CV_SEGMENTATION_BIN_PACKING_H
T left() const
Returns the left (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:416
unsigned int width() const
Returns the width (the number of horizontal including pixels) of this bounding box.
Definition: PixelBoundingBox.h:482
T top() const
Returns the top (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:423
unsigned int height() const
Returns the height (the number of vertical including pixels) of this bounding box.
Definition: PixelBoundingBox.h:489
This class provides the relevant packing information for a 2D box with pixel precision.
Definition: BinPacking.h:37
const CV::PixelPosition topLeft() const
Returns the location of the top left corner of the packed 2D box.
Definition: BinPacking.h:150
bool transposed() const
Returns whether the packed 2D box needs to be transposed.
Definition: BinPacking.h:155
Index32 boxIndex() const
Returns the index of the packed 2D box.
Definition: BinPacking.h:145
Packing()=default
Default constructor creating an invalid packing information.
const CV::PixelPosition topLeft_
The location of the top left corner of the packed 2D box.
Definition: BinPacking.h:77
This class implements bin packing algorithms.
Definition: BinPacking.h:30
static Packings binPacking(const CV::PixelBoundingBoxes &boxes, const bool allowTransposed, unsigned int *width=nullptr, unsigned int *height=nullptr)
Invokes a bin packing for given 2D boxes with pixel precision.
std::vector< BoundingBoxIdPair > BoundingBoxIdPairs
Definition of a vector holding box id pairs.
Definition: BinPacking.h:98
std::set< CV::PixelBoundingBox, BinPacking > BoxSet
Definition of an ordered set holding 2D boxes with pixel accuracy.
Definition: BinPacking.h:103
bool operator()(const CV::PixelBoundingBox &boxA, const CV::PixelBoundingBox &boxB) const
Compares the size of two boxes and returns whether the first box is smaller than the second box.
Definition: BinPacking.h:160
static bool sortBoundingBoxIdPair(const BoundingBoxIdPair &boundingBoxIdPairA, const BoundingBoxIdPair &boundingBoxIdPairB)
Compares two pairs of bounding boxes with ids and returns whether the left pair as a lower id than th...
Definition: BinPacking.h:173
std::vector< Packing > Packings
Definition of a vector holding Packing objects.
Definition: BinPacking.h:86
std::pair< CV::PixelBoundingBox, Index32 > BoundingBoxIdPair
Definition of a pair combining a 2D box with an id.
Definition: BinPacking.h:93
uint32_t Index32
Definition of a 32 bit index value.
Definition: Base.h:84
std::vector< PixelBoundingBox > PixelBoundingBoxes
Definition of a vector holding bounding box objects with only positive coordinate values.
Definition: PixelBoundingBox.h:42
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15