using OrpaonVision.Core.LayerRecognition; using OrpaonVision.Core.PartRecognition; using OrpaonVision.Core.Results; using OrpaonVision.Core.Common; namespace OrpaonVision.Core.PositionValidation; /// /// 位置校验服务接口。 /// public interface IPositionValidationService { /// /// 校验槽位位置。 /// /// 部件列表。 /// 槽位列表。 /// 取消令牌。 /// 槽位校验结果。 Task> ValidateSlotsAsync(IReadOnlyList parts, IReadOnlyList slots, CancellationToken cancellationToken = default); /// /// 校验重叠率。 /// /// 部件列表。 /// 最大重叠率。 /// 取消令牌。 /// 重叠率校验结果。 Task> ValidateOverlapAsync(IReadOnlyList parts, double maxOverlapRatio, CancellationToken cancellationToken = default); /// /// 校验中心点位置。 /// /// 部件列表。 /// 期望位置列表。 /// 位置容差。 /// 取消令牌。 /// 中心点校验结果。 Task> ValidateCenterPointsAsync(IReadOnlyList parts, IReadOnlyList expectedPositions, double tolerance, CancellationToken cancellationToken = default); /// /// 校验边界框位置。 /// /// 部件列表。 /// 期望边界框列表。 /// 位置容差。 /// 取消令牌。 /// 边界框校验结果。 Task> ValidateBoundingBoxesAsync(IReadOnlyList parts, IReadOnlyList expectedBoundingBoxes, double tolerance, CancellationToken cancellationToken = default); /// /// 校验角度偏差。 /// /// 部件列表。 /// 期望角度列表。 /// 角度容差。 /// 取消令牌。 /// 角度校验结果。 Task> ValidateAnglesAsync(IReadOnlyList parts, IReadOnlyList expectedAngles, double tolerance, CancellationToken cancellationToken = default); /// /// 批量位置校验。 /// /// 部件列表。 /// 校验规则列表。 /// 取消令牌。 /// 批量校验结果。 Task> BatchValidatePositionAsync(IReadOnlyList parts, IReadOnlyList validationRules, CancellationToken cancellationToken = default); /// /// 计算几何特征。 /// /// 部件列表。 /// 取消令牌。 /// 几何特征。 Task> CalculateGeometricFeaturesAsync(IReadOnlyList parts, CancellationToken cancellationToken = default); /// /// 获取位置校验统计信息。 /// /// 开始时间。 /// 结束时间。 /// 取消令牌。 /// 统计信息。 Task> GetValidationStatisticsAsync(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default); /// /// 创建位置校验规则。 /// /// 校验规则。 /// 取消令牌。 /// 创建结果。 Task> CreateValidationRuleAsync(PositionValidationRule rule, CancellationToken cancellationToken = default); /// /// 更新位置校验规则。 /// /// 校验规则。 /// 取消令牌。 /// 更新结果。 Task UpdateValidationRuleAsync(PositionValidationRule rule, CancellationToken cancellationToken = default); /// /// 删除位置校验规则。 /// /// 规则ID。 /// 取消令牌。 /// 删除结果。 Task DeleteValidationRuleAsync(Guid ruleId, CancellationToken cancellationToken = default); /// /// 获取位置校验规则列表。 /// /// 产品类型编码(可选)。 /// 层级编号(可选)。 /// 校验类型(可选)。 /// 取消令牌。 /// 规则列表。 Task>> GetValidationRulesAsync(string? productTypeCode = null, int? layerNumber = null, PositionValidationType? validationType = null, CancellationToken cancellationToken = default); } /// /// 槽位校验结果。 /// public sealed class SlotValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 总槽位数量。 /// public int TotalSlots { get; init; } /// /// 已占用槽位数量。 /// public int OccupiedSlots { get; init; } /// /// 空闲槽位数量。 /// public int EmptySlots { get; init; } /// /// 槽位占用率。 /// public double SlotOccupancyRate => TotalSlots > 0 ? (double)OccupiedSlots / TotalSlots * 100 : 0.0; /// /// 槽位匹配结果列表。 /// public IReadOnlyList SlotMatches { get; init; } = Array.Empty(); /// /// 未匹配的部件列表。 /// public IReadOnlyList UnmatchedParts { get; init; } = Array.Empty(); /// /// 未占用的槽位列表。 /// public IReadOnlyList UnoccupiedSlots { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 槽位匹配结果。 /// public sealed class SlotMatch { /// /// 匹配的部件。 /// public MappedPart Part { get; init; } = new(); /// /// 匹配的槽位。 /// public SlotDefinition Slot { get; init; } = new(); /// /// 匹配置信度。 /// public double MatchConfidence { get; init; } /// /// 位置偏差。 /// public double PositionDeviation { get; init; } /// /// 大小偏差。 /// public double SizeDeviation { get; init; } /// /// 是否为最佳匹配。 /// public bool IsBestMatch { get; init; } /// /// 匹配质量。 /// public MatchQuality MatchQuality { get; init; } } /// /// 重叠率校验结果。 /// public sealed class OverlapValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 最大重叠率。 /// public double MaxOverlapRatio { get; init; } /// /// 平均重叠率。 /// public double AverageOverlapRatio { get; init; } /// /// 重叠对数量。 /// public int OverlapPairCount { get; init; } /// /// 重叠对列表。 /// public IReadOnlyList OverlapPairs { get; init; } = Array.Empty(); /// /// 允许的最大重叠率。 /// public double AllowedMaxOverlapRatio { get; init; } /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 重叠对信息。 /// public sealed class OverlapPair { /// /// 部件1。 /// public MappedPart Part1 { get; init; } = new(); /// /// 部件2。 /// public MappedPart Part2 { get; init; } = new(); /// /// 重叠率。 /// public double OverlapRatio { get; init; } /// /// 重叠面积。 /// public double OverlapArea { get; init; } /// /// 重叠区域。 /// public BoundingBox OverlapRegion { get; init; } = new(); } /// /// 中心点校验结果。 /// public sealed class CenterPointValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 总部件数量。 /// public int TotalParts { get; init; } /// /// 匹配部件数量。 /// public int MatchedParts { get; init; } /// /// 平均位置偏差。 /// public double AveragePositionDeviation { get; init; } /// /// 最大位置偏差。 /// public double MaxPositionDeviation { get; init; } /// /// 允许的位置容差。 /// public double AllowedTolerance { get; init; } /// /// 中心点匹配结果列表。 /// public IReadOnlyList CenterPointMatches { get; init; } = Array.Empty(); /// /// 未匹配的部件列表。 /// public IReadOnlyList UnmatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 中心点匹配结果。 /// public sealed class CenterPointMatch { /// /// 匹配的部件。 /// public MappedPart Part { get; init; } = new(); /// /// 期望位置。 /// public ExpectedPosition ExpectedPosition { get; init; } = new(); /// /// 实际中心点。 /// public Point ActualCenterPoint { get; init; } /// /// 期望中心点。 /// public Point ExpectedCenterPoint { get; init; } /// /// 位置偏差。 /// public double PositionDeviation { get; init; } /// /// 是否在容差范围内。 /// public bool IsWithinTolerance { get; init; } /// /// 匹配质量。 /// public MatchQuality MatchQuality { get; init; } } /// /// 边界框校验结果。 /// public sealed class BoundingBoxValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 总部件数量。 /// public int TotalParts { get; init; } /// /// 匹配部件数量。 /// public int MatchedParts { get; init; } /// /// 平均位置偏差。 /// public double AveragePositionDeviation { get; init; } /// /// 平均大小偏差。 /// public double AverageSizeDeviation { get; init; } /// /// 允许的位置容差。 /// public double AllowedTolerance { get; init; } /// /// 边界框匹配结果列表。 /// public IReadOnlyList BoundingBoxMatches { get; init; } = Array.Empty(); /// /// 未匹配的部件列表。 /// public IReadOnlyList UnmatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 边界框匹配结果。 /// public sealed class BoundingBoxMatch { /// /// 匹配的部件。 /// public MappedPart Part { get; init; } = new(); /// /// 期望边界框。 /// public ExpectedBoundingBox ExpectedBoundingBox { get; init; } = new(); /// /// 实际边界框。 /// public BoundingBox ActualBoundingBox { get; init; } = new(); /// /// 位置偏差。 /// public double PositionDeviation { get; init; } /// /// 大小偏差。 /// public double SizeDeviation { get; init; } /// /// 是否在容差范围内。 /// public bool IsWithinTolerance { get; init; } /// /// 匹配质量。 /// public MatchQuality MatchQuality { get; init; } } /// /// 角度校验结果。 /// public sealed class AngleValidationResult { /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 总部件数量。 /// public int TotalParts { get; init; } /// /// 匹配部件数量。 /// public int MatchedParts { get; init; } /// /// 平均角度偏差。 /// public double AverageAngleDeviation { get; init; } /// /// 最大角度偏差。 /// public double MaxAngleDeviation { get; init; } /// /// 允许的角度容差。 /// public double AllowedTolerance { get; init; } /// /// 角度匹配结果列表。 /// public IReadOnlyList AngleMatches { get; init; } = Array.Empty(); /// /// 未匹配的部件列表。 /// public IReadOnlyList UnmatchedParts { get; init; } = Array.Empty(); /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 角度匹配结果。 /// public sealed class AngleMatch { /// /// 匹配的部件。 /// public MappedPart Part { get; init; } = new(); /// /// 期望角度。 /// public ExpectedAngle ExpectedAngle { get; init; } = new(); /// /// 实际角度。 /// public double ActualAngle { get; init; } /// /// 期望角度。 /// public double ExpectedAngleValue { get; init; } /// /// 角度偏差。 /// public double AngleDeviation { get; init; } /// /// 是否在容差范围内。 /// public bool IsWithinTolerance { get; init; } /// /// 匹配质量。 /// public MatchQuality MatchQuality { get; init; } } /// /// 批量位置校验结果。 /// public sealed class BatchPositionValidationResult { /// /// 校验结果列表。 /// public IReadOnlyList ValidationResults { get; init; } = Array.Empty(); /// /// 总规则数量。 /// public int TotalRules { get; init; } /// /// 通过的规则数量。 /// public int PassedRules { get; init; } /// /// 失败的规则数量。 /// public int FailedRules { get; init; } /// /// 总体通过率。 /// public double OverallPassRate => TotalRules > 0 ? (double)PassedRules / TotalRules * 100 : 0.0; /// /// 批量处理时间。 /// public DateTime BatchProcessingTimeUtc { get; init; } /// /// 总耗时(毫秒)。 /// public long TotalElapsedMs { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 几何特征。 /// public sealed class GeometricFeatures { /// /// 部件数量。 /// public int PartCount { get; init; } /// /// 总面积。 /// public double TotalArea { get; init; } /// /// 平均面积。 /// public double AverageArea { get; init; } /// /// 最大面积。 /// public double MaxArea { get; init; } /// /// 最小面积。 /// public double MinArea { get; init; } /// /// 面积标准差。 /// public double AreaStandardDeviation { get; init; } /// /// 平均长宽比。 /// public double AverageAspectRatio { get; init; } /// /// 中心点分布。 /// public IReadOnlyList CenterPoints { get; init; } = Array.Empty(); /// /// 中心点分布密度。 /// public double CenterPointDensity { get; init; } /// /// 重叠率统计。 /// public OverlapStatistics OverlapStatistics { get; init; } = new(); /// /// 计算时间。 /// public DateTime CalculationTimeUtc { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 重叠统计信息。 /// public sealed class OverlapStatistics { /// /// 重叠对数量。 /// public int OverlapPairCount { get; init; } /// /// 平均重叠率。 /// public double AverageOverlapRatio { get; init; } /// /// 最大重叠率。 /// public double MaxOverlapRatio { get; init; } /// /// 重叠率分布。 /// public IReadOnlyList Distribution { get; init; } = Array.Empty(); } /// /// 重叠分布信息。 /// public sealed class OverlapDistribution { /// /// 重叠率范围。 /// public string Range { get; init; } = string.Empty; /// /// 数量。 /// public int Count { get; init; } /// /// 百分比。 /// public double Percentage { get; init; } } /// /// 位置校验统计信息。 /// public sealed class PositionValidationStatistics { /// /// 总校验次数。 /// public int TotalValidations { get; init; } /// /// 成功校验次数。 /// public int SuccessfulValidations { get; init; } /// /// 失败校验次数。 /// public int FailedValidations { get; init; } /// /// 总体成功率。 /// public double OverallSuccessRate => TotalValidations > 0 ? (double)SuccessfulValidations / TotalValidations * 100 : 0.0; /// /// 按校验类型分组的统计。 /// public Dictionary ByValidationType { get; init; } = new(); /// /// 按部件类型分组的统计。 /// public Dictionary ByPartType { get; init; } = new(); /// /// 按产品类型分组的统计。 /// public Dictionary ByProductType { get; init; } = new(); /// /// 按层级分组的统计。 /// public Dictionary ByLayer { get; init; } = new(); /// /// 统计开始时间。 /// public DateTime StartTimeUtc { get; init; } /// /// 统计结束时间。 /// public DateTime EndTimeUtc { get; init; } } /// /// 位置校验结果基类。 /// public abstract class PositionValidationResult { /// /// 规则ID。 /// public Guid RuleId { get; init; } /// /// 规则名称。 /// public string RuleName { get; init; } = string.Empty; /// /// 校验类型。 /// public PositionValidationType ValidationType { get; init; } /// /// 是否通过校验。 /// public bool IsValid { get; init; } /// /// 校验时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 错误消息。 /// public string? ErrorMessage { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 位置校验规则。 /// public sealed class PositionValidationRule { /// /// 规则ID。 /// public Guid RuleId { get; init; } /// /// 规则名称。 /// public string RuleName { get; init; } = string.Empty; /// /// 规则描述。 /// public string RuleDescription { get; init; } = string.Empty; /// /// 产品类型编码。 /// public string ProductTypeCode { get; init; } = string.Empty; /// /// 层级编号。 /// public int LayerNumber { get; init; } /// /// 校验类型。 /// public PositionValidationType ValidationType { get; init; } /// /// 位置容差。 /// public double PositionTolerance { get; init; } /// /// 大小容差。 /// public double SizeTolerance { get; init; } /// /// 角度容差。 /// public double AngleTolerance { get; init; } /// /// 最大重叠率。 /// public double MaxOverlapRatio { get; init; } /// /// 是否启用。 /// public bool IsEnabled { get; init; } = true; /// /// 优先级。 /// public int Priority { get; init; } = 1; /// /// 创建时间。 /// public DateTime CreatedAtUtc { get; init; } /// /// 更新时间。 /// public DateTime UpdatedAtUtc { get; init; } /// /// 版本号。 /// public string Version { get; init; } = "1.0"; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 槽位定义。 /// public sealed class SlotDefinition { /// /// 槽位ID。 /// public Guid SlotId { get; init; } /// /// 槽位名称。 /// public string SlotName { get; init; } = string.Empty; /// /// 槽位区域。 /// public BoundingBox SlotRegion { get; init; } = new(); /// /// 期望部件类型。 /// public PartType? ExpectedPartType { get; init; } /// /// 期望部件编码。 /// public string? ExpectedPartCode { get; init; } /// /// 是否为必需槽位。 /// public bool IsRequired { get; init; } = true; /// /// 槽位权重。 /// public double Weight { get; init; } = 1.0; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 期望位置。 /// public sealed class ExpectedPosition { /// /// 位置ID。 /// public Guid PositionId { get; init; } /// /// 期望中心点。 /// public Point ExpectedCenterPoint { get; init; } /// /// 期望部件类型。 /// public PartType? ExpectedPartType { get; init; } /// /// 期望部件编码。 /// public string? ExpectedPartCode { get; init; } /// /// 位置权重。 /// public double Weight { get; init; } = 1.0; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 期望边界框。 /// public sealed class ExpectedBoundingBox { /// /// 边界框ID。 /// public Guid BoundingBoxId { get; init; } /// /// 期望边界框。 /// public BoundingBox BoundingBox { get; init; } = new(); /// /// 期望部件类型。 /// public PartType? ExpectedPartType { get; init; } /// /// 期望部件编码。 /// public string? ExpectedPartCode { get; init; } /// /// 边界框权重。 /// public double Weight { get; init; } = 1.0; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 期望角度。 /// public sealed class ExpectedAngle { /// /// 角度ID。 /// public Guid AngleId { get; init; } /// /// 期望角度(度)。 /// public double ExpectedAngleValue { get; init; } /// /// 期望部件类型。 /// public PartType? ExpectedPartType { get; init; } /// /// 期望部件编码。 /// public string? ExpectedPartCode { get; init; } /// /// 角度权重。 /// public double Weight { get; init; } = 1.0; /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 点坐标。 /// public sealed class Point { /// /// X坐标。 /// public double X { get; init; } /// /// Y坐标。 /// public double Y { get; init; } /// /// 计算到另一个点的距离。 /// public double DistanceTo(Point other) => Math.Sqrt(Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2)); } /// /// 位置校验类型枚举。 /// public enum PositionValidationType { /// /// 槽位校验。 /// Slot = 0, /// /// 重叠率校验。 /// Overlap = 1, /// /// 中心点校验。 /// CenterPoint = 2, /// /// 边界框校验。 /// BoundingBox = 3, /// /// 角度校验。 /// Angle = 4 }