cz.cuni.amis.pogamut.base.utils.math
Class DistanceUtils

Package class diagram package DistanceUtils
java.lang.Object
  extended by cz.cuni.amis.pogamut.base.utils.math.DistanceUtils

public class DistanceUtils
extends Object

DistanceUtils consists of usual routines for selecting "X" from some "collections of Xes" that are in some "distance" relation to the "target", e.g., "get nearest weapon from collection of available weapons to my position".

Note that you may always use custom metric via DistanceUtils.IGetDistance interface.

Note that you may always use some custom filters via DistanceUtils.IDistanceFilter interface.

Author:
Jimmy, ik

Nested Class Summary
static class DistanceUtils.AcceptAllDistanceFilter<T>
          Filter that accepts all "objects" (does not filter anything out).
static class DistanceUtils.FilterAdapter<T>
          Adapter that wraps IFilter making it into DistanceUtils.IDistanceFilter.
static class DistanceUtils.GetLocatedDistance2D<T extends ILocated>
          Simple implementation of DistanceUtils.IGetDistance that uses Location.getDistance2D(Location) method.
static class DistanceUtils.GetLocatedDistance3D<T extends ILocated>
          Simple implementation of DistanceUtils.IGetDistance that uses Location.getDistance(Location) method.
static interface DistanceUtils.IDistanceFilter<T>
          Filter that allows to check whether "object" is accepted with respect to "distanceToTarget" for given "target".
static interface DistanceUtils.IGetDistance<T>
          Distance estimator between object of types T and some Location target.
static class DistanceUtils.RangeDistanceFilter<T>
          Filter that accepts all "objects" that are within range of min/max distance (inclusive).
static class DistanceUtils.VisibleFilter<T extends IViewable>
          Accepts only VISIBLE (IViewable.isVisible() == TRUE) objects.
 
Field Summary
static DistanceUtils.AcceptAllDistanceFilter acceptAllDistanceFilter
          See DistanceUtils.AcceptAllDistanceFilter.
static DistanceUtils.GetLocatedDistance2D<ILocated> getLocatedDistance2D
          See DistanceUtils.GetLocatedDistance2D.
static DistanceUtils.GetLocatedDistance3D<ILocated> getLocatedDistance3D
          See DistanceUtils.GetLocatedDistance3D.
static DistanceUtils.VisibleFilter<IViewable> visibleFilter
          See DistanceUtils.VisibleFilter.
 
Constructor Summary
DistanceUtils()
           
 
Method Summary
static
<T extends ILocated>
List<T>
getDistanceSorted(Collection<T> locations, ILocated target)
          Returns "locations" sorted according to the distance to "target".
static
<T extends ILocated>
List<T>
getDistanceSorted(Collection<T> locations, ILocated target, DistanceUtils.IDistanceFilter... filters)
          Returns "locations" accepted by all "filters" sorted according to the distance to "target".
static
<T> List<T>
getDistanceSorted(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance)
          Returns "locations" sorted according to the distance to "target".
static
<T> List<T>
getDistanceSorted(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance, DistanceUtils.IDistanceFilter... filters)
          Returns "locations" accepted by all "filters" sorted according to the distance to "target".
static
<T extends ILocated>
List<T>
getDistanceSorted(Collection<T> locations, ILocated target, double maxDistance)
          Returns "locations" sorted according to the distance to "target".
static
<T extends ILocated>
List<T>
getDistanceSortedFiltered(Collection<T> locations, ILocated target, cz.cuni.amis.utils.IFilter filter)
          Returns "locations" sorted according to the distance to "target".
static
<T extends IViewable>
List<T>
getDistanceSortedVisible(Collection<T> locations, ILocated target)
          Returns visible "locations" sorted according to the distance to "target".
static
<T extends ILocated>
T
getNearest(Collection<T> locations, ILocated target)
          Returns the nearest object to 'target'.
