Polygon Detection from a Set of Lines
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 and respectively, where is the number of line segments and 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.
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 intersections between 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 time to solve it [3, 6].
In [1] Balaban proposes two algorithms for finding intersecting segments, a deterministic and asymptotically optimal for both time and space algorithm and a simpler one that can perform the same task in -time. Before that, Chazelle and Edelsbrunner [5] reached a time optimal algorithm with a space requirement of . The randomized approach devised by Clarkson and Shor [6] produced an algorithm for reporting all intersecting pairs that requires time and space.
In 1979 Bentley and Ottmann proposed an algorithm that solved this problem in time and 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.
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 induced by set in 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 represent all endpoints and proper intersection points of line segments in , and the edges of are the maximal relatively open sub-segments of lines in that do not contain any vertex of . The major drawback of this implementation lies in that parallel edges are produced in the graph for overlap** segments. We assume that contains no such segments. For example, the set shown in Figure 1, Compute-Induced-Graph, will produce the graph , 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 produced in the previous step.
The first known linear-time algorithm for listing all graph cycles was presented by Syslo [20]. This algorithm requires space and time, where and are the number of vertices and cycles in , respectively. Later, Dogrusöz and Krishnamoorthy proposed a vector space algorithm for enumerating all cycles of a planar graph that runs in time and 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 with , where 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.
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 . A cycle basis is defined as a basis for the cycle space of , 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 [9, 4], where is the number of edges and the number of vertices in and is the number of connected components of .
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 time [14] or in 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 and can be performed in time and 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 upper bound in time complexity, which is insignificant compared to other steps of this algorithm.
In Select-Cycles, we use a greedy algorithm to find the MCB from set of cycles. To do this Horton [14] suggests representing the cycles as rows of a 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 time. Knowing that 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 operations and space requirements of .
Figure 5 shows an example of , the set of cycles resulting from applying the Minimum-Cycle-Basis to graph shown in Figure 2.
3.3 Polygon construction
The last step of our approach consists of constructing a set of polygons from the MCB. An algorithm to perform this operation can easily run in time, where is the number of cycles in MCB. Such an algorithm is listed in Polygons-From-Cycles, which returns a set of polygons.
4 Algorithm Outline
We can now outline Detect-Polygons. This algorithm can detect a set of polygons from an initial set 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.
As referred in section 2.2, Compute-Induced-Graph runs in time and space. The Shortest-Cycle-Basis runs in operations and has a space requirement of , making this the critical step in the complexity of this algorithm, since the Polygons-From-Cycles needs time.
Since the number of vertices in the graph is no greater than the sum of line endpoints with detected intersections , we can then conclude that the proposed algorithm has time and space complexities of and , respectively.
5 Experimental Results
The algorithm proposed in this paper was implemented in 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 |
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.