【问题标题】:Check If ray has Intersection with virtual Box检查光线是否与虚拟框相交
【发布时间】:2020-06-24 14:09:55
【问题描述】:

我有一个包含 2 个 3DPoint 的虚拟框,其中一个是最小值 (x,y,z),第二个是最大值 (x,y,z)

我有一条带有中心点和方向矢量的射线

如何检查向量是否与此虚拟框相交

(dotProduct、crossProduct、distance等方法我都有)但是我不知道我需要如何开始查找是否有交点,

在附加图像中,我尝试显示 2 种状态,一种是光线有相交,另一种没有。 如何通过代码找到它

现在如果有交叉点,我只需要查找布尔值,实际上我不需要找到这些点。

public class BoundaryVolume {

    public Point3D min;
    public Point3D max;
....

public boolean boundingIntersection(Ray ray) {

     //Point3D p0 = ray.get_POO();
     //   Vector v = ray.get_direction();

     //   Vector v1 = min.subtract(p0);
     //   Vector v2 = max.subtract(p0);
     //   double s1 = v.dotProduct(v1.crossProduct(v2).normalized());
     //   if (isZero(s1)) return false;
    
    
}
}

雷:

public class Ray {

 private    Point3D _POO;
 private    Vector _direction;
....
}

我想检查是否有点

【问题讨论】:

  • 盒子是否总是 AABB(轴对齐)?检查一下:Cone to box collision 您可以使用line closest(axis a0,convex_mesh m0) 并测试返回的行是否长度为零。您可以对其进行很多优化,因为它适用于通用凸网格,而不仅仅是 AABB ...

标签: java geometry raytracing


【解决方案1】:

让射线有起点rx, ry, rz和方向向量dx, dy, dz,轴对齐框的两个角是A和B(B分量大于A分量)。

在参数形式中,射线可以表示为

x = rx + t * dx
y = ry + t * dy
z = rz + t * dz

其中 t 是0..Infinity 范围内的参数

获取光线与平面 A.x、B.x、A.y 等相交的 t 个参数。

t_ax = (A.x - rx) / dx
t_bx = (B.x - rx) / dx
t_ay = (A.y - ry) / dy
...

参数取正值,每次计算交点是否在对应的矩形内

y_ax = ry + t_ax * dy
z_ax = rz + t_ax * dz
if (A.y<=y_ax<=B.y) and ((A.z<=z_ax<=B.z)) 
    ray intersects a face
if not - continue with the next face

【讨论】:

    【解决方案2】:

    这是我最后的工作代码:

    public boolean boundingIntersection(Ray ray) {
                
            Point3D po = ray.get_POO();    
            double dirfra_x =  1.0f / ray.get_direction().get_head().get_x().get();
            double dirfra_y =  1.0f / ray.get_direction().get_head().get_y().get();
            double dirfra_z =  1.0f / ray.get_direction().get_head().get_z().get();
    
    
            // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
            // r.org is origin of ray
            double t1 = (min.get_x().get() - po.get_x().get()) * dirfra_x;
            double t2 = (max.get_x().get() - po.get_x().get()) * dirfra_x;
            double t3 = (min.get_y().get() - po.get_y().get()) * dirfra_y;
            double t4 = (max.get_y().get() - po.get_y().get()) * dirfra_y;
            double t5 = (min.get_z().get() - po.get_z().get()) * dirfra_z;
            double t6 = (max.get_z().get() - po.get_z().get()) * dirfra_z;
    
    
            double tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)), Math.min(t5, t6));
            double tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)), Math.max(t5, t6));
    
            // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
            if (tmax < 0)
            {
                return false;
            }
            // if tmin > tmax, ray doesn't intersect AABB
            if (tmin > tmax)
            {
                return false;
            }
            return true;
            
        }
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多