CG Lecture 1 : basic facts Problem: give a set of n points P in the plane, compute Convex Hull Algorithms its convex hull CH(P). Basic facts: •2D • CH(P) is a with complexity O(n). • Basic facts • Algorithms: Naïve, Gift wrapping, Graham scan, Quick hull, • Vertices of CH(P) are a subset of the input points P. p Divide-and-conquer p3 9

• Lower bound p8 p4 p p1 13 •3D p p 7 p • Basic facts 5 10 p6 Input: p ,…, p • Algorithms: Gift wrapping, Divide and conquer, incremental p2 p12 1 13 CH vertices: p1,p2,p11,p12,p13,p9,p3 • Convex hulls in higher dimensions p11 1 2 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Naive algorithm Triangle Area Algorithm

• For each pair of points construct its 2()()⋅=−×−Area P21 P P 31 P connecting segment and supporting line. =−⋅−PPPP21 31sinα • Find all the segments whose supporting P (x ,y ) Yes xxyy−− 3 3 3 lines divide the plane into two halves, = 21 21 such that one half plane contains all the xxyy31−− 31 other points. α xy111 P1(x1,y1) P (x ,y ) • Construct the convex hull out of these 2 2 2 = xy221 segments. No xy1 33 • All pairs: The determinant is twice the area of the triangle ⎛⎞n nn(1)− 2 OO()⎜⎟== ( ) On () ⎝⎠2 2 whose vertices are the rows of the matrix. • Check all points for each pair: O(n) 3 3 4 Leo Joskowicz, Spring 2005each, O(n ) total. Leo Joskowicz, Spring 2005

Orientation and point classification Triangle Area

(x1,y1) xy111 2()()⋅=−×−Area P P P P 1 21 31 Area = xy221 =−⋅−PPPPsinα 2 + 21 31 xy1 P3(x3,y3) 33 (x ,y ) xxyy−− 2 2 = 21 21 (x3,y3) xxyy31−− 31 • The area sign indicates the orientation of the points. xy1 α 11 P (x ,y ) • Positive area ≡ counterclockwise orientation ≡ left turn. 1 1 1 P2(x2,y2) = xy221 • Negative area ≡ clockwise orientation ≡ right turn. xy331 • This test can be used to determine whether a given point is “above” or “below” a given line The determinant is twice the area of the triangle whose vertices are the rows of the matrix.

5 6 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

1 Orientation and point classification Possible problems

xy1 (x1,y1) • Degenerate cases, e.g., 3 collinear 1 11 points, may harm the correctness of Area = xy221 2 + the algorithm. Segments AB, BC and A xy331 (x2,y2) AC will all be included in the B (x3,y3) convex hull. • The area sign indicates the orientation of the points. C • Positive area ≡ counterclockwise orientation ≡ left turn. • Negative area ≡ clockwise orientation ≡ right turn. • Numerical problems – We might conclude • This test can be used to determine whether a given point that none of the three segments (or a wrong is “above” or “below” a given line pair of them) belongs to the convex hull.

• Question: How is colinearity detected? 7 8 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

General position assumption Gift wrapping algorithm p • When designing a geometric algorithm, we first Algorithm: 3 make some simplifying assumptions, e.g.: p8 p13 1. Find the lowest point p1 and its hull edge e p4 p1 • No three collinear points; 2. For each remaining point pi (i > 2) do p7 p5 p10 • No two points with the same x or y coordinate; • Compute the CCW angle αi from the p p6 p previous hull edge 2 2 • Other configurations: no three points on a circle, … •Let pj be the point with the smallest αi p1

• Later, we consider the general case: • Make (p1 pi) the new hull edge

• Behavior of algorithm to degenerate cases? Rotate counterclockwise a line through p1 • Will the correctness be preserved? until it touches one of the other points 2 • Will the running time remain the same? Time complexity: O(n ) • Modify/extend algorithm to handle degeneracies In fact, the complexity is O(nh), 9 where n is the input size and h is the hull size. 10 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Line equation and angle Graham’s scan algorithm Algorithm: •Let (x1,y1) and (x2,y2) be two points. • Find a point p in the interior of the hull. • The explicit line equation is: 0 • Compute the CCW angle α from p to all i 0 p0 y = mx + c other points.

y − y y x − y x y = mx+c • Sort the points by angle αi. y = 2 1 x + 1 2 2 1 x − x x − x • Construct the boundary by scanning the 2 1 2 1 m = tan θ points in the sorted order and performing only “right turns” (trim off “left turns”). y2 − y1 tanθ = c x2 − x1 p p0 Use a stack to process sorted points 0 Time Complexity: O(n log n) • Singularity at x1= x2 (vertical line) Right turn Left turn Question: How do we check for a right or left turn? 11 12 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

