public double Planeness(Point3d p1, Point3d p2, Point3d p3, Point3d p4)
 {
     Line l1 = new Line(p1, p3);
     Line l2 = new Line(p2, p4);
     return l1.MinimumDistanceTo(l2);
 }
    private static void Reduce_RecursiveComponent(Point3d[] P, bool[] vertex_map, double tolerance, int A, int B)
    {
      //Abort if there is nothing left to collapse.
      if (B <= (A + 1)) { return; }

      // Create basis segment.E
      Line segment = new Line(P[A], P[B]);

      double max_d = 0.0;
      int max_i = A;

      // Iterate over remaining points in subset and find furthest point.
      for (int i = A + 1; i < B; i++)
      {
        double loc_d = segment.MinimumDistanceTo(P[i]);
        if (loc_d > max_d)
        {
          max_d = loc_d;
          max_i = i;
        }
      }

      if (max_d > tolerance)
      {
        vertex_map[max_i] = true;

        Reduce_RecursiveComponent(P, vertex_map, tolerance, A, max_i);
        Reduce_RecursiveComponent(P, vertex_map, tolerance, max_i, B);
      }
    }
        public List<double> checkCoplanarity(SpringMesh sMesh, double thickness, List<Line> linesUp, List<Line> linesDown)
        {
            List<double> coplanarity = new List<double>();

            foreach (Edge edge in sMesh.Edges)
            {
                if (edge.SecondTriangleIndex >= 0)
                {
                    int triLeft = edge.FirstTriangleIndex;
                    int triRight = edge.SecondTriangleIndex;

                    //Point3d centreLeft = sMesh.ComputeTriangleCenter(triLeft);
                    //Point3d centreRight = sMesh.ComputeTriangleCenter(triRight);

                    Point3d centreLeft = sMesh.ComputeCircumscribedCircleCenter(triLeft);
                    Point3d centreRight = sMesh.ComputeCircumscribedCircleCenter(triRight);

                    Vector3d normalLeft = sMesh.ComputeTriangleNormal(triLeft);
                    Vector3d normalRight = sMesh.ComputeTriangleNormal(triRight);

                    Point3d centreLeftUp = centreLeft + normalLeft * thickness;
                    Point3d centreRightUp = centreRight + normalRight * thickness;

                    Point3d centreLeftDown = centreLeft - normalLeft * thickness;
                    Point3d centreRightDown = centreRight - normalRight * thickness;

                    linesUp.Add(new Line(centreLeftUp, centreRightUp));
                    linesDown.Add(new Line(centreLeftDown, centreRightDown));

                    Line planarCheckLine01 = new Line(centreLeftUp, centreRightDown);
                    Line planarCheckLine02 = new Line(centreRightUp, centreLeftDown);

                    double dist = planarCheckLine01.MinimumDistanceTo(planarCheckLine02);
                    coplanarity.Add(dist);
                }
            }
            return coplanarity;
        }