1 #ifndef __DALI_TOOLKIT_ATLAS_PACKER_H__
2 #define __DALI_TOOLKIT_ATLAS_PACKER_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <dali/public-api/math/rect.h>
33 * Binary space tree based bin packing algorithm.
34 * It is initialised with a fixed width and height and will fit each block into the first node where it fits
35 * and then split that node into 2 parts (down and right) to track the remaining empty space.
42 * rectangular area (x,y,width,height)
44 typedef uint32_t SizeType;
45 typedef Rect<SizeType> RectArea;
52 Node( Node* parent, SizeType x, SizeType y, SizeType width, SizeType height );
63 * @param[in] atlasWidth The width of the atlas.
64 * @param[in] atlasHeight The height of the atlas.
66 AtlasPacker( SizeType atlasWidth, SizeType atlasHeight );
74 * Pack a block into the atlas.
76 * @param[in] blockWidth The width of the block to pack.
77 * @param[in] blockHeight The height of the block to pack.
78 * @param[out] packPositionX The x coordinate of the position to pack the block.
79 * @param[out] packPositionY The y coordinate of the position to pack the block.
80 * @return True if there are room for this block, false otherwise.
82 bool Pack( SizeType blockWidth, SizeType blockHeight,
83 SizeType& packPositionX, SizeType& packPositionY);
88 * @param[in] packPositionX The x coordinate of the pack position.
89 * @param[in] packPositionY The y coordinate of the pack position.
90 * @param[in] blockWidth The width of the block to delete.
91 * @param[in] blockHeight The height of the block to delete.
93 void DeleteBlock( SizeType packPositionX, SizeType packPositionY, SizeType blockWidth, SizeType blockHeight );
96 * Query how much empty space left.
98 * @return The area available for packing.
100 unsigned int GetAvailableArea() const;
105 * Search the node which can pack the block with given size.
107 * @param[in] root The root node of the subtree to be searched.
108 * @param[in] blockWidth The width of the block to pack.
109 * @param[in] blockHeight The height of the block to pack.
110 * @return The poniter pointing to node that can pack the block.
111 * If it is NULL, there are no room in the subtree to pack the block.
113 Node* InsertNode( Node* root, SizeType blockWidth, SizeType blockHeight );
116 * Split the node into two to fit the block width/size.
118 * @parm[in] node The node to split.
119 * @param[in] blockWidth The width of the block to pack.
120 * @param[in] blockHeight The height of the block to pack.
122 void SplitNode( Node* node, SizeType blockWidth, SizeType blockHeight );
125 * Search the node at the given position and with the given size.
127 * @param[in] node The root node of the subtree to be searched.
128 * @param[in] packPositionX The x coordinate of the pack position.
129 * @param[in] packPositionY The y coordinate of the pack position.
130 * @param[in] blockWidth The width of the block.
131 * @param[in] blockHeight The height of the block.
133 Node* SearchNode( Node* node, SizeType packPositionX, SizeType packPositionY, SizeType blockWidth, SizeType blockHeight );
136 * Merge the rect of the node to non-occupied area.
138 * @param[in] node The node to me merged to the non-occupied area
140 void MergeToNonOccupied( Node* node );
143 * Delete a node and its subtree.
145 * @parm[in] node The node to delete.
147 void DeleteNode( Node* node );
150 AtlasPacker( const AtlasPacker& imageAtlas);
153 AtlasPacker& operator=( const AtlasPacker& imageAtlas );
157 Node* mRoot; ///< The root of the binary space tree
158 unsigned int mAvailableArea;
163 } // namespace Internal
165 } // namespace Toolkit
169 #endif /* __DALI_TOOLKIT_ATLAS_PACKER_H__ */