computational geometry - indian institute of technology...

20
Computational Geometry Guarding and Triangulating Polygons John Augustine Guarding and Triangulating Polygons

Upload: others

Post on 17-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Computational Geometry

Guarding and Triangulating Polygons

John Augustine

Guarding and Triangulating Polygons

Page 2: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

The Art Gallery Problem

A simple polygon is a region enclosed by a single closedpolygonal chain that does not intersect itself.

Models 2D spaces such as floor plans.

Given a simple polygon P with n vertices, place a smallnumber of guards inside P so that it is completelymonitored. I.e., for any point p inside P , there mustbe a guard positioned at g such that line segment pqis completely inside P .

Computing the optimum (i.e., minimum) is NP-hard.

Guarding and Triangulating Polygons 1

Page 3: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Triangulation

A convex polygon only needs one guard! So, if wecan decompose P into convex pieces, we need one perconvex piece.

A diagonal of P is a line segment that connects twovertices of P and is contained in the interior of P .

A triangulation is a decomposition of P into trianglesbe a maximal number of non-intersecting diagonals.

Theorem 1. Any simple polygon P with n verticesadmits a triangulation and any triangulation has(n− 2) triangles.

Guarding and Triangulating Polygons 2

Page 4: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

v

w

u

v

w

u

v′

Proof Sketch. First we show that a diagonal alwaysexists. (See figures above.) Then, we can always cutalong such a diagonal, thereby decomposing P intotwo smaller polygons that we can induct on to showboth claims.

Assume for now that a triangulation of P is given. Dowe really need n− 2 guards? Can we do better?

Guarding and Triangulating Polygons 3

Page 5: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Back to the Art Gallery Problem

A 3-colouring of a triangulated polygon P is anassignment of one of three distinct colours to eachvertex such that any two vertices connected by anedge or a diagonal are assigned different colours.

Note. The three vertices of any triangle will beassigned different colours.

How can we show that bn/3c guards suffice if ∃ 3-colouring?

?

µ

ν

The dual graph of a triangulated polygon P is a graphG = (V,E) such that V is the set of (n− 2) trianglesand edges connect two triangles that share an edge.

We use DFS on the dual graph to 3-colour P .

Guarding and Triangulating Polygons 4

Page 6: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Invariant. Vertices of all triangles corresponding todual graph nodes traversed are 3-colored.

Start DFS from any node. 3-Colour the vertices ofcorresponding triangle.

Consider any subsequent edge traversed. It goesthrough a diagonal d and enters a new triangle. Theends of d are already coloured and the third vertexof the newly entered triangle can only be coloured byone colour. The good news is that the invariant ismaintained.

Theorem 2 (Art Gallery Theorem). To guard a simplepolygon with n vertices, bn/3c guards are sufficientand sometimes necessary.

bn/3c prongs

Guarding and Triangulating Polygons 5

Page 7: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Overview of Approach

Theorem 3. We can compute the positions of thebn/3c guards for a simple polygon P with n verticesin O(n log n) time.

The birds eye view of the algorithm is as follows.

1. Decompose P into y-monotone pieces. (Definitionpending.)

2. Triangulate each y-monotone piece. This gives atriangulation of P .

3. 3-colour the vertices of P using the triangulation ofP .

4. Find the colour used least and place guards onvertices of that colour.

We need a data structure to store planar subdivisions.

Guarding and Triangulating Polygons 6

Page 8: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Doubly Connected Edge List (DCEL)

Each vertex (optional), edge, and face (optional) arerecords.

Each edge e = {a, b} is stored as two half edgese′ = (a, b) and e′′ = (b, a). The two half edges aretwins of each other and have pointers to each other.

Each half edge e′ when traversed consists of a faceface(e′) on its left. There is a link from e′ to face(e′)and vice versa.

Each half edge has a link to the next and previous

edges along the boundary of face face(e′).

Each vertex v has a pointer to one of its incident half

edges (v, v′). What about the rest?

How to delete/insert edges and traverse the subdivision?

Guarding and Triangulating Polygons 7

Page 9: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Decomposing P into y-monotone pieces

A y-monotone polygon is a simple polygon such thatits intersection with any line perpendicular to the y-axisis connected.

y-axis

