Audio plugin host https://kx.studio/carla
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.

juce_RectangleList.h 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 (RectangleType rect)
  163. {
  164. if (auto numRects = rects.size())
  165. {
  166. auto x1 = rect.getX();
  167. auto y1 = rect.getY();
  168. auto x2 = x1 + rect.getWidth();
  169. auto y2 = y1 + rect.getHeight();
  170. for (int i = numRects; --i >= 0;)
  171. {
  172. auto& r = rects.getReference (i);
  173. auto rx1 = r.getX();
  174. auto ry1 = r.getY();
  175. auto rx2 = rx1 + r.getWidth();
  176. auto ry2 = ry1 + r.getHeight();
  177. if (! (x2 <= rx1 || x1 >= rx2 || y2 <= ry1 || y1 >= ry2))
  178. {
  179. if (x1 > rx1 && x1 < rx2)
  180. {
  181. if (y1 <= ry1 && y2 >= ry2 && x2 >= rx2)
  182. {
  183. r.setWidth (x1 - rx1);
  184. }
  185. else
  186. {
  187. r.setX (x1);
  188. r.setWidth (rx2 - x1);
  189. rects.insert (++i, RectangleType (rx1, ry1, x1 - rx1, ry2 - ry1));
  190. ++i;
  191. }
  192. }
  193. else if (x2 > rx1 && x2 < rx2)
  194. {
  195. r.setX (x2);
  196. r.setWidth (rx2 - x2);
  197. if (y1 > ry1 || y2 < ry2 || x1 > rx1)
  198. {
  199. rects.insert (++i, RectangleType (rx1, ry1, x2 - rx1, ry2 - ry1));
  200. ++i;
  201. }
  202. }
  203. else if (y1 > ry1 && y1 < ry2)
  204. {
  205. if (x1 <= rx1 && x2 >= rx2 && y2 >= ry2)
  206. {
  207. r.setHeight (y1 - ry1);
  208. }
  209. else
  210. {
  211. r.setY (y1);
  212. r.setHeight (ry2 - y1);
  213. rects.insert (++i, RectangleType (rx1, ry1, rx2 - rx1, y1 - ry1));
  214. ++i;
  215. }
  216. }
  217. else if (y2 > ry1 && y2 < ry2)
  218. {
  219. r.setY (y2);
  220. r.setHeight (ry2 - y2);
  221. if (x1 > rx1 || x2 < rx2 || y1 > ry1)
  222. {
  223. rects.insert (++i, RectangleType (rx1, ry1, rx2 - rx1, y2 - ry1));
  224. ++i;
  225. }
  226. }
  227. else
  228. {
  229. rects.remove (i);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. /** Removes all areas in another RectangleList from this one.
  236. Any rectangles in the list which overlap this will be clipped and subdivided
  237. if necessary.
  238. @returns true if the resulting list is non-empty.
  239. */
  240. bool subtract (const RectangleList& otherList)
  241. {
  242. for (auto& r : otherList)
  243. {
  244. if (isEmpty())
  245. return false;
  246. subtract (r);
  247. }
  248. return ! isEmpty();
  249. }
  250. /** Removes any areas of the region that lie outside a given rectangle.
  251. Any rectangles in the list which overlap this will be clipped and subdivided
  252. if necessary.
  253. Returns true if the resulting region is not empty, false if it is empty.
  254. @see getIntersectionWith
  255. */
  256. bool clipTo (RectangleType rect)
  257. {
  258. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  259. bool notEmpty = false;
  260. if (rect.isEmpty())
  261. {
  262. clear();
  263. }
  264. else
  265. {
  266. for (int i = rects.size(); --i >= 0;)
  267. {
  268. auto& r = rects.getReference (i);
  269. if (! rect.intersectRectangle (r))
  270. rects.remove (i);
  271. else
  272. notEmpty = true;
  273. }
  274. }
  275. return notEmpty;
  276. }
  277. /** Removes any areas of the region that lie outside a given rectangle list.
  278. Any rectangles in this object which overlap the specified list will be clipped
  279. and subdivided if necessary.
  280. Returns true if the resulting region is not empty, false if it is empty.
  281. @see getIntersectionWith
  282. */
  283. template <typename OtherValueType>
  284. bool clipTo (const RectangleList<OtherValueType>& other)
  285. {
  286. if (isEmpty())
  287. return false;
  288. RectangleList result;
  289. for (auto& rect : rects)
  290. {
  291. for (auto& r : other)
  292. {
  293. auto clipped = r.template toType<ValueType>();
  294. if (rect.intersectRectangle (clipped))
  295. result.rects.add (clipped);
  296. }
  297. }
  298. swapWith (result);
  299. return ! isEmpty();
  300. }
  301. /** Creates a region which is the result of clipping this one to a given rectangle.
  302. Unlike the other clipTo method, this one doesn't affect this object - it puts the
  303. resulting region into the list whose reference is passed-in.
  304. Returns true if the resulting region is not empty, false if it is empty.
  305. @see clipTo
  306. */
  307. bool getIntersectionWith (RectangleType rect, RectangleList& destRegion) const
  308. {
  309. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  310. destRegion.clear();
  311. if (! rect.isEmpty())
  312. for (auto r : rects)
  313. if (rect.intersectRectangle (r))
  314. destRegion.rects.add (r);
  315. return ! destRegion.isEmpty();
  316. }
  317. /** Swaps the contents of this and another list.
  318. This swaps their internal pointers, so is hugely faster than using copy-by-value
  319. to swap them.
  320. */
  321. void swapWith (RectangleList& otherList) noexcept
  322. {
  323. rects.swapWith (otherList.rects);
  324. }
  325. //==============================================================================
  326. /** Checks whether the region contains a given point.
  327. @returns true if the point lies within one of the rectangles in the list
  328. */
  329. bool containsPoint (Point<ValueType> point) const noexcept
  330. {
  331. for (auto& r : rects)
  332. if (r.contains (point))
  333. return true;
  334. return false;
  335. }
  336. /** Checks whether the region contains a given point.
  337. @returns true if the point lies within one of the rectangles in the list
  338. */
  339. bool containsPoint (ValueType x, ValueType y) const noexcept
  340. {
  341. return containsPoint (Point<ValueType> (x, y));
  342. }
  343. /** Checks whether the region contains the whole of a given rectangle.
  344. @returns true all parts of the rectangle passed in lie within the region
  345. defined by this object
  346. @see intersectsRectangle, containsPoint
  347. */
  348. bool containsRectangle (RectangleType rectangleToCheck) const
  349. {
  350. if (rects.size() > 1)
  351. {
  352. RectangleList r (rectangleToCheck);
  353. for (auto& rect : rects)
  354. {
  355. r.subtract (rect);
  356. if (r.isEmpty())
  357. return true;
  358. }
  359. }
  360. else if (! isEmpty())
  361. {
  362. return rects.getReference (0).contains (rectangleToCheck);
  363. }
  364. return false;
  365. }
  366. /** Checks whether the region contains any part of a given rectangle.
  367. @returns true if any part of the rectangle passed in lies within the region
  368. defined by this object
  369. @see containsRectangle
  370. */
  371. bool intersectsRectangle (RectangleType rectangleToCheck) const noexcept
  372. {
  373. for (auto& r : rects)
  374. if (r.intersects (rectangleToCheck))
  375. return true;
  376. return false;
  377. }
  378. /** Checks whether this region intersects any part of another one.
  379. @see intersectsRectangle
  380. */
  381. bool intersects (const RectangleList& other) const noexcept
  382. {
  383. for (auto& r : rects)
  384. if (other.intersectsRectangle (r))
  385. return true;
  386. return false;
  387. }
  388. //==============================================================================
  389. /** Returns the smallest rectangle that can enclose the whole of this region. */
  390. RectangleType getBounds() const noexcept
  391. {
  392. if (isEmpty())
  393. return {};
  394. auto& r = rects.getReference (0);
  395. if (rects.size() == 1)
  396. return r;
  397. auto minX = r.getX();
  398. auto minY = r.getY();
  399. auto maxX = minX + r.getWidth();
  400. auto maxY = minY + r.getHeight();
  401. for (int i = rects.size(); --i > 0;)
  402. {
  403. auto& r2 = rects.getReference (i);
  404. minX = jmin (minX, r2.getX());
  405. minY = jmin (minY, r2.getY());
  406. maxX = jmax (maxX, r2.getRight());
  407. maxY = jmax (maxY, r2.getBottom());
  408. }
  409. return { minX, minY, maxX - minX, maxY - minY };
  410. }
  411. /** Optimises the list into a minimum number of constituent rectangles.
  412. This will try to combine any adjacent rectangles into larger ones where
  413. possible, to simplify lists that might have been fragmented by repeated
  414. add/subtract calls.
  415. */
  416. void consolidate()
  417. {
  418. for (int i = 0; i < rects.size() - 1; ++i)
  419. {
  420. auto& r = rects.getReference (i);
  421. auto rx1 = r.getX();
  422. auto ry1 = r.getY();
  423. auto rx2 = rx1 + r.getWidth();
  424. auto ry2 = ry1 + r.getHeight();
  425. for (int j = rects.size(); --j > i;)
  426. {
  427. auto& r2 = rects.getReference (j);
  428. auto jrx1 = r2.getX();
  429. auto jry1 = r2.getY();
  430. auto jrx2 = jrx1 + r2.getWidth();
  431. auto jry2 = jry1 + r2.getHeight();
  432. // if the vertical edges of any blocks are touching and their horizontals don't
  433. // line up, split them horizontally..
  434. if (jrx1 == rx2 || jrx2 == rx1)
  435. {
  436. if (jry1 > ry1 && jry1 < ry2)
  437. {
  438. r.setHeight (jry1 - ry1);
  439. rects.add (RectangleType (rx1, jry1, rx2 - rx1, ry2 - jry1));
  440. i = -1;
  441. break;
  442. }
  443. if (jry2 > ry1 && jry2 < ry2)
  444. {
  445. r.setHeight (jry2 - ry1);
  446. rects.add (RectangleType (rx1, jry2, rx2 - rx1, ry2 - jry2));
  447. i = -1;
  448. break;
  449. }
  450. else if (ry1 > jry1 && ry1 < jry2)
  451. {
  452. r2.setHeight (ry1 - jry1);
  453. rects.add (RectangleType (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
  454. i = -1;
  455. break;
  456. }
  457. else if (ry2 > jry1 && ry2 < jry2)
  458. {
  459. r2.setHeight (ry2 - jry1);
  460. rects.add (RectangleType (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
  461. i = -1;
  462. break;
  463. }
  464. }
  465. }
  466. }
  467. for (int i = 0; i < rects.size() - 1; ++i)
  468. {
  469. auto& r = rects.getReference (i);
  470. for (int j = rects.size(); --j > i;)
  471. {
  472. if (r.enlargeIfAdjacent (rects.getReference (j)))
  473. {
  474. rects.remove (j);
  475. i = -1;
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. /** Adds an x and y value to all the coordinates. */
  482. void offsetAll (Point<ValueType> offset) noexcept
  483. {
  484. for (auto& r : rects)
  485. r += offset;
  486. }
  487. /** Adds an x and y value to all the coordinates. */
  488. void offsetAll (ValueType dx, ValueType dy) noexcept
  489. {
  490. offsetAll (Point<ValueType> (dx, dy));
  491. }
  492. /** Scales all the coordinates. */
  493. template <typename ScaleType>
  494. void scaleAll (ScaleType scaleFactor) noexcept
  495. {
  496. for (auto& r : rects)
  497. r *= scaleFactor;
  498. }
  499. /** Applies a transform to all the rectangles.
  500. Obviously this will create a mess if the transform involves any
  501. rotation or skewing.
  502. */
  503. void transformAll (const AffineTransform& transform) noexcept
  504. {
  505. for (auto& r : rects)
  506. r = r.transformedBy (transform);
  507. }
  508. //==============================================================================
  509. /** Creates a Path object to represent this region. */
  510. Path toPath() const
  511. {
  512. Path p;
  513. for (auto& r : rects)
  514. p.addRectangle (r);
  515. return p;
  516. }
  517. //==============================================================================
  518. /** Standard method for iterating the rectangles in the list. */
  519. const RectangleType* begin() const noexcept { return rects.begin(); }
  520. /** Standard method for iterating the rectangles in the list. */
  521. const RectangleType* end() const noexcept { return rects.end(); }
  522. /** Increases the internal storage to hold a minimum number of rectangles.
  523. Calling this before adding a large number of rectangles means that
  524. the array won't have to keep dynamically resizing itself as the elements
  525. are added, and it'll therefore be more efficient.
  526. @see Array::ensureStorageAllocated
  527. */
  528. void ensureStorageAllocated (int minNumRectangles)
  529. {
  530. rects.ensureStorageAllocated (minNumRectangles);
  531. }
  532. private:
  533. //==============================================================================
  534. Array<RectangleType> rects;
  535. };
  536. } // namespace juce