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.

652 lines
20KB

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