java - 将高度图分成象限

标签 java arrays quadtree heightmap

我正在使用四叉树系统渲染地形。我需要使用方法 splitHeightmap(float[] OriginalMap, int Quadrant) 将高度图分成四个部分,象限是 0-3 之间的数字。 map 需要分成四分之一,因此如果传递 0 作为象限,则数组的左下四分之一将作为新的 float 组返回。我有一些基本代码,但我不确定如何根据所需的象限对 map 进行实际采样:

protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant) {
    float[] newHeightMap = new float[size >> 1];
    int newSize = size >> 1;

    for (int i = 0; i < newSize; i++)
        for (int j = 0; j < newSize; j++)
            newHeightMap[i * newSize + j] = sampleHeightAt(heightMap, i, j);


    return newHeightMap;
}

protected float sampleHeightAt(float[] heightMap, int x, int z) {
    return heightMap[z + x * size];
}

编辑:我已经写了我认为应该有效的内容,但我得到了索引 65792 的 ArrayIndexOutOfBoundsException (原始高度图为 512x512):

    protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant) {
    float[] newHeightMap = new float[(size >> 1) * (size >> 1)];
    int newSize = size >> 1;

    int xOffset = 0, zOffset = 0;
    int xCount = 0, yCount = 0;

    switch (quadrant) {
        case BottomRight:
            xOffset = newSize;
            break;
        case TopLeft:
            zOffset = newSize; 
            break;
        case TopRight:
            xOffset = newSize;
            zOffset = newSize;
            break;
        default:
            break;
    }

    for (int x = xOffset; x < xOffset + newSize; x++)
        for (int z = zOffset; z < zOffset + newSize; z++) {
            newHeightMap[xCount + yCount * newSize] = heightMap[z + x * size];  // should this be 'z + x * size' or 'x + z * size'?

            xCount++;
            yCount++;
        }

    return newHeightMap;
}

最佳答案

如果我理解正确的话,那么你必须使用二维数组。使用这种类型的数组,可以轻松获取数组的任何区域。

您的 heightMap 现在将是 float[][] 类型,因此您必须更正您填充此数组的代码。

我为您实现了一个示例并使用了 4x4 数组:

| 2 2 3 3 |
| 2 2 3 3 | 
| 0 0 1 1 | 
| 0 0 1 1 |

据我了解,您想要获取所有“0”、所有“1”、所有“2”和所有“3”等区域。

    public static void main ( String[] args )
    {
        //setting up initial array 'heightMap' (you can name it like you want)
        float[][] f = { { 2, 2, 3, 3 }, { 2, 2, 3, 3 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 } };

        float[][] f2 = splitHeightMap ( f, TerrainQuadrant.BotttomRight );
        for ( float[] floats : f2 )
        {
            System.out.println ( Arrays.toString ( floats ) );
        }
    }

    protected static float[][] splitHeightMap ( float[][] heightMap, TerrainQuadrant quadrant )
    {
        //this gives you half of the 'heightMap' length
        int newSize = heightMap.length >> 1;
        float[][] newHeightMap = new float[ newSize ][ newSize ];

        //its your offsets, indicating from what place to start iteration
        int xOffset = 0;
        int yOffset = newSize;

        //its max values to reach while iterating
        int xRestriction = newSize;
        int yRestriction = heightMap.length;

        //setting up params according to 'quadrant'
        switch ( quadrant )
        {
            case BottomRight:
                xOffset = newSize;
                yOffset = newSize;

                xRestriction = heightMap.length;
                break;
            case TopLeft:
                yOffset = 0;

                yRestriction = newSize;
                break;
            case TopRight:
                yOffset = 0;
                xOffset = newSize;

                xRestriction = heightMap.length;
                yRestriction = newSize;
                break;
            default:
                break;
        }

        //counters not to reach new array bounds
        int xCount = 0, yCount = 0;
        for ( int y = yOffset; y < yRestriction; y++ )
        {
            //taking row at 'y' position
            float[] row = heightMap[ y ];
            for ( int x = xOffset; x < xRestriction; x++ )
            {
                //taking value from 'y' row at 'x' position.
                float value = row[ x ];

                //set fetched value to new map.
                newHeightMap[ yCount ][ xCount ] = value;

                //increase x position, but do not touch row
                xCount++;
            }

            //new row - new 'x' position
            xCount = 0;
            yCount++;
        }
        return newHeightMap;
    }

此实现向您展示:

| 1 1 |
| 1 1 |

要更改它 - 更改 main 方法。

关于java - 将高度图分成象限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325127/

相关文章:

java - 如何在 RCP 工作台工具栏和 RCP View 之间进行通信

java - 在 Java 8 中是否有一种简洁的方法来迭代带有索引的流?

php - sql连接等于数组

c++ - 在C++中的赋值运算符重载方法中删除旧的动态分配内存

algorithm - 对从小到大尺寸的物体进行空间分区/碰撞检测的最佳算法?

java - 在 Swing 中使用 setBounds() 的缺点

java - 两个整数的中间值

ios - 使用 AFNetworking 2 将 Json 数组发送到服务器时出错

python - 使用 scipy 动态更新 coo 矩阵

检查四叉树水平对称性的算法?