Visibility polygon

From HandWiki
Revision as of 20:29, 6 February 2024 by Scavis2 (talk | contribs) (change)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Polygonal region of all points visible from a given point in a plane
Visibility polygon shown in yellow. Four obstacles are shown in blue.

In computational geometry, the visibility polygon or visibility region for a point p in the plane among obstacles is the possibly unbounded polygonal region of all points of the plane visible from p. The visibility polygon can also be defined for visibility from a segment, or a polygon. Visibility polygons are useful in robotics, video games, and in various optimization problems such as the facility location problem and the art gallery problem.

If the visibility polygon is bounded then it is a star-shaped polygon. A visibility polygon is bounded if all rays shooting from the point eventually terminate in some obstacle. This is the case, e.g., if the obstacles are the edges of a simple polygon and p is inside the polygon. In the latter case the visibility polygon may be found in linear time.[1][2][3][4]

Definitions

Formally, we can define the planar visibility polygon problem as such. Let [math]\displaystyle{ S }[/math] be a set of obstacles (either segments, or polygons) in [math]\displaystyle{ \mathbb{R}^2 }[/math]. Let [math]\displaystyle{ p }[/math] be a point in [math]\displaystyle{ \mathbb{R}^2 }[/math] that is not within an obstacle. Then, the point visibility polygon [math]\displaystyle{ V }[/math] is the set of points in [math]\displaystyle{ \mathbb{R}^2 }[/math], such that for every point [math]\displaystyle{ q }[/math] in [math]\displaystyle{ V }[/math], the segment [math]\displaystyle{ pq }[/math] does not intersect any obstacle in [math]\displaystyle{ S }[/math].

Likewise, the segment visibility polygon or edge visibility polygon is the portion visible to any point along a line segment.

Applications

Visibility polygons are useful in robotics. For example, in robot localization, a robot using sensors such as a lidar will detect obstacles that it can see, which is similar to a visibility polygon.[5]

They are also useful in video games, with numerous online tutorials explaining simple algorithms for implementing it.[6][7][8]

Algorithms for point visibility polygons

Numerous algorithms have been proposed for computing the point visibility polygon. For different variants of the problem (e.g. different types of obstacles), algorithms vary in time complexity.

Naive algorithms

Naive algorithms are easy to understand and implement, but they are not optimal, since they can be much slower than other algorithms.

Uniform ray casting "physical" algorithm

In real life, a glowing point illuminates the region visible to it because it emits light in every direction. This can be simulated by shooting rays in many directions. Suppose that the point is [math]\displaystyle{ p }[/math] and the set of obstacles is [math]\displaystyle{ S }[/math]. Then, the pseudocode may be expressed in the following way:

algorithm naive_bad_algorithm([math]\displaystyle{ p }[/math], [math]\displaystyle{ S }[/math]) is
    [math]\displaystyle{ V }[/math] := [math]\displaystyle{ \emptyset }[/math]
    for [math]\displaystyle{ \theta = 0, \cdots, 2\pi }[/math]:
        // shoot a ray with angle [math]\displaystyle{ \theta }[/math]
        [math]\displaystyle{ r }[/math] := [math]\displaystyle{ \infty }[/math]
        for each obstacle in [math]\displaystyle{ S }[/math]:
            [math]\displaystyle{ r }[/math] := min([math]\displaystyle{ r }[/math], distance from [math]\displaystyle{ p }[/math] to the obstacle in this direction)
        add vertex [math]\displaystyle{ (\theta, r) }[/math] to [math]\displaystyle{ V }[/math]
    return [math]\displaystyle{ V }[/math]

Now, if it were possible to try all the infinitely many angles, the result would be correct. Unfortunately, it is impossible to really try every single direction due to the limitations of computers. An approximation can be created by casting many, say, 50 rays spaced uniformly apart. However, this is not an exact solution, since small obstacles might be missed by two adjacent rays entirely. Furthermore, it is very slow, since one may have to shoot many rays to gain a roughly similar solution, and the output visibility polygon may have many more vertices in it than necessary.

Ray casting to every vertex

The previously described algorithm can be significantly improved in both speed and correctness by making the observation that it suffices to only shoot rays to every obstacle's vertices. This is because any bends or corners along the boundary of a visibility polygon must be due to some corner (i.e. a vertex) in an obstacle.

