HTML conversions sometimes display errors due to content that did not convert correctly from the source. This paper uses the following packages that are not yet supported by the HTML conversion tool. Feedback on these issues are not necessary; they are known and are being worked on.

  • failed: mhchem
  • failed: hanging

Authors: achieve the best HTML results from your LaTeX submissions by following these best practices.

License: CC BY 4.0
arXiv:2312.16363v1 [cs.CG] 27 Dec 2023

Polygon Detection from a Set of Lines

Alfredo Ferreira Jr. Manuel J. Fonseca Joaquim A. Jorge
Department of Information Systems and Computer Science, INESC-ID/IST/University of Lisboa
R. Alves Redol, 9, 1000-029 Lisboa, Portugal
[email protected], [email protected], [email protected]
(October 8, 2003)
Abstract

Detecting polygons defined by a set of line segments in a plane is an important step in analyzing vector drawings. This paper presents an approach combining several algorithms to detect basic polygons from arbitrary line segments. The resulting algorithm runs in polynomial time and space, with complexities of O((N+M)4)𝑂superscript𝑁𝑀4O\left((N+M)^{4}\right)italic_O ( ( italic_N + italic_M ) start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) and O((N+M)2)𝑂superscript𝑁𝑀2O\left((N+M)^{2}\right)italic_O ( ( italic_N + italic_M ) start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) respectively, where N𝑁Nitalic_N is the number of line segments and M𝑀Mitalic_M is the number of intersections between line segments. Our choice of algorithms was made to strike a good compromise between efficiency and ease of implementation. The result is a simple and efficient solution to detect polygons from lines.

keywords

Polygon Detection, Segment Intersection, Minimum Cycle Basis

1 Introduction

Unlike image processing, where data consist of raster images, our algorithm deals with drawings in vector format, consisting of line segments. This requires completely different approaches, such as described here.

We divide this task into four major steps to perform polygon detection from a set of line segments. First, we detect line segment intersections using the Bentley-Ottmann algorithm [15]. The next step creates a graph induced by the drawing, where vertices represent endpoints or proper intersection points of line segments and edges represent maximal relatively open subsegments that contain no vertices. The third step finds the Minimum Cycle Basis (MCB) [20] of the graph induced in the previous step, using Horton’s algorithm [14]. The last step constructs a set of polygons based on cycles in the previously found MCB. This is straightforward if we transform each cycle into a polygon, where each node represents a polygon vertex, and each edge in the cycle represents an edge in the polygon.

A previous version of this paper was presented in [16]. In sections 2 and 3, we describe the four steps of our method. Section 4 presents the whole algorithm and experimental results in section 55. Finally, in section 6, we discuss conclusions and future work.

2 Intersection Removal

In a vector drawing composed of a set of line segments, many intersections might exist between these segments. To detect polygonal shapes, we must remove proper segment intersections, thus creating a new set of line segments in which any pair of segments share at most one endpoint.

2.1 Finding line segment intersections

The first step of our approach consists of detecting all M𝑀Mitalic_M intersections between N𝑁Nitalic_N line segments in a plane. This is considered one of the fundamental problems of Computational Geometry, and it is known that any algorithm within the algebraic decision tree model has a lower bound of Ω(NlogN+M)Ω𝑁𝑁𝑀\Omega(N\log N+M)roman_Ω ( italic_N roman_log italic_N + italic_M ) time to solve it  [3, 6].

In [1] Balaban proposes two algorithms for finding intersecting segments, a deterministic and asymptotically optimal for both time O(NlogN+M)𝑂𝑁𝑁𝑀O(N\log N+M)italic_O ( italic_N roman_log italic_N + italic_M ) and space O(N)𝑂𝑁O(N)italic_O ( italic_N ) algorithm and a simpler one that can perform the same task in O(Nlog2N+M)𝑂𝑁superscript2𝑁𝑀O\left(N\log^{2}N+M\right)italic_O ( italic_N roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_N + italic_M )-time. Before that, Chazelle and Edelsbrunner [5] reached a time optimal algorithm O(NlogN+M)𝑂𝑁𝑁𝑀O(N\log N+M)italic_O ( italic_N roman_log italic_N + italic_M ) with a space requirement of O(N+M)𝑂𝑁𝑀O(N+M)italic_O ( italic_N + italic_M ). The randomized approach devised by Clarkson and Shor [6] produced an algorithm for reporting all intersecting pairs that requires O(NlogN+M)𝑂𝑁𝑁𝑀O(N\log N+M)italic_O ( italic_N roman_log italic_N + italic_M ) time and O(N)𝑂𝑁O(N)italic_O ( italic_N ) space.

