Convex Hull 3d
Convex Hull 3d
Convex Hull 3d
Timothy M. Chan School of Computer Science University of Waterloo Waterloo, Ontario N2L 3G1, Canada [email protected] June 4, 2003
Abstract We give a simple interpretation and a simple implementation of the classical divide-andconquer algorithm for computing 3-d convex hulls (and in particular, 2-d Delaunay triangulations and Voronoi diagrams). The entire C++ code is under 100 lines long, requires no special data structures, and uses only 6n pointers for space.
Introduction
This article contains a complete 2-page C++ code that implements an optimal O(n log n) algorithm for one of the core problems in computational geometry: the construction of the convex hull of n points in 3-d. Applications for this problem are well-known and numerous. For example, we mention intersections of halfspaces, shape analysis and bounding volumes in 3-d, as well as Delaunay triangulations and Voronoi diagrams in 2-d, the latter of which in turn have applications to nearest/farthest neighbors, Euclidean minimum spanning trees, largest empty circles, etc. (see any standard computational geometry textbook for more information [13, 16, 26, 27, 29]). Disclaimer : The intended goal here is not the fastest code for the 3-d convex hull problem. Many ecient, robust implementations are already available [1, 4, 19, 22, 33]. Our implementation in fact ignores degenerate cases and robustness issues (to keep code simple) and is hardly of professional quality. Our challenge, however, is in nding the shortest program with a reasonable worst-case performance. The motivation is pedagogical. Considering the fundamental nature of the convex hull problem, it would be nice to have a complete solution (from scratch) that students can see and understand. Simpler programs are easier to learn, adapt, and experiment with. For the 3-d convex hull problem in particular, a self-contained implementation also has less overhead compared to the use of existing code or library routines, due to the variety of output formats an application may desire (a list of vertices, a graph formed by the edges, a planar subdivision under various representation schemes, etc.)
Revisiting known algorithms from the minimalist point of view can sometimes shed new light on old problems (for example, as in Anderssons work on balanced search trees [3]). For 3-d convex hulls, we discover a dierent interpretation of the classic divide-and-conquer algorithm, showing that it is not as dicult to implement as previously thought, even for the novice; the new description (see Section 3) can be understood by non-geometers.
We rst give a quick survey on the existing options, to assess and compare their relative ease of implementation. (Detailed explanations of these algorithms can be found in computational geometry texts.) Brute-force methods. It is actually easy to write a complete program to compute the 3-d convex hull if the input is small (and non-degenerate). ORourkes book [27] gave concise code by testing each triple of points as a possible facet. The running time is O(n4 ); a slight variation improves this to O(n3 ) by instead testing each pair of points as a possible edge. Our implementation is 26 lines long.1 Gift-wrapping. The gift-wrapping algorithm [9, 29] computes the convex hull in O(nh) time by generating facets one at a time via an implicit breadth- or depth-rst search. Here and throughout, h denotes the number of output facets. At rst glance, the algorithm appears to require a queue or stack for the graph search and a dictionary to prevent revisiting old facets. Actually, both data structures can be avoided in the 3-d case, by using recursion for depth-rst search and a linear scan for checking duplicate facets (the cost is still O(n) per facet). Our implementation is 38 lines long (again, assuming non-degeneracy). Incremental methods. Incremental methods are conceptually simple and maintain the convex hull as points are inserted one at a time. To insert a point outside the current hull, old visible facets are identied and removed, and new facets (attached to the new point) are generated, by somehow locating an initial new facet and subsequently performing breadth- or depth-rst search. Despite their O(n2 ) worst-case performance, incremental methods have gained popularity over the years, due to the discovery by Clarkson and Shor [11, 13, 26] that an optimal O(n log n) expected running time can be obtained by randomizing the order of insertion and maintaining a suitable pointer structure as points are inserted. Incremental methods are actually not as straightforward to implement as one might think. First, the current hull needs to be stored in a planar subdivision structure to support the necessary graph traversal and maintenance operations. Many such structures have been proposed in the literature (e.g., doubly-connected edge lists, the winged-edge or the quad-edge representation, as well as triangle-based representations), but all require delicate manipulations of pointers and primitives. Then there is the problem of nding the initial new facet at each iteration (a point location problem); this requires storing an additional conict graph or history dag, if the optimal randomized versions are adopted (although there are simpler suboptimal approaches [25]). See [10, 14, 22, 27] for implementations.
In this article, line counts include everything (e.g., I/O, declarations, etc.) but comments and blank lines. Source code is available at http://www.cs.uwaterloo.ca/~tmchan/.
1
Sweeping. Fortune [19, 13] gave a nice O(n log n) algorithm for computing 2-d Voronoi diagrams, based on a process that can viewed as tracking a 1-d parabolic front moving from left to right. This sweep-line algorithm requires a priority queue and a balanced search tree to ensure optimal worst-case performance (though Fortunes original implementation opted for a hash table instead of a search tree). Assuming the availability of these data structures, the algorithm is not hard to code up; in particular, a planar subdivision structure is not necessary. Unfortunately, the algorithm does not generalize to compute arbitrary 3-d convex hulls. Divide-and-conquer. The divide-and-conquer method, proposed by Shamos and Hoey [32] for 2-d Voronoi diagrams and Preparata and Hong [28] for 3-d convex hulls, was the earliest algorithm to achieve O(n log n) running time. The algorithm resembles mergesort: divide the point set into two halves by a vertical plane, recursively compute the hull of each half, and merge the two hulls into one. To merge, new bridge facets along the dividing vertical plane are generated by a graph search, and old facets underneath are removed. ORourke [27] commented in his popular book: this algorithm is both theoretically important, and quite beautiful. It is, however, rather dicult to implement and seems not used as frequently in practice as other asymptotically slower algorithms. . . (For these reasons, ORourke decided to illustrate instead the implementation of the incremental algorithm in his book; similarly, Fortunes survey [20] chose not to detail the divide-and-conquer algorithm for Voronoi diagrams.) The divide-and-conquer algorithm has been somewhat overshadowed by Fortunes and Clarkson and Shors discoveries. Indeed, its implementation appears to require not only a planar subdivision structure capable of the necessary stitching operations, but also the handling of some very tricky details for the bridge computation. Part of the blame is perhaps of a historical nature: the original presentations in both the ShamosHoey and PreparataHong paper were sketchy and did not address all the subtleties (ORourke [27] mentioned some potential problems). Lee and Schachter [24] gave a more detailed account of the divide-and-conquer algorithm for 2-d Voronoi diagrams. The rst full description for 3-d convex hulls appeared in Edelsbrunners book [16] and was 15 pages long. Day [12] described an implementation that was even lengthier (and worse, his code didnt appear to guarantee O(n log n) running time). An elegant implementation was nally given by Guibas and Stol [22], who demonstrated that much terser code is possible, if done carefully. A proper choice of planar subdivision structure is still a prerequisite, and in fact, a large part of Guibas and Stols paper was devoted to the development of their quad-edge representation scheme. Another thorough implementation was done by Shewchuk [33], using a triangle-based representation scheme. Although the divide-and-conquer algorithm is perceived as complicated, it is not only the sole deterministic optimal 3-d convex hull algorithm known in theory, but also the fastest in practice, according to experimental studies by Shewchuk [33] (see also [36]), especially if a variation by Dwyer [15] is adopted. From a robustness standpoint, the algorithm uses algebraic predicates of the lowest degree, in contrast to Fortunes algorithm, for example. (From an academic standpoint, it is also one of the best illustrations of the power of the divide-and-conquer paradigm, in the authors opinion.) Other algorithms. In the literature, one can also nd output-sensitive O(n log 2 h) algorithms [8, 18], optimal O(n log h) algorithms [7, 11], and parallel algorithms [2] for 3-d convex hulls, some using more sophisticated forms of divide-and-conquer. We omit their discussion, as these algorithms are more complicated to implement.
In this section, we present a new explanation of the 3-d divide-and-conquer algorithm that makes its details more transparent and intuitive. Except for the remarks at the end, our presentation is meant to be self-contained and accessible to readers with no background in 3-d computational geometry (just familiarity with 2-d convex hulls). Viewing as a kinetic 2-d problem. We begin with the problem statement and basic observations (which one can take as denitions). The input is a 3-d point set P = {p1 , . . . , pn }, where pi = (xi , yi , zi ). For simplicity, we assume that the points are non-degenerate, e.g., no four points lie on a common plane, and no three points lie on a common vertical plane. The output is the convex hull of P , a convex polytope made out of vertices (0-faces), edges (1faces), and facets (2-faces), dened as follows. A j -face of the lower hull is a tuple of j + 1 points in P that lie on some plane z = sx + ty + b such that all other points in P lie above the plane. Faces of the upper hull are similarly dened. The faces of the convex hull are the faces of the upper and lower hull combined. By symmetry, it suces to describe an algorithm for the lower hull. Since it is easier to visualize in 2-d than in 3-d, we avoid dealing with a 3-d object by taking (an innite number of) projections in 2-d. For each point pi , dene p i (t) = (xi , zi tyi ). (t) = {p Let P 1 (t), . . . , p n (t)}. By a change of variable y = z ty , a point pi is a vertex of the lower hull of P i for some t, p i (t) lies on some line y = sx + b while all other p (t)s lie above the line, i.e., i p i (t) is a vertex of the lower hull of the 2-d point set P (t) for some t. Similarly, pi pj is an (t) for some t. Therefore, edge of the lower hull of P i p i (t) pj (t) is an edge of the lower hull of P (t) as time t goes from to . our problem is reduced to the maintenance of the lower hull of P In other words, we want to make a movie of a kinetic 2-d lower hull, as input points move vertically (at dierent but xed speeds) in the plane. This is the setting in which we will study the problem; from now on, we can forget about (most of the) 3-d terminologies from the previous paragraph. How is the movie represented? As points move, the kinetic 2-d lower hull gains and loses vertices. In an insertion event, a new vertex p j just appears between p i and p k on the 2-d hull, creating two new edges p i p j , p j p k and destroying the old edge p i p k . In a deletion event, a vertex p j disappears from the 2-d hull, destroying two edges p i p j , p j p k and creating a new edge p i p k . (In either case, the triple pi pj pk makes a facet of the 3-d lower hull.) Each point is inserted at most once and deleted at most once, so the total number of events (and thus facets) is at most 2n. The desired movie can be specied by the sequence of events, sorted in time, along with the initial hull at t = . Applying divide-and-conquer. Sort the points p 1 , . . . , p n in increasing x-coordinates (recalling that points only move vertically). We use divide-and-conquer to solve the kinetic 2-d problem. Recursively create a movie for the lower hull L of p 1 , . . . , p n/2 and a movie for the lower hull R of p n/2 +1 , . . . , p n . We will create a movie for the lower hull A of all points p 1 , . . . , p n by a merging process. The initial hull A can be computed from the initial hulls L and R by identifying the common tangent uv , called the bridge, and removing the part of L to the right of u and the part of R to the left of v . (See Figure 1.) The initial bridge uv can be found in O(n) time by a simple iterative algorithm that starts with u = p n/2 and v = p n/2 +1 and repeatedly advances u leftward on L and 4
L u u u+ v v+ v Figure 1: Merging two 2-d lower hulls. v rightward on R, until u uv and uvv + both form counterclockwise turns. The notation w (resp. w+ ) represents the vertex immediately to the left (resp. right) of w on either the hull L or the hull R. As time progresses, we keep track of the current hulls L and R and the current bridge uv . The movies for L and R tell us when and how L and R change: If L undergoes an insertion or deletion of a vertex w, then A undergoes the same event provided that w is to the left of u. If R undergoes an insertion or deletion of a vertex w, then B undergoes the same event provided that w is to the right of v . The pair uv remains a bridge as long as u uv and uvv + are counterclockwise turns and uu+ v and uv v are clockwise turns. When u uv turns clockwise, u v becomes the new bridge. Then A undergoes deletion of u (between u and v ). When uu+ v turns counterclockwise, u+ v becomes the new bridge. Then A undergoes insertion of u+ between u and v . When uvv + turns clockwise, uv + becomes the new bridge. Then A undergoes deletion of v (between u and v + ). When uv v turns counterclockwise, uv becomes the new bridge. Then A undergoes insertion of v between u and v . To determine which of the above six possibilities should be executed next, we compute the six time values at which they occur and take the minimum. Continuing until there are no changes, we obtain the entire event sequence for the hull A. The description of the algorithm is complete and its correctness is self-evident. The running time satises the standard mergesort recurrence: T (n) = O(1) if n = 1 T ( n/2 ) + T ( n/2 ) + O(n) if n > 1. R
Remarks. The Delaunay triangulation of a 2-d point set (and by duality, the Voronoi diagram) [13, 2 16, 29] can now be computed by setting zi = x2 i + yi . The kinetic approach was popularized by Basch et al. [5]. Their rst paper actually contained a discussion on how to maintain the 2-d convex hull for points moving in arbitrary directions (rather than vertically). This is a more general problem and requires near-quadratic number of events in the worst case. The kinetic approach is related to the classical idea of a plane/space sweep. In fact, when translated to dual space [13, 16, 29] (in terms of the 3-d halfspace intersection problem or the 2-d Voronoi diagram problem), the time parameter corresponds precisely to one of the coordinates. So, our merging algorithm can indeed be thought of as a space sweep. The order of the sweep is dierent from Fortunes algorithm for Voronoi diagrams [19] but is identical to Seidels shelling method for higher-dimensional convex hulls [31] (once dualized). Seidels method performs only one pass to compute the entire hull (ours performs recursion), but uses a priority queue (ours avoids an explicit heap because of the recursion) and, more importantly, needs an initial O(n2 ) preprocessing to predict the insertion time of each vertex. Compared to the original 3-d divide-and-conquer algorithm, the main dierence that allows us to bypass planar subdivision structures is that we keep the facets (i.e., events) ordered by their time values. More remarks, on querying. The divide-and-conquer algorithm is particularly suitable for answering 3-d extreme point queries (a dual point location problem [16, 29, 35]): given values s and t, nd a point (xi , yi , zi ) minimizing zi sxi tyi . Such queries can be used to nd nearest neighbors in 2-d: given a point (a, b), minimizing the Euclidean distance (xi a)2 + (yi b)2 is equivalent 2 to minimizing zi 2axi 2byi with zi = x2 i + yi . We can answer such a query by remembering what the 2-d hull A looks like at time t and performing a 2-d extreme point query. By comparing s with the slope of the bridge uv , we can decide which side L or R to search. The approach leads to an O(log n) query time if appropriate pointers are kept between the event sequence of A and the event sequences of L and R; the data structure uses O(n log n) space. This is analogous to the layered version of Lee and Preparatas chain method of point location [23] (as the bridges form a monotone chain in the dual plane), but we have managed to obtain a pointlocation data structure directly while the hull is being constructed. Guibas, Knuth, and Stol [21], for example, were interested in obtaining a data structure for 2-d nearest neighbor queries without the application of two separate methods, one for Voronoi diagram construction and one for point location; their randomized incremental approach gave an O(n)-size structure that guaranteed an O(log2 n) expected query time only. Remembering the past is another classical idea, called persistence. In fact, our version of the divide-and-conquer algorithm combines nicely with Sarnak and Tarjans method of point location using persistent search trees [30]. This method achieves optimal O(n) space and O(log n) query time, and requires the extraction of the sequence of changes to a list formed by moving a sweep line across the (dual) planar subdivision, but the output of our convex hull algorithm gives us precisely this sequence (in contrast, Fortunes sweep is done in a dierent order, so a separate sorting step is still required if Sarnak and Tarjans method is to be used).
A minimalists implementation
The algorithm description in the previous section is simple yet detailed enough that a competent programmer should be able to implement it without too much eort (we invite the do-it-yourselfers to try their own version). For example, event sequences can be stored in arrays, and the current hulls L and R can be stored as doubly linked lists. There is no need to learn a planar-subdivision representation scheme. In this section, we comment on one particular implementation, given in the appendix, that aims at saving space (and is thus not the most straightforward version possible). This version requires only 6n pointers for working storage in addition to an array holding the input coordinates; the output is not stored explicitly. The amount is small, considering that an explicit array of facets or an array of edges would require 6n pointers already (recall that in the worst case there are about 3n facets and 2n edges). A quad-edge data structure, used in Guibas and Stols implementation of the divide-and-conquer algorithm [22], would require 15n pointers (an edge points to 4 adjacent edges and an incident vertex); on the other hand, Shewchuks triangle-based approach boasted an improvement to 12n pointers (a triangle points to 3 adjacent triangles and 3 incident vertices). A guided tour of the code. Our 2-page implementation is completely self-contained. We even include code for sorting; appropriately, we choose mergesort (lines 4053), and to avoid data movement and extra space, we choose a version that returns a linked list (using the next eld). The algebraic primitives (lines 3038) are simple (testing whether three points form a clockwise turn at t = , and determining the time when three points switch from a clockwise to a counterclockwise turn or vice versa). In the main recursive procedure hull(list, n, A, B), the input is a sorted list of points list and the number of points n. The output is an array of 2n events stored in A; the array B, also of size 2n, is used as temporary storage. The initial hull at t = is stored from left to right as a doubly linked list using the prev and next elds. To specify an insertion event fully, we appear to need a record of three points, to tell which pair of vertices p i and p k the new vertex p j is inserted between. In our space-economical version, we decide to store just p j in the event array A; but we will use the prev and next elds of p j to point to p i and p k (since p j is not in the initial hull, the elds have not been used). For a deletion event for vertex p j , we again store only p j in the event array (as it turns out, an extra bit to dierentiate insertions from deletions is unnecessary). As a result, the event array A is just an array of 2n points, representing the order in which points are inserted and/or deleted. Using this compact storage scheme, pointer manipulations actually get easier. The base case (line 61) is trivial (unlike in Guibas and Stols or Shewchuks implementation). The two recursive calls are made in lines 6366. The bridge at t = is found in lines 6871. In the main loop (lines 7492), time t progresses and we maintain doubly linked lists for the two hulls L and R using the prev and next elds. At the same time, we track the bridge uv and generate the output event array A. One problem arises with this compact scheme. During an insertion event, although we know which pair of vertices p i and p k a vertex p j is inserted between, we cannot store p i and p k in the prev and next elds yet, because they are still in use in the linked list for L or R. This problem can be xed by performing a second pass (lines 95104), where time t goes backwards and the role of insertions and deletions is reversed. Here, we use the prev and next elds to maintain the doubly linked list for the output hull A. At the same time, we track the bridge uv . At the end, the prev and 7
next elds will store the positions where a vertex is last seen, i.e., inserted, if it leaves the list, and the list itself will store the surviving hull, i.e., the hull at t = . The desired output requirement is thus fullled. (If space is not crucial, this second pass can be eliminated by a more straightforward implementation.) This version of the code prints the facets of the 3-d lower hull (lines 119120). This is done by processing the events in the output array A and tracking the linked list for the 2-d hull using the same prev and next elds. The code can be modied to print the vertices and/or edges of the 3-d lower hull by a similar process. For example, we can do the following to print each edge exactly once:
for (u = list; u->next != NIL; u = u->next) cout << u-P << " " << u->next-P << "\n"; for (i = 0; A[i] != NIL; A[i++]->act()) if (A[i]->prev->next != A[i]) { // insertion event cout << A[i]->prev-P << " " << A[i]-P << "\n"; cout << A[i]-P << " " << A[i]->next-P << "\n"; } else cout << A[i]->prev-P << " " << A[i]->next-P << "\n";
// deletion event
Remarks. Excluding blank lines and comments, the code is 91 lines long and is signicantly shorter than all implementations [1, 4, 10, 19, 22, 33] that we are aware of for 3-d convex hulls and 2-d Voronoi diagrams with comparable performance. To be fair, these implementations are designed not with the same educational purpose in mind, and neither for minimizing line counts. Since our goal is not in optimizing speed, we will skip a full report on runtimes. We did test the program to compute 2-d Voronoi diagrams for 100,000 points uniformly distributed on the unit square; its output matched with the output of other programs, and it ran in under 20 seconds on a Sun Ultra 10. There are some obvious ways to optimize the program at the expense of increasing its length; for example, in the main loop (lines 7592), we can avoid computing all 6 time values at each iteration, because some have already been computed in the previous iteration. We dont believe, though, that our program could be made faster than Guibas and Stols or Shewchuks implementation in practice, for the following reason. In the original divide-and-conquer algorithm using planar subdivisions, the merging process requires only the examination of facets near the bridge, whereas in our version, the merging process must go through all facets of the left and right hull. There are more degenerate cases in our version of the divide-and-conquer algorithm: two facets with the same time values can cause potential problems. We dont see an easy way to resolve these problems other than to apply a general perturbation scheme (such as [17]). For a robust implementation using exact arithmetic, and perhaps adaptive lters (e.g., see [34]), we have to deal with a more involved algebraic primitive, the comparison of time values. The degree of this primitive is higher than the degree in the original divide-and-conquer algorithm (4 instead of 3 for 3-d convex hulls, and 6 instead of 4 for 2-d Voronoi diagrams), but lower than in Fortunes sweep algorithm. In terms of space, we can actually get by with 5n pointers under the same setup: in the nal merge, dont store the output event array but print the answer instead (the code becomes longer, however).
Conclusions
By transforming a 3-d problem into a kinetic 2-d problem, we have given a more accessible description of the 3-d divide-and-conquer convex hull algorithm. This simpler description enables a short implementation that uses no special data structures other than arrays and linked lists. It would be interesting to see minimalistic implementations for other basic problems in computational geometry. For 3-d convex hulls, it would be intriguing to nd algorithms that use as little extra space as possible (as sought also by Br onnimann et al. [6]).
References
[1] The CGAL home page. http://www.cgal.org/. [2] N. M. Amato, M. T. Goodrich, and E. A. Ramos. Parallel algorithms for higher-dimensional convex hulls. In Proc. 34th IEEE Sympos. Found. Comput. Sci., pages 683694, 1994. [3] A. Andersson. Balanced search trees made simple. Proc. 3rd Workshop Algorithms Data Struct., Lect. Notes in Comput. Sci., vol. 709, Springer-Verlag, pages 6071, 1993. [4] C. B. Barber, D. P. Dobkin, and H. T. Huhdanpaa. The Quckhull algorithm for convex hulls. ACM Trans. on Math. Software, 22:469483, 1996. (Source code available at http://www.geom.umn.edu/ software/qhull/.) [5] J. Basch, L. J. Guibas, and J. Hershberger. Data structures for mobile data. J. Algorithms, 31:128, 1999. [6] H. Br onnimann, J. Iacono, J. Katajainen, P. Morin, J. Morrison, and G. Toussaint. Optimal in-place planar convex hull algorithms. In Proc. Latin American Sympos. on Theoretical Informatics, pages 494 507, 2002. [7] T. M. Chan. Optimal output-sensitive convex hull algorithms in two and three dimensions. Discrete Comput. Geom., 16:361368, 1996. [8] T. M. Chan, J. Snoeyink, and C.-K. Yap. Primal dividing and dual pruning: output-sensitive construction of four-dimensional polytopes and three-dimensional Voronoi diagrams. Discrete Comput. Geom., 18:433 454, 1997. [9] D. R. Chand and S. S. Kapur. An algorithm for convex polytopes. J. ACM, 17:7886, 1970. [10] K. L. Clarkson, K. Mehlhorn, and R. Seidel. Four results on randomized incremental constructions. Comput. Geom. Theory Appl., 3:185212, 1993. (Source code available at http://cm.bell-labs.com/ netlib/voronoi/hull.html.) [11] K. L. Clarkson and P. W. Shor. Applications of random sampling in computational geometry, II. Discrete Comput. Geom., 4:387421, 1989. [12] A. M. Day. The implementation of an algorithm to nd the convex hull of a set of three-dimensional points. ACM Trans. on Graphics, 9:105132, 1990. [13] M. de Berg, M. van Kreveld, M. Overmars, and O. Schwarzkopf. Computational Geometry: Algorithms and Applications. Springer-Verlag, Heidelberg, 1997. [14] O. Devillers. Improved incremental randomized Delaunay triangulation. In Proc. 14th ACM Sympos. Comput. Geom., pages 106115, 1998. [15] R. A. Dwyer. A faster divide-and-conquer algorithm for constructing Delaunay triangulations. Algorithmica, 2:137151, 1987.
[16] H. Edelsbrunner. Algorithms in Combinatorial Geometry. Springer-Verlag, Berlin, 1987. [17] H. Edelsbrunner and E. P. M ucke. Simulation of simplicity: a technique to cope with degenerate cases in geometric algorithms. ACM Trans. on Graphics, 9:66104, 1990. [18] H. Edelsbrunner and W. Shi. An O(n log 2 h) time algorithm for the three-dimensional convex hull problem. SIAM J. Comput., 20:259277, 1991. [19] S. Fortune. A sweepline algorithm for Voronoi diagrams. Algorithmica, 2:153174, 1987. (Source code available at http://cm.bell-labs.com/cm/cs/who/sjf/.) [20] S. Fortune. Voronoi diagrams and Delaunay triangulations. In Euclidean Geometry and Computers (D. A. Du and F. K. Hwang, eds.), pages 193233, World Scientic Publishing Co., 1992. [21] L. J. Guibas, D. E. Knuth, and M. Sharir. Randomized incremental construction of Delaunay and Voronoi diagrams. Algorithmica, 7:381413, 1992. [22] L. J. Guibas and J. Stol. Primitives for the manipulation of general subdivisions and the computation of Voronoi diagrams. ACM Trans. on Graphics, 4:74123, 1985. (Source code at http:// www.dcc.unicamp.br/~stolfi/EXPORT/software/c/Index.html.) [23] D. T. Lee and F. P. Preparata. Location of a point in a planar subdivision and its applications. SIAM J. Comput., 6:594606, 1977. [24] D. T. Lee and B. J. Schachter. Two algorithms for constructing a Delaunay triangulation. Int. J. Comput. Inf. Sci., 9:219242, 1980. [25] E. P. M ucke, I. Saias, and B. Zhu. Fast randomized point location without preprocessing in two- and three-dimensional Delaunay triangulations. In Proc. 12th ACM Sympos. Comput. Geom., pages 274283, 1996. [26] K. Mulmuley. Computational Geometry: An Introduction Through Randomized Algorithms. Prentice-Hall, Englewood Clis, NJ, 1994. [27] J. ORourke. Computational Geometry in C (2nd ed.). Cambridge University Press, 1998. [28] F. P. Preparata and S. J. Hong. Convex hulls of nite sets of points in two and three dimensions. Commun. ACM, 20:8793, 1977. [29] F. P. Preparata and M. I. Shamos. Computational Geometry: An Introduction. Springer-Verlag, New York, 1985. [30] N. Sarnak and R. E. Tarjan. Planar point location using persistent search trees. Commun. ACM, 29:669 679, 1986. [31] R. Seidel. Constructing higher-dimensional convex hulls at logarithmic cost per face. In Proc. 18th ACM Sympos. Theory Comput., pages 404413, 1986. [32] M. I. Shamos and D. Hoey. Closest-point problems. In Proc. 16th IEEE Sympos. Found. Comput. Sci., pages 151162, 1975. [33] J. R. Shewchuk. Triangle: engineering a 2D quality mesh generator and Delaunay triangulator. In Proc. 1st Workshop on Applied Comput. Geom., pages 124133, 1996. (Source code available at http:// www-2.cs.cmu.edu/~quake/triangle.html.) [34] J. R. Shewchuk. Adaptive precision oating-point arithmetic and fast robust geometric predicates. Discrete Comput. Geom., 18:305363, 1997. [35] J. Snoeyink. Point location. In Handbook of Discrete and Computational Geometry (J. E. Goodman and J. ORourke, eds.), CRC Press, Boca Raton, FL, pages 559574, 1997. [36] P. Su and R. L. S. Drysdale. A comparison of sequential Delaunay triangulation algorithms. Comput. Geom. Theory Appl., 7:361386, 1997.
10
#include <stream.h> struct Point { double x, y, z; Point *prev, *next; void act() { if (prev->next != this) prev->next = next->prev = this; // insert else { prev->next = next; next->prev = prev; } // delete } }; const double INF = 1e99; static Point nil = {INF, INF, INF, 0, 0}; Point *NIL = &nil; inline double turn(Point *p, Point *q, Point *r) { // <0 iff cw if (p == NIL || q == NIL || r == NIL) return 1.0; return (q->x-p->x)*(r->y-p->y) - (r->x-p->x)*(q->y-p->y); } inline double time(Point *p, Point *q, Point *r) { // when turn changes if (p == NIL || q == NIL || r == NIL) return INF; return ((q->x-p->x)*(r->z-p->z) - (r->x-p->x)*(q->z-p->z)) / turn(p,q,r); } Point *sort(Point P[], int n) { Point *a, *b, *c, head; if (n == 1) { P[0].next = NIL; return P; } a = sort(P, n/2); b = sort(P+n/2, n-n/2); c = &head; do if (a->x < b->x) { c = c->next = a; a = a->next; } else { c = c->next = b; b = b->next; } while (c != NIL); return head.next; } void hull(Point *list, int n, Point **A, Point **B) { Point *u, *v, *mid; double t[6], oldt, newt; int i, j, k, l, minl; // the algorithm // mergesort
11
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
if (n == 1) { A[0] = list->prev = list->next = NIL; return; } for (u = list, i = 0; i < n/2-1; u = u->next, i++) ; mid = v = u->next; hull(list, n/2, B, A); // recurse on left and right sides hull(mid, n-n/2, B+n/2*2, A+n/2*2); for ( ; ; ) // find initial bridge if (turn(u, v, v->next) < 0) v = v->next; else if (turn(u->prev, u, v) < 0) u = u->prev; else break; // merge by tracking bridge uv over time for (i = k = 0, j = n/2*2, oldt = -INF; ; oldt = newt) { t[0] = time(B[i]->prev, B[i], B[i]->next); t[1] = time(B[j]->prev, B[j], B[j]->next); t[2] = time(u, u->next, v); t[3] = time(u->prev, u, v); t[4] = time(u, v->prev, v); t[5] = time(u, v, v->next); for (newt = INF, l = 0; l < 6; l++) if (t[l] > oldt && t[l] < newt) { minl = l; newt = t[l]; } if (newt == INF) break; switch (minl) { case 0: if (B[i]->x < u->x) A[k++] = B[i]; B[i++]->act(); break; case 1: if (B[j]->x > v->x) A[k++] = B[j]; B[j++]->act(); break; case 2: A[k++] = u = u->next; break; case 3: A[k++] = u; u = u->prev; break; case 4: A[k++] = v = v->prev; break; case 5: A[k++] = v; v = v->next; break; } } A[k] = NIL; u->next = v; v->prev = u; // now go back in time to update pointers for (k--; k >= 0; k--) if (A[k]->x <= u->x || A[k]->x >= v->x) { A[k]->act(); if (A[k] == u) u = u->prev; else if (A[k] == v) v = v->next; } else { u->next = A[k]; A[k]->prev = u; v->prev = A[k]; A[k]->next = v; if (A[k]->x < mid->x) u = A[k]; else v = A[k]; } } main() { int n, i; cin >> n; Point *P = new Point[n]; // input for (i = 0; i < n; i++) { cin >> P[i].x; cin >> P[i].y; cin >> P[i].z; } Point *list = sort(P, n); Point **A = new Point *[2*n], **B = new Point *[2*n]; hull(list, n, A, B); for (i = 0; A[i] != NIL; A[i++]->act()) // output cout << A[i]->prev-P << " " << A[i]-P << " " << A[i]->next-P << "\n"; delete A; delete B; delete P; }
12