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.

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