Geometric Algorithms Lecture: Line segment intersection for map - - PowerPoint PPT Presentation

geometric algorithms
SMART_READER_LITE
LIVE PREVIEW

Geometric Algorithms Lecture: Line segment intersection for map - - PowerPoint PPT Presentation

1/69 Geometric Algorithms Lecture: Line segment intersection for map overlay Subdivisions Map layers 2/69 In a geographic information system (GIS) data is stored in separate layers A layer stores the geometric information about some


slide-1
SLIDE 1

1/69

Geometric Algorithms

Lecture: Line segment intersection for map overlay Subdivisions

slide-2
SLIDE 2

2/69

Map layers

In a geographic information system (GIS) data is stored in separate layers A layer stores the geometric information about some theme, like land cover, road network, municipality boundaries, red fox habitat, ...

slide-3
SLIDE 3

3/69

Map overlay

Map overlay is the combination of two (or more) map layers It is needed to answer questions like:

◮ What is the total length of roads

through forests?

◮ What is the total area of corn

fields within 1km from a river?

slide-4
SLIDE 4

4/69

Map overlay

To solve map overlay questions, we need (at the least) intersection points from two sets of line segments (possibly, boundaries of regions)

slide-5
SLIDE 5

5/69

The (easy) problem

Let’s first look at the easiest version of the problem: Where did the caribous cross roads? Given a set of of n line segments in the plane, find all intersection points efficiently

slide-6
SLIDE 6

6/69

An 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,ej ∈ S 2. do if ei and ej intersect 3. then report their intersection point Question: Why can we say that this algorithm is optimal?

slide-7
SLIDE 7

7/69

slide-8
SLIDE 8

8/69

Output-sensitive algorithm

The asymptotic running time of an algorithm is always input-sensitive (depends on n) We may also want the running time to be output-sensitive: if the output is large, it is fine to spend a lot of time, but if the output is small, we want a fast algorithm

slide-9
SLIDE 9

9/69

Intersection points in practice

Question: How many intersection points do we typically expect in our applications? If this number is k, and if k = O(n), it would be nice if the algorithm runs in O(nlogn) time

slide-10
SLIDE 10

10/69

First attempt

Observation: Two line segments can

  • nly intersect if their y-spans have

an overlap So, how about only testing pairs of line segments that intersect in the y-projection? 1-D problem: Given a set of intervals

  • n the real line, find all partly
  • verlapping pairs

x y s1 s2s3 s4 s5 s6 (s1, s2), (s4, s6), (s5, s6)

slide-11
SLIDE 11

11/69

Second attempt

Refined observation: Two line segments can only intersect if their y-spans have an overlap, and they are adjacent in the x-order at that y-coordinate (they are horizontal neighbors)

slide-12
SLIDE 12

12/69

Plane sweep

The plane sweep technique: Imagine a horizontal line passing over the plane from top to bottom, solving the problem as it moves

◮ The sweep line stops and the algorithm computes at

certain positions ⇒ events

◮ The algorithm stores the relevant situation at the current

position of the sweep line ⇒ status

◮ The algorithm knows everything it needs to know above

the sweep line, and found all intersection points

slide-13
SLIDE 13

13/69

Sweep

computed unexplored

slide-14
SLIDE 14

14/69

Sweep and status

computed unexplored status

slide-15
SLIDE 15

15/69

Status and events

The status of this particular plane sweep algorithm, at the current position of the sweep line, is the set of line segments intersecting the sweep line, ordered from left to right The events occur when the status changes, and when output is generated event ≈ interesting y-coordinate

slide-16
SLIDE 16

16/69

s1 s2 s3 s4 s6 s5 s7 s8

add s1

slide-17
SLIDE 17

17/69

s1 s2 s3 s4 s6 s5 s7 s8

add s2 after s1

slide-18
SLIDE 18

18/69

s1 s2 s3 s4 s6 s5 s7 s8

add s3 between s1 and s2

slide-19
SLIDE 19

19/69

s1 s2 s3 s4 s6 s5 s7 s8

add s4 before s1

slide-20
SLIDE 20

20/69

s1 s2 s3 s4 s6 s5 s7 s8