To triangulate P , we first partition P into y-monotonesub-polygons (in O(n log n) time and then triangulatethe y-monotone pieces (again, in O(n log n) time.

Guarding and Triangulating Polygons 8

Page 10: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Classification of Vertices

A vertex at which, as we walk along the edges, thedirection of the edges change from downward to upwardor vice versa is called a turn vertex. A vertex at whichthe direction does not change is called a regular vertex.

The following figure illustrates 4 types of turn vertices.

v1

v2

v3

v4

v5

v6

v7

v8

v9

v10

v11

v12

v13

v14v15

e1

e2

e3e4e5

e6

e7e8

e9

e10

e11

e13

e14

e15

e12

= merge vertex

= regular vertex

= end vertex

= start vertex

= split vertex

Lemma 1. A simple polygon is y-monotone if it doesnot contain any split or merge vertex.

The proof is left as an exercise. While this lemma iseasy to “see,” a formal proof needs a bit of care.

Guarding and Triangulating Polygons 9

Page 11: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Plane Sweep Method

Let v1 be the rightmost vertex in P . Traverse theedges of P starting from v1 so that the polygon isalways to your left. In so doing, denote the ith vertexyou encounter as vi for all 1 < i ≤ n.

Denote edge vivi+1 as ei for 1 ≤ i < n. Denote edgevnv1 as en.

Let Left be the set of edges of P such that thepolygon lies to its right.

We sweep a line ` from top to bottom. The status ofthe sweep line ` at any given time is the edges in Leftthat ` intersects. They are stored in L→ R order in abalanced binary tree.

The events are the vertices of P and since they areknown up front, they can be stored in an array sortedaccording to the descending order of y coordinates.

Guarding and Triangulating Polygons 10

Page 12: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Let e be an edge in the status of `. We define the helperof e, denoted helper(e), as the lowest vertex above `such that the horizontal line segment connecting thevertex to e lies inside P .

Note: helper(e) may be the upper endpoint of e.(The figure below shows the helper of an edge ej.)

e j ekvi

`

ei−1 ei

helper(e j)

Given Lemma ??, we can focus our efforts oneliminating merge and split vertices.

Guarding and Triangulating Polygons 11

Page 13: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Pseudocode from BCKO1

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.1. Construct a priority queue Q on the vertices of P, using their y-coordinates as priority.

If two points have the same y-coordinate, the one with smaller x-coordinate has higherpriority.

2. Initialize an empty binary search tree T.3. while Q is not empty4. do Remove the vertex vi with the highest priority from Q.5. Call the appropriate procedure to handle the vertex, depending on its type.

7

HANDLESTARTVERTEX(vi)1. Insert ei in T and set helper(ei) to vi.

8

HANDLEENDVERTEX(vi)1. if helper(ei−1) is a merge vertex2. then Insert the diagonal connecting vi to helper(ei−1) in D.3. Delete ei−1 from T.

9

HANDLESPLITVERTEX(vi)1. Search in T to find the edge e j directly left of vi.2. Insert the diagonal connecting vi to helper(e j) in D.3. helper(e j)← vi4. Insert ei in T and set helper(ei) to vi.

10

1Computational Geometry: Algorithms and Applications, by de Berg,Cheong, van Krevald, and Overmars.

Guarding and Triangulating Polygons 12

Page 14: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

HANDLEMERGEVERTEX(vi)1. if helper(ei−1) is a merge vertex2. then Insert the diagonal connecting vi to helper(ei−1) in D.3. Delete ei−1 from T.4. Search in T to find the edge e j directly left of vi.5. if helper(e j) is a merge vertex6. then Insert the diagonal connecting vi to helper(e j) in D.7. helper(e j)← vi

11

HANDLEREGULARVERTEX(vi)1. if the interior of P lies to the right of vi2. then if helper(ei−1) is a merge vertex3. then Insert the diagonal connecting vi to helper(ei−1) in D.4. Delete ei−1 from T.5. Insert ei in T and set helper(ei) to vi.6. else Search in T to find the edge e j directly left of vi.7. if helper(e j) is a merge vertex8. then Insert the diagonal connecting vi to helper(e j) in D.9. helper(e j)← vi

12

Lemma 2. Algorithm MakeMonotone correctlyadds a set of non-intersecting diagonals thatpartitions P into monotone subpolygons.Theorem 4. A simple polygon with n vertices canbe partioned into y-monotone polygons in O(n log n)time with an algorithm that uses O(n) storage.

Proofs are left as exercises.

Guarding and Triangulating Polygons 13

Page 15: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

e j

vi ek

vm

diagonal will be addedwhen the sweep linereaches vm

v1v2

v3v4

v5

v6

v7

v8

v9

v10

v11

v12

v13

v14v15

e1

e2

e3e4e5

e6

e7

e8

e9

e10e11

e13

e14

e15

e12

Guarding and Triangulating Polygons 14

Page 16: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Triangulating a Monotone Polygon

Given a y-monotone polygon P , we want to triangulateit. We assume that P is given as a sequence of verticesordered anticlockwise.

From the given sequence, we can order the vertices in

decreasing order of their y-coordinates. How?

Once ordered, we pass through the vertices top →bottom.

As the algorithm progresses, a (connected) portion ofP will be untriangulated, while the rest is triangulated.

Even though we might have passed some of thevertices, they may still be on the boundary of theuntriangulated part of P .

Guarding and Triangulating Polygons 15

Page 17: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

not yettriangulated

trianglessplit off

As can be seen above, the untriangulated, but passedvertices form a geometric shape that has an invertedfunnel-like structure with the following properties.

Straight side. This is a “part” of an edge.

Concave side. The other side forms a sequence ofreflex vertices (i.e., vertices whose internal angle is> 180◦). This side might have triangulated piecessticking to it.

Guarding and Triangulating Polygons 16

Page 18: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

We will need a stack S, which is initially empty. Atany time, it stores the vertices of the untriangulatedpart. The vertices are stored in S such that if poppedone after another, we will get the lowest → hightestsequence.

If next vertex vj is on the straight side: We popvertices one-by-one and add diagonals from eachpopped vertex to v. The last vertex popped will bethe other side of the straight edge, so we don’t adda diagonal between the last vertex and v.

v j

popped

pushed

popped andpushed

e

Guarding and Triangulating Polygons 17

Page 19: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

If next vertex vj is on the concave side: We againpop vertices from the stack and add diagonals aslong as (i) we can add diagonals that don’t intersect2

edges of P and (ii) S is not empty.

If the diagonal to top vertex v′ in S intersects edgesof P , then do not pop v′. Furthermore, push thevertex that was popped immediately prior to v′ (ifit exists) back into the stack.

Finally, push vj into the stack as well.

pushed

v j

popped

pushed

v j

popped andpushed

popped andpushed

2How can this be tested in O(1) time?

Guarding and Triangulating Polygons 18

Page 20: Computational Geometry - Indian Institute of Technology Madrasaugustine/cs6110_odd2013/lectures/CG03_GuardingAnd... · Computational Geometry Guarding and Triangulating Polygons John

Algorithm TRIANGULATEMONOTONEPOLYGON(P)Input. A strictly y-monotone polygon P stored in a doubly-connected edge list D.Output. A triangulation of P stored in the doubly-connected edge list D.1. Merge the vertices on the left chain and the vertices on the right chain of P into one

sequence, sorted on decreasing y-coordinate. If two vertices have the same y-coordinate,then the leftmost one comes first. Let u1, . . . ,un denote the sorted sequence.

2. Initialize an empty stack S, and push u1 and u2 onto it.3. for j← 3 to n−14. do if u j and the vertex on top of S are on different chains5. then Pop all vertices from S.6. Insert into D a diagonal from u j to each popped vertex, except the last one.7. Push u j−1 and u j onto S.8. else Pop one vertex from S.9. Pop the other vertices from S as long as the diagonals from u j to them are

inside P. Insert these diagonals into D. Push the last vertex that has beenpopped back onto S.

10. Push u j onto S.11. Add diagonals from un to all stack vertices except the first and the last one.

13

Theorem 5. A strictly y-monotone polygon with nvertices can be triangulated in O(n) time.

Theorem 6. A simple polygon with n vertices can betriangulated in O(n log n) time.

Theorem 7. A planar subdivision with n vertices canbe triangulated in O(n log n) time.

Guarding and Triangulating Polygons 19