123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using System.Windows.Forms;
- namespace PaintDotNet.Annotation.ImageCollect
- {
- using static PaintDotNet.Base.SettingModel.LabelStyleModel;
- /// <summary>
- /// 图像拼接->多边形
- /// </summary>
- public class DrawStitchingPolygon : DrawStithchingBase
- {
- /// <summary>
- /// 鼠标形状
- /// </summary>
- private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
- /// <summary>
- /// 点集合
- /// </summary>
- private List<PointF> pointArray = new List<PointF>();
- /// <summary>
- /// 标注样式信息model
- /// </summary>
- public PolygonPolygon polygonStyle;
- private RectangleF rectangleF;
- public DrawStitchingPolygon() : base()
- {
- this.objectType = DrawClass.Stitch;
- this.drawToolType = DrawToolType.DrawStitchingPolygon;
- Initialize();
- }
- public DrawStitchingPolygon(ISurfaceBox surfaceBox, int x1, int y1) : base()
- {
- this.objectType = DrawClass.Stitch;
- this.drawToolType = DrawToolType.DrawStitchingPolygon;
- polygonStyle = surfaceBox.GetLabelStyleModel().polygonPolygon;
- startPoint.X = x1;
- startPoint.Y = y1;
- pointArray.Add(new PointF(x1, y1));
- Initialize();
- }
- public DrawStitchingPolygon(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
- {
- this.objectType = DrawClass.Stitch;
- this.drawToolType = DrawToolType.DrawStitchingPolygon;
- this.ISurfaceBox = surfaceBox;
- polygonStyle = (PolygonPolygon)parentStyleModel;
- pointArray = points;
- startPoint = points[points.Count - 2];
- endPoint = points[points.Count - 1];
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone()
- {
- DrawStitchingPolygon drawPolygon = new DrawStitchingPolygon();
- drawPolygon.objectType = DrawClass.Stitch;
- drawPolygon.drawToolType = DrawToolType.DrawStitchingPolygon;
- drawPolygon.ISurfaceBox = this.ISurfaceBox;
- foreach (PointF p in this.pointArray)
- {
- drawPolygon.pointArray.Add(p);
- }
- drawPolygon.polygonStyle = this.polygonStyle;
- drawPolygon.startPoint = this.startPoint;
- drawPolygon.endPoint = this.endPoint;
- FillDrawObjectFields(drawPolygon);
- return drawPolygon;
- }
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- DrawStitchingPolygon drawPolygon = new DrawStitchingPolygon();
- drawPolygon.objectType = DrawClass.Stitch;
- drawPolygon.drawToolType = DrawToolType.DrawStitchingPolygon;
- drawPolygon.ISurfaceBox = surfaceBox;
- foreach (PointF p in this.pointArray)
- {
- drawPolygon.pointArray.Add(p);
- }
- drawPolygon.startPoint = this.startPoint;
- drawPolygon.endPoint = this.endPoint;
- drawPolygon.polygonStyle = this.polygonStyle;
- FillDrawObjectFields(drawPolygon);
- return drawPolygon;
- }
- public override void Draw(Graphics g)
- {
- if (HandleCount >= 3)
- {
- g.SmoothingMode = SmoothingMode.AntiAlias;
- //Color color = Color.FromArgb(this.polygonStyle.lineColor);
- Color color = Color.Gray;
- Pen pen = new Pen(color, PenWidth);
- int penWidth = 1; //this.polygonStyle.lineWidth;
- pen.DashStyle = DashStyle.Solid; // (DashStyle)this.polygonStyle.lineStyle;
- pen.Width = penWidth;
- g.DrawPolygon(pen, pointArray.ToArray());
- pen.Color = Color.FromArgb(this.polygonStyle.lineColor);
- float halfW = rectangleF.Width / 2;
- float halfH = rectangleF.Height / 2;
- int xNum = CalGridNum(rectangleF.Width, ViewWidth);
- int yNum = CalGridNum(rectangleF.Height, ViewHeight);
- if (xNum != tiles[0] || yNum != tiles[1])
- {
- tiles[0] = xNum;
- tiles[1] = yNum;
- this.deletedPointId.Clear();
- this.zscan.Clear();
- for (int j = 0; j < tiles[1]; j++)
- {
- for (int i = 0; i < tiles[0]; i++)
- {
- zscan.Add(new ZScanParameter());
- }
- }
- }
- PointF centerPoint = new PointF();
- centerPoint.X = rectangleF.X + halfW;
- centerPoint.Y = rectangleF.Y + halfH;
- PointF startPoint = new PointF();
- if (tiles[0] % 2 == 0)
- {
- startPoint.X = (float)centerPoint.X - ViewWidth * (tiles[0] / 2 - Interval / 2 - (tiles[0] / 2 - 1) * Interval);
- }
- else
- {
- int n = (tiles[0] - 1) / 2;
- startPoint.X = (float)centerPoint.X - ViewWidth / 2 - n * ViewWidth * (1 - Interval);
- }
- if (tiles[1] % 2 == 0)
- {
- startPoint.Y = (float)centerPoint.Y - ViewHeight * (tiles[1] / 2 - Interval / 2 - (tiles[1] / 2 - 1) * Interval);
- }
- else
- {
- int n = (tiles[1] - 1) / 2;
- startPoint.Y = (float)centerPoint.Y - ViewHeight / 2 - n * ViewHeight * (1 - Interval);
- }
- rectangleMax = new RectangleF(startPoint, new SizeF(ViewWidth * (tiles[0] - (tiles[0] - 1) * Interval), ViewHeight * (tiles[1] - (tiles[1] - 1) * Interval)));
- _points.Clear();
- int index = 0;
- List<PointF> tmpList;
- for (int j = 0; j < tiles[1]; j++)
- {
- tmpList = new List<PointF>();
- for (int i = 0; i < tiles[0]; i++)
- {
- float x = startPoint.X + i * (ViewWidth * (1 - Interval));
- float y = startPoint.Y + j * (ViewHeight * (1 - Interval));
- tmpList.Add(new PointF(x, y));
- }
- if (j % 2 == 1)
- {
- for (int m = tmpList.Count - 1; m >= 0; m--)
- {
- Dictionary<int, object> myDictionary = new Dictionary<int, object>();
- myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
- if (!deletedPointId.Contains(index))
- {
- g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);
- myDictionary.Add(1, 0);
- }
- else
- {
- myDictionary.Add(1, 1);
- }
- _points.Add(myDictionary);
- index++;
- }
- }
- else
- {
- for (int m = 0; m < tmpList.Count; m++)
- {
- Dictionary<int, object> myDictionary = new Dictionary<int, object>();
- myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
- if (!deletedPointId.Contains(index))
- {
- g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);
- myDictionary.Add(1, 0);
- }
- else
- {
- myDictionary.Add(1, 1);
- }
- _points.Add(myDictionary);
- index++;
- }
- }
- }
- pen.Dispose();
- }
- }
- private int CalGridNum(float width, int side)
- {
- for (int i = 0; i <= 10000; i++)
- {
- if ((i - (i + 1) * Interval) * side > width)
- {
- return i;
- }
- }
- return 0;
- }
- public void AddPoint(PointF point)
- {
- pointArray.Add(point);
- }
- public override Rectangle GetHandleRectangle(int handleNumber)
- {
- if (lockType != LockType.NULL)
- {
- return new Rectangle();
- }
- PointF point = GetHandle(handleNumber);
- return new Rectangle((int)(point.X - 100), (int)(point.Y - 100), 201, 201);
- }
- public override int HandleCount
- {
- get
- {
- return pointArray.Count;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public override PointF GetHandle(int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- return pointArray[handleNumber - 1];
- }
- public override Cursor GetHandleCursor(int handleNumber)
- {
- return handleCursor;
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- pointArray[handleNumber - 1] = point;
- }
- public override void Move(int deltaX, int deltaY)
- {
- int n = pointArray.Count;
- PointF point;
- for (int i = 0; i < n; i++)
- {
- point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
- pointArray[i] = point;
- }
- }
- /// <summary>
- /// Draw tracker for selected object
- /// </summary>
- /// <param name="g"></param>
- public override void DrawTracker(Graphics g)
- {
- if (!Selected)
- return;
- SolidBrush brush = new SolidBrush(Color.Black);
- for (int i = 1; i <= HandleCount; i++)
- {
- g.FillRectangle(brush, GetHandleRectangle(i));
- }
- brush.Dispose();
- rectangleF = GetBoundingBox();
- g.DrawRectangle(new Pen(Color.White), rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
- }
- /// <summary>
- /// Hit test.
- /// Return value: -1 - no hit
- /// 0 - hit anywhere
- /// > 1 - handle number
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- public override int HitTest(Point point)
- {
- if (Selected)
- {
- for (int i = 1; i <= HandleCount; i++)
- {
- if (GetHandleRectangle(i).Contains(point))
- return i;
- }
- }
- if (PointInObject(point))
- return 0;
- return -1;
- }
- protected override bool PointInObject(Point point)
- {
- int counter = 0;
- int i;
- double xinters;
- PointF p1, p2;
- int pointCount = pointArray.Count;
- p1 = pointArray[0];
- for (i = 1; i <= pointCount; i++)
- {
- p2 = pointArray[i % pointCount];
- if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y
- && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y
- {
- if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
- {
- if (p1.Y != p2.Y)//线段不平行于X轴
- {
- xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
- if (p1.X == p2.X || point.X <= xinters)
- {
- counter++;
- }
- }
- }
- }
- p1 = p2;
- }
- if (counter % 2 == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public override bool IntersectsWith(Rectangle rectangle)
- {
- return rectangleF.IntersectsWith(rectangle);
- }
- public override RectangleF GetBoundingBox()
- {
- RectangleF rectangle;
- float minx = 0, maxx = 0, miny = 0, maxy = 0;
- for (int i = 0; i < pointArray.Count; i++)
- {
- if (i == 0)
- {
- minx = maxx = pointArray[i].X;
- miny = maxy = pointArray[i].Y;
- }
- else
- {
- if (pointArray[i].X > maxx) maxx = pointArray[i].X;
- if (pointArray[i].X < minx) minx = pointArray[i].X;
- if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
- if (pointArray[i].Y < miny) miny = pointArray[i].Y;
- }
- }
- rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
- return rectangle;
- }
- internal void setNextPoint(PointF p)
- {
- AddPoint(p);
- startPoint = endPoint;
- endPoint = p;
- }
- internal void setEndPoint(PointF p)
- {
- endPoint = p;
- }
- public override List<PointF> GetPoints()
- {
- return pointArray;
- }
- public override ParentStyleModel GetStyle()
- {
- return polygonStyle;
- }
- }
- }
|