static
<T extends ILocated>
T
getNearest(Collection<T> locations, ILocated target, DistanceUtils.IDistanceFilter... filters)
          Returns the nearest object to 'target' that is accepted by all 'filters'.
static
<T> T
getNearest(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance)
          Returns the nearest object to 'target'.
static
<T> T
getNearest(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance, DistanceUtils.IDistanceFilter... filters)
          Returns the nearest object to 'target' that is accepted by all 'filters'.
static
<T extends ILocated>
T
getNearest(Collection<T> locations, ILocated target, double maxDistance)
          Returns the nearest object to 'target' that is not further than 'maxDistance'.
static
<T extends ILocated>
T
getNearest2D(Collection<T> locations, ILocated target)
          Returns the nearest (in 2D) object to 'target'.
static
<T extends ILocated>
T
getNearest2D(Collection<T> locations, ILocated target, DistanceUtils.IDistanceFilter... filters)
          Returns the nearest (in 2D) object to 'target' that is accepted by all 'filters'.
static
<T extends ILocated>
T
getNearest2D(Collection<T> locations, ILocated target, double maxDistance)
          Returns the nearest (in 2D) object to 'target' that is not further than 'maxDistance'.
static
<T extends ILocated>
T
getNearest2DFiltered(Collection<T> locations, ILocated target, cz.cuni.amis.utils.IFilter<T> filter)
          Returns the nearest (in 2D) object to 'target' that is accepted by filter.
static
<T extends IViewable>
T
getNearest2DVisible(Collection<T> locations, ILocated target)
          Returns the nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).
static
<T extends ILocated>
T
getNearestFiltered(Collection<T> locations, ILocated target, cz.cuni.amis.utils.IFilter filter)
          Returns the nearest object to 'target' that is accepted by filter.
static
<T extends IViewable>
T
getNearestVisible(Collection<T> locations, ILocated target)
          Returns the nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).
static
<T extends ILocated>
T
getSecondNearest(Collection<T> locations, ILocated target)
          Returns the second nearest object to 'target'.
static
<T extends ILocated>
T
getSecondNearest(Collection<T> locations, ILocated target, DistanceUtils.IDistanceFilter... filters)
          Returns the second nearest object to 'target' that is accepted by all 'filters'.
static
<T> T
getSecondNearest(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance)
          Returns the second nearest object to 'target'.
static
<T> T
getSecondNearest(Collection<T> locations, ILocated target, DistanceUtils.IGetDistance getDistance, DistanceUtils.IDistanceFilter... filters)
          Returns the second nearest object to 'target'.
static
<T extends ILocated>
T
getSecondNearest(Collection<T> locations, ILocated target, double maxDistance)
          Returns the second nearest object to 'target' that is not further than 'maxDistance'.
static
<T extends ILocated>
T
getSecondNearest2D(Collection<T> locations, ILocated target)
          Returns the second nearest (in 2D) object to 'target'.
static
<T extends ILocated>
T
getSecondNearest2D(Collection<T> locations, ILocated target, DistanceUtils.IDistanceFilter... filters)
          Returns the second nearest (in 2D) object to 'target' that is accepted by all 'filters'.
static
<T extends ILocated>
T
getSecondNearest2D(Collection<T> locations, ILocated target, double maxDistance)
          Returns the second nearest (in 2D) object to 'target' that is not further than 'maxDistance'.
static
<T extends ILocated>
T
getSecondNearest2DFiltered(Collection<T> locations, ILocated target, cz.cuni.amis.utils.IFilter<T> filter)
          Returns the second nearest (in 2D) object to 'target' that is accepted by filter.
static
<T extends IViewable>
T
getSecondNearest2DVisible(Collection<T> locations, ILocated target)
          Returns the second nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).
static
<T extends ILocated>
T
getSecondNearestFiltered(Collection<T> locations, ILocated target, cz.cuni.amis.utils.IFilter filter)
          Returns the second nearest object to 'target' that is accepted by filter.