In 1979 Bentley and Ottmann proposed an algorithm that solved this problem in O((N+M)logN)𝑂𝑁𝑀𝑁O((N+M)\log N)italic_O ( ( italic_N + italic_M ) roman_log italic_N ) time and O(N+O(N+italic_O ( italic_N + M)M)italic_M ) space [15]. This algorithm is the well-known Bentley-Ottmann algorithm, and after more than 40 years, it is still widely adopted in practical implementations because it is easy to understand and implement [18, 13]. In realizing that this is not the most complex part of our approach, we use the Bentley-Ottmann algorithm since its complexity is acceptable for our purposes, and its published implementations are quite simple.

Refer to caption
Figure 1: Set ΦΦ\Phiroman_Φ of line segments

2.2 Removing line segment intersections

The next step of our approach is to remove all proper intersections between line segments, dividing each intersected segment in sub-segments without proper intersections, only sharing endpoints. To find and remove intersections, performing at once the first two steps of our approach, we use a robust and efficient implementation of the Bentley-Ottmann algorithm, described by Bartuschka, Mehlhorn and Naher [2] that computes the planar graph induced by a set of line segments. Their implementation, represented in this paper by Compute-Induced-Graph, computes the graph G𝐺Gitalic_G induced by set ΦΦ\Phiroman_Φ in O((N+M)logN)𝑂𝑁𝑀𝑁O((N+M)\log N)italic_O ( ( italic_N + italic_M ) roman_log italic_N ) time. Since this algorithm is quite long, we choose not to present it here. We refer our readers to [2] for a detailed description.

In this implementation, the vertices of G𝐺Gitalic_G represent all endpoints and proper intersection points of line segments in ΦΦ\Phiroman_Φ, and the edges of G𝐺Gitalic_G are the maximal relatively open sub-segments of lines in ΦΦ\Phiroman_Φ that do not contain any vertex of G𝐺Gitalic_G. The major drawback of this implementation lies in that parallel edges are produced in the graph for overlap** segments. We assume that ΦΦ\Phiroman_Φ contains no such segments. For example, the set ΦΦ\Phiroman_Φ shown in Figure 1, Compute-Induced-Graph, will produce the graph G𝐺Gitalic_G, depicted in Figure 2, where each edge represents a nonintersecting line segment.

3 Polygon Detection

Detecting polygons is similar to finding cycles on the graph G𝐺Gitalic_G produced in the previous step.

The first known linear-time algorithm for listing all graph cycles was presented by Syslo [20]. This algorithm requires O(V)𝑂𝑉O(V)italic_O ( italic_V ) space and O(V×C)𝑂𝑉𝐶O(V\times C)italic_O ( italic_V × italic_C ) time, where V𝑉Vitalic_V and C𝐶Citalic_C are the number of vertices and cycles in G𝐺Gitalic_G, respectively. Later, Dogrusöz and Krishnamoorthy proposed a vector space algorithm for enumerating all cycles of a planar graph that runs in O(V2×C)𝑂superscript𝑉2𝐶O\left(V^{2}\times C\right)italic_O ( italic_V start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT × italic_C ) time and O(V)𝑂𝑉O(V)italic_O ( italic_V ) space  [8]. Although asymptotically slower, this algorithm is much simpler than Syslo’s and is amenable to parallelization. Unfortunately, the number of cycles in a planar graph can grow exponentially with the number of vertices [17]. An example of this situation is the graph presented in Figure 3. In this case, the number of cycles, including the interior region numbered 1, is O(2r)𝑂superscript2𝑟O\left(2^{r}\right)italic_O ( 2 start_POSTSUPERSCRIPT italic_r end_POSTSUPERSCRIPT ) with r=k/2+1𝑟𝑘21r=k/2+1italic_r = italic_k / 2 + 1, where k𝑘kitalic_k is the number of vertices since one can choose any combination of the remaining regions to define a cycle [8]. This is why detecting all polygons that can be constructed from a set of lines is not very feasible. In this paper, we chose to detect the minimal polygons with few edges that cannot be constructed by joining other minimal polygons.

