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
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);
        }
Ejemplo n.º 2
0
        NNInfoInternal GetNearestInternal(Vector3 position, NNConstraint constraint, bool fastCheck)
        {
            if (nodes == null)
            {
                return(new NNInfoInternal());
            }

            if (optimizeForSparseGraph)
            {
                return(new NNInfoInternal(lookupTree.GetNearest((VInt3)position, fastCheck ? null : constraint)));
            }

            float maxDistSqr = constraint == null || constraint.constrainDistance ? AstarPath.active.maxNearestNodeDistanceSqr : float.PositiveInfinity;

            var   nnInfo       = new NNInfoInternal(null);
            float minDist      = float.PositiveInfinity;
            float minConstDist = float.PositiveInfinity;

            for (int i = 0; i < nodeCount; i++)
            {
                PointNode node = nodes[i];
                float     dist = (position - (Vector3)node.position).sqrMagnitude;

                if (dist < minDist)
                {
                    minDist     = dist;
                    nnInfo.node = node;
                }

                if (dist < minConstDist && dist < maxDistSqr && (constraint == null || constraint.Suitable(node)))
                {
                    minConstDist           = dist;
                    nnInfo.constrainedNode = node;
                }
            }

            if (!fastCheck)
            {
                nnInfo.node = nnInfo.constrainedNode;
            }

            nnInfo.UpdateInfo();
            return(nnInfo);
        }