簡體   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