brute-force triangulation 1. find a diagonal. 2. triangulate the two resulting subpolygons...

30
Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recurs w u v u v w How to find a diagonal? leftmost vertex case 1 case 2 closest to v O(n) time to find a diagonal at every step. (n ) time in the worst case. 2

Upload: devon-tibbett

Post on 16-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Brute-Force Triangulation

1. Find a diagonal.

2. Triangulate the two resulting subpolygons recursively.

w

u

v

u

v

w

How to find a diagonal?

leftmost vertex

case 1case 2

closest to v

O(n) time to find a diagonal at every step.

(n ) time in the worst case. 2

Page 2: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Triangulating a Convex Polygon

(n) time!

Idea:

Decompose a simple polygon into convex pieces.Triangulate the pieces.

monotone

as difficultas triangulation

Page 3: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

y-monotone Pieces

y-axisy-monotone if any line perpendicular to the y-axis has a connected intersection with the polygon.

walk alwaysdownward orhorizontal

lowest vertex

highest vertex

Stragegy:

Partition the polygon into monotonepieces and then triangulate.

Page 4: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Turn Vertex

Turn vertex is where the walkfrom highest vertex to the lowestvertex switches direction.

v

At vertex v

Both adjacent edges are below.

The polygon interior lies above.

Choose a diagonal that goes up.

Page 5: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Five Types of VerticesPoint p = (x, y) is “below” a different point q = (u, v) if

y < v or y = v and x > u.

Otherwise p is “above” q. p

qpq

Start vetex: lies above its two neighbors and has interior angle < .

End vetex: lies below its two neighbors and has interior angle < .

Merge vetex: lies below its two neighbors and has interior angle > .

Split vetex: lies above its two neighbors and has interior angle > .

Regular vetex: the remaining vertices (no turn).

4 types of turn vertices

What happens if we rotate the polygon by ?

start vertices end vertices split vertices merge vertices

Page 6: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Local Non-Monotonicity

Lemma A polygon is y-monotone if it has no split or merge vertices.

Proof Suppose the polygon is not y-monotone. We prove that it contains a split or merge vertex.

r

interior

p ql

exterior

qp = r l

exterior

interior

Start at q, traverse the boundary counterclockwise untilit crosses the line l again at point r.

r

There exists a horizontal line l intersecting the polygon in >1 components, of which the leftmost is a segment between some vertices p (left) and q (right).

r p

The highest vertex during the traversal from q to r must be a split vertex.

r = pTraverse in the oppose directionfrom q and crossesline l again at point r’. The lowest point during this traversal must be a merge vertex.

split vertex

merge vertex

Page 7: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Partitioning into Monotone Pieces

The lemma implies that the polygon will have y-monotone pieces once its split and merge vertices are removed.

Add a downward diagonal at every merge vertex.

Add an upward diagonal at every split vertex.

Use a downward plane sweep.

No new event point will be created except the vertices.

The event queue is implemented as priority queue (e.g., heap).

The next event found in time O(log n).

nvvv ,....,, 21

Page 8: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Removal of a Split Vertex

e i-1e j

ek

e i

v ih(e )j

e : edge immediately to its left. j

v : split vertex i

e : edge immediately to its right. k

or the upper vertex of e if such vertex does notexist.

j

j k

h(e ): lowest vertex above v and between e and e .

j i

Connect v to h(e ). i j

edgehelper

Page 9: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Removal of a Merge Vertex

e jek

v i e : edge immediately to its left. j

v : merge vertex i

e : edge immediately to its right. k

But why not all in the same sweep?

Merge vertices can be handled the same way in an upward sweepas split vertices in a downward sweep.

iConnect v to v . m

v : highest vertex below the sweep line and between e and e .

m

j k

v m

Check if the old helper is a merge vertex and add the diagonal if so. (The diagonal is always added if the new helper is a split vertex).

v will replace v as the helper of e . m i j

In case the helper of e is not replaced any more, connect to its lower endpoint. j

Page 10: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Sweep-Line Status

Implemented as a binary search tree.

Only edges to the left of the polygon interior are stored.

Edges are stored in the left-to-right order.

This is because we are only interested in edges tothe left of split and merge vertices.

With every edge e its helper h(e) is also stored.

Page 11: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

DCEL Representation

n vertices + n edges + 2 faces (initially)

Adding an edge can be done in O(1) time.

Construct a doubly-connected edge list to represent the polygon.

Edges in the status BST and corresponding ones in DCEL cross-point each other.

Add in diagonals computed for split and merge vertices.

Page 12: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

The Algorithm

MakeMonotone(P)Input: A simple polygon P stored in DCEL D.Output: A partitioning of P into monotone subpolygons stored in D.1. Q priority queue storing vertices of P2. T // initialize the status as a binary search tree.3. i 04. while Q 5. do v the highest priority vertex from Q // removal from Q6. case v of 7. start vertex: HandleStartVertex(v )8. end vertex: HandleEndVertex(v ) 9. split vertex: HandleSplitVertx(v ) 10. merge vertex: HandleMergeVertex(v ) 11. regular vertex: HandleRegularVertex(v ) 12. i i + 1

