dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.38k stars 4.75k forks source link

[API Proposal]: Plane.Project #107407

Open PavielKraskouski opened 2 months ago

PavielKraskouski commented 2 months ago

Background and motivation

Projecting a point onto a plane is a basic operation that can be used in a variety of problems.

API Proposal

namespace System.Numerics;

public struct Plane
{
    public static Vector3 Project(Plane plane, Vector3 point)
    {
        return point - (Vector3.Dot(plane.Normal, point) + plane.D) * plane.Normal;
    }
}

API Usage

Vector3 point = new(1, 2, 3);
Plane plane = new(Vector3.UnitY, -5);
Vector3 projectedPoint = Plane.Project(plane, point);

Alternative Designs

No response

Risks

No response

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.

julealgon commented 2 months ago

Wouldn't this be more intuitive if it was an extension on Vector instead of a pure static method?

public static class VectorProjectionExtensions
{
    public static Vector3 ProjectOnto(this Vector3 point, Plane plane)
Vector3 point = new(1, 2, 3);
Plane plane = new(Vector3.UnitY, -5);
Vector3 projectedPoint = point.ProjectOnto(plane);
PavielKraskouski commented 2 months ago

In this case, it is better to make this method static for Vector3, similar to Vector3.Transform methods.

namespace System.Numerics;

public struct Vector3
{
    public static Vector3 Project(Vector3 point, Plane plane);
}
julealgon commented 2 months ago

In this case, it is better to make this method static for Vector3, similar to Vector3.Transform methods.

@PavielKraskouski can you elaborate why it is better? Are you arguing just for consistency with Vector3.Transform, or is there more to it?

I think the extension method approach not only makes this more intuitive to use, but also more discoverable.

I do agree that Vector3.Project seems more intuitive than Plane.Project though as the latter gives the impression you are "projecting the plane", which is not really what is happening.

PavielKraskouski commented 2 months ago

@julealgon My main argument is that .Net doesn't have a separate extension method class for vectors, so I think creating a class with one method is impractical. However, if .Net developers approve this method, they'll decide where to add it.