66
15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Embed Size (px)

Citation preview

Page 1: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

15-211Fundamental Data Structures and Algorithms

Margaret Reid-Miller

26 April 2005

Computational Geometry

Page 2: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Announcements

HW6 the clock is ticking ...

FCEs

Final Exam on Thursday May 5, 8:30am-11:30amreview session April 28

Page 3: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

The Basics

Page 4: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Computational Geometry

Lots of applications:

computer graphics CAD/CAM motion planning maps simulation

Could be done in dimension D, but we will stick to D=2.

D=3 is already much harder.

Page 5: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Airplane wing triangulation

Page 6: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

QuickTime™ and aYUV420 codec decompressor

are needed to see this picture.

Moving Mesh

Page 7: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

The Basics

What are the basic objects in plane geometry, how do we represent them as data structures?

- point two floats

- line two points

- ray two points, constraints

- line segment two points, constraints

and triangles, rectangles, polygons, ...

Never mind curves such as circles and ellipses.

Page 8: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Disclaimer

There are two annoying issues one has to confront in Computational Geometry algorithms.

- Degenerate Cases

Points are collinear, lines parallel, …

Dealing with degeneracy is a huge pain, not to mention boring.

- Floating point numbers are not reals. Floating point arithmetic introduces errors and hence leads to accuracy problems.

Page 9: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

High Precision Arithmetic

If 32 or 64 bits are not enough, use 128, or 512, or 1024, or …

Problems:

We have to be able to analyze how many bits are required.

Slows down computation significantly.

See GNU gmp.

Page 10: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Interval Arithmetic

Replace “real number” x by an interval

xL x xU

Problems:

Arithmetic becomes quite a bit more complicated.

Slows down computation significantly.

Page 11: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Symbolic Computation

Use (arbitrary precision) rationals and whatever algebraic operations that are necessary symbolically: compute with the formal expressions, not their numerical values.

Sqrt[2]

Problems:

Manipulating these expressions is complicated.

Slows down computation significantly.

Page 12: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Warm-Up

Let's figure out how to check if a point P lies on a line L.

P = (p1,p2)

L = (A,B) two-points representation

Need

P = A + (B - A)

where is a real parameter.

A

B

P

P

Page 13: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Linear Equations

The vector equation

P = A + (B – A) = (1- ) A + B

is just short-hand for two linear equations:

p1 = a1 + (b1 – a1) p2 = a2 + (b2 – a2)

Will have a common solution if P lies on L.

Page 14: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Subroutines

Solving systems of equations such as

p1 = a1 + (b1 – a1) p2 = a2 + (b2 – a2)

is best left to specialized software: send all linear equation subproblems to an equation solver - that will presumably handle all the tricky cases properly and will produce reasonable accuracy.

Page 15: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

What if L =[A,B) is the ray from A through B?

No problem. Now we need

P = A + (B - A)

where is a non-negative real.

Likewise, if L = [A,B] is the line segment from A to B we need

0 1.

Both and (1 – ) must be non-negative.

Rays and Line Segments

A

B

A

B

Page 16: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Intersections

How do we check if two lines intersect?

A + (B - A) = C + (D – C)

Two linear equations, two unknowns ,

Add constraints for intersection of rays, line segments.

A

D

CB

Page 17: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

General Placement

Here is a slightly harder problem:

Which side of L is the point P on?

A

B

right

lefton

P

P

P

Page 18: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Left/Right Turns

March from A to B, then perform a left/right turn.

A

B

right

lefton

Page 19: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Calculus: Cross Product

The cross product of two (3-D) vectors is another vector perpendicular to the given ones

Z = X Y

Length and direction of Z express the signed area of the parallelogram spanned by the vectors.

antisymmetric:

X Y = - Y X

Page 20: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

In 2-D the analog to the cross product of vectors X=(x1, x2) and Y=(y1, y2), is the determinant

| x1 x2 | | y1 y2 |

which is the (signed) area of the parallelogram spanned by X and Y.

Calculus: determinants

(x1,x2)

(y1,y2)

= x1 y2 – x2 y1

Page 21: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Determinants and left/right turns

Let P=(p1,p2), A=(a1,a2), B=(b1, b2)

If the area of the parallelogram spanned by B-A and P-A is

0: then P is on the line (A, B)>0: then P is to the left of (A, B)<0: then P is to the right of (A,B)

where the area is the determinant

|b1-a1, b2-a2| |p1-a1, p2-a2|

B = (b1,b2)

P = (p1,p2)

A = (a1,a2)

D =

Page 22: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Determinants

Alternatively one can compute the determinant of the matrix

