确定点是否多面体内体内、多面

2023-09-08 01:09:27 作者:来自于你的强大

我试图确定特定点位于一个多面体内部。在我目前的执行情况,我的工作方法需要我们要找的多面体的面孔组成的数组点(三角形在这种情况下,但后来也可能是其他的多边形)。我一直在努力工作的信息在这里找到: http://softsurfer.com/Archive/ algorithm_0111 / algorithm_0111.htm

I'm attempting to determine if a specific point lies inside a polyhedron. In my current implementation, the method I'm working on take the point we're looking for an array of the faces of the polyhedron (triangles in this case, but it could be other polygons later). I've been trying to work from the info found here: http://softsurfer.com/Archive/algorithm_0111/algorithm_0111.htm

下面,你会看到我的内部的方法。我知道NRML /很正常的事情是种奇怪的..它是旧code的结果。当我运行这个它似乎总是返回true,不管输入什么我给它。 (这是解决了,请参阅我的回答如下 - 这code为现在的工作)。

Below, you'll see my "inside" method. I know that the nrml/normal thing is kind of weird .. it's the result of old code. When I was running this it seemed to always return true no matter what input I give it. (This is solved, please see my answer below -- this code is working now).

bool Container::inside(Point* point, float* polyhedron[3], int faces) {
  Vector* dS = Vector::fromPoints(point->X, point->Y, point->Z,
                 100, 100, 100);
  int T_e = 0;
  int T_l = 1;

  for (int i = 0; i < faces; i++) {
    float* polygon = polyhedron[i];

    float* nrml = normal(&polygon[0], &polygon[1], &polygon[2]);
    Vector* normal = new Vector(nrml[0], nrml[1], nrml[2]);
    delete nrml;

    float N = -((point->X-polygon[0][0])*normal->X + 
                (point->Y-polygon[0][1])*normal->Y +
                (point->Z-polygon[0][2])*normal->Z);
    float D = dS->dot(*normal);

    if (D == 0) {
      if (N < 0) {
        return false;
      }

      continue;
    }

    float t = N/D;

    if (D < 0) {
      T_e = (t > T_e) ? t : T_e;
      if (T_e > T_l) {
        return false;
      }
    } else {
      T_l = (t < T_l) ? t : T_l;
      if (T_l < T_e) {
        return false;
      }
    }
  }

  return true;
}

这是在C ++中,但中提到的意见,这真的很语言无关。

This is in C++ but as mentioned in the comments, it's really very language agnostic.

推荐答案

事实证明,这个问题是我在阅读上面的链接引用的算法。我读:

It turns out that the problem was my reading of the algorithm referenced in the link above. I was reading:

N = - dot product of (P0-Vi) and ni;

N = - dot product of S and ni;

已经改变了这一点,code以上,现在似乎正常工作。 (我也更新了code中的问题,以反映正确的解决方案)。

Having changed this, the code above now seems to work correctly. (I'm also updating the code in the question to reflect the correct solution).