i

i

i

i

i

ii

Page 13: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Start & End Vertices

v 5e 5

v 7

v 6

v 8

v 10

v 9

v 12

v 11v 13

v 15

v 14v 1

v 2

v 3v 4e 4

e 6

e 7e 8

e 10

e 9

e 11e 12

e 14

e 13

e 15

e 3

e 2

e 1

HandleStartVertex(v )1. T T { e }2. h(e ) v // set the helperii

i

i

insert into T

HandleEndVertex(v )1. if h(e ) is a merge vertex2. then insert the diagonal connecting v to h(e ) in D3. T T – { e }

i

i –1

i -1

i –1

i

Page 14: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Split Vertex

v 5e 5

v 7

v 6

v 8

v 10

v 9

v 12

v 11v 13

v 15

v 14

v 1

v 2

v 3v 4e 4

e 6

e 7e 8

e 10

e 9

e 11e 12

e 14

e 13

e 15

e 3

e 2

e 1

HandleSplitVertex(v )1. Search in T to find e directly left of v2. insert the diagonal connect v to h(e ) into D 3. h(e ) v4. T T + { e }5. h(e ) v

i

i

i

j

j

ji

i

i i

Page 15: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Merge Vertex (1)

v 5e 5

v 7

v 6

v 8

v 10

v 9

v 12

v 11v 13

v 15

v 14

v 1

v 2

v 3v 4e 4

e 6

e 7e 8

e 10

e 9

e 11e 12

e 14

e 13

e 15

e 3

e 2

e 1

HandleMergeVertex(v )1. if h(e ) is a merge vertex2. then insert the diagonal connecting v to h(e ) in D3. T T – { e }4. Search in T to find the edge e directly left of v . 5. if h(e ) is a merge vertex then insert the diagonal connecting v to h(e ) in D6. h(e ) v

i

i –1

i -1

i –1

i

j

j

i

i

i

j

j

Page 16: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Merge Vertex (2)

HandleMergeVertex(v )1. if h(e ) is a merge vertex2. then insert the diagonal connecting v to h(e ) in D3. T T – { e }4. Search in T to find the edge e directly left of v . 5. if h(e ) is a merge vertex then insert the diagonal connecting v to h(e ) in D6. h(e ) v

i

i –1

i -1

i –1

i

j

j

i

i

i

j

jv i

e j h(e )j

e i – 1

h(e )i –1

Page 17: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Regular Vertices (1)

v 5e 5

v 7

v 6

v 8

v 10

v 9

v 12

v 11v 13

v 15

v 14

v 1

v 2

v 3v 4e 4

e 6

e 7e 8

e 10

e 9

e 11e 12

e 14

e 13

e 15

e 3

e 2

e 1

HandleRegularVertex(v )1. if the polygon interior lies to the right of v2. then if h(e ) is a merge vertex3. then insert the diagonal connecting v to h(e ) in D4. T T – { e }5. T T + { e }6. h(e ) v 7. else search in T to find the edge e directly left of v . 8. if h(e ) is a merge vertex then insert the diagonal connecting v to h(e ) in D9. h(e ) v

i

i –1

i -1

i –1

i

j

i

i

i

j

i

i

i

i j

j

Page 18: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Handling Regular Vertices (2)HandleRegularVertex(v )1. if the polygon interior lies to the right of v2. then if h(e ) is a merge vertex3. then insert the diagonal connecting v to h(e ) in D4. T T – { e }5. T T + { e }6. h(e ) v 7. else search in T to find the edge e directly left of v . 8. if h(e ) is a merge vertex then insert the diagonal connecting v to h(e ) in D9. h(e ) v

i

i –1

i -1

i –1

i

j

i

i

i

j

i

i

i

i j

j

v i

e j

h(e )j

Page 19: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Correctness

Theorem The algorithm adds a set of non-intersecting diagonals that partitions the polygon into monotone pieces.

Proof The pieces that result from the partitioning contain no split or merge vertices. Hence they are monotone by an earlier lemma.

We need only prove that the added segments are diagonals that intersect neither the polygon edges nor each other.

Establish the above claim for the handling of each of the five type of vertices during the sweep. (Read the textbook on how to do this for the case of a split vertex.)

Page 20: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Running Time on Partitioning

MakeMonotone(P)Input: A simple polygon P stored in DCEL D.Output: A partitioning of P into monotone subpolygons stored in D.1. Q priority queue storing vertices of P2. T 3. i 04. while Q 5. do v the highest priority vertex from Q6. case v of 7. start vertex: HandleStartVertex(v )8. end vertex: HandleEndVertex(v ) 9. split vertex: HandleSplitVertx(v ) 10. merge vertex: HandleMergeVertex(v ) 11. regular vertex: HanleRegularVertex(v ) 12. i i + 1

i

i

i

i

i

ii

