Java 检查光线是否与虚拟长方体相交
我有一个虚拟框,其中包含3个点中的2个,一个是最小点(x,y,z),另一个是最大点(x,y,z) 我有一条带中心点和方向向量的射线 如何检查向量是否与此虚拟框相交 (我有所有的点积、叉积、距离等方法)但我不知道如何开始查找是否有交点 在附加图像中,我尝试显示两种状态,一种是光线有交点,另一种是没有交点。 我如何通过代码找到它 现在我只需要找到布尔值,如果有交点,我不需要找到这个点Java 检查光线是否与虚拟长方体相交,java,geometry,raytracing,Java,Geometry,Raytracing,我有一个虚拟框,其中包含3个点中的2个,一个是最小点(x,y,z),另一个是最大点(x,y,z) 我有一条带中心点和方向向量的射线 如何检查向量是否与此虚拟框相交 (我有所有的点积、叉积、距离等方法)但我不知道如何开始查找是否有交点 在附加图像中,我尝试显示两种状态,一种是光线有交点,另一种是没有交点。 我如何通过代码找到它 现在我只需要找到布尔值,如果有交点,我不需要找到这个点 public class BoundaryVolume { public Point3D min;
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;
}
}
雷:
我想看看是否有点
让光线有起点
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
y_ax=ry+t_ax*dy
z_ax=rz+t_ax*dz
如果(A.y这是我的最终工作代码:
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;
}
public boolean boundingIntersection(光线){
Point3D po=ray.get_POO();
双方向x=1.0f/射线。获取方向()。获取头部()。获取x()。获取();
double dirfra_y=1.0f/ray.get_direction().get_head().get_y().get();
双dirfra_z=1.0f/ray.get_direction().get_head().get_z().get();
//lb是AABB的角点,具有最小坐标-左下角,rt是最大角点
//org是射线的起源
双t1=(min.get_x().get()-po.get_x().get())*dirfra_x;
double t2=(max.get_x().get()-po.get_x().get())*dirfra_x;
双t3=(min.get_y().get()-po.get_y().get())*dirfra_y;
双t4=(最大值get_y().get()-po.get_y().get())*dirfra_y;
double t5=(min.get_z().get()-po.get_z().get())*dirfra_z;
双t6=(max.get_z().get()-po.get_z().get())*dirfra_z;
双tmin=Math.max(Math.max(Math.min(t1,t2),Math.min(t3,t4)),Math.min(t5,t6));
双tmax=Math.min(Math.min(Math.max(t1,t2),Math.max(t3,t4)),Math.max(t5,t6));
//如果tmax<0,光线(直线)与AABB相交,但整个AABB在我们后面
如果(tmax<0)
{
返回false;
}
//如果tmin>tmax,则光线不与AABB相交
如果(tmin>tmax)
{
返回false;
}
返回true;
}
框是否始终为AABB(轴对齐)?请检查:您可以使用最近的线(轴a0,凸面网格m0)
并测试返回线是否为零长度。您可以对其进行大量优化,因为它适用于通用凸面网格,而不仅仅是AABB。。。
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;
}