\input PLocHdr.tex \begfile{PLoc.tex of October 6, 1983 4:35 pm --- Stolfi} \inputtty % your chance to \def\printfilestamp{F} % THINGS TO FIX: % figure numbering; lemma/theorem numbering % revise symbols for \Hor, \G, etc. \paper{Optimal Point Location in a Monotone Subdivision} \def\paptitle{Point location} \author{Herbert Edelsbrunner, Leo J. Guibas, and Jorge Stolfi} \institution{University of Graz, Xerox PARC, and Stanford University} \def\AuEde{AE} \def\BenMa{BM} \def\BiPre{BP} \def\ChazOne{C1} \def\ChazTwo{C2} \def\DoLi{DL} \def\EHH{EHH} \def\EdMa{EM} \def\EOS{EOS} \def\GuiSto{GS} \def\Har{H} \def\Kir{Ki} \def\Knu{Kn} \def\LeePre{LP} \def\LiTa{LT} \def\Pre{P} \def\PreSup{PS} \def\Sha{S} \def\Tar{Tar} \def\Wi{W} \def\Thei{1} \def\Theii{2} \def\Theiii{3} \def\Theiv{4} \def\Thev{5} \def\Thevi{6} \def\Thevii{7} \def\Theviii{8} \def\Theix{9} \def\Thex{10} \def\Thexi{11} \def\Thexii{12} \def\proj#1{\Pi #1} \def\below{\lsls} \def\above{\grgr} \def\justbelow{\prec} \def\justabove{\succ} \def\regionindex{\mathop{\#}} \def\regionabove{\mathop{\char a\char b\char o\char v\char e}} \def\regionbelow{\mathop{\char b\char e\char l\char o\char w}} \def\LCA{\mathop{\char l\char c\char a}} \def\MSB{\mathop{\char m\char s\char b}} \def\REV{\mathop{\char r\char e\char v}} % TREE, DAG, AND QUAD-EDGE OPERATORS \def\father{\mathop{\char f\char a\char t\char h\char e\char r}} \def\edge{\mathop{\char e\char d\char g\char e}} \def\upptr{\mathop{\char u\char p}} \def\downptr{\mathop{\char d\char o\char w\char n}} \def\chain{\mathop{\char c\char h\char a\char i\char n}} \def\xval{\mathop{\char x\char v\char a\char l}} \def\leftptr{\mathop{\char l\char e\char f\char t}} \def\rightptr{\mathop{\char r\char i\char g\char h\char t}} \def\region{\mathop{\char r\char e\char g\char i\char o\char n}} \def\lastin{\mathop{\char l\char a\char s\char t\>\char i\char n}} \def\lastout{\mathop{\char l\char a\char s\char t\>\char o\char u\char t}} \def\firstin{\mathop{\char f\char i\char r\char s\char t\>\char i\char n}} \def\firstout{\mathop{\char f\char i\char r\char s\char t\>\char o\char u\char t}} \def\forward{\mathop{\char f\char o\char r\char w\char a\char r\char d}} \def\base{\mathop{\char b\char a\char s\char e}} \def\onext#1{\mathop{\char o\char n\char e\char x\char t}(#1)} \def\oprev#1{\mathop{\char o\char p\char r\char e\char v}(#1)} \def\dnext#1{\mathop{\char d\char n\char e\char x\char t}(#1)} \def\rot#1{\mathop{\char r\char o\char t}(#1)} \def\org#1{\mathop{\char o\char r\char g}(#1)} \def\dest#1{\mathop{\char d\char e\char s\char t}(#1)} \def\sym#1{\mathop{\char s\char y\char m}(#1)} \def\T{\hbox{\bi T\/}} \def\Hor{S} \def\Ver{{S^\ast}} \def\G{G} \def\H{H} \def\eat#1{} % eats next token or group \def\f#1{\hbox{\hskip1pt {\fx #1}\hskip1pt}} \def\point#1#2{\vskip \refvskip pt\hbox to \textwidth pt{\hskip\refindent pt\hbox par \refbodywidth pt{\hbox to 0.0in {$\hskip -\refindent pt \hbox{#1} \hskip 0pt plus 1000pt minus 1000pt$}#2}}\par} \support{The work of H.\ Edelsbrunner was supported by the Austrian Fonds zur F{\cmr\"\rm o}rderung der wissenschaftlichen Forschung. J.\ Stolfi, who is on leave from the University of S{\cmr\s\rm a}o Paulo, was partially supported by a grant from Conselho Nacional de Desenvolvimento Cient{\cmr\'\i}fico e Tecnol{\cmr\'\rm o}gico (CNPq).} \abstract{Point location, often known in graphics as "hit detection", is one of the fundamental problems of computational geometry. In a point location query we want to identify which of a given collection of geometric objects contains a particular point. Let $\Sscr$ denote a subdivision of the Euclidean plane into monotone regions by a straight-line graph of $m$ edges. In this paper we exhibit a substantial refinement of the technique of Lee and Preparata for locating a point in $\Sscr$ based on separating chains. The new data structure, called a {\it layered dag}, can be built in $O(m)$ time, uses $O(m)$ storage, and makes possible point location in $O(\log m)$ time. Unlike previous structures that attain these optimal bounds, the layered dag can be implemented in a simple and practical way, and is extensible to subdivisions with edges more general than straight-line segments.} \abstract{{\bf Keywords:} point location, monotone polygons, planar graphs, partial order, graph traversal, computational geometry.} \section{1. Introduction} {\it Point location} is one of the fundamental problems in computational geometry. In the two-dimensional case, we are given a subdivision $\Sscr$ of the plane into two or more regions, and then asked to determine which of those regions contain a given query point $p$. If the same subdivision is to be used for a large number of queries, as is often the case, we can reduce the total cost of this task by preprocessing the subdivision into a data structure suitable for the search. We will measure the performance of a proposed solution to this problem by three quantities, the preprocessing cost $P$, the storage cost $S$, and the query cost $Q$. Optimal solutions for this search problem are known since Lipton and Tarjan [\LiTa] and Kirkpatrick [\Kir]. For a subdivision $\Sscr$ with $m$ edges these optimal solutions simultaneously attain $S = O(m)$, $Q = O(\log m)$, and, under certain assumptions, also $P = O(m)$. The Lipton-Tarjan method is based on their graph separator theorem, and so far has been only of theoretical interest. Kirkpatrick's method, which consists of building a hierarchy of coarser and coarser subdivisions, is implementable, but still the implied constants are so large as to make current implementations of little practical interest. In addition, neither of these techniques seems to extend in a natural way to non-straight-line subdivisions. Historically Dobkin and Lipton [\DoLi] were the first to achieve $O(\log n)$ query time, while using $O(n^2)$ space. Their method was subsequently refined by Preparata [\Pre] so that $O(n\log n)$ space suffices. Later Bilardi and Preparata [\BiPre] again gave a refinement which achieves $O(n)$ space in the expected case, while retaining $O(n\log n)$ space and $O(\log n)$ query time in the worst case. These solutions are applicable to non-straight-line subdivisions and seem to admit of efficient implementations. A substantially different approach was initiated by Shamos [\Sha] and led to the well-known point location paper of Lee and Preparata [\LeePre], based on the construction of separating chains. This data structure attains $P=O(m\log m)$, $S=O(m)$, and $Q=O(\log^2 m)$. The constants of proportionality in these expressions are quite small, and the algorithms, particularly the query one, are simpler than those of Kirkpatrick. While Kirkpatrick's algorithm requires the regions to be triangulated, that of Lee and Preparata works for regions of a fairly general shape (monotone polygons, which include the convex ones). Recently Chazelle [\ChazTwo] described a modification of this solution as a special instance of a general method for ``searching in history''. His structure needs the same amount of space and time. These techniques again are applicable to non-straight-line subdivisions, although this possibility was not examined. In a separate development, Edelsbrunner and Maurer [\EdMa] came up with a space-optimal solution which works for general subdivisions, and even for sets of arbitrary non-overlapping regions. The query time is $Q=O(\log^3 n)$, but it can be improved to $Q=O(\log^2 n)$ for rectangular subdivisions, where the structure becomes especially simple. For this reason a generalization to rectangular point location in higher dimensions has also succeeded; see Edelsbrunner, Haring, and Hilbert [\EHH] The purpose of this paper is to show an elegant modification to the separating chain method of Lee and Preparata that yields a new optimal point location algorithm for monotone subdivisions of the plane; the algorithm is based on a new data structure called the {\it layered dag}. In this new structure the separating chains built into a binary tree by Lee and Preparata are refined so that (1) once a point has been discriminated against a chain, it can be discriminated against a child of that chain with constant extra effort, and (2) the overall storage only doubles. The layered dag simultaneously attains $S = O(m)$ and $Q = O(\log m)$. An additional insight allows us to build this dag (as well as the original Lee-Preparata tree) in only $O(m)$ time. Not only is this new structure optimal with respect to all of preprocessing, space, and query costs, but in fact a simple implementation, with small constants of proportionality, is possible. Like its Lee-Preparata predecessor, it also extends to non-straight-line subdivisions. In the organization of the paper we have adopted the policy that each new data structure is first introduced by how it is to be used, and then by how it is to be constructed. We have placed emphasis throughout on implementation considerations, as well as the underlying theory. Section 2 describes the basic notions surrounding monotone polygons and subdivisions. Section 3 shows how non-monotone subdivisions can be made monotone. Sections 4, 5, and 6 introduce a partial ordering of the regions and its use in getting a complete family of separators. Section 7 reviews the Lee-Preparata structure, while sections 8 and 9 introduce the layered dag, and explain its use in point location and its construction. Section 10 gives an algorithm for constructing a complete family of separators based on a traversal of the subdivision. Two key implementation issues are taken up in sections 11 and 12; these may be omitted on a first reading. Section 11 describes some bit-twiddling trickery used to give us linear preprocessing time, while section 12 discusses how subdivisions can be traversed without auxiliary storage. Finally section 13 contains some further applications of the layered dag to problems in computational geometry. \section{2. Monotone polygons and subdivisions} An {\it interval} is a convex subset of a straight line, i.e., the whole line, a ray, a segment, a single point, or the empty set. An interval is {\it proper} if it contains more than one point, and is {\it closed} or {\it open} depending on whether it contains or not its endpoints (if any). A subset of the plane is said to be {\it $y$-monotone} if its intersection with any line parallel to the $y$ axis is a single interval (possibly empty). Every convex polygon is $y$-monotone, but the converse is obviously false. For any direction $d$ the concept of a $d$-monotone polygon can similarly be defined. Throughout this paper the regions, polygons, etc., we will be dealing with will all be monotone in the {\it same} direction. Without loss of generality we can then assume that they are all monotone in $y$. From now on the term ``monotone'' will be used as a synonym for ``$y$-monotone''. We define a {\it polygon} as an open and simply connected subset of the plane whose boundary can be decomposed into a finite number of points ({\it vertices}) and open intervals ({\it edges}). Note that, according to these definition, polygons may have infinite extent. A {\it subdivision} is a partition of the plane into a finite number of disjoint polygons ({\it regions}), edges, and vertices; these parts are collectively called the {\it elements} of the subdivision. It is not hard to see that every edge of a subdivision is on the boundary between two regions (not necessarily distinct), that every vertex is incident to some edge, that every finite endpoint of an edge is a vertex, and that every region (unless there is only one) has some edge on its boundary. From these facts and from Euler's theorem, we can conclude that a subdivision with $m$ edges has $O(m)$ vertices and regions, thus justifying our use of $m$ as the measure of problem size. A subdivision is said to be {\it monotone} if all its regions are monotone and it has no vertical edges. The last condition is a technical one: it is imposed only to simplify the proofs and algorithms and can be removed with some care. Figure 1 illustrates a monotone subdivision (arrowheads denote vertices at infinity). With minor caveats and modifications, monotone subdivisions include many interesting subcases, such as triangulations, subdivisions of the plane into convex pieces, and so forth. The lemma below shows that monotone subdivisions are precisely the ``regular planar straight-line graphs'' as defined by Lee and Preparata [\LeePre]: \lemma{\Thei}{A subdivision with no vertical edges is monotone if and only if every vertex is incident to at least two distinct edges, one at its left and one at its right.} \proof{First, assume there is a vertex $v$ such that all edges incident to it lie to the right of $v$. Let $R$ be the region that is first encountered by moving down from $v$; it must also be the same region we encounter first when moving up from $v$. Then $R$ is not monotone, for its intersection with the vertical line passing through $v$ consists of at least two distinct intervals separated by $v$. A similar argument holds if all edges incident to $v$ lie to its left. \pp For the converse, assume $R$ is a region that is not monotone, and let $\lscr$ be a vertical line whose intersection with $R$ consists of two or more components, as in figure 2. Since $R$ is connected, any two components $I1$, $I2$ of the intersection are connected by a simple path $\pi$ in $R$. We can always choose $I1$, $I2$, and $\pi$ so that $I1$ is above $I2$, and $\pi$ does not meet $\lscr$ except at its endpoints $p1\in I1$ and $p2\in I2$. Then $\pi$ and the interval $[p1, p2]$ delimit a closed and bounded region $S$ of the plane. \pp Suppose $\pi$ lies to the left of $\lscr$. The boundary of $R$ must enter $S$ at the lower endpoint of $I1$ (and also at the upper endpoint of $I2$). Since the boundary cannot meet the path $\pi$, there must be some vertex of the subdivision in the interior of $S$. Let $v$ be the {\it leftmost} such vertex; clearly all edges incident to $v$ lie to the right of it, and we have proved the lemma. A similar argument holds if $\pi$ lies to the right of $\lscr$.} Therefore, we can check whether any subdivision is monotone by enumerating all the edges incident to each vertex, and checking whether they satisfy lemma {\Thei}. Each edge will be examined at most twice, and each vertex once, so this algorithm runs in $O(m)$ time. In fact, it is possible to determine within this time bound whether there is any rotation of the subdivision that makes it monotone, as was shown by Preparata and Supowit [\PreSup]. \section{3. Regularization} Lee and Preparata [\LeePre] have shown that an arbitrary subdivision with $m$ edges can be transformed into a monotone subdivision having $\leq 2m$ edges in $O(m\log m)$ time, a process they termed {\it regularization}. (They define a vertex as being {\it regular} if it satisfies the condition of lemma {\Thei}.) For the sake of completeness we include here an algorithm for regularization attaining these bounds. We believe that our explanation is more complete and at the same time more succint than that of [\LeePre]. The problem of whether regularization can be done in time faster than $O(m\log m)$ remains open. The task of the regularization process is to add additional edges to the subdivision so as to make each vertex regular. These new edges must maintain the ``subdivisionhood'' of our structure: they must connect existing vertices, may not intersect other edges except at endpoints, and will therefore subdivide existing regions into subregions. An example of a non-monotone subdivision and a possible regularization of it is shown in figure 3. The algorithm we propose in this section is based on the {\it sweep-line} paradigm, that has extensively been used in computational geometry. We imagine that a vertical line sweeps the plane from left to right. We call {\it active} those vertices, edges, and regions currently intersecting the sweeping line. The active edges have an obvious vertical ordering, except possibly at a vertex. In fact this ordering can change only when the sweep-line passes through a vertex. At that instant some edges may end and others may start. Each interval of the sweep-line between successive intersections by edges can be assigned a vertex we call its {\it generator}, which represents the last vertex that gave rise to this interval. Formally this is defined as the last vertex swept over that happened to lie in the interval between the two edges in question. This vertex may be a left endpoint of one of the two edges, as figure 4 shows, or it may an irregular vertex with no right-going edge, as figure 5 shows. The regularization algorithm starts out by sorting all the vertices of the subdivision $\Sscr$ according to their $x$-coordinate. Conceptually we imagine that the sweep line jumps from one corridor between successive vertices to the next, because within a corridor neither the ordering of the active edges nor the generators of any intervals can change. In practice we maintain a balanced tree of all the active edges, ordered according to the $y$-coordinate where they intersect the sweeping line. As we pass each vertex $v$, the list of active edges is updated by standard balance tree insertion and deletion operations. It is also straightforward to see how to update the generators as we pass through the vertex $v$. The new vertex simply becomes the generator for each interval bounded by an edge starting at $v$. If, however, $v$ has no right-going edges, it becomes the generator for its immediately enclosing interval. The regularization algorithm can now be stated as follows: when a vertex $v$ is encountered that has no left-going edges, connect it by an edge to the generator of the interval that $v$ happened to fall in. This satisfies all the left-irregular vertices, and a symmetric pass of the sweep line from right to left will satisfy the right-irregular vertices. We omit here the actual code for this algorithm as it is quite simple and not in the mainstream of the results of this paper. \lemma{\Theii}{The edges added in the regularization process connect vertices and do not intersect any previously existing edges. Thus the result of regularization is a new monotone subdivision.} \proof{Consider the sweep-line when it has just arrived at the left-irregular vertex $v$, as in figure 6. Let $a$ and $b$ denote the edges immediately above and below $v$ in the vertical ordering, and let $w$ denote the generator of the interval $[a,b]$. We claim that the straight-line segment from $v$ to $w$ does not intersect any other edge of the subdivision. This follows because the hatched region in figure 6 is free of other vertices or edges of the subdivision, by the definition of the generator $w$.} The dominant term in the complexity of this regularization process is the sorting of the vertices and maintaining the balanced tree; thus the cost in time is $O(m\log m)$. We have not dealt with the case of vertical edges, but the required modifications to the above algorithm are not difficult. \section{4. The vertical ordering} We denote by $\proj{A}$ the orthogonal projection of a set $A$ on the $x$-axis. The projection $\proj{R}$ of a monotone polygon is an open interval of the $x$-axis, whose endpoints, if any, are the projections of at most two {\it extremal vertices} of $R$. The {\it frontier} of $R$ is the boundary of $R$ minus its extremal vertices. This frontier consists of two disjoint pieces, the {\it top} and the {\it bottom} of the region $R$. Each is either empty, or a connected polygonal line, which may extend to infinity in one or both directions. Given two subsets $A$ and $B$ of the plane, we say that $A$ is above $B$ ($A\above B$), and $B$ is below $A$ ($B\below A$), if for every pair of vertically aligned points $(x, ya)$ of $A$ and $(x, yb)$ of $B$ we have $ya \geq yb$, with strict inequality holding at least once. Some of these concepts are illustrated in figure 7. For the rest of this section, we will restrict $\above$ to the elements of a fixed monotone subdivision $\Sscr$, with $n$ regions and $m$ edges. Then $\above$ has the following properties: \lemma{\Theiii}{For any two elements $A$ and $B$ of a monotone subdivision, $A\above B$ if and only if $A\neq B$ and there is some vertical line segment with lower endpoint in $B$ and upper endpoint in $A$.} \proof{The ``only if'' part is obvious from the definition. For the converse, assume there is such a segment with endpoints $(x, ya)\in A$ and $(x, yb)\in B$, $ya>yb$, but suppose $A\not\above B$. Then there should be two other points $(x\pr, ya\pr)\in A$ and $(x\pr, yb\pr)\in B$ such that $ya\pr2$. \pp The $x$-projections of any two consecutive elements $Ei$ and $E{i+1}$ in this cycle must have some abscissa $xi$ in common. If for some $i$ we had $\proj{E{i}}\subset \proj{E{i-1}}$, then the vertical line through $xi$ would meet $E{i-1}$, $Ei$, and $E{i+1}$; transitivity would hold, and we would have $E{i-1}\below E{i+1}$. See figure 10. But then we could omit $Ei$ and still get a cycle, contradicting the assumption that $k$ is minimum. For analogous reasons, we cannot have $\proj{E{i}}\subset \proj{E{i+1}}$. \pp Let $Ei$ be one of the ``leftmost'' cycle elements, i.e., such that none of the intervals $\proj{Ej}$ contains a point to the left of $\proj{Ei}$. The projections $\proj{E{i-1}}$ and $\proj{E{i+1}}$ of its neighbors must both meet $\proj{E{i}}$, and, by what we have just said, extend beyond its right endpoint. But then there would be again a vertical line meeting all three elements, implying $E{i-1}\below E{i+1}$ and $k$ is not minimum. We conclude that no such cycle exists.} We say that a region $A$ is {\it immediately above} a region $B$ ($A\justabove B$) if $A\above B$ and the frontiers of the two regions have at least one common edge; see figure 11. Clearly, $\justabove$ implies $\above$, but the converse is not true in general. The following result may then seem somewhat surprising: \lemma{\Thevi}{The transitive closure of $\below$ is the same as that of $\justbelow$} \proof{Clearly, since $\justbelow$ implies $\below$, the same will be true of their transitive closures, so we only have to prove the other direction. \pp If $B\below A$, for every $x\in \proj{A}\inte\proj{B}$ there is a vertical segment with abcissa $x$, lower endpoint in $B$, and upper endpoint in $A$. We can always choose $x$ so that the segment passes through no vertex of $\Sscr$. The regions $A, C1, C2, \ldots, Ck, B$ intercepted by this segment, from bottom to top, clearly satisfy $A\justbelow C1\justbelow C2 \justbelow \ldotss \justbelow Ck\justbelow B$. We conclude that $\below$, and therefore its transitive closure, imply the transitive closure of $\justbelow$.} \section{5. Separators} A {\it separator} for $\Sscr$ is a polygonal line $s$, consisting of vertices and edges of $\Sscr$, with the property that it meets every vertical line at exactly one point. Since $s$ extends from $x=-\infty$ to $x=+\infty$, any element of $\Sscr$ that is not part of $s$ is either above or below it. See figure 12. The elements of $s$ have pairwise disjoint projections on the $x$-axis, and so can be ordered from left to right; the first and last elements are infinite edges. A {\it complete family of separators} for a monotone subdivision $\Sscr$ with $n$ regions is a sequence of $n-1$ distinct separators $s1\below s2 \below \ldotss \below s{n-1}$. It is clear that there must be at least one region between any two consecutive separators, and also one below $s1$ and one above $s{n-1}$. If a region is below a separator $si$, it must also be below any separator $sj$ with $j>i$. We can conclude that, if $\Sscr$ admits a complete family of separators, its regions can be enumerated as $R0, R1, \ldots, R{n-1}$ in such a way that $Ri\below sj$ if and only if $i< j$; in particular, $$R0\below s1 \below R1 \below s2 \below \ldots \below s{n-1} \below R{n-1}.\eqno(5.1)$$ Given a complete family of separators and an enumeration of the regions as in (5.1), let us denote by $\regionindex R$ the index of a region $R$ in the enumeration. Then $s{\regionindex R}\below R\below s{\regionindex R + 1}$. Also, if we let $\regionabove(e)$ be the region above the edge or vertex $e$, and $\regionbelow(e)$ the one below it, then the following property holds: \lemma{\Thevii}{If $i=\regionindex{\regionbelow(e)}$ and $j=\regionindex{\regionabove(e)}$, then the separators containing $e$ will be exactly $s{i+1}, s{i+2}, \ldots, sj$.} \proof{The edge $e$ will belong to a separator $sk$ if and only if the latter lies above $Ri$ and below $Rj$, i.e., if and only if $i< k\leq j$.} \section{6. Existence of a complete family of separators} It is a well-known result [\Knu] that any acyclic relation over a finite set can be extended to a linear (or total) order. Therefore, by using lemma {\Thev} we can conclude that there is an enumeration $R0, R1, \ldots, R{n-1}$ of the the regions of $\Sscr$ that is compatible with $\below$, i.e. such that $Ri\below Rj$ implies $ii$. That is, a vertex or edge $e$ belongs to $si$ if and only if it is on the frontiers of two regions $Rk$ and $Rj$ with $k< i \leq j$. See figure 12. \pp Now consider any vertical line $\lscr$, and let $R{i1}, R{i2}, \ldots, R{iq}$ be the regions it meets, from bottom to top. Since $\lscr$ meets $R0$ and $R{n-1}$, and $R{i1}\below R{i2}\below \ldots\below R{iq}$, we will have $0=ii