How to determine if 4 points are on the same plane

I select 4 points from the image displayed by Kinect, because each point has its own coordinates (x, y, z).

My goal is to determine if 4 points fall on the same plane.

This is my function:

    public bool isValidPlane()
    {
        for (int i = 0; i < edgesPoints.Length; i++)
        {
            double absPlaneEquation = Math.Abs(distance -
                (normal.X * edgesPoints[i].X + normal.Y * edgesPoints[i].Y + normal.Z * edgesPoints[i].Z));
            if (absPlaneEquation > 1500) /* 1500 is a tolerance error*/
            {
                return false;
            }
        }
        return true;
    }

normal It is also normal (the transverse product of two vectors on the plane, which was previously calculated from 3 of 4 selected points) and the plane is normalized:

    private void calcPlaneNormalVector()
    {
        if (lastEdgeNumber < 3)
        {
            return;
        }
        Vector3D vec1 = new Vector3D(edgesPoints[0], edgesPoints[1]);
        Vector3D vec2 = new Vector3D(edgesPoints[0], edgesPoints[2]);
        vec2 = vec1.crossProduct(vec2);
        double lengthNormal = Math.Sqrt(Math.Pow(vec2.X, 2) + Math.Pow(vec2.Y, 2) + Math.Pow(vec2.Z, 2));
//normalizing:
        normal = new Vector3D((vec2.X / lengthNormal), (vec2.Y / lengthNormal), (vec2.Z / lengthNormal));
        distance = (-1) * (edgesPoints[0].X * normal.X + edgesPoints[0].Y * normal.Y + edgesPoints[0].Z + normal.Z);
    }

Vector3D is a class representing a vector:

public  class Vector3D
{
    private double x, y, z;

    public Vector3D(Point3D p1, Point3D p2)
    {
        x = p2.X - p1.X;
        y = p2.Y - p1.Y;
        z = p2.Z - p1.Z;
    }
    public Vector3D(double a = 0, double b = 0, double c = 0)
    {
        x = a;
        y = b;
        z = c;
    }

    <get properties for x, y, z >

    public Vector3D crossProduct(Vector3D u)
    {
        double tmpX = 0, tmpY = 0, tmpZ = 0;
        tmpX = y * u.Z - z * u.Y;
        tmpY = z * u.X - x * u.Z;
        tmpZ = x * u.Y - y * u.X;
        return new Vector3D(tmpX, tmpY, tmpZ);
    }
    public double dotProduct(Vector3D u)
    {
        return x * u.X + y * u.Y + z * u.Z;
    }
}

i always turns out 1300 <= absPlaneEquation <= 1400, even when 4 points are chosen so that they will not be on the same plane.

What is the best way to determine that 4 points are on the same plane?

+4
source share
1 answer

, :

normal vector components : [A, B, C]
Plane equation           : A·x + B·y + C·z + D = 0;

(P1, P2 P3), D, , (P4) :

 D = - (A·x1 + B·y1 + C·z1)
 A·x4 + B·y4 + C·z4 - (A·x1 + B·y1 + C·z1) = 0

importat, , , . , :

 |A·x4 + B·y4 + C·z4 - (A·x1 + B·y1 + C·z1)| < TOLERANCE

UPDATE: :

public struct Point3D
{
    public double X { get; }
    public double Y { get; }
    public double Z { get; }

    public Point3D(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

public struct Vector3D
{
    public double X { get; }
    public double Y { get; }
    public double Z { get; }
    public double Magnitude => Math.Sqrt(X * X + Y * Y + Z * Z);

    public Vector3D(Point3D p1, Point3D p2)
        : this(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z)
    {
    }

    public Vector3D(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public static Vector3D CrossProduct(Vector3D left, Vector3D right)
    {
        double tmpX = 0, tmpY = 0, tmpZ = 0;
        tmpX = left.Y * right.Z - left.Z * right.Y;
        tmpY = left.Z * right.X - left.X * right.Z;
        tmpZ = left.X * right.Y - left.Y * right.X;
        return new Vector3D(tmpX, tmpY, tmpZ);
    }

    public static double DotProduct(Vector3D left, Vector3D right)
    {
        return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
    }
}

public struct Plane3D
{
    private const double TOLERANCE = 0.001;

    private readonly double independentTerm;
    public Vector3D Normal { get; }

    public Plane3D(Point3D p1, Point3D p2, Point3D p3)
    {
        Normal = Vector3D.crossProduct(new Vector3D(p1, p2), new Vector3D(p1, p3));

        if (Normal.Magnitude < TOLERANCE)
            throw new ArgumentException("Specified points do not define a valid plane.");

        independentTerm = -(Normal.X * p1.X + Normal.Y * p1.Y + Normal.Z * p1.Z);
    }

    public bool Contains(Point3D p) => Math.Abs(Normal.X * p.X + Normal.Y * p.Y + Normal.Z * p.Z + independentTerm) < TOLERANCE;
}

:

  • Point3D Vector3D . , , , , .
  • . ; , , , , . .
  • , .
  • static. .
  • TOLERANCE const Plane. , , , .
  • ; .
+5

Source: https://habr.com/ru/post/1652343/


All Articles