what is computational geometry? study of data structures and algorithms for geometric problems 1970...
TRANSCRIPT
What is Computational Geometry?
• Study of Data Structures and Algorithms for Geometric Problems
• 1970’s: Need for Computational Geometry Recognized; Progress made on 1 and 2
• Today: “Mastered” 1 and 2, not so successful with 3
Problems inApplicationDomains
SpecificGeometrical
Problems
Algorithms toSolve Geometric
Problems
formulate design
modify methods for original problems(implementation)
1
3
2
Application Domains
• Computer Graphics and Virtual Reality– 2-D & 3-D: intersections, hidden surface elimination, ray tracing– Virtual Reality: collision detection (intersection)
• Robotics– Motion planning, assembly orderings, collision detection,
shortest path finding • Global Information Systems (GIS)
– Large Data Sets data structure design– Overlays Find points in multiple layers– Interpolation Find additional points based on values of known
points– Voronoi Diagrams of points
Application Domains continued
• Computer Aided Design and Manufacturing (CAD / CAM)
– Design 3-D objects and manipulate them• Possible manipulations: merge (union), separate, move
– “Design for Assembly”• CAD/CAM provides a test on objects for ease of assembly,
maintenance, etc.
• Computational Biology
– Determine how proteins combine together based on folds in structure
• Surface modeling, path finding, intersection
Example: 2-D Convex Hulls
• Definitions
– A subset P of the plane is convex iff for every p,q P line segment pq is completely contained in P.
– The convex hull of a set P, denoted CH(P), is the smallest convex set containing P.
• The intersection of all convex sets containing P.
• Analogy: If the points are nails in a board, a rubber band (convex hull) encloses all of the nails in the board.
• Alternative Definition: CH(P) is the unique convex polygon whose vertices are points of P that contains all points in P
Geometry
Examples of Non-Euclidean Geometry
Computational Geometry• Inclusion problems:
– locating a point in a planar subdivision,– reporting which point among a given set are contained in a spec
ified domain, etc.• Intersection problems:
– finding intersections of line segments, polygons, circles, rectangles, polyhedra, half spaces, etc.
• Proximity problems:– determining the closest pair among a set of given points,– computing the smallest distance from one set of points to anoth
er.• Construction problems:
– identify the convex hull of a polygon,– obtaining the smallest box that includes a set of points, etc.
Elementary Geometric Methods• Easy to visualize, hard to implement (but sometimes it wi
ll lead to much more efficient algorithms than other methods).
• A graph is planar if it can be drawn in the plane so that no two of its edges intersect.
• A planar subdivision is the drawing of a planar graph in straight-line segments.
• A polygon is simple if no two edges of it intersect.• Two polygons Q and R are said to be intersect if an edge
of Q cross an edge of R.• Basic geometric data structures:
type point = record x, y: integer end; line = record p1, p2: point end;var polygon: array[0..Nmax] of point;
Line Segment Properties• Two distinct points: p1=(x1,y1), p2=(x2,y2)
• Define a new point p3 = ap1+(1-a)p2, where 0 a 1
– p3 is any point on the line between p1 and p2
– p3 is a convex combination of p1 and p2
• line segment p1p2 is the set of convex combinations of p1 and p2.
– p1 and p2 are the endpoints of segment p1p2
p1
p2
p3
x
y
Cross Products• Can solve many geometric problems using the cross product.
• Two points: p1=(x1,y1), p2=(x2,y2)
• Cross product of the two points is defined by
p1 x p2 = the determinant of a matrix x1 x2
y1 y2
= x1y2 – x2y1
= the signed area of the parallelogram of four points: (0,0), p1, p2, p1+p2
• What happens if p1 x p2 = 0 ?
• If p1 x p2 is positive then p1 is clockwise from p2
• If p1 x p2 is negative then p1 is counterclockwise from p2
p1(x1,y1)
p2(x2,y2)p1+p2
x
y
Clockwise or Counterclockwise• Problem definition
– Determine whether a directed segment p0p1 is clockwise from a directed segment p0p2 w.r.t. p0
• Solution
1. Map p0 to (0,0), p1 to p1’, p2 to p2’
– p1’ = p1 – p0, p2’ = p2 – p0
2. If p1’ x p2’ > 0 then the segment p0p1 is clockwise from p0p2
3. else counterclockwise
p0
p1
p2
(a) p0p2 is counterclockwise
from p0p1: (p2-p0)x(p1-p0) < 0
p0
p2
p1
(b) p0p2 is clockwise from p0p1:
(p2-p0)x(p1-p0) > 0
Turn Left or Turn Right
• Problem definition
– Determine whether two consecutive line segments p0p1 and p1p2 turn left or right at the common point p1.
• Solution
– Determine p0p2 is clockwise or counterclockwise from p0p1 w.r.t. p0
– If counterclockwise then turn left at p0
– else turn right at p0
p0
p1
p2
(a) Turn left at p1
(p2-p0)x(p1-p0) < 0
p0
p2
p1
(b) Turn right at p1
(p2-p0)x(p1-p0) > 0
Intersection of 2 Line Segments
Two Segments Intersect (2)• Five cases
p1
p4
p2
(a) p1p2, p3p4 intersect
p3
p1
p2
p4
(b) p1p2, p3p4 do not intersect
p3
p1
p2
p4
(c) p1p2, p3p4 intersect
p3
p1
p2
p4
(d) p1p2, p3p4 intersect
p3
p1
p3
p4
(e) p1p2, p3p4 do not intersect
p2
Two Segments Intersect (3)• Consider two cross products: 1. (p3 – p1) x (p2 – p1)
2. (p4 – p1) x (p2 – p1)
p1
p4
p2
(a) p1p2, p3p4 intersect:
(p3 – p1) x (p2 – p1) < 0
(p4 – p1) x (p2 – p1) > 0
p3
p1
p2
p4
(b) p1p2, p3p4 do not intersect:
(p3 – p1) x (p2 – p1) < 0
(p4 – p1) x (p2 – p1) < 0
p3
Two Segments Intersect (4)
p1
p2
p4
(c) p1p2, p3p4 intersect
(p3 – p1) x (p2 – p1) < 0
(p4 – p1) x (p2 – p1) = 0
p3
p1
p2
p4
(d) p1p2, p3p4 intersect
(p3 – p1) x (p2 – p1) = 0
(p4 – p1) x (p2 – p1) = 0
p3
Two Segments Intersect (5)
• Case (e)
– What are the cross products ?• (p3 – p1) x (p2 – p1) = 0
• (p4 – p1) x (p2 – p1) = 0
– The cross products are zero’s, but they do not intersect
– Same result with Case (d)
p1
p3
p4
(e) p1p2, p3p4 do not intersect
p2
Bounding Boxes• Definition
– Given a geometric object, the bounding box is defined by the smallest rectangle that contains the object
• Given two line segments p1p2 and p3p4
• The bounding box of the line segment p1p2
– The rectangle with lower left point=(x1’,y1’)=(min(x1,x2), min(y1,y2)) and
– upper right point=(x2’,y2’)= (max(x1,x2), max(y1,y2))
• The bounding box of the line segment p3p4 is
– The rectangle with lower left point=(x3’,y3’)=(min(x3,x4), min(y3,y4)) and
– upper right point=(x4’,y4’)=(max(x3,x4), max(y3,y4))
p1
p3
p4
p2
Bounding Boxes
• What is the condition for the two bounding boxes intersect?
– (x3’ x2’) and (x1’ x4’) and (y3’ y2’) and (y1’ y4’)
• If two bounding boxes do not intersect, then the two line segments do not intersect.
• But it is not always true that
– if two bounding boxes intersect, then the two line segments intersect
– e.g., the figure
p1
p3
p4
p2
Two Segments Intersect (6)• Case summary
– (a) p1p2, p3p4 intersect• (p3 – p1) x (p2 – p1) < 0• (p4 – p1) x (p2 – p1) > 0
– (b) p1p2, p3p4 do not intersect• (p3 – p1) x (p2 – p1) < 0• (p4 – p1) x (p2 – p1) < 0
– (c) p1p2, p3p4 intersect • (p3 – p1) x (p2 – p1) < 0• (p4 – p1) x (p2 – p1) = 0
– (d) p1p2, p3p4 intersect • (p3 – p1) x (p2 – p1) = 0• (p4 – p1) x (p2 – p1) = 0
– (e) p1p2, p3p4 do not intersect • (p3 – p1) x (p2 – p1) = 0
• (p4 – p1) x (p2 – p1) = 0
Two Segments Intersect - Algorithm
• Two_line_segments_intersect (p1p2, p3p4)
1. (x1’,y1’)=(min(x1,x2), min(y1,y2))
2. (x2’,y2’)=(max(x1,x2), max(y1,y2))
3. (x3’,y3’)=(min(x3,x4), min(y3,y4))
4. (x4’,y4’)=(max(x3,x4), max(y3,y4))
5. if not ((x3’ x2’) and (x1’ x4’) and (y3’ y2’) and (y1’ y4’)) then
6. return false // case (e) and more
7. else
8. if (p3–p1) x (p2–p1) or (p4–p1) x (p2–p1) are zero then
9. return true // case (c) and (d)
10. else if (p3–p1) x (p2–p1) and (p4–p1) x (p2–p1) have different sign then
11. return true // case (a)
12. else
13. return false // case (b)
Line Segment Intersection Problem• The Problem: Given n line segments, is any pair of them i
nsect?• Clearly, doing pairwise intersection testing takes O(n2) ti
me. By a sweeping technique, we can solve it in
O(n log n) time (without printing all the intersections).• In the worst case, there are (n2) intersections.• Simplifying assumptions
– No input segment is vertical.– No three input segments intersect at a single point.
Problem Definition:
Input : S={s1, s2, …, sn} of n segments in plane.
Output: set I of intersection points among segments in S. (with segments containing each intersection pt)
How many intersections possible?
In worst case, )(2
2nn
Problem Definition
Algorithm: Brute Force Intersect(S)
Test all pairs (si, sj)SXS, i not equal j.
If si intersects sj, report si intersects sj.
Complexity
-There may be much fewer intersections.
-Can we spend less time (depending on |I|) ?
Output sensitive:
-Running time depends on:
n: #of input segments
k: size of the output
)( 2n
Brute Force Intersect Algorithm
What would be the best time?
O(n+k)?
Actually a lower bound of (nlogn+k) is known.
Goal:
An output-sensitive algorithm that avoids testing pairs of segments for intersection if they are “far apart”.
Goal
Idea:
Project segments onto y-axis.
1. If si & sj intersect, then so do their y-intervals (e.g. s1 & s2 ).
2. If the y-intervals of si & sj don’t intersect, then neither do si & sj (e.g. s1 & s3 )
3. If the y-intervals of si & sj do intersect, then maybe si & sj intersect (e.g. s3 & s4 )
we may still test extra, but better than before.
y
x
S1
S2
S4
S3
Idea
Plane Sweep Algorithm:
L is horizontal sweep line initially above all segments.
Sweep L down over segments, and keep track of all segments intersecting it.
Status T of sweep line is the set of segments currently intersecting L .
Events are points where status changes.At each event point
Update status of sweep line: add/remove segments from TPerform intersection tests
Plane Sweep Algorithm
Ordering Segments• Two nonintersecting segments s1 and s2 are comparable at
x if the vertical sweep line with x‑coordinate x intersects both of them.
• s1 is above s2 at x, written s1 >x s2, if s1 and s2 are comparable at x and the intersection of s1 with the sweep line at x is higher than the intersection of s2 with the same sweep line.
Example
Moving the Sweep Line• Sweeping algorithms typically manage two sets of data:
1. The sweep‑line status gives the relationships among the objects intersected by the sweep line.
2. The event‑point schedule is a sequence of x‑coordinates, ordered from left to right, that defines the halting positions (event points) of the sweep line. Changes to the sweep‑line status occur only at event points.
• The event‑point schedule can be determined dynamically as the algorithm progresses or determined statically, based solely on simple properties of the input data.
Moving the Sweep Line• Sort the segment endpoints by increasing
x‑coordinate and proceed from left to right. • Insert a segment into the sweep‑line status when its left
endpoint is encountered, and delete it from the sweep‑line status when its right endpoint is encountered.
• Whenever two segments first become consecutive in the total order, we check whether they intersect.
Moving the Sweep Line• The sweep‑line status is a total order T. for which we
require the following operations:– INSERT(T, s): insert segment s into T.– DELETE(T, s): delete segment s from T.– ABOVE(T, s): return the segment immediately above segment
s in T.– BELOW(T, s): return the segment immediately below segment
s in T.
• If there are n segments in the input, we can perform each of the above operations in O(log n) time using red‑black trees.
• Replace the key comparisons by cross‑product comparisons that determine the relative ordering of two segments.
Segment‑intersection Pseudocode
Simple Closed Path• Problem: Finding a tour through aset of n given points. • Observation: Restricted TSP.• Algorithm:
1. Pick an "anchor" point. 2. Compute the angle made by drawing a line from eac
h of the points in the set to the anchor and then out in the positive horizontal direction.
3. Sort the points according to the angles computed in step 2.
4. Connect adjacent points.• Complexity: O(n log n).
Illustration
Computing the Angle• angle = tan-1(dy/dx).• It make sense to use a function that is much easier to co
mpute but has the same ordering property as the arctangent.function theta(p1, p2: point): real;var dx, dy, ax, ay: integer; t: real;begin dx := p2.x - p1.x; ax := abs(dx); dy := p2.y - p1.y; ay := abs(dy); if ax+ay = 0 then t := 0 else t := dy/(ax+ay); if dx < 0 then t := 2 - t else if dy < 0 then t := 4 + t; theta := t*90.0; /* returns a no. between 0 and 360 */end;
Whether a Point is Inside
• Problem definition
– Given a simple polygon P and a point q, determine whether the point is inside or outside the polygon.
• The polygon can be a non-convex polygon
• Idea
– Draw a line from the point to a point outside of the polygon, count the number of intersections with the edges of the polygon.
– The point is inside the polygon iff the number of intersections is odd
Whether a Point is Inside (2)
• Point_in_polygon(P, q)
// P: polygon with vertices p1, p2, .., pn, edges e1, e2, .., en
// q: point
1. pick an arbitrary point s outside the polygon
2. count = 0
3. for all edges ei of the polygon do
4. if ei intersects the line segment qs then
5. count++
6. if count is odd then return true
7. else return false
• Complexity
– O(n) when n = size of the polygon
Whether a Point is Inside (3)
• Assume special cases don’t occur
– Case (a) and (b)
• Observation
– Is it indeed important to find an optimal line (i.e., the line segment qs) that minimizes the number of intersections?
– No. Finding the number of intersections is more important
• Idea: Can use the vertical line that pass the point q
q
s(a) should not count (b) should count
q
s
Whether a Point is Inside (4)
• Point_in_polygon_2(P, q)
// P: polygon with vertices p1, p2, .., pn, edges e1, e2, .., en
// q: point (x0, y0)
1. count = 0
2. for all edges ei of the polygon do
3. if ei intersects the line x=x0 then
4. yi = y-coordinate of the intersection
5. if yi < y0 then count++ // this can be yi > y0
6. if count is odd then return true
7. else return false
• Complexity
– O(n) when n = size of the polygon
Finding the Convex Hull• A convex hull of n given points is defined as the smallest
convex polygon containing them all
• Computing Convex hull is closed related to sorting. (Why?)
Idea
Method 1: Package Wrapping1. Find an anchor point (say, the one with the smallest y va
lue).2. Take a horizontal ray in the positive direction, and swee
p it upward until hitting another point, this point must be on the hull.
3. Anchor at the newly found point and continue sweeping until hitting another point.
4. Repeat step 3 until the "package" is fully "wrapped"
• Time complexity: If there are m vertices on the hull, then package wrapping technique requires about mn steps: (n-1) + (n-2) + ... + (n-m) = mn - m(m+1)/2.
Method 2: The Graham Scan1. Construct a simple closed path from the given n points.2. Select an extreme point, start a scan on all the points. If
an angle ABC is found to be reflex, Eliminate point B from the convex hull.
• Time complexity: O( n lg n). After the sort, the Graham scan is a linear-time process.
Method 3: Interior Elimination
• The interior elimination method is linear, on the average.
Method 4: A D&C approach
D&P Approach Continued
• MERGE takes O(n) time.• t(n) = 2t(n/2) + cn = O(nlog n).
Reduction Technique:
Correctness
Correctness
Maximal Polygonal Distance
• Only has to consider the distances between vertices.• Straightforward algorithm: O(n2)• Clockwise traversal: O(n)