Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is Computational Geometry? Study of Data Structures and Algorithms for Geometric Problems 1970 ’ s: Need for Computational Geometry Recognized; Progress.

Similar presentations


Presentation on theme: "What is Computational Geometry? Study of Data Structures and Algorithms for Geometric Problems 1970 ’ s: Need for Computational Geometry Recognized; Progress."— Presentation transcript:

1 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

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

3 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

4 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

5 Geometry

6 Examples of Non-Euclidean Geometry

7 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.

8 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;

9 Line Segment Properties Two distinct points: p 1 =(x 1,y 1 ), p 2 =(x 2,y 2 ) Define a new point p 3 = ap 1 +(1-a)p 2, where 0  a  1 –p 3 is any point on the line between p 1 and p 2 –p 3 is a convex combination of p 1 and p 2 line segment p 1 p 2 is the set of convex combinations of p 1 and p 2. –p 1 and p 2 are the endpoints of segment p 1 p 2 p1p1 p2p2 p3p3 x y

10 Cross Products Can solve many geometric problems using the cross product. Two points: p 1 =(x 1,y 1 ), p 2 =(x 2,y 2 ) Cross product of the two points is defined by p 1 x p 2 = the determinant of a matrix x 1 x 2 y 1 y 2 = x 1 y 2 – x 2 y 1 = the signed area of the parallelogram of four points: (0,0), p 1, p 2, p 1 +p 2 What happens if p 1 x p 2 = 0 ? If p 1 x p 2 is positive then p 1 is clockwise from p 2 If p 1 x p 2 is negative then p 1 is counterclockwise from p 2 p 1 (x 1,y 1 ) p 2 (x 2,y 2 ) p 1 +p 2 x y

11 Clockwise or Counterclockwise Problem definition –Determine whether a directed segment p 0 p 1 is clockwise from a directed segment p 0 p 2 w.r.t. p 0 Solution 1.Map p 0 to (0,0), p 1 to p 1 ’, p 2 to p 2 ’ –p 1 ’ = p 1 – p 0, p 2 ’ = p 2 – p 0 2.If p 1 ’ x p 2 ’ > 0 then the segment p 0 p 1 is clockwise from p 0 p 2 3.else counterclockwise p0p0 p1p1 p2p2 (a) p 0 p 2 is counterclockwise from p 0 p 1 : (p 2 -p 0 )x(p 1 -p 0 ) < 0 p0p0 p2p2 p1p1 (b) p 0 p 2 is clockwise from p 0 p 1 : (p 2 -p 0 )x(p 1 -p 0 ) > 0

12 Turn Left or Turn Right Problem definition –Determine whether two consecutive line segments p 0 p 1 and p 1 p 2 turn left or right at the common point p 1. Solution –Determine p 0 p 2 is clockwise or counterclockwise from p 0 p 1 w.r.t. p 0 –If counterclockwise then turn left at p 0 –else turn right at p 0 p0p0 p1p1 p2p2 (a)Turn left at p 1 (p 2 -p 0 )x(p 1 -p 0 ) < 0 p0p0 p2p2 p1p1 (b) Turn right at p 1 (p 2 -p 0 )x(p 1 -p 0 ) > 0

13 Intersection of 2 Line Segments

14 Two Segments Intersect (2) Five cases p1p1 p4p4 p2p2 (a)p 1 p 2, p 3 p 4 intersect p3p3 p1p1 p2p2 p4p4 (b) p 1 p 2, p 3 p 4 do not intersect p3p3 p1p1 p2p2 p4p4 (c) p 1 p 2, p 3 p 4 intersect p3p3 p1p1 p2p2 p4p4 (d) p 1 p 2, p 3 p 4 intersect p3p3 p1p1 p3p3 p4p4 (e) p 1 p 2, p 3 p 4 do not intersect p2p2

15 Two Segments Intersect (3) Consider two cross products: 1. (p 3 – p 1 ) x (p 2 – p 1 ) 2. (p 4 – p 1 ) x (p 2 – p 1 ) p1p1 p4p4 p2p2 (a)p 1 p 2, p 3 p 4 intersect: (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) > 0 p3p3 p1p1 p2p2 p4p4 (b) p 1 p 2, p 3 p 4 do not intersect: (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) < 0 p3p3