static
<T extends IViewable>
T
getSecondNearestVisible(Collection<T> locations, ILocated target)
          Returns the second nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

getLocatedDistance3D

public static final DistanceUtils.GetLocatedDistance3D<ILocated> getLocatedDistance3D
See DistanceUtils.GetLocatedDistance3D.


getLocatedDistance2D

public static final DistanceUtils.GetLocatedDistance2D<ILocated> getLocatedDistance2D
See DistanceUtils.GetLocatedDistance2D.


acceptAllDistanceFilter

public static final DistanceUtils.AcceptAllDistanceFilter acceptAllDistanceFilter
See DistanceUtils.AcceptAllDistanceFilter.


visibleFilter

public static final DistanceUtils.VisibleFilter<IViewable> visibleFilter
See DistanceUtils.VisibleFilter.

Constructor Detail

DistanceUtils

public DistanceUtils()
Method Detail

getNearest

public static <T> T getNearest(Collection<T> locations,
                               ILocated target,
                               DistanceUtils.IGetDistance getDistance)
Returns the nearest object to 'target'.

Distance is obtained via provided IGetDistance#getDistance(Object, Location).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
getDistance - distance computer between 'locations' and 'target'.
Returns:
nearest object from collection of objects

getNearest

public static <T> T getNearest(Collection<T> locations,
                               ILocated target,
                               DistanceUtils.IGetDistance getDistance,
                               DistanceUtils.IDistanceFilter... filters)
Returns the nearest object to 'target' that is accepted by all 'filters'.

Distance is obtained via provided IGetDistance#getDistance(Object, Location).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
getDistance - distance computer between 'locations' and 'target'.
filters - filters to be used (that can filter out unsuitable results)
Returns:
nearest object from collection of objects

getNearest

public static <T extends ILocated> T getNearest(Collection<T> locations,
                                                ILocated target,
                                                DistanceUtils.IDistanceFilter... filters)
Returns the nearest object to 'target' that is accepted by all 'filters'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filters -
Returns:
nearest object from collection of objects

getNearest

public static <T extends ILocated> T getNearest(Collection<T> locations,
                                                ILocated target)
Returns the nearest object to 'target'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
nearest object from collection of objects

getNearest

public static <T extends ILocated> T getNearest(Collection<T> locations,
                                                ILocated target,
                                                double maxDistance)
Returns the nearest object to 'target' that is not further than 'maxDistance'.

Using DistanceUtils.RangeDistanceFilter (minDistance = 0, maxDistance is provided).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
maxDistance -
Returns:
nearest object from collection of objects that is not further than 'maxDistance'.

getNearestFiltered

public static <T extends ILocated> T getNearestFiltered(Collection<T> locations,
                                                        ILocated target,
                                                        cz.cuni.amis.utils.IFilter filter)
Returns the nearest object to 'target' that is accepted by filter.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filter - if null behave as if ALL locations are accepted
Returns:
nearest object from collection of objects

getNearestVisible

public static <T extends IViewable> T getNearestVisible(Collection<T> locations,
                                                        ILocated target)
Returns the nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations - must be objects implementing IViewable as well as ILocated (so Item or Player is usable)
target -
Returns:
nearest visible object from collection of objects

getDistanceSorted

public static <T> List<T> getDistanceSorted(Collection<T> locations,
                                            ILocated target,
                                            DistanceUtils.IGetDistance getDistance)
Returns "locations" sorted according to the distance to "target". Sorted from the nearest to the farthest.

Distance is provided by IGetDistance#getDistance(Object, Location).

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
getDistance -
Returns:

getDistanceSorted

public static <T> List<T> getDistanceSorted(Collection<T> locations,
                                            ILocated target,
                                            DistanceUtils.IGetDistance getDistance,
                                            DistanceUtils.IDistanceFilter... filters)
Returns "locations" accepted by all "filters" sorted according to the distance to "target". Sorted from the nearest to the farthest.

