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.

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