Skip to content

Commit

Permalink
Waypoint heatmap visualization
Browse files Browse the repository at this point in the history
Co-Authored-By: Puvikaran Santhirasegaram <[email protected]>
  • Loading branch information
S3JER and Puvikaran2001 committed Nov 8, 2024
1 parent 5b56a6f commit 4d792c0
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions Assets/Scripts/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class GlobalSettings {

public static readonly int TicksBeforeExplorationHeatMapCold = 10 * 60 * 4;
public static readonly int TicksBeforeCoverageHeatMapCold = 10 * 60 * 4;
public static readonly int TicksBeforeWaypointCoverageHeatMapCold = 10 * 60 * 4;

public static bool IsRosMode = false;

Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Map/Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class Vertex : ICloneable
{
private readonly HashSet<Vertex> _neighbors = new HashSet<Vertex>();
public float Weight { get; }
public int LastTimeVisitedTick { get; private set; }
public int LastTimeVisitedTick { get; private set; } = 0;
public Vector2Int Position { get; }
public Color Color { get; }
public Color Color { get; set;}

public Vertex(float weight, Vector2Int position, Color? color = null)
{
Expand Down
12 changes: 12 additions & 0 deletions Assets/Scripts/Map/Visualization/WaypointHeatMapVisualization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Maes.Statistics;

namespace Maes.Map.Visualization
{
public class WaypointHeatMapVisualizationMode : IPatrollingVisualizationMode
{
public void UpdateVisualization(PatrollingVisualizer visualizer, int currentTick)
{
visualizer.ShowWaypointHeatMap(currentTick);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Scripts/Statistics/PatrollingVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class PatrollingVisualizer : MonoBehaviour, IVisualizer<Tile> {

private List<GameObject> _visualizers = new List<GameObject>();

private Dictionary<Vertex, GameObject> _vertexVisualizers = new Dictionary<Vertex, GameObject>();

public void SetSimulationMap(SimulationMap<Tile> simulationMap, Vector3 offset)
{
// We have to offset this for some reason ¯\_(ツ)_/¯
Expand All @@ -40,6 +42,7 @@ private void CreateVisualizers()
var meshRenderer = vertexVisualizer.GetComponent<MeshRenderer>();
meshRenderer.material.color = vertex.Color;
_visualizers.Add(vertexVisualizer);
_vertexVisualizers.Add(vertex, vertexVisualizer);

foreach (var otherVertex in vertex.Neighbors) {
var edgeVisualizer = GameObject.Instantiate(EdgeVisualizer, transform);
Expand All @@ -52,5 +55,16 @@ private void CreateVisualizers()
}
}
}

public void ShowWaypointHeatMap(int currentTick){

foreach (var vertex in _patrollingMap.Verticies)
{
var ticksSinceLastExplored = currentTick - vertex.LastTimeVisitedTick;
float coldness = Mathf.Min((float) ticksSinceLastExplored / (float) GlobalSettings.TicksBeforeWaypointCoverageHeatMapCold, 1.0f);
var color = Color32.Lerp(ExplorationVisualizer.WarmColor, ExplorationVisualizer.ColdColor, coldness);
_vertexVisualizers[vertex].GetComponent<MeshRenderer>().material.color = color;
}
}
}
}
17 changes: 6 additions & 11 deletions Assets/Scripts/Trackers/PatrollingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Maes.Map.Visualization;
using Maes.Robot;
using Maes.Statistics;

using MAES.Trackers;

using UnityEngine;
Expand All @@ -19,7 +18,7 @@ public class PatrollingTracker : Tracker<Tile, PatrollingVisualizer, IPatrolling
private PatrollingSimulation PatrollingSimulation { get; }
private PatrollingMap Map { get;}
private Dictionary<Vector2Int, VertexDetails> Vertices { get; }

public int WorstGraphIdleness { get; private set; }
// TODO: TotalDistanceTraveled is not set any where in the code, don't know how to calculate it yet
public float TotalDistanceTraveled { get; private set; } = 0;
Expand All @@ -39,7 +38,7 @@ public PatrollingTracker(SimulationMap<Tile> collisionMap, PatrollingVisualizer
Map = map;
Vertices = map.Verticies.ToDictionary(vertex => vertex.Position, vertex => new VertexDetails(vertex));

_currentVisualizationMode = new PatrollingDummyVisualizationMode();
_currentVisualizationMode = new WaypointHeatMapVisualizationMode();
}

public void OnReachedVertex(Vertex vertex, int atTick)
Expand Down Expand Up @@ -83,6 +82,10 @@ protected override RayTracingMap<Tile>.CellFunction RayTracingMapCellFunction(Sl
return (x, y) => true;
}

public void ShowWaypointHeatMap()
{
SetVisualizationMode(new WaypointHeatMapVisualizationMode());
}
private IReadOnlyList<int> GetEachVertexIdleness()
{
var currentTick = PatrollingSimulation.SimulatedLogicTicks;
Expand All @@ -94,12 +97,4 @@ private void SetCompletedCycles()
CompletedCycles = Vertices.Values.Select(v => v.NumberOfVisits).Min();
}
}

public class PatrollingDummyVisualizationMode : IPatrollingVisualizationMode
{
public void UpdateVisualization(PatrollingVisualizer visualizer, int currentTick)
{

}
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/Trackers/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class Tracker<TCell, TVisualizer, TVisualizationMode> : ITracker
{
private CoverageCalculator _coverageCalculator;

private TVisualizer _visualizer;
protected TVisualizer _visualizer;

protected SimulationMap<TCell> _map;
private RayTracingMap<TCell> _rayTracingMap;
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/UI/Patrolling.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public sealed class PatrollingInfoUIController : SimulationInfoUIControllerBase<
public TextMeshProUGUI WorstGraphIdlenessText;
public TextMeshProUGUI AverageGraphIdlenessText;

public Button WaypointHeatMapButton;

protected override void AfterStart()
{
StoppingCriteriaToggle.onValueChanged.AddListener(delegate {
//TODO: when the stopping criteria is toggled
});

WaypointHeatMapButton.onClick.AddListener(() => {
ExecuteAndRememberMapVisualizationModification(sim => {
if (sim != null) {
sim.PatrollingTracker.ShowWaypointHeatMap();
}
});
});
}

protected override void NotifyNewSimulation(PatrollingSimulation newSimulation)
Expand Down Expand Up @@ -61,5 +70,7 @@ private void SetWorstGraphIdleness(float idleness) =>

private void SetAverageGraphIdleness(float idleness) =>
AverageGraphIdlenessText.text = $"Average graph idleness: {idleness} ticks";


}
}

0 comments on commit 4d792c0

Please sign in to comment.