Пример #1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="GeomPolyVal" /> class
 /// </summary>
 /// <param name="geomP">The geom</param>
 /// <param name="k">The </param>
 public GeomPolyVal(GeomPoly geomP, int k)
 {
     GeomP = geomP;
     Key   = k;
 }
Пример #2
0
        /// <summary>
        ///     Used in polygon composition to composit polygons into scan lines Combining polya and polyb into one
        ///     super-polygon stored in polya.
        /// </summary>
        private static void CombLeft(ref GeomPoly polya, ref GeomPoly polyb)
        {
            CxFastList <Vector2>     ap = polya.Points;
            CxFastList <Vector2>     bp = polyb.Points;
            CxFastListNode <Vector2> ai = ap.Begin();
            CxFastListNode <Vector2> bi = bp.Begin();

            Vector2 b = bi.GetElem();
            CxFastListNode <Vector2> prea = null;

            while (ai != ap.End())
            {
                Vector2 a = ai.GetElem();
                if (VecDsq(a, b) < MathConstants.Epsilon)
                {
                    //ignore shared vertex if parallel
                    if (prea != null)
                    {
                        Vector2 a0 = prea.GetElem();
                        b = bi.GetNext().GetElem();

                        Vector2 u = a - a0;

                        //vec_new(u); vec_sub(a.p.p, a0.p.p, u);
                        Vector2 v = b - a;

                        //vec_new(v); vec_sub(b.p.p, a.p.p, v);
                        float dot = VecCross(u, v);
                        if (dot * dot < MathConstants.Epsilon)
                        {
                            ap.Erase(prea, ai);
                            polya.Length--;
                            ai = prea;
                        }
                    }

                    //insert polyb into polya
                    bool fst = true;
                    CxFastListNode <Vector2> preb = null;
                    while (!bp.Empty())
                    {
                        Vector2 bb = bp.Front();
                        bp.Pop();
                        if (!fst && !bp.Empty())
                        {
                            ai = ap.Insert(ai, bb);
                            polya.Length++;
                            preb = ai;
                        }

                        fst = false;
                    }

                    //ignore shared vertex if parallel
                    ai = ai.GetNext();
                    Vector2 a1 = ai.GetElem();
                    ai = ai.GetNext();
                    if (ai == ap.End())
                    {
                        ai = ap.Begin();
                    }

                    Vector2 a2  = ai.GetElem();
                    Vector2 a00 = preb.GetElem();
                    Vector2 uu  = a1 - a00;

                    //vec_new(u); vec_sub(a1.p, a0.p, u);
                    Vector2 vv = a2 - a1;

                    //vec_new(v); vec_sub(a2.p, a1.p, v);
                    float dot1 = VecCross(uu, vv);
                    if (dot1 * dot1 < MathConstants.Epsilon)
                    {
                        ap.Erase(preb, preb.GetNext());
                        polya.Length--;
                    }

                    return;
                }

                prea = ai;
                ai   = ai.GetNext();
            }
        }