Distance is provided by IGetDistance#getDistance(Object, Location).

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
getDistance -
Returns:

getDistanceSorted

public static <T extends ILocated> List<T> getDistanceSorted(Collection<T> locations,
                                                             ILocated target,
                                                             DistanceUtils.IDistanceFilter... filters)
Returns "locations" accepted by all "filters" sorted according to the distance to "target". Sorted from the nearest to the farthest.

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filters -
Returns:
nearest object from collection of objects

getDistanceSorted

public static <T extends ILocated> List<T> getDistanceSorted(Collection<T> locations,
                                                             ILocated target)
Returns "locations" sorted according to the distance to "target". Sorted from the nearest to the farthest.

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
nearest object from collection of objects

getDistanceSorted

public static <T extends ILocated> List<T> getDistanceSorted(Collection<T> locations,
                                                             ILocated target,
                                                             double maxDistance)
Returns "locations" sorted according to the distance to "target". Sorted from the nearest to the farthest.

Using DistanceUtils.RangeDistanceFilter (minDistance = 0, maxDistance is provided).

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
maxDistance -
Returns:
nearest object from collection of objects that is not further than 'maxDistance'.

getDistanceSortedFiltered

public static <T extends ILocated> List<T> getDistanceSortedFiltered(Collection<T> locations,
                                                                     ILocated target,
                                                                     cz.cuni.amis.utils.IFilter filter)
Returns "locations" sorted according to the distance to "target". Sorted from the nearest to the farthest.

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filter - if null behave as if ALL locations are accepted
Returns:
nearest object from collection of objects

getDistanceSortedVisible

public static <T extends IViewable> List<T> getDistanceSortedVisible(Collection<T> locations,
                                                                     ILocated target)
Returns visible "locations" sorted according to the distance to "target". Sorted from the nearest to the farthest.

WARNING: 2*O(n) + O(n*log n) complexity!

Type Parameters:
T -
Parameters:
locations - must be objects implementing IViewable as well as ILocated (so Item or Player is usable)
target -
Returns:
nearest visible object from collection of objects

getSecondNearest

public static <T> T getSecondNearest(Collection<T> locations,
                                     ILocated target,
                                     DistanceUtils.IGetDistance getDistance)
Returns the second nearest object to 'target'.

Distance is provided by DistanceUtils.IGetDistance.getDistance(Object, ILocated).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
nearest object from collection of objects

getSecondNearest

public static <T> T getSecondNearest(Collection<T> locations,
                                     ILocated target,
                                     DistanceUtils.IGetDistance getDistance,
                                     DistanceUtils.IDistanceFilter... filters)
Returns the second nearest object to 'target'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
nearest object from collection of objects

getSecondNearest

public static <T extends ILocated> T getSecondNearest(Collection<T> locations,
                                                      ILocated target,
                                                      DistanceUtils.IDistanceFilter... filters)
Returns the second nearest object to 'target' that is accepted by all 'filters'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filters -
Returns:
nearest object from collection of objects

getSecondNearest

public static <T extends ILocated> T getSecondNearest(Collection<T> locations,
                                                      ILocated target)
Returns the second nearest object to 'target'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
second nearest object from collection of objects

getSecondNearest

public static <T extends ILocated> T getSecondNearest(Collection<T> locations,
                                                      ILocated target,
                                                      double maxDistance)
Returns the second nearest object to 'target' that is not further than 'maxDistance'.

Using DistanceUtils.RangeDistanceFilter (minDistance = 0, maxDistance is provided).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
maxDistance -
Returns:
second nearest object from collection of objects that is not further than 'maxDistance'.

getSecondNearestFiltered

public static <T extends ILocated> T getSecondNearestFiltered(Collection<T> locations,
                                                              ILocated target,
                                                              cz.cuni.amis.utils.IFilter filter)
Returns the second nearest object to 'target' that is accepted by filter.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filter - if null behave as if ALL locations are accepted
Returns:
second nearest object from collection of objects