// O(n) heap construction

// O(log n)

// O(log n) each case// queries and updates// in O(log n) time and// insertion of a diagonal// in O(1) time.

Total time: O(n log n) Total storage: O(n)

Page 21: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Triangulating a y-Monotone Polygon

Assumption The polygon is strictly y-monotone (no horizontal edges).

It is for clarity of presentation and will be easily removed later.

Order of processing: in decreasing y-coordinate.

A stack S: vertices that have been encountered and may still need diagonals.

Idea: add as many diagonals from the current vertex handled to those on the stack as possible.

lowest vertex on top. funnel

Invariants of iteration:

One boundary of the funnel is a polygon edge.

The other boundary is a chain of reflex vertices(with interior angles > ) plus one convex vertex (the highest) at bottom of the stack.

reflexvertices

convexvertex

currentvertex

Page 22: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Case 1: Next Vertex on Opposite Chain

currentvertex uj

uj –1

uj –4

uj –2

uj –3

e

This vertex must be the lower endpoint of the single edge e bounding the chain.

uj –1

uj –4

uj –3

uj –2

Pop these vertices from the stack.

Add diagonals from the current vertexto them (except the bottom one) as they are popped.

uj

uj –1

Push the previous top of the stack and the current vertex back onto the stack.

Page 23: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Case 2: Next Vertex on the Same Chain

currentvertex uj

uj –1

uj –4

uj –2

uj –3

The vertices that can connect to the current vertex are all on the stack.

uj –2

uj –5

uj –4

uj –3

Pop one vertex from the stack.

It shares an edge with the current vertex.

uj

uj –5

Pop other vertices from the stack as long as they are visible from the currentvertex.

Draw a diagonal between each of themand the current vertex.

uj –5

Pushed the last popped vertex back ontothe stack followed by the current vertex.

uj –1

uj –4

Page 24: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

The Triangulation Algorithm

21

j

j

j

TriangulateMonotonePolygon(P)Input: A strictly y-monotone polygon P stored in DCEL D.Output: A triangulation of P stored in D.1. Merge the vertices on the left and right chains into one sequence sorted in decreasing y-coordinate. (In case there is a tie, the one with smaller x-coordinate comes first.)2. Push(u , S)3. Push(u , S)4. for j 3 to n – 1 5. do if u and Top(S) are on different chains6. then while Next(Top(S)) NULL 7. v Top(S)8. Pop(S)9. insert a diagonal from u to v 10. Pop(S) 11. Push(u , S) 12. Push(u , S) 13. else Pop(S) 14. while Top(S) is visible from u inside the polygon15. v Top(S) 16. insert a diagonal between u and v17. Pop(S) 18. Push the last popped vertex back onto S 19. Push(u , S) j

j

j –1 j

Page 25: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

An Exampleu1

u2u3

u8

u5

u4

u7

u6

u10

u9

u14u13

u11

u12

u15 u16

u17

u19

u18

u20

u2

u1

Start:

u2

u1

u3j =3: j =4:

u2

u1

u3u4

j =5:

u1

u2

u4

u5 j =6:

u1

u5

u5

u6

Page 26: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Example (cont’d)u1

u2u3

u8

u5

u4

u7

u6

u10

u9

u14u13

u11

u12

u15 u16

u17

u19

u18

u20

u6

u5

j = 7:

u6

u7j =8: j =9:

u8

u9

j =10:

u8

u9 j =11:u10

u8

u10

u6

u7

u7

u8

u10

u11

Page 27: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Example (Cont’d)u1

u2u3

u8

u5

u4

u7

u6

u10

u9

u14u13

u11

u12

u15 u16

u17

u19

u18

u20

j = 15:

u10

u15

j = 16:

u10

u15

u16 j = 17:

u10

u16

u15u17

u10

j = 18: j = 19:u17u18

u17

u19u18

u17

Page 28: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Removal of Strict y-monotonicity

The running time of TriangulateMonotonePolygon is (n).

#pushes 2n – 4 ( 2 vertices pushed each of n – 3 iteration steps plus 2 at the beginning)

#pops #pushes

What to do if some vertices have the same y-coordinates?

Treat them from left to right.

The effect of this is equivalent to that of rotating the plane slightly clockwise and then every vertex will have different y coordinate.

Page 29: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Time Complexity of Triangulation

1. Partition a simple polygon into monotone pieces.

2. Triangulate each monotone piece.

O(n log n)

(n) for all monotone pieces together

Theorem A simple polygon can be triangulated in O(n log n) time and O(n) storage.

Page 30: Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. w u v u v w How to find a diagonal? leftmost vertex

Triangulation of a Planar Subdivision

The algorithm for splitting a polygon into monotone piecesdoes not use the fact that the polygon was simple.

The plane sweep for decomposition of a polygon into monotone pieces takes as input only edges that lie to the left of the interior.

This easily generalizes to a planar subdivision in a bounding box.

Thus a planar subdivision with n vertices can also be triangulated in O(n log n) time using O(n) storage.