2 Graham’s scan: complexity analysis Quick hull algorithm a Algorithm: • Find four extreme points of P: highest a, lowest •Sorting –O(n log n) c b, leftmost c, rightmost d. d • D = number of points popped on processing p , i i • Discard all points in the quadrilateral interior nn • Find the hulls of the four triangular regions time=+=+ (D 1) nD ∑∑ii exterior to the quadrilateral. b ii==11 • To process triangular regions, find the extreme • Each point is pushed on the stack only once. point in linear time. Two new exterior regions • Once a point is popped – it cannot be popped again. A, B will be formed, each processed separately. • Recurse until no points are left in the exterior: • Hence n the convex hull is the union of the exteriors. ∑ Dni ≤ extreme point c i=1 Time Complexity: |B|= β T(n) = O(n)+T(α)+T(β) where α+β=n–1 |A|= α a 13 = T(n–1) + O(n) = O(n2) worst case! 14 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 b

Divide-and-Conquer Finding tangents (1) • Two disjoint convex polygons have four tangents Algorithm: median • Find a point with a median x coordinate in that classify them as either being entirely to the O(n) time tangents left (+) or to the right (−) of the line: • Partition point set in two halves • Compute the convex hull of each half (+,+) (recursive execution) (+,−) • Combine the two convex hulls by finding their upper and lower tangents in O(n). (−,+) left hull right hull Time Complexity: O(n log n) ⎛ n ⎞ (−,−) T (n) = 2T ⎜ ⎟ + O(n) ⎝ 2 ⎠ 15 16 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Finding tangents (2) Lower bound for convex hull in 2D Lower tangent: Connect rightmost 1: (-,+) Claim: Convex hull computation takes Θ(n log n) point of left hull to 2: (-,*) leftmost point in Proof: reduction from Sorting to right hull and Convex Hull: “walk” around both 3: (*,*)5: (*,-) •Given n real values xi, generate n boundaries until the points on the graph of a convex lower tangent is 2 function, e.g. (xi,xi ). reached. • Compute the (ordered) convex Complexity(CH)=Ω(n log n) Complexity: O(n). hull of the points. Since there is a O(n log n)-time • The order of the convex hull algorithm, Complexity(CH)= Θ(n log n) points is the order of the xi. 17 18 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

3 Convex hulls in 3D Convex hull in 3D: properties z Theorem: A convex polyhedron with |V| = n vertices hast at most |E| = 3n–6 edges and |F| = 2n–4 faces. Proof: from Euler’s formula for planar graphs: |V| – |E| + |F| = 2 Every face has at least 3 edges (triangle) y Every edge is incident to at least 2 faces Output: Convex hull x Input: Æ 3|F| ≤ 2|E| Æ |E| ≤ 3n–6 and |F| ≤ 2n–4 Points in 3D • The complexity of the convex hull is O(n). • The equality holds when all faces are triangles. • When no three points are on a line and no four on a Representation: plane, the faces of the convex hull are all triangles.

Planar subdivision 19 20 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Gift wrapping in 3D (1) Gift wrapping in 3D (2) • Form a triangular face containing a,b,c and repeat the Idea: generalize the operation for edges bc and ac. 2D procedure. The c c wrapping element is a plane instead of a line. b b a a Pivot a plane around the a edges of the hull; find • Start from lowest edge in the convex hull and “work the smallest angle of the b around and upwards” until the wrap is over. e d f planes Πi containing b d c b segment ab and points pi c

21 a 22 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 a

Gift wrapping in 3D (3) Divide-and-Conquer in 3D Idea: generalize the 2D Algorithm sketch procedure. Recursively split B Maintain a queue of facets and examine their edges in the point set into two disjoint turn, computing for each the smallest angle with it. sets, compute their hulls and merge them in linear time. A Complexity Recursion ends when 4 points • O(n) operations are required at each edge to find the minimum angle. are left (tetraededron). • Each edge is visited at most once. • Since there are O(n) edges, the complexity is O(n2). Key step: merging two disjoint • In fact, it is O(n|F|), where |F| is the number of convex polyhedra in O(n). facets in the final hull (as in 2D case). 23 24 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

4 Merging two disjoint convex polyhedra Merging two disjoint convex polyhedra Idea: Algorithm sketch 1. Identify the merge boundaries of A and B. 1. Find the lowest new edge of the hull formed by one 2. Create new triangular faces with two vertices from the merge boundary of A and one vertex from the merge vertex a of A and one vertex b of B. boundary of B (and vice-versa). 2. “Pivot” a plane Π around edge ab to find the first p. 3. Delete hidden faces of A and B. 3. Form a triangular face (abp) and repeat with the new Remarks: pivot edge formed by p and its opposite (either a or b) • The “chain” of hidden faces starts and ends at the merge boundary edges. 4. Repeat step 3 until the wrapping is done. • The “band” of new faces has the topology of a 5. Delete the hidden faces by following the faces around cylinder with no caps (it wraps around). A and B whose edges are on the merge boundary.

