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 21KB

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