n维立方体中的连接点

时间:2018-08-16 12:37:46

标签: java vector 3d processing 4d

我正在尝试在不使用p3d渲染器的情况下创建类似3d的草图。我设法制作了一个立方体,但为此我对所有坐标和连接进行了硬编码,一旦您想添加另一个维度,它就会变得有些无聊。所以我创建了一个创建所有坐标的函数:

float[][] cube(int dims, float w) {
  int outputSize = (int)pow(2, dims);
  float[] temp = new float[dims];
  float[][] res = new float[outputSize][dims];
  Arrays.fill(temp, w);
  res[0] = temp.clone();
  for (int i = 0; i < outputSize - 1; i++) {
    for (int j = dims - 1; true; j--) {
      temp[j] *= -1;
      if (temp[j] < 0) {
        break; 
      }
    }
    res[i + 1] = temp.clone();
  }
  return res;
}

它仅通过使用二进制进行工作,因此输入(2,1)多维数据集为:

[[1, 1], [1, -1], [-1, 1], [-1, -1]]

它可以正常工作,但问题是它只返回角但不返回女巫角进行连接,但是我找不到有效的方法来实现。我需要另一个函数来返回要连接的索引。

以下是给定上述数组的函数应执行的示例:

[[0, 1], [1, 3], [3, 2], [2, 0]]

(内部数组的顺序可能不同)

是否存在任何已知的算法来连接n维立方体的角?

如果其他点生成有帮助,我可以更改其他功能。

1 个答案:

答案 0 :(得分:1)

这是一种迭代生成坐标和索引的方法:

  • 从维度为n的多维数据集开始
  • 制作两个立方体的副本,并在第n + 1轴上的每个极坐标(例如-1和+1)上放置一个副本
  • 使边缘连接到立方体上的每对对应顶点

enter image description here

您已经知道顶点数V(n) = 2^n。由于添加到n + 1多维数据集的边数等于此值(所有对应的顶点对),加上复制的n立方体的边数,因此边数的重复关系为:

E(n) = 2 * E(n - 1) + V(n - 1)   // copies + joining edges
E(1) = 1                         // base case for n = 1

-->  E(n) = n * 2^(n - 1)

n  | E(n)
-------------
1  | 1
2  | 4
3  | 12
4  | 32
5  | 80

这样可以在复制新的多维数据集/添加新的边缘时预先分配所需边缘的数量并计算索引偏移。


代码:

// edge index
class Edge
{
   public int A, B;
   public Edge(int a, int b)
   {
      A = a; B = b;
   }
   public Edge shift(int n)
   {
      return new Edge(A + n, B + n);
   }
}

// cube class
class Cube
{
   // I'll leave out the get-functions etc here
   private float[][] m_verts;
   private Edge[] m_edges;
   public Cube(float[][] v, Edge[] e)
   {
      m_verts = v;
      m_edges = e;
   }
}

Cube cube_N(int dims, float w)
{
   // base case
   if (dims < 1)
      return null;

   // calculate buffer sizes
   int dpow2 = 1 << dims;
   int numVerts = dpow2;
   int numEdges = dims * (dpow2 / 2);

   // buffers
   float[] temp = new float[dims];
   float[][] verts = new float[numVerts][];
   Edge[] edges = new Edge[numEdges];

   // base case creation
   Arrays.fill(temp, w);
   verts[0] = temp.clone();
   edges[0] = new Edge(0, 1);

   // iterative step
   for (int i = 0; i < dims; i++)
   {
      int nV = 1 << i;
      int nE = i * (nV / 2);

      // copy + shift vertices
      for (int j = 0; j < nV; j++)
      {
         float[] v = verts[j].clone();
         v[i] = -w;
         verts[nV + j] = v;
      }

      // copy + shift previous edges
      for (int j = 0; j < nE; j++)
      {
         edges[nE + j] = edges[j].shift(nV);
      }

      // create new edges to join cube copies
      int off = nE * 2;
      for (int j = 0; j < nV; j++)
      {
         edges[off + j] = new Edge(j, nV + j);
      }
   }

   return new Cube(verts, edges);
}

n = 3的结果:

verts:
[1, 1,  1], [-1, 1,  1], [1, -1,  1], [-1, -1,  1],
[1, 1, -1], [-1, 1, -1], [1, -1, -1], [-1, -1, -1]
edges:
[0, 1], [2, 3], [0, 2], [1, 3], [4, 5], [6, 7],
[4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]

n = 4的结果:

verts: 
[1, 1,  1,  1], [-1, 1,  1,  1], [1, -1,  1,  1], [-1, -1,  1,  1],
[1, 1, -1,  1], [-1, 1, -1,  1], [1, -1, -1,  1], [-1, -1, -1,  1],
[1, 1,  1, -1], [-1, 1,  1, -1], [1, -1,  1, -1], [-1, -1,  1, -1],
[1, 1, -1, -1], [-1, 1, -1, -1], [1, -1, -1, -1], [-1, -1, -1, -1]

edges:
[0 ,  1], [2 ,  3], [0 ,  2], [1 ,  3], [4,  5], [6 ,  7], [4 ,  6], [5 ,  7],
[0 ,  4], [1 ,  5], [2 ,  6], [3 ,  7], [8,  9], [10, 11], [8 , 10], [9 , 11],
[12, 13], [14, 15], [12, 14], [13, 15], [8, 12], [9 , 13], [10, 14], [11, 15],
[0 ,  8], [1 ,  9], [2 , 10], [3 , 11], [4, 12], [5 , 13], [6 , 14], [7 , 15]