/// <summary> /// Provides all of the unidirectional edges from the current H3Index. /// </summary> /// <param name="origin">The origin hexagon H3Index to find edges for.</param> /// <param name="edges">The memory to store all of the edges inside.</param> /// <!-- Based off 3.1.1 --> public static void getH3UnidirectionalEdgesFromHexagon(H3Index origin, List <H3Index> edges) { // Determine if the origin is a pentagon and special treatment needed. int isPentagon = H3Index.h3IsPentagon(origin); // This is actually quite simple. Just modify the bits of the origin // slightly for each direction, except the 'k' direction in pentagons, // which is zeroed. for (int i = 0; i < 6; i++) { if (isPentagon != 0 && i == 0) { edges[i] = H3Index.H3_INVALID_INDEX; } else { edges[i] = origin; var ei = edges[i]; H3Index.H3_SET_MODE(ref ei, Constants.H3_UNIEDGE_MODE); H3Index.H3_SET_RESERVED_BITS(ref ei, (ulong)i + 1); edges[i] = ei; } } }
/// <summary> /// Returns a unidirectional edge H3 index based on the provided origin and /// destination /// </summary> /// <param name="origin">The origin H3 hexagon index</param> /// <param name="destination">The destination H3 hexagon index</param> /// <returns>The unidirectional edge H3Index, or 0 on failure.</returns> /// <!-- Based off 3.1.1 --> public static H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination) { // Short-circuit and return an invalid index value if they are not neighbors if (h3IndexesAreNeighbors(origin, destination) == 0) { return(H3Index.H3_INVALID_INDEX); } // Otherwise, determine the IJK direction from the origin to the destination H3Index output = origin; H3Index.H3_SET_MODE(ref output, Constants.H3_UNIEDGE_MODE); // Checks each neighbor, in order, to determine which direction the // destination neighbor is located. Skips CENTER_DIGIT since that // would be this index. for (var direction = Direction.K_AXES_DIGIT; direction < Direction.NUM_DIGITS; direction++) { int rotations = 0; H3Index neighbor = Algos.h3NeighborRotations(origin, direction, ref rotations); if (neighbor == destination) { H3Index.H3_SET_RESERVED_BITS(ref output, (ulong)direction); return(output); } } // This should be impossible, return an invalid H3Index in this case; return(H3Index.H3_INVALID_INDEX); // LCOV_EXCL_LINE }
/// <summary> /// Returns the origin hexagon from the unidirectional edge H3Index /// </summary> /// <param name="edge">The edge H3 index</param> /// <returns>The origin H3 hexagon index</returns> /// <!-- Based off 3.1.1 --> public static H3Index getOriginH3IndexFromUnidirectionalEdge(H3Index edge) { if (H3Index.H3_GET_MODE(ref edge) != Constants.H3_UNIEDGE_MODE) { return(H3Index.H3_INVALID_INDEX); } H3Index origin = edge.value; H3Index.H3_SET_MODE(ref origin, Constants.H3_HEXAGON_MODE); H3Index.H3_SET_RESERVED_BITS(ref origin, 0); return(origin); }