平面/射线相交与点/平面投影的差异

时间:2018-11-07 15:33:19

标签: c++ graphics 3d

我在Wikipedia中找到了射线平面相交代码的解决方案,该方法行之有效,而且我只是在其中求解线性方程组。

后来,我找到了一些指向平面投影的代码,这些代码的实现方式显然有所不同,在某些条件下也会产生不同的解决方案。

但是,我并没有真正得到沿着矢量的点的投影与射线的交点(由点和矢量建立)之间的区别是什么。在这两种情况下,我都希望能找到射线与飞机相交的点?

在任何地方都可以用数字来说明差异吗?

struct Plane {
  glm::vec3 _normal;
  glm::vec3 _point;
};

glm::vec3 intersection(const Plane &p, const glm::vec3 &point, const glm::vec3 &direction) {
  // See: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
  const float t = glm::dot(p._normal, point - p._point) / glm::dot(p._normal, -direction);
  return point + t * direction;
}

glm::vec3 orthogonalProjection(const Plane &p, const glm::vec3 &point, const glm::vec3 &direction) {
  // from https://stackoverflow.com/questions/9605556/how-to-project-a-point-onto-a-plane-in-3d
  const float t = -(glm::dot(point, direction) - glm::dot(p.getOrigin(), p.getNormal()));
  return point+ t * direction;
}

2 个答案:

答案 0 :(得分:1)

射线是无限的线,因此具有方向。将射线与平面相交意味着找到直线穿过平面的位置。

点是悬浮在空间某处的无量纲点。将一个点投影到一个平面上,意味着发射一条穿过该点并垂直于该平面的射线(称为“法线”),并查看其着陆的位置。

射线已经有方向,点没有。选择投影点的方向是垂直于平面的方向,仅仅是因为这就是定义投影的方式。

因此,在特殊情况下,射线的方向与平面的法线重合,在这种情况下,将射线与平面相交并投射恰好位于射线某处的点会导致相同的结果,但这仅仅是特例。

答案 1 :(得分:0)

对于那些感兴趣的人,这是上面交集函数的 c# 版本。

谢谢@dgrat,你让我开心。

    /// <summary>Calculates the intersection of a ray with a plane</summary>
    /// <param name="rayOrigin">Any point of the ray</param>
    /// <param name="rayDirection">The direction of the ray</param>
    /// <param name="planeOrigin">Any point of the plane</param>
    /// <param name="planeNormal">The normal of the plane</param>
    /// <returns>The intersection</returns>
    private Point3D Intersection(Point3D rayOrigin, Vector3D rayDirection, Point3D planeOrigin, Vector3D planeNormal)
    {
        var length = Vector3D.DotProduct(planeNormal, rayOrigin - planeOrigin) / Vector3D.DotProduct(planeNormal, -rayDirection);
        return rayOrigin + length * rayDirection;
    }
相关问题