lecture: line segment intersection and subdivisionsand sis the line segment that ends at p: 1.search...
TRANSCRIPT
1/69
Geometric Algorithms
Lecture:
Line segment intersection for map overlay
Subdivisions
2/69Map layers
In a geographic information system(GIS) data is stored in separatelayers
A layer stores the geometricinformation about some theme, likeland cover, road network,municipality boundaries, red foxhabitat, . . .
3/69Map overlay
Map overlay is the combination oftwo (or more) map layers
It is needed to answer questionslike:
I What is the total length of roadsthrough forests?
I What is the total area of cornfields within 1km from a river?
4/69Map overlay
To solve map overlay questions, weneed (at the least) intersectionpoints from two sets of linesegments (possibly, boundaries ofregions)
5/69The (easy) problem
Let’s first look at the easiestversion of the problem:
Where did the caribous crossroads?
Given a set of of n linesegments in the plane, find allintersection points efficiently
6/69An easy, optimal algorithm?
Algorithm FINDINTERSECTIONS(S)Input. A set S of line segments in the plane.Output. The set of intersection points among the segments in S.1. for each pair of line segments ei,e j ∈ S2. do if ei and e j intersect3. then report their intersection point
Question: Why can we say that this algorithm is optimal?
7/69
8/69Output-sensitive algorithm
The asymptotic running time of analgorithm is always input-sensitive(depends on n)
We may also want the running timeto be output-sensitive: if the outputis large, it is fine to spend a lot oftime, but if the output is small, wewant a fast algorithm
9/69Intersection points in practice
Question: How many intersectionpoints do we typically expect in ourapplications?
If this number is k, and if k = O(n), itwould be nice if the algorithm runsin O(n logn) time
10/69First attempt
Observation: Two line segments canonly intersect if their y-spans havean overlap
So, how about only testing pairs ofline segments that intersect in they-projection?
1-D problem: Given a set of intervalson the real line, find all partlyoverlapping pairs
x
y
s1 s2s3 s4 s5 s6
(s1, s2), (s4, s6), (s5, s6)
11/69Second attempt
Refined observation: Two linesegments can only intersect if theiry-spans have an overlap, and theyare adjacent in the x-order at thaty-coordinate (they are horizontalneighbors)
12/69Plane sweep
The plane sweep technique: Imagine a horizontal linepassing over the plane from top to bottom, solving theproblem as it moves
I The sweep line stops and the algorithm computes atcertain positions⇒ events
I The algorithm stores the relevant situation at the currentposition of the sweep line⇒ status
I The algorithm knows everything it needs to know abovethe sweep line, and found all intersection points
13/69Sweep
computed
unexplored
14/69Sweep and status
computed
unexplored
status
15/69Status and events
The status of this particular plane sweep algorithm, at thecurrent position of the sweep line, is the set of line segmentsintersecting the sweep line, ordered from left to right
The events occur when the status changes, and when outputis generated
event≈ interesting y-coordinate
16/69
s1
s2s3
s4
s6
s5
s7
s8
add s1
17/69
s1
s2s3
s4
s6
s5
s7
s8
add s2 after s1
18/69
s1
s2s3
s4
s6
s5
s7
s8
add s3 between s1and s2
19/69
s1
s2s3
s4
s6
s5
s7
s8
add s4 before s1
20/69
s1
s2s3
s4
s6
s5
s7
s8
reportintersection(s1,s2); swap s1and s3
21/69
s1
s2s3
s4
s6
s5
s7
s8
remove s2
22/69
s1
s2s3
s4
s6
s5
s7
s8
remove s1
23/69
s1
s2s3
s4
s6
s5
s7
s8
add s5
24/69
s1
s2s3
s4
s6
s5
s7
s8
reportintersection(s3,s4); swap s3and s4
25/69
. . . and so on . . .
26/69The events
When do the events happen? When the sweep line is
I at an upper endpoint of a line segmentI at a lower endpoint of a line segmentI at an intersection point of a line segment
At each type, the status changes; at the third type output isfound too
27/69Assume no degenerate cases
We will at first exclude degenerate cases:
I No two endpoints have the samey-coordinate
I No more than two line segmentsintersect in a point
I . . .
Question: Are there more degeneratecases?
28/69Event list and status structure
The event list is an abstract data structure that stores allevents in the order in which they occur
The status structure is an abstract data structure thatmaintains the current status
Here: The status is the subset of currently intersected linesegments in the order of intersection by the sweep line
29/69Status structure
We use a balanced binary search tree with the line segmentsin the leaves as the status structure
s1 s2s3 s4 s5 s6
s7s8
s1
s2
s3
s4
s5
s6
s7
s1 s2 s3 s4 s5 s6 s7 s8
30/69Status structure
s1 s2s3 s4 s5 s6
s7s8
s1
s2
s3
s4
s5
s6
s7
s1 s2 s3 s4 s5 s6 s7 s8
s9
Upper endpoint: search, and insert
31/69Status structure
s1 s2s3 s4 s5 s6
s7s8
s1
s2
s3
s4
s5
s6
s7
s1 s2 s3 s4 s5 s6 s7 s8
s9
Upper endpoint: search, and insert
32/69Status structure
s1 s2s3 s4 s5 s6
s7s8
s1
s2
s3
s4
s5
s6
s7
s1 s2
s3
s4 s5 s6 s7 s8
s9
s9
s9
Upper endpoint: search, and insert
33/69Status structure
Sweep line reaches lower endpoint of a line segment: deletefrom the status structure
Sweep line reaches intersection point: swap two leaves in thestatus structure (and update information on the search paths)
34/69Finding events
Before the sweep algorithm starts, we know all upperendpoint events and all lower endpoint events
But: How do we know intersection point events???(those we were trying to find . . .)
Recall: Two line segments can only intersect if they arehorizontal neighbors
35/69Finding events
Lemma: Two line segments si and s j canonly intersect after (= below) they havebecome horizontal neighbors
Proof: Just imagine that the sweep line isever so slightly above the intersectionpoint of si and s j, but below any otherevent �
Also: some earlier (= higher) event madesi and s j horizontally adjacent!!!
sisj
sisj
36/69Event list
The event list must be a balanced binary search tree, becauseduring the sweep, we discover new events that will happenlater and we want to be able to test whether an event isalready in the list
We know upper endpoint events and lower endpoint eventsbeforehand; we find intersection point events when theinvolved line segments become horizontal neighbors
37/69Structure of sweep algorithm
Algorithm FINDINTERSECTIONS(S)Input. A set S of line segments in the plane.Output. The intersection points of the segments in S, with for
each intersection point the segments that contain it.1. Initialize an empty event queue Q. Next, insert the
segment endpoints into Q; when an upper endpoint isinserted, the corresponding segment should be storedwith it
2. Initialize an empty status structure T3. while Q is not empty4. do Determine next event point p in Q and delete it5. HANDLEEVENTPOINT(p)
38/69Event handling
If the event is an upper endpoint event,and s is the line segment that starts atp:
1. Search with p in T , and insert s
2. If s intersects its left neighbor inT , then determine the intersectionpoint and insert it Q
3. If s intersects its right neighbor inT , then determine the intersectionpoint and insert it Q
p
s
39/69Event handling
If the event is a lower endpoint event,and s is the line segment that ends atp:
1. Search with p in T , and delete s
2. Let sl and sr be the left and rightneighbors of s in T (beforedeletion). If they intersect belowthe sweep line, then insert theirintersection point as an event in Q
p
s
40/69Event handling
If the event is an intersection pointevent where s and s′ intersect at p:
1. . . .
2. . . .
3. . . .
4. . . .
p
s s′
41/69Event handling
If the event is an intersection pointevent where s and s′ intersect at p:
1. Exchange s and s′ in T
2. . . .
3. . . .
4. . . .
p
s s′
42/69Event handling
If the event is an intersection pointevent where s and s′ intersect at p:
1. Exchange s and s′ in T
2. If s′ and its new left neighbor inT intersect below the sweepline, then insert thisintersection point in Q
3. . . .
4. . . .
p
s s′
43/69Event handling
If the event is an intersection pointevent where s and s′ intersect at p:
1. Exchange s and s′ in T
2. If s′ and its new left neighbor inT intersect below the sweepline, then insert thisintersection point in Q
3. If s and its new right neighbor inT intersect below the sweepline, then insert thisintersection point in Q
4. . . .
p
s s′
44/69Event handling
If the event is an intersection pointevent where s and s′ intersect at p:
1. Exchange s and s′ in T
2. If s′ and its new left neighbor inT intersect below the sweepline, then insert thisintersection point in Q
3. If s and its new right neighbor inT intersect below the sweepline, then insert thisintersection point in Q
4. Report the intersection point
p
s s′
45/69Event handling
p
s
s′
Can it be that newhorizontal neighborsalready intersected abovethe sweep line?
Can it be that we insert anewly detected intersectionpoint event, but it alreadyoccurs in Q?
Insert events only once!
45/69Event handling
p
s
s′
Can it be that newhorizontal neighborsalready intersected abovethe sweep line?
Can it be that we insert anewly detected intersectionpoint event, but it alreadyoccurs in Q?
Insert events only once!
46/69Efficiency
How much time to handle an event?
At most one search in T and/or one insertion, deletion, orswap
At most twice finding a neighbor in T
At most one deletion from and two insertions in Q
Since T and Q are balanced binary search trees, handling anevent takes only O(logn) time
47/69Efficiency
How many events?
I 2n for the upper and lower endpointsI k for the intersection points, if there are k of them
In total: O(n+ k) events
48/69Efficiency
Initialization takes O(n logn) time (to put all upper and lowerendpoint events in Q)
Each of the O(n+ k) events takes O(logn) time
The algorithm takes O(n logn+ k logn) time
If k = O(n), then this is O(n logn)
Note that if k is really large, the brute force O(n2) timealgorithm is more efficient
49/69Efficiency
Question: How much storage does the algorithm take?
50/69Efficiency
Question: Given that the event list is a binary tree that maystore O(k) = O(n2) events, is the efficiency in jeopardy?
51/69Efficiency
Solution:Only store intersection points of currently adjacent segments.
52/69Degenerate cases
How do we deal with degenerate cases?
For two different events with the same y-coordinate, we treatthem from left to right⇒ the “upper” endpoint of ahorizontal line segment is its left endpoint
53/69Degenerate cases
How about multiply coinciding event points?
p
Let U(p) and L(p) be the line segments that have p as upperand lower endpoint, and C(p) the ones that contain p
Question: How do we handle this multi-event?
54/69Plane Sweep – Conclusion
For every sweep algorithm:
I Define the statusI Choose the status structure and the event listI Figure out how events must be handled (with sketches!)I To analyze, determine the number of events and how
much time they take
Then deal with degeneracies
55/69Map overlay
To solve map overlay questions, weneed (at the least) intersectionpoints from two sets of linesegments.
56/69Map overlay
To solve map overlayquestions, we also need to beable to represent subdivisions
clayclay sand
sandloess
rockrock
sand
sand
loess
clay
rock rock
clay
57/69Subdivisions
A planar subdivision is a structure induced by a set of linesegments in the plane that can only intersect at commonendpoints. It consists of vertices, edges, and faces
face
vertex
edge
58/69Subdivisions
Vertices are the endpoints of the linesegments
Edges are the interiors of the linesegments
Faces are the interiors of connectedtwo-dimensional regions that do notcontain any point of any line segment
Objects of the same dimensionality areadjacent or not; objects of differentdimensionality are incident or not adjacent
incident
59/69Subdivisions
Exactly one face is unbounded, theouter face
Every other face is bounded and hasan outer boundary consisting ofvertices and edges
Any face has zero or more innerboundaries
60/69Representing subdivisions
A subdivision representation has avertex-object class, an edge-objectclass, and a face-object class
It is a pointer structure whereobjects can reach incident (oradjacent) objects easily
61/69Representing subdivisions
Use the edge as the central object
For any edge, exactly two verticesare incident, up to two faces areincident, and zero or more otheredges are adjacent
f1f2
v1
v2
62/69Representing subdivisions
Use the edge as the central object,and give it a direction
Now we can speak of Origin,Destination, Left Face, and RightFace
fleft
vorigin
fright
vdestination
63/69Representing subdivisions
Four edges are ofspecial interest fleft
fright
for fright
for frightfor fleft
for fleft
next edgenext edge
previous edgeprevious edge
64/69Representing subdivisions
It would be nice if we could traversea boundary cycle by continuouslyfollowing the next edge for fleft orfright
. . . but, no consistent edgeorientation needs to exist
65/69Representing subdivisions
We apply a trick/hack/impossibility:split every edge length-wise(!) intotwo half-edges
Every half-edge:
I has exactly one half-edge as itsTwin
I is directed opposite to its TwinI is incident to only one face (left)
~e Twin(~e)
Next(~e)
Prev(~e)
66/69The doubly-connected edge list
The doubly-connected edgelist is a subdivisionrepresentation structure withan object for every vertex,every half-edge, and everyface
A vertex object stores:
I CoordinatesI IncidentEdge (some
half-edge leaving it)
A half-edge object stores:
I Origin (vertex)I Twin (half-edge)I IncidentFace (face)I Next (half-edge in cycle of
the incident face)I Prev (half-edge in cycle of
the incident face)
67/69The doubly-connected edge list
A face object stores:
I OuterComponent(half-edge of outer cycle)
I InnerComponents (list ofhalf-edges for the innercycles bounding the face)
f
68/69The doubly-connected edge list
A vertex object stores:
I CoordinatesI IncidentEdgeI Any attributes, mark bits
A face object stores:
I OuterComponent(half-edge of outer cycle)
I InnerComponents(half-edges for the innercycles)
I Any attributes, mark bits
A half-edge object stores:
I Origin (vertex)I Twin (half-edge)I IncidentFace (face)I Next (half-edge in cycle of
the incident face)I Prev (half-edge in cycle of
the incident face)I Any attributes, mark bits
69/69Map overlay problem
The map overlay problem for twosubdivisions S1 and S2 is to computea subdivision S that is the overlay ofS1 and S2
All edges of S are (parts of) edgesfrom S1 and S2. All vertices of S arealso in S1 or S2, or intersections ofedges from S1 and S2
The line-segment intersectionalgorithm can be extended to thisproblem using doubly-connectededge lists. [Chapter 2.3]