242-535 ada: 16. cg topics1 objective o an examination of four important cg topics o just a taster...

57
242-535 ADA: 16. CG Topics 1 • Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015 16. Computational Geometry Topics

Upload: linda-bradford

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 1

• Objectiveo an examination of four important CG topicso just a taster of a very large research area

Algorithm Design and Analysis

(ADA)242-535, Semester 1 2014-2015

16. Computational Geometry Topics

Page 2: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 2

1. Intersection of Multiple Line Segmentso the sweeping algorithm

2. Finding the Convex Hullo Graham Scan, Jarvis' March, QuickHull

3. Finding the Closest Pair of Pointso divide-and-conquer

4. The Art Gallery Problem

Overview

Page 3: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 3

1.1. Multiple Line Segments

1.2. A Brute-Force Algorithm

1.3. The Sweeping Algorithm

1.4. Implementing Sweeping

1. Intersection of Multiple Line Segments

Page 4: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

1.1. Multiple Line Segments

Input: a set of n line segments in the plane. Output: all intersections, and for each intersection the involved segments. .

Page 5: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

1.2. A Brute-Force Algorithm

Look at each pair of segments, and check if they intersect.If so, output the intersection.

n(n-1)/2 comparison are needed in the worst case, so the running time is O(n2)

• Most segments do not intersect, or if they do, only with a few other segments

Need a faster algorithm that deals with such situations!

But the lines are sparsly distributed in practice:

Page 6: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

1.3. The Sweeping Algorithm

Avoid testing pairs of segments that are far apart.

Idea: imagine a vertical sweep line passes through the given set of line segments, from left to right.

Sweepline

also known as the "Bentley-Ottmann" Algorithmand the Sweep Line Algorithm

Page 7: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Non-Degeneracy Assumptions

No segment is vertical. // this means that the sweep line will always hit a segment at a point.

If an input segment is vertical, then it is rotated clockwise by a tiny angle.

Page 8: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Sweep Line StatusThe set of segments intersecting the sweep line.

It changes as the sweep line moves, but not continuously.

Updates of status happen only at event points. endpoints

intersections

event points

AG

C

T

Page 9: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Ordering SegmentsA total order over the segments that intersect the current position of the sweep line:

A

B

C

D

E

B > C > D(A and E not inthe ordering)

C > D(B drops out ofthe ordering)

At an event point, the sequence of segments changes:

Update the status. Detect the intersections.

later

Page 10: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Status Update (1)

¨ A new segment L intersecting the sweep line

L

M

K

¨ Check if L intersects with the segment above (K) and the segment below (M).

new eventpoint

¨ Intersection(s) are new event points.

Event point is the left endpoint of a segment.

N

K, M, N K, L, M, N

O

Page 11: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Status Update (2)

¨ The two intersecting segments (L and M) change order.

L

M

K

¨ Check intersection with new neighbors (M with O and L with N).

¨ Intersection(s) are new event points.

Event point is an intersection.

N

O

O, L, M, N O, M, L, N

Page 12: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Status Update (3)

¨ The two neighbors (O and L) become adjacent.

L

M

K

¨ Check if they (O and L) intersect.

¨ Intersection is new event point.

Event point is a lower endpoint of a segment.

N

O, M, L, N O, L, N

O

Page 13: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 13

The algorithm manages two kinds of data:• 1. The sweep line status gives the relationships

among the objects intersected by the sweep line.

• 2. The event point queue is a sequence of event points that defines the halting positions of the sweep line.

1.4. Implementing Sweeping

Page 14: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Event Point Queue Operations

Data structure: a balanced binary search tree (e.g., red-black tree).

Manages the ordering of event points:

• by x-coordinates• by y-coordinates in case of a tie in x-coordinates

Supports the following operations on a segment s.

inserting an event fetching the next event

Every event point p is stored with all segments starting at p.

// O(log m)// O(log m) m = #event

points currentlybeing managed

Page 15: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 15

• The sweep line status (T) requires the following operations:• INSERT(T, s): insert segment s into T.• DELETE(T, s): delete segment s from T.• ABOVE(T, s): return the segment immediately above segment

s in T.• BELOW(T, s): return the segment immediately below

segment s in T.

• Use a balanced binary search tree for T (e.g. Red-black trees): O(log n) for each operation

Sweep Line Operations

Page 16: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 16

ANY-SEGMENTS-INTERSECT(S)1 T = Ø2 sort the endpoints of the segments in S from left to right,breaking ties by putting left endpoints before right endpointsand breaking further ties by putting points with lowery-coordinates first

3 for (each point p in the sorted list of endpoints) {4 if (p is the left endpoint of a segment s) {5 INSERT(T, s)6 if (ABOVE(T, s) exists and intersects s) or

(BELOW(T, s) exists and intersects s)7 return TRUE

}8 if (p is the right endpoint of a segment s) {9 if (both ABOVE(T, s) and BELOW(T, s) exist) and

(ABOVE(T, s) intersects BELOW(T, s))10 return TRUE11 DELETE(T, s)

}}

