繁体   English   中英

3D线碰到3D点?

[英]3D line hits a 3D point?

我想创建一个函数,该函数知道一条线是否达到目标。 有这样的功能吗? 我也想以厘米为单位设置3D点的大小,但不知道该怎么做。

我感谢您的帮助。

例如: 在此处输入图片说明

假设这些点具有半径,并且这些线不会精确地到达中间的点,那么该函数是否可以显示一条直线是否达到了该点?

我认为您实际要计算的是3d中点与线之间的距离

请参阅: 点线距离

当距离小于您的点周围的球体的半径时,您将具有一个匹配项。

好的,我有适用于所有维度的经典解决方案。

首先,您需要球体和直线,并且需要对它们有一个好的模型。 只要拥有Vector .center.diameter Sphere就很容易。

class Sphere:
    def __init__( sphere, center, diameter ):
       sphere.center=Vector(center)
       sphere.diameter=float(diameter)

对于初学者来说,线可能会有更多问题,因为线可以用很多方式定义。 最有用的是参数方程式,您在Vector .direction有一个方向,在.center有一些凝视点。 我们假设.direction是单位长度, .center是距(0,0)的直线上最近的点。 在大多数情况下,我们需要创建一条线,必须指向矢量:

def line_on_two_points( A, B ):
    return Line( direction= Vector(B)-A, center=A )

因此,我们必须在构造函数中固定directioncenter .direction易于修复,仅需使其长度即可。 要找到.center ,我们需要标量投影 这是D的向量: 从(0,0)开始的线上最近点

.direction为单位长度A到B, center为C到A,我们可以将行初始化为:

class Line:
   def __init__( line, direction, center ):
        line.direction= Vector(direction) / length(direction)
        line.center= center - line.direction*dot(center,line.direction)

如果我们没有一条线,那么我们可以做两点:

#class Sphere:
def colide_line_on_two_points( sphere, A, B ):
    line=line_on_two_points( A-sphere.center, B-sphere.center)
    return length(line.center) < sphere.diameter

但是,当我们有一行时,我们尝试将其优化为:

#class Sphere:
def colide_line( sphere, line ):
    return line.distance_to(sphere.center) < sphere.diameter

.distance_to()函数有点棘手:

从线到点的向量

#class Line:

   def vector_to( line, P ):
       return line.center + line.direction * dot(line.direction,P) - P

   def distance_to( line, P ):
       return length( line.center + line.direction * dot(line.direction,P) - P )

   def move_to( line, P ):
       line.center += line.direction * dot(line.direction,P) - P

最后但并非最不重要的是Vector类型,我尝试使用numpy,但是对于2D,3D来说它相当慢:

from numpy import array as Vector
from numpy import dot
from numpy.linalg import norm as length

您正在寻找的是一种算法,用于查找直线和球体之间的交点。 这是图形编程中常见的问题,有很多文章可能比我更好地解释了它。 http://www.lighthouse3d.com/tutorials/maths/ray-sphere-intersection/中有一个

基本思想是将球体投影到直线上,然后使用勾股定理求解由相交点,球体中心和投影点形成的直角三角形。

这是我在路径跟踪渲染器中使用的代码:

hitdata intersectwith(Sphere sphere)
{
    d3Vector projected;

    float t = V.dot(sphere.pos.subtract(O));
    projected = V.normalize().scalarmultiply(t); //the projected vector
    float distnce = (projected.subtract(sphere.pos.subtract(O))).magnitude();
    //the length between the center of your sphere and the projected point
    hitdata outdata; // a class containing the results of the intersection
    outdata.hit = false;
    outdata.t = 110;
    if(t<=0)
    {
        return outdata;
    }
    if(distnce<sphere.r)
    {// the line is less distant from the center of the sphere than the surface
        outdata.hit = true;
        float deltaT = sqrtf((sphere.r*sphere.r)-(distnce*distnce));//Pythagorean theorem
        outdata.coord = O.add(V.scalarmultiply(t-deltaT));
        //calculating intersection coordinates
        outdata.normal = outdata.coord.subtract(sphere.pos);
        outdata.normal = outdata.normal.normalize();//calculating surface normals
        outdata.material = sphere.material;
        outdata.t = t-deltaT;
    }
    return outdata;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM