Make Nodes readonly and track Node computation on PVS

This commit is contained in:
Jarrod Doyle 2025-01-28 17:35:10 +00:00
parent 77f69be219
commit b493c447c7
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 17 additions and 19 deletions

View File

@ -7,10 +7,9 @@ namespace KeepersCompound.Lightmapper;
public class PotentiallyVisibleSet public class PotentiallyVisibleSet
{ {
private struct Node(List<int> edgeIndices) private readonly struct Node(List<int> edgeIndices)
{ {
public bool VisibilityComputed = false; public readonly HashSet<int> VisibleNodes = [];
public HashSet<int> VisibleNodes = [];
public readonly List<int> EdgeIndices = edgeIndices; public readonly List<int> EdgeIndices = edgeIndices;
} }
@ -76,6 +75,7 @@ public class PotentiallyVisibleSet
} }
private readonly Node[] _graph; private readonly Node[] _graph;
private readonly bool[] _computedMap;
private readonly List<Edge> _edges; private readonly List<Edge> _edges;
private const float Epsilon = 0.1f; private const float Epsilon = 0.1f;
@ -99,11 +99,13 @@ public class PotentiallyVisibleSet
public PotentiallyVisibleSet(WorldRep.Cell[] cells) public PotentiallyVisibleSet(WorldRep.Cell[] cells)
{ {
_graph = new Node[cells.Length]; _graph = new Node[cells.Length];
_computedMap = new bool[cells.Length];
var portalCount = 0; var portalCount = 0;
foreach (var cell in cells) for (var i = 0; i < cells.Length; i++)
{ {
portalCount += cell.PortalPolyCount; _computedMap[i] = false;
portalCount += cells[i].PortalPolyCount;
} }
_edges = new List<Edge>(portalCount); _edges = new List<Edge>(portalCount);
@ -158,16 +160,14 @@ public class PotentiallyVisibleSet
{ {
// TODO: Handle out of range indices // TODO: Handle out of range indices
var node = _graph[cellIdx]; var node = _graph[cellIdx];
if (node.VisibilityComputed) if (_computedMap[cellIdx])
{ {
return [..node.VisibleNodes]; return [..node.VisibleNodes];
} }
var visibleCells = ComputeVisibility(cellIdx); ComputeVisibility(cellIdx);
node.VisibilityComputed = true;
node.VisibleNodes = visibleCells;
_graph[cellIdx] = node; _graph[cellIdx] = node;
return [..visibleCells]; return [.._graph[cellIdx].VisibleNodes];
} }
private void ComputeEdgeMightSee(Edge source) private void ComputeEdgeMightSee(Edge source)
@ -239,17 +239,15 @@ public class PotentiallyVisibleSet
} }
} }
private HashSet<int> ComputeVisibility(int cellIdx) public void ComputeVisibility(int cellIdx)
{ {
if (cellIdx >= _graph.Length) if (cellIdx >= _graph.Length)
{ {
return []; return;
} }
// A cell can always see itself, so we'll add that now // A cell can always see itself, so we'll add that now
var visible = new HashSet<int>(); _graph[cellIdx].VisibleNodes.Add(cellIdx);
visible.Add(cellIdx);
foreach (var edgeIdx in _graph[cellIdx].EdgeIndices) foreach (var edgeIdx in _graph[cellIdx].EdgeIndices)
{ {
var edge = _edges[edgeIdx]; var edge = _edges[edgeIdx];
@ -258,13 +256,13 @@ public class PotentiallyVisibleSet
{ {
if (edge.MightSee[i]) if (edge.MightSee[i])
{ {
visible.Add(i); _graph[cellIdx].VisibleNodes.Add(i);
} }
} }
} }
return visible; _computedMap[cellIdx] = true;
// if (cellIdx >= _portalGraph.Length) // if (cellIdx >= _portalGraph.Length)
// { // {
// return []; // return [];