DrawStitchingPolygon.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Globalization;
  8. using System.Windows.Forms;
  9. namespace PaintDotNet.Annotation.ImageCollect
  10. {
  11. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. /// <summary>
  13. /// 图像拼接->多边形
  14. /// </summary>
  15. public class DrawStitchingPolygon : DrawStithchingBase
  16. {
  17. /// <summary>
  18. /// 鼠标形状
  19. /// </summary>
  20. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  21. /// <summary>
  22. /// 点集合
  23. /// </summary>
  24. private List<PointF> pointArray = new List<PointF>();
  25. /// <summary>
  26. /// 标注样式信息model
  27. /// </summary>
  28. public PolygonPolygon polygonStyle;
  29. private RectangleF rectangleF;
  30. public DrawStitchingPolygon() : base()
  31. {
  32. this.objectType = DrawClass.Stitch;
  33. this.drawToolType = DrawToolType.DrawStitchingPolygon;
  34. Initialize();
  35. }
  36. public DrawStitchingPolygon(ISurfaceBox surfaceBox, int x1, int y1) : base()
  37. {
  38. this.objectType = DrawClass.Stitch;
  39. this.drawToolType = DrawToolType.DrawStitchingPolygon;
  40. polygonStyle = surfaceBox.GetLabelStyleModel().polygonPolygon;
  41. startPoint.X = x1;
  42. startPoint.Y = y1;
  43. pointArray.Add(new PointF(x1, y1));
  44. Initialize();
  45. }
  46. public DrawStitchingPolygon(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  47. {
  48. this.objectType = DrawClass.Stitch;
  49. this.drawToolType = DrawToolType.DrawStitchingPolygon;
  50. this.ISurfaceBox = surfaceBox;
  51. polygonStyle = (PolygonPolygon)parentStyleModel;
  52. pointArray = points;
  53. startPoint = points[points.Count - 2];
  54. endPoint = points[points.Count - 1];
  55. }
  56. /// <summary>
  57. /// Clone this instance
  58. /// </summary>
  59. public override DrawObject Clone()
  60. {
  61. DrawStitchingPolygon drawPolygon = new DrawStitchingPolygon();
  62. drawPolygon.objectType = DrawClass.Stitch;
  63. drawPolygon.drawToolType = DrawToolType.DrawStitchingPolygon;
  64. drawPolygon.ISurfaceBox = this.ISurfaceBox;
  65. foreach (PointF p in this.pointArray)
  66. {
  67. drawPolygon.pointArray.Add(p);
  68. }
  69. drawPolygon.polygonStyle = this.polygonStyle;
  70. drawPolygon.startPoint = this.startPoint;
  71. drawPolygon.endPoint = this.endPoint;
  72. FillDrawObjectFields(drawPolygon);
  73. return drawPolygon;
  74. }
  75. public override DrawObject Clone(ISurfaceBox surfaceBox)
  76. {
  77. DrawStitchingPolygon drawPolygon = new DrawStitchingPolygon();
  78. drawPolygon.objectType = DrawClass.Stitch;
  79. drawPolygon.drawToolType = DrawToolType.DrawStitchingPolygon;
  80. drawPolygon.ISurfaceBox = surfaceBox;
  81. foreach (PointF p in this.pointArray)
  82. {
  83. drawPolygon.pointArray.Add(p);
  84. }
  85. drawPolygon.startPoint = this.startPoint;
  86. drawPolygon.endPoint = this.endPoint;
  87. drawPolygon.polygonStyle = this.polygonStyle;
  88. FillDrawObjectFields(drawPolygon);
  89. return drawPolygon;
  90. }
  91. public override void Draw(Graphics g)
  92. {
  93. if (HandleCount >= 3)
  94. {
  95. g.SmoothingMode = SmoothingMode.AntiAlias;
  96. //Color color = Color.FromArgb(this.polygonStyle.lineColor);
  97. Color color = Color.Gray;
  98. Pen pen = new Pen(color, PenWidth);
  99. int penWidth = 1; //this.polygonStyle.lineWidth;
  100. pen.DashStyle = DashStyle.Solid; // (DashStyle)this.polygonStyle.lineStyle;
  101. pen.Width = penWidth;
  102. g.DrawPolygon(pen, pointArray.ToArray());
  103. pen.Color = Color.FromArgb(this.polygonStyle.lineColor);
  104. float halfW = rectangleF.Width / 2;
  105. float halfH = rectangleF.Height / 2;
  106. int xNum = CalGridNum(rectangleF.Width, ViewWidth);
  107. int yNum = CalGridNum(rectangleF.Height, ViewHeight);
  108. if (xNum != tiles[0] || yNum != tiles[1])
  109. {
  110. tiles[0] = xNum;
  111. tiles[1] = yNum;
  112. this.deletedPointId.Clear();
  113. this.zscan.Clear();
  114. for (int j = 0; j < tiles[1]; j++)
  115. {
  116. for (int i = 0; i < tiles[0]; i++)
  117. {
  118. zscan.Add(new ZScanParameter());
  119. }
  120. }
  121. }
  122. PointF centerPoint = new PointF();
  123. centerPoint.X = rectangleF.X + halfW;
  124. centerPoint.Y = rectangleF.Y + halfH;
  125. PointF startPoint = new PointF();
  126. if (tiles[0] % 2 == 0)
  127. {
  128. startPoint.X = (float)centerPoint.X - ViewWidth * (tiles[0] / 2 - Interval / 2 - (tiles[0] / 2 - 1) * Interval);
  129. }
  130. else
  131. {
  132. int n = (tiles[0] - 1) / 2;
  133. startPoint.X = (float)centerPoint.X - ViewWidth / 2 - n * ViewWidth * (1 - Interval);
  134. }
  135. if (tiles[1] % 2 == 0)
  136. {
  137. startPoint.Y = (float)centerPoint.Y - ViewHeight * (tiles[1] / 2 - Interval / 2 - (tiles[1] / 2 - 1) * Interval);
  138. }
  139. else
  140. {
  141. int n = (tiles[1] - 1) / 2;
  142. startPoint.Y = (float)centerPoint.Y - ViewHeight / 2 - n * ViewHeight * (1 - Interval);
  143. }
  144. rectangleMax = new RectangleF(startPoint, new SizeF(ViewWidth * (tiles[0] - (tiles[0] - 1) * Interval), ViewHeight * (tiles[1] - (tiles[1] - 1) * Interval)));
  145. _points.Clear();
  146. int index = 0;
  147. List<PointF> tmpList;
  148. for (int j = 0; j < tiles[1]; j++)
  149. {
  150. tmpList = new List<PointF>();
  151. for (int i = 0; i < tiles[0]; i++)
  152. {
  153. float x = startPoint.X + i * (ViewWidth * (1 - Interval));
  154. float y = startPoint.Y + j * (ViewHeight * (1 - Interval));
  155. tmpList.Add(new PointF(x, y));
  156. }
  157. if (j % 2 == 1)
  158. {
  159. for (int m = tmpList.Count - 1; m >= 0; m--)
  160. {
  161. Dictionary<int, object> myDictionary = new Dictionary<int, object>();
  162. myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
  163. if (!deletedPointId.Contains(index))
  164. {
  165. g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);
  166. myDictionary.Add(1, 0);
  167. }
  168. else
  169. {
  170. myDictionary.Add(1, 1);
  171. }
  172. _points.Add(myDictionary);
  173. index++;
  174. }
  175. }
  176. else
  177. {
  178. for (int m = 0; m < tmpList.Count; m++)
  179. {
  180. Dictionary<int, object> myDictionary = new Dictionary<int, object>();
  181. myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
  182. if (!deletedPointId.Contains(index))
  183. {
  184. g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);
  185. myDictionary.Add(1, 0);
  186. }
  187. else
  188. {
  189. myDictionary.Add(1, 1);
  190. }
  191. _points.Add(myDictionary);
  192. index++;
  193. }
  194. }
  195. }
  196. pen.Dispose();
  197. }
  198. }
  199. private int CalGridNum(float width, int side)
  200. {
  201. for (int i = 0; i <= 10000; i++)
  202. {
  203. if ((i - (i + 1) * Interval) * side > width)
  204. {
  205. return i;
  206. }
  207. }
  208. return 0;
  209. }
  210. public void AddPoint(PointF point)
  211. {
  212. pointArray.Add(point);
  213. }
  214. public override Rectangle GetHandleRectangle(int handleNumber)
  215. {
  216. if (lockType != LockType.NULL)
  217. {
  218. return new Rectangle();
  219. }
  220. PointF point = GetHandle(handleNumber);
  221. return new Rectangle((int)(point.X - 100), (int)(point.Y - 100), 201, 201);
  222. }
  223. public override int HandleCount
  224. {
  225. get
  226. {
  227. return pointArray.Count;
  228. }
  229. }
  230. /// <summary>
  231. /// Get handle pointscroll by 1-based number
  232. /// </summary>
  233. /// <param name="handleNumber"></param>
  234. /// <returns></returns>
  235. public override PointF GetHandle(int handleNumber)
  236. {
  237. if (handleNumber < 1)
  238. handleNumber = 1;
  239. if (handleNumber > pointArray.Count)
  240. handleNumber = pointArray.Count;
  241. return pointArray[handleNumber - 1];
  242. }
  243. public override Cursor GetHandleCursor(int handleNumber)
  244. {
  245. return handleCursor;
  246. }
  247. public override void MoveHandleTo(Point point, int handleNumber)
  248. {
  249. if (handleNumber < 1)
  250. handleNumber = 1;
  251. if (handleNumber > pointArray.Count)
  252. handleNumber = pointArray.Count;
  253. pointArray[handleNumber - 1] = point;
  254. }
  255. public override void Move(int deltaX, int deltaY)
  256. {
  257. int n = pointArray.Count;
  258. PointF point;
  259. for (int i = 0; i < n; i++)
  260. {
  261. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  262. pointArray[i] = point;
  263. }
  264. }
  265. /// <summary>
  266. /// Draw tracker for selected object
  267. /// </summary>
  268. /// <param name="g"></param>
  269. public override void DrawTracker(Graphics g)
  270. {
  271. if (!Selected)
  272. return;
  273. SolidBrush brush = new SolidBrush(Color.Black);
  274. for (int i = 1; i <= HandleCount; i++)
  275. {
  276. g.FillRectangle(brush, GetHandleRectangle(i));
  277. }
  278. brush.Dispose();
  279. rectangleF = GetBoundingBox();
  280. g.DrawRectangle(new Pen(Color.White), rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
  281. }
  282. /// <summary>
  283. /// Hit test.
  284. /// Return value: -1 - no hit
  285. /// 0 - hit anywhere
  286. /// > 1 - handle number
  287. /// </summary>
  288. /// <param name="pointscroll"></param>
  289. /// <returns></returns>
  290. public override int HitTest(Point point)
  291. {
  292. if (Selected)
  293. {
  294. for (int i = 1; i <= HandleCount; i++)
  295. {
  296. if (GetHandleRectangle(i).Contains(point))
  297. return i;
  298. }
  299. }
  300. if (PointInObject(point))
  301. return 0;
  302. return -1;
  303. }
  304. protected override bool PointInObject(Point point)
  305. {
  306. int counter = 0;
  307. int i;
  308. double xinters;
  309. PointF p1, p2;
  310. int pointCount = pointArray.Count;
  311. p1 = pointArray[0];
  312. for (i = 1; i <= pointCount; i++)
  313. {
  314. p2 = pointArray[i % pointCount];
  315. if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y
  316. && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y
  317. {
  318. if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
  319. {
  320. if (p1.Y != p2.Y)//线段不平行于X轴
  321. {
  322. xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
  323. if (p1.X == p2.X || point.X <= xinters)
  324. {
  325. counter++;
  326. }
  327. }
  328. }
  329. }
  330. p1 = p2;
  331. }
  332. if (counter % 2 == 0)
  333. {
  334. return false;
  335. }
  336. else
  337. {
  338. return true;
  339. }
  340. }
  341. public override bool IntersectsWith(Rectangle rectangle)
  342. {
  343. return rectangleF.IntersectsWith(rectangle);
  344. }
  345. public override RectangleF GetBoundingBox()
  346. {
  347. RectangleF rectangle;
  348. float minx = 0, maxx = 0, miny = 0, maxy = 0;
  349. for (int i = 0; i < pointArray.Count; i++)
  350. {
  351. if (i == 0)
  352. {
  353. minx = maxx = pointArray[i].X;
  354. miny = maxy = pointArray[i].Y;
  355. }
  356. else
  357. {
  358. if (pointArray[i].X > maxx) maxx = pointArray[i].X;
  359. if (pointArray[i].X < minx) minx = pointArray[i].X;
  360. if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
  361. if (pointArray[i].Y < miny) miny = pointArray[i].Y;
  362. }
  363. }
  364. rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
  365. return rectangle;
  366. }
  367. internal void setNextPoint(PointF p)
  368. {
  369. AddPoint(p);
  370. startPoint = endPoint;
  371. endPoint = p;
  372. }
  373. internal void setEndPoint(PointF p)
  374. {
  375. endPoint = p;
  376. }
  377. public override List<PointF> GetPoints()
  378. {
  379. return pointArray;
  380. }
  381. public override ParentStyleModel GetStyle()
  382. {
  383. return polygonStyle;
  384. }
  385. }
  386. }