需要一个算法3D矢量路口 [英] Need an algorithm for 3D vectors intersection

查看:144
本文介绍了需要一个算法3D矢量路口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个载体,分别由2 三维点(来源和方向)定义。我需要找出它们的交叉点。 的一点点帮助总是受欢迎的。 我会后我的功能,这给了我错误的输出。

I have 2 vectors, each defined by 2 Point3D (origin and direction). I need to find out the point of their intersection. A little bit of help is always welcome. I will post my function, which gives me wrong output.

public static CurvIntersect3D Intersect2Linii3D (Vector3D dr1, Vector3D dr2) {
    CurvIntersect3D result = new CurvIntersect3D(0, null);

    double x = Math3D.VectorNorm3D(dr1.getDirectie());
    double t = Math3D.VectorNorm3D(dr2.getDirectie());
    double cosa = (dr1.getDirectie().getX()*dr2.getDirectie().getX() + dr1.getDirectie().getY()*dr2.getDirectie().getY() + dr1.getDirectie().getZ()*dr2.getDirectie().getZ()) / (t*x);
    Punct3D p1 = dr1.getOrigine();
    Punct3D p2 = new Punct3D(), p3 = new Punct3D();
    for (int i=0; i<3; i++)
    {
        p2.set(i, dr1.getOrigine().get(i) + dr1.getDirectie().get(i));
        p3.set(i, dr1.getOrigine().get(i) + dr2.getDirectie().get(i));
    }

    Matrici.Matrice3x3 rot = Math3D.GetMatriceRotatie(p1, p2, p3);
    Punct3D orig = new Punct3D();
    for (int i=0; i<3; i++)
        orig.set(i, rot.getElement(i, 0) * (dr2.getOrigine().getX()-dr1.getOrigine().getX()) +
                    rot.getElement(i, 1) * (dr2.getOrigine().getY()-dr1.getOrigine().getY()) +
                    rot.getElement(i, 2) * (dr2.getOrigine().getZ()-dr1.getOrigine().getZ()));

    x = orig.getY() - orig.getZ()* cosa / Math.sqrt(1 - cosa*cosa);
    p1 = new Punct3D();
    for (int i=0; i<3; i++)
        p1.set(i, dr1.getOrigine().get(i) + x*dr1.getDirectie().get(i));
    result.setCount(1);
    result.add(p1);
    return result;
}

CurvIntersec3D是存储点和它的长度的阵列的结构。

CurvIntersec3D is a structure that stores the array of points and its length.

推荐答案

如前所述两条线可能无法在单个点相遇。您可以在一般能做的就是找​​到最接近2号线线路1点,反之亦然。连接这两点来创建的公法的方向。

As mentioned before the two lines may not meet at a single point. The best you can do in general is find the point on line1 closest to line2 and vise versa. Connect those two points to create the common normal direction.

鉴于两行通过3D点 R1 = [R1X,r1y,r1z] R2 = [R2X,R2Y,r2z] 和具有单位方向 E1 = [E1X,e1y,e1z] E2 = [E2X,e2y,e2z] ,你可以找到上线最贴近的其他线路像这样几点:

Given two lines passing through 3D points r1=[r1x,r1y,r1z] and r2=[r2x,r2y,r2z] and having unit directions e1=[e1x,e1y,e1z] and e2=[e2x,e2y,e2z] you can find the points on the line which are closest to the other line like this:

  1. 找到方向投影 U =点(E1,E2)= E1X * E2X + e1y * e2y + e1z * e2z
  2. 如果 U的== 1 然后线平行。没有交集存在。
  3. 找到分离预测 T1 =点(R2-R1,E1) T2 =点(R2-R1,E2)
  4. 找到沿一号线 D1 =(T1-U * T2)/(1-U * U)距离
  5. 找到沿2号线 D 2 =(T2-U * T1)距离/(U * U-1)
  6. 找到上线1点 P1 =添加(R1,量表(D1,E1))
  7. 找到在2号线的点 P2 =添加(R2,量表(D2,E2))
  1. Find the direction projection u=Dot(e1,e2)=e1x*e2x+e1y*e2y+e1z*e2z
  2. If u==1 then lines are parallel. No intersection exists.
  3. Find the separation projections t1=Dot(r2-r1,e1) and t2=Dot(r2-r1,e2)
  4. Find distance along line1 d1 = (t1-u*t2)/(1-u*u)
  5. Find distance along line2 d2 = (t2-u*t1)/(u*u-1)
  6. Find the point on line1 p1=Add(r1,Scale(d1,e1))
  7. Find the point on line2 p2=Add(r2,Scale(d2,e2))

请注意:你必须有方向为单位向量,点(E1,E1)= 1 点(E2,E2)= 1 。 函数点()是向量的点积。函数添加()增加向量的组件和功能量表()乘以与向量的分量一个数字。

Note: You must have the directions as unit vectors, Dot(e1,e1)=1 and Dot(e2,e2)=1. The function Dot() is the vector dot product. The function Add() adds the components of vectors, and the function Scale() multiplies the components of the vector with a number.

祝你好运。

这篇关于需要一个算法3D矢量路口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