mathnet / mathnet-spatial

Math.NET Spatial
http://spatial.mathdotnet.com
MIT License
376 stars 132 forks source link

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

Closed vanzomerenc closed 1 year ago

vanzomerenc commented 5 years ago

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;
    }
JohanLarsson commented 5 years ago

Is this a new bug in 0.5.0?

vanzomerenc commented 5 years ago

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.

vanzomerenc commented 5 years ago

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.