report intersection (s1,s2); swap s1 and s3

slide-21
SLIDE 21

21/69

s1 s2 s3 s4 s6 s5 s7 s8

remove s2

slide-22
SLIDE 22

22/69

s1 s2 s3 s4 s6 s5 s7 s8

remove s1

slide-23
SLIDE 23

23/69

s1 s2 s3 s4 s6 s5 s7 s8

add s5

slide-24
SLIDE 24

24/69

s1 s2 s3 s4 s6 s5 s7 s8

report intersection (s3,s4); swap s3 and s4

slide-25
SLIDE 25

25/69

... and so on ...

slide-26
SLIDE 26

26/69

The events

When do the events happen? When the sweep line is

◮ at an upper endpoint of a line segment ◮ at a lower endpoint of a line segment ◮ at an intersection point of a line segment

At each type, the status changes; at the third type output is found too

slide-27
SLIDE 27

27/69

Assume no degenerate cases

We will at first exclude degenerate cases:

◮ No two endpoints have the same

y-coordinate

◮ No more than two line segments

intersect in a point

◮ ...

Question: Are there more degenerate cases?

slide-28
SLIDE 28

28/69

Event list and status structure

The event list is an abstract data structure that stores all events in the order in which they occur The status structure is an abstract data structure that maintains the current status Here: The status is the subset of currently intersected line segments in the order of intersection by the sweep line

slide-29
SLIDE 29

29/69

Status structure

We use a balanced binary search tree with the line segments in the leaves as the status structure

s1 s2 s3 s4 s5 s6 s7 s8 s1 s2 s3 s4 s5 s6 s7 s1 s2 s3 s4 s5 s6 s7 s8

slide-30
SLIDE 30

30/69

Status structure

s1 s2 s3 s4 s5 s6 s7 s8 s1 s2 s3 s4 s5 s6 s7 s1 s2 s3 s4 s5 s6 s7 s8 s9

Upper endpoint: search, and insert

slide-31
SLIDE 31

31/69

Status structure

s1 s2 s3 s4 s5 s6 s7 s8 s1 s2 s3 s4 s5 s6 s7 s1 s2 s3 s4 s5 s6 s7 s8 s9

Upper endpoint: search, and insert

slide-32
SLIDE 32

32/69

Status structure

s1 s2 s3 s4 s5 s6 s7 s8 s1 s2 s3 s4 s5 s6 s7 s1 s2 s3 s4 s5 s6 s7 s8 s9 s9 s9

Upper endpoint: search, and insert

slide-33
SLIDE 33

33/69

Status structure

Sweep line reaches lower endpoint of a line segment: delete from the status structure Sweep line reaches intersection point: swap two leaves in the status structure (and update information on the search paths)

slide-34
SLIDE 34

34/69

Finding events

Before the sweep algorithm starts, we know all upper endpoint 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 are horizontal neighbors

slide-35
SLIDE 35

35/69

Finding events

Lemma: Two line segments si and sj can

  • nly intersect after (= below) they have

become horizontal neighbors Proof: Just imagine that the sweep line is ever so slightly above the intersection point of si and sj, but below any other event Also: some earlier (= higher) event made si and sj horizontally adjacent!!!

si sj si sj

slide-36
SLIDE 36

36/69

Event list

The event list must be a balanced binary search tree, because during the sweep, we discover new events that will happen later and we want to be able to test whether an event is already in the list We know upper endpoint events and lower endpoint events beforehand; we find intersection point events when the involved line segments become horizontal neighbors

slide-37
SLIDE 37

37/69

Structure 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 is inserted, the corresponding segment should be stored with it 2. Initialize an empty status structure T 3. while Q is not empty 4. do Determine next event point p in Q and delete it 5. HANDLEEVENTPOINT(p)

slide-38
SLIDE 38

38/69

Event handling

If the event is an upper endpoint event, and s is the line segment that starts at p:

  • 1. Search with p in T, and insert s
  • 2. If s intersects its left neighbor in

T, then determine the intersection point and insert it Q

  • 3. If s intersects its right neighbor in

T, then determine the intersection point and insert it Q

p s

slide-39
SLIDE 39

39/69

Event handling

