Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public partial struct Plane : System.IEquatable<System.Numerics.Plane>
public static System.Numerics.Plane Normalize(System.Numerics.Plane value) { throw null; }
public static bool operator ==(System.Numerics.Plane value1, System.Numerics.Plane value2) { throw null; }
public static bool operator !=(System.Numerics.Plane value1, System.Numerics.Plane value2) { throw null; }
public static System.Numerics.Vector3 Project(System.Numerics.Plane plane, System.Numerics.Vector3 point) { throw null; }
public override readonly string ToString() { throw null; }
public static System.Numerics.Plane Transform(System.Numerics.Plane plane, System.Numerics.Matrix4x4 matrix) { throw null; }
public static System.Numerics.Plane Transform(System.Numerics.Plane plane, System.Numerics.Quaternion rotation) { throw null; }
Expand Down
40 changes: 40 additions & 0 deletions src/libraries/System.Numerics.Vectors/tests/PlaneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,46 @@ public void PlaneNormalizeTest()
Assert.True(MathHelper.Equal(expected, actual), "Plane.Normalize returns unexpected value.");
}

[Fact]
public void PlaneProjectTest()
{
// Example from the API proposal: the plane y = 5.
Plane plane = new Plane(Vector3.UnitY, -5);
Vector3 actual = Plane.Project(plane, new Vector3(1, 2, 3));
Assert.True(MathHelper.Equal(new Vector3(1, 5, 3), actual), "Plane.Project returns unexpected value.");

// Points on either side of the plane project to the same point.
actual = Plane.Project(plane, new Vector3(1, 8, 3));
Assert.True(MathHelper.Equal(new Vector3(1, 5, 3), actual), "Plane.Project returns unexpected value.");

// A point already on the plane is unchanged.
actual = Plane.Project(plane, new Vector3(-4, 5, 7));
Assert.True(MathHelper.Equal(new Vector3(-4, 5, 7), actual), "Plane.Project returns unexpected value.");
}

[Theory]
[InlineData(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, -6.0f, 7.0f)]
[InlineData(-3.0f, 0.5f, 2.0f, -1.5f, 0.0f, 0.0f, 0.0f)]
[InlineData(0.0f, 0.0f, 1.0f, 0.0f, 10.0f, 20.0f, -30.0f)]
public void PlaneProjectArbitraryPlaneTest(float x, float y, float z, float d, float px, float py, float pz)
{
Plane plane = Plane.Normalize(new Plane(x, y, z, d));
Vector3 point = new Vector3(px, py, pz);

Vector3 actual = Plane.Project(plane, point);

// The projected point lies on the plane.
Assert.True(MathHelper.Equal(0.0f, Plane.DotCoordinate(plane, actual)), "Plane.Project did not return a point on the plane.");

// The projection moves the point along the plane's normal by its signed distance to the plane.
Vector3 expected = point - ((Vector3.Dot(plane.Normal, point) + plane.D) * plane.Normal);
Assert.True(MathHelper.Equal(expected, actual), "Plane.Project returns unexpected value.");
Assert.True(MathHelper.Equal(Vector3.Zero, Vector3.Cross(point - actual, plane.Normal)), "Plane.Project did not move the point along the plane's normal.");

// Projecting again doesn't move the point.
Assert.True(MathHelper.Equal(actual, Plane.Project(plane, actual)), "Plane.Project is not idempotent.");
}

[Fact]
// Transform by matrix
public void PlaneTransformTest1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public static float DotNormal(Plane plane, Vector3 value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Plane Normalize(Plane value) => Normalize(value.AsVector128()).AsPlane();

/// <summary>Projects a point onto a normalized plane.</summary>
/// <param name="plane">The normalized plane to project onto.</param>
/// <param name="point">The point to project.</param>
/// <returns>The point on <paramref name="plane" /> that is closest to <paramref name="point" />.</returns>
/// <remarks><paramref name="plane" /> must already be normalized so that its <see cref="Normal" /> vector is of unit length before this method is called.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Project(Plane plane, Vector3 point) => point - (DotCoordinate(plane, point) * plane.Normal);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<float> Normalize(Vector128<float> value)
{
Expand Down
Loading