druid_shell/region.rs
1// Copyright 2020 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::kurbo::{BezPath, Point, Rect, Shape, Vec2};
16
17/// A union of rectangles, useful for describing an area that needs to be repainted.
18#[derive(Clone, Debug)]
19pub struct Region {
20 rects: Vec<Rect>,
21}
22
23impl Region {
24 /// The empty region.
25 pub const EMPTY: Region = Region { rects: Vec::new() };
26
27 /// Returns the collection of rectangles making up this region.
28 #[inline]
29 pub fn rects(&self) -> &[Rect] {
30 &self.rects
31 }
32
33 /// Adds a rectangle to this region.
34 pub fn add_rect(&mut self, rect: Rect) {
35 if rect.area() > 0.0 {
36 self.rects.push(rect);
37 }
38 }
39
40 /// Replaces this region with a single rectangle.
41 pub fn set_rect(&mut self, rect: Rect) {
42 self.clear();
43 self.add_rect(rect);
44 }
45
46 /// Sets this region to the empty region.
47 pub fn clear(&mut self) {
48 self.rects.clear();
49 }
50
51 /// Returns a rectangle containing this region.
52 pub fn bounding_box(&self) -> Rect {
53 if self.rects.is_empty() {
54 Rect::ZERO
55 } else {
56 self.rects[1..]
57 .iter()
58 .fold(self.rects[0], |r, s| r.union(*s))
59 }
60 }
61
62 #[doc(hidden)]
63 #[deprecated(since = "0.7.0", note = "Use bounding_box() instead")]
64 // this existed on the previous Region type, and I've bumped into it
65 // a couple times while updating
66 pub fn to_rect(&self) -> Rect {
67 self.bounding_box()
68 }
69
70 /// Returns `true` if this region has a non-empty intersection with the given rectangle.
71 pub fn intersects(&self, rect: Rect) -> bool {
72 self.rects.iter().any(|r| r.intersect(rect).area() > 0.0)
73 }
74
75 /// Returns `true` if the given `point` is contained within any rectangle in the region.
76 pub fn contains(&self, point: Point) -> bool {
77 self.rects.iter().any(|r| r.contains(point))
78 }
79
80 /// Returns `true` if this region is empty.
81 pub fn is_empty(&self) -> bool {
82 // Note that we only ever add non-empty rects to self.rects.
83 self.rects.is_empty()
84 }
85
86 /// Converts into a Bezier path. Note that this just gives the concatenation of the rectangle
87 /// paths, which is not the smartest possible thing. Also, it's not the right answer for an
88 /// even/odd fill rule.
89 pub fn to_bez_path(&self) -> BezPath {
90 let mut ret = BezPath::new();
91 for rect in self.rects() {
92 // Rect ignores the tolerance.
93 ret.extend(rect.path_elements(0.0));
94 }
95 ret
96 }
97
98 /// Modifies this region by including everything in the other region.
99 pub fn union_with(&mut self, other: &Region) {
100 self.rects.extend_from_slice(&other.rects);
101 }
102
103 /// Modifies this region by intersecting it with the given rectangle.
104 pub fn intersect_with(&mut self, rect: Rect) {
105 // TODO: this would be a good use of the nightly drain_filter function, if it stabilizes
106 for r in &mut self.rects {
107 *r = r.intersect(rect);
108 }
109 self.rects.retain(|r| r.area() > 0.0)
110 }
111}
112
113impl std::ops::AddAssign<Vec2> for Region {
114 fn add_assign(&mut self, rhs: Vec2) {
115 for r in &mut self.rects {
116 *r = *r + rhs;
117 }
118 }
119}
120
121impl std::ops::SubAssign<Vec2> for Region {
122 fn sub_assign(&mut self, rhs: Vec2) {
123 for r in &mut self.rects {
124 *r = *r - rhs;
125 }
126 }
127}
128
129impl From<Rect> for Region {
130 fn from(rect: Rect) -> Region {
131 Region { rects: vec![rect] }
132 }
133}