protected virtual void ExcuteFloodFill(BitMap2d data, Int16Double seed) { this.bmp = data; data.ResetVisitCount(); flagsMap = new FlagMap2d(data.width, data.height); Int16Double[] adjPoints4 = new Int16Double[4]; flagsMap.SetFlagOn(seed.X, seed.Y, true); container.Push(seed); Process(seed); while (!container.Empty()) { Int16Double p = container.Pop(); InitAdj4(ref adjPoints4, ref p); for (int adjIndex = 0; adjIndex < 4; adjIndex++) { Int16Double t = adjPoints4[adjIndex]; if (t.X < data.width && t.X >= 0 && t.Y < data.height && t.Y >= 0) { if (!flagsMap.GetFlagOn(t.X, t.Y) && IncludePredicate(t)) { flagsMap.SetFlagOn(t.X, t.Y, true); container.Push(t); Process(t); } } } } return; }
private void CheckRange(int yleft, int yright, int x, ParentDirectionsY ptype) { for (int i = yleft; i <= yright;) { if ((!flagsMap.GetFlagOn(x, i)) && IncludePredicate(x, i)) { int lb = i; int rb = i + 1; while (rb <= yright && (!flagsMap.GetFlagOn(x, rb)) && IncludePredicate(x, rb)) { rb++; } rb--; SpanY span = new SpanY(); span.YLeft = lb; span.YRight = rb; span.X = x; if (lb == yleft && rb == yright) { span.Extended = ExtendTypesY.UnRez; } else if (rb == yright) { span.Extended = ExtendTypesY.RightRequired; } else if (lb == yleft) { span.Extended = ExtendTypesY.LeftRequired; } else { span.Extended = ExtendTypesY.AllRez; } span.ParentDirection = ptype; for (int j = lb; j <= rb; j++) { flagsMap.SetFlagOn(x, j, true); Process(new Int16Double(x, j)); } queue.Push(span); i = rb + 1; } else { i++; } } }
protected void CheckRange(int xleft, int xright, int y, ParentDirections ptype) { for (int i = xleft; i <= xright;) { if ((!flagsMap.GetFlagOn(i, y)) && IncludePredicate(i, y)) { int lb = i; int rb = i + 1; while (rb <= xright && (!flagsMap.GetFlagOn(rb, y)) && IncludePredicate(rb, y)) { rb++; } rb--; Span span = new Span(); span.XLeft = lb; span.XRight = rb; span.Y = y; if (lb == xleft && rb == xright) { span.Extended = ExtendTypes.UnRez; } else if (rb == xright) { span.Extended = ExtendTypes.RightRequired; } else if (lb == xleft) { span.Extended = ExtendTypes.LeftRequired; } else { span.Extended = ExtendTypes.AllRez; } span.ParentDirection = ptype; for (int j = lb; j <= rb; j++) { flagsMap.SetFlagOn(j, y, true); Process(new Int16Double(j, y)); } container.Push(span); i = rb + 1; } else { i++; } } }//区段法的CheckRange 注意与扫描线的CheckRange的不同
}//该函数为扫描线法主体 protected void CheckRange(int xleft, int xright, int y) { for (int i = xleft; i <= xright;) { if ((!flagsMap.GetFlagOn(i, y)) && IncludePredicate(i, y)) { int rb = i + 1; while (rb <= xright && (!flagsMap.GetFlagOn(rb, y)) && IncludePredicate(rb, y)) { rb++; } rb--; Int16Double t = new Int16Double(rb, y); flagsMap.SetFlagOn(rb, y, true); container.Push(t); Process(t); i = rb + 1; } else { i++; } } }//CheckRange操作
protected virtual void ExcuteScanlineFill(BitMap2d data, Int16Double seed) { this.bmp = data; data.ResetVisitCount(); flagsMap = new FlagMap2d(data.width, data.height); flagsMap.SetFlagOn(seed.X, seed.Y, true); container.Push(seed); Process(seed); while (!container.Empty()) { Int16Double p = container.Pop(); int xleft = FindXLeft(p); int xright = FindXRight(p); if (p.Y - 1 >= 0) { CheckRange(xleft, xright, p.Y - 1); } if (p.Y + 1 < data.height) { CheckRange(xleft, xright, p.Y + 1); } } }//该函数为扫描线法主体
public virtual void ExcuteSpanFill_S(BitMap2d data, Int16Double seed) { this.bmp = data; data.ResetVisitCount(); flagsMap = new FlagMap2d(data.width, data.height); queue = new Container_Stack <SpanY>(); Process(seed); flagsMap.SetFlagOn(seed.X, seed.Y, true); SpanY seedspan = new SpanY(); seedspan.YLeft = seed.Y; seedspan.YRight = seed.Y; seedspan.X = seed.X; seedspan.ParentDirection = ParentDirectionsY.Non; seedspan.Extended = ExtendTypesY.UnRez; queue.Push(seedspan); while (!queue.Empty()) { SpanY span = queue.Pop(); #region AllRez if (span.Extended == ExtendTypesY.AllRez) { if (span.ParentDirection == ParentDirectionsY.X2) { if (span.X - 1 >= 0) { CheckRange(span.YLeft, span.YRight, span.X - 1, ParentDirectionsY.X2); } continue; } if (span.ParentDirection == ParentDirectionsY.X0) { if (span.X + 1 < bmp.width) { CheckRange(span.YLeft, span.YRight, span.X + 1, ParentDirectionsY.X0); } continue; } throw new Exception(); } #endregion #region UnRez if (span.Extended == ExtendTypesY.UnRez) { int yl = FindYLeft(span.YLeft, span.X); int yr = FindYRight(span.YRight, span.X); if (span.ParentDirection == ParentDirectionsY.X2) { if (span.X - 1 >= 0) { CheckRange(yl, yr, span.X - 1, ParentDirectionsY.X2); } if (span.X + 1 < bmp.width) { if (yl != span.YLeft) { CheckRange(yl, span.YLeft, span.X + 1, ParentDirectionsY.X0); } if (span.YRight != yr) { CheckRange(span.YRight, yr, span.X + 1, ParentDirectionsY.X0); } } continue; } if (span.ParentDirection == ParentDirectionsY.X0) { if (span.X + 1 < bmp.width) { CheckRange(yl, yr, span.X + 1, ParentDirectionsY.X0); } if (span.X - 1 >= 0) { if (yl != span.YLeft) { CheckRange(yl, span.YLeft, span.X - 1, ParentDirectionsY.X2); } if (span.YRight != yr) { CheckRange(span.YRight, yr, span.X - 1, ParentDirectionsY.X2); } } continue; } if (span.ParentDirection == ParentDirectionsY.Non) { if (span.X + 1 < bmp.width) { CheckRange(yl, yr, span.X + 1, ParentDirectionsY.X0); } if (span.X - 1 >= 0) { CheckRange(yl, yr, span.X - 1, ParentDirectionsY.X2); } continue; } throw new Exception(); } #endregion #region LeftRequired if (span.Extended == ExtendTypesY.LeftRequired) { int yl = FindYLeft(span.YLeft, span.X); if (span.ParentDirection == ParentDirectionsY.X2) { if (span.X - 1 >= 0) { CheckRange(yl, span.YRight, span.X - 1, ParentDirectionsY.X2); } if (span.X + 1 < bmp.width && yl != span.YLeft) { CheckRange(yl, span.YLeft, span.X + 1, ParentDirectionsY.X0); } continue; } if (span.ParentDirection == ParentDirectionsY.X0) { if (span.X + 1 < bmp.width) { CheckRange(yl, span.YRight, span.X + 1, ParentDirectionsY.X0); } if (span.X - 1 >= 0 && yl != span.YLeft) { CheckRange(yl, span.YLeft, span.X - 1, ParentDirectionsY.X2); } continue; } throw new Exception(); } #endregion #region RightRequired if (span.Extended == ExtendTypesY.RightRequired) { int yr = FindYRight(span.YRight, span.X); if (span.ParentDirection == ParentDirectionsY.X2) { if (span.X - 1 >= 0) { CheckRange(span.YLeft, yr, span.X - 1, ParentDirectionsY.X2); } if (span.X + 1 < bmp.width && span.YRight != yr) { CheckRange(span.YRight, yr, span.X + 1, ParentDirectionsY.X0); } continue; } if (span.ParentDirection == ParentDirectionsY.X0) { if (span.X + 1 < bmp.width) { CheckRange(span.YLeft, yr, span.X + 1, ParentDirectionsY.X0); } if (span.X - 1 >= 0 && span.YRight != yr) { CheckRange(span.YRight, yr, span.X - 1, ParentDirectionsY.X2); } continue; } throw new Exception(); } #endregion } }
protected Container <Span> container;//以Span为单位的Queue或Stack容器 protected virtual void ExcuteSpanFill(BitMap2d data, Int16Double seed) { this.bmp = data; data.ResetVisitCount(); flagsMap = new FlagMap2d(data.width, data.height); Process(seed); flagsMap.SetFlagOn(seed.X, seed.Y, true); Span seedspan = new Span(); seedspan.XLeft = seed.X; seedspan.XRight = seed.X; seedspan.Y = seed.Y; seedspan.ParentDirection = ParentDirections.Non; seedspan.Extended = ExtendTypes.UnRez; container.Push(seedspan); while (!container.Empty()) { Span span = container.Pop(); #region AllRez if (span.Extended == ExtendTypes.AllRez) { if (span.ParentDirection == ParentDirections.Y2) { if (span.Y - 1 >= 0) { CheckRange(span.XLeft, span.XRight, span.Y - 1, ParentDirections.Y2); } continue; } if (span.ParentDirection == ParentDirections.Y0) { if (span.Y + 1 < bmp.height) { CheckRange(span.XLeft, span.XRight, span.Y + 1, ParentDirections.Y0); } continue; } throw new Exception(); } #endregion #region UnRez if (span.Extended == ExtendTypes.UnRez) { int xl = FindXLeft(span.XLeft, span.Y); int xr = FindXRight(span.XRight, span.Y); if (span.ParentDirection == ParentDirections.Y2) { if (span.Y - 1 >= 0) { CheckRange(xl, xr, span.Y - 1, ParentDirections.Y2); } if (span.Y + 1 < bmp.height) { if (xl != span.XLeft) { CheckRange(xl, span.XLeft, span.Y + 1, ParentDirections.Y0); } if (span.XRight != xr) { CheckRange(span.XRight, xr, span.Y + 1, ParentDirections.Y0); } } continue; } if (span.ParentDirection == ParentDirections.Y0) { if (span.Y + 1 < bmp.height) { CheckRange(xl, xr, span.Y + 1, ParentDirections.Y0); } if (span.Y - 1 >= 0) { if (xl != span.XLeft) { CheckRange(xl, span.XLeft, span.Y - 1, ParentDirections.Y2); } if (span.XRight != xr) { CheckRange(span.XRight, xr, span.Y - 1, ParentDirections.Y2); } } continue; } if (span.ParentDirection == ParentDirections.Non) { if (span.Y + 1 < bmp.height) { CheckRange(xl, xr, span.Y + 1, ParentDirections.Y0); } if (span.Y - 1 >= 0) { CheckRange(xl, xr, span.Y - 1, ParentDirections.Y2); } continue; } throw new Exception(); } #endregion #region LeftRequired if (span.Extended == ExtendTypes.LeftRequired) { int xl = FindXLeft(span.XLeft, span.Y); if (span.ParentDirection == ParentDirections.Y2) { if (span.Y - 1 >= 0) { CheckRange(xl, span.XRight, span.Y - 1, ParentDirections.Y2); } if (span.Y + 1 < bmp.height && xl != span.XLeft) { CheckRange(xl, span.XLeft, span.Y + 1, ParentDirections.Y0); } continue; } if (span.ParentDirection == ParentDirections.Y0) { if (span.Y + 1 < bmp.height) { CheckRange(xl, span.XRight, span.Y + 1, ParentDirections.Y0); } if (span.Y - 1 >= 0 && xl != span.XLeft) { CheckRange(xl, span.XLeft, span.Y - 1, ParentDirections.Y2); } continue; } throw new Exception(); } #endregion #region RightRequired if (span.Extended == ExtendTypes.RightRequired) { int xr = FindXRight(span.XRight, span.Y); if (span.ParentDirection == ParentDirections.Y2) { if (span.Y - 1 >= 0) { CheckRange(span.XLeft, xr, span.Y - 1, ParentDirections.Y2); } if (span.Y + 1 < bmp.height && span.XRight != xr) { CheckRange(span.XRight, xr, span.Y + 1, ParentDirections.Y0); } continue; } if (span.ParentDirection == ParentDirections.Y0) { if (span.Y + 1 < bmp.height) { CheckRange(span.XLeft, xr, span.Y + 1, ParentDirections.Y0); } if (span.Y - 1 >= 0 && span.XRight != xr) { CheckRange(span.XRight, xr, span.Y - 1, ParentDirections.Y2); } continue; } throw new Exception(); } #endregion } }