SignedDistancePlanePoint() public static method

public static SignedDistancePlanePoint ( Vector3 planeNormal, Vector3 planePoint, Vector3 point ) : float
planeNormal Vector3
planePoint Vector3
point Vector3
return float
Esempio n. 1
0
 /// <summary>
 /// Checks the collision status of a polygon.
 /// </summary>
 /// <param name="poly"></param>
 /// <param name="p0"></param>
 /// <param name="pn"></param>
 /// <param name="inside"></param>
 /// <param name="outside"></param>
 /// <param name="threshold"></param>
 public static void CheckPoly(IPoly poly, Vector3 p0, Vector3 pn, out bool inside, out bool outside, float threshold = 0.001f)
 {
     inside  = false;
     outside = false;
     for (int i = 0; i < poly.Resolution; i++)
     {
         Vector3 point = poly.GetPoint(i);
         float   dis   = Math3d.SignedDistancePlanePoint(pn, p0, point);
         if (dis < -threshold)
         {
             inside = true;
         }
         if (dis > threshold)
         {
             outside = true;
         }
         if (inside && outside)
         {
             return;
         }
     }
 }
Esempio n. 2
0
        private T GetPoly(BSPNode <T> node, Vector3 point)
        {
            if (node == null)
            {
                return(default(T));
            }

            if (node.poly.working.PointInPoly(point))
            {
                return(node.poly.original);
            }


            float value = Math3d.SignedDistancePlanePoint(node.planeNormal, node.planePoint, point);

            if (value < 0f)
            {
                return(GetPoly(node.inside, point));
            }
            else
            {
                return(GetPoly(node.outside, point));
            }
        }
        /// <summary>
        /// Divide a polygon using the specified plane.
        /// </summary>
        /// <param name="poly">
        /// The polygon to divide.
        /// </param>
        /// <param name="p0">
        /// Plane Origin.
        /// </param>
        /// <param name="pn">
        /// Plane Normal.
        /// </param>
        /// <param name="constructor">
        /// The Polygon constructor can be specified for more flexability.
        /// Defaults to new NGon.
        /// </param>
        /// <param name="threshold">
        /// The percision threshold for comparing points to the plane.
        /// </param>
        /// <returns>
        /// outputs new object[]{IPoly insidePolygon, IPoly outsidePolygon, Vector3 newlyCreatedPoint};
        /// newly created point is useful for face concstruction for Block Divison, and texture maping.
        /// </returns>
        public static object[] Divide(
            IPoly poly,
            Vector3 p0,
            Vector3 pn,
            Func <IPoly, IEnumerable <Vector3>, IPoly> constructor,
            float threshold = 0.001f)
        {
            //The following algorithm is used to divide faces
            //Create a list for the inside, and outside points
            //for each point, append it to the appropriate list
            //if the point intersects the boundry, append it to the itnersection list
            //construct new faces from the list
            //the intersection point exiting the plane is returned, for use by other algorithms

            //initialization
            List <Vector3> insidePoints  = new List <Vector3>();
            List <Vector3> outsidePoints = new List <Vector3>();
            bool           hasOutputPont = false;
            Vector3        outputPoint   = default(Vector3);
            int            l             = poly.Resolution;

            int[] signs = new int[l];

            //itterate through poitns and calculate their signs
            for (int i = 0; i < l; i++)
            {
                float f = Math3d.SignedDistancePlanePoint(pn, p0, poly.GetPoint(i));
                if (f < -threshold)
                {
                    signs[i] = -1;
                }
                else if (f > threshold)
                {
                    signs[i] = 1;
                }
                else
                {
                    signs[i] = 0;
                }
            }

            //itterate through points, and append them to the appropriate list
            for (int i = 0; i < l; i++)
            {
                Vector3 l0 = poly.GetPoint(i);
                //inside point
                if (signs[i] == -1)
                {
                    insidePoints.Add(l0);
                }
                //intersecting point
                else if (signs[i] == 0)
                {
                    insidePoints.Add(l0);
                    outsidePoints.Add(l0);
                    if (signs[(i + 1) % l] == 1)
                    {
                        outputPoint   = l0;
                        hasOutputPont = true;
                    }
                }
                //outside point
                else
                {
                    outsidePoints.Add(l0);
                }

                //detect intersections
                if (signs[i] * signs[(i + 1) % l] == -1)
                {
                    Vector3 ld           = poly.GetPoint((i + 1) % l) - l0;
                    Vector3 intersection = Math3d.LinePlaneIntersection(l0, ld, p0, pn);
                    insidePoints.Add(intersection);
                    outsidePoints.Add(intersection);
                    if (signs[i] == -1)
                    {
                        outputPoint   = intersection;
                        hasOutputPont = true;
                    }
                }
            }

            //construct output
            object point = null;

            if (hasOutputPont)
            {
                point = outputPoint;
            }
            return(new object[] {
                insidePoints.Count >= 3 ? constructor(poly, insidePoints) : null,
                outsidePoints.Count >= 3 ? constructor(poly, outsidePoints) : null,
                point
            });
        }