algorithm naive_better_algorithm([math]\displaystyle{ p }[/math], [math]\displaystyle{ S }[/math]) is
    [math]\displaystyle{ V }[/math] := [math]\displaystyle{ \emptyset }[/math]
    for each obstacle [math]\displaystyle{ b }[/math] in [math]\displaystyle{ S }[/math]:
        for each vertex [math]\displaystyle{ v }[/math] of [math]\displaystyle{ b }[/math]:
            // shoot a ray from [math]\displaystyle{ p }[/math] to [math]\displaystyle{ v }[/math]
            [math]\displaystyle{ r }[/math] := distance from [math]\displaystyle{ p }[/math] to [math]\displaystyle{ v }[/math]
            [math]\displaystyle{ \theta }[/math] := angle of [math]\displaystyle{ v }[/math] with respect to [math]\displaystyle{ p }[/math]
            for each obstacle [math]\displaystyle{ b' }[/math] in [math]\displaystyle{ S }[/math]:
                [math]\displaystyle{ r }[/math] := min([math]\displaystyle{ r }[/math], distance from [math]\displaystyle{ p }[/math] to [math]\displaystyle{ b' }[/math])
            add vertex [math]\displaystyle{ (\theta, r) }[/math] to [math]\displaystyle{ V }[/math]
    return [math]\displaystyle{ V }[/math]

The above algorithm may not be correct. See the discussion under Talk.

The time complexity of this algorithm is [math]\displaystyle{ O(n^2) }[/math]. This is because the algorithm shoots a ray to every one of the [math]\displaystyle{ n }[/math] vertices, and to check where the ray ends, it has to check for intersection with every one of the [math]\displaystyle{ O(n) }[/math] obstacles. This is sufficient for many simple applications such as video games, and as such many online tutorials teach this method.[8] However, as we shall see later, there are faster [math]\displaystyle{ O(n\log n) }[/math] algorithms (or even faster ones if the obstacle is a simple polygon or if there are a fixed number of polygonal holes).

Optimal algorithms for a point in a simple polygon

A visibility polygon for a point in the center (shown in white) inside a simple polygon, outlined in black.

Given a simple polygon [math]\displaystyle{ \mathcal{P} }[/math] and a point [math]\displaystyle{ p }[/math], a linear time algorithm is optimal for computing the region in [math]\displaystyle{ \mathcal{P} }[/math] that is visible from [math]\displaystyle{ p }[/math]. Such an algorithm was first proposed in 1981.[2] However, it is quite complicated. In 1983, a conceptually simpler algorithm was proposed,[3] which had a minor error that was corrected in 1987.[4]

The latter algorithm will be briefly explained here. It simply walks around the boundary of the polygon [math]\displaystyle{ \mathcal{P} }[/math], processing the vertices in the order in which they appear, while maintaining a stack of vertices [math]\displaystyle{ \mathcal{S}=s_0, s_1,\cdots, s_t }[/math] where [math]\displaystyle{ s_t }[/math] is the top of the stack. The stack constitutes the vertices encountered so far which are visible to [math]\displaystyle{ p }[/math]. If, later during the execution of the algorithm, some new vertices are encountered that obscure part of [math]\displaystyle{ \mathcal{S} }[/math], then the obscured vertices in [math]\displaystyle{ \mathcal{S} }[/math] will be popped from the stack. By the time the algorithm terminates, [math]\displaystyle{ \mathcal{S} }[/math] will consist of all the visible vertices, i.e. the desired visibility polygon. An efficient implementation was published in 2014.[9]

Optimal algorithms for a point in a polygon with holes

For a point in a polygon with [math]\displaystyle{ h }[/math] holes and [math]\displaystyle{ n }[/math] vertices in total, it can be shown that in the worst case, a [math]\displaystyle{ \Theta(n + h\log h) }[/math] algorithm is optimal. Such an algorithm was proposed in 1995 together with its proof of optimality.[10] However, it relies on the linear time polygon triangulation algorithm by Chazelle, which is extremely complex.

Optimal algorithms for a point among segments

Segments that do not intersect except at their endpoints

A visibility polygon for a point in the center (shown in white) amongst a set of arbitrary line segments in the plane, allowed to intersect only at their endpoints, acting as obstacles (shown in black).

For a point among a set of [math]\displaystyle{ n }[/math] segments that do not intersect except at their endpoints, it can be shown that in the worst case, a [math]\displaystyle{ \Theta(n\log n) }[/math] algorithm is optimal. This is because a visibility polygon algorithm must output the vertices of the visibility polygon in sorted order, hence the problem of sorting can be reduced to computing a visibility polygon.[11]

Notice that any algorithm that computes a visibility polygon for a point among segments can be used to compute a visibility polygon for all other kinds of polygonal obstacles, since any polygon can be decomposed into segments.

Divide and conquer

A divide-and-conquer algorithm to compute the visibility polygon was proposed in 1987.[12]

Angular sweep

An angular sweep, i.e. rotational plane sweep algorithm to compute the visibility polygon was proposed in 1985[13] and 1986.[11] The idea is to first sort all the segment endpoints by polar angle, and then iterate over them in that order. During the iteration, the event line is maintained as a heap. An efficient implementation was published in 2014.[9]

Generally intersecting segments

For a point among generally intersecting segments, the visibility polygon problem is reducible, in linear time, to the lower envelope problem. By the Davenport–Schinzel argument, the lower envelope in the worst case has [math]\displaystyle{ \Theta(n\alpha(n)) }[/math] number of vertices, where [math]\displaystyle{ \alpha(n) }[/math] is the inverse Ackermann function. A worst case optimal divide-and-conquer algorithm running in [math]\displaystyle{ \Theta(n\log n) }[/math] time was created by John Hershberger in 1989.[14]

References

  1. Franco P. Preparata and Michael Ian Shamos (1985). Computational Geometry - An Introduction. Springer-Verlag. 1st edition; 2nd printing, corrected and expanded, 1988: ISBN:3-540-96131-3; Russian translation, 1989: ISBN:5-03-001041-6. ISBN 0-387-96131-3. 
  2. 2.0 2.1 El Gindy, Hossam; Avis, David (1981). "A linear algorithm for computing the visibility polygon from a point". Journal of Algorithms 2 (2): 186–197. doi:10.1016/0196-6774(81)90019-5. 
  3. 3.0 3.1 Lee, D. T. (May 1983). "Visibility of a simple polygon". Computer Vision, Graphics, and Image Processing 22 (2): 207–221. doi:10.1016/0734-189X(83)90065-8. 
  4. 4.0 4.1 Joe, Barry; Simpson, R. B. (1987). "Corrections to Lee's visibility polygon algorithm". BIT Numerical Mathematics 27 (4): 458–473. doi:10.1007/BF01937271. 
  5. Guibas, Leonidas; Motwani, Rajeev; Raghavan, Prabhakar (1992). "The robot localization problem in two dimensions". ACM-SIAM symposium on Discrete algorithms. Society for Industrial and Applied Mathematics. 
  6. Liow, Nicklaus. "SIGHT & LIGHT how to create 2D visibility/shadow effects for your game". http://ncase.me/sight-and-light/. 
  7. Patel, Amit (5 July 2012). "2d Visibility Algorithm". http://www.redblobgames.com/articles/visibility/. 
  8. 8.0 8.1 Patel, Amit (5 July 2012). "Blobs in Games: 2d Visibility". http://simblob.blogspot.ca/2012/07/2d-visibility.html. 
  9. 9.0 9.1 Bungiu, Francisc; Hemmer, Michael; Hershberger, John; Huang, Kan; Kröller, Alexander (2014). "Efficient Computation of Visibility Polygons". arXiv:1403.3905 [cs.CG].
  10. Heffernan, Paul; Mitchell, Joseph (1995). "An optimal algorithm for computing visibility in the plane". SIAM Journal on Computing 24 (1): 184–201. doi:10.1137/S0097539791221505. https://ecommons.cornell.edu/bitstream/1813/8838/1/TR000953.pdf. 
  11. 11.0 11.1 Suri, Subhash; O'Rourke, Joseph (1986). "Worst-case optimal algorithms for constructing visibility polygons with holes". Symposium on Computational geometry. ACM. pp. 14–23. doi:10.1145/10515.10517. 
  12. Template:Cite tech report
  13. Asano, Tetsuo (1985). "An efficient algorithm for finding the visibility polygon for a polygonal region with holes.". Institute of Electronics, Information, and Communication Engineers. 68. pp. 557–559. 
  14. Hershberger, John (1989). "Finding the upper envelope of [math]\displaystyle{ n }[/math] line segments in [math]\displaystyle{ O(n\log n) }[/math] time". Information Processing Letters 33 (4): 169–174. doi:10.1016/0020-0190(89)90136-1. 

External links

Software

  • VisiLibity: A free open source C++ library for visibility computations in planar polygonal environments.
  • visibility-polygon-js: A public domain Javascript library for computing a visibility polygon for a point among segments using the angular sweep method.