Files
BQP/srcs/RobotNet10/RobotApp/RobotNet10.RobotApp/SLAM/Cartographer/Helpers/MapNameHelper.cs
2026-07-13 09:25:40 +07:00

61 lines
2.0 KiB
C#

using RobotNet10.RobotApp.Shared;
namespace RobotNet10.RobotApp.SLAM.Cartographer.Helpers;
/// <summary>
/// Shared helper for map name sanitization and legacy metadata migration.
/// Used by CartographerService and MapSaveProcessor.
/// </summary>
public static class MapNameHelper
{
/// <summary>
/// Sanitize map name for use as directory name.
/// Removes invalid filename characters and trims spaces/dots.
/// </summary>
public static string Sanitize(string mapName)
{
if (string.IsNullOrWhiteSpace(mapName))
throw new ArgumentException("Map name cannot be null or empty", nameof(mapName));
var invalidChars = Path.GetInvalidFileNameChars();
var sanitized = mapName;
foreach (var c in invalidChars)
{
sanitized = sanitized.Replace(c, '_');
}
sanitized = sanitized.Trim(' ', '.');
if (string.IsNullOrWhiteSpace(sanitized))
throw new ArgumentException("Map name is invalid after sanitization", nameof(mapName));
return sanitized;
}
/// <summary>
/// Ensures MapInfo has Size calculated from Bounds for legacy map.json files
/// that don't have Size field populated.
/// </summary>
public static MapInfo EnsureMapSize(MapInfo metadata)
{
if (metadata.Size.Width == 0 && metadata.Size.Height == 0)
{
var width = metadata.Bounds.MaxX - metadata.Bounds.MinX;
var height = metadata.Bounds.MaxY - metadata.Bounds.MinY;
return new MapInfo
{
Name = metadata.Name,
FolderPath = metadata.FolderPath,
CreatedDate = metadata.CreatedDate,
Resolution = metadata.Resolution,
Size = new MapSize(width, height),
Origin = metadata.Origin,
Bounds = metadata.Bounds,
TrajectoryNodeCount = metadata.TrajectoryNodeCount
};
}
return metadata;
}
}