确定一个点是否在多面体内部

新手上路,请多包涵

我试图确定一个特定点是否位于多面体内。在我目前的实现中,我正在研究的方法是我们正在寻找多面体的面数组(在这种情况下是三角形,但以后可能是其他多边形)。我一直在尝试从这里找到的信息中工作:http: //softsurfer.com/Archive/algorithm_0111/algorithm_0111.htm

下面,您将看到我的“内部”方法。我知道 nrml/normal 有点奇怪.. 这是旧代码的结果。当我运行它时,无论我给它什么输入,它似乎总是返回 true。 (这已经解决了,请看我下面的答案——这段代码现在可以工作了)。

 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++ 中,但正如评论中所提到的,它真的与语言无关。

原文由 gregghz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

事实证明,问题在于我阅读了上面链接中引用的算法。我在读:

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

作为

N = - dot product of S and ni;

更改了这一点后,上面的代码现在似乎可以正常工作了。 (我也在更新问题中的代码以反映正确的解决方案)。

原文由 gregghz 发布,翻译遵循 CC BY-SA 3.0 许可协议

您问题中的链接已过期,我无法从您的代码中理解算法。假设您有一个带有 逆时针方向 的面(从外面看)的 多面体,那么检查您的点是否在所有面的后面就足够了。为此,您可以从点到每个面的向量,并检查标量积的符号与面的法线。如果为正,则该点在脸的后面;如果为零,则该点在脸上;如果为负数,则该点在面部前面。

这是一些完整的 C++11 代码,适用于 3 点面或普通的多点面(仅考虑前 3 点)。您可以轻松更改 bound 以排除边界。

 #include <vector>
#include <cassert>
#include <iostream>
#include <cmath>

struct Vector {
  double x, y, z;

  Vector operator-(Vector p) const {
    return Vector{x - p.x, y - p.y, z - p.z};
  }

  Vector cross(Vector p) const {
    return Vector{
      y * p.z - p.y * z,
      z * p.x - p.z * x,
      x * p.y - p.x * y
    };
  }

  double dot(Vector p) const {
    return x * p.x + y * p.y + z * p.z;
  }

  double norm() const {
    return std::sqrt(x*x + y*y + z*z);
  }
};

using Point = Vector;

struct Face {
  std::vector<Point> v;

  Vector normal() const {
    assert(v.size() > 2);
    Vector dir1 = v[1] - v[0];
    Vector dir2 = v[2] - v[0];
    Vector n  = dir1.cross(dir2);
    double d = n.norm();
    return Vector{n.x / d, n.y / d, n.z / d};
  }
};

bool isInConvexPoly(Point const& p, std::vector<Face> const& fs) {
  for (Face const& f : fs) {
    Vector p2f = f.v[0] - p;         // f.v[0] is an arbitrary point on f
    double d = p2f.dot(f.normal());
    d /= p2f.norm();                 // for numeric stability

    constexpr double bound = -1e-15; // use 1e15 to exclude boundaries
    if (d < bound)
      return false;
  }

  return true;
}

int main(int argc, char* argv[]) {
  assert(argc == 3+1);
  char* end;
  Point p;
  p.x = std::strtod(argv[1], &end);
  p.y = std::strtod(argv[2], &end);
  p.z = std::strtod(argv[3], &end);

  std::vector<Face> cube{ // faces with 4 points, last point is ignored
    Face{{Point{0,0,0}, Point{1,0,0}, Point{1,0,1}, Point{0,0,1}}}, // front
    Face{{Point{0,1,0}, Point{0,1,1}, Point{1,1,1}, Point{1,1,0}}}, // back
    Face{{Point{0,0,0}, Point{0,0,1}, Point{0,1,1}, Point{0,1,0}}}, // left
    Face{{Point{1,0,0}, Point{1,1,0}, Point{1,1,1}, Point{1,0,1}}}, // right
    Face{{Point{0,0,1}, Point{1,0,1}, Point{1,1,1}, Point{0,1,1}}}, // top
    Face{{Point{0,0,0}, Point{0,1,0}, Point{1,1,0}, Point{1,0,0}}}, // bottom
  };

  std::cout << (isInConvexPoly(p, cube) ? "inside" : "outside") << std::endl;

  return 0;
}

用你喜欢的编译器编译它

clang++ -Wall -std=c++11 code.cpp -o inpoly

并像这样测试它

$ ./inpoly 0.5 0.5 0.5
inside
$ ./inpoly 1 1 1
inside
$ ./inpoly 2 2 2
outside

原文由 John 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题