简体   繁体   中英

Finding all adjacent items in 3D list

I have a 3D list (3 nested lists), and for each item in the list, I need to find the adjacent items. Currently, I'm creating an array for each adjacent item, and storing that in each item. This is pretty resource intensive, so I'm looking to simplify it a bit.

I was considering using this approach:

for (int i = 0; i < 8; i++)
{
    switch (i)
    {
        case (0):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y - 1];
        case (1):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y + 1];
        case (2):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y];
        case (3):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y];
        case (4):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y + 1];
        case (5):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y - 1];
        case (6):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y + 1];
        case (7):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y - 1];
    }

    // do some stuff with the neighbor here
}

This is a lot better than what I have now in terms of performance, but I'm wondering if there is an easier way to accomplish this? It seems a bit messy, and I really feel like there would be a way to do this in 1 line, but I just cant figure out the math.

edit : Sorry, I may have omitted some important details. The code which works with the "neighbor" variable has been left out, as its very long and doesnt help with the solution. I do not need to maintain a list of the "neighbor" variables (Thats what I am currently doing, and it uses far too much memory (around 400megs). I just need a quick way to look at each item, find each of the adjacent 8 items, do something with them one at a time, and then move onto the next node. The above code will work to go through them, but it doesnt feel like the most optimized way to accomplish this.

It looks like you are actually finding neighbours in the 2D array at a given depth in your 3D data structure. Ie you are ignoring neigbours at adjacent depths.

Your approach is probably pretty performant. You could find some fancier way that involves less typing, eg this answer to a similar question on SO, but I doubt it will be faster.

Obviously you also need to include some checks in your code to handle items at the edge which won't have 8 neighbours.

private void DoSomethingWithNeighbours(int x, int y, int z)
{
    foreach (var neighbout in this.GetNeighbours(x, y, z)
    {
        // ...
    }
}

private IEnumerable<Item> GetNeighbours(int x, int y, int z)
{
    if (x > 0)
    {
        if (y > 0)
        {
            yield return myMap.Depth[z].Rows[x - 1].Columns[y - 1];
        }

        yield return myMap.Depth[z].Rows[x - 1].Columns[y];

        if (y < ColumnCount - 1)
        {
            yield return myMap.Depth[z].Rows[x - 1].Columns[y + 1];
        }
    }

    if (y > 0)
    {
        yield return myMap.Depth[z].Rows[x].Columns[y - 1];
    }

    if (y < ColumnCount - 1)
    {
        yield return myMap.Depth[z].Rows[x].Columns[y + 1];
    }

    if (x < RowCount - 1)
    {
        if (y > 0)
        {
            yield return myMap.Depth[z].Rows[x + 1].Columns[y - 1];
        }

        yield return myMap.Depth[z].Rows[x + 1].Columns[y];

        if (y < ColumnCount - 1)
        {
            yield return myMap.Depth[z].Rows[x + 1].Columns[y + 1];
        }
    }
}

Or the following alternative which is rather more concise, with only a marginal performance cost:

private IEnumerable<int> GetNeighbours2(int x, int y, int z)
{
    for (int i = x - 1; i <= x + 1; ++i)
    {
        for (int j = y - 1; j <= y + 1; ++j)
        {
            if (i >= 0 && i < Rows && j >= 0 && j < Cols && !(i == x && j == y))
            {
                yield return myMap.Depth[z].Rows[i].Columns[j];
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM