Example #1
0
        /// <summary>
        /// Returns whether or not the provided H3Indexes are neighbors.
        /// </summary>
        /// <param name="origin">The origin H3 index</param>
        /// <param name="destination">The destination H3 index</param>
        /// <returns>1 if the indexes are neighbors, 0 otherwise</returns>
        /// <!-- Based off 3.1.1 -->
        public static int h3IndexesAreNeighbors(H3Index origin, H3Index destination)
        {
            // Make sure they're hexagon indexes
            if (H3Index.H3_GET_MODE(ref origin) != Constants.H3_HEXAGON_MODE ||
                H3Index.H3_GET_MODE(ref destination) != Constants.H3_HEXAGON_MODE)
            {
                return(0);
            }

            // Hexagons cannot be neighbors with themselves
            if (origin == destination)
            {
                return(0);
            }

            // Only hexagons in the same resolution can be neighbors
            if (H3Index.H3_GET_RESOLUTION(origin) != H3Index.H3_GET_RESOLUTION(destination))
            {
                return(0);
            }

            // H3 Indexes that share the same parent are very likely to be neighbors
            // Child 0 is neighbor with all of its parent's 'offspring', the other
            // children are neighbors with 3 of the 7 children. So a simple comparison
            // of origin and destination parents and then a lookup table of the children
            // is a super-cheap way to possibly determine they are neighbors.
            int parentRes = H3Index.H3_GET_RESOLUTION(origin) - 1;

            if (parentRes > 0 && (H3Index.h3ToParent(origin, parentRes) ==
                                  H3Index.h3ToParent(destination, parentRes)))
            {
                Direction originResDigit      = H3Index.H3_GET_INDEX_DIGIT(origin, parentRes + 1);
                Direction destinationResDigit =
                    H3Index.H3_GET_INDEX_DIGIT(destination, parentRes + 1);
                if (originResDigit == Direction.CENTER_DIGIT ||
                    destinationResDigit == Direction.CENTER_DIGIT)
                {
                    return(1);
                }

                // These sets are the relevant neighbors in the clockwise
                // and counter-clockwise
                Direction[] neighborSetClockwise =
                {
                    Direction.CENTER_DIGIT, Direction.JK_AXES_DIGIT, Direction.IJ_AXES_DIGIT,
                    Direction.J_AXES_DIGIT, Direction.IK_AXES_DIGIT, Direction.K_AXES_DIGIT,
                    Direction.I_AXES_DIGIT
                };
                Direction[] neighborSetCounterclockwise =
                {
                    Direction.CENTER_DIGIT, Direction.IK_AXES_DIGIT, Direction.JK_AXES_DIGIT,
                    Direction.K_AXES_DIGIT, Direction.IJ_AXES_DIGIT, Direction.I_AXES_DIGIT,
                    Direction.J_AXES_DIGIT
                };
                if (
                    neighborSetClockwise[(int)originResDigit] == destinationResDigit ||
                    neighborSetCounterclockwise[(int)originResDigit] == destinationResDigit)
                {
                    return(1);
                }
            }

            // Otherwise, we have to determine the neighbor relationship the "hard" way.
            var neighborRing = new ulong[7].Select(cell => new H3Index(cell)).ToList();

            Algos.kRing(origin, 1, ref neighborRing);
            for (int i = 0; i < 7; i++)
            {
                if (neighborRing[i] == destination)
                {
                    return(1);
                }
            }

            // Made it here, they definitely aren't neighbors
            return(0);
        }