38 lines
1.6 KiB
C#
38 lines
1.6 KiB
C#
using System;
|
||||
|
|
|
|||
|
|
namespace MultiWheelC.TrajectoryPlanning.Mapping;
|
|||
|
|
|
|||
|
|
/// <summary>从只读规划地图生成简单的 RGBA 占据图字节数组。</summary>
|
|||
|
|
internal static class PlanningMapImageRenderer
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 渲染规划地图为行主序 RGBA 像素。
|
|||
|
|
///
|
|||
|
|
/// 参数:map 为只读规划快照;pixelsPerCell 为每个栅格的像素边长;width、height 返回图像像素尺寸。
|
|||
|
|
/// 返回:长度为 width × height × 4 的 RGBA 字节数组;不会修改地图。
|
|||
|
|
/// </summary>
|
|||
|
|
public static byte[] Render(PlanningGridMap map, int pixelsPerCell, out int width, out int height)
|
|||
|
|
{
|
|||
|
|
if (map == null) throw new ArgumentNullException(nameof(map));
|
|||
|
|
width = checked(map.Cols * pixelsPerCell);
|
|||
|
|
height = checked(map.Rows * pixelsPerCell);
|
|||
|
|
var rgba = new byte[checked(width * height * 4)];
|
|||
|
|
for (int row = 0; row < map.Rows; row++)
|
|||
|
|
for (int col = 0; col < map.Cols; col++)
|
|||
|
|
{
|
|||
|
|
bool occupied = map.IsOccupied(row, col);
|
|||
|
|
byte red = occupied ? (byte)220 : (byte)245;
|
|||
|
|
byte green = occupied ? (byte)45 : (byte)245;
|
|||
|
|
byte blue = occupied ? (byte)45 : (byte)245;
|
|||
|
|
int displayRow = map.Rows - 1 - row;
|
|||
|
|
for (int py = 0; py < pixelsPerCell; py++)
|
|||
|
|
for (int px = 0; px < pixelsPerCell; px++)
|
|||
|
|
{
|
|||
|
|
int index = ((displayRow * pixelsPerCell + py) * width + col * pixelsPerCell + px) * 4;
|
|||
|
|
rgba[index] = red; rgba[index + 1] = green; rgba[index + 2] = blue; rgba[index + 3] = 255;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return rgba;
|
|||
|
|
}
|
|||
|
|
}
|