Efficient Computation of Visibility Polygons
Total Page:16
File Type:pdf, Size:1020Kb
EuroCG 2014, Ein-Gedi, Israel, March 3{5, 2014 Efficient Computation of Visibility Polygons Francisc Bungiu∗ Michael Hemmery John Hershbergerz Kan Huangx Alexander Kr¨ollery Abstract a subroutine in algorithms for other problems, most prominently in the context of the Art Gallery Prob- Determining visibility in planar polygons and ar- lem [16]. In experimental work on this problem [15] rangements is an important subroutine for many al- we have identified visibility computations as having a gorithms in computational geometry. In this paper, substantial impact on overall computation times, even we report on new implementations, and correspond- though it is a low-order polynomial-time subroutine in ing experimental evaluations, for two established and an algorithm solving an NP-hard problem. Therefore one novel algorithm for computing visibility polygons. it is of enormous interest to have efficient implemen- These algorithms will be released to the public shortly, tations of visibility polygon algorithms available. as a new package for the Computational Geometry CGAL, the Computational Geometry Algorithms Algorithms Library (CGAL). Library [5], contains a large number of algorithms and data structures, but unfortunately not for the 1 Introduction computation of visibility polygons. We present im- plementations for three algorithms, which form the Visibility is a basic concept in computational geome- basis for an upcoming new CGAL package for visi- try. For a polygon P R2, we say that a point p P bility. Two of these are efficient implementations for ⊂ 2 is visible from q P if the line segment pq P . The standard O(n)- and O(n log n)-time algorithms from 2 ⊆ points that are visible from q form the visibility re- the literature. In addition, we describe a novel algo- gion (q). Usually (if no degeneracies occur) (q) is a rithm with preprocessing, called triangular expansion, V V polygon; hence it is often called the visibility polygon. based on an intriguingly simple idea using a triangu- If there are multiple vertices collinear with q, then lation of the underlying polygon. Even though this (q) may take the form of a polygon with attached algorithm has a worst-case query runtime of O(n2), it V one-dimensional antennae (see Figure 1). is extremely fast in practice. In experiments we have Computing (q), given a query point q, is a well- found it to outperform the other implementations by V known problem with a number of established algo- about two orders of magnitude on large and practi- rithms; see the book by Ghosh [9]. Besides being cally relevant inputs. The implementations follow the of interest on its own, it also frequently appears as exact computation paradigm and handle all degener- ∗Institute of Information Security at ETH Z¨urich, Switzer- ate cases, whether or not to include antennae in the land. [email protected] output is configurable. yDepartment of Computer Science, TU Braunschweig, Ger- Our contribution is therefore threefold: We present many. [email protected],[email protected] a new algorithm for computing visibility polygons. zMentor Graphics Corp., Wilsonville, OR, USA. john [email protected] We report on an experimental evaluation, demon- xDepartment of Applied Mathematics & Statistics, New strating the efficiency of the implementations, and re- York, USA. [email protected] vealing superior speed of the novel algorithm. The publication of the code is scheduled for CGAL re- lease 4.5 (October 2014). 2 Related Work For a detailed coverage of visibility algorithms in two dimensions, see the book [9]. We denote by n the number of vertices of the input polygon P , by h the number of its holes, and by k the complexity of the visibility region (q) of the query point q. V The problem of computing (q) was first addressed for simple polygons in [7]. TheV first correct O(n) time algorithm was given by Joe and Simpson [14]. For a polygon with holes, O(n log n) time algorithms were Figure 1: Visibility polygon (q) with antenna bc. proposed by Suri et al. [17] and Asano et al. [3]. An V This is an extended abstract of a presentation given at EuroCG 2014. It has been made public for the benefit of the community and should be considered a preprint rather than a formally reviewed paper. Thus, this work is expected to appear in a conference with formal proceedings and/or in a journal. 30th European Workshop on Computational Geometry, 2014 optimal O(n + h log h) algorithm was later given by states are entered when vivi+1 turns behind a previ- Heffernan and Mitchell [11]. ous edge. Then the algorithm scans the subsequent Ghodsi et al. [10] reduced the query time for simple edges until the boundary reappears from behind the polygons to O(log n + k), at the expense of prepro- last visible edge. In order to deal with cases in which cessing requiring O(n3) time and space. In [4] Bose the polygon winds more than 360◦, a winding counter et al. showed that this time can also be achieved for is used during this edge processing. points outside of P with an O(n3 log n) preprocessing time. Aronov et al. [1] reduced the preprocessing time 3.2 Algorithm of Asano [2] and space to O(n2 log n) and O(n2) respectively with 2 the query time being increased to O(log n + k). The algorithm by Asano [2] runs in O(n log n) time Allowing preprocessing for polygons with holes, and O(n) space. It follows the classical plane sweep Asano et al. [3] presented an algorithm with O(n) paradigm with the difference that the event line L is a query time that requires O(n2) preprocessing time ray that originates from and rotates around the query and space. Vegter [18] gave an output sensitive algo- point q. Thus, initially all vertices of the input poly- rithm whose query time is O(k log(n=k)), where pre- gon (event points) are sorted according to their polar processing takes O(n2 log n) time and O(n2) space. angles with respect to the query point q. Segments Zarei and Ghodsi [19] gave another output sensitive that intersect the ray L are stored in a balanced bi- algorithm whose query time is O(1+min(h; k) log n+ nary tree T based on their order of intersections with k) with O(n3 log n) preprocessing time and O(n3) L. As the sweep proceeds, T is updated and a new space. Recent work by Inkulu and Kapoor [12] ex- vertex of V (q) is generated each time the smallest el- tends the approaches of [1] and [19] by incorporating ement (segment closest to q) in T changes. a trade-off between preprocessing and query time. The algorithm can be implemented using basic al- Surprisingly, it seems that there are no exact and gorithms and data structures such as std::sort (for complete implementations of these algorithms at all. ordering event points) and std::map (ordering seg- The only available software is GEOMPACK [13] but ments on the ray). However, it is crucial that the it does not use exact arithmetic and only implements underlying comparison operations are efficient. We the algorithm for simple polygons of Joe and Simp- omit details of the angular comparison for vertices son [14]. and sketch the ordering along L. For an efficient compari- b1 son of two segments s1 and 3 Implemented Algorithms b2 s2 along the ray L, it is s2 The following algorithms are implemented in C++, and important to avoid explicit s1 are part of an upcoming CGAL package for visibility. (and expensive) construc- a2 tion of intersection points with L. In fact, L can stay 3.1 Algorithm of Joe and Simpson [14] completely abstract. As- a1 The visibility polygon algorithm by Joe and Simp- suming that s1 and s2 have q son [14] runs in O(n) time and space. It performs a no common endpoint, the sequential scan of the boundary of P , a simple poly- order can be determined using at most five orientation gon (without holes), and uses a stack s of boundary predicates. Consider the figure to the right: the seg- points s0; s1; : : : ; st such that at any time, the stack ment s1 is further away since q, a2 and b2 are on the represents the visibility polygon with respect to the same side of s1 (three calls). In case this is not con- already scanned part of P 's boundary. clusive, two additional calls for q and a1 with respect When the current edge vivi+1 is in process, three to s2 are sufficient. possible operations can be performed: new bound- ary points are added to the stack, obsolete boundary 3.3 New Algorithm: Triangular Expansion points are deleted from the stack, or subsequent edges on the polygon's boundary are scanned until a certain We introduce a new algorithm that, to the best of condition is met. The later is performed when the our knowledge, has not been discussed in the litera- edge enters a so-called \hidden-window," where the ture. Its worst case complexity is O(n2) but it is very points are not visible from the viewpoint q. efficient in practice as it is, in some sense, output sen- The implementation uses states to process the sitive. boundary of the polygon P and manipulate the stack In an initial preprocessing phase the polygon is tri- s such that, in the end, it contains the vertices defin- angulated. For polygons with holes this is possible in ing the visibility polygon's boundary. The main state O(n log n) time [8] and in O(n) time for simple poly- handles the case when the current edge vivi+1 takes a gons [6].