Refer to caption
Figure 2: Graph G𝐺Gitalic_G induced by ΦΦ\Phiroman_Φ

3.1 Minimum Cycle Basis of a Graph

Since we want to detect the minimal polygons, this can be treated as searching for a Minimum Cycle Basis (MCB). So, the second step of our approach consists in obtaining an MCB of graph G𝐺Gitalic_G. A cycle basis is defined as a basis for the cycle space of G𝐺Gitalic_G, which consists entirely of elementary cycles. A cycle is called elementary if it contains no vertex more than once. The dimension of the cycle space is given by the cyclomatic number ν=EV+P𝜈𝐸𝑉𝑃\nu=E-V+Pitalic_ν = italic_E - italic_V + italic_P [9, 4], where E𝐸Eitalic_E is the number of edges and V𝑉Vitalic_V the number of vertices in G𝐺Gitalic_G and P𝑃Pitalic_P is the number of connected components of G𝐺Gitalic_G.

Refer to caption
Figure 3: A planar graph with an exponential number of cycles

3.2 All Cycles of a Graph

Horton presented the first known polynomial-time algorithm to find the shortest cycle basis of a graph, which runs in O(E3V)𝑂superscript𝐸3𝑉O\left(E^{3}V\right)italic_O ( italic_E start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_V ) time [14] or in O(E4)𝑂superscript𝐸4O\left(E^{4}\right)italic_O ( italic_E start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) on simple planar graphs [12], which is the case. While asymptotically better solutions have been published in the literature, the Bentley-Ottmann algorithm is simple and usable for our needs. The pseudo-code of this algorithm is listed in Minimum-Cycle-Basis and shortly described below. A further detailed description of this algorithm and its concepts can be found in [14].

The All-Pairs-Shortest-Paths finds the shortest paths between all pairs of vertices in graph G𝐺Gitalic_G and can be performed in O(V3)𝑂superscript𝑉3O\left(V^{3}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT ) time and O(V2)𝑂superscript𝑉2O\left(V^{2}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) space using FloydWarshall or Dijkstra algorithms [7]. Order-By-length orders the cycles by ascending length and can be implemented by any efficient sorting algorithm. This is a non-critical step because it has a O(VνlogV)𝑂𝑉𝜈𝑉O(V\nu\log V)italic_O ( italic_V italic_ν roman_log italic_V ) upper bound in time complexity, which is insignificant compared to other steps of this algorithm.

[Uncaptioned image]

In Select-Cycles, we use a greedy algorithm to find the MCB from ΓΓ\Gammaroman_Γ set of cycles. To do this Horton [14] suggests representing the cycles as rows of a 01010-10 - 1 incidence matrix, in which columns correspond to the edges of the graph, and rows are the incidence vectors of each cycle. Gaussian elimination using elementary row operations over the integers modulo two can then be applied to the incidence matrix, processing each row in turn, in ascending order of the weights of cycles, until enough independent cycles are found.

This step dominates the time complexity from other steps since it takes O(Eν2V)𝑂𝐸superscript𝜈2𝑉O\left(E\nu^{2}V\right)italic_O ( italic_E italic_ν start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_V ) time. Knowing that G𝐺Gitalic_G is always a simple planar graph we can conclude that as a whole, the Minimum-Cycle-Basis algorithm has a worst-case upper bound of O(Eν2V)=O(E3V)=O(E4)𝑂𝐸superscript𝜈2𝑉𝑂superscript𝐸3𝑉𝑂superscript𝐸4O\left(E\nu^{2}V\right)=O\left(E^{3}V\right)=O\left(E^{4}\right)italic_O ( italic_E italic_ν start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_V ) = italic_O ( italic_E start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_V ) = italic_O ( italic_E start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) operations and space requirements of O(V2)𝑂superscript𝑉2O\left(V^{2}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ).

Refer to caption
Figure 4: Set ΘΘ\Thetaroman_Θ of polygons detected from ΦΦ\Phiroman_Φ

Figure 5 shows an example of ΓΓ\Gammaroman_Γ, the set of cycles resulting from applying the Minimum-Cycle-Basis to graph G𝐺Gitalic_G shown in Figure 2.

3.3 Polygon construction

