using OrpaonVision.Core.LayerRecognition;
using OrpaonVision.Core.PartRecognition;
using OrpaonVision.Core.PositionValidation;
using OrpaonVision.Core.Results;
namespace OrpaonVision.Core.PlacementJudgment;
///
/// 到位判定服务接口。
///
public interface IPlacementJudgmentService
{
///
/// 判定部件到位状态。
///
/// 部件列表。
/// 判定规则列表。
/// 取消令牌。
/// 到位判定结果。
Task> JudgePlacementAsync(IReadOnlyList parts, IReadOnlyList judgmentRules, CancellationToken cancellationToken = default);
///
/// 判定面积到位状态。
///
/// 部件列表。
/// 期望面积列表。
/// 面积容差(百分比)。
/// 取消令牌。
/// 面积判定结果。
Task> JudgeAreaAsync(IReadOnlyList parts, IReadOnlyList expectedAreas, double tolerance, CancellationToken cancellationToken = default);
///
/// 判定位置偏差到位状态。
///
/// 部件列表。
/// 期望位置列表。
/// 位置容差。
/// 取消令牌。
/// 位置偏差判定结果。
Task> JudgePositionDeviationAsync(IReadOnlyList parts, IReadOnlyList expectedPositions, double tolerance, CancellationToken cancellationToken = default);
///
/// 判定稳定帧到位状态。
///
/// 帧历史记录。
/// 稳定性规则列表。
/// 取消令牌。
/// 稳定帧判定结果。
Task> JudgeStabilityAsync(IReadOnlyList frameHistory, IReadOnlyList stabilityRules, CancellationToken cancellationToken = default);
///
/// 复合到位判定。
///
/// 部件列表。
/// 帧历史记录。
/// 复合判定规则列表。
/// 取消令牌。
/// 复合判定结果。
Task> JudgeCompositeAsync(IReadOnlyList parts, IReadOnlyList frameHistory, IReadOnlyList compositeRules, CancellationToken cancellationToken = default);
///
/// 批量到位判定。
///
/// 部件列表。
/// 帧历史记录。
/// 判定规则列表。
/// 取消令牌。
/// 批量判定结果。
Task> BatchJudgeAsync(IReadOnlyList parts, IReadOnlyList frameHistory, IReadOnlyList judgmentRules, CancellationToken cancellationToken = default);
///
/// 获取到位判定统计信息。
///
/// 开始时间。
/// 结束时间。
/// 取消令牌。
/// 统计信息。
Task> GetJudgmentStatisticsAsync(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default);
///
/// 创建到位判定规则。
///
/// 判定规则。
/// 取消令牌。
/// 创建结果。
Task> CreateJudgmentRuleAsync(PlacementJudgmentRule rule, CancellationToken cancellationToken = default);
///
/// 更新到位判定规则。
///
/// 判定规则。
/// 取消令牌。
/// 更新结果。
Task UpdateJudgmentRuleAsync(PlacementJudgmentRule rule, CancellationToken cancellationToken = default);
///
/// 删除到位判定规则。
///
/// 规则ID。
/// 取消令牌。
/// 删除结果。
Task DeleteJudgmentRuleAsync(Guid ruleId, CancellationToken cancellationToken = default);
///
/// 获取到位判定规则列表。
///
/// 产品类型编码(可选)。
/// 层级编号(可选)。
/// 判定类型(可选)。
/// 取消令牌。
/// 规则列表。
Task>> GetJudgmentRulesAsync(string? productTypeCode = null, int? layerNumber = null, PlacementJudgmentType? judgmentType = null, CancellationToken cancellationToken = default);
}
///
/// 到位判定结果。
///
public sealed class PlacementJudgmentResult
{
///
/// 是否通过判定。
///
public bool IsPlaced { get; init; }
///
/// 总体置信度。
///
public double OverallConfidence { get; init; }
///
/// 判定时间。
///
public DateTime JudgmentTimeUtc { get; init; }
///
/// 部件判定结果列表。
///
public IReadOnlyList PartJudgmentResults { get; init; } = Array.Empty();
///
/// 未到位部件列表。
///
public IReadOnlyList UnplacedParts { get; init; } = Array.Empty();
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 部件判定结果。
///
public sealed class PartJudgmentResult
{
///
/// 部件信息。
///
public MappedPart Part { get; init; } = new();
///
/// 是否到位。
///
public bool IsPlaced { get; init; }
///
/// 到位置信度。
///
public double PlacementConfidence { get; init; }
///
/// 面积判定结果。
///
public AreaJudgmentResult? AreaResult { get; init; }
///
/// 位置偏差判定结果。
///
public PositionDeviationJudgmentResult? PositionResult { get; init; }
///
/// 稳定性判定结果。
///
public StabilityJudgmentResult? StabilityResult { get; init; }
///
/// 判定质量。
///
public JudgmentQuality JudgmentQuality { get; init; }
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 面积判定结果。
///
public sealed class AreaJudgmentResult
{
///
/// 是否通过面积判定。
///
public bool IsAreaValid { get; init; }
///
/// 实际面积。
///
public double ActualArea { get; init; }
///
/// 期望面积。
///
public double ExpectedArea { get; init; }
///
/// 面积偏差(百分比)。
///
public double AreaDeviation { get; init; }
///
/// 允许的面积容差。
///
public double AllowedTolerance { get; init; }
///
/// 面积判定时间。
///
public DateTime JudgmentTimeUtc { get; init; }
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 位置偏差判定结果。
///
public sealed class PositionDeviationJudgmentResult
{
///
/// 是否通过位置偏差判定。
///
public bool IsPositionValid { get; init; }
///
/// 实际位置。
///
public Point ActualPosition { get; init; }
///
/// 期望位置。
///
public Point ExpectedPosition { get; init; }
///
/// 位置偏差。
///
public double PositionDeviation { get; init; }
///
/// 允许的位置容差。
///
public double AllowedTolerance { get; init; }
///
/// 位置判定时间。
///
public DateTime JudgmentTimeUtc { get; init; }
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 稳定性判定结果。
///
public sealed class StabilityJudgmentResult
{
///
/// 是否通过稳定性判定。
///
public bool IsStable { get; init; }
///
/// 稳定性分数。
///
public double StabilityScore { get; init; }
///
/// 稳定帧数量。
///
public int StableFrameCount { get; init; }
///
/// 总帧数量。
///
public int TotalFrameCount { get; init; }
///
/// 稳定性阈值。
///
public double StabilityThreshold { get; init; }
///
/// 位置变化率。
///
public double PositionChangeRate { get; init; }
///
/// 面积变化率。
///
public double AreaChangeRate { get; init; }
///
/// 置信度变化率。
///
public double ConfidenceChangeRate { get; init; }
///
/// 稳定性判定时间。
///
public DateTime JudgmentTimeUtc { get; init; }
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 复合判定结果。
///
public sealed class CompositeJudgmentResult
{
///
/// 是否通过复合判定。
///
public bool IsCompositeValid { get; init; }
///
/// 复合置信度。
///
public double CompositeConfidence { get; init; }
///
/// 面积判定结果。
///
public AreaJudgmentResult? AreaResult { get; init; }
///
/// 位置偏差判定结果。
///
public PositionDeviationJudgmentResult? PositionResult { get; init; }
///
/// 稳定性判定结果。
///
public StabilityJudgmentResult? StabilityResult { get; init; }
///
/// 加权分数。
///
public double WeightedScore { get; init; }
///
/// 判定策略。
///
public CompositeJudgmentStrategy JudgmentStrategy { get; init; }
///
/// 复合判定时间。
///
public DateTime JudgmentTimeUtc { get; init; }
///
/// 判定详情。
///
public string? Details { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 批量判定结果。
///
public sealed class BatchJudgmentResult
{
///
/// 判定结果列表。
///
public IReadOnlyList JudgmentResults { 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 PlacementJudgmentStatistics
{
///
/// 总判定次数。
///
public int TotalJudgments { get; init; }
///
/// 成功判定次数。
///
public int SuccessfulJudgments { get; init; }
///
/// 失败判定次数。
///
public int FailedJudgments { get; init; }
///
/// 总体成功率。
///
public double OverallSuccessRate => TotalJudgments > 0 ? (double)SuccessfulJudgments / TotalJudgments * 100 : 0.0;
///
/// 按判定类型分组的统计。
///
public Dictionary ByJudgmentType { 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 sealed class JudgmentTypeStatistics
{
///
/// 判定类型。
///
public PlacementJudgmentType JudgmentType { get; init; }
///
/// 判定次数。
///
public int JudgmentCount { get; init; }
///
/// 成功次数。
///
public int SuccessCount { get; init; }
///
/// 成功率。
///
public double SuccessRate => JudgmentCount > 0 ? (double)SuccessCount / JudgmentCount * 100 : 0.0;
///
/// 平均耗时(毫秒)。
///
public double AverageElapsedMs { get; init; }
}
///
/// 部件类型判定统计。
///
public sealed class PartTypeJudgmentStatistics
{
///
/// 部件类型。
///
public PartType PartType { get; init; }
///
/// 判定次数。
///
public int JudgmentCount { get; init; }
///
/// 成功次数。
///
public int SuccessCount { get; init; }
///
/// 成功率。
///
public double SuccessRate => JudgmentCount > 0 ? (double)SuccessCount / JudgmentCount * 100 : 0.0;
///
/// 平均置信度。
///
public double AverageConfidence { get; init; }
}
///
/// 产品类型判定统计。
///
public sealed class ProductTypeJudgmentStatistics
{
///
/// 产品类型编码。
///
public string ProductTypeCode { get; init; } = string.Empty;
///
/// 判定次数。
///
public int JudgmentCount { get; init; }
///
/// 成功次数。
///
public int SuccessCount { get; init; }
///
/// 成功率。
///
public double SuccessRate => JudgmentCount > 0 ? (double)SuccessCount / JudgmentCount * 100 : 0.0;
///
/// 平均置信度。
///
public double AverageConfidence { get; init; }
}
///
/// 层级判定统计。
///
public sealed class LayerJudgmentStatistics
{
///
/// 层级编号。
///
public int LayerNumber { get; init; }
///
/// 判定次数。
///
public int JudgmentCount { get; init; }
///
/// 成功次数。
///
public int SuccessCount { get; init; }
///
/// 成功率。
///
public double SuccessRate => JudgmentCount > 0 ? (double)SuccessCount / JudgmentCount * 100 : 0.0;
///
/// 平均置信度。
///
public double AverageConfidence { get; init; }
}
///
/// 到位判定规则。
///
public sealed class PlacementJudgmentRule
{
///
/// 规则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 PlacementJudgmentType JudgmentType { get; init; }
///
/// 面积容差(百分比)。
///
public double AreaTolerance { get; init; }
///
/// 位置容差。
///
public double PositionTolerance { get; init; }
///
/// 稳定性阈值。
///
public double StabilityThreshold { get; init; }
///
/// 稳定帧窗口大小。
///
public int StabilityWindowSize { get; init; }
///
/// 复合判定策略。
///
public CompositeJudgmentStrategy CompositeStrategy { get; init; }
///
/// 权重配置。
///
public JudgmentWeights Weights { get; init; } = new();
///
/// 是否启用。
///
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 JudgmentWeights
{
///
/// 面积权重。
///
public double AreaWeight { get; init; } = 0.3;
///
/// 位置权重。
///
public double PositionWeight { get; init; } = 0.4;
///
/// 稳定性权重。
///
public double StabilityWeight { get; init; } = 0.3;
///
/// 验证权重总和是否为1。
///
public bool IsValid => Math.Abs(AreaWeight + PositionWeight + StabilityWeight - 1.0) < 0.001;
}
///
/// 期望面积。
///
public sealed class ExpectedArea
{
///
/// 面积ID。
///
public Guid AreaId { get; init; }
///
/// 期望面积。
///
public double ExpectedAreaValue { 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 StabilityRule
{
///
/// 规则ID。
///
public Guid RuleId { get; init; }
///
/// 规则名称。
///
public string RuleName { get; init; } = string.Empty;
///
/// 稳定性阈值。
///
public double StabilityThreshold { get; init; }
///
/// 窗口大小。
///
public int WindowSize { get; init; }
///
/// 位置变化阈值。
///
public double PositionChangeThreshold { get; init; }
///
/// 面积变化阈值。
///
public double AreaChangeThreshold { get; init; }
///
/// 置信度变化阈值。
///
public double ConfidenceChangeThreshold { get; init; }
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 复合判定规则。
///
public sealed class CompositeJudgmentRule
{
///
/// 规则ID。
///
public Guid RuleId { get; init; }
///
/// 规则名称。
///
public string RuleName { get; init; } = string.Empty;
///
/// 判定策略。
///
public CompositeJudgmentStrategy Strategy { get; init; }
///
/// 权重配置。
///
public JudgmentWeights Weights { get; init; } = new();
///
/// 最小通过条件。
///
public int MinimumPassConditions { get; init; } = 2;
///
/// 扩展属性。
///
public Dictionary ExtendedProperties { get; init; } = new();
}
///
/// 到位判定类型枚举。
///
public enum PlacementJudgmentType
{
///
/// 面积判定。
///
Area = 0,
///
/// 位置偏差判定。
///
PositionDeviation = 1,
///
/// 稳定性判定。
///
Stability = 2,
///
/// 复合判定。
///
Composite = 3
}
///
/// 判定质量枚举。
///
public enum JudgmentQuality
{
///
/// 优秀。
///
Excellent = 0,
///
/// 良好。
///
Good = 1,
///
/// 一般。
///
Fair = 2,
///
/// 较差。
///
Poor = 3
}
///
/// 复合判定策略枚举。
///
public enum CompositeJudgmentStrategy
{
///
/// 全部通过。
///
AllPass = 0,
///
/// 任意通过。
///
AnyPass = 1,
///
/// 加权平均。
///
WeightedAverage = 2,
///
/// 最小通过数量。
///
MinimumPass = 3
}