12 return FALSE

Segments intersect Pseudocode

Page 17: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 17

Execution Example

edb

the intersection of segments d and b is detected when segment c is deleted

Page 18: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 18

• If the segment set contains n segments, then ANY-SEGMENTS-INTERSECT runs in time O(n log n)o Line 1 takes O(1) time. o Line 2 takes O(n log n) time, using merge sort or

heapsort. o The for loop of lines 3–11 iterates at most once per

event point, and so with 2n event points, the loop iterates at most 2n times.

o Each iteration takes O(log n) time, since each tree operation takes O(log n) time and each intersection test takes O(1) time (by using the intersection function from part 15).

o Total cost = O(1) + O(n * (log n + 1)) = O(n log n)

Running Time

Page 19: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 19

2.1. Convex & Concave Sets2.2. The Convex Hull2.3. The Graham Scan2.4. Jarvis’ March2.5. QuickHull2.6. Lower Bound of O(n log n)

2. Finding the Convex Hull

Page 20: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

2.1. Convex and Concave Sets

A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R.

Otherwise, it is called concave.

Convex

p

q

R1

p

q

R2

Concave

Page 21: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

2.2. The Convex HullThe convex hull CH(P) of a set of points P is the smallest convex region that contains all of P.

Rubber band

When P is finite, its convex hull is the unique convex polygon whose vertices are from P and that contains all points of P.

Page 22: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

The Convex Hull Problem

Input: a set P = { p , p , …, p } of points

Output: a list of vertices of CH(P) in counterclockwise order.

1 2 n

Example

8

pp

p

p

p

p

p

p

pp

9

210

5

76 1

3 4

CH(P) = [ p5, p9, p2, p8, p10, p7 ]

Not all the pointsin P are in CH(P)

Page 23: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Edges of a Convex Hull

For every edge both endpoints p, q P.

pq

All other points in P lie to the same side of the line passing through p and q

all points onthis side of theq – p edge

Page 24: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Floating Arithmetic is not Exact

p

r

qNearly colinear points p, q, r.

p to the left of qr. q to the left of rp. r to the left of qp.

All three accepted as edges!

The algorithm is not robust – it could fail due to small numerical error.

Page 25: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

2.3. The Graham Scan

p 0

pp

pp

pp

p

pp

p

12

3

4

p5

6

7

8

9

10

11

The center point has the minimum y-coordinate

Labels are in the polar angle order.(What if two points have the same polar angle?)

How to break a tie?

sort by polar angle

handling degeneracies

Page 26: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 26

• Consider each of the points in the sorted sequence.

• For each point, is moving from the two previously considered points to this point a "left turn" or "right turn"?

• "Right turn": this means that the second-to-last point is not part of the convex hull and should be removed. o this process is continued for as long as the set of the last

three points is a "right turn"

• "Left turn": the algorithm moves to the next point in the sequence.

A Turning Algorithm

Page 27: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 27

Turning in Action

C is a left turn compared to A – B; add C

D is a right turn compared to B - C; remove C

D is a left turn compared to A - B; add D

X

Page 28: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Graham-Scan(P) let p0 be the point in P with minimum y-coordinate let p1 , p2 , …, pn-1 be the remaining points in P sorted in counterclockwise order by polar angle around p0 Stack s = new Stack(); s.push(p0) s.push(p1) s.push(p2) for i = 3 to n 1 while ( pi makes a nonleft turn from the line segment determined by s.top() and s.nextToTop() ) s.pop() s.push(pi) return s

The Graham Scan Algorithm

the convex hull pointsare stored on a stack

Page 29: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Stack Usage

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

2

Page 30: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

3

Page 31: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

Page 32: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 5

Page 33: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

Page 34: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

p 7

p 8

Page 35: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

p 7

Page 36: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p10

Page 37: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p11

Page 38: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Finish

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p11

Page 39: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Proof of CorrectnessEach point popped from stack S is not a vertex of CH(P).

pp

p

p

0

i

jk

p

p

p

p0

i

j

k

Two cases when pj is popped:

In neither case can pj become a vertex of CH(P).

Proof

pk is a rightturn comparedto pi - pj

pk is a 0 angleturn comparedto pi - pj

X X

Page 40: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

The points on stack S always form the vertices of a convex polygon in counterclockwise order (an invariant).

• Popping a point from S preserves the invariant.

The regioncontaining pi The invariant still holds.

Proof • The claim holds at S initialization when p0, p1, p2 form a triangle (which is obviously convex)

• Consider a point pi being pushed onto S.

p0

pj

pi

Page 41: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Running Time

The running time of Graham’s Scan is O(n log n).

#operations time / operation total

Push n O(1) (n)

Pop n 2 O(1) O(n)

Sorting 1 O(n log n) O(n log n)

Why?

Findingp0

1 (n) (n)

Page 42: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

2.4. Jarvis’ March A “package/gift wrapping” technique using two "chains"

Page 43: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 43

• We choose the first vertex as the lowest point p0, and start the right chain.o The next vertex, p1, has the smallest polar angle of any

