DrawStitchingRectangle.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using PaintDotNet.Annotation.Enum;
  10. using PaintDotNet.Base.SettingModel;
  11. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. namespace PaintDotNet.Annotation.ImageCollect
  13. {
  14. /// <summary>
  15. /// 图像拼接 - 矩形
  16. /// </summary>
  17. public class DrawStitchingRectangle : DrawStithchingBase
  18. {
  19. /// <summary>
  20. /// 标注样式信息model
  21. /// </summary>
  22. public PolygonRectangle labelRectStyleModel;
  23. public DrawStitchingRectangle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
  24. {
  25. this.deletedPointId = new List<int>();
  26. this._points = new List<Dictionary<int, object>>();
  27. this.objectType = DrawClass.Stitch;
  28. this.drawToolType = DrawToolType.DrawStitchingRectangle;
  29. this.labelRectStyleModel = surfaceBox.GetLabelStyleModel().polygonRectangle;
  30. this.ISurfaceBox = surfaceBox;
  31. this.rectangle.X = x;
  32. this.rectangle.Y = y;
  33. this.rectangle.Width = width;
  34. this.rectangle.Height = height;
  35. Initialize();
  36. }
  37. public DrawStitchingRectangle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel) : base()
  38. {
  39. this.deletedPointId = new List<int>();
  40. this._points = new List<Dictionary<int, object>>();
  41. this.objectType = DrawClass.Stitch;
  42. this.drawToolType = DrawToolType.DrawStitchingRectangle;
  43. this.ISurfaceBox = surfaceBox;
  44. labelRectStyleModel = (PolygonRectangle)parentStyleModel;
  45. rectangle.X = points[0].X;
  46. rectangle.Y = points[0].Y;
  47. rectangle.Width = points[1].X - points[0].X;
  48. rectangle.Height = points[1].Y - points[0].Y;
  49. }
  50. /// <summary>
  51. /// Clone this instance
  52. /// </summary>
  53. public override DrawObject Clone()
  54. {
  55. DrawStitchingRectangle drawRectangle = new DrawStitchingRectangle(ISurfaceBox, 0, 0, 1, 0);
  56. drawRectangle.objectType = DrawClass.Stitch;
  57. drawRectangle.drawToolType = DrawToolType.DrawStitchingRectangle;
  58. drawRectangle.ISurfaceBox = this.ISurfaceBox;
  59. drawRectangle.rectangle = this.rectangle;
  60. FillDrawObjectFields(drawRectangle);
  61. return drawRectangle;
  62. }
  63. public override DrawObject Clone(ISurfaceBox surfaceBox)
  64. {
  65. DrawStitchingRectangle drawRectangle = new DrawStitchingRectangle(surfaceBox, 0, 0, 1, 0);
  66. drawRectangle.objectType = DrawClass.Stitch;
  67. drawRectangle.drawToolType = DrawToolType.DrawStitchingRectangle;
  68. drawRectangle.ISurfaceBox = surfaceBox;
  69. drawRectangle.rectangle = this.rectangle;
  70. drawRectangle.labelRectStyleModel = this.labelRectStyleModel;
  71. FillDrawObjectFields(drawRectangle);
  72. return drawRectangle;
  73. }
  74. /// <summary>
  75. /// Draw rectangle
  76. /// </summary>
  77. /// <param name="g"></param>
  78. public override void Draw(Graphics g)
  79. {
  80. Color color = Color.Gray;
  81. Pen pen = new Pen(color, PenWidth);
  82. int penWidth = 1; //this.labelRectStyleModel.lineWidth;
  83. pen.DashStyle = DashStyle.Solid; // (DashStyle)this.labelRectStyleModel.lineStyle;
  84. pen.Width = penWidth;
  85. var rettmp = new RectangleF(rectangle.X + OffsetX, rectangle.Y + OffsetY, rectangle.Width, rectangle.Height);
  86. g.DrawRectangle(pen, DrawStitchingRectangle.GetNormalizedRectangle(rettmp));
  87. pen.Color = Color.FromArgb(this.labelRectStyleModel.lineColor);
  88. //fillBrush.Dispose();
  89. float halfW = rectangle.Width / 2;
  90. float halfH = rectangle.Height / 2;
  91. int xNum = CalGridNum(rectangle.Width, ViewWidth);
  92. int yNum = CalGridNum(rectangle.Height, ViewHeight);
  93. if (xNum != tiles[0] || yNum != tiles[1])
  94. {
  95. tiles[0] = xNum;
  96. tiles[1] = yNum;
  97. this.deletedPointId.Clear();
  98. this.zscan.Clear();
  99. for (int j = 0; j < tiles[1]; j++)
  100. {
  101. for (int i = 0; i < tiles[0]; i++)
  102. {
  103. zscan.Add(new ZScanParameter());
  104. }
  105. }
  106. }
  107. PointF centerPoint = new PointF();
  108. centerPoint.X = rectangle.X + halfW;
  109. centerPoint.Y = rectangle.Y + halfH;
  110. PointF startPoint = new PointF();
  111. if (tiles[0] % 2 == 0)
  112. {
  113. startPoint.X = (float)centerPoint.X - ViewWidth * (tiles[0] / 2 - Interval / 2 - (tiles[0] / 2 - 1) * Interval);
  114. }
  115. else
  116. {
  117. int n = (tiles[0] - 1) / 2;
  118. startPoint.X = (float)centerPoint.X - ViewWidth / 2 - n * ViewWidth * (1 - Interval);
  119. }
  120. if (tiles[1] % 2 == 0)
  121. {
  122. startPoint.Y = (float)centerPoint.Y - ViewHeight * (tiles[1] / 2 - Interval / 2 - (tiles[1] / 2 - 1) * Interval);
  123. }
  124. else
  125. {
  126. int n = (tiles[1] - 1) / 2;
  127. startPoint.Y = (float)centerPoint.Y - ViewHeight / 2 - n * ViewHeight * (1 - Interval);
  128. }
  129. rectangleMax = new RectangleF(startPoint, new SizeF(ViewWidth * (tiles[0] - (tiles[0] - 1) * Interval), ViewHeight * (tiles[1] - (tiles[1] - 1) * Interval)));
  130. _points.Clear();
  131. List<PointF> tmpList;
  132. int index = 0;
  133. for (int j = 0; j < tiles[1]; j++)
  134. {
  135. tmpList = new List<PointF>();
  136. for (int i = 0; i < tiles[0]; i++)
  137. {
  138. float x = startPoint.X + i * (ViewWidth * (1 - Interval));
  139. float y = startPoint.Y + j * (ViewHeight * (1 - Interval));
  140. tmpList.Add(new PointF(x, y));
  141. }
  142. if (j % 2 == 1)
  143. {
  144. for (int m = tmpList.Count - 1; m >= 0; m--)
  145. {
  146. Dictionary<int, object> myDictionary = new Dictionary<int, object>();
  147. myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
  148. if (!deletedPointId.Contains(index))
  149. {
  150. g.DrawRectangle(pen, tmpList[m].X + OffsetX, tmpList[m].Y + OffsetY, ViewWidth, ViewHeight);
  151. myDictionary.Add(1, 0);
  152. }
  153. else
  154. {
  155. myDictionary.Add(1, 1);
  156. }
  157. _points.Add(myDictionary);
  158. index++;
  159. }
  160. }
  161. else
  162. {
  163. for (int m = 0; m < tmpList.Count; m++)
  164. {
  165. Dictionary<int, object> myDictionary = new Dictionary<int, object>();
  166. myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));
  167. if (!deletedPointId.Contains(index))
  168. {
  169. g.DrawRectangle(pen, tmpList[m].X + OffsetX, tmpList[m].Y + OffsetY, ViewWidth, ViewHeight);
  170. myDictionary.Add(1, 0);
  171. }
  172. else
  173. {
  174. myDictionary.Add(1, 1);
  175. }
  176. _points.Add(myDictionary);
  177. index++;
  178. }
  179. }
  180. }
  181. pen.Dispose();
  182. //OnPropertyChanged();
  183. }
  184. private int CalGridNum(float width, int side)
  185. {
  186. for (int i = 0; i <= 10000; i++)
  187. {
  188. if ((i - (i + 1) * Interval) * side > width)
  189. {
  190. return i;
  191. }
  192. }
  193. return 0;
  194. }
  195. protected void SetRectangle(float x, float y, float width, float height)
  196. {
  197. this.rectangle.X = x;
  198. this.rectangle.Y = y;
  199. this.rectangle.Width = width;
  200. this.rectangle.Height = height;
  201. }
  202. /// <summary>
  203. /// Get number of handles
  204. /// </summary>
  205. public override int HandleCount
  206. {
  207. get
  208. {
  209. return 8;
  210. }
  211. }
  212. /// <summary>
  213. /// Get handle pointscroll by 1-based number
  214. /// </summary>
  215. /// <param name="handleNumber"></param>
  216. /// <returns></returns>
  217. public override PointF GetHandle(int handleNumber)
  218. {
  219. float x, y, xCenter, yCenter;
  220. xCenter = this.rectangle.X + this.rectangle.Width / 2;
  221. yCenter = this.rectangle.Y + this.rectangle.Height / 2;
  222. x = this.rectangle.X;
  223. y = this.rectangle.Y;
  224. switch (handleNumber)
  225. {
  226. case 1:
  227. x = this.rectangle.X;
  228. y = this.rectangle.Y;
  229. break;
  230. case 2:
  231. x = xCenter;
  232. y = this.rectangle.Y;
  233. break;
  234. case 3:
  235. x = this.rectangle.Right;
  236. y = this.rectangle.Y;
  237. break;
  238. case 4:
  239. x = this.rectangle.Right;
  240. y = yCenter;
  241. break;
  242. case 5:
  243. x = this.rectangle.Right;
  244. y = this.rectangle.Bottom;
  245. break;
  246. case 6:
  247. x = xCenter;
  248. y = this.rectangle.Bottom;
  249. break;
  250. case 7:
  251. x = this.rectangle.X;
  252. y = this.rectangle.Bottom;
  253. break;
  254. case 8:
  255. x = this.rectangle.X;
  256. y = yCenter;
  257. break;
  258. }
  259. return OffsetPointF(x, y);
  260. }
  261. public override Rectangle GetHandleRectangle(int handleNumber)
  262. {
  263. if (lockType == LockType.SIZE)
  264. {
  265. return new Rectangle();
  266. }
  267. else if (lockType == LockType.AREA)
  268. {
  269. if (handleNumber % 2 == 1)
  270. {
  271. return new Rectangle();
  272. }
  273. }
  274. PointF point = GetHandle(handleNumber);
  275. return new Rectangle((int)(point.X - 100), (int)(point.Y - 100), 201, 201);
  276. }
  277. /// <summary>
  278. /// Hit test.
  279. /// Return value: -1 - no hit
  280. /// 0 - hit anywhere
  281. /// > 1 - handle number
  282. /// </summary>
  283. /// <param name="pointscroll"></param>
  284. /// <returns></returns>
  285. public override int HitTest(Point point)
  286. {
  287. if (Selected)
  288. {
  289. for (int i = 1; i <= HandleCount; i++)
  290. {
  291. if (GetHandleRectangle(i).Contains(point))
  292. return i;
  293. }
  294. }
  295. if (PointInObject(point))
  296. return 0;
  297. return -1;
  298. }
  299. protected override bool PointInObject(Point point)
  300. {
  301. return rectangle.Contains(point);
  302. }
  303. /// <summary>
  304. /// Get cursor for the handle
  305. /// </summary>
  306. /// <param name="handleNumber"></param>
  307. /// <returns></returns>
  308. public override Cursor GetHandleCursor(int handleNumber)
  309. {
  310. switch (handleNumber)
  311. {
  312. case 1:
  313. return Cursors.SizeNWSE;
  314. case 2:
  315. return Cursors.SizeNS;
  316. case 3:
  317. return Cursors.SizeNESW;
  318. case 4:
  319. return Cursors.SizeWE;
  320. case 5:
  321. return Cursors.SizeNWSE;
  322. case 6:
  323. return Cursors.SizeNS;
  324. case 7:
  325. return Cursors.SizeNESW;
  326. case 8:
  327. return Cursors.SizeWE;
  328. default:
  329. return Cursors.Default;
  330. }
  331. }
  332. /// <summary>
  333. /// Move handle to new pointscroll (resizing)
  334. /// </summary>
  335. /// <param name="pointscroll"></param>
  336. /// <param name="handleNumber"></param>
  337. public override void MoveHandleTo(Point point, int handleNumber)
  338. {
  339. float left = Rectangle.Left;
  340. float top = Rectangle.Top;
  341. float right = Rectangle.Right;
  342. float bottom = Rectangle.Bottom;
  343. switch (handleNumber)
  344. {
  345. case 1:
  346. left = point.X;
  347. top = point.Y;
  348. break;
  349. case 2:
  350. top = point.Y;
  351. break;
  352. case 3:
  353. right = point.X;
  354. top = point.Y;
  355. break;
  356. case 4:
  357. right = point.X;
  358. break;
  359. case 5:
  360. right = point.X;
  361. bottom = point.Y;
  362. break;
  363. case 6:
  364. bottom = point.Y;
  365. break;
  366. case 7:
  367. left = point.X;
  368. bottom = point.Y;
  369. break;
  370. case 8:
  371. left = point.X;
  372. break;
  373. }
  374. float length = 0f;
  375. float xchange = 0f;
  376. if (lockType == LockType.AREA)
  377. {
  378. switch (handleNumber)
  379. {
  380. case 2:
  381. length = top - Rectangle.Top;
  382. xchange = (Rectangle.Width - (Rectangle.Width * Rectangle.Height) / (Rectangle.Height + length)) / 2;
  383. left = left + xchange;
  384. right = right - xchange;
  385. break;
  386. case 4:
  387. length = right - Rectangle.Right;
  388. xchange = (Rectangle.Height - (Rectangle.Width * Rectangle.Height) / (Rectangle.Width + length)) / 2;
  389. top = top + xchange;
  390. bottom = bottom - xchange;
  391. break;
  392. case 6:
  393. length = bottom - Rectangle.Bottom;
  394. xchange = (Rectangle.Width - (Rectangle.Width * Rectangle.Height) / (Rectangle.Height + length)) / 2;
  395. left = left + xchange;
  396. right = right - xchange;
  397. break;
  398. case 8:
  399. length = left - Rectangle.Left;
  400. xchange = (Rectangle.Height - (Rectangle.Width * Rectangle.Height) / (Rectangle.Width + length)) / 2;
  401. top = top + xchange;
  402. bottom = bottom - xchange;
  403. break;
  404. }
  405. }
  406. SetRectangle(left, top, right - left, bottom - top);
  407. }
  408. public override bool IntersectsWith(Rectangle rectangle)
  409. {
  410. return Rectangle.IntersectsWith(rectangle);
  411. }
  412. /// <summary>
  413. /// Move object
  414. /// </summary>
  415. /// <param name="deltaX"></param>
  416. /// <param name="deltaY"></param>
  417. public override void Move(int deltaX, int deltaY)
  418. {
  419. int x = ISurfaceBox.UnscaleScalar(deltaX);
  420. int y = ISurfaceBox.UnscaleScalar(deltaY);
  421. rectangle.X += x;
  422. rectangle.Y += y;
  423. }
  424. public override RectangleF GetBoundingBox()
  425. {
  426. return rectangle;
  427. }
  428. public override List<PointF> GetPoints()
  429. {
  430. List<PointF> points = new List<PointF>();
  431. points.Add(new PointF(rectangle.X, rectangle.Y));
  432. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  433. return points;
  434. }
  435. public override ParentStyleModel GetStyle()
  436. {
  437. return labelRectStyleModel;
  438. }
  439. }
  440. }