Family of intersection problems Family of intersection problems CG Lecture 2 CG Lecture 2 1. Line segment intersection : given n line segments, Line segment intersection report their intersections efficiently. • Worst case: k = n ( n –1)/2 = O ( n 2 ) intersections. • Optimal algorithm: O ( n log n + k ) time and O ( n ) space. • Intersecting two line segments 2. Polygon intersection • Line sweep algorithm • Intersection of two simple polygons is not a simple polygon. • Convex polygon intersection • Boolean operations of union and difference very similar • Boolean operations on polygons • Optimal algorithm: O ( n log n + k ) time, O ( n+m ) for convex. 3. Subdivision overlay: given two planar subdivisions, • Subdivision overlay algorithm computer their overlay. Generalization of 2. 1 2 Family of intersection problems (2) Family of intersection problems (2) Intersection of two line segments Intersection of two line segments Theorem: Segments ( p 1 ,p 2 ) and ( p 3 ,p 4 ) 4. Line intersections and arrangements : given n lines p 2 intersect in their interior iff (half-spaces), compute their intersections and the p 4 regions they define efficiently. • p 1 and p 2 on different sides of line p 3 p 4 5. Face and polyhedra intersections • p 3 and p 4 on different sides of line p 1 p 2 p 1 p 3 • Intersection of two polygonal (triangular) faces in space • Intersection of two (convex) polyhedra. Both conditions can be tested by Applications: MANY! computing the orientations of four • Basic operations in graphics. triangles . Which ones? • Engineering and VLSI design Special cases: • Non-geometric domains: databases, parallelization. 3 4 Line segment intersections Line segment intersections Intersection point computation Intersection point computation Problem : Given n line-segments = + − ≤ ≤ p t ( ) p ( p p t ) 0 t 1 in the plane, compute all their 1 2 1 = + − ≤ ≤ q s ( ) q ( q q s ) 0 s 1 intersections. 1 2 1 p 2 Variant : Is there any pair of q 1 Question: What is the meaning of other intersecting segments? values of s and t ? Assume : general position q 2 p 1 • No line segment is vertical. Solve (2D) linear vector equation for t • No two segments are collinear. # of segments: n # of segments: n = and s : p t ( ) q s ( ) # of intersections: # of intersections: k k • No three segments intersect at a ∈ ∈ check that t [0,1] and s [0,1] 0 ≤ 0 ≤ k k ≤ ≤ n n ( ( n n – –1)/2 1)/2 common point . Naive algorithm : Check each pair of segments for intersection. Time complexity: Θ ( n 2 ). 5 6 1
Line segment intersections Sweep line: idea Line segment intersections Sweep line: idea Segment adjacencies with Goal : (s 1 ,s 2 ,s 3 ) respect to a horizontal (vertical) Output-sensitive algorithm s 2 (s 1 ,s 3 ,s 2 ) s 1 s 3 line change locally: • O ( n log n + k log n ) time (s 1 ,s 3 ) s 6 • s i and s j must be adjacent for • O( n ) space the intersection to occur. • not optimal, but good start s 5 s 4 • ( s i ,s j ) before intersection. Idea: If x and y projections If x and y projections • ( s j ,s i ) after intersection. • Segments that are close don don’ ’t intersect, t intersect, together are candidates for no intersection. no intersection. intersection. Keep track of segment adjacencies as vertical line • Keep track of “closedness” moves from left to right. changes. Stop only at event points! 7 8 Adjacency theorem Adjacency theorem Sweep line algorithm Sweep line algorithm () Theorem: Just before an intersection • An event is any endpoint or ( s 2 ) s 2 occurs (infinitesimally-close to it), intersection point. ( s 2 , s 3 ) s 3 the two respective segments are • Sweep a horizontal line from top s 1 adjacent to each other in the s 6 to bottom. sweep-line status. Maintain two data structures : s 5 s 4 Proof : • Event priority queue Q : sorted by y coordinate. In practice: Look ahead: • Sweep line status L : stores whenever two line segments segments currently intersected by # of events # of events ≤ ≤ n n + + k k become adjacent along the sweep the sweep line, sorted by y # of segments in L L ≤ ≤ n n # of segments in line, check for their intersection coordinate. below the sweep line. 9 10 Balanced binary tree Sweep line algorithm Balanced binary tree Sweep line algorithm Initialization: • Balanced binary tree sorted by x coordinate • Put all segment endpoints in the event queue Q , • Insertion, deletion, and neighbor operations in O (log n ) sorted according to y coordinates. Time: O( n log n ). s 3 s 3 • Sweep line status is initially empty. s 4 s 1 s 2 s 5 s 1 s 4 The algorithm proceeds by inserting, deleting, and s 2 s 5 s 1 s 4 handling discrete events s 3 s 2 from the queue Q until it is empty . It also maintains the list L . 11 12 2
Event handling – – start of segment start of segment Event handling – – end of segment end of segment Event handling Event handling Event of type A : Beginning of Event of type B : End of segment segment (upper endpoint) (lower endpoint) s k s i s j s l • Locate segment position in the status. s k s i s j s l • Locate segment position in the status. • Insert segment into sweep line status. • Delete segment from sweep line status. • Test for intersection below the sweep • Test for intersection below the sweep line line with the segments immediately to between the segments immediately to the the left and right of it. Insert intersection left and right. Insert intersection point (if point(s) (if found, and if not already in found, and if not already in the queue) into the queue) into the event queue. the event queue. Time complexity: n events, O(log n ) Time complexity: n events, O(log n ) time for each → O( n log n ) total. time for each → O( n log n ) in total. 13 14 Event handling Event handling – – intersection intersection Example Example Event of type C : Intersection point a3 • Report the point. b4 s m s l s i s j s3 • Swap the two respective line segments in e1 s4 the sweep line status. b3 a2 • For the new left and right segments – test a4 s2 each for intersection against the segment b b0 immediately adjacent the status line (if s1 1 b2 exists). Insert intersection point (if found, s0 a1 a0 and if not already in the queue) into the event queue. k is the k is the Sweep Line Time complexity : k such events, O(log n ) output size output size Status each → O( k log n ) in total. 15 16 Event Queue Complexity analysis Complexity analysis Handling degeneracies degeneracies Handling • Basic data structures: • Horizontal segments with p • Event queue: heap s i q same y coordinate � • Sweep line status: balanced binary tree treat them in arbitrary order s j s k • Each heap/tree operation requires O (log n ) time. p s i (i.e., from left to right y (Why is O(log k) = O(log n) ?) coordinate) • Total time complexity: O (( n+k ) log n ). • Overlapping segments � • If k ≈ n 2 this is slightly worse than the naive algorithm. report them as special • When k=o ( n 2 /log n ) then the sweep algorithm is faster. • Multiple segment p • Total space complexity: O( n+k ). intersections at the same s i Question : How can this be improved to O( n )? point s j • Note: There exists a better algorithm whose running time is Θ ( n log n + k ) with O( n+k ) space complexity. 17 18 3
Line segment intersection algorithm Line segment intersection algorithm Handling degeneracies degeneracies Handling 19 20 Intersection of convex polygons Intersection of convex polygons Code for convex polygon intersection Code for convex polygon intersection Input : Two convex polygons 8 | A | = n and | B | = m. 9 4 Output : one convex polygon 11 10 | A ∩ B | ≤ n + m in optimal 5 3 O ( n+m ) time and space. 6 7 Observations: 2 • alternation of A and B boundary pieces. 1 • alternation at intersection points A = A = { { 1,2,3,4,5 1,2,3,4,5 } } • order of vertices is kept. B = { B = { 6,7,8,9 6,7,8,9 } } • at each intersection, choose I = { = { 10,11 10,11 } } I leftmost segment. A ∩ A ∩ B B = { = { 6,7 6,7, ,10 10, ,8,9 8,9, ,11 11 } } 21 22 Example of convex intersection trace Example of convex intersection trace Advance rules Advance rules a a b b advance advance a i a i b j b j advance advance a i b j a i b j Do not advance on the boundary of either A or B whose Do not advance on the boundary of either A or B whose current edge may contain a yet- current edge may contain a yet -to to- -be be- -found intersection found intersection 23 24 4
Recommend
More recommend