The last step of our approach consists of constructing a set ΘΘ\Thetaroman_Θ of polygons from the MCB. An algorithm to perform this operation can easily run in O(CV)𝑂𝐶𝑉O(CV)italic_O ( italic_C italic_V ) time, where C𝐶Citalic_C is the number of cycles in MCB. Such an algorithm is listed in Polygons-From-Cycles, which returns a set ΘΘ\Thetaroman_Θ of polygons.

Refer to caption
Figure 5: Shortest cycle basis ΓΓ\Gammaroman_Γ of graph G𝐺Gitalic_G
[Uncaptioned image]

Figure 4 illustrates the resulting set ΘΘ\Thetaroman_Θ of polygons generated by applying Polygons-From-Cycles to ΓΓ\Gammaroman_Γ depicted in Figure 5.

4 Algorithm Outline

We can now outline Detect-Polygons. This algorithm can detect a set ΘΘ\Thetaroman_Θ of polygons from an initial set ΨΨ\Psiroman_Ψ of line segments. To perform this task, we pipeline the algorithms referred to in previous sections for line segment intersection removal, MCB finding, and cycle-to-polygon conversion.

[Uncaptioned image]

As referred in section 2.2, Compute-Induced-Graph runs in O((N+M)logN)𝑂𝑁𝑀𝑁O((N+M)\log N)italic_O ( ( italic_N + italic_M ) roman_log italic_N ) time and O(N+M)𝑂𝑁𝑀O(N+M)italic_O ( italic_N + italic_M ) space. The Shortest-Cycle-Basis runs in O(V4)𝑂superscript𝑉4O\left(V^{4}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) operations and has a space requirement of O(V2)𝑂superscript𝑉2O\left(V^{2}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ), making this the critical step in the complexity of this algorithm, since the Polygons-From-Cycles needs O(CV)𝑂𝐶𝑉O(CV)italic_O ( italic_C italic_V ) time.

Since the number V𝑉Vitalic_V of vertices in the graph is no greater than the sum of line endpoints (2×N)2𝑁(2\times N)( 2 × italic_N ) with detected intersections M𝑀Mitalic_M, we can then conclude that the proposed algorithm has time and space complexities of O(V4)=𝑂superscript𝑉4absentO\left(V^{4}\right)=italic_O ( italic_V start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) = O((N+M)4)𝑂superscript𝑁𝑀4O\left((N+M)^{4}\right)italic_O ( ( italic_N + italic_M ) start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT ) and O(V2)=O((N+M)2)𝑂superscript𝑉2𝑂superscript𝑁𝑀2O\left(V^{2}\right)=O\left((N+M)^{2}\right)italic_O ( italic_V start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) = italic_O ( ( italic_N + italic_M ) start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ), respectively.

5 Experimental Results

The algorithm proposed in this paper was implemented in C++\mathrm{C}++roman_C + + and tested in an Intel Pentium III 1GHz 512MB RAM computer running Windows XP. We tested the algorithm with line segments created from simple test drawings, technical drawings of mechanical parts, and hand-sketched drawings. Table 1 presents the results obtained from these tests.

These results show that performance is acceptable for online processing in sets with less than three hundred lines, like hand sketches or small-size technical drawings. If the line set has about 2,500 lines, the algorithm will take more than twenty minutes to detect the polygons. Still, this remains a feasible solution for batch processing of medium-size technical drawings.

6 Conclusions and Future Work

The proposed algorithm uses polygon detection in vector drawings to create descriptions based on spatial and topological relationships between polygons. Another use is detecting planar shapes in sketches. Both applications have been implemented as working prototypes for shape retrieval and architectural drawing from sketches.

The algorithm presented here detects all minimal polygons that can be constructed from a set of line segments in polynomial time and space. This approach uses well-known and simple-to-implement algorithms to perform line segment intersection detection and to find an MCB of a graph instead of using more efficient but less simple methods.

Indeed, the presented algorithm has considerable room for improvement, namely through more recent, complex, and efficient algorithms. Further work may be carried out regarding detecting and correcting rounding errors resulting from finite precision computations.

Acknowledgments

We thank Professor Mukkai S. Krishnamoorthy from Rensselaer Polytechnic Institute, New York, for his very helpful suggestions.

This work was partly funded by the Portuguese Foundation for Science and Technology, project 34672/99, and the European Commission, project SmartSketches IST-200028169.

