1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_RECTANGLELIST_H_INCLUDED
#define JUCE_RECTANGLELIST_H_INCLUDED
//==============================================================================
/**
Maintains a set of rectangles as a complex region.
This class allows a set of rectangles to be treated as a solid shape, and can
add and remove rectangular sections of it, and simplify overlapping or
adjacent rectangles.
@see Rectangle
*/
template <typename ValueType>
class RectangleList
{
public:
typedef Rectangle<ValueType> RectangleType;
//==============================================================================
/** Creates an empty RectangleList */
RectangleList() noexcept {}
/** Creates a copy of another list */
RectangleList (const RectangleList& other) : rects (other.rects)
{
}
/** Creates a list containing just one rectangle. */
RectangleList (const RectangleType& rect)
{
addWithoutMerging (rect);
}
/** Copies this list from another one. */
RectangleList& operator= (const RectangleList& other)
{
rects = other.rects;
return *this;
}
#if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
RectangleList (RectangleList&& other) noexcept
: rects (static_cast<Array<RectangleType>&&> (other.rects))
{
}
RectangleList& operator= (RectangleList&& other) noexcept
{
rects = static_cast<Array<RectangleType>&&> (other.rects);
return *this;
}
#endif
//==============================================================================
/** Returns true if the region is empty. */
bool isEmpty() const noexcept { return rects.size() == 0; }
/** Returns the number of rectangles in the list. */
int getNumRectangles() const noexcept { return rects.size(); }
/** Returns one of the rectangles at a particular index.
@returns the rectangle at the index, or an empty rectangle if the index is out-of-range.
*/
RectangleType getRectangle (int index) const noexcept { return rects[index]; }
//==============================================================================
/** Removes all rectangles to leave an empty region. */
void clear()
{
rects.clearQuick();
}
/** Merges a new rectangle into the list.
The rectangle being added will first be clipped to remove any parts of it
that overlap existing rectangles in the list, and adjacent rectangles will be
merged into it.
The rectangle can have any size and may be empty, but if it's floating point
then it's expected to not contain any INF values.
*/
void add (const RectangleType& rect)
{
jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
if (! rect.isEmpty())
{
if (rects.size() == 0)
{
rects.add (rect);
}
else
{
bool anyOverlaps = false;
for (int j = rects.size(); --j >= 0;)
{
RectangleType& ourRect = rects.getReference (j);
if (rect.intersects (ourRect))
{
if (rect.contains (ourRect))
rects.remove (j);
else if (! ourRect.reduceIfPartlyContainedIn (rect))
anyOverlaps = true;
}
}
if (anyOverlaps && rects.size() > 0)
{
RectangleList r (rect);
for (int i = rects.size(); --i >= 0;)
{
const RectangleType& ourRect = rects.getReference (i);
if (rect.intersects (ourRect))
{
r.subtract (ourRect);
if (r.rects.size() == 0)
return;
}
}
rects.addArray (r.rects);
}
else
{
rects.add (rect);
}
}
}
}
/** Merges a new rectangle into the list.
The rectangle being added will first be clipped to remove any parts of it
that overlap existing rectangles in the list.
*/
void add (ValueType x, ValueType y, ValueType width, ValueType height)
{
add (RectangleType (x, y, width, height));
}
/** Dumbly adds a rectangle to the list without checking for overlaps.
This simply adds the rectangle to the end, it doesn't merge it or remove
any overlapping bits.
The rectangle can have any size and may be empty, but if it's floating point
then it's expected to not contain any INF values.
*/
void addWithoutMerging (const RectangleType& rect)
{
jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
if (! rect.isEmpty())
rects.add (rect);
}
/** Merges another rectangle list into this one.
Any overlaps between the two lists will be clipped, so that the result is
the union of both lists.
*/
void add (const RectangleList& other)
{
for (const RectangleType* r = other.begin(), * const e = other.end(); r != e; ++r)
add (*r);
}
/** Removes a rectangular region from the list.
Any rectangles in the list which overlap this will be clipped and subdivided
if necessary.
*/
void subtract (const RectangleType& rect)
{
const int originalNumRects = rects.size();
if (originalNumRects > 0)
{
const ValueType x1 = rect.getX();
const ValueType y1 = rect.getY();
const ValueType x2 = x1 + rect.getWidth();
const ValueType y2 = y1 + rect.getHeight();
for (int i = getNumRectangles(); --i >= 0;)
{
RectangleType& r = rects.getReference (i);
const ValueType rx1 = r.getX();
const ValueType ry1 = r.getY();
const ValueType rx2 = rx1 + r.getWidth();
const ValueType ry2 = ry1 + r.getHeight();
if (! (x2 <= rx1 || x1 >= rx2 || y2 <= ry1 || y1 >= ry2))
{
if (x1 > rx1 && x1 < rx2)
{
if (y1 <= ry1 && y2 >= ry2 && x2 >= rx2)
{
r.setWidth (x1 - rx1);
}
else
{
r.setX (x1);
r.setWidth (rx2 - x1);
rects.insert (++i, RectangleType (rx1, ry1, x1 - rx1, ry2 - ry1));
++i;
}
}
else if (x2 > rx1 && x2 < rx2)
{
r.setX (x2);
r.setWidth (rx2 - x2);
if (y1 > ry1 || y2 < ry2 || x1 > rx1)
{
rects.insert (++i, RectangleType (rx1, ry1, x2 - rx1, ry2 - ry1));
++i;
}
}
else if (y1 > ry1 && y1 < ry2)
{
if (x1 <= rx1 && x2 >= rx2 && y2 >= ry2)
{
r.setHeight (y1 - ry1);
}
else
{
r.setY (y1);
r.setHeight (ry2 - y1);
rects.insert (++i, RectangleType (rx1, ry1, rx2 - rx1, y1 - ry1));
++i;
}
}
else if (y2 > ry1 && y2 < ry2)
{
r.setY (y2);
r.setHeight (ry2 - y2);
if (x1 > rx1 || x2 < rx2 || y1 > ry1)
{
rects.insert (++i, RectangleType (rx1, ry1, rx2 - rx1, y2 - ry1));
++i;
}
}
else
{
rects.remove (i);
}
}
}
}
}
/** Removes all areas in another RectangleList from this one.
Any rectangles in the list which overlap this will be clipped and subdivided
if necessary.
@returns true if the resulting list is non-empty.
*/
bool subtract (const RectangleList& otherList)
{
for (int i = otherList.rects.size(); --i >= 0 && rects.size() > 0;)
subtract (otherList.rects.getReference (i));
return rects.size() > 0;
}
/** Removes any areas of the region that lie outside a given rectangle.
Any rectangles in the list which overlap this will be clipped and subdivided
if necessary.
Returns true if the resulting region is not empty, false if it is empty.
@see getIntersectionWith
*/
bool clipTo (const RectangleType& rect)
{
jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
bool notEmpty = false;
if (rect.isEmpty())
{
clear();
}
else
{
for (int i = rects.size(); --i >= 0;)
{
RectangleType& r = rects.getReference (i);
if (! rect.intersectRectangle (r))
rects.remove (i);
else
notEmpty = true;
}
}
return notEmpty;
}
/** Removes any areas of the region that lie outside a given rectangle list.
Any rectangles in this object which overlap the specified list will be clipped
and subdivided if necessary.
Returns true if the resulting region is not empty, false if it is empty.
@see getIntersectionWith
*/
template <typename OtherValueType>
bool clipTo (const RectangleList<OtherValueType>& other)
{
if (rects.size() == 0)
return false;
RectangleList result;
for (int j = 0; j < rects.size(); ++j)
{
const RectangleType& rect = rects.getReference (j);
for (const Rectangle<OtherValueType>* r = other.begin(), * const e = other.end(); r != e; ++r)
{
RectangleType clipped (r->template toType<ValueType>());
if (rect.intersectRectangle (clipped))
result.rects.add (clipped);
}
}
swapWith (result);
return ! isEmpty();
}
/** Creates a region which is the result of clipping this one to a given rectangle.
Unlike the other clipTo method, this one doesn't affect this object - it puts the
resulting region into the list whose reference is passed-in.
Returns true if the resulting region is not empty, false if it is empty.
@see clipTo
*/
bool getIntersectionWith (const RectangleType& rect, RectangleList& destRegion) const
{
jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
destRegion.clear();
if (! rect.isEmpty())
{
for (int i = rects.size(); --i >= 0;)
{
RectangleType r (rects.getReference (i));
if (rect.intersectRectangle (r))
destRegion.rects.add (r);
}
}
return destRegion.rects.size() > 0;
}
/** Swaps the contents of this and another list.
This swaps their internal pointers, so is hugely faster than using copy-by-value
to swap them.
*/
void swapWith (RectangleList& otherList) noexcept
{
rects.swapWith (otherList.rects);
}
//==============================================================================
/** Checks whether the region contains a given point.
@returns true if the point lies within one of the rectangles in the list
*/
bool containsPoint (Point<ValueType> point) const noexcept
{
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (r->contains (point))
return true;
return false;
}
/** Checks whether the region contains a given point.
@returns true if the point lies within one of the rectangles in the list
*/
bool containsPoint (ValueType x, ValueType y) const noexcept
{
return containsPoint (Point<ValueType> (x, y));
}
/** Checks whether the region contains the whole of a given rectangle.
@returns true all parts of the rectangle passed in lie within the region
defined by this object
@see intersectsRectangle, containsPoint
*/
bool containsRectangle (const RectangleType& rectangleToCheck) const
{
if (rects.size() > 1)
{
RectangleList r (rectangleToCheck);
for (int i = rects.size(); --i >= 0;)
{
r.subtract (rects.getReference (i));
if (r.rects.size() == 0)
return true;
}
}
else if (rects.size() > 0)
{
return rects.getReference (0).contains (rectangleToCheck);
}
return false;
}
/** Checks whether the region contains any part of a given rectangle.
@returns true if any part of the rectangle passed in lies within the region
defined by this object
@see containsRectangle
*/
bool intersectsRectangle (const RectangleType& rectangleToCheck) const noexcept
{
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (r->intersects (rectangleToCheck))
return true;
return false;
}
/** Checks whether this region intersects any part of another one.
@see intersectsRectangle
*/
bool intersects (const RectangleList& other) const noexcept
{
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (other.intersectsRectangle (*r))
return true;
return false;
}
//==============================================================================
/** Returns the smallest rectangle that can enclose the whole of this region. */
RectangleType getBounds() const noexcept
{
if (rects.size() <= 1)
{
if (rects.size() == 0)
return RectangleType();
return rects.getReference (0);
}
const RectangleType& r = rects.getReference (0);
ValueType minX = r.getX();
ValueType minY = r.getY();
ValueType maxX = minX + r.getWidth();
ValueType maxY = minY + r.getHeight();
for (int i = rects.size(); --i > 0;)
{
const RectangleType& r2 = rects.getReference (i);
minX = jmin (minX, r2.getX());
minY = jmin (minY, r2.getY());
maxX = jmax (maxX, r2.getRight());
maxY = jmax (maxY, r2.getBottom());
}
return RectangleType (minX, minY, maxX - minX, maxY - minY);
}
/** Optimises the list into a minimum number of constituent rectangles.
This will try to combine any adjacent rectangles into larger ones where
possible, to simplify lists that might have been fragmented by repeated
add/subtract calls.
*/
void consolidate()
{
for (int i = 0; i < getNumRectangles() - 1; ++i)
{
RectangleType& r = rects.getReference (i);
const ValueType rx1 = r.getX();
const ValueType ry1 = r.getY();
const ValueType rx2 = rx1 + r.getWidth();
const ValueType ry2 = ry1 + r.getHeight();
for (int j = rects.size(); --j > i;)
{
RectangleType& r2 = rects.getReference (j);
const ValueType jrx1 = r2.getX();
const ValueType jry1 = r2.getY();
const ValueType jrx2 = jrx1 + r2.getWidth();
const ValueType jry2 = jry1 + r2.getHeight();
// if the vertical edges of any blocks are touching and their horizontals don't
// line up, split them horizontally..
if (jrx1 == rx2 || jrx2 == rx1)
{
if (jry1 > ry1 && jry1 < ry2)
{
r.setHeight (jry1 - ry1);
rects.add (RectangleType (rx1, jry1, rx2 - rx1, ry2 - jry1));
i = -1;
break;
}
if (jry2 > ry1 && jry2 < ry2)
{
r.setHeight (jry2 - ry1);
rects.add (RectangleType (rx1, jry2, rx2 - rx1, ry2 - jry2));
i = -1;
break;
}
else if (ry1 > jry1 && ry1 < jry2)
{
r2.setHeight (ry1 - jry1);
rects.add (RectangleType (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
i = -1;
break;
}
else if (ry2 > jry1 && ry2 < jry2)
{
r2.setHeight (ry2 - jry1);
rects.add (RectangleType (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
i = -1;
break;
}
}
}
}
for (int i = 0; i < rects.size() - 1; ++i)
{
RectangleType& r = rects.getReference (i);
for (int j = rects.size(); --j > i;)
{
if (r.enlargeIfAdjacent (rects.getReference (j)))
{
rects.remove (j);
i = -1;
break;
}
}
}
}
/** Adds an x and y value to all the coordinates. */
void offsetAll (Point<ValueType> offset) noexcept
{
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r += offset;
}
/** Adds an x and y value to all the coordinates. */
void offsetAll (ValueType dx, ValueType dy) noexcept
{
offsetAll (Point<ValueType> (dx, dy));
}
/** Scales all the coordinates. */
template <typename ScaleType>
void scaleAll (ScaleType scaleFactor) noexcept
{
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r *= scaleFactor;
}
/** Applies a transform to all the rectangles.
Obviously this will create a mess if the transform involves any
rotation or skewing.
*/
void transformAll (const AffineTransform& transform) noexcept
{
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r = r->transformedBy (transform);
}
//==============================================================================
/** Creates a Path object to represent this region. */
Path toPath() const
{
Path p;
for (int i = 0; i < rects.size(); ++i)
p.addRectangle (rects.getReference (i));
return p;
}
//==============================================================================
/** Standard method for iterating the rectangles in the list. */
const RectangleType* begin() const noexcept { return rects.begin(); }
/** Standard method for iterating the rectangles in the list. */
const RectangleType* end() const noexcept { return rects.end(); }
/** Increases the internal storage to hold a minimum number of rectangles.
Calling this before adding a large number of rectangles means that
the array won't have to keep dynamically resizing itself as the elements
are added, and it'll therefore be more efficient.
@see Array::ensureStorageAllocated
*/
void ensureStorageAllocated (int minNumRectangles)
{
rects.ensureStorageAllocated (minNumRectangles);
}
private:
//==============================================================================
Array<RectangleType> rects;
};
#endif // JUCE_RECTANGLELIST_H_INCLUDED
|