a1 a2 1b1 b2 1p1 p2 1

This may be a better solution since presumably the determinant subroutine deals better with numerical problems.

Page 23: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Some Applications

Page 24: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Membership

How do we check if a point X belongs to some region R in the plane (triangle, rectangle, polygon, ... ).

XR

X

R X

X

Page 25: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Membership

Clearly the difficulty of a membership query depends a lot on the complexity of the region. For some regions it is not even clear that there is an inside and an outside.

Page 26: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Simple Polgyons

A polygon is simple if its lines do not intersect except at the vertices of the polygon (and then only two).By the Jordan Curve Theorem any simple polygon partitions the plane into inside and outside.

Page 27: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Point Membership

The proof of the JCT provides a membership test. Let P be a simple polygon and X a point. Draw a line through X and count the intersections of that line with the edges of P.

Page 28: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Odd

One can show that X lies inside of P iff the number of intersections (on either side) is odd.

Note that there are bothersome degenerate cases:X lies on an edge, an edge lies on the line through X.Maybe wiggle the line a little.

Page 29: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Algorithm

Theorem: One can test in linear time whether a point lies inside a simple polygon.

Linear here refers to the number of edges of the polygon.

And, of course, we assume that these edges are given as a list of points, say, in counterclockwise order.

Page 30: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Convexity

Here is one type of simple region.

A region R in the plane is convex if for all A, B in R: the line segment [A,B] is a subset of R.

In particular convex polygons: boundary straight line segments.

Page 31: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Membership in Convex Polygon

Traverse the boundary of the convex polygon in counterclockwise order, check that the point in question is always to the left of the corresponding line.

This is clearly linear in the number of boundary points.

Page 32: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Intersections of LSs

Suppose we are given a collection of LSs and want to compute all the intersections between them.

Note that the number of intersections may be quadratic in the number n of LSs.

The brute force algorithms is (n2): consider all pairs of LSs and check each pair for intersection.

Bad if there are only a few intersections.

Page 33: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Use of the Geometry

Suppose there are s intersections. We would like a faster algorithm with running time O( (n+s) ??? ) where ??? is small. Such an algorithm is called an output-sensitive algorithm.

How do we avoid testing all pairs of segments for intersections? Use the geometry: segments close together are candidates for intersections while segments far apart are not.

x

Page 34: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Sweep Line

The key idea is to use a sweep line: Moves from left to right.

Maintain the set of line segments that intersect with the sweep line.

When does the set change? These points are called event points.

Page 35: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Sweep Line

When the sweep line reaches the left endpoint of a segment, the segment must be added to the set and tested for intersection with other segments in the set.

When the sweep line reaches the right endpoint the segment can be deleted from the set.

Still quadratic, even if s is small, though. Can you think of an example?

Page 36: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Neighbors

Notice how a segment needs to consider only its immediate neighbors wrt to the sweep line.

Sort the segments from bottom to top as they intersect the sweep line.

q < r < s < t

s

r

t

q

Page 37: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

SL Operations

The SL has to keep track of all the LSs that currently intersect it. So we need operations

insert(s)delete(s)

where s is a given LS.

The “time” when the segments are inserted/deleted are given by the endpoints of the input LSs.

(Static events, preprocess by sorting.)

Page 38: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Detecting Intersections

When a new LS is entered, we have to check if it interacts with any of the neighboring LSs. So we need additional operations

above(s)below(s)

which return the immediate neighborsabove/below on the SL.

Page 39: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Sweep line operations

What is an appropriate data structure for these operations?

insert(s)delete(s)

above(s) below(s)

Page 40: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Sweep line operations

What is an appropriate data structure for these operations?

insert(s)delete(s)

above(s) below(s)

Answer: a dictionary, like balanced binary trees.

Then each operation can be done in O(log n) time.

May want a threaded tree to get O(1) above/below operations.

Page 41: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Detecting Intersections

Notice that the segment ordering changes at intersection points.

Each new intersection becomes a new event point, assuming it was not detected earlier, which can occur if the segment pair were adjacent before.

These intersections are dynamic events points, not known ahead of time.

When two segments change their order, each may get one new neighbor against which it needs to test for an intersection.

Page 42: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Events

Only consider possible lines at:

segment endpoints- known initially

intersections- insert dynamically

Need to be able to remove next event.

Page 43: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Events

• What is an appropriate data structure to keep the positions of the sweep line?

Answer: priority queue!

Page 44: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Sweep algorithm

Insert all segment endpoints into the queue Q While Q not empty, extract next event E

If E = segment left point, insert segment into SL dictionary based on its y-coordinate. Test for intersection with segment immediately above and below.

