【发布时间】:2012-02-11 06:25:55
【问题描述】:
我正在尝试确定一个特定点是否位于多面体内。在我当前的实现中,我正在研究的方法是我们正在寻找多面体的面数组(在这种情况下是三角形,但以后可能是其他多边形)。我一直在尝试使用此处找到的信息: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++ 编写的,但正如 cmets 中所提到的,它确实与语言无关。
【问题讨论】:
-
您应该更新此问题,使其与语言无关。您要问的内容并非特定于 openGL 或 C++。一旦你有了一个通用的理论,你就可以让它适应你想要的每种语言和 3D API
-
创建一个简单的案例,您可以在其中验证它不在对象内,然后开始调试它。快速浏览后代码看起来差不多......
-
这看起来不是一个非常健壮的方法。首先,它只适用于凸多面体。其次,对于各种边界情况(选择的射线位于其中一个面的平面等),它都会失败。
-
我不担心凹多面体,所以没关系。但是,我愿意接受有关如何捕获更多边界情况的建议。
-
@duedl0r,感谢您提供的本应是显而易见的方法。听取这个简单的建议是我找到解决方案的原因。
标签: c++ 3d computational-geometry polyhedra