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.

661 lines
22KB

  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.size() == 0; }
  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 (rects.size() == 0)
  90. {
  91. rects.add (rect);
  92. }
  93. else
  94. {
  95. bool anyOverlaps = false;
  96. for (int j = rects.size(); --j >= 0;)
  97. {
  98. RectangleType& 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 && rects.size() > 0)
  108. {
  109. RectangleList r (rect);
  110. for (int i = rects.size(); --i >= 0;)
  111. {
  112. const RectangleType& ourRect = rects.getReference (i);
  113. if (rect.intersects (ourRect))
  114. {
  115. r.subtract (ourRect);
  116. if (r.rects.size() == 0)
  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 (const 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 (const RectangleType* r = other.begin(), * const e = other.end(); r != e; ++r)
  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 (const RectangleType& rect)
  163. {
  164. const int originalNumRects = rects.size();
  165. if (originalNumRects > 0)
  166. {
  167. const ValueType x1 = rect.getX();
  168. const ValueType y1 = rect.getY();
  169. const ValueType x2 = x1 + rect.getWidth();
  170. const ValueType y2 = y1 + rect.getHeight();
  171. for (int i = getNumRectangles(); --i >= 0;)
  172. {
  173. RectangleType& r = rects.getReference (i);
  174. const ValueType rx1 = r.getX();
  175. const ValueType ry1 = r.getY();
  176. const ValueType rx2 = rx1 + r.getWidth();
  177. const ValueType 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 (int i = otherList.rects.size(); --i >= 0 && rects.size() > 0;)
  244. subtract (otherList.rects.getReference (i));
  245. return rects.size() > 0;
  246. }
  247. /** Removes any areas of the region that lie outside a given rectangle.
  248. Any rectangles in the list which overlap this will be clipped and subdivided
  249. if necessary.
  250. Returns true if the resulting region is not empty, false if it is empty.
  251. @see getIntersectionWith
  252. */
  253. bool clipTo (const RectangleType& rect)
  254. {
  255. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  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. auto& rect = rects.getReference (j);
  289. for (auto& r : other)
  290. {
  291. auto 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. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  308. destRegion.clear();
  309. if (! rect.isEmpty())
  310. {
  311. for (int i = rects.size(); --i >= 0;)
  312. {
  313. auto r = rects.getReference (i);
  314. if (rect.intersectRectangle (r))
  315. destRegion.rects.add (r);
  316. }
  317. }
  318. return destRegion.rects.size() > 0;
  319. }
  320. /** Swaps the contents of this and another list.
  321. This swaps their internal pointers, so is hugely faster than using copy-by-value
  322. to swap them.
  323. */
  324. void swapWith (RectangleList& otherList) noexcept
  325. {
  326. rects.swapWith (otherList.rects);
  327. }
  328. //==============================================================================
  329. /** Checks whether the region contains a given point.
  330. @returns true if the point lies within one of the rectangles in the list
  331. */
  332. bool containsPoint (Point<ValueType> point) const noexcept
  333. {
  334. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  335. if (r->contains (point))
  336. return true;
  337. return false;
  338. }
  339. /** Checks whether the region contains a given point.
  340. @returns true if the point lies within one of the rectangles in the list
  341. */
  342. bool containsPoint (ValueType x, ValueType y) const noexcept
  343. {
  344. return containsPoint (Point<ValueType> (x, y));
  345. }
  346. /** Checks whether the region contains the whole of a given rectangle.
  347. @returns true all parts of the rectangle passed in lie within the region
  348. defined by this object
  349. @see intersectsRectangle, containsPoint
  350. */
  351. bool containsRectangle (const RectangleType& rectangleToCheck) const
  352. {
  353. if (rects.size() > 1)
  354. {
  355. RectangleList r (rectangleToCheck);
  356. for (int i = rects.size(); --i >= 0;)
  357. {
  358. r.subtract (rects.getReference (i));
  359. if (r.rects.size() == 0)
  360. return true;
  361. }
  362. }
  363. else if (rects.size() > 0)
  364. {
  365. return rects.getReference (0).contains (rectangleToCheck);
  366. }
  367. return false;
  368. }
  369. /** Checks whether the region contains any part of a given rectangle.
  370. @returns true if any part of the rectangle passed in lies within the region
  371. defined by this object
  372. @see containsRectangle
  373. */
  374. bool intersectsRectangle (const RectangleType& rectangleToCheck) const noexcept
  375. {
  376. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  377. if (r->intersects (rectangleToCheck))
  378. return true;
  379. return false;
  380. }
  381. /** Checks whether this region intersects any part of another one.
  382. @see intersectsRectangle
  383. */
  384. bool intersects (const RectangleList& other) const noexcept
  385. {
  386. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  387. if (other.intersectsRectangle (*r))
  388. return true;
  389. return false;
  390. }
  391. //==============================================================================
  392. /** Returns the smallest rectangle that can enclose the whole of this region. */
  393. RectangleType getBounds() const noexcept
  394. {
  395. if (rects.size() <= 1)
  396. {
  397. if (rects.size() == 0)
  398. return RectangleType();
  399. return rects.getReference (0);
  400. }
  401. const RectangleType& r = rects.getReference (0);
  402. ValueType minX = r.getX();
  403. ValueType minY = r.getY();
  404. ValueType maxX = minX + r.getWidth();
  405. ValueType maxY = minY + r.getHeight();
  406. for (int i = rects.size(); --i > 0;)
  407. {
  408. const RectangleType& r2 = rects.getReference (i);
  409. minX = jmin (minX, r2.getX());
  410. minY = jmin (minY, r2.getY());
  411. maxX = jmax (maxX, r2.getRight());
  412. maxY = jmax (maxY, r2.getBottom());
  413. }
  414. return RectangleType (minX, minY, maxX - minX, maxY - minY);
  415. }
  416. /** Optimises the list into a minimum number of constituent rectangles.
  417. This will try to combine any adjacent rectangles into larger ones where
  418. possible, to simplify lists that might have been fragmented by repeated
  419. add/subtract calls.
  420. */
  421. void consolidate()
  422. {
  423. for (int i = 0; i < rects.size() - 1; ++i)
  424. {
  425. RectangleType& r = rects.getReference (i);
  426. const ValueType rx1 = r.getX();
  427. const ValueType ry1 = r.getY();
  428. const ValueType rx2 = rx1 + r.getWidth();
  429. const ValueType ry2 = ry1 + r.getHeight();
  430. for (int j = rects.size(); --j > i;)
  431. {
  432. RectangleType& r2 = rects.getReference (j);
  433. const ValueType jrx1 = r2.getX();
  434. const ValueType jry1 = r2.getY();
  435. const ValueType jrx2 = jrx1 + r2.getWidth();
  436. const ValueType jry2 = jry1 + r2.getHeight();
  437. // if the vertical edges of any blocks are touching and their horizontals don't
  438. // line up, split them horizontally..
  439. if (jrx1 == rx2 || jrx2 == rx1)
  440. {
  441. if (jry1 > ry1 && jry1 < ry2)
  442. {
  443. r.setHeight (jry1 - ry1);
  444. rects.add (RectangleType (rx1, jry1, rx2 - rx1, ry2 - jry1));
  445. i = -1;
  446. break;
  447. }
  448. if (jry2 > ry1 && jry2 < ry2)
  449. {
  450. r.setHeight (jry2 - ry1);
  451. rects.add (RectangleType (rx1, jry2, rx2 - rx1, ry2 - jry2));
  452. i = -1;
  453. break;
  454. }
  455. else if (ry1 > jry1 && ry1 < jry2)
  456. {
  457. r2.setHeight (ry1 - jry1);
  458. rects.add (RectangleType (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
  459. i = -1;
  460. break;
  461. }
  462. else if (ry2 > jry1 && ry2 < jry2)
  463. {
  464. r2.setHeight (ry2 - jry1);
  465. rects.add (RectangleType (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
  466. i = -1;
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. for (int i = 0; i < rects.size() - 1; ++i)
  473. {
  474. RectangleType& r = rects.getReference (i);
  475. for (int j = rects.size(); --j > i;)
  476. {
  477. if (r.enlargeIfAdjacent (rects.getReference (j)))
  478. {
  479. rects.remove (j);
  480. i = -1;
  481. break;
  482. }
  483. }
  484. }
  485. }
  486. /** Adds an x and y value to all the coordinates. */
  487. void offsetAll (Point<ValueType> offset) noexcept
  488. {
  489. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  490. *r += offset;
  491. }
  492. /** Adds an x and y value to all the coordinates. */
  493. void offsetAll (ValueType dx, ValueType dy) noexcept
  494. {
  495. offsetAll (Point<ValueType> (dx, dy));
  496. }
  497. /** Scales all the coordinates. */
  498. template <typename ScaleType>
  499. void scaleAll (ScaleType scaleFactor) noexcept
  500. {
  501. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  502. *r *= scaleFactor;
  503. }
  504. /** Applies a transform to all the rectangles.
  505. Obviously this will create a mess if the transform involves any
  506. rotation or skewing.
  507. */
  508. void transformAll (const AffineTransform& transform) noexcept
  509. {
  510. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  511. *r = r->transformedBy (transform);
  512. }
  513. //==============================================================================
  514. /** Creates a Path object to represent this region. */
  515. Path toPath() const
  516. {
  517. Path p;
  518. for (int i = 0; i < rects.size(); ++i)
  519. p.addRectangle (rects.getReference (i));
  520. return p;
  521. }
  522. //==============================================================================
  523. /** Standard method for iterating the rectangles in the list. */
  524. const RectangleType* begin() const noexcept { return rects.begin(); }
  525. /** Standard method for iterating the rectangles in the list. */
  526. const RectangleType* end() const noexcept { return rects.end(); }
  527. /** Increases the internal storage to hold a minimum number of rectangles.
  528. Calling this before adding a large number of rectangles means that
  529. the array won't have to keep dynamically resizing itself as the elements
  530. are added, and it'll therefore be more efficient.
  531. @see Array::ensureStorageAllocated
  532. */
  533. void ensureStorageAllocated (int minNumRectangles)
  534. {
  535. rects.ensureStorageAllocated (minNumRectangles);
  536. }
  537. private:
  538. //==============================================================================
  539. Array<RectangleType> rects;
  540. };