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.) [13], 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 [1012] 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.

Fig. 1.
figure 1

Computation of the circle-square intersection. The green square represents the area where the radius can hit the square cell. The purple circle is the area which is tested for the full intersection with the radius. The red corners represent the areas where the point to corner vertex distance has to be checked. (Color figure online)

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 \).

Fig. 2.
figure 2

Computation of the circle-hexagon intersection. The gray rectangle represents the area where the radius can hit the hexagonal cell. The purple circle represents the area which is tested for the full intersection with the radius. If the radius hits the inscribed green circle, a partial intersection is detected. The red distance \( d \) is computed to handle the area between the two circles (Color figure online).

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:

Fig. 3.
figure 3

Indexation of the cells: (A) Square, (B) Hexagonal

  1. 1.

    A query vertex \( \varvec{p} \) is localized in the grid. The coordinates and the linear hash of the corresponding cell are computed.

  2. 2.

    The surrounding cells are browsed in the manner described in Fig. 3.

  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. 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. 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].

Fig. 4.
figure 4

Random vertex distributions: (A) Uniform, (B) Gaussian, (C) Gaussian islands

Fig. 5.
figure 5

Medians of distances calculated to find the FRNN of \( \left| V \right| \) vertices on the square and hexagonal grids. The neighbors were searched with/without application of the circle-cell intersection function and different grid segmentation. The white numbers represent the standard deviation.

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.