37
Polygon Triangulation

Polygon Triangulation. Triangulation We already know that any polygon can be triangulated by means of diagonals (Theorem 3.1) It is easy to triangulate

Embed Size (px)

Citation preview

Polygon Triangulation

Triangulation

• We already know that any polygon can be triangulated by means of diagonals(Theorem 3.1)

• It is easy to triangulate a convex polygon

• Therefore the first idea is to decompose a polygon by means of diagonals into convex pieces and then triangulate each piece

• Unfortunately, to decompose a polygon into convex pieces is as hard as to triangulate it!

Strategy

• Partition a polygon into monotone pieces

• Triangulate each monotone piece

Partitioning into monotone pieces

Idea

• We will use plane sweep method to partition pieces into y-monotone pieces, i.e. the pieces which are monotone with respect to the y-axis

Move line downward and note how the form of the intersection of the line withthe polygon changes

Sweeping a polygon

Sweeping a polygon

Types of vertices:

start

end

regular

split

merge

Formal definitions

With respect to the y-axis, a vertex v is called

start v. if its two neighbors lie below it and the interior angle at v is < 180o

end v. if its two neighbors lie above it and the interior angle at v is < 180o

split v. if its two neighbors lie below it and the interior angle at v is > 180o

merge v. if its two neighbors lie above it and the interior angle at v is > 180o

other vertices are called regular

v

v

v

v

Note

• In the previous definition,a point q is below another point p if py>qy or py=qy and px < qx

• For example, the following is a split vertex:

v

Lemma 3.4• A polygon without horizontal edges is

y-monotone (i.e. monotone with respect to the y-axis) if an only if it has no split or merge vertices

y

Proof

Proof by contradiction: assume that P has a split vertex v (similarly for the case of a merge vertex)

Let l be a horizontal line below v such that the distance between v and l is less then the distance between l and any other vertex of P

Then the intersection of l and P is not connected

• y-monotone implies no merge or split vertices

v l

Proof• No merge or split vertices implies monotone

Proof by contradiction: assume that P is not monotone

Let l be a horizontal line such that the intersection of l and P is not connected

Then there exists two points, a and b, in the intersection of l and P, such that the open interval (ab) is in the complement of P

Then there exists a chain avi vi+1…vkb, where vi vi+1…vk are consecutive vertices of P, such that this chain does not intersect (ab), and such that the interior of the polygon avi vi+1…vkba lies completely outside of P

Suppose that vi vi+1…vk there are vertices above the line l(the case when there are vertices only below is similar)

Let v be the highest of such vertices

It is easy to see that v is a split vertex

Proof• No merge or split vertices implies monotone

la b

v

Monotone pieces

• Thus to partition a polygon into monotone pieces we need to "get rid" of all split and merge vertices

• We will do this by adding diagonals during the plane sweep:A diagonal going "upward" from each

split vertex

A diagonal going "downward" from each merge vertex

Plane sweep: preparation• Event points are vertices of polygon P

• Vertices are ordered as follows: p>q if py>qy

or py=qy and px < qx

• It takes O(n log n) time to order vertices

• No new event points are generated during the plane sweep!

• Let v1,v2,…,vn be vertices of P in counterclockwise order

• Let e1,e2,…,en be edges of P such that ei = vivi+1

• Each time when the sweep line reaches split vertex we add a diagonal to a vertex lying above the sweep line

• In the case of merge vertices, diagonals are added later during the algorithm

Helper of an edge

For a given position of the sweep line,the helper of an edge e, helper (e), is defined as the lowest vertex above the sweep line such that the horizontal segment connecting the vertex to e lies inside polygon P

e

diagonal

helper(e)

Helper of an edge

Note: a helper of an edge can be the upper endpoint of this edge!

e'

