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

Kyl*_*ker 1 c++ algorithm math geometry

我在 3D 空间中有一个平面方程:ax + by + cz + d = 0,我想用规则分布的点在平面上特定点的给定半径内填充该平面。在我看来,应该有一个数学上优雅的答案,但我没有看到它。用 C++ 或伪代码回答会更好。

ltj*_*jax 5

我假设您有一个相当好的 3d 矢量类,并在答案中将其称为 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;
}
Run Code Online (Sandbox Code Playgroud)

这种构造保证 dot(n, getPerpendillary(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);
Run Code Online (Sandbox Code Playgroud)

现在,您可以通过缩放 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
Run Code Online (Sandbox Code Playgroud)

epsilon 确保您的点数是对称的,并且您不会错过极端的最后一个点。