Пример #3
0
        /// <summary>
        ///     Marching squares over the given domain using the mesh defined via the dimensions (wid,hei) to build a set of
        ///     polygons such that f(x,y) less than 0, using the given number 'bin' for recursive linear inteprolation along cell
        ///     boundaries. if 'comb' is true, then the polygons will also be composited into larger possible concave polygons.
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="cellWidth"></param>
        /// <param name="cellHeight"></param>
        /// <param name="f"></param>
        /// <param name="lerpCount"></param>
        /// <param name="combine"></param>
        /// <returns></returns>
        public static List <Vertices> DetectSquares(Aabb domain, float cellWidth, float cellHeight, sbyte[,] f,
                                                    int lerpCount, bool combine)
        {
            CxFastList <GeomPoly> ret = new CxFastList <GeomPoly>();

            List <Vertices> verticesList = new List <Vertices>();

            //NOTE: removed assignments as they were not used.
            List <GeomPoly> polyList;
            GeomPoly        gp;

            int  xn = (int)(domain.Extents.X * 2 / cellWidth);
            bool xp = xn == domain.Extents.X * 2 / cellWidth;
            int  yn = (int)(domain.Extents.Y * 2 / cellHeight);
            bool yp = yn == domain.Extents.Y * 2 / cellHeight;

            if (!xp)
            {
                xn++;
            }

            if (!yp)
            {
                yn++;
            }

            sbyte[,] fs       = new sbyte[xn + 1, yn + 1];
            GeomPolyVal[,] ps = new GeomPolyVal[xn + 1, yn + 1];

            //populate shared function lookups.
            for (int x = 0; x < xn + 1; x++)
            {
                int x0;
                if (x == xn)
                {
                    x0 = (int)domain.UpperBound.X;
                }
                else
                {
                    x0 = (int)(x * cellWidth + domain.LowerBound.X);
                }

                for (int y = 0; y < yn + 1; y++)
                {
                    int y0;
                    if (y == yn)
                    {
                        y0 = (int)domain.UpperBound.Y;
                    }
                    else
                    {
                        y0 = (int)(y * cellHeight + domain.LowerBound.Y);
                    }

                    fs[x, y] = f[x0, y0];
                }
            }

            //generate sub-polys and combine to scan lines
            for (int y = 0; y < yn; y++)
            {
                float y0 = y * cellHeight + domain.LowerBound.Y;
                float y1;
                if (y == yn - 1)
                {
                    y1 = domain.UpperBound.Y;
                }
                else
                {
                    y1 = y0 + cellHeight;
                }

                GeomPoly pre = null;
                for (int x = 0; x < xn; x++)
                {
                    float x0 = x * cellWidth + domain.LowerBound.X;
                    float x1;
                    if (x == xn - 1)
                    {
                        x1 = domain.UpperBound.X;
                    }
                    else
                    {
                        x1 = x0 + cellWidth;
                    }

                    gp = new GeomPoly();

                    int key = MarchSquare(f, fs, ref gp, x, y, x0, y0, x1, y1, lerpCount);
                    if (gp.Length != 0)
                    {
                        if (combine && pre != null && (key & 9) != 0)
                        {
                            CombLeft(ref pre, ref gp);
                            gp = pre;
                        }
                        else
                        {
                            ret.Add(gp);
                        }

                        ps[x, y] = new GeomPolyVal(gp, key);
                    }
                    else
                    {
                        gp = null;
                    }

                    pre = gp;
                }
            }

            if (!combine)
            {
                polyList = ret.GetListOfElements();

                foreach (GeomPoly poly in polyList)
                {
                    verticesList.Add(new Vertices(poly.Points.GetListOfElements()));
                }

                return(verticesList);
            }

            //combine scan lines together
            for (int y = 1; y < yn; y++)
            {
                int x = 0;
                while (x < xn)
                {
                    GeomPolyVal p = ps[x, y];

                    //skip along scan line if no polygon exists at this point
                    if (p == null)
                    {
                        x++;
                        continue;
                    }

                    //skip along if current polygon cannot be combined above.
                    if ((p.Key & 12) == 0)
                    {
                        x++;
                        continue;
                    }

                    //skip along if no polygon exists above.
                    GeomPolyVal u = ps[x, y - 1];
                    if (u == null)
                    {
                        x++;
                        continue;
                    }

                    //skip along if polygon above cannot be combined with.
                    if ((u.Key & 3) == 0)
                    {
                        x++;
                        continue;
                    }

                    float ax = x * cellWidth + domain.LowerBound.X;
                    float ay = y * cellHeight + domain.LowerBound.Y;

                    CxFastList <Vector2> bp = p.GeomP.Points;
                    CxFastList <Vector2> ap = u.GeomP.Points;

                    //skip if it's already been combined with above polygon
                    if (u.GeomP == p.GeomP)
                    {
                        x++;
                        continue;
                    }

                    //combine above (but disallow the hole thingies
                    CxFastListNode <Vector2> bi = bp.Begin();
                    while (Square(bi.GetElem().Y - ay) > MathConstants.Epsilon || bi.GetElem().X < ax)
                    {
                        bi = bi.GetNext();
                    }

                    //NOTE: Unused
                    //Vector2 b0 = bi.elem();
                    Vector2 b1 = bi.GetNext().GetElem();
                    if (Square(b1.Y - ay) > MathConstants.Epsilon)
                    {
                        x++;
                        continue;
                    }

                    bool brk = true;
                    CxFastListNode <Vector2> ai = ap.Begin();
                    while (ai != ap.End())
                    {
                        if (VecDsq(ai.GetElem(), b1) < MathConstants.Epsilon)
                        {
                            brk = false;
                            break;
                        }

                        ai = ai.GetNext();
                    }

                    if (brk)
                    {
                        x++;
                        continue;
                    }

                    CxFastListNode <Vector2> bj = bi.GetNext().GetNext();
                    if (bj == bp.End())
                    {
                        bj = bp.Begin();
                    }

                    while (bj != bi)
                    {
                        ai = ap.Insert(ai, bj.GetElem()); // .clone()
                        bj = bj.GetNext();
                        if (bj == bp.End())
                        {
                            bj = bp.Begin();
                        }

                        u.GeomP.Length++;
                    }

                    //u.p.simplify(float.Epsilon,float.Epsilon);
                    //
                    ax = x + 1;
                    while (ax < xn)
                    {
                        GeomPolyVal p2 = ps[(int)ax, y];
                        if (p2 == null || p2.GeomP != p.GeomP)
                        {
                            ax++;
                            continue;
                        }

                        p2.GeomP = u.GeomP;
                        ax++;
                    }

                    ax = x - 1;
                    while (ax >= 0)
                    {
                        GeomPolyVal p2 = ps[(int)ax, y];
                        if (p2 == null || p2.GeomP != p.GeomP)
                        {
                            ax--;
                            continue;
                        }

                        p2.GeomP = u.GeomP;
                        ax--;
                    }

                    ret.Remove(p.GeomP);
                    p.GeomP = u.GeomP;

                    x = (int)((bi.GetNext().GetElem().X - domain.LowerBound.X) / cellWidth) + 1;

                    //x++; this was already commented out!
                }
            }

            polyList = ret.GetListOfElements();

            foreach (GeomPoly poly in polyList)
            {
                verticesList.Add(new Vertices(poly.Points.GetListOfElements()));
            }

            return(verticesList);
        }
