microui_redux/rect_packer.rs
1//
2// Copyright 2023-Present (c) Raja Lehtihet & Wael El Oraiby
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9//
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// 3. Neither the name of the copyright holder nor the names of its contributors
15// may be used to endorse or promote products derived from this software without
16// specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28// POSSIBILITY OF SUCH DAMAGE.
29//
30////////////////////////////////////////////////////////////////////////////////
31//
32// The MIT License (MIT)
33//
34// Copyright (c) 2014 Coeuvre Wong
35//
36// Permission is hereby granted, free of charge, to any person obtaining a copy
37// of this software and associated documentation files (the "Software"), to deal
38// in the Software without restriction, including without limitation the rights
39// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40// copies of the Software, and to permit persons to whom the Software is
41// furnished to do so, subject to the following conditions:
42//
43// The above copyright notice and this permission notice shall be included in all
44// copies or substantial portions of the Software.
45//
46// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52// SOFTWARE.
53
54//! Pack small rectangles into a larger one. This is useful for creating texture atlases for the efficient GPU rendering.
55
56use crate::*;
57
58/// Describes size and padding requirements of rectangle packing.
59#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
60pub struct Config {
61 /// Width of the encompassing rectangle.
62 pub width: i32,
63 /// Height of the encompassing rectangle.
64 pub height: i32,
65
66 /// Minimum spacing between border and rectangles.
67 pub border_padding: i32,
68 /// Minimum spacing between rectangles.
69 pub rectangle_padding: i32,
70}
71
72/// Simplified rectangle interface required by the packer implementations.
73pub trait RectTrait {
74 /// Returns the top edge (minimum y).
75 fn top(&self) -> i32;
76 /// Returns the bottom edge (exclusive).
77 fn bottom(&self) -> i32;
78 /// Returns the left edge (minimum x).
79 fn left(&self) -> i32;
80 /// Returns the right edge (exclusive).
81 fn right(&self) -> i32;
82
83 /// Returns the area of the rectangle.
84 fn area(&self) -> i32 { (self.bottom() - self.top()) * (self.right() - self.left()) }
85
86 /// Check if intersection of two rectangles is non empty.
87 fn intersects(&self, other: &Self) -> bool {
88 self.contains_point(other.left(), other.top())
89 || self.contains_point(other.left(), other.bottom() - 1)
90 || self.contains_point(other.right() - 1, other.bottom() - 1)
91 || self.contains_point(other.right() - 1, other.top())
92 || other.contains_point(self.left(), self.top())
93 || other.contains_point(self.left(), self.bottom() - 1)
94 || other.contains_point(self.right() - 1, self.bottom() - 1)
95 || other.contains_point(self.right() - 1, self.top())
96 }
97
98 /// Check if `other` rectangle is completely inside `self`.
99 fn contains_rect(&self, other: &Self) -> bool {
100 self.left() <= other.left() && self.right() >= other.right() && self.top() <= other.top() && self.bottom() >= other.bottom()
101 }
102
103 /// Check if given pixel is inside this rectangle.
104 fn contains_point(&self, x: i32, y: i32) -> bool { self.left() <= x && x < self.right() && self.top() <= y && y < self.bottom() }
105}
106
107impl RectTrait for Recti {
108 #[inline(always)]
109 fn top(&self) -> i32 { self.y }
110
111 #[inline(always)]
112 fn bottom(&self) -> i32 { self.y + self.height }
113
114 #[inline(always)]
115 fn left(&self) -> i32 { self.x }
116
117 #[inline(always)]
118 fn right(&self) -> i32 { self.x + self.width }
119}
120
121/// `Packer` is the main structure in this crate. It holds packing context.
122#[derive(Clone)]
123pub struct Packer {
124 config: Config,
125 packer: DensePacker,
126}
127
128impl Packer {
129 /// Create new empty `Packer` with the provided parameters.
130 pub fn new(config: Config) -> Packer {
131 let width = std::cmp::max(0, config.width + config.rectangle_padding - 2 * config.border_padding);
132 let height = std::cmp::max(0, config.height + config.rectangle_padding - 2 * config.border_padding);
133
134 Packer {
135 config: config,
136 packer: DensePacker::new(width, height),
137 }
138 }
139
140 /// Get config that this packer was created with.
141 pub fn config(&self) -> Config { self.config }
142
143 /// Pack new rectangle. Returns position of the newly added rectangle. If there is not enough space returns `None`.
144 /// If it returns `None` you can still try to add smaller rectangles.
145 ///
146 /// `allow_rotation` - allow 90° rotation of the input rectangle. You can detect whether rectangle was rotated by comparing
147 /// returned `width` and `height` with the supplied ones.
148 pub fn pack(&mut self, width: i32, height: i32, allow_rotation: bool) -> Option<Recti> {
149 if width <= 0 || height <= 0 {
150 return None;
151 }
152
153 if let Some(mut rect) = self
154 .packer
155 .pack(width + self.config.rectangle_padding, height + self.config.rectangle_padding, allow_rotation)
156 {
157 rect.width -= self.config.rectangle_padding;
158 rect.height -= self.config.rectangle_padding;
159 rect.x += self.config.border_padding;
160 rect.y += self.config.border_padding;
161
162 Some(rect)
163 } else {
164 None
165 }
166 }
167
168 /// Check if rectangle with the specified size can be added.
169 pub fn can_pack(&self, width: i32, height: i32, allow_rotation: bool) -> bool {
170 self.packer
171 .can_pack(width + self.config.rectangle_padding, height + self.config.rectangle_padding, allow_rotation)
172 }
173}
174
175#[derive(Clone)]
176struct Skyline {
177 pub left: i32,
178 pub y: i32,
179 pub width: i32,
180}
181
182impl Skyline {
183 #[inline(always)]
184 pub fn right(&self) -> i32 { self.left + self.width }
185}
186
187/// Similar to `Packer` but does not add any padding between rectangles.
188#[derive(Clone)]
189pub struct DensePacker {
190 width: i32,
191 height: i32,
192
193 // the skylines are sorted by their `x` position
194 skylines: Vec<Skyline>,
195}
196
197impl DensePacker {
198 /// Create new empty `DensePacker` with the provided parameters.
199 pub fn new(width: i32, height: i32) -> DensePacker {
200 let width = std::cmp::max(0, width);
201 let height = std::cmp::max(0, height);
202
203 let skylines = vec![Skyline { left: 0, y: 0, width: width }];
204
205 DensePacker {
206 width: width,
207 height: height,
208 skylines: skylines,
209 }
210 }
211
212 /// Get size that this packer was created with.
213 pub fn size(&self) -> (i32, i32) { (self.width, self.height) }
214
215 /// Set new size for this packer.
216 ///
217 /// New size should be not less than the current size.
218 pub fn resize(&mut self, width: i32, height: i32) {
219 assert!(width >= self.width && height >= self.height);
220
221 self.width = width;
222 self.height = height;
223
224 // Add a new skyline to fill the gap
225 // The new skyline starts where the furthest one ends
226 let left = self.skylines.last().unwrap().right();
227 self.skylines.push(Skyline { left: left, y: 0, width: width - left });
228 }
229
230 /// Pack new rectangle. Returns position of the newly added rectangle. If there is not enough space returns `None`.
231 /// If it returns `None` you can still try to add smaller rectangles.
232 ///
233 /// `allow_rotation` - allow 90° rotation of the input rectangle. You can detect whether rectangle was rotated by comparing
234 /// returned `width` and `height` with the supplied ones.
235 pub fn pack(&mut self, width: i32, height: i32, allow_rotation: bool) -> Option<Recti> {
236 if width <= 0 || height <= 0 {
237 return None;
238 }
239
240 if let Some((i, rect)) = self.find_skyline(width, height, allow_rotation) {
241 self.split(i, &rect);
242 self.merge();
243
244 Some(rect)
245 } else {
246 None
247 }
248 }
249
250 /// Check if rectangle with the specified size can be added.
251 pub fn can_pack(&self, width: i32, height: i32, allow_rotation: bool) -> bool { self.find_skyline(width, height, allow_rotation).is_some() }
252
253 // return `rect` if rectangle (w, h) can fit the skyline started at `i`
254 fn can_put(&self, mut i: usize, w: i32, h: i32) -> Option<Recti> {
255 let mut rect = Rect::new(self.skylines[i].left, 0, w, h);
256 let mut width_left = rect.width;
257 loop {
258 rect.y = std::cmp::max(rect.y, self.skylines[i].y);
259 // the source rect is too large
260 if !Rect::new(0, 0, self.width, self.height).contains_rect(&rect) {
261 return None;
262 }
263 if self.skylines[i].width >= width_left {
264 return Some(rect);
265 }
266 width_left -= self.skylines[i].width;
267 i += 1;
268 assert!(i < self.skylines.len());
269 }
270 }
271
272 fn find_skyline(&self, w: i32, h: i32, allow_rotation: bool) -> Option<(usize, Recti)> {
273 let mut bottom = std::i32::MAX;
274 let mut width = std::i32::MAX;
275 let mut index = None;
276 let mut rect = Rect::new(0, 0, 0, 0);
277
278 // keep the `bottom` and `width` as small as possible
279 for i in 0..self.skylines.len() {
280 if let Some(r) = self.can_put(i, w, h) {
281 if r.bottom() < bottom || (r.bottom() == bottom && self.skylines[i].width < width) {
282 bottom = r.bottom();
283 width = self.skylines[i].width;
284 index = Some(i);
285 rect = r;
286 }
287 }
288
289 if allow_rotation {
290 if let Some(r) = self.can_put(i, h, w) {
291 if r.bottom() < bottom || (r.bottom() == bottom && self.skylines[i].width < width) {
292 bottom = r.bottom();
293 width = self.skylines[i].width;
294 index = Some(i);
295 rect = r;
296 }
297 }
298 }
299 }
300
301 if let Some(index) = index {
302 Some((index, rect))
303 } else {
304 None
305 }
306 }
307
308 fn split(&mut self, i: usize, rect: &Recti) {
309 let skyline = Skyline {
310 left: rect.left(),
311 y: rect.bottom(),
312 width: rect.width,
313 };
314
315 assert!(skyline.right() <= self.width);
316 assert!(skyline.y <= self.height);
317
318 self.skylines.insert(i, skyline);
319
320 while i + 1 < self.skylines.len() {
321 assert!(self.skylines[i].left <= self.skylines[i + 1].left);
322
323 if self.skylines[i + 1].left >= self.skylines[i].right() {
324 break;
325 }
326
327 let shrink = self.skylines[i].right() - self.skylines[i + 1].left;
328 if self.skylines[i + 1].width <= shrink {
329 self.skylines.remove(i + 1);
330 } else {
331 self.skylines[i + 1].left += shrink;
332 self.skylines[i + 1].width -= shrink;
333 break;
334 }
335 }
336 }
337
338 fn merge(&mut self) {
339 let mut i = 1;
340 while i < self.skylines.len() {
341 if self.skylines[i - 1].y == self.skylines[i].y {
342 self.skylines[i - 1].width += self.skylines[i].width;
343 self.skylines.remove(i);
344 } else {
345 i += 1;
346 }
347 }
348 }
349}