@@ -423,6 +423,11 @@ bool MaxRectsBinPack::SplitFreeNode(const Rect &freeNode, const Rect &usedNode)
423
423
usedNode.y >= freeNode.y + freeNode.height || usedNode.y + usedNode.height <= freeNode.y )
424
424
return false ;
425
425
426
+ // We add up to four new free rectangles to the free rectangles list below. None of these
427
+ // four newly added free rectangles can overlap any other three, so keep a mark of them
428
+ // to avoid testing them against each other.
429
+ newFreeRectanglesLastSize = newFreeRectangles.size ();
430
+
426
431
if (usedNode.x < freeNode.x + freeNode.width && usedNode.x + usedNode.width > freeNode.x )
427
432
{
428
433
// New node at the top side of the used node.
@@ -471,7 +476,7 @@ void MaxRectsBinPack::InsertNewFreeRectangle(const Rect &newFreeRect)
471
476
assert (newFreeRect.width > 0 );
472
477
assert (newFreeRect.height > 0 );
473
478
474
- for (size_t i = 0 ; i < newFreeRectangles. size () ;)
479
+ for (size_t i = 0 ; i < newFreeRectanglesLastSize ;)
475
480
{
476
481
// This new free rectangle is already accounted for?
477
482
if (IsContainedIn (newFreeRect, newFreeRectangles[i]))
@@ -480,7 +485,11 @@ void MaxRectsBinPack::InsertNewFreeRectangle(const Rect &newFreeRect)
480
485
// Does this new free rectangle obsolete a previous new free rectangle?
481
486
if (IsContainedIn (newFreeRectangles[i], newFreeRect))
482
487
{
483
- newFreeRectangles[i] = newFreeRectangles.back ();
488
+ // Remove i'th new free rectangle, but do so by retaining the order
489
+ // of the older vs newest free rectangles that we may still be placing
490
+ // in calling function SplitFreeNode().
491
+ newFreeRectangles[i] = newFreeRectangles[--newFreeRectanglesLastSize];
492
+ newFreeRectangles[newFreeRectanglesLastSize] = newFreeRectangles.back ();
484
493
newFreeRectangles.pop_back ();
485
494
}
486
495
else
@@ -514,6 +523,15 @@ void MaxRectsBinPack::PruneFreeList()
514
523
// Merge new and old free rectangles to the group of old free rectangles.
515
524
freeRectangles.insert (freeRectangles.end (), newFreeRectangles.begin (), newFreeRectangles.end ());
516
525
newFreeRectangles.clear ();
526
+
527
+ #ifdef _DEBUG
528
+ for (size_t i = 0 ; i < freeRectangles.size (); ++i)
529
+ for (size_t j = i+1 ; j < freeRectangles.size (); ++j)
530
+ {
531
+ assert (!IsContainedIn (freeRectangles[i], freeRectangles[j]));
532
+ assert (!IsContainedIn (freeRectangles[j], freeRectangles[i]));
533
+ }
534
+ #endif
517
535
}
518
536
519
537
}
0 commit comments