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.

659 lines
22KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. Maintains a set of rectangles as a complex region.
  21. This class allows a set of rectangles to be treated as a solid shape, and can
  22. add and remove rectangular sections of it, and simplify overlapping or
  23. adjacent rectangles.
  24. @see Rectangle
  25. */
  26. template <typename ValueType>
  27. class RectangleList
  28. {
  29. public:
  30. typedef Rectangle<ValueType> RectangleType;
  31. //==============================================================================
  32. /** Creates an empty RectangleList */
  33. RectangleList() noexcept {}
  34. /** Creates a copy of another list */
  35. RectangleList (const RectangleList& other) : rects (other.rects)
  36. {
  37. }
  38. /** Creates a list containing just one rectangle. */
  39. RectangleList (const RectangleType& rect)
  40. {
  41. addWithoutMerging (rect);
  42. }
  43. /** Copies this list from another one. */
  44. RectangleList& operator= (const RectangleList& other)
  45. {
  46. rects = other.rects;
  47. return *this;
  48. }
  49. /** Move constructor */
  50. RectangleList (RectangleList&& other) noexcept
  51. : rects (static_cast<Array<RectangleType>&&> (other.rects))
  52. {
  53. }
  54. /** Move assignment operator */
  55. RectangleList& operator= (RectangleList&& other) noexcept
  56. {
  57. rects = static_cast<Array<RectangleType>&&> (other.rects);
  58. return *this;
  59. }
  60. //==============================================================================
  61. /** Returns true if the region is empty. */
  62. bool isEmpty() const noexcept { return rects.size() == 0; }
  63. /** Returns the number of rectangles in the list. */
  64. int getNumRectangles() const noexcept { return rects.size(); }
  65. /** Returns one of the rectangles at a particular index.
  66. @returns the rectangle at the index, or an empty rectangle if the index is out-of-range.
  67. */
  68. RectangleType getRectangle (int index) const noexcept { return rects[index]; }
  69. //==============================================================================
  70. /** Removes all rectangles to leave an empty region. */
  71. void clear()
  72. {
  73. rects.clearQuick();
  74. }
  75. /** Merges a new rectangle into the list.
  76. The rectangle being added will first be clipped to remove any parts of it
  77. that overlap existing rectangles in the list, and adjacent rectangles will be
  78. merged into it.
  79. The rectangle can have any size and may be empty, but if it's floating point
  80. then it's expected to not contain any INF values.
  81. */
  82. void add (const RectangleType& rect)
  83. {
  84. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  85. if (! rect.isEmpty())
  86. {
  87. if (rects.size() == 0)
  88. {
  89. rects.add (rect);
  90. }
  91. else
  92. {
  93. bool anyOverlaps = false;
  94. for (int j = rects.size(); --j >= 0;)
  95. {
  96. RectangleType& ourRect = rects.getReference (j);
  97. if (rect.intersects (ourRect))
  98. {
  99. if (rect.contains (ourRect))
  100. rects.remove (j);
  101. else if (! ourRect.reduceIfPartlyContainedIn (rect))
  102. anyOverlaps = true;
  103. }
  104. }
  105. if (anyOverlaps && rects.size() > 0)
  106. {
  107. RectangleList r (rect);
  108. for (int i = rects.size(); --i >= 0;)
  109. {
  110. const RectangleType& ourRect = rects.getReference (i);
  111. if (rect.intersects (ourRect))
  112. {
  113. r.subtract (ourRect);
  114. if (r.rects.size() == 0)
  115. return;
  116. }
  117. }
  118. rects.addArray (r.rects);
  119. }
  120. else
  121. {
  122. rects.add (rect);
  123. }
  124. }
  125. }
  126. }
  127. /** Merges a new rectangle into the list.
  128. The rectangle being added will first be clipped to remove any parts of it
  129. that overlap existing rectangles in the list.
  130. */
  131. void add (ValueType x, ValueType y, ValueType width, ValueType height)
  132. {
  133. add (RectangleType (x, y, width, height));
  134. }
  135. /** Dumbly adds a rectangle to the list without checking for overlaps.
  136. This simply adds the rectangle to the end, it doesn't merge it or remove
  137. any overlapping bits.
  138. The rectangle can have any size and may be empty, but if it's floating point
  139. then it's expected to not contain any INF values.
  140. */
  141. void addWithoutMerging (const RectangleType& rect)
  142. {
  143. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  144. if (! rect.isEmpty())
  145. rects.add (rect);
  146. }
  147. /** Merges another rectangle list into this one.
  148. Any overlaps between the two lists will be clipped, so that the result is
  149. the union of both lists.
  150. */
  151. void add (const RectangleList& other)
  152. {
  153. for (const RectangleType* r = other.begin(), * const e = other.end(); r != e; ++r)
  154. add (*r);
  155. }
  156. /** Removes a rectangular region from the list.
  157. Any rectangles in the list which overlap this will be clipped and subdivided
  158. if necessary.
  159. */
  160. void subtract (const RectangleType& rect)
  161. {
  162. const int originalNumRects = rects.size();
  163. if (originalNumRects > 0)
  164. {
  165. const ValueType x1 = rect.getX();
  166. const ValueType y1 = rect.getY();
  167. const ValueType x2 = x1 + rect.getWidth();
  168. const ValueType y2 = y1 + rect.getHeight();
  169. for (int i = getNumRectangles(); --i >= 0;)
  170. {
  171. RectangleType& r = rects.getReference (i);
  172. const ValueType rx1 = r.getX();
  173. const ValueType ry1 = r.getY();
  174. const ValueType rx2 = rx1 + r.getWidth();
  175. const ValueType 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 (int i = otherList.rects.size(); --i >= 0 && rects.size() > 0;)
  242. subtract (otherList.rects.getReference (i));
  243. return rects.size() > 0;
  244. }
  245. /** Removes any areas of the region that lie outside a given rectangle.
  246. Any rectangles in the list which overlap this will be clipped and subdivided
  247. if necessary.
  248. Returns true if the resulting region is not empty, false if it is empty.
  249. @see getIntersectionWith
  250. */
  251. bool clipTo (const RectangleType& rect)
  252. {
  253. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  254. bool notEmpty = false;
  255. if (rect.isEmpty())
  256. {
  257. clear();
  258. }
  259. else
  260. {
  261. for (int i = rects.size(); --i >= 0;)
  262. {
  263. RectangleType& r = rects.getReference (i);
  264. if (! rect.intersectRectangle (r))
  265. rects.remove (i);
  266. else
  267. notEmpty = true;
  268. }
  269. }
  270. return notEmpty;
  271. }
  272. /** Removes any areas of the region that lie outside a given rectangle list.
  273. Any rectangles in this object which overlap the specified list will be clipped
  274. and subdivided if necessary.
  275. Returns true if the resulting region is not empty, false if it is empty.
  276. @see getIntersectionWith
  277. */
  278. template <typename OtherValueType>
  279. bool clipTo (const RectangleList<OtherValueType>& other)
  280. {
  281. if (rects.size() == 0)
  282. return false;
  283. RectangleList result;
  284. for (int j = 0; j < rects.size(); ++j)
  285. {
  286. auto& rect = rects.getReference (j);
  287. for (auto& r : other)
  288. {
  289. auto clipped = r.template toType<ValueType>();
  290. if (rect.intersectRectangle (clipped))
  291. result.rects.add (clipped);
  292. }
  293. }
  294. swapWith (result);
  295. return ! isEmpty();
  296. }
  297. /** Creates a region which is the result of clipping this one to a given rectangle.
  298. Unlike the other clipTo method, this one doesn't affect this object - it puts the
  299. resulting region into the list whose reference is passed-in.
  300. Returns true if the resulting region is not empty, false if it is empty.
  301. @see clipTo
  302. */
  303. bool getIntersectionWith (const RectangleType& rect, RectangleList& destRegion) const
  304. {
  305. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  306. destRegion.clear();
  307. if (! rect.isEmpty())
  308. {
  309. for (int i = rects.size(); --i >= 0;)
  310. {
  311. auto r = rects.getReference (i);
  312. if (rect.intersectRectangle (r))
  313. destRegion.rects.add (r);
  314. }
  315. }
  316. return destRegion.rects.size() > 0;
  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 (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  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 (const RectangleType& rectangleToCheck) const
  350. {
  351. if (rects.size() > 1)
  352. {
  353. RectangleList r (rectangleToCheck);
  354. for (int i = rects.size(); --i >= 0;)
  355. {
  356. r.subtract (rects.getReference (i));
  357. if (r.rects.size() == 0)
  358. return true;
  359. }
  360. }
  361. else if (rects.size() > 0)
  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 (const RectangleType& rectangleToCheck) const noexcept
  373. {
  374. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  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 (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  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 (rects.size() <= 1)
  394. {
  395. if (rects.size() == 0)
  396. return RectangleType();
  397. return rects.getReference (0);
  398. }
  399. const RectangleType& r = rects.getReference (0);
  400. ValueType minX = r.getX();
  401. ValueType minY = r.getY();
  402. ValueType maxX = minX + r.getWidth();
  403. ValueType maxY = minY + r.getHeight();
  404. for (int i = rects.size(); --i > 0;)
  405. {
  406. const RectangleType& r2 = rects.getReference (i);
  407. minX = jmin (minX, r2.getX());
  408. minY = jmin (minY, r2.getY());
  409. maxX = jmax (maxX, r2.getRight());
  410. maxY = jmax (maxY, r2.getBottom());
  411. }
  412. return RectangleType (minX, minY, maxX - minX, maxY - minY);
  413. }
  414. /** Optimises the list into a minimum number of constituent rectangles.
  415. This will try to combine any adjacent rectangles into larger ones where
  416. possible, to simplify lists that might have been fragmented by repeated
  417. add/subtract calls.
  418. */
  419. void consolidate()
  420. {
  421. for (int i = 0; i < rects.size() - 1; ++i)
  422. {
  423. RectangleType& r = rects.getReference (i);
  424. const ValueType rx1 = r.getX();
  425. const ValueType ry1 = r.getY();
  426. const ValueType rx2 = rx1 + r.getWidth();
  427. const ValueType ry2 = ry1 + r.getHeight();
  428. for (int j = rects.size(); --j > i;)
  429. {
  430. RectangleType& r2 = rects.getReference (j);
  431. const ValueType jrx1 = r2.getX();
  432. const ValueType jry1 = r2.getY();
  433. const ValueType jrx2 = jrx1 + r2.getWidth();
  434. const ValueType jry2 = jry1 + r2.getHeight();
  435. // if the vertical edges of any blocks are touching and their horizontals don't
  436. // line up, split them horizontally..
  437. if (jrx1 == rx2 || jrx2 == rx1)
  438. {
  439. if (jry1 > ry1 && jry1 < ry2)
  440. {
  441. r.setHeight (jry1 - ry1);
  442. rects.add (RectangleType (rx1, jry1, rx2 - rx1, ry2 - jry1));
  443. i = -1;
  444. break;
  445. }
  446. if (jry2 > ry1 && jry2 < ry2)
  447. {
  448. r.setHeight (jry2 - ry1);
  449. rects.add (RectangleType (rx1, jry2, rx2 - rx1, ry2 - jry2));
  450. i = -1;
  451. break;
  452. }
  453. else if (ry1 > jry1 && ry1 < jry2)
  454. {
  455. r2.setHeight (ry1 - jry1);
  456. rects.add (RectangleType (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
  457. i = -1;
  458. break;
  459. }
  460. else if (ry2 > jry1 && ry2 < jry2)
  461. {
  462. r2.setHeight (ry2 - jry1);
  463. rects.add (RectangleType (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
  464. i = -1;
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. for (int i = 0; i < rects.size() - 1; ++i)
  471. {
  472. RectangleType& r = rects.getReference (i);
  473. for (int j = rects.size(); --j > i;)
  474. {
  475. if (r.enlargeIfAdjacent (rects.getReference (j)))
  476. {
  477. rects.remove (j);
  478. i = -1;
  479. break;
  480. }
  481. }
  482. }
  483. }
  484. /** Adds an x and y value to all the coordinates. */
  485. void offsetAll (Point<ValueType> offset) noexcept
  486. {
  487. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  488. *r += offset;
  489. }
  490. /** Adds an x and y value to all the coordinates. */
  491. void offsetAll (ValueType dx, ValueType dy) noexcept
  492. {
  493. offsetAll (Point<ValueType> (dx, dy));
  494. }
  495. /** Scales all the coordinates. */
  496. template <typename ScaleType>
  497. void scaleAll (ScaleType scaleFactor) noexcept
  498. {
  499. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  500. *r *= scaleFactor;
  501. }
  502. /** Applies a transform to all the rectangles.
  503. Obviously this will create a mess if the transform involves any
  504. rotation or skewing.
  505. */
  506. void transformAll (const AffineTransform& transform) noexcept
  507. {
  508. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  509. *r = r->transformedBy (transform);
  510. }
  511. //==============================================================================
  512. /** Creates a Path object to represent this region. */
  513. Path toPath() const
  514. {
  515. Path p;
  516. for (int i = 0; i < rects.size(); ++i)
  517. p.addRectangle (rects.getReference (i));
  518. return p;
  519. }
  520. //==============================================================================
  521. /** Standard method for iterating the rectangles in the list. */
  522. const RectangleType* begin() const noexcept { return rects.begin(); }
  523. /** Standard method for iterating the rectangles in the list. */
  524. const RectangleType* end() const noexcept { return rects.end(); }
  525. /** Increases the internal storage to hold a minimum number of rectangles.
  526. Calling this before adding a large number of rectangles means that
  527. the array won't have to keep dynamically resizing itself as the elements
  528. are added, and it'll therefore be more efficient.
  529. @see Array::ensureStorageAllocated
  530. */
  531. void ensureStorageAllocated (int minNumRectangles)
  532. {
  533. rects.ensureStorageAllocated (minNumRectangles);
  534. }
  535. private:
  536. //==============================================================================
  537. Array<RectangleType> rects;
  538. };