Debian Codegraph @

src/cohen_sutherland.rs

raw symbols SCIP
1 //! Implements the Cohen-Sutherland line clipping algorithm.
2 //!
3 //! Returns the clipped line if the original line intersects the clipping window, or `None` if the
4 //! original line is completely outside the clipping window.
5 //!
6 //! Reference: [Cohen-Sutherland algorithm](https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm)
7 //!
8 //! The Cohen-Sutherland algorithm is a line clipping algorithm that divides the 2D plane into 9
9 //! regions and then determines the region in which the line lies. If the line lies completely
10 //! outside the clipping window, it is rejected. If the line lies completely inside the clipping
11 //! window, it is accepted. If the line lies partially inside the clipping window, it is clipped.
12 //!
13 //! The regions are defined as follows:
14 //!
15 //! ```plain
16 //! 1001 | 1000 | 1010
17 //! -----|------|-----
18 //! 0001 | 0000 | 0010
19 //! -----|------|-----
20 //! 0101 | 0100 | 0110
21 //! ```
22 //!
23 //! The algorithm works as follows:
24 //!
25 //! 1. Determine the region in which the line's starting point lies.
26 //! 2. Determine the region in which the line's ending point lies.
27 //! 3. If both points lie in region 0000, the line is completely inside the clipping window and
28 //! should be accepted.
29 //! 4. If both points lie in the same region that is not 0000, the line is completely outside the
30 //! clipping window and should be rejected.
31 //! 5. If the points lie in different regions, the line is partially inside the clipping window and
32 //! should be clipped.
33 //! 6. Clip the line using the Cohen-Sutherland algorithm.
34 //! 7. Repeat the process for the clipped line.
35 //!
36 //! The Cohen-Sutherland algorithm is commonly used in computer graphics to clip lines against a
37 //! rectangular window.
38 //!
39 //! # Examples
40 //!
41 //! ```
42 //! use line_clipping::cohen_sutherland::clip_line;
43 //! use line_clipping::{LineSegment, Point, Window};
44 //!
45 //! let line = LineSegment::new(Point::new(-10.0, -10.0), Point::new(20.0, 20.0));
46 //! let window = Window::new(0.0, 10.0, 0.0, 10.0);
47 //! let clipped_line = clip_line(line, window);
48 //! ```
49 use bitflags::bitflags;
50
51 use crate::{LineSegment, Point, Window};
52
53 /// Clips a line segment against a rectangular window using the Cohen-Sutherland algorithm.
54 ///
55 /// See the [module-level documentation](crate::cohen_sutherland) for more details on the algorithm.
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// use line_clipping::cohen_sutherland::clip_line;
61 /// use line_clipping::{LineSegment, Point, Window};
62 ///
63 /// let line = LineSegment::new(Point::new(-10.0, -10.0), Point::new(20.0, 20.0));
64 /// let window = Window::new(0.0, 10.0, 0.0, 10.0);
65 /// let clipped_line = clip_line(line, window);
66 /// ```
67 #[must_use]
68 pub fn clip_line(mut line: LineSegment, window: Window) -> Option<LineSegment> {
69 let mut region_1 = Region::from_point(line.p1, window);
70 let mut region_2 = Region::from_point(line.p2, window);
71
72 while region_1.is_outside() || region_2.is_outside() {
73 if region_1.intersects(region_2) {
74 // The line is completely outside the clipping window.
75 return None;
76 }
77 if region_1.is_outside() {
78 line.p1 = calculate_intersection(line.p1, line.p2, region_1, window);
79 region_1 = Region::from_point(line.p1, window);
80 } else {
81 line.p2 = calculate_intersection(line.p2, line.p1, region_2, window);
82 region_2 = Region::from_point(line.p2, window);
83 }
84 }
85
86 Some(line)
87 }
88
89 fn calculate_intersection(p1: Point, p2: Point, region: Region, window: Window) -> Point {
90 let dx = p2.x - p1.x;
91 let dy = p2.y - p1.y;
92 if region.contains(Region::LEFT) {
93 let y = p1.y + (window.x_min - p1.x) * dy / dx;
94 Point::new(window.x_min, y)
95 } else if region.contains(Region::RIGHT) {
96 let y = p1.y + (window.x_max - p1.x) * dy / dx;
97 Point::new(window.x_max, y)
98 } else if region.contains(Region::BOTTOM) {
99 let x = p1.x + (window.y_min - p1.y) * dx / dy;
100 Point::new(x, window.y_min)
101 } else if region.contains(Region::TOP) {
102 let x = p1.x + (window.y_max - p1.y) * dx / dy;
103 Point::new(x, window.y_max)
104 } else {
105 p1
106 }
107 }
108
109 bitflags! {
110 /// Represents the regions in the Cohen-Sutherland algorithm.
111 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
112 struct Region: u8 {
113 const LEFT = 0b0001;
114 const RIGHT = 0b0010;
115 const BOTTOM = 0b0100;
116 const TOP = 0b1000;
117 }
118 }
119
120 impl Region {
121 const fn is_outside(self) -> bool {
122 !self.is_empty()
123 }
124
125 /// Determines the region in which a point lies.
126 fn from_point(point: Point, window: Window) -> Self {
127 let mut region = Self::empty();
128 if point.x < window.x_min {
129 region |= Self::LEFT;
130 } else if point.x > window.x_max {
131 region |= Self::RIGHT;
132 }
133 if point.y < window.y_min {
134 region |= Self::BOTTOM;
135 } else if point.y > window.y_max {
136 region |= Self::TOP;
137 }
138 region
139 }
140 }
141
142 #[cfg(test)]
143 mod tests {
144 use rstest::rstest;
145
146 use super::*;
147
148 #[rstest]
149 #[case::left(Point::new(-2.0, 0.0), Region::LEFT)]
150 #[case::right(Point::new(2.0, 0.0), Region::RIGHT)]
151 #[case::top(Point::new(0.0, 2.0), Region::TOP)]
152 #[case::bottom(Point::new(0.0, -2.0), Region::BOTTOM)]
153 #[case::top_left(Point::new(-2.0, 2.0), Region::LEFT | Region::TOP)]
154 #[case::top_right(Point::new(2.0, 2.0), Region::RIGHT | Region::TOP)]
155 #[case::bottom_left(Point::new(-2.0, -2.0), Region::LEFT | Region::BOTTOM)]
156 #[case::bottom_right(Point::new(2.0, -2.0), Region::RIGHT | Region::BOTTOM)]
157 #[case::inside(Point::new(0.0, 0.0), Region::empty())]
158 #[case::inside_left(Point::new(-1.0, 0.0), Region::empty())]
159 #[case::inside_right(Point::new(1.0, 0.0), Region::empty())]
160 #[case::inside_top(Point::new(0.0, 1.0), Region::empty())]
161 #[case::inside_bottom(Point::new(0.0, -1.0), Region::empty())]
162 #[case::inside_top_left(Point::new(-1.0, 1.0), Region::empty())]
163 #[case::inside_top_right(Point::new(1.0, 1.0), Region::empty())]
164 #[case::inside_bottom_left(Point::new(-1.0, -1.0), Region::empty())]
165 #[case::inside_bottom_right(Point::new(1.0, -1.0), Region::empty())]
166 fn region_from_point(#[case] point: Point, #[case] expected: Region) {
167 let window = Window::new(-1.0, 1.0, -1.0, 1.0);
168 assert_eq!(Region::from_point(point, window), expected);
169 }
170
171 #[rstest]
172 #[case::top_left(Point::new(-2.0, 2.0), Point::new(-3.0, 3.0))]
173 #[case::top_right(Point::new(2.0, 2.0), Point::new(3.0, 3.0))]
174 #[case::bottom_left(Point::new(-2.0, -2.0), Point::new(-3.0, -3.0))]
175 #[case::bottom_right(Point::new(2.0, -2.0), Point::new(3.0, -3.0))]
176 #[case::left(Point::new(-2.0, 0.0), Point::new(-3.0, 0.0))]
177 #[case::right(Point::new(2.0, 0.0), Point::new(3.0, 0.0))]
178 #[case::top(Point::new(0.0, 2.0), Point::new(0.0, 2.0))]
179 #[case::bottom(Point::new(0.0, -2.0), Point::new(0.0, -3.0))]
180 fn outside(#[case] p1: Point, #[case] p2: Point) {
181 let line = LineSegment::new(p1, p2);
182 let window = Window::new(-1.0, 1.0, -1.0, 1.0);
183 assert_eq!(clip_line(line, window), None);
184 }
185
186 #[rstest]
187 #[case::left_border(Point::new(-1.0, -1.0), Point::new(-1.0, 1.0))]
188 #[case::right_border(Point::new(1.0, -1.0), Point::new(1.0, 1.0))]
189 #[case::top_border(Point::new(-1.0, 1.0), Point::new(1.0, 1.0))]
190 #[case::bottom_border(Point::new(-1.0, -1.0), Point::new(1.0, -1.0))]
191 #[case::corners_up(Point::new(-1.0, -1.0), Point::new(1.0, 1.0))]
192 #[case::corners_down(Point::new(-1.0, 1.0), Point::new(1.0, -1.0))]
193 #[case::horizontal(Point::new(-0.5, 0.0), Point::new(0.5, 0.0))]
194 #[case::vertical(Point::new(0.0, -0.5), Point::new(0.0, 0.5))]
195 #[case::diagonal_up(Point::new(-0.5, -0.5), Point::new(0.5, 0.5))]
196 #[case::diagonal_down(Point::new(-0.5, 0.5), Point::new(0.5, -0.5))]
197 fn inside(#[case] p1: Point, #[case] p2: Point) {
198 let line = LineSegment::new(p1, p2);
199 let window = Window::new(-1.0, 1.0, -1.0, 1.0);
200 assert_eq!(clip_line(line, window), Some(line));
201 }
202
203 /// Test cases for lines that point to the origin and intersect the window. The cases move
204 /// clockwise around the window. This makes sure that we test the intersection of the line with
205 /// the window from all regions.
206 ///
207 /// ```
208 /// 1 2 3 4 5 6 7 8 1
209 /// 8 2
210 /// 7 ┌───────┐ 3
211 /// 6 │ │ 4
212 /// 5 │ . │ 5
213 /// 4 │ │ 6
214 /// 3 └───────┘ 7
215 /// 2 8
216 /// 1 8 7 6 5 4 3 2 1
217 /// ```
218 #[rstest]
219 #[case::top_1(Point::new(-2.0, 2.0), Point::new(-1.0, 1.0))]
220 #[case::top_2(Point::new(-1.5, 2.0), Point::new(-0.75, 1.0))]
221 #[case::top_3(Point::new(-1.0, 2.0), Point::new(-0.5, 1.0))]
222 #[case::top_4(Point::new(-0.5, 2.0), Point::new(-0.25, 1.0))]
223 #[case::top_5(Point::new(0.0, 2.0), Point::new(0.0, 1.0))]
224 #[case::top_6(Point::new(0.5, 2.0), Point::new(0.25, 1.0))]
225 #[case::top_7(Point::new(1.0, 2.0), Point::new(0.5, 1.0))]
226 #[case::top_8(Point::new(1.5, 2.0), Point::new(0.75, 1.0))]
227 #[case::right_1(Point::new(2.0, 2.0), Point::new(1.0, 1.0))]
228 #[case::right_2(Point::new(2.0, 1.5), Point::new(1.0, 0.75))]
229 #[case::right_3(Point::new(2.0, 1.0), Point::new(1.0, 0.5))]
230 #[case::right_4(Point::new(2.0, 0.5), Point::new(1.0, 0.25))]
231 #[case::right_5(Point::new(2.0, 0.0), Point::new(1.0, 0.0))]
232 #[case::right_6(Point::new(2.0, -0.5), Point::new(1.0, -0.25))]
233 #[case::right_7(Point::new(2.0, -1.0), Point::new(1.0, -0.5))]
234 #[case::right_8(Point::new(2.0, -1.5), Point::new(1.0, -0.75))]
235 #[case::bottom_1(Point::new(2.0, -2.0), Point::new(1.0, -1.0))]
236 #[case::bottom_2(Point::new(1.5, -2.0), Point::new(0.75, -1.0))]
237 #[case::bottom_3(Point::new(1.0, -2.0), Point::new(0.5, -1.0))]
238 #[case::bottom_4(Point::new(0.5, -2.0), Point::new(0.25, -1.0))]
239 #[case::bottom_5(Point::new(0.0, -2.0), Point::new(0.0, -1.0))]
240 #[case::bottom_6(Point::new(-0.5, -2.0), Point::new(-0.25, -1.0))]
241 #[case::bottom_7(Point::new(-1.0, -2.0), Point::new(-0.5, -1.0))]
242 #[case::bottom_8(Point::new(-1.5, -2.0), Point::new(-0.75, -1.0))]
243 #[case::left_1(Point::new(-2.0, -2.0), Point::new(-1.0, -1.0))]
244 #[case::left_2(Point::new(-2.0, -1.5), Point::new(-1.0, -0.75))]
245 #[case::left_3(Point::new(-2.0, -1.0), Point::new(-1.0, -0.5))]
246 #[case::left_4(Point::new(-2.0, -0.5), Point::new(-1.0, -0.25))]
247 #[case::left_5(Point::new(-2.0, 0.0), Point::new(-1.0, 0.0))]
248 #[case::left_6(Point::new(-2.0, 0.5), Point::new(-1.0, 0.25))]
249 #[case::left_7(Point::new(-2.0, 1.0), Point::new(-1.0, 0.5))]
250 #[case::left_8(Point::new(-2.0, 1.5), Point::new(-1.0, 0.75))]
251 fn one_intersection(#[case] p1: Point, #[case] expected: Point) {
252 let line = LineSegment::new(p1, Point::ORIGIN);
253 let window = Window::new(-1.0, 1.0, -1.0, 1.0);
254 let expected = LineSegment::new(expected, Point::ORIGIN);
255 assert_eq!(clip_line(line, window), Some(expected));
256 }
257
258 const A: Point = Point::new(-2.0, 2.0);
259 const B: Point = Point::new(0.0, 2.0);
260 const C: Point = Point::new(2.0, 2.0);
261 const D: Point = Point::new(2.0, 0.0);
262 const E: Point = Point::new(2.0, -2.0);
263 const F: Point = Point::new(0.0, -2.0);
264 const G: Point = Point::new(-2.0, -2.0);
265 const H: Point = Point::new(-2.0, 0.0);
266
267 /// Test cases for lines that intersect the window twice. The cases move clockwise around the
268 /// window. This makes sure that we test every region to each other region.
269 /// ```
270 /// A B C
271 ///
272 /// ┌───────┐
273 /// │ │
274 /// H │ . │ D
275 /// │ │
276 /// └───────┘
277 ///
278 /// G F E
279 /// ```
280 #[rstest]
281 #[case::a_to_d(A, D, Point::new(0.0, 1.0), Point::new(1.0, 0.5))]
282 #[case::a_to_e(A, E, Point::new(-1.0, 1.0), Point::new(1.0, -1.0))]
283 #[case::a_to_k(A, F, Point::new(-1.0, 0.0), Point::new(-0.5, -1.0))]
284 #[case::b_to_d(B, D, Point::new(1.0, 1.0), Point::new(1.0, 1.0))]
285 #[case::b_to_e(B, E, Point::new(0.5, 1.0), Point::new(1.0, 0.0))]
286 #[case::b_to_f(B, F, Point::new(0.0, 1.0), Point::new(0.0, -1.0))]
287 #[case::b_to_g(B, G, Point::new(-0.5, 1.0), Point::new(-1.0, -0.0))]
288 #[case::b_to_h(B, H, Point::new(-1.0, 1.0), Point::new(-1.0, 1.0))]
289 #[case::c_to_f(C, F, Point::new(1.0, 0.0), Point::new(0.5, -1.0))]
290 #[case::c_to_g(C, G, Point::new(1.0, 1.0), Point::new(-1.0, -1.0))]
291 #[case::c_to_h(C, H, Point::new(0.0, 1.0), Point::new(-1.0, 0.5))]
292 #[case::d_to_f(D, F, Point::new(1.0, -1.0), Point::new(1.0, -1.0))]
293 #[case::d_to_g(D, G, Point::new(1.0, -0.5), Point::new(0.0, -1.0))]
294 #[case::d_to_h(D, H, Point::new(1.0, 0.0), Point::new(-1.0, 0.0))]
295 #[case::e_to_h(E, H, Point::new(0.0, -1.0), Point::new(-1.0, -0.5))]
296 #[case::f_to_h(F, H, Point::new(-1.0, -1.0), Point::new(-1.0, -1.0))]
297 fn two_intersections(
298 #[case] p1: Point,
299 #[case] p2: Point,
300 #[case] expected_p1: Point,
301 #[case] expected_p2: Point,
302 ) {
303 let line = LineSegment::new(p1, p2);
304 let window = Window::new(-1.0, 1.0, -1.0, 1.0);
305 let expected = LineSegment::new(expected_p1, expected_p2);
306 assert_eq!(clip_line(line, window).unwrap(), expected);
307 }
308 }