Pathfinding.PointKDTree.GetNearest C# (CSharp) Method

GetNearest() public method

Closest node to the point which satisfies the constraint
public GetNearest ( Int3 point, NNConstraint constraint ) : GraphNode
point Int3
constraint NNConstraint
return GraphNode
		public GraphNode GetNearest (Int3 point, NNConstraint constraint) {
			GraphNode best = null;
			long bestSqrDist = long.MaxValue;

			GetNearestInternal(1, point, constraint, ref best, ref bestSqrDist);
			return best;
		}

Usage Example

Ejemplo n.º 1
0
        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);
        }
All Usage Examples Of Pathfinding.PointKDTree::GetNearest