.net3dplane

.NET System.Numerics.Plane creation difficulties


I want to use the .NET System.Numerics.Plane struct in my geometry calculations, but I'm finding it difficult to create instances of it from the data I have. I have an array of Vector3 vertices (all coplanar) and a normal, but in my data it is possible that some consecutive vertices could be collinear, so while the Plane.CreateFromVertices() method is usable, I'd have to do extra work to present three vertices that are guaranteed not collinear. I'm sure there must be a more efficient method.

All the other methods for constructing a Plane seem to need (in various representations) the normal and the (shortest) distance D from the plane to the origin. I don't know how to calculate D, and there doesn't seem to be much help out there!

It should be possible to create a plane from one vertex and the plane normal, but when I look online for help with that (e.g. here), all the solutions seem to end up with a standard plane equation (ax + by + cz = k), and I can't figure out how to create a Plane struct from that, either!

I would really appreciate help in any of the three scenarios above, summariazed as follows:

  1. How to calculate D from my data
  2. How to instantiate Plane with one arbitrary vertex and the normal
  3. How to instantiate Plane with an equation like ax + by + cz = k

Item 2 above would be most useful.


Solution

  • The answer to item 2 is fairly straightforward:

    public static Plane CreateFromArbitraryVertexAndNormal(Vector3 vertex, Vector3 normal)
    {
        return new(normal, Vector3.Dot(normal, vertex));
    }
    

    Note that 'D' is "the distance of the plane along its normal from the origin." (Quoted from the Plane.cs source code.) This distance is not an absolute value; it would be negative if the normal pointed towards the origin.