Ejemplo n.º 1
0
        // ---------------------------------------------------------------
        // Determine shortest distance to ring perimeter
        // i0 and i1 are indices for vertexes around closest line in ring
        // In the case of equality, one is chosen specifically to handle
        //   the degenerate case in point-in-polygon test
        // ---------------------------------------------------------------
        public float DistanceToRingPerimeter(Vector3 v, out int i0, out int i1)
        {
            int prevIndex = verts.Count - 1;

            i0 = prevIndex;
            i1 = 0;
            float dMin         = float.PositiveInfinity;
            float lastLineDist = VectorUtil.DistanceToLine(v, verts[prevIndex - 1], verts[prevIndex]);

            for (int i = 0; i < verts.Count; i++)
            {
                float lineDist = VectorUtil.DistanceToLine(v, verts[prevIndex], verts[i]);;
                float d        = VectorUtil.DistanceToEdge(v, verts[prevIndex], verts[i]);
                if (d == dMin)   // handle degenerate case for point-in-polygon test
                {
                    if (lineDist > lastLineDist)
                    {
                        dMin = d;
                        i0   = prevIndex;
                        i1   = i;
                    }
                }
                else if (d < dMin)
                {
                    dMin = d;
                    i0   = prevIndex;
                    i1   = i;
                }
                lastLineDist = lineDist;
                prevIndex    = i;
            }
            return(dMin);
        }
Ejemplo n.º 2
0
        // ----------------------------------------------------------
        // Checks if a point is on the positive side of the template
        // ----------------------------------------------------------
        public bool IsAbove(Vector3 v)
        {
            Vector3 pv = plane.Project(v);
            float   dMin;
            int     i0;
            Vector3 dif;
            float   lastLineDist;

            if (isClosed)
            {
                dif          = VectorUtil.VectorToEdge(pv, points.Last(), points.First());
                lastLineDist = VectorUtil.DistanceToLine(pv, points.Last(), points.First());
                i0           = points.Count - 1;
                dMin         = dif.magnitude;
            }
            else
            {
                dif          = VectorUtil.VectorToEdge(pv, points[0], points[1]);
                lastLineDist = VectorUtil.DistanceToLine(pv, points[0], points[1]);
                i0           = 0;
                dMin         = dif.magnitude;
            }
            for (int i = 0; i < points.Count - 1; i++)
            {
                float   lineDist = VectorUtil.DistanceToLine(pv, points[i], points[i + 1]);
                Vector3 vec      = VectorUtil.VectorToEdge(pv, points[i], points[i + 1]);
                if (vec.magnitude == dMin)
                {
                    if (lineDist > lastLineDist)
                    {
                        i0   = i;
                        dMin = vec.magnitude;
                        dif  = vec;
                    }
                }
                else if (vec.magnitude < dMin)
                {
                    i0   = i;
                    dMin = vec.magnitude;
                    dif  = vec;
                }
                lastLineDist = lineDist;
            }
            int i1 = i0 + 1 == points.Count ? 0 : i0 + 1;

            return(Vector3.Dot(
                       Vector3.Cross(
                           dif,
                           points[i1] - points[i0]
                           ),
                       normal
                       ) > 0);
        }