【问题标题】:Flattened 3D array interior values展平的 3D 数组内部值
【发布时间】:2014-12-06 20:38:10
【问题描述】:

我有一个扁平的 3D 数组,表示我正在尝试优化的程序网格的顶点索引。我像这样创建数组:

int* vertIndices = new int[WIDTH * HEIGHT * DEPTH];

并添加到数组中

vertIndices[x + WIDTH * (y + HEIGHT * z)] = vertIndex;

问题是我只需要跟踪网格表面上的顶点。不会创建内部顶点。

因此,我创建了许多从未被使用过的浪费整数。

这是一个遍历网格的 vertIndices 数组的循环,其中 WIDTH: 7, HEIGHT: 7 和 DEPTH: 7

所有这些 -1163005939 值都是位于网格内部但不会被创建的顶点。

我的问题是如何改进公式

x + WIDTH * (y + HEIGHT * z)

忽略内部值。

谢谢!

【问题讨论】:

    标签: c++ arrays


    【解决方案1】:

    我认为您不会在公式中引入某种条件。像这样的:

    int getIndex(int x, int y, int z) {
        //First get the amount of points in all layers before this z layer.
        int beforeZ = (z) ? WIDTH*HEIGHT + (z - 1)*2*(WIDTH + HEIGHT - 2) : 0;
    
        //Then get the amount of points within this layer before this line.
        int beforeY = (y) ? WIDTH + 2*(y - 1) : 0;
        if(z == 0 || z == DEPTH - 1) beforeY = y*WIDTH;
    
        //And finally the amount of points within this line before this point.
        int beforeX = (x) ? 1 : 0;
        if(z == 0 || z == DEPTH - 1 || y == 0 || y == HEIGHT - 1) beforeX = x;
    
        //Return the amount of points before this one.
        return beforeZ + beforeY + beforeX;
    }
    

    我承认这有点难看,但我认为它已经接近你能得到的最好的了。至少如果您不想创建某种将坐标与索引匹配的查找表,反之亦然。当然,这样的查找表将是可以处理任何情况的真正大枪,但缺点是内存使用量很大并且可能运行速度较慢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2020-12-29
      • 2017-10-08
      • 1970-01-01
      • 2017-05-13
      相关资源
      最近更新 更多