public override Vector3 mapPointToPath(Vector3 point, mapReturnStruct tStruct)//Vector3 tangent, float outside)
        {
            float   d;
            float   minDistance = float.MaxValue;// FLT_MAX;
            Vector3 onPath      = Vector3.ZERO;

            // loop over all segments, find the one nearest to the given point
            for (int i = 1; i < pointCount; i++)
            {
                segmentLength = lengths[i];
                segmentNormal = normals[i];
                d             = pointToSegmentDistance(point, points[i - 1], points[i]);
                if (d < minDistance)
                {
                    minDistance     = d;
                    onPath          = chosen;
                    tStruct.tangent = segmentNormal;
                }
            }

            // measure how far original point is outside the Pathway's "tube"
            tStruct.outside = (onPath - point).Length - radius;//Vector3::distance (onPath, point) - radius;

            // return point on path
            return(onPath);
        }
        // ----------------------------------------------------------------------------
        // Path Following behaviors



        public Vector3 steerToStayOnPath(float predictionTime, Pathway path)
        {
            // predict our future position
            Vector3 futurePosition = predictFuturePosition(predictionTime);

            // find the point on the path nearest the predicted future position
            //Vector3 tangent;
            //float outside;

            mapReturnStruct tStruct = new mapReturnStruct();

            Vector3 onPath = path.mapPointToPath(futurePosition, tStruct);

            if (tStruct.outside < 0)
            {
                // our predicted future position was in the path,
                // return zero steering.
                return(Vector3.Zero);
            }
            else
            {
                // our predicted future position was outside the path, need to
                // steer towards it.  Use onPath projection of futurePosition
                // as seek target
                annotatePathFollowing(futurePosition, onPath, onPath, tStruct.outside);
                return(steerForSeek(onPath));
            }
        }
        // how far outside path tube is the given point?  (negative is inside)
        public float howFarOutsidePath(Vector3 point)
        {
            //float outside;
            //Vector3 tangent;
            mapReturnStruct tStruct = new mapReturnStruct();

            mapPointToPath(point, tStruct);//tangent, outside);
            return(tStruct.outside);
        }
        // is the given point inside the path tube?
        public bool isInsidePath(Vector3 point)
        {
            //float outside;
            //Vector3 tangent;
            mapReturnStruct tStruct = new mapReturnStruct();

            mapPointToPath(point, tStruct);//tangent, outside);
            return tStruct.outside < 0;
        }
        public Vector3 steerToFollowPath(int direction, float predictionTime, Pathway path)
        {
            // our goal will be offset from our path distance by this amount
            float pathDistanceOffset = direction * predictionTime * speed();

            // predict our future position
            Vector3 futurePosition = predictFuturePosition(predictionTime);

            // measure distance along path of our current and predicted positions
            float nowPathDistance =
                path.mapPointToPathDistance(Position);
            float futurePathDistance =
                path.mapPointToPathDistance(futurePosition);

            // are we facing in the correction direction?
            bool rightway = ((pathDistanceOffset > 0) ?
                             (nowPathDistance < futurePathDistance) :
                             (nowPathDistance > futurePathDistance));

            // find the point on the path nearest the predicted future position
            // XXX need to improve calling sequence, maybe change to return a
            // XXX special path-defined object which includes two Vector3s and a
            // XXX bool (onPath,tangent (ignored), withinPath)
            // Vector3 tangent;
            //float outside;
            mapReturnStruct tStruct = new mapReturnStruct();
            Vector3         onPath  = path.mapPointToPath(futurePosition, tStruct);

            // no steering is required if (a) our future position is inside
            // the path tube and (b) we are facing in the correct direction
            if ((tStruct.outside < 0) && rightway)
            {
                // all is well, return zero steering
                return(Vector3.Zero);
            }
            else
            {
                // otherwise we need to steer towards a target point obtained
                // by adding pathDistanceOffset to our current path position

                float   targetPathDistance = nowPathDistance + pathDistanceOffset;
                Vector3 target             = path.mapPathDistanceToPoint(targetPathDistance);

                annotatePathFollowing(futurePosition, onPath, target, tStruct.outside);

                // return steering to seek target on path
                return(steerForSeek(target));
            }
        }
 // Given an arbitrary point ("A"), returns the nearest point ("P") on
 // this path.  Also returns, via output arguments, the path tangent at
 // P and a measure of how far A is outside the Pathway's "tube".  Note
 // that a negative distance indicates A is inside the Pathway.
 public virtual Vector3 mapPointToPath(Vector3 point, mapReturnStruct tStruct)
 {
     return(Vector3.ZERO);
 }
 // Given an arbitrary point ("A"), returns the nearest point ("P") on
 // this path.  Also returns, via output arguments, the path tangent at
 // P and a measure of how far A is outside the Pathway's "tube".  Note
 // that a negative distance indicates A is inside the Pathway.
 public virtual Vector3 mapPointToPath(Vector3 point, mapReturnStruct tStruct)
 {
     return Vector3.ZERO;
 }
        //Vector3 tangent, float outside)
        public override Vector3 mapPointToPath(Vector3 point, mapReturnStruct tStruct)
        {
            float d;
            float minDistance = float.MaxValue;// FLT_MAX;
            Vector3 onPath=Vector3.ZERO;

            // loop over all segments, find the one nearest to the given point
            for (int i = 1; i < pointCount; i++)
            {
                segmentLength = lengths[i];
                segmentNormal = normals[i];
                d = pointToSegmentDistance (point, points[i-1], points[i]);
                if (d < minDistance)
                {
                    minDistance = d;
                    onPath = chosen;
                    tStruct.tangent = segmentNormal;
                }
            }

            // measure how far original point is outside the Pathway's "tube"
            tStruct.outside = (onPath - point).Length - radius;//Vector3::distance (onPath, point) - radius;

            // return point on path
            return onPath;
        }
        // ----------------------------------------------------------------------------
        // Path Following behaviors
        public Vector3 steerToStayOnPath(float predictionTime, Pathway path)
        {
            // predict our future position
             Vector3 futurePosition = predictFuturePosition (predictionTime);

            // find the point on the path nearest the predicted future position
            //Vector3 tangent;
            //float outside;

            mapReturnStruct tStruct = new mapReturnStruct();

            Vector3 onPath = path.mapPointToPath(futurePosition, tStruct);

            if (tStruct.outside < 0)
            {
                // our predicted future position was in the path,
                // return zero steering.
                return Vector3.ZERO;
            }
            else
            {
                // our predicted future position was outside the path, need to
                // steer towards it.  Use onPath projection of futurePosition
                // as seek target
                annotatePathFollowing (futurePosition, onPath, onPath,tStruct.outside);
                return steerForSeek (onPath);
            }
        }
        public Vector3 steerToFollowPath(int direction, float predictionTime, Pathway path)
        {
            // our goal will be offset from our path distance by this amount
             float pathDistanceOffset = direction * predictionTime * speed();

            // predict our future position
             Vector3 futurePosition = predictFuturePosition (predictionTime);

            // measure distance along path of our current and predicted positions
             float nowPathDistance =
                path.mapPointToPathDistance (Position);
             float futurePathDistance =
                path.mapPointToPathDistance (futurePosition);

            // are we facing in the correction direction?
             bool rightway = ((pathDistanceOffset > 0) ?
                                   (nowPathDistance < futurePathDistance) :
                                   (nowPathDistance > futurePathDistance));

            // find the point on the path nearest the predicted future position
            // XXX need to improve calling sequence, maybe change to return a
            // XXX special path-defined object which includes two Vector3s and a
            // XXX bool (onPath,tangent (ignored), withinPath)
               // Vector3 tangent;
            //float outside;
             mapReturnStruct tStruct = new mapReturnStruct();
             Vector3 onPath = path.mapPointToPath(futurePosition, tStruct);

            // no steering is required if (a) our future position is inside
            // the path tube and (b) we are facing in the correct direction
             if ((tStruct.outside < 0) && rightway)
            {
                // all is well, return zero steering
                return Vector3.ZERO;
            }
            else
            {
                // otherwise we need to steer towards a target point obtained
                // by adding pathDistanceOffset to our current path position

                float targetPathDistance = nowPathDistance + pathDistanceOffset;
                Vector3 target = path.mapPathDistanceToPoint (targetPathDistance);

                annotatePathFollowing(futurePosition, onPath, target, tStruct.outside);

                // return steering to seek target on path
                return steerForSeek (target);
            }
        }