point with respect to p0. o Then, p2 has the smallest polar angle with respect to p1. o The right chain goes as high as the highest point p3.

• Then,we return to p0 and build the left chain by finding smallest polar angles with respect to the negative x-axis.

The Operation of Jarvis’ March

Page 44: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Running Time of Jarvis’ March

Let h be the number of vertices of the convex hull.

• For each vertex, finding the point with the minimum Polar angle, that is, the next vertex, takes time O(n)

• The comparison between two polar angles can be done using the cross product.

Thus O(nh) time in total.

Page 45: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 45

• Concentrate on points close to hull boundary• Named for similarity to Quicksort

o O(n log n)

2.5. QuickHull

Set QuickHull(a, b, S) if S = 0 return {} else c = index of point with max distance from a-b A = points strictly right of (a, c) B = points strictly right of (c, b) return QuickHull(a, c, A) + {c} + QuickHull(c, b, B)

a

b

finds one of upper or lower hull

c

A

Page 46: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 46

• The worst-case time to find the convex hull of n points using a decision tree model is O(n log n)

• Proof based on sorting:o Given an unsorted list of n numbers: (x1,x2 ,…,

xn)o Form an unsorted set of points: (xi, xi

2) for each xi

o Convex hull of these points produces a sorted list!

• a parabola: so every point is on the convex hull

o Finding the convex hull of n points is therefore at least as hard as sorting n points, so worst-case time is in O(n log n)

2.6. Lower Bound of O(n log n)

Convex hullof red pts

Page 47: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 47

• There are a set of n points P = { p1,…pn }.

• Find a pair of points p, q such that |p – q| is the minimum of all |pi – pj|

• Easy to do in O(n2) timeo for all pi ≠ pj, compute ║pi - pj║ on all the pairs

and choose the minimum, which involves n(n-1)/2 comparisons

• We will aim for O(n log n) time

3. Finding the Closest Pair of Points

Page 48: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 48

• Divide:o Compute the median of the x-coordinateso Split the points into a left half PL and right half

PR, each of size n/2

• Conquer: compute the closest pairs for PL and PR

• Combine the results (the hard part)

Divide and Conquer

PL PR

median line

Page 49: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 49

• dL = closestDist(PL)dR = closestDist(PR)d = min( d1, d2 )

• Observe:o Need to check only pairs which

cross the dividing lineo Only interested in pairs within

distance < d of each other

• It's enough to look at only the points in the 2d wide strip around the median line

Combine

PL PR

median line

Page 50: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 50

• Sort all points in the strip by their y-coords, forming q1…qk, k ≤ n.

• Let yi be the y-coord of qi dmin = d for i = 1 to k { j = i - 1 while (yi - yj < d){ if(║qi-qj║< d) dmin =║qi-qj║ j = j-1 } }

• Report dmin (and the corresponding pair)

Scanning the Strip

Page 51: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

242-535 ADA: 16. CG Topics 51

• Combine: O(n log n) because we sort the y-coords. But, we can:o Sort all points the y-coords at the beginning, outside of

the main loop since the Divide stage preserves the y-order of points

o Then this combine stage only takes O(n)

• We get T(n)=2T(n/2)+O(n), so T(n) is O(n log n)

Running Time

divide the data by half and calculate the closest pair on PL and PR

combine

Page 52: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

4. The Art Gallery Problem

camera

How many cameras are needed to guard a gallery and where should they be placed?

Page 53: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

Simple Polygon ModelModel the art gallery as a region bounded by some simple polygon (no self-crossing). Regions with holes are not allowed.

convex polygonone camera

an arbitrary n-gon (n vertices)

Bad news: finding the minimum number of cameras for a given polygon is NP-hard (exponential time).

Page 54: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

TriangulationTo make things easier, we decompose a polygon into pieces that areeasy to guard. Draw diagonals between pairs of vertices.

diagonals

Triangulation: decomposition of a polygon into triangles by a maximal set of non-intersecting diagonals.

Guard the art galleryby placing a camera in every triangle …

Page 55: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

No. of CamerasTheorem: Every simple polygon has a triangulation. Any triangulation of a simple polygon with n vertices consists of n – 2 triangles. n – 2 cameras can guard the simple polygon.

Note: a camera sitting on a diagonal guards two triangles. the no. of cameras can be reduced to roughly n/2

Note: a vertex is adjacent to many triangles so placing cameras at vertices can reduce the number even more…

Page 56: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

3-Coloring of Vertices Idea: Select vertices, such that every triangle has at least oneof those vertices.

Assign each vertex a color: pink, green, or yellow.

Any two vertices connected by an edge or a diagonal must be assigned different colors.

Thus the vertices of every trianglewill be in three different colors.

Choose the smallest color set, and place cameras at all the vertices using that color n/3 cameras.

Page 57: 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis

The Dual GraphA dual graph G has a node inside every triangle and an edge between every pair of nodes whose corresponding triangles share a diagonal.

The dual graph G is actually a tree (marked in red on this slide), and it is possible to build one using DFS.