1.2k likes | 2.04k Views
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. Application Domains. Computer Graphics and Virtual Reality
E N D
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
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
Computational Geometry • Inclusion problems: • locating a point in a planar subdivision, • reporting which point among a given set are contained in a specified 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 another. • 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 will 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 y p2 p3 p1 x
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 y p1+p2 p2(x2,y2) p1(x1,y1) x
Clockwise or Counterclockwise • Problem definition • Determine whether a directed segment p0p1 is clockwise from a directed segment p0p2 w.r.t. p0 • Solution • Map p0 to (0,0), p1 to p1’, p2 to p2’ • p1’ = p1 – p0, p2’ = p2 – p0 • If p1’ x p2’ > 0 then the segment p0p1 is clockwise from p0p2 • else counterclockwise p2 p1 p1 p2 p0 p0 (a) p0p2 is counterclockwise from p0p1: (p2-p0)x(p1-p0) < 0 (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 p2 p1 p1 p2 p0 p0 • Turn left at p1 • (p2-p0)x(p1-p0) < 0 (b) Turn right at p1 (p2-p0)x(p1-p0) > 0
Two Segments Intersect (2) • Five cases p2 p4 p3 p3 p4 p2 p1 p1 • p1p2, p3p4 intersect (b) p1p2, p3p4 do not intersect p3 p4 p4 p2 p2 p3 p2 p4 p3 p1 p1 p1 (c) p1p2, p3p4 intersect (d) p1p2, p3p4 intersect (e) p1p2, p3p4 do not intersect
Two Segments Intersect (3) • Consider two cross products: • (p3 – p1) x (p2 – p1) • (p4 – p1) x (p2 – p1) p2 p4 p3 p3 p4 p2 p1 p1 • 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
Two Segments Intersect (4) p3 p4 p2 p2 p4 p3 p1 p1 (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
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) p4 p3 p2 p1 (e) p1p2, p3p4 do not intersect
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)) p4 p3 p2 p1
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 p4 p2 p3 p1
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) • (x1’,y1’)=(min(x1,x2), min(y1,y2)) • (x2’,y2’)=(max(x1,x2), max(y1,y2)) • (x3’,y3’)=(min(x3,x4), min(y3,y4)) • (x4’,y4’)=(max(x3,x4), max(y3,y4)) • if not ((x3’ x2’) and (x1’ x4’) and (y3’ y2’) and (y1’ y4’)) then • return false // case (e) and more • else • if (p3–p1) x (p2–p1) or (p4–p1) x (p2–p1) are zero then • return true // case (c) and (d) • else if (p3–p1) x (p2–p1) and (p4–p1) x (p2–p1) have different sign then • return true // case (a) • else • return false // case (b)
Line Segment Intersection Problem • The Problem: Given n line segments, is any pair of them insect? • Clearly, doing pairwise intersection testing takes O(n2) time. 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 W(n2) intersections. • Simplifying assumptions • No input segment is vertical. • No three input segments intersect at a single point.
Problem Definition 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,
Brute Force Intersect Algorithm 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
Goal 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”.
Idea 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.
Plane Sweep Algorithm Plane Sweep Algorithm: Lis 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 T Perform intersection tests
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.
Moving the Sweep Line • Sweeping algorithms typically manage two sets of data: • The sweep‑line status gives the relationships among the objects intersected by the sweep line. • 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.
Simple Closed Path • Problem: Finding a tour through aset of n given points. • Observation: Restricted TSP. • Algorithm: • Pick an "anchor" point. • Compute the angle made bydrawing a line from each of thepoints in the set to the anchor andthen out in the positive horizontaldirection. • Sort the points according to theangles computed in step 2. • Connect adjacent points. • Complexity: O(n log n).
Computing the Angle • angle = tan-1(dy/dx). • It make sense to use a function thatis much easier to compute but hasthe same ordering property as thearctangent. 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 • pick an arbitrary point s outside the polygon • count = 0 • for all edges ei of the polygon do • if ei intersects the line segment qs then • count++ • if count is odd then return true • 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 q s s (b) should count (a) should not count
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) • count = 0 • for all edges ei of the polygon do • if ei intersects the line x=x0 then • yi = y-coordinate of the intersection • if yi < y0 then count++ // this can be yi > y0 • if count is odd then return true • 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?)
Method 1: Package Wrapping • Find an anchor point (say, the one with the smallest y value). • Take a horizontal ray in the positive direction, and sweep it upward until hitting another point, this point must be on the hull. • Anchor at the newly found point and continue sweeping until hitting another point. • 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 Scan • Construct a simple closed path from the given n points. • 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.