If the event is a lower endpoint event, and s is the line segment that ends at p:

  • 1. Search with p in T, and delete s
  • 2. Let sl and sr be the left and right

neighbors of s in T (before deletion). If they intersect below the sweep line, then insert their intersection point as an event in Q

p s

slide-40
SLIDE 40

40/69

Event handling

If the event is an intersection point event where s and s′ intersect at p:

  • 1. ...
  • 2. ...
  • 3. ...
  • 4. ...

p s s′

slide-41
SLIDE 41

41/69

Event handling

If the event is an intersection point event where s and s′ intersect at p:

  • 1. Exchange s and s′ in T
  • 2. ...
  • 3. ...
  • 4. ...

p s s′

slide-42
SLIDE 42

42/69

Event handling

If the event is an intersection point event where s and s′ intersect at p:

  • 1. Exchange s and s′ in T
  • 2. If s′ and its new left neighbor in

T intersect below the sweep line, then insert this intersection point in Q

  • 3. ...
  • 4. ...

p s s′

slide-43
SLIDE 43

43/69

Event handling

If the event is an intersection point event where s and s′ intersect at p:

  • 1. Exchange s and s′ in T
  • 2. If s′ and its new left neighbor in

T intersect below the sweep line, then insert this intersection point in Q

  • 3. If s and its new right neighbor in

T intersect below the sweep line, then insert this intersection point in Q

  • 4. ...

p s s′

slide-44
SLIDE 44

44/69

Event handling

If the event is an intersection point event where s and s′ intersect at p:

  • 1. Exchange s and s′ in T
  • 2. If s′ and its new left neighbor in

T intersect below the sweep line, then insert this intersection point in Q

  • 3. If s and its new right neighbor in

T intersect below the sweep line, then insert this intersection point in Q

  • 4. Report the intersection point

p s s′

slide-45
SLIDE 45

45/69

Event handling

p s s′

Can it be that new horizontal neighbors already intersected above the sweep line? Can it be that we insert a newly detected intersection point event, but it already

  • ccurs in Q?
slide-46
SLIDE 46

45/69

Event handling

p s s′

Can it be that new horizontal neighbors already intersected above the sweep line? Can it be that we insert a newly detected intersection point event, but it already

  • ccurs in Q?

Insert events only once!

slide-47
SLIDE 47

46/69

Efficiency

How much time to handle an event? At most one search in T and/or one insertion, deletion, or swap 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 an event takes only O(logn) time

slide-48
SLIDE 48

47/69

Efficiency

How many events?

◮ 2n for the upper and lower endpoints ◮ k for the intersection points, if there are k of them

In total: O(n+k) events

slide-49
SLIDE 49

48/69

Efficiency

Initialization takes O(nlogn) time (to put all upper and lower endpoint events in Q) Each of the O(n+k) events takes O(logn) time The algorithm takes O(nlogn+klogn) time If k = O(n), then this is O(nlogn) Note that if k is really large, the brute force O(n2) time algorithm is more efficient

slide-50
SLIDE 50

49/69

Efficiency

Question: How much storage does the algorithm take?

slide-51
SLIDE 51

50/69

Efficiency

Question: Given that the event list is a binary tree that may store O(k) = O(n2) events, is the efficiency in jeopardy?

slide-52
SLIDE 52

51/69

Efficiency

Solution: Only store intersection points of currently adjacent segments.

slide-53
SLIDE 53

52/69

Degenerate cases

How do we deal with degenerate cases? For two different events with the same y-coordinate, we treat them from left to right ⇒ the “upper” endpoint of a horizontal line segment is its left endpoint

slide-54
SLIDE 54

53/69

Degenerate cases

How about multiply coinciding event points?

p

Let U(p) and L(p) be the line segments that have p as upper and lower endpoint, and C(p) the ones that contain p Question: How do we handle this multi-event?

slide-55
SLIDE 55

54/69

Plane Sweep – Conclusion

For every sweep algorithm:

◮ Define the status ◮ Choose the status structure and the event list ◮ Figure out how events must be handled (with sketches!) ◮ To analyze, determine the number of events and how

much time they take Then deal with degeneracies

slide-56
SLIDE 56

55/69

