mathnet/mathnet-spatial

Plane.Project(Point, UnitVector3D) does not project onto the plane.

vanzomerenc opened this issue · 3 comments

This is with MathNet.Spatial 0.5.0 beta 4.

Plane.Project(Point, UnitVector3D) projects the point in the direction of the given vector, but does not project it onto the given plane.

Here is an example which fails:

    [TestMethod]
    public void TestPlaneProjection()
    {
        var plane = new Plane(new UnitVector3D(3, 0, 4), new Point3D(0, 0, 0));
        var point = new Point3D(8, 0, 0);
        
        var projectedPoint = plane.Project(point, UnitVector3D.ZAxis);
        var expectedPoint = new Point3D(8, 0, -6);
        
        Assert.AreEqual(expectedPoint.X, projectedPoint.X, 0.1);
        Assert.AreEqual(expectedPoint.Y, projectedPoint.Y, 0.1);
        Assert.AreEqual(expectedPoint.Z, projectedPoint.Z, 0.1);
    }

And here is a version of Project which passes, though I don't know if it's the most efficient or numerically stable way to do it:

    public Point3D Project(Point3D point, UnitVector3D direction)
    {
        var distance = ((this.RootPoint - point).DotProduct(this.Normal)) / (direction.DotProduct(this.Normal));
        return point + distance * direction;
    }

Is this a new bug in 0.5.0?

It looks like it isn't new. I've tried it with version 0.4 and have the same problem, and it looks like that function hasn't been changed in 5 years.

It doesn't appear to affect anything else within the library, because the only places where a vector is specified, that vector is already perpendicular to the plane.