修改Gridmap缓存重建机制,考虑所有物流构件变化

This commit is contained in:
tian 2025-10-07 15:57:30 +08:00
parent f5d1361146
commit a8e8760e2b

View File

@ -9,14 +9,14 @@ namespace NavisworksTransport.PathPlanning
{
/// <summary>
/// GridMap缓存键用于标识唯一的GridMap生成配置
/// 基于通道数据、车辆尺寸、网格尺寸等关键参数
/// 基于所有物流属性构件、车辆尺寸、网格尺寸等关键参数
/// </summary>
public class GridMapCacheKey : IEquatable<GridMapCacheKey>
{
/// <summary>
/// 通道数据哈希值(所有可通行物流构件
/// 物流数据哈希值(所有物流属性构件,包括通道、门、无关项等
/// </summary>
public string ChannelDataHash { get; set; }
public string LogisticsDataHash { get; set; }
/// <summary>
/// 边界范围哈希
@ -46,10 +46,10 @@ namespace NavisworksTransport.PathPlanning
/// <summary>
/// 构造函数
/// </summary>
public GridMapCacheKey(string channelDataHash, string boundsHash, double cellSize,
public GridMapCacheKey(string logisticsDataHash, string boundsHash, double cellSize,
double vehicleRadius, double safetyMargin, double vehicleHeight)
{
ChannelDataHash = channelDataHash ?? throw new ArgumentNullException(nameof(channelDataHash));
LogisticsDataHash = logisticsDataHash ?? throw new ArgumentNullException(nameof(logisticsDataHash));
BoundsHash = boundsHash ?? throw new ArgumentNullException(nameof(boundsHash));
CellSize = cellSize;
VehicleRadius = vehicleRadius;
@ -65,7 +65,7 @@ namespace NavisworksTransport.PathPlanning
if (other == null) return false;
if (ReferenceEquals(this, other)) return true;
return ChannelDataHash == other.ChannelDataHash &&
return LogisticsDataHash == other.LogisticsDataHash &&
BoundsHash == other.BoundsHash &&
Math.Abs(CellSize - other.CellSize) < 1e-6 &&
Math.Abs(VehicleRadius - other.VehicleRadius) < 1e-6 &&
@ -89,7 +89,7 @@ namespace NavisworksTransport.PathPlanning
unchecked
{
int hash = 17;
hash = hash * 23 + (ChannelDataHash?.GetHashCode() ?? 0);
hash = hash * 23 + (LogisticsDataHash?.GetHashCode() ?? 0);
hash = hash * 23 + (BoundsHash?.GetHashCode() ?? 0);
hash = hash * 23 + CellSize.GetHashCode();
hash = hash * 23 + VehicleRadius.GetHashCode();
@ -104,21 +104,20 @@ namespace NavisworksTransport.PathPlanning
/// </summary>
public override string ToString()
{
var channelPreview = ChannelDataHash != null ?
ChannelDataHash.Substring(0, Math.Min(8, ChannelDataHash.Length)) :
var logisticsPreview = LogisticsDataHash != null ?
LogisticsDataHash.Substring(0, Math.Min(8, LogisticsDataHash.Length)) :
"NULL";
var boundsPreview = BoundsHash != null ?
BoundsHash.Substring(0, Math.Min(8, BoundsHash.Length)) :
"NULL";
return $"GridMapKey[Ch={channelPreview}, Bounds={boundsPreview}, " +
return $"GridMapKey[Logistics={logisticsPreview}, Bounds={boundsPreview}, " +
$"Cell={CellSize:F2}, VR={VehicleRadius:F1}m, SM={SafetyMargin:F1}m, VH={VehicleHeight:F1}m]";
}
/// <summary>
/// 根据参数生成GridMap缓存键
/// </summary>
/// <param name="document">Navisworks文档</param>
/// <param name="bounds">边界范围</param>
/// <param name="cellSize">网格大小</param>
/// <param name="vehicleRadius">车辆半径(米)</param>
@ -129,19 +128,19 @@ namespace NavisworksTransport.PathPlanning
double cellSize, double vehicleRadius,
double safetyMargin, double vehicleHeight)
{
var channelDataHash = ComputeChannelDataHash();
var logisticsDataHash = ComputeLogisticsDataHash();
var boundsHash = ComputeBoundsHash(bounds);
return new GridMapCacheKey(channelDataHash, boundsHash, cellSize,
return new GridMapCacheKey(logisticsDataHash, boundsHash, cellSize,
vehicleRadius, safetyMargin, vehicleHeight);
}
/// <summary>
/// 计算通道数据哈希值
/// 基于所有可通行物流构件的ID、属性、几何等信息
/// 计算物流数据哈希值
/// 基于所有物流属性构件(包括通道、门、无关项等)的ID、属性、几何等信息
/// </summary>
/// <returns>通道数据哈希</returns>
private static string ComputeChannelDataHash()
/// <returns>物流数据哈希</returns>
private static string ComputeLogisticsDataHash()
{
try
{
@ -149,13 +148,13 @@ namespace NavisworksTransport.PathPlanning
{
var hashBuilder = new StringBuilder();
// 获取所有可通行的物流模型项
var allChannelItems = CategoryAttributeManager.GetAllTraversableLogisticsItems();
// 获取所有有物流属性的模型项(不仅仅是可通行项)
var allLogisticsItems = CategoryAttributeManager.GetAllLogisticsItems();
// 按InstanceGuid排序确保一致性
var sortedItems = allChannelItems.OrderBy(item => item.InstanceGuid.ToString()).ToList();
var sortedItems = allLogisticsItems.OrderBy(item => item.InstanceGuid.ToString()).ToList();
hashBuilder.Append($"ChannelCount:{sortedItems.Count}|");
hashBuilder.Append($"LogisticsCount:{sortedItems.Count}|");
foreach (var item in sortedItems)
{
@ -199,8 +198,8 @@ namespace NavisworksTransport.PathPlanning
}
catch (Exception ex)
{
LogManager.Warning($"[通道哈希] 计算通道数据哈希失败: {ex.Message},使用时间戳标识");
return $"CHANNEL_HASH_FAILED_{DateTime.Now.Ticks}";
LogManager.Warning($"[物流哈希] 计算物流数据哈希失败: {ex.Message},使用时间戳标识");
return $"LOGISTICS_HASH_FAILED_{DateTime.Now.Ticks}";
}
}