DrawStitchingCircle.cs 17 KB

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