25 26 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Find the hull starting edge Plane rotation Lemma: when the plane Π is rotated around segment ab, the first point encountered c, must be adjacent to either a or b. Proof: by convexity arguments (omitted here)

Only the neighbors of edge ab need to be Starting edge tested at each time. Since there are at How do we find the next point p? most n–1 neighbors, the overall How do we know that the faces will not self-intersect? complexity is O(n2).

27 28 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Merge boundary triangulation Plane rotation – improvement (1) 1. Start from lowest hull edge Improvement: the testing of all neigboring points is 2. Alternate between left and right merge boundary wasteful and repeats work. Only O(n) vertices should points, creating triangles. be examined overall (amortized cost). No search or testing necessary. Æ Keep track of A-winners and B-winners: Complexity: O(n). • A-winner: the vertex α adjacent to a with smallest angle. Overall complexity: • B-winner: the vertex β adjacent to a with smallest O(n2) 6 angle. Can we do better? 4 Lemma: If αi is a winner at iteration i, the B-winner at 1 7 O(n log n) 5 the next iteration βi+1 is counterclockwise of βi around Look again at rotation. 3 b (same for α and β reversed –proof omitted). 2 29 30 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

5 Divide and Conquer in 3D (end) Incremental convex hull in 3D Idea: incrementally add a point to a convex polyhedra P • The number of candidate vertices in each loop iteration decreases monotonically. Each edge is examined at Two cases: most twice Æ operation takes O(n). 1. The new point is inside P Æ do nothing • Divide-and conquer recurrence equation: 2. The new point is outside of P Æ fix P! ⎛ n ⎞ T (n) = 2T ⎜ ⎟ + O(n) Membership ⎝ 2 ⎠ test is done in • Theorem: the convex hull of a set of points in space O(n) time. can be computed in optimal O(n log n) time and O(n) space. What needs to Æ Same complexity as 2D problem! be done to add

31 a point? 32 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Incremental convex hull in 3D Incremental convex hull in 3D How to update the convex polyhedron: Once we know what the horizon is, both operations 1. Add faces from edges of the horizon to the new point. can be performed in O(n) time. 2. Remove hidden faces starting from edges in the horizon.

33 34 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Face visibility Incremental 3D convex hull algorithm The visibility of a face F from a point p can be • For all faces, compute the signed volume. determined by the signed volume of the tetrahedron • Keep hidden faces and discard visible faces. formed by three points on the face abc and p. • Edges sharing visible and hidden faces form the horizon. p p Algorithm sketch c b b Initialize CH to the tetrahedron (p1,p2,p3,p4) c F For each remaining point, do F a a For each face F of CH, compute the signed volume V Visible V < 0 Not visible V > 0 of the tetrahedron formed by p and F. Mark F visible if V<0 ax ay az 1

1 bx by bz 1 If no faces are visible, discard p (p is inside CH) V = 6 cx cy cz 1 Else for each border face F construct cone face.

px py pz 1 35 for each visible face F, delete F. 36 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

6 Complexity and degeneracies Improvement (1) • Overall complexity is O(n2). Can we do better? • Look-ahead computation to make it cheaper to find visible facets. • Degeneracy: if points can be coplanar, coplanar • Maintain for each facet f of the current convex hull triangles will be created. This can be fixed by deleting CH(Pi) a conflict set: the shared diagonal, thereby creating a larger face. Pconflict(f){⊆ pi+1,…pn} containing the points that f can see.

• For each point pj, j>i, maintain the set of facets of CH(Pi) visible from pj, called Fconflict(pj)

• Point pj is in conflict with a face f because once pj is added, the face must be deleted from the convex hull.

37 38 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005

Improvement (2) • We obtain a bipartite graph, called the conflict graph G. • Update the conflict graph

when adding pi:

• Discard neighbors of pi • Add nodes to G for the newly

created facets and discard pi • Find conflict list of new facets. All others remain unaffected! • Algorithm sketch: add

points p1,…pn sequentially, using the conflict graph G to Complexity: O(n log n) 39 40 Leo Joskowicz, Spring 2005determine visible faces. expected randomized Leo Joskowicz, Spring 2005

Convex hulls in higher dimensions Problem: given n points in Rd, find their convex hull (also called a ). • Faces become hyperfaces of dimension 2,3,…,d–1. • Hyperfaces form a graph structure where adjacencies between features of dimension i and i–1 are stored. • Some of the previous algorithms “scale up” (applicable in principle) with proper extensions. Theorem 1: the convex hull of n points in d-dimensional space has at most Ω ( n ⎣⎦ d / 2 ) hyperfaces. Theorem 2: the convex hull can be computed with the gift-wrapping algorithm in Ω (n ⎣⎦d / 2 +1 ) + Ω(n ⎣⎦d / 2 log n) 41 Leo Joskowicz, Spring 2005

7