If E = segment right point, delete from SL dictionary. Test for intersection the segments immediately preceding and succeeding this one.

If E = intersection, swap the intersecting segments in the SL dictionary. Test the new upper segment for intersection with predecessor. Test the new lower segment for intersection with successor.

Theorem: Can compute all s intersections of n line segments in O((n+s) log n) time steps.

Page 45: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Testing Simplicity

How do we check if a polygon is simple?

Use the line segment intersection algorithm on the edges.

Page 46: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Polgyon Intersection

How do we check if two simple polygon intersect?

Note: two cases!

Page 47: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Polgyon Intersection

The first case can be handled with LS intersection testing.

For the second, pick any vertex in P and check wether it lies in (the interior of) Q.

Total running time: O(n log n).

Page 48: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Convex Hulls

Page 49: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

A Hull Operation

Suppose P is a set of points in the plane. The convex hull of P is the least set of points Q such that:

- P is a subset of Q,- Q is convex.

Written CH(P).

This pattern should look very familiar by now

(reachability in graphs, equivalence relations, ...)

Page 50: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Convex Combinations

Abstractly it is easy to describe CH(P): is the set of all points

X = i Pi

where 0 i and i = 1.

X is a convex combination of the Pi .

So the convex combinations of A and B are the line segment [A,B].

And the convex combinations of three points (in general position) form a triangle. And so on.

Page 51: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

So?

Geometrically this is nice, but computationally this characterization of the convex hull is not too useful.

We want an algorithm that takes as input a simple polygon P and returns as output the simple polygon CH(P).

P

CH(P)

Page 52: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Extremal Points

Point X in region R is extremal iff X is not a convex combination of other points in R.

For a polygon, the extremal points make up the CH.

Page 53: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

CH Algorithms

Let us assume that the input is given as a set (list) P of n points p1 ,p2, ...,pn in the plane. Example, 1.223, 2.322 8.344, 21.232 -21.222, 38.233 21.832, 12.780 14.435, 31.023 29.439, 29.532 29.982, 2.879 -6.872, 35.671

We need to compute the extremal points and ouput them in some natural order (say, traversing the boundary of the hull counterclockwise, starting at the south-east corner).

However, the input may not be ordered in any particular way.

Page 54: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

An Observation

So let's deal with a set of points P rather than regions.

Lemma: Point X in P is not extremal iff X lies in the interior of a triangle spanned by three other points in P.

Page 55: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

An Algorithm

Hence we can find the convex hull of a simple polygon: for all n points we eliminate non-extremal points by trying all possible triangles using the membership test for convex polygons.

In the end we sort the remaining extremal points in counterclockwise order.

Unfortunately, this is O(n4).

Could it be faster? When do we get n4?

Page 56: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Computation and Physics

Note that there is a simple analogue algorithm to solve the convex hull problem: use a board, nails and a rubber band.

Is linear (analog) time.

Page 57: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Extremal Points

It is easy to find a few points guaranteed to be on the convex hull.

p0

Page 58: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Quick Hull Idea

Find two points p and q such that [p,q] is a chord in the convex hull of P. Split the points into “above” and “below” the baseline [p,q].

Page 59: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Upper Half

Find the point r furthest from the baseline. Discard all points in the triangle (p,q,r).

Page 60: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Repeat

In the end only the extremal points are left over.

Page 61: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Comments

The divide step is clearly linear (in the number of points on that side).

Combining the solutions is trivial here: we can just join the lists of extremal points.

This is very similar to Quicksort.

Page 62: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Analysis

When does this approach work well?

When does it work poorly?

What is the running time in general?

What is average running time on uniformly random points?

Can we force fast behavior?

Page 63: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Analysis

When does this approach work well? Many points on the interior.

When does it work poorly? All points on the hull and imbalanced splits O( n2)

What is the running time in general? O( n log n)

What is average running time on uniformly random points? O( n )

Can we force fast behavior?

Page 64: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Lower Bound for CH

O( n log n ) is optimal for convex hull.

Show that Sorting can be reduced to Convex Hull.

Consider n positive real numbers xi.

Define pi = (xi,xi2).

Easy to see: are all extremal. Traversing the boundary of CH(P) produces a sorted list.

Page 65: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Application: Membership Test

The output convention makes algorithmic sense: we can then test in logarithmic time whether a point lies in the convex region.

Q

Q

Page 66: 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 26 April 2005 Computational Geometry

Membership Test c't

Find central point p, then use binary search to identify the sector that Q belongs to: between rays [p,pi) and [p,pi+1).

Check if Q lies in the corresponding triangle.

p

Q

Qpi+1

pi