Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. How to find a diagonal? w leftmost vertex w v v u case 1 u closest to v case 2 O(n) time to.

Download Report

Transcript Brute-Force Triangulation 1. Find a diagonal. 2. Triangulate the two resulting subpolygons recursively. How to find a diagonal? w leftmost vertex w v v u case 1 u closest to v case 2 O(n) time to.

Brute-Force Triangulation
1. Find a diagonal.
2. Triangulate the two resulting subpolygons recursively.
How to find a diagonal?
w
leftmost
vertex
w
v
v
u
case 1
u
closest
to v
case 2
O(n) time to find a diagonal at every step.
(n 2) time in the worst case.
Triangulating a Convex Polygon
(n) time!
Idea:
monotone
Decompose a simple polygon into convex pieces.
as difficult
Triangulate the pieces.
as triangulation
y-monotone Pieces
y-axis
highest
vertex
y-monotone if any line perpendicular
to the y-axis has a connected
intersection with the polygon.
Stragegy:
walk always
downward or
horizontal
Partition the polygon into monotone
pieces and then triangulate.
lowest
vertex
Turn Vertex
Turn vertex is where the walk
from highest vertex to the lowest
vertex switches direction.
At vertex v
v
 Both adjacent edges are below.
The polygon interior lies above.
Choose a diagonal that goes up.
Five Types of Vertices
Point p = (x, y) is “below” a different point q = (u, v) if
q
p
y<v
or
Otherwise p is “above” q.
y = v and x > u.
q
p
4 types of turn vertices
Start vetex: lies above its two neighbors and
has interior angle < .
Split 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 > .
Regular vetex: the remaining vertices (no turn).
What happens if we rotate the polygon by ?
start vertices  end vertices
split vertices  merge vertices
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
split
vertex
l
p
a split or merge vertex.
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).
exterior
Start at q, traverse the boundary counterclockwise until
it crosses the line l again at point r.
q
r
interior
rp
The highest vertex during
the traversal from q to r
must be a split vertex.
interior
r=p
Traverse in the
p=r
q r
l
oppose direction
from q and crosses
line l again at point r’.
The lowest point during this
exterior
traversal must be a merge vertex.
merge 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).
v1 , v2 ,...., vn
The next event found in time O(log n).
Removal of a Split Vertex
vi : split vertex
e j : edge immediately to its left.
h(e j )
ej
e i-1
ek
ek : edge immediately to its right.
ei
h(e j ): lowest vertex above vi
vi
and between ej and ek .
edge
helper
Connect v i to h(e j ).
or the upper vertex of
e j if such vertex does not
exist.
Removal of a Merge Vertex
Merge vertices can be handled the same way in an upward sweep
as split vertices in a downward sweep.
But why not all in the same sweep?
vi
ej
vm
ek
vi : merge vertex
e j : edge immediately to its left.
ek : edge immediately to its right.
vm : highest vertex below the
sweep line and between
e j and ek .
Connect vi to v m .
v m will replace vi as the helper of ej .
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).
In case the helper of ej is not replaced any more, connect to its lower endpoint.
Sweep-Line Status
Implemented as a binary search tree.
Only edges to the left of the polygon interior are stored.
This is because we are only interested in edges to
the left of split and merge vertices.
Edges are stored in the left-to-right order.
With every edge e its helper h(e) is also stored.
DCEL Representation
Construct a doubly-connected edge list to represent the polygon.
n vertices + n edges + 2 faces (initially)
Add in diagonals computed for split and merge vertices.
Edges in the status BST and corresponding ones in DCEL
cross-point each other.
Adding an edge can be done in O(1) time.
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 P
2. T   // initialize the status as a binary search tree.
3. i  0
4. while Q  
5.
do v i  the highest priority vertex from Q // removal from Q
6.
case v i of
7.
start vertex: HandleStartVertex(v i )
8.
end vertex: HandleEndVertex(vi )
9.
split vertex: HandleSplitVertx(v i )
10.
merge vertex: HandleMergeVertex(vi )
11.
regular vertex: HandleRegularVertex(v i )
12.
ii+1
Handling Start & End Vertices
insert into T
v5
v3
e5
v4
e
4
v6
e3
e6
v1
v 14 e2
v
v2
v9 7
v 8 e7
e1
e8
e15
e14
e9
v 12
v 15
v 10 e
10 e11
e13
e12
v 11
v 13
HandleStartVertex(v i )
1. T  T  { e i }
2. h(e i )  v i // set the helper
HandleEndVertex(v i )
1. if h(ei –1) is a merge vertex
2.
then insert the diagonal
connecting vi to h(ei –1)
in D
3. T  T – { ei -1 }
Handling Split Vertex
v5
v3
e5
v4
e
4
v6
e3
e6
v1
e2
v
v2
v9 7
v 8 e7
e1
e8
e15
v 14
e14
e9
v 12
v 15
v 10 e
10 e11
e13
e12
v 11
v 13
HandleSplitVertex(vi )
1. Search in T to find e j directly left of v i
2. insert the diagonal connect vi to h(e j )
into D
3. h(e j )  v i
4. T  T + { ei }
5. h(ei )  vi
Handling Merge Vertex (1)
v5
v3
e5
v4
e
4
v6
e3
e6
v1
e2
v
v2
v9 7
v 8 e7
e1
e8
e15
v 14
e14
e9
v 12
v 15
v 10 e
10 e11
e13
e12
v 11
v 13
HandleMergeVertex(vi )
1. if h(ei –1) is a merge vertex
2.
then insert the diagonal
connecting vi to h(ei –1)
in D
3. T  T – { ei -1 }
4. Search in T to find the edge e j
directly left of v i .
5. if h(e j ) is a merge vertex
then insert the diagonal
connecting vi to h(ej )
in D
6. h(e j )  v i
Handling Merge Vertex (2)
ei–1
ej
h(e j )
vi
h(ei –1 )
HandleMergeVertex(vi )
1. if h(ei –1) is a merge vertex
2.
then insert the diagonal
connecting vi to h(ei –1)
in D
3. T  T – { ei -1 }
4. Search in T to find the edge e j
directly left of v i .
5. if h(e j ) is a merge vertex
then insert the diagonal
connecting vi to h(ej )
in D
6. h(e j )  v i
Handling Regular Vertices (1)
v5
v3
e5
v4
e
4
v6
e3
e6
v1
e2
v
v2
v9 7
v 8 e7
e1
e8
e15
v 14
e14
e9
v 12
v 15
v 10 e
10 e11
e13
e12
v 11
v 13
HandleRegularVertex(v i )
1. if the polygon interior lies to the right
of v i
2.
then if h(e i –1) is a merge vertex
3.
then insert the diagonal
connecting v i to h(e i –1 )
in D
4.
T  T – { e i -1 }
5.
T  T + { ei }
6.
h(e i )  v i
7.
else search in T to find the edge e j
directly left of vi .
8.
if h(ej ) is a merge vertex
then insert the diagonal
connecting v i to h(e j )
in D
9.
h(e j )  v i
Handling Regular Vertices (2)
ej
vi
h(e j )
HandleRegularVertex(v i )
1. if the polygon interior lies to the right
of v i
2.
then if h(e i –1) is a merge vertex
3.
then insert the diagonal
connecting v i to h(e i –1 )
in D
4.
T  T – { e i -1 }
5.
T  T + { ei }
6.
h(e i )  v i
7.
else search in T to find the edge e j
directly left of vi .
8.
if h(ej ) is a merge vertex
then insert the diagonal
connecting v i to h(e j )
in D
9.
h(e j )  v i
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.)
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 P // O(n) heap construction
2. T  
3. i  0
4. while Q  
5.
do v i  the highest priority vertex from Q // O(log n)
6.
case v i of
7.
start vertex: HandleStartVertex(v i ) // O(log n) each case
8.
end vertex: HandleEndVertex(vi )
// queries and updates
9.
split vertex: HandleSplitVertx(v i )
// in O(log n) time and
10.
merge vertex: HandleMergeVertex(vi ) // insertion of a diagonal
11.
regular vertex: HanleRegularVertex(v i )// in O(1) time.
12.
ii+1
Total time: O(n log n)
Total storage: O(n)
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.
convex
vertex
lowest vertex on top.
funnel
current
vertex
reflex
vertices
Idea: add as many diagonals from the current
vertex handled to those on the stack as
possible.
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.
Case 1: Next Vertex on Opposite Chain
This vertex must be the lower endpoint of the
single edge e bounding the chain.
u j –4
u j –3
u j –2
e
current
vertex
Pop these vertices from the stack.
u j –1
Add diagonals from the current vertex
to them (except the bottom one) as they
are popped.
uj
Push the previous top of the stack and
the current vertex back onto the stack.
u j –1
u j –2
u j –3
uj
u j –4
u j –1
Case 2: Next Vertex on the Same Chain
u j –5
The vertices that can connect to the current
vertex are all on the stack.
u j –4
u j –3
u j –2
u j –1
current
vertex u j
Pop one vertex from the stack.
It shares an edge with the current vertex.
Pop other vertices from the stack as
long as they are visible from the current
vertex.
u j –1
u j –2
u j –3
uj
u j –4
u j –4
u j –5
u j –5
Draw a diagonal between each of them
and the current vertex.
Pushed the last popped vertex back onto
the stack followed by the current vertex.
The Triangulation Algorithm
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)
1
3. Push(u2 , S)
4. for j  3 to n – 1
5.
do if uj and Top(S) are on different chains
6.
then while Next(Top(S))  NULL
7.
v  Top(S)
8.
Pop(S)
9.
insert a diagonal from u j to v
10.
Pop(S)
11.
Push(u
, S)
j –1
12.
Push(u , S)
j
13.
else Pop(S)
14.
while Top(S) is visible from u j inside the polygon
15.
v  Top(S)
16.
insert a diagonal between u and v
j
17.
Pop(S)
18.
Push the last popped vertex back onto S
19.
Push(u j , S)
An Example
u3
u2
u1
u4
Start:
u6
u5
u7
u9
u2
u1
u8
u 10
u 11
u3
j =3:
u 12
u
u 1413
u 15
u2
u 43
j =4:
u1
u1
u 16
u2
u 18
u 17
u4
j =5:
u 19
u 20
u 52
u1
j =6:
u 65
u 51
Example (cont’d)
u3
u2
u1
u4
j = 7:
u6
u5
u7
u9
u 65
u8
u 10
u 11
j =8:
u 12
u 87
j =9:
u 76
u 13
u 14
u 15
u 76
u9
u8
u 16
u 18
u 17
j =10:
u 19
u 20
u 10
9
u8
j =11:
u 11
10
u 10
8
Example (Cont’d)
u3
u2
u1
u4
j = 15:
u6
u5
u7
u9
u 10
u8
u 10
u 11
j = 16:
u 12
u
u 1413
u 15
u 15
u 16
u 16
j = 17:
u 16
u 15
u 17
15
u 10
u 10
u 18
17
u 18
19
u 18
u 17
u 19
u 20
j = 18:
u 17
10
j = 19:
u 17
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.
Time Complexity of Triangulation
1. Partition a simple polygon into monotone pieces.
O(n log n)
2. Triangulate each monotone piece.
(n) for all monotone pieces together
Theorem A simple polygon can be triangulated in
O(n log n) time and O(n) storage.
Triangulation of a Planar Subdivision
The algorithm for splitting a polygon into monotone pieces
does 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.