Lines Intersections Nodes Edges Time (ms)
6 9 21 24 10
36 16 58 68 50
167 9 169 177 3986
286 47 389 376 8623
518 85 697 679 36703
872 94 1066 10050 128995
2507 10 2407 2526 1333547
Table 1: Results of algorithm tests

References

  • [1] Ivan J. Balaban. An optimal algorithm for finding segment intersections. In Proceedings of the 11th Annual ACM Symposium Computational Geometry, pages 211–219. ACM, 1995.
  • [2] Ulrike Bartuschka, Kurt Mehlhorn, and Stefan Naher. A robust and efficient implementation of a sweep line algorithm for the straight line segment intersection. In Proceedings of Workshop on Algorithm Engineering, pages 124–135, Venice, Italy, September 1997.
  • [3] M. Ben-Or. Lower bounds for algebraic computation trees. In Proceedings of the 15th Annual ACM Symposium Theory of Computing, pages 80–86. ACM, 1983.
  • [4] Augustin-Louis Cauchy. Recherche sur les polyèdres. J. Ecole Polytechnique, 9(16):68–86, 1813.
  • [5] Bernard Chazelle and Herbert Edelsbrunner. An optimal algorithm for intersecting line segments in the plane. Journal of the ACM, 39:1–54, 1992.
  • [6] Ken Clarkson and Peter W. Shor. Applications of random sampling in computational geometry, ii. Discrete and Computational Geometry, 4:387–421, 1989.
  • [7] Thomas Cormen, Charles Leiserson, and Ronald Rivest. Introduction to Algorithms. MIT Press, McGraw-Hill, 2nd edition, 1990.
  • [8] U. Dogrusöz and M. Krishnamoorthy. Cycle vector space algorithms for enumerating all cycles of a planar graph. Technical Report 5, Rensselaer Polytechnic Institute, Dept. of Computer Science, Troy, New York 12180 USA, January 1995.
  • [9] Leonhard Euler. Elementa doctrinae solidorum. Novi Commentarii Academiae Scientiarum Petropolitanae, 4:109–140, 1752.
  • [10] Alfredo Ferreira, Simone Marini, Marco Attene, Manuel J. Fonseca, Michela Spagnuolo, Joaquim A. Jorge, and Bianca Falcidieno. Thesaurus-based 3d object retrieval with part-in-whole matching. International Journal of Computer Vision, 89(2–3):327–347, June 2009.
  • [11] Manuel J. Fonseca, Alfredo Ferreira, and Joaquim A. Jorge. Content-based retrieval of technical drawings. International Journal of Computer Applications in Technology, 23(2/3/4):86, 2005.
  • [12] David Hartvigsen and Russel Mardon. The all-pairs minimum cut problem and the minimum cycle basis problem on planar graphs. SIAM Journal on Computing, 7(3):403–418, August 1994.
  • [13] John Hobby. Practical segment intersection with finite precision output. Computational Geometry: Theory and Applications, 13(4), 1999.
  • [14] J.D.Horton. A polynomial-time algorithm to find the shortest cycle basis of a graph. SIAM Journal on Computing, 16(2):358–366, April 1987.
  • [15] J.L.Bentley and T.Ottmann. Algorithms for reporting and counting geometric intersections. IEEE Transactions on Computers, pages 643–647, 1979.
  • [16] Alfredo Ferreira Jr., Manuel Fonseca, and Joaquim Jorge. Polygon Detection from a Set of Lines. In Adérito Marcos, Ana Mendonça, Miguel Leitão, António Costa, and Joaquim Jorge, editors, 12º Encontro Português de Computação Gráfica. The Eurographics Association, 2003.
  • [17] Prabhaker Mateti and Narsingh Deo. On algorithms for enumerating all circuits of a graph. SIAM Journal on Computing, 5(1):90–99, March 1976.
  • [18] Joseph O’Rourke. Computational Geometry in C, chapter Section 7.7 "Intersection of Segments", pages 264–266. Cambridge University Press, 2nd edition, 1998.
  • [19] Nelson F Silva. Cascading recognizers for ambiguous calligraphic interaction. In EUROGRAPHICS Workshop on Sketch-Based Interfaces and Modeling. The Eurographics Association, 2004.
  • [20] Maciej M. Syslo. An efficient cycle vector space algorithm for listing all cycles of a planar graph. SIAM Journal on Computing, 10(4):797–808, November 1981.