The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

686 lines
22KB

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