Map overlay

To solve map overlay questions, we need (at the least) intersection points from two sets of line segments.

slide-57
SLIDE 57

56/69

Map overlay

To solve map overlay questions, we also need to be able to represent subdivisions

clay clay sand sand loess rock rock sand sand loess clay rock rock clay

slide-58
SLIDE 58

57/69

Subdivisions

A planar subdivision is a structure induced by a set of line segments in the plane that can only intersect at common

  • endpoints. It consists of vertices, edges, and faces

face vertex edge

slide-59
SLIDE 59

58/69

Subdivisions

Vertices are the endpoints of the line segments Edges are the interiors of the line segments Faces are the interiors of connected two-dimensional regions that do not contain any point of any line segment Objects of the same dimensionality are adjacent or not; objects of different dimensionality are incident or not

adjacent incident

slide-60
SLIDE 60

59/69

Subdivisions

Exactly one face is unbounded, the

  • uter face

Every other face is bounded and has an outer boundary consisting of vertices and edges Any face has zero or more inner boundaries

slide-61
SLIDE 61

60/69

Representing subdivisions

A subdivision representation has a vertex-object class, an edge-object class, and a face-object class It is a pointer structure where

  • bjects can reach incident (or

adjacent) objects easily

slide-62
SLIDE 62

61/69

Representing subdivisions

Use the edge as the central object For any edge, exactly two vertices are incident, up to two faces are incident, and zero or more other edges are adjacent

f1 f2 v1 v2

slide-63
SLIDE 63

62/69

Representing subdivisions

Use the edge as the central object, and give it a direction Now we can speak of Origin, Destination, Left Face, and Right Face

fleft vorigin fright vdestination

slide-64
SLIDE 64

63/69

Representing subdivisions

Four edges are of special interest

fleft fright for fright for fright for fleft for fleft next edge next edge previous edge previous edge

slide-65
SLIDE 65

64/69

Representing subdivisions

It would be nice if we could traverse a boundary cycle by continuously following the next edge for fleft or fright ... but, no consistent edge

  • rientation needs to exist
slide-66
SLIDE 66

65/69

Representing subdivisions

We apply a trick/hack/impossibility: split every edge length-wise(!) into two half-edges Every half-edge:

◮ has exactly one half-edge as its

Twin

◮ is directed opposite to its Twin ◮ is incident to only one face (left)

  • e

Twin( e) Next( e) Prev( e)

slide-67
SLIDE 67

66/69

The doubly-connected edge list

The doubly-connected edge list is a subdivision representation structure with an object for every vertex, every half-edge, and every face A vertex object stores:

◮ Coordinates ◮ IncidentEdge (some

half-edge leaving it) A half-edge object stores:

◮ Origin (vertex) ◮ Twin (half-edge) ◮ IncidentFace (face) ◮ Next (half-edge in cycle of

the incident face)

◮ Prev (half-edge in cycle of

the incident face)

slide-68
SLIDE 68

67/69

The doubly-connected edge list

A face object stores:

◮ OuterComponent

(half-edge of outer cycle)

◮ InnerComponents (list of

half-edges for the inner cycles bounding the face)

f

slide-69
SLIDE 69

68/69

The doubly-connected edge list

A vertex object stores:

◮ Coordinates ◮ IncidentEdge ◮ Any attributes, mark bits

A face object stores:

◮ OuterComponent

(half-edge of outer cycle)

◮ InnerComponents

(half-edges for the inner cycles)

◮ Any attributes, mark bits

A half-edge object stores:

◮ Origin (vertex) ◮ Twin (half-edge) ◮ IncidentFace (face) ◮ Next (half-edge in cycle of

the incident face)

◮ Prev (half-edge in cycle of

the incident face)

◮ Any attributes, mark bits

slide-70
SLIDE 70

69/69

Map overlay problem

The map overlay problem for two subdivisions S1 and S2 is to compute a subdivision S that is the overlay of S1 and S2 All edges of S are (parts of) edges from S1 and S2. All vertices of S are also in S1 or S2, or intersections of edges from S1 and S2 The line-segment intersection algorithm can be extended to this problem using doubly-connected edge lists. [Chapter 2.3]