getSecondNearestVisible

public static <T extends IViewable> T getSecondNearestVisible(Collection<T> locations,
                                                              ILocated target)
Returns the second nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations - must be objects implementing IViewable as well as ILocated (so Item or Player is usable)
target -
Returns:
second nearest visible object from collection of objects

getNearest2D

public static <T extends ILocated> T getNearest2D(Collection<T> locations,
                                                  ILocated target,
                                                  DistanceUtils.IDistanceFilter... filters)
Returns the nearest (in 2D) object to 'target' that is accepted by all 'filters'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filters -
Returns:
nearest (in 2D) object from collection of objects

getNearest2D

public static <T extends ILocated> T getNearest2D(Collection<T> locations,
                                                  ILocated target)
Returns the nearest (in 2D) object to 'target'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
nearest (in 2D) object from collection of objects

getNearest2D

public static <T extends ILocated> T getNearest2D(Collection<T> locations,
                                                  ILocated target,
                                                  double maxDistance)
Returns the nearest (in 2D) object to 'target' that is not further than 'maxDistance'.

Using DistanceUtils.RangeDistanceFilter (minDistance = 0, maxDistance is provided).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
maxDistance -
Returns:
nearest (in 2D) object from collection of objects that is not further than 'maxDistance'.

getNearest2DFiltered

public static <T extends ILocated> T getNearest2DFiltered(Collection<T> locations,
                                                          ILocated target,
                                                          cz.cuni.amis.utils.IFilter<T> filter)
Returns the nearest (in 2D) object to 'target' that is accepted by filter.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filter - if null behave as if ALL locations are accepted
Returns:
nearest (in 2D) object from collection of objects

getNearest2DVisible

public static <T extends IViewable> T getNearest2DVisible(Collection<T> locations,
                                                          ILocated target)
Returns the nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations - must be objects implementing IViewable as well as ILocated (so Item or Player is usable)
target -
Returns:
nearest (in 2D) visible object from collection of objects

getSecondNearest2D

public static <T extends ILocated> T getSecondNearest2D(Collection<T> locations,
                                                        ILocated target,
                                                        DistanceUtils.IDistanceFilter... filters)
Returns the second nearest (in 2D) object to 'target' that is accepted by all 'filters'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filters -
Returns:
second nearest (in 2D) object from collection of objects

getSecondNearest2D

public static <T extends ILocated> T getSecondNearest2D(Collection<T> locations,
                                                        ILocated target)
Returns the second nearest (in 2D) object to 'target'.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
Returns:
second nearest (in 2D) object from collection of objects

getSecondNearest2D

public static <T extends ILocated> T getSecondNearest2D(Collection<T> locations,
                                                        ILocated target,
                                                        double maxDistance)
Returns the second nearest (in 2D) object to 'target' that is not further than 'maxDistance'.

Using DistanceUtils.RangeDistanceFilter (minDistance = 0, maxDistance is provided).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
maxDistance -
Returns:
second nearest (in 2D) object from collection of objects that is not further than 'maxDistance'.

getSecondNearest2DFiltered

public static <T extends ILocated> T getSecondNearest2DFiltered(Collection<T> locations,
                                                                ILocated target,
                                                                cz.cuni.amis.utils.IFilter<T> filter)
Returns the second nearest (in 2D) object to 'target' that is accepted by filter.

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations -
target -
filter - if null behave as if ALL locations are accepted
Returns:
second nearest (in 2D) object from collection of objects

getSecondNearest2DVisible

public static <T extends IViewable> T getSecondNearest2DVisible(Collection<T> locations,
                                                                ILocated target)
Returns the second nearest object to 'target' that is visible (using DistanceUtils.VisibleFilter).

WARNING: O(n) complexity!

Type Parameters:
T -
Parameters:
locations - must be objects implementing IViewable as well as ILocated (so Item or Player is usable)
target -
Returns:
second nearest (in 2D) visible object from collection of objects


Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.