16 Two Segments Intersect (4) p1p1 p2p2 p4p4 (c) p 1 p 2, p 3 p 4 intersect (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0 p3p3 p1p1 p2p2 p4p4 (d) p 1 p 2, p 3 p 4 intersect (p 3 – p 1 ) x (p 2 – p 1 ) = 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0 p3p3

17 Two Segments Intersect (5) Case (e) –What are the cross products ? ( p 3 – p 1 ) x (p 2 – p 1 ) = 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0 –The cross products are zero’s, but they do not intersect –Same result with Case (d) p1p1 p3p3 p4p4 (e) p 1 p 2, p 3 p 4 do not intersect p2p2

18 Bounding Boxes Definition –Given a geometric object, the bounding box is defined by the smallest rectangle that contains the object Given two line segments p 1 p 2 and p 3 p 4 The bounding box of the line segment p 1 p 2 –The rectangle with lower left point=(x 1 ’,y 1 ’)=(min(x 1,x 2 ), min(y 1,y 2 )) and –upper right point=(x 2 ’,y 2 ’)= (max(x 1,x 2 ), max(y 1,y 2 )) The bounding box of the line segment p 3 p 4 is –The rectangle with lower left point=(x 3 ’,y 3 ’)=(min(x 3,x 4 ), min(y 3,y 4 )) and –upper right point=(x 4 ’,y 4 ’)=(max(x 3,x 4 ), max(y 3,y 4 )) p1p1 p3p3 p4p4 p2p2

19 Bounding Boxes What is the condition for the two bounding boxes intersect? –(x 3 ’  x 2 ’) and (x 1 ’  x 4 ’) and (y 3 ’  y 2 ’) and (y 1 ’  y 4 ’) 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 p1p1 p3p3 p4p4 p2p2

20 Two Segments Intersect (6) Case summary –(a) p 1 p 2, p 3 p 4 intersect (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) > 0 –(b) p 1 p 2, p 3 p 4 do not intersect (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) < 0 –(c) p 1 p 2, p 3 p 4 intersect (p 3 – p 1 ) x (p 2 – p 1 ) < 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0 –(d) p 1 p 2, p 3 p 4 intersect (p 3 – p 1 ) x (p 2 – p 1 ) = 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0 –(e) p 1 p 2, p 3 p 4 do not intersect ( p 3 – p 1 ) x (p 2 – p 1 ) = 0 (p 4 – p 1 ) x (p 2 – p 1 ) = 0

21 Two Segments Intersect - Algorithm Two_line_segments_intersect (p 1 p 2, p 3 p 4 ) 1.(x 1 ’,y 1 ’)=(min(x 1,x 2 ), min(y 1,y 2 )) 2.(x 2 ’,y 2 ’)=(max(x 1,x 2 ), max(y 1,y 2 )) 3.(x 3 ’,y 3 ’)=(min(x 3,x 4 ), min(y 3,y 4 )) 4.(x 4 ’,y 4 ’)=(max(x 3,x 4 ), max(y 3,y 4 )) 5.if not ((x 3 ’  x 2 ’) and (x 1 ’  x 4 ’) and (y 3 ’  y 2 ’) and (y 1 ’  y 4 ’)) then 6. return false // case (e) and more 7.else 8. if (p 3 –p 1 ) x (p 2 –p 1 ) or (p 4 –p 1 ) x (p 2 –p 1 ) are zero then 9. return true// case (c) and (d) 10. else if (p 3 –p 1 ) x (p 2 –p 1 ) and (p 4 –p 1 ) x (p 2 –p 1 ) have different sign then 11. return true// case (a) 12. else 13. return false// case (b)

22 Line Segment Intersection Problem The Problem: Given n line segments, is any pair of them insect? Clearly, doing pairwise intersection testing takes O(n 2 ) 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  (n 2 ) intersections. Simplifying assumptions –No input segment is vertical. –No three input segments intersect at a single point.

23 Problem Definition: Input : S={s 1, s 2, …, s n } 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, Problem Definition

24 Algorithm: Brute Force Intersect(S) Test all pairs (s i, s j )  SXS, i not equal j. If s i intersects s j, report s i intersects s j. 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 Brute Force Intersect Algorithm

25 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

26 Idea: Project segments onto y-axis. 1. If s i & s j intersect, then so do their y-intervals (e.g. s 1 & s 2 ). 2. If the y-intervals of s i & s j don ’ t intersect, then neither do s i & s j (e.g. s 1 & s 3 ) 3. If the y-intervals of s i & s j do intersect, then maybe s i & s j intersect (e.g. s 3 & s 4 ) we may still test extra, but better than before. Idea

27 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 T Perform intersection tests Plane Sweep Algorithm

28 Ordering Segments Two nonintersecting segments s 1 and s 2 are comparable at x if the vertical sweep line with x ‑ coordinate x intersects both of them. s 1 is above s 2 at x, written s 1 > x s 2, if s 1 and s 2 are comparable at x and the intersection of s 1 with the sweep line at x is higher than the intersection of s 2 with the same sweep line.

29 Example

30 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.

31 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.

32 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.

33 Segment ‑ intersection Pseudocode

34

35 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 each 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).

36 Illustration

37 Computing the Angle angle = tan -1 (dy/dx). It make sense to use a function that is much easier to compute 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;

38 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

39 Whether a Point is Inside (2) Point_in_polygon(P, q) // P: polygon with vertices p 1, p 2,.., p n, edges e 1, e 2,.., e n // q: point 1.pick an arbitrary point s outside the polygon 2.count = 0 3.for all edges e i of the polygon do 4. if e i 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

40 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

41 Whether a Point is Inside (4) Point_in_polygon_2(P, q) // P: polygon with vertices p 1, p 2,.., p n, edges e 1, e 2,.., e n // q: point (x 0, y 0 ) 1.count = 0 2.for all edges e i of the polygon do 3. if e i intersects the line x=x 0 then 4. y i = y-coordinate of the intersection 5. if y i y 0 6.if count is odd then return true 7.else return false Complexity –O(n) when n = size of the polygon

42 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?)

43 Idea

44 Method 1: Package Wrapping 1.Find an anchor point (say, the one with the smallest y value). 2.Take a horizontal ray in the positive direction, and sweep 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.

45

46 Method 2: The Graham Scan 1.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.

47

48

49 Method 3: Interior Elimination The interior elimination method is linear, on the average.

50 Method 4: A D&C approach

51 D&P Approach Continued MERGE takes O(n) time. t(n) = 2t(n/2) + cn = O(n  log n).

52 Reduction Technique:

53

54

55

56

57

58 Correctness

59

60

61

62

63 Maximal Polygonal Distance Only has to consider the distances between vertices. Straightforward algorithm: O(n 2 ) Clockwise traversal: O(n)

64


Download ppt "What is Computational Geometry? Study of Data Structures and Algorithms for Geometric Problems 1970 ’ s: Need for Computational Geometry Recognized; Progress."

Similar presentations


Ads by Google