Пример #4
0
        /// <summary>
        ///     Look-up table to relate polygon key with the vertices that should be used for the sub polygon in marching
        ///     squares Perform a single celled marching square for for the given cell defined by (x0,y0) (x1,y1) using the
        ///     function f
        ///     for recursive interpolation, given the look-up table 'fs' of the values of 'f' at cell vertices with the result to
        ///     be
        ///     stored in 'poly' given the actual coordinates of 'ax' 'ay' in the marching squares mesh.
        /// </summary>
        private static int MarchSquare(sbyte[,] f, sbyte[,] fs, ref GeomPoly poly, int ax, int ay, float x0, float y0,
                                       float x1, float y1, int bin)
        {
            //key lookup
            int   key = 0;
            sbyte v0  = fs[ax, ay];

            if (v0 < 0)
            {
                key |= 8;
            }

            sbyte v1 = fs[ax + 1, ay];

            if (v1 < 0)
            {
                key |= 4;
            }

            sbyte v2 = fs[ax + 1, ay + 1];

            if (v2 < 0)
            {
                key |= 2;
            }

            sbyte v3 = fs[ax, ay + 1];

            if (v3 < 0)
            {
                key |= 1;
            }

            int val = LookMarch[key];

            if (val != 0)
            {
                CxFastListNode <Vector2> pi = null;
                for (int i = 0; i < 8; i++)
                {
                    Vector2 p;
                    if ((val & (1 << i)) != 0)
                    {
                        if (i == 7 && (val & 1) == 0)
                        {
                            poly.Points.Add(p = new Vector2(x0, Ylerp(y0, y1, x0, v0, v3, f, bin)));
                        }
                        else
                        {
                            if (i == 0)
                            {
                                p = new Vector2(x0, y0);
                            }
                            else if (i == 2)
                            {
                                p = new Vector2(x1, y0);
                            }
                            else if (i == 4)
                            {
                                p = new Vector2(x1, y1);
                            }
                            else if (i == 6)
                            {
                                p = new Vector2(x0, y1);
                            }

                            else if (i == 1)
                            {
                                p = new Vector2(Xlerp(x0, x1, y0, v0, v1, f, bin), y0);
                            }
                            else if (i == 5)
                            {
                                p = new Vector2(Xlerp(x0, x1, y1, v3, v2, f, bin), y1);
                            }

                            else if (i == 3)
                            {
                                p = new Vector2(x1, Ylerp(y0, y1, x1, v1, v2, f, bin));
                            }
                            else
                            {
                                p = new Vector2(x0, Ylerp(y0, y1, x0, v0, v3, f, bin));
                            }

                            pi = poly.Points.Insert(pi, p);
                        }

                        poly.Length++;
                    }
                }

                //poly.simplify(float.Epsilon,float.Epsilon);
            }

            return(key);
        }