Abstract
The nearest neighbors (NN) algorithm is a traditional algorithm utilized in many research areas like computer graphics, classification and/or machine learning. This paper aims at the fixed-radius nearest neighbors algorithm in 2D space. The neighbors are searched within a circular neighborhood which is positioned in the bounded space. The radius of the circle is known in advance. The uniform grids can be efficiently utilized for a nearest points query acceleration. This paper presents a study comparing the square and the hexagonal uniform grids and their suitability for a circular neighborhood querying. The two algorithms checking the mutual position/intersection of a circle and a square or a hexagon are described. The tests show the supremacy of the hexagonal grid.
1 Introduction
The fixed-radius nearest neighbors (FRNN) algorithm searches all the points in a circular neighborhood with the defined radius. Usually, the neighbors of all vertices of a dataset are searched. The brute force algorithm thus computes \( {\mathcal{O}}\left( {\left| V \right|^{2} } \right) \) distances, where \( \left| V \right| \) is the total number of vertices. The radius is known in advance, so that it is possible to build a query structure that decides which spatial areas are worth to browse. Beside the traditional hierarchical clustering methods (Octree, kd-tree, BVH, etc.) [1–3], the straightforward methods utilizing the uniform regular grids can be used. The surrounding cells potentially hit by the radius contain the significant vertices. Franklin [4] pointed out that the hierarchical methods need a lot of memory to store the hierarchical information, their preprocessing time is mostly from \( {\mathcal{O}}\left( {\left| V \right|\log \left| V \right|} \right) \) to \( {\mathcal{O}}\left( {\left| V \right|^{2} } \right) \) and their query time complexity is about \( {\mathcal{O}}\left( {\log \left| V \right|} \right) \). Thus, he introduced a nearest neighbor method called the Nearpt3 [4], which is based on the idea of the uniform grid and its query complexity is considered to be \( {\mathcal{O}}\left( 1 \right) \). The Nearpt3 uses a linear representation of the vertices instead of the spatial trees. The number of vertices located in the surrounding cells is limited to some \( k \). which usually represents just a small fraction of \( \left| V \right| \). The computation time is thus much lower in the datasets with a reasonable vertex distribution. Several single-thread and parallel nearest neighbors algorithms based on the uniform grids have been published for large graphs visualization [5], particle simulation [6, 7] and/or ray tracing [8]. The fundamental uniform grid cells are the squares, because they are very simple to use. However, the uniform hexagonal grids can be also utilized in a similar way. The paper [9] presents a method using a linear representation of the hexagonal cells for a search of the NN in 2D sparse datasets. Only the surrounding hexagonal rings are checked for neighbors. The hexagons can efficiently tile the 2D space and their shape is closer to a circle, so that they can even overcome the orthogonal grids in some cases. The papers [10–12] showed the advantages of the hexagonal grids for the circular neighborhood search on a theoretical level.
This paper aims at the FRNN search using the uniform grids. It provides a comparison of the orthogonal and hexagonal grids. The method introduced in this paper is based on the simple algorithm from the paper [9] which is improved here with novel algorithms testing the circle-square or circle-hexagon mutual position, which reduce the necessary number of distance computations.
2 Our Approach
This section introduces the modified FRNN algorithm. It extends the principle from [9] by the intersection algorithms. The two uniform grids are tested: square and hexagonal. First, the algorithms testing the intersection of a cell and a circle are described in Sect. 2.1. Next, the algorithm searching the FRNN is briefly introduced in Sect. 2.2.
2.1 Circle Intersection
This section presents two intuitive algorithms testing the mutual position of a circle and a cell (square, hexagon), which return the corresponding values. If a cell is completely outside the circle the cell will be skipped (Value 0). If a cell is just hit by the circle all the contained vertices will be sequentially checked (Value 1). If a cell is completely inside the circle all the contained vertices will be classified as neighbors (Value 2). The principal metric of the grid is the side size \( S \) of a square or a regular hexagon respectively. The advantage is that the both cell shapes can be precisely inscribed into a circle.
Circle-square Intersection.
In the square grids, the intersection of the circle of neighborhood and the square cells have to be decided. The robust algorithm solving this problem was published in [13]. This section describes an algorithm specially simplified for the 2D space and its easy cases. The algorithm is illustrated in Fig. 1 and described in Function CircleSqrInt. First, the case when the distance of just one coordinate is too long is checked. Second, the cases when the whole circumscribed circle (purple) is out of the radius or completely inside the radius are tested. Next, the easy cases when the square is partially hit by radius from side (green area) and the cases when the radius hits the corners (red areas) are checked. Otherwise, no intersection is detected.
Circle-hexagon Intersection.
This section describes an algorithm checking the intersection of a circle and a hexagon. This is utilized for a neighborhood search in the 2D hexagonal grids. The pointy-topped hexagonal grid is considered. The algorithm is illustrated in Fig. 2 and described in Function CircleHexaInt. First, the intersection with the gray bounding box is checked. Next, if the whole circumscribed circle (purple) is inside the radius, the full intersection is detected. If it is not even hit by the radius, no intersection is detected. If the green inscribed circle is hit by the radius, a partial intersection is detected. Otherwise, the area between the both circles have to be checked according to distance \( d \) computed from the angle between the line connecting the \( C \) and \( P \) and the grid orientation \( u = \left( {0,1} \right) \). The modified algorithm could be used for a general orientation of the grid. Equations would be changed according to a different bounding box and orientation \( u \).
2.2 Fixed-Radius Nearest Neighbors Search
The FRNN algorithm using the presented intersection algorithms is briefly described here. A point cloud is usually a set of unordered points. This set has to be divided to clusters, which represent a generalized location of contained vertices. The clusters are the grid cells here. The FRNN algorithm is based on the searching procedure introduced in [9] which is accelerated by the intersection algorithms. The linear vertex representation based on a list of existing cells (clusters) is utilized to address the surrounding cells on the grid hit by the radius \( r \) (details in [9]).
Initialization.
The \( \left| V \right| \) vertices have to be localized in the grid. For each vertex coordinates of the cell containing the vertex have to be computed. The vertex localization is trivial in the orthogonal grid. The localization in the hexagonal grid is explained e.g. in [9]. The cell coordinates are recalculated to a linear hash according to the C-curve order. The vertices are sorted then according to these hashes. The point clouds are usually sparse, thus this kind of clustering secures that only the existing cells are considered. A list \( Cs \) of cells is built by grouping together of the vertices of the same cell.
Fixed-radius Nearest Neighbors Query.
A query vertex \( p \) is localized in the grid. The algorithm spirals out to the surrounding rings of cells containing the potential neighbors (see the indexations in Fig. 3). The precomputed list \( Cs \) of clusters is used to address the cells. The list of neighbors \( Rs \) is returned. The searching algorithm goes as follows:
-
1.
A query vertex \( \varvec{p} \) is localized in the grid. The coordinates and the linear hash of the corresponding cell are computed.
-
2.
The surrounding cells are browsed in the manner described in Fig. 3.
-
3.
Each cell is checked for circle-cell intersection. The algorithm distinguishes the three cases depending on the value returned by an intersection function: 0 – no intersection, 1 – partial intersection, 2 – full intersection (see Sect. 2.1)
-
4.
In the cases of Value 1 and 2, the cell is binary searched in the \( Ls \) according to the linear hash and the contained vertices are proceeded and popped to the \( Rs \).
-
5.
The list of all neighbors \( Rs \) within the fixed radius \( r \) is returned.
The presented FRNN algorithm browses the \( c \) surrounding cells and checks maximally \( n \) vertices in each cell. The number of cells in the list \( Cs \) is \( \left| {Cs} \right| \le \left| V \right| \). In the worst case, the query time is \( {\mathcal{O}}\left( {c \cdot \log_{2} \left| {Cs} \right|} \right) + {\mathcal{O}}\left( {c \cdot n} \right) \) if all the surrounding cells are hit by the radius. However, the real time complexity will be much lesser. See the Sect. 3.
3 Experiments
This section shows the comparison of the square and hexagonal grids. Both grids were tested on the artificial datasets with \( \left| V \right| = 20000 \) shown in Fig. 4. The charts in Fig. 5 show the comparison of the grids and their suitability for the circular neighborhood search. The Euclidean distances calculated to find the FRNN of all the \( \left| V \right| \) vertices are compared. The neighborhood is divided to the specific number of levels (cell rings) according to the grid segmentation. The FRNN methods with and without application of the presented intersection algorithms are compared. The dependency on the grid segmentation is shown as well. The approaches utilizing the intersection algorithms reflect the position of a vertex in the cell and they overcome the previous basic algorithm [9].
4 Conclusion
The Fixed-Radius Nearest Neighbors (FRNN) algorithms comparison on the square and hexagonal grids was presented. The two simple algorithms to check the circle-square and circle-hexagon intersection were described. The experiments showed that the intersection algorithms significantly reduced the number of computed distances in comparison with the basic approach presented in [9]. The hexagonal grid is obviously better designed for the circular neighborhoods search than the square one. The gentler segmentation of the grids also reduced the number of distances on the tested datasets.
References
Samet, H.: The quadtree and related hierarchical data structures. ACM Comput. Surv. 16(2), 187–260 (1984)
Bédorf, J., Gaburov, E., Zwart, S.P.: A sparse octree gravitational N-body code that runs entirely on the GPU processor. J. Comput. Phys. 231(7), 2825–2839 (2012)
Yin, M., Li, S.: Fast BVH construction and refit for ray tracing of dynamic scenes. Multimedia Tools Appl. 72(2), 1823–1839 (2014)
Franklin, W.R.: Nearest Point Query on 184,088,599 Points in E3 with a Uniform Grid (2006). http://wrfranklin.org/Research/nearpt3/
Uher, V., Gajdoš, P., Ježowicz, T.: Solving nearest neighbors problem on GPU to speed up the Fruchterman-Reingold graph layout algorithm. In: 2015 IEEE 2nd International Conference on Cybernetics (CYBCONF), pp. 305–310. IEEE Press (2015)
Green, S.: Particle simulation using cuda. NVIDIA Whitepaper 6, 121–128 (2010)
Hoetzlein, R.: Fast fixed-radius nearest neighbors: interactive million-particle fluids. In: GPU Technology Conference (GTC) 2014 (2014)
Kalojanov, J., Slusallek, P.: A parallel algorithm for construction of uniform grids. In: 2009 Proceedings of the Conference on High Performance Graphics, HPG 2009, pp. 23–28. ACM, New York (2009)
Uher, V., Gajdoš, P., Ježowicz, T., Snášel, V.: Application of hexagonal coordinate systems for searching the K-NN in 2D space. In: Snášel, V., Abraham, A., Krömer, P., Pant, M., Muda, A.K. (eds.) Innovations in Bio-Inspired Computing and Applications. AISC, vol. 424, pp. 209–220. Springer, Heidelberg (2016)
Lester, L.N., Sandor, J.: Computer graphics on a hexagonal grid. Comput. Graph. 8(4), 401–409 (1984)
Haverkort, H.J.: Recursive tilings and space-filling curves with little fragmentation. CoRR abs/1002.1843 (2010)
Ben, J., Tong, X., Chen, R.: A spatial indexing method for the hexagon discrete global grid system. In: 2010 18th International Conference on Geoinformatics, pp. 1–5, June 2010
Ratschek, H., Rokne, J.: Test for intersection between circle and rectangle. Appl. Math. Lett. 6(4), 21–23 (1993)
Acknowledgment
This work was supported by The Ministry of Education, Youth and Sports from the National Programme of Sustainability (NPU II) project “IT4Innovations excellence in science - LQ1602”. This work was supported by SGS project, VSB-Technical University of Ostrava, under the grants no. SP2016/97 and no. SP2016/68.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2017 Springer International Publishing AG
About this paper
Cite this paper
Uher, V., Gajdoš, P., Snášel, V. (2017). Searching of Circular Neighborhoods in the Square and Hexagonal Regular Grids. In: Pan, JS., Snášel, V., Sung, TW., Wang, X. (eds) Intelligent Data Analysis and Applications. ECC 2016. Advances in Intelligent Systems and Computing, vol 535. Springer, Cham. https://doi.org/10.1007/978-3-319-48499-0_15
Download citation
DOI: https://doi.org/10.1007/978-3-319-48499-0_15
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-48498-3
Online ISBN: 978-3-319-48499-0
eBook Packages: EngineeringEngineering (R0)