Commit 12d985cf authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add getNextFreePoints to SpawnArray

parent f761199f
package org.vadere.state.util; package org.vadere.state.util;
import org.lwjgl.system.CallbackI;
import org.vadere.state.scenario.DynamicElement; import org.vadere.state.scenario.DynamicElement;
import org.vadere.util.geometry.shapes.VPoint; import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle; import org.vadere.util.geometry.shapes.VRectangle;
import sun.java2d.pipe.ValidatePipe;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -95,6 +98,26 @@ public class SpawnArray { ...@@ -95,6 +98,26 @@ public class SpawnArray {
return null; return null;
} }
/**
* @param neighbours Test against this List. Caller must ensure that this neighbours are in the
* vicinity of the source bound.
* @return This function returns as many free points as possible. Caller must check how many
* points where found.
*/
public LinkedList<VPoint> getNextFreePoints(int maxPoints, final List<DynamicElement> neighbours) {
double d = getMaxElementDim() / 2; // radius.
LinkedList<VPoint> points = new LinkedList<>();
for (VPoint p : spawnPoints) {
boolean overlap = neighbours.parallelStream().anyMatch(n -> ((n.getShape().distance(p) < d) || n.getShape().contains(p)));
if (!overlap) {
points.add(p);
if (points.size() == maxPoints) {
break;
}
}
}
return points;
}
/** /**
* This function only spawns non overlapping groups. This means that for instance within a * This function only spawns non overlapping groups. This means that for instance within a
......
...@@ -76,6 +76,43 @@ public class SpawnArrayTest { ...@@ -76,6 +76,43 @@ public class SpawnArrayTest {
} }
@Test
public void testFreeSpawns() {
source = new VRectangle(1.0, 1.0, 4.0, 4.0);
elementBound = new VRectangle(0.0, 0.0, 1.0, 1.0);
spawnArray = new SpawnArray(source, elementBound);
assertEquals("Number of spawn points does not match", 16, spawnArray.getSpawnPoints().length);
VPoint[] spawnPoints = spawnArray.getSpawnPoints();
List<DynamicElement> dynamicElements = createMock(0.5,
spawnPoints[0],
spawnPoints[1],
spawnPoints[3],
spawnPoints[4]);
LinkedList<VPoint> points = spawnArray.getNextFreePoints(4, dynamicElements);
assertEquals(4, points.size());
assertEquals(spawnPoints[2], points.pollFirst());
assertEquals(spawnPoints[5], points.pollFirst());
assertEquals(spawnPoints[6], points.pollFirst());
assertEquals(spawnPoints[7], points.pollFirst());
source = new VRectangle(1.0, 1.0, 2.0, 2.0);
elementBound = new VRectangle(0.0, 0.0, 1.0, 1.0);
spawnArray = new SpawnArray(source, elementBound);
assertEquals("Number of spawn points does not match", 4, spawnArray.getSpawnPoints().length);
spawnPoints = spawnArray.getSpawnPoints();
dynamicElements = createMock(0.5,
spawnPoints[0],
spawnPoints[1],
spawnPoints[3]);
points = spawnArray.getNextFreePoints(4, dynamicElements);
assertEquals(1, points.size());
assertEquals(spawnPoints[2], points.pollFirst());
}
@Test @Test
public void testGroupSpawnMatchDims() { public void testGroupSpawnMatchDims() {
source = new VRectangle(1.0, 1.0, 8.0, 8.0); source = new VRectangle(1.0, 1.0, 8.0, 8.0);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment