检查射线是否与虚拟框相交

Check If ray has Intersection with virtual Box

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

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

如何检查向量是否与这个虚拟框有交集

(点积、交叉积、距离等方法我都有)但是不知道有交点s怎么开始找,

在附加图像中,我尝试显示 2 种状态,一种是光线有交点,另一种没有。 如何通过code

找到

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

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;
....
}

我想查看是否有积分

设射线有起点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

中的参数

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

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

这是我的最终工作代码:

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;
        
    }