RotatedRect.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Prism.Mvvm;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace TeamAAS_VP.Controls
  8. {
  9. /// <summary>
  10. /// 支持旋转的矩形
  11. /// </summary>
  12. public class RotatedRect : BindableBase
  13. {
  14. private double _x;
  15. private double _y;
  16. private double _width;
  17. private double _height;
  18. private double _angle; // 旋转角度,单位:度
  19. public double X
  20. {
  21. get => _x;
  22. set => SetProperty(ref _x, value);
  23. }
  24. public double Y
  25. {
  26. get => _y;
  27. set => SetProperty(ref _y, value);
  28. }
  29. public double Width
  30. {
  31. get => _width;
  32. set => SetProperty(ref _width, Math.Max(0, value));
  33. }
  34. public double Height
  35. {
  36. get => _height;
  37. set => SetProperty(ref _height, Math.Max(0, value));
  38. }
  39. public double Angle
  40. {
  41. get => _angle;
  42. set => SetProperty(ref _angle, NormalizeAngle(value));
  43. }
  44. public double CenterX => X + Width / 2;
  45. public double CenterY => Y + Height / 2;
  46. public RotatedRect()
  47. {
  48. }
  49. public RotatedRect(double x, double y, double width, double height, double angle = 0)
  50. {
  51. X = x;
  52. Y = y;
  53. Width = width;
  54. Height = height;
  55. Angle = angle;
  56. }
  57. private double NormalizeAngle(double angle)
  58. {
  59. // 将角度规范到0-360度
  60. angle %= 360;
  61. if (angle < 0) angle += 360;
  62. return angle;
  63. }
  64. public System.Windows.Rect ToRect()
  65. {
  66. return new System.Windows.Rect(X, Y, Width, Height);
  67. }
  68. public OpenCvSharp.RotatedRect ToCvRotatedRect()
  69. {
  70. var center = new OpenCvSharp.Point2f((float)CenterX, (float)CenterY);
  71. var size = new OpenCvSharp.Size2f((float)Width, (float)Height);
  72. return new OpenCvSharp.RotatedRect(center, size, (float)Angle);
  73. }
  74. public bool ContainsPoint(System.Windows.Point point)
  75. {
  76. // 简化版:不考虑旋转的包含检测
  77. return point.X >= X && point.X <= X + Width &&
  78. point.Y >= Y && point.Y <= Y + Height;
  79. }
  80. public override string ToString()
  81. {
  82. return $"X={X:F1}, Y={Y:F1}, W={Width:F1}, H={Height:F1}, Angle={Angle:F1}°";
  83. }
  84. }
  85. /// <summary>
  86. /// ROI操作模式
  87. /// </summary>
  88. public enum RoiOperationMode
  89. {
  90. None, // 无操作
  91. Draw, // 绘制
  92. Move, // 移动
  93. Resize, // 调整大小
  94. Rotate, // 旋转
  95. CornerResize // 角点调整
  96. }
  97. /// <summary>
  98. /// 拖动控制点类型
  99. /// </summary>
  100. public enum DragHandleType
  101. {
  102. None,
  103. TopLeft,
  104. TopRight,
  105. BottomLeft,
  106. BottomRight,
  107. Top,
  108. Right,
  109. Bottom,
  110. Left,
  111. Rotate
  112. }
  113. }