Search in sources :

Example 6 with Zone

use of me.wobblyyyy.pathfinder2.zones.Zone in project Pathfinder2 by Wobblyyyy.

the class TestLocalizedPathGen method testUnobstructedPath.

@Test
public void testUnobstructedPath() {
    List<Zone> zones = new ArrayList<>();
    LocalizedPathGen gen = new LocalizedPathGen(zones, 0.5, 0.5);
    PointXY start = new PointXY(0, 0);
    PointXY end = new PointXY(10, 10);
    List<PointXY> path = gen.getPath(start, end);
}
Also used : Zone(me.wobblyyyy.pathfinder2.zones.Zone) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 7 with Zone

use of me.wobblyyyy.pathfinder2.zones.Zone in project Pathfinder2 by Wobblyyyy.

the class TestNodeValidator method testNodeValidation.

@Test
public void testNodeValidation() {
    LocalizedGrid grid = new LocalizedGrid(Grid.generateGrid(10, 10), 0, 0, 10, 10);
    Rectangle blocker = new Rectangle(5, 0, 10, 10);
    Zone blockerZone = new Zone(blocker);
    List<Zone> zones = new ArrayList<Zone>(1) {

        {
            add(blockerZone);
        }
    };
    NodeValidator.validateNodes(grid, zones);
    Assertions.assertTrue(grid.getNode(new PointXY(0, 0)).isValid());
    Assertions.assertFalse(grid.getNode(new PointXY(6, 6)).isValid());
    Assertions.assertFalse(grid.getNode(new PointXY(5, 0)).isValid());
    Assertions.assertTrue(grid.getNode(new PointXY(3, 5)).isValid());
    Assertions.assertFalse(grid.getNode(new PointXY(7, 7)).isValid());
    Assertions.assertFalse(grid.getNode(new PointXY(8, 8)).isValid());
}
Also used : Zone(me.wobblyyyy.pathfinder2.zones.Zone) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY) Rectangle(me.wobblyyyy.pathfinder2.geometry.Rectangle) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 8 with Zone

use of me.wobblyyyy.pathfinder2.zones.Zone in project Pathfinder2 by Wobblyyyy.

the class TestPathOptimizer method testLinearOptimization.

@Test
public void testLinearOptimization() {
    List<Zone> zones = new ArrayList<>();
    LocalizedPathGen gen = new LocalizedPathGen(zones, 0.5, 0.5);
    PointXY start = new PointXY(0, 0);
    PointXY end = new PointXY(10, 10);
    List<PointXY> path = gen.getPath(start, end);
    path = PathOptimizer.optimize(path);
    Assertions.assertNotNull(path);
}
Also used : Zone(me.wobblyyyy.pathfinder2.zones.Zone) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 9 with Zone

use of me.wobblyyyy.pathfinder2.zones.Zone in project Pathfinder2 by Wobblyyyy.

the class TestPathOptimizer method testNonlinearOptimization.

@Test
public void testNonlinearOptimization() {
    Rectangle blockerShape = new Rectangle(5, 1, 6, 10);
    Zone blockerZone = new Zone(blockerShape);
    List<Zone> zones = new ArrayList<Zone>() {

        {
            add(blockerZone);
        }
    };
    LocalizedPathGen gen = new LocalizedPathGen(zones, 0.5, 0.5);
    PointXY start = new PointXY(0, 0);
    PointXY end = new PointXY(10, 10);
    List<PointXY> unoptimized = gen.getPath(start, end);
    List<PointXY> optimized = PathOptimizer.optimize(unoptimized);
    List<PointXY> overOptimized = PathOptimizer.optimize(optimized);
    Assertions.assertNotNull(unoptimized);
    Assertions.assertNotNull(optimized);
    Assertions.assertNotNull(overOptimized);
    int unoptimizedSize = unoptimized.size();
    int optimizedSize = optimized.size();
    int overOptimizedSize = overOptimized.size();
    double unoptimizedLength = PathOptimizer.determineLength(unoptimized);
    double optimizedLength = PathOptimizer.determineLength(optimized);
    double overOptimizedLength = PathOptimizer.determineLength(overOptimized);
// Assertions.assertEquals(30, unoptimizedSize);
// Assertions.assertEquals(16, optimizedSize);
// Assertions.assertEquals(16, overOptimizedSize);
// Assertions.assertTrue(Equals.soft(16.778, unoptimizedLength, 0.01));
// Assertions.assertEquals(optimizedLength, overOptimizedLength);
}
Also used : Zone(me.wobblyyyy.pathfinder2.zones.Zone) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY) Rectangle(me.wobblyyyy.pathfinder2.geometry.Rectangle) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 10 with Zone

use of me.wobblyyyy.pathfinder2.zones.Zone in project Pathfinder2 by Wobblyyyy.

the class ZoneProcessor method update.

/**
 * Based on the provided point, call the correct methods of each of
 * the zones.
 *
 * <ul>
 *     <li>
 *         If the robot has ENTERED the zone (previously, it was not in
 *         the zone, but now it is), the zone's {@link Zone#onEnter(Pathfinder)}
 *         method will be called.
 *     </li>
 *     <li>
 *         If the robot has EXITED the zone (previously, it was in the zone,
 *         but now it is not), the zone's {@link Zone#onExit(Pathfinder)}
 *         method will be called.
 *     </li>
 *     <li>
 *         If the robot is INSIDE the zone (this will be activated every
 *         time the {@code onEnter} method is called, as well as whenever
 *         the robot is inside the zone), the zone's
 *         {@link Zone#whileInside(Pathfinder)} method will be called.
 *     </li>
 * </ul>
 *
 * @param pathfinder the instance of Pathfinder.
 */
public void update(Pathfinder pathfinder) {
    if (zones.size() == 0)
        return;
    PathfinderPluginManager manager = pathfinder.getPluginManager();
    List<Zone> lastZones = currentZones;
    currentZones = getContainingZones(pathfinder.getPosition());
    List<Zone> enteredZones = getEnteredZones(lastZones, currentZones);
    List<Zone> exitedZones = getExitedZones(lastZones, currentZones);
    for (Zone zone : enteredZones) {
        zone.onEnter(pathfinder);
        manager.onEnterZone(pathfinder, zone);
    }
    for (Zone zone : currentZones) {
        zone.whileInside(pathfinder);
        manager.whileInsideZone(pathfinder, zone);
    }
    for (Zone zone : exitedZones) {
        zone.onExit(pathfinder);
        manager.onExitZone(pathfinder, zone);
    }
}
Also used : PathfinderPluginManager(me.wobblyyyy.pathfinder2.plugin.PathfinderPluginManager)

Aggregations

ArrayList (java.util.ArrayList)9 PointXY (me.wobblyyyy.pathfinder2.geometry.PointXY)9 Zone (me.wobblyyyy.pathfinder2.zones.Zone)9 Test (org.junit.jupiter.api.Test)8 Rectangle (me.wobblyyyy.pathfinder2.geometry.Rectangle)6 PathfinderPluginManager (me.wobblyyyy.pathfinder2.plugin.PathfinderPluginManager)1