using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeamAAS_VP.Controls
{
///
/// 支持旋转的矩形
///
public class RotatedRect : BindableBase
{
private double _x;
private double _y;
private double _width;
private double _height;
private double _angle; // 旋转角度,单位:度
public double X
{
get => _x;
set => SetProperty(ref _x, value);
}
public double Y
{
get => _y;
set => SetProperty(ref _y, value);
}
public double Width
{
get => _width;
set => SetProperty(ref _width, Math.Max(0, value));
}
public double Height
{
get => _height;
set => SetProperty(ref _height, Math.Max(0, value));
}
public double Angle
{
get => _angle;
set => SetProperty(ref _angle, NormalizeAngle(value));
}
public double CenterX => X + Width / 2;
public double CenterY => Y + Height / 2;
public RotatedRect()
{
}
public RotatedRect(double x, double y, double width, double height, double angle = 0)
{
X = x;
Y = y;
Width = width;
Height = height;
Angle = angle;
}
private double NormalizeAngle(double angle)
{
// 将角度规范到0-360度
angle %= 360;
if (angle < 0) angle += 360;
return angle;
}
public System.Windows.Rect ToRect()
{
return new System.Windows.Rect(X, Y, Width, Height);
}
public OpenCvSharp.RotatedRect ToCvRotatedRect()
{
var center = new OpenCvSharp.Point2f((float)CenterX, (float)CenterY);
var size = new OpenCvSharp.Size2f((float)Width, (float)Height);
return new OpenCvSharp.RotatedRect(center, size, (float)Angle);
}
public bool ContainsPoint(System.Windows.Point point)
{
// 简化版:不考虑旋转的包含检测
return point.X >= X && point.X <= X + Width &&
point.Y >= Y && point.Y <= Y + Height;
}
public override string ToString()
{
return $"X={X:F1}, Y={Y:F1}, W={Width:F1}, H={Height:F1}, Angle={Angle:F1}°";
}
}
///
/// ROI操作模式
///
public enum RoiOperationMode
{
None, // 无操作
Draw, // 绘制
Move, // 移动
Resize, // 调整大小
Rotate, // 旋转
CornerResize // 角点调整
}
///
/// 拖动控制点类型
///
public enum DragHandleType
{
None,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Top,
Right,
Bottom,
Left,
Rotate
}
}