Audio plugin host https://kx.studio/carla
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.

663 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. #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 index is out-of-range.
  68. */
  69. RectangleType getRectangle (int index) const noexcept { return rects[index]; }
  70. //==============================================================================
  71. /** Removes all rectangles to leave an empty region. */
  72. void clear()
  73. {
  74. rects.clearQuick();
  75. }
  76. /** Merges a new rectangle into the list.
  77. The rectangle being added will first be clipped to remove any parts of it
  78. that overlap existing rectangles in the list, and adjacent rectangles will be
  79. merged into it.
  80. The rectangle can have any size and may be empty, but if it's floating point
  81. then it's expected to not contain any INF values.
  82. */
  83. void add (const RectangleType& rect)
  84. {
  85. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  86. if (! rect.isEmpty())
  87. {
  88. if (rects.size() == 0)
  89. {
  90. rects.add (rect);
  91. }
  92. else
  93. {
  94. bool anyOverlaps = false;
  95. for (int j = rects.size(); --j >= 0;)
  96. {
  97. RectangleType& ourRect = rects.getReference (j);
  98. if (rect.intersects (ourRect))
  99. {
  100. if (rect.contains (ourRect))
  101. rects.remove (j);
  102. else if (! ourRect.reduceIfPartlyContainedIn (rect))
  103. anyOverlaps = true;
  104. }
  105. }
  106. if (anyOverlaps && rects.size() > 0)
  107. {
  108. RectangleList r (rect);
  109. for (int i = rects.size(); --i >= 0;)
  110. {
  111. const RectangleType& ourRect = rects.getReference (i);
  112. if (rect.intersects (ourRect))
  113. {
  114. r.subtract (ourRect);
  115. if (r.rects.size() == 0)
  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 (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. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  255. bool notEmpty = false;
  256. if (rect.isEmpty())
  257. {
  258. clear();
  259. }
  260. else
  261. {
  262. for (int i = rects.size(); --i >= 0;)
  263. {
  264. RectangleType& r = rects.getReference (i);
  265. if (! rect.intersectRectangle (r))
  266. rects.remove (i);
  267. else
  268. notEmpty = true;
  269. }
  270. }
  271. return notEmpty;
  272. }
  273. /** Removes any areas of the region that lie outside a given rectangle list.
  274. Any rectangles in this object which overlap the specified list will be clipped
  275. and subdivided if necessary.
  276. Returns true if the resulting region is not empty, false if it is empty.
  277. @see getIntersectionWith
  278. */
  279. template <typename OtherValueType>
  280. bool clipTo (const RectangleList<OtherValueType>& other)
  281. {
  282. if (rects.size() == 0)
  283. return false;
  284. RectangleList result;
  285. for (int j = 0; j < rects.size(); ++j)
  286. {
  287. const RectangleType& rect = rects.getReference (j);
  288. for (const Rectangle<OtherValueType>* r = other.begin(), * const e = other.end(); r != e; ++r)
  289. {
  290. RectangleType clipped (r->template toType<ValueType>());
  291. if (rect.intersectRectangle (clipped))
  292. result.rects.add (clipped);
  293. }
  294. }
  295. swapWith (result);
  296. return ! isEmpty();
  297. }
  298. /** Creates a region which is the result of clipping this one to a given rectangle.
  299. Unlike the other clipTo method, this one doesn't affect this object - it puts the
  300. resulting region into the list whose reference is passed-in.
  301. Returns true if the resulting region is not empty, false if it is empty.
  302. @see clipTo
  303. */
  304. bool getIntersectionWith (const RectangleType& rect, RectangleList& destRegion) const
  305. {
  306. jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
  307. destRegion.clear();
  308. if (! rect.isEmpty())
  309. {
  310. for (int i = rects.size(); --i >= 0;)
  311. {
  312. RectangleType r (rects.getReference (i));
  313. if (rect.intersectRectangle (r))
  314. destRegion.rects.add (r);
  315. }
  316. }
  317. return destRegion.rects.size() > 0;
  318. }
  319. /** Swaps the contents of this and another list.
  320. This swaps their internal pointers, so is hugely faster than using copy-by-value
  321. to swap them.
  322. */
  323. void swapWith (RectangleList& otherList) noexcept
  324. {
  325. rects.swapWith (otherList.rects);
  326. }
  327. //==============================================================================
  328. /** Checks whether the region contains a given point.
  329. @returns true if the point lies within one of the rectangles in the list
  330. */
  331. bool containsPoint (Point<ValueType> point) const noexcept
  332. {
  333. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  334. if (r->contains (point))
  335. return true;
  336. return false;
  337. }
  338. /** Checks whether the region contains a given point.
  339. @returns true if the point lies within one of the rectangles in the list
  340. */
  341. bool containsPoint (ValueType x, ValueType y) const noexcept
  342. {
  343. return containsPoint (Point<ValueType> (x, y));
  344. }
  345. /** Checks whether the region contains the whole of a given rectangle.
  346. @returns true all parts of the rectangle passed in lie within the region
  347. defined by this object
  348. @see intersectsRectangle, containsPoint
  349. */
  350. bool containsRectangle (const RectangleType& rectangleToCheck) const
  351. {
  352. if (rects.size() > 1)
  353. {
  354. RectangleList r (rectangleToCheck);
  355. for (int i = rects.size(); --i >= 0;)
  356. {
  357. r.subtract (rects.getReference (i));
  358. if (r.rects.size() == 0)
  359. return true;
  360. }
  361. }
  362. else if (rects.size() > 0)
  363. {
  364. return rects.getReference (0).contains (rectangleToCheck);
  365. }
  366. return false;
  367. }
  368. /** Checks whether the region contains any part of a given rectangle.
  369. @returns true if any part of the rectangle passed in lies within the region
  370. defined by this object
  371. @see containsRectangle
  372. */
  373. bool intersectsRectangle (const RectangleType& rectangleToCheck) const noexcept
  374. {
  375. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  376. if (r->intersects (rectangleToCheck))
  377. return true;
  378. return false;
  379. }
  380. /** Checks whether this region intersects any part of another one.
  381. @see intersectsRectangle
  382. */
  383. bool intersects (const RectangleList& other) const noexcept
  384. {
  385. for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  386. if (other.intersectsRectangle (*r))
  387. return true;
  388. return false;
  389. }
  390. //==============================================================================
  391. /** Returns the smallest rectangle that can enclose the whole of this region. */
  392. RectangleType getBounds() const noexcept
  393. {
  394. if (rects.size() <= 1)
  395. {
  396. if (rects.size() == 0)
  397. return RectangleType();
  398. return rects.getReference (0);
  399. }
  400. const RectangleType& r = rects.getReference (0);
  401. ValueType minX = r.getX();
  402. ValueType minY = r.getY();
  403. ValueType maxX = minX + r.getWidth();
  404. ValueType maxY = minY + r.getHeight();
  405. for (int i = rects.size(); --i > 0;)
  406. {
  407. const RectangleType& r2 = rects.getReference (i);
  408. minX = jmin (minX, r2.getX());
  409. minY = jmin (minY, r2.getY());
  410. maxX = jmax (maxX, r2.getRight());
  411. maxY = jmax (maxY, r2.getBottom());
  412. }
  413. return RectangleType (minX, minY, maxX - minX, maxY - minY);
  414. }
  415. /** Optimises the list into a minimum number of constituent rectangles.
  416. This will try to combine any adjacent rectangles into larger ones where
  417. possible, to simplify lists that might have been fragmented by repeated
  418. add/subtract calls.
  419. */
  420. void consolidate()
  421. {
  422. for (int i = 0; i < getNumRectangles() - 1; ++i)
  423. {
  424. RectangleType& r = rects.getReference (i);
  425. const ValueType rx1 = r.getX();
  426. const ValueType ry1 = r.getY();
  427. const ValueType rx2 = rx1 + r.getWidth();
  428. const ValueType ry2 = ry1 + r.getHeight();
  429. for (int j = rects.size(); --j > i;)
  430. {
  431. RectangleType& r2 = rects.getReference (j);
  432. const ValueType jrx1 = r2.getX();
  433. const ValueType jry1 = r2.getY();
  434. const ValueType jrx2 = jrx1 + r2.getWidth();
  435. const ValueType jry2 = jry1 + r2.getHeight();
  436. // if the vertical edges of any blocks are touching and their horizontals don't
  437. // line up, split them horizontally..
  438. if (jrx1 == rx2 || jrx2 == rx1)
  439. {
  440. if (jry1 > ry1 && jry1 < ry2)
  441. {
  442. r.setHeight (jry1 - ry1);
  443. rects.add (RectangleType (rx1, jry1, rx2 - rx1, ry2 - jry1));
  444. i = -1;
  445. break;
  446. }
  447. if (jry2 > ry1 && jry2 < ry2)
  448. {
  449. r.setHeight (jry2 - ry1);
  450. rects.add (RectangleType (rx1, jry2, rx2 - rx1, ry2 - jry2));
  451. i = -1;
  452. break;
  453. }
  454. else if (ry1 > jry1 && ry1 < jry2)
  455. {
  456. r2.setHeight (ry1 - jry1);
  457. rects.add (RectangleType (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
  458. i = -1;
  459. break;
  460. }
  461. else if (ry2 > jry1 && ry2 < jry2)
  462. {
  463. r2.setHeight (ry2 - jry1);
  464. rects.add (RectangleType (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
  465. i = -1;
  466. break;
  467. }
  468. }
  469. }
  470. }
  471. for (int i = 0; i < rects.size() - 1; ++i)
  472. {
  473. RectangleType& r = rects.getReference (i);
  474. for (int j = rects.size(); --j > i;)
  475. {
  476. if (r.enlargeIfAdjacent (rects.getReference (j)))
  477. {
  478. rects.remove (j);
  479. i = -1;
  480. break;
  481. }
  482. }
  483. }
  484. }
  485. /** Adds an x and y value to all the coordinates. */
  486. void offsetAll (Point<ValueType> offset) noexcept
  487. {
  488. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  489. *r += offset;
  490. }
  491. /** Adds an x and y value to all the coordinates. */
  492. void offsetAll (ValueType dx, ValueType dy) noexcept
  493. {
  494. offsetAll (Point<ValueType> (dx, dy));
  495. }
  496. /** Scales all the coordinates. */
  497. template <typename ScaleType>
  498. void scaleAll (ScaleType scaleFactor) noexcept
  499. {
  500. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  501. *r *= scaleFactor;
  502. }
  503. /** Applies a transform to all the rectangles.
  504. Obviously this will create a mess if the transform involves any
  505. rotation or skewing.
  506. */
  507. void transformAll (const AffineTransform& transform) noexcept
  508. {
  509. for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  510. *r = r->transformedBy (transform);
  511. }
  512. //==============================================================================
  513. /** Creates a Path object to represent this region. */
  514. Path toPath() const
  515. {
  516. Path p;
  517. for (int i = 0; i < rects.size(); ++i)
  518. p.addRectangle (rects.getReference (i));
  519. return p;
  520. }
  521. //==============================================================================
  522. /** Standard method for iterating the rectangles in the list. */
  523. const RectangleType* begin() const noexcept { return rects.begin(); }
  524. /** Standard method for iterating the rectangles in the list. */
  525. const RectangleType* end() const noexcept { return rects.end(); }
  526. /** Increases the internal storage to hold a minimum number of rectangles.
  527. Calling this before adding a large number of rectangles means that
  528. the array won't have to keep dynamically resizing itself as the elements
  529. are added, and it'll therefore be more efficient.
  530. @see Array::ensureStorageAllocated
  531. */
  532. void ensureStorageAllocated (int minNumRectangles)
  533. {
  534. rects.ensureStorageAllocated (minNumRectangles);
  535. }
  536. private:
  537. //==============================================================================
  538. Array<RectangleType> rects;
  539. };
  540. #endif // JUCE_RECTANGLELIST_H_INCLUDED