/// <summary>
/// Calculates connections for all nodes in the graph.
/// This is an IEnumerable, you can iterate through it using e.g foreach to get progress information.
/// </summary>
IEnumerable <Progress> ConnectNodesAsync()
{
if (maxDistance >= 0)
{
// To avoid too many allocations, these lists are reused for each node
var connections = new List <Connection>();
var candidateConnections = new List <GraphNode>();
long maxSquaredRange;
// Max possible squared length of a connection between two nodes
// This is used to speed up the calculations by skipping a lot of nodes that do not need to be checked
if (maxDistance == 0 && (limits.x == 0 || limits.y == 0 || limits.z == 0))
{
maxSquaredRange = long.MaxValue;
}
else
{
maxSquaredRange = (long)(Mathf.Max(limits.x, Mathf.Max(limits.y, Mathf.Max(limits.z, maxDistance))) * Int3.Precision) + 1;
maxSquaredRange *= maxSquaredRange;
}
// Report progress every N nodes
const int YieldEveryNNodes = 512;
// Loop through all nodes and add connections to other nodes
for (int i = 0; i < nodeCount; i++)
{
if (i % YieldEveryNNodes == 0)
{
yield return(new Progress(i / (float)nodeCount, "Connecting nodes"));
}
connections.Clear();
var node = nodes[i];
if (optimizeForSparseGraph)
{
candidateConnections.Clear();
lookupTree.GetInRange(node.position, maxSquaredRange, candidateConnections);
for (int j = 0; j < candidateConnections.Count; j++)
{
var other = candidateConnections[j] as PointNode;
float dist;
if (other != node && IsValidConnection(node, other, out dist))
{
connections.Add(new Connection(
other,
/// <summary>TODO: Is this equal to .costMagnitude</summary>
(uint)Mathf.RoundToInt(dist * Int3.FloatPrecision)
));
}
}
}
else
{
// Only brute force is available in the free version
for (int j = 0; j < nodeCount; j++)
{
if (i == j)
{
continue;
}
PointNode other = nodes[j];
float dist;
if (IsValidConnection(node, other, out dist))
{
connections.Add(new Connection(
other,
/// <summary>TODO: Is this equal to .costMagnitude</summary>
(uint)Mathf.RoundToInt(dist * Int3.FloatPrecision)
));
}
}
}
node.connections = connections.ToArray();
node.SetConnectivityDirty();
}
}
}