helper(e')

Handling a split vertex• Suppose that the sweep line reached a split vertex v

• We need to connect it to some other vertex so that the diagonal is inside P

• Therefore a good candidate for this other vertex is a vertex which is close to v in some sense, e.g. in vertical direction

• If e is the edge immediately to the left of v on the sweep line, we can connect v to the helper(e)

• Note: v becomes the new helper of e

Handling a split vertex

ehelper(e)

v

New helper(e)

e is immediately to the leftof v on the sweep line

Example 1

12

11

10

9

8

7

3

2 ?

We met a merge vertex

It is logical to connect it to vertex 6, which is below!

45

6

Handling a merge vertex• Suppose that the sweep line reached a merge vertex v

• We need to connect it to some other vertex so that the diagonal is inside P

• We will find this vertex below the sweep line

• Namely, If e is the edge immediately to the left of v on the sweep line, then v becomes helper(e)

• We will connect v to the new helper(e) at the moment when it changes!

• If the helper of e is never replaced, we connect v to the lower endpoint of e

Example

v

v becomeshelper of e

e a

a becomes new helper of e, so we connect a and v!

Example

v

v becomeshelper of e

e a

The helper of e never changed until we reachedthe lower endpoint of e, so we connectthe lower endpoint of e and v

Data structures

• Event (priority) queue Q (contains vertices of P in the order of decreasing y-coordinates)

• Self-balancing binary search tree T

The edges of P intersecting the sweep line are stored in T

With each edge in T we store its helper

Note: due to the nature of the algorithm, we need to store in T only those edges of P that have the interior of P to their right

• T, together with helpers, form the status of the algorithm

• The status changes as the sweep line moves (we update information about edges and helpers)

• DCEL to store information about P and subsequently added diagonals

Algorithm MakeMonotone(P)

Input. A simple polygon P stored in a doubly-connected edge list D

Output. A partitioning of P into monotone subpolygons, stored in D

Construct a priority queue Q on the vertices of P

use y-coordinates to determine priority

if two points have the same y-coordinate, the one with smaller x-coordinate has higher priority

Initialize an empty binary search tree T

While Q is not empty Do

remove the vertex vi with the highest priority from Q

call the appropriate procedure to handle the vertex, depending on the type of the vertex

End Do

HandleStartVertex(vi)

Insert ei in T

helper(ei)=vi

HandleEndVertex(vi)

If helper(ei-1) is a merge vertex

Insert the diagonal connecting vi to helper(ei-1)in D

Delete ei-1 from Tvi

ei

vi+1

ei-1

vi

vi-1 helper(e i-1)

HandleSplitVertex(vi)

search in T to find the edge ej directly left of vi

insert the diagonal,connecting vi

to helper(ej), in D

helper(ej) = vi

insert ei in T

helper(ei) = vi

ej

helper(ej)

vi

New helper(ej)and helper(ei)

ei

HandleMergeVertex(vi) If helper(ei−1) is a merge vertex

insert the diagonal connecting vi to helper(ei−1) in D

delete ei−1 from T

search in T to find the edge ej directly left of vi

If helper(ej) is a merge vertex

Insert the diagonal connecting vi to helper(ej) in D

helper(ej) = vi vi

ei-1

h(ej)

New helper(ej)

ej

h(ei-1)

HandleRegularVertex(vi)

If the interior of P lies to the right of vi Then

If helper(ei−1) is a merge vertex Then

Insert the diagonal connecting vi to helper(ei−1) in D

End If

Delete ei−1 from T

Insert ei in T

helper(ei) = vi

vi

ei-1

ei

help

er(e

i−1)

Else (i.e. interior of P lies to the left of vi)

Search in T to find the edge ej directly left of vi

If helper(ej) is a merge vertex Then

Insert the diagonalconnecting vi to helper(ej) in D

End If

helper(ej) = vi

End If vi

ei-1

eiej

help

er(e

j)

Note: in this case we do not modify Tsince there is no interior of Pimmediately to the right of ei or ei-1

Running time

• Claim. The algorithm MakeMonotone(P), that partitions P into monotone pieces, requires O(n log n) time

• Notes

The algorithm described above is due to Lee and Preparata (1977)

It was an open problem whether a faster thanO(n log n) time algorithm exists for triangulation of a simple polygon

In 1990 Chazelle found an O(n)-time algorithm

Algorithm MakeMonotone(P)

Construct a priority queue Q on the vertices of P

use y-coordinates to determine priority

if two points have the same y-coordinate, the one with smaller x-coordinate has higher priority

Initialize an empty binary search tree T

While Q is not empty Do

remove the vertex vi with the highest priority from Q

call the appropriate procedureto handle the vertex, depending on the type of the vertex

End Do

O (n log n)

n times

O(log n)

O(n log n)

HandleStartVertex(vi)

Insert ei in T

helper(ei)=vi

HandleEndVertex(vi)

If helper(ei-1) is a merge vertex

Insert the diagonal connecting vi to helper(ei-1)in D

Delete ei-1 from Tvi

ei

vi+1

ei-1

vi

vi-1 helper(e i-1)

O(log n)

O(1)

O(log n)

HandleSplitVertex(vi)

search in T to find the edge ej directly left of vi

insert the diagonal,connecting vi

to helper(ej), in D

helper(ej) = vi

insert ei in T

helper(ei) = vi

ej

helper(ej)

vi

New helper(ej)and helper(ei)

ei

O(1)

O(log n)

HandleMergeVertex(vi) If helper(ei−1) is a merge vertex

insert the diagonal connecting vi to helper(ei−1) in D

delete ei−1 from T

search in T to find the edge ej directly left of vi

If helper(ej) is a merge vertex

Insert the diagonal connecting vi to helper(ej) in D

helper(ej) = vi

vi

ei-1

h(ej)

New helper(ej)

ej

h(ei-1)

O(1)

O(log n)

O(log n)

O(1)

HandleRegularVertex(vi)

If the interior of P lies to the right of vi Then

If helper(ei−1) is a merge vertex Then

Insert the diagonal connecting vi to helper(ei−1) in D

End If

Delete ei−1 from T

Insert ei in T

helper(ei) = vi

vi

ei-1

ei

help

er(e

i−1)

O(1)

O(log n)

O(log n)

Else (i.e. interior of P lies to the left of vi)

Search in T to find the edge ej directly left of vi

If helper(ej) is a merge vertex Then

Insert the diagonalconnecting vi to helper(ej) in D

End If

helper(ej) = vi

End If vi

ei-1

eiej

help

er(e

j)

Note: in this case we do not modify Tsince there is no interior of Pimmediately to the right of ei or ei-1

O(1)

O(log n)