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.

647 lines
20KB

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