算法用于确定在三维空间中一条线和一个立方体是否相交。

huangapple go评论55阅读模式
英文:

The algorithm to find if a line and a cube intersect in 3D space

问题

A line is defined by two (2) points A(Xa, Ya, Za), B(Xb, Yb, Zb)
一条线由两个(2)点A(Xa,Ya,Za)和B(Xb,Yb,Zb)定义。

A cube is defined by one (1) point C(Xc, Yc, Zc)
一个立方体由一个(1)点C(Xc,Yc,Zc)定义。

The cube is axis aligned, therefore its points are defined at (Xc, Yc, Zc) -> (Xc + 1, Yc + 1, Zc + 1)
该立方体是轴对齐的,因此其点被定义在(Xc,Yc,Zc)->(Xc + 1,Yc + 1,Zc + 1)。

I've tried using the 2D algorithm for the same problem and scaling it up another dimension, I was not able to get it to work.
我尝试使用相同问题的二维算法并将其扩展到另一个维度,但未能使其工作。

英文:

A line is defined by two (2) points A(Xa, Ya, Za), B(Xb, Yb, Zb)
A cube is defined by one (1) point C(Xc, Yc, Zc)

The cube is axis aligned, therefore it's points are defined at (Xc, Yc, Zc) -> (Xc + 1, Yc + 1, Zc + 1)

I've tried using the 2D algorithm for the same problem and scaling it up another dimension, i was not able to get it to work

答案1

得分: 1

以下是翻译好的代码部分:

一个可能的方法是将立方体视为被6个平面切割的一部分。然后只需通过这些平面切割线段,看是否有剩余。

为了方便(避免分别考虑线段、射线和线段),我们可以预先通过足够大的半径切割线段,以生成线段。

bool DoesLineIntersectCube(Line line, Cube cube)
{
    projectedOrigin = Project(cube.Centre, line);
    segment = Segment(projectedOrigin - line.Direction * 2, projectedOrigin + line.Direction * 2);
    for (axis : {0, 1, 2})
    {
        direction = (0, 0, 0);
        direction[axis] = 1;
        segment = Cut(segment, MakePlane(cube.Centre + direction * 0.5, direction));
        segment = Cut(segment, MakePlane(cube.Centre - direction * 0.5, -direction));
    }
    return !segment.IsEmpty();
}

希望这有所帮助。如果您需要任何进一步的帮助,请随时提问。

英文:

One of possible ways is to look at the cube as a part of space cut by 6 planes. Then just cut the line by those planes and see if anything left.

For convenience (to avoid considering line, ray and segment separately) we can cut the line preemptively by some large enough radius to make a segment from it.

bool DoesLineIntersectCube(Line line,Cube cube)
{
    projectedOrigin=Project(cube.Centre,line);
    segment=Segment(projectedOrigin-line.Direction*2,projectedorigin+line.Direction*2);
    for(axis:{0,1,2})
    {
        direction=(0,0,0);
        direction[axis]=1;
        segment=Cut(segment,MakePlane(cube.Centre+direction*0.5,direction);
        segment=Cut(segment,MakePlane(cube.Centre-direction*0.5,-direction);
    }
    return !segment.IsEmpty();
}

答案2

得分: 0

请看分离平面:

  • 考虑由线段的一个端点和立方体的一条边(2个端点)定义的平面。
  • 检查立方体的剩余6个顶点和线的另一个端点在平面的哪一侧。
  • 如果立方体的6个顶点都在同一侧,但只有线的端点在相反的一侧,那么线段和立方体不相交。

对所有可能的平面执行此检查。

英文:

Find separating plane:

  • Consider a plane defined by the one endpoint of the line segment and one edge(2 end points) of the cube.
  • Check which side of the plane the cube's remaining 6 vertices and line's another endpoint are.
  • If the cube's 6 vertices are on the same side but only the line's endpoint is on the opposite side, line segment and cube do not intersect.

Check this for all possible plane.

huangapple
  • 本文由 发表于 2023年5月7日 16:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定