从平面方程中生成点网格的算法



我在三维空间中有一个平面方程:ax+bycz++d=0,我想用正则分布点在平面上特定点的给定半径内填充这个平面。在我看来,应该是一个数学上优雅的答案,但我看不出来。用C++或伪代码回答会更好。

我假设您有一个相当好的三维向量类,并在答案中称其为vec3。你首先需要的是平面上的一个向量。给定法向平面方程,有几种方法可以生成一个,但我更喜欢这个:

vec3 getPerpendicular(vec3 n)
{
  // find smallest component
  int min=0;
  for (int i=1; i<3; ++i)
    if (abs(n[min])>abs(n[i]))
      min=i;
  // get the other two indices
  int a=(min+1)%3;
  int b=(min+2)%3;
  vec3 result;
  result[min]=0.f;
  result[a]=n[b];
  result[b]=-n[a];
  return result;
}

这种构造保证了dot(n,getVertical(n))为零,这是正交性条件,同时也保持向量的大小尽可能高。请注意,将幅值最小的分量设置为0也可以确保不会得到0,0,0向量,除非这已经是您的输入。在这种情况下,你的平面会退化。

现在在飞机上获取基本矢量:

vec3 n(a,b,c); // a,b,c from your equation
vec3 u=normalize(getPerpendicular(n));
vec3 v=cross(u, n);

现在,可以通过缩放u和v并将其添加到平面上的向量中来生成点。

float delta = radius/N; // N is how many points you want max in one direction
float epsilon=delta*0.5f;
for (float y=-radius; y<radius+epsilon; radius+=delta)
   for (float x=-radius; x<radius+epsilon; radius+=delta)
      if (x*x+y*y < radius*radius) // only in the circle
          addPoint(P+x*u+y*v); // P is the point on the plane

ε确保你的点数是对称的,你不会错过极端的最后一点。

最新更新