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.

643 lines
21KB

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