NNInfoInternal GetNearestInternal(Vector3 position, NNConstraint constraint, bool fastCheck)
{
if (nodes == null)
{
return(new NNInfoInternal());
}
var iposition = (Int3)position;
if (optimizeForSparseGraph)
{
if (nearestNodeDistanceMode == NodeDistanceMode.Node)
{
return(new NNInfoInternal(lookupTree.GetNearest(iposition, fastCheck ? null : constraint)));
}
else
{
var closestNode = lookupTree.GetNearestConnection(iposition, fastCheck ? null : constraint, maximumConnectionLength);
if (closestNode == null)
{
return(new NNInfoInternal());
}
return(FindClosestConnectionPoint(closestNode as PointNode, position));
}
}
float maxDistSqr = constraint == null || constraint.constrainDistance ? AstarPath.active.maxNearestNodeDistanceSqr : float.PositiveInfinity;
maxDistSqr *= Int3.FloatPrecision * Int3.FloatPrecision;
var nnInfo = new NNInfoInternal(null);
long minDist = long.MaxValue;
long minConstDist = long.MaxValue;
for (int i = 0; i < nodeCount; i++)
{
PointNode node = nodes[i];
long dist = (iposition - node.position).sqrMagnitudeLong;
if (dist < minDist)
{
minDist = dist;
nnInfo.node = node;
}
if (dist < minConstDist && (float)dist < maxDistSqr && (constraint == null || constraint.Suitable(node)))
{
minConstDist = dist;
nnInfo.constrainedNode = node;
}
}
if (!fastCheck)
{
nnInfo.node = nnInfo.constrainedNode;
}
nnInfo.UpdateInfo();
return(nnInfo);
}