/// <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.Elem().Y - ay) > Settings.Epsilon || bi.Elem().X < ax) { bi = bi.Next(); } //NOTE: Unused //Vector2 b0 = bi.elem(); Vector2 b1 = bi.Next().Elem(); if (Square(b1.Y - ay) > Settings.Epsilon) { x++; continue; } bool brk = true; CxFastListNode <Vector2> ai = ap.Begin(); while (ai != ap.End()) { if (VecDsq(ai.Elem(), b1) < Settings.Epsilon) { brk = false; break; } ai = ai.Next(); } if (brk) { x++; continue; } CxFastListNode <Vector2> bj = bi.Next().Next(); if (bj == bp.End()) { bj = bp.Begin(); } while (bj != bi) { ai = ap.Insert(ai, bj.Elem()); // .clone() bj = bj.Next(); 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.Next().Elem().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); }
/// <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) < 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="cell_width"></param> /// <param name="cell_height"></param> /// <param name="f"></param> /// <param name="lerp_count"></param> /// <param name="combine"></param> /// <returns></returns> public static List <Vertices> DetectSquares(AABB domain, float cell_width, float cell_height, sbyte[,] f, int lerp_count, bool combine) { var ret = new CxFastList <GeomPoly>(); List <Vertices> verticesList = new List <Vertices>(); List <GeomPoly> polyList = ret.GetListOfElements(); GeomPoly gp = new GeomPoly(); var xn = (int)(domain.Extents.X * 2 / cell_width); var xp = xn == (domain.Extents.X * 2 / cell_width); var yn = (int)(domain.Extents.Y * 2 / cell_height); var yp = yn == (domain.Extents.Y * 2 / cell_height); if (!xp) { xn++; } if (!yp) { yn++; } var fs = new sbyte[xn + 1, yn + 1]; var 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 * cell_width + 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 * cell_height + domain.LowerBound.Y); } fs[x, y] = f[x0, y0]; } } //generate sub-polys and combine to scan lines for (int y = 0; y < yn; y++) { var y0 = y * cell_height + domain.LowerBound.Y; float y1; if (y == yn - 1) { y1 = domain.UpperBound.Y; } else { y1 = y0 + cell_height; } GeomPoly pre = null; for (int x = 0; x < xn; x++) { var x0 = x * cell_width + domain.LowerBound.X; float x1; if (x == xn - 1) { x1 = domain.UpperBound.X; } else { x1 = x0 + cell_width; } gp = new GeomPoly(); var key = marchSquare(f, fs, ref gp, x, y, x0, y0, x1, y1, lerp_count); 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 (var poly in polyList) { verticesList.Add(new Vertices(poly.points.GetListOfElements())); } return(verticesList); } //combine scan lines together for (int y = 1; y < yn; y++) { var x = 0; while (x < xn) { var 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. var 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; } var ax = x * cell_width + domain.LowerBound.X; var ay = y * cell_height + domain.LowerBound.Y; var bp = p.p.points; var ap = u.p.points; //skip if it's already been combined with above polygon if (u.p == p.p) { x++; continue; } //combine above (but disallow the hole thingies var bi = bp.begin(); while (square(bi.elem().Y - ay) > float.Epsilon || bi.elem().X < ax) { bi = bi.next(); } Vector2 b0 = bi.elem(); var b1 = bi.next().elem(); if (square(b1.Y - ay) > float.Epsilon) { x++; continue; } var brk = true; var ai = ap.begin(); while (ai != ap.end()) { if (vec_dsq(ai.elem(), b1) < float.Epsilon) { brk = false; break; } ai = ai.next(); } if (brk) { x++; continue; } var bj = bi.next().next(); if (bj == bp.end()) { bj = bp.begin(); } while (bj != bi) { ai = ap.insert(ai, bj.elem()); // .clone() bj = bj.next(); if (bj == bp.end()) { bj = bp.begin(); } u.p.length++; } //u.p.simplify(float.Epsilon,float.Epsilon); // ax = x + 1; while (ax < xn) { var p2 = ps[(int)ax, y]; if (p2 == null || p2.p != p.p) { ax++; continue; } p2.p = u.p; ax++; } ax = x - 1; while (ax >= 0) { var p2 = ps[(int)ax, y]; if (p2 == null || p2.p != p.p) { ax--; continue; } p2.p = u.p; ax--; } ret.remove(p.p); p.p = u.p; x = (int)((bi.next().elem().X - domain.LowerBound.X) / cell_width) + 1; //x++; this was already commented out! } } polyList = ret.GetListOfElements(); foreach (var poly in polyList) { verticesList.Add(new Vertices(poly.points.GetListOfElements())); } return(verticesList); }