Commit f97329c7 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

add missing model tests again :(, dont know why I have deleted them.

parent 50f9c592
Pipeline #67561 failed with stages
in 53 minutes and 30 seconds
......@@ -4,10 +4,10 @@ import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.vadere.util.data.Row;
import org.vadere.util.data.Table;
import org.vadere.util.data.Tripel;
import org.vadere.util.data.Tupel;
/**
* This class convert a stream of table lines {@link java.util.stream.Stream<String>} into a
......@@ -55,10 +55,10 @@ public class TableReader {
long limit = headerFormat.chars().filter(c -> c == '%').count();
return Stream
.iterate(Tupel.of(headerFormat, line.get()), t -> nextResidual(t))
.iterate(Pair.of(headerFormat, line.get()), t -> nextResidual(t))
.limit(limit)
.filter(t -> t.isPresent())
.map(t -> toValue(t.v1, t.v2).toString()).toArray(String[]::new);
.filter(pair -> pair.getValue() != null)
.map(pair -> toValue(pair.getLeft(), pair.getRight()).toString()).toArray(String[]::new);
}
return headline;
}
......@@ -74,9 +74,9 @@ public class TableReader {
format = Arrays.stream(line.split(" ")).reduce("", (s1, s2) -> s1.concat("%s ")).trim();
}
Row row = Stream
.iterate(Tripel.of(0, format, line), t -> nextResidual(t))
.iterate(Triple.of(0, format, line), t -> nextResidual(t))
.limit(variables.length)
.map(tripel -> toColumn(variables[tripel.v1], tripel.v2, tripel.v3))
.map(tripel -> toColumn(variables[tripel.getLeft()], tripel.getMiddle(), tripel.getRight()))
.reduce(new Row(), (r1, r2) -> r1.combine(r2));
return row;
}
......@@ -105,37 +105,37 @@ public class TableReader {
return row;
}
private Tripel<Integer, String, String> nextResidual(final Tripel<Integer, String, String> tripel) {
Integer varCount = tripel.v1;
Tupel<String, String> tupel = nextResidual(Tupel.of(tripel.v2, tripel.v3));
private Triple<Integer, String, String> nextResidual(final Triple<Integer, String, String> tripel) {
Integer varCount = tripel.getLeft();
Pair<String, String> tupel = nextResidual(Pair.of(tripel.getMiddle(), tripel.getRight()));
return tupel.isPresent() ? Tripel.of(varCount + 1, tupel.v1, tupel.v2) : Tripel.empty();
return tupel.getValue() != null ? Triple.of(varCount + 1, tupel.getLeft(), tupel.getRight()) : Triple.of(null, null, null);
}
private Tupel<String, String> nextResidual(final Tupel<String, String> tupel) {
if (!tupel.isPresent()) {
return tupel;
private Pair<String, String> nextResidual(final Pair<String, String> pair) {
if (pair.getValue() == null) {
return pair;
}
String residualFormat = tupel.v1;
String residualLine = tupel.v2;
String residualFormat = pair.getLeft();
String residualLine = pair.getRight();
int first = residualFormat.indexOf('%');
residualFormat = residualFormat.substring(first + TYPE_PATTERN_LENGTH);
first = residualFormat.indexOf('%');
Tupel<String, String> resultTupel;
Pair<String, String> resultPair;
if (first != -1) {
String seperator = residualFormat.substring(0, first);
residualFormat = residualFormat.substring(residualFormat.indexOf('%'));
residualLine = residualLine.substring(residualLine.indexOf(seperator) + seperator.length());
resultTupel = Tupel.of(residualFormat, residualLine);
resultPair = Pair.of(residualFormat, residualLine);
} else {
resultTupel = Tupel.empty();
resultPair = Pair.of(null, null);
}
return resultTupel;
return resultPair;
}
/**
......
......@@ -65,7 +65,7 @@ public class FloorDiscretizer {
/**
* Similar to
* {@link #setGridDistanceValuesForShape(CellGrid, VShape, PathFindingTag, PathFindingTag)},
* {@link #setGridValuesForShapeCentered(CellGrid, VShape, CellState)},
* but sets the values considering the CENTER of the cells, not the left lower corner.
*/
public static void setGridValuesForShapeCentered(CellGrid floorGrid,
......@@ -103,61 +103,6 @@ public class FloorDiscretizer {
}
}
/**
* Computes the distance to the given shape of all contained plus directly adjacent points.
* A point is considered adjacent if one of its eight neighbors is contained in the shape.
*
* @deprecated not used by any function but its test. The reason for this function was that the
* target shapes were not correctly rendered on the grid. They are now, in the
* FastMarching algorithm itself.
*/
public static void setGridDistanceValuesForShape(CellGrid floorGrid, VShape elementShape, PathFindingTag tagInside,
PathFindingTag tagOutside) {
// Axis aligned bounds of the given body.
Rectangle2D bodyBounds = elementShape.getBounds2D();
// Nearest points on the grid of the left lower and right upper corners
// of the bounds of the body. These represent the outer bounds of the
// grid points to write.
Point pointLeftLower, pointRightUpper;
double eps = GeometryUtils.DOUBLE_EPS;
// Compute nearest grid point of the lower left corner of bodyBounds.
pointLeftLower = floorGrid.getNearestPoint(bodyBounds.getMinX() - eps,
bodyBounds.getMinY() - eps);
// Compute nearest grid point of the right upper corner of bodyBounds.
pointRightUpper = floorGrid.getNearestPoint(bodyBounds.getMaxX() + eps,
bodyBounds.getMaxY() + eps);
// Run through all grid points of bounds and set its value if being
// covered by the bodies footprint.
for (int x = pointLeftLower.x - 1; x <= pointRightUpper.x + 1; ++x) {
for (int y = pointLeftLower.y - 1; y <= pointRightUpper.y + 1; ++y) {
// Convert the grid point to the bodies coordinate system and
// verify if lies within the bodies shape.
boolean isCloseToShape = false;
for (int xx = x - 1; xx <= x + 1; xx++) {
for (int yy = y - 1; yy <= y + 1; yy++) {
if (elementShape.contains(floorGrid.pointToCoord(xx, yy))) {
isCloseToShape = true;
}
}
}
if (isCloseToShape && floorGrid.isValidPoint(new Point(x, y))) {
double dist = elementShape.distance(new VPoint(x, y));
if (dist < eps) {
floorGrid.setValue(x, y, new CellState(0.0, tagInside));
} else {
floorGrid.setValue(x, y, new CellState(dist, tagOutside));
}
}
}
}
}
/**
* Returns the sampling points of the given grid which are covered by the
* footprint of the body.
......
package org.vadere.util.potential;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.potential.CellGrid;
import org.vadere.util.potential.CellState;
import org.vadere.util.potential.FloorDiscretizer;
import org.vadere.util.potential.PathFindingTag;
public class TestFloorDiscretizer {
private VRectangle shapeToDiscretize;
private double gridWidth = 2;
private double gridHeight = 2;
private double gridResolution = 1;
private CellGrid cellGrid;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
cellGrid = new CellGrid(gridWidth, gridHeight, gridResolution, new CellState(0.0, PathFindingTag.Undefined));
}
/**
* Test method for
* {@link org.vadere.util.potential.FloorDiscretizer#setGridDistanceValuesForShape(CellGrid, org.vadere.util.geometry.shapes.VShape, PathFindingTag)}.
*/
@Test
public void testSetGridDistanceValuesForShape_halfGrid() {
shapeToDiscretize = new VRectangle(0, 0, gridWidth, gridHeight / 2);
FloorDiscretizer.setGridDistanceValuesForShape(cellGrid, shapeToDiscretize, PathFindingTag.Target,
PathFindingTag.Reachable);
// test if all the values are set to zero even only half the grid was filled (i.e. the
// initial values are zero)
double counter = 0;
for (int y = 0; y < cellGrid.numPointsY; y++) {
for (int x = 0; x < cellGrid.numPointsX; x++) {
counter += cellGrid.getValue(x, y).potential;
}
}
assertEquals(0, counter, 1e-8);
// test if all states inside the shape to discretize are set to target
for (int y = 0; y < cellGrid.numPointsY / 2; y++) {
for (int x = 0; x < cellGrid.numPointsX; x++) {
assertEquals("y " + y + " and x " + x + " produced an error.", PathFindingTag.Target,
cellGrid.getValue(x, y).tag);
}
}
}
/**
* Test method for
* {@link org.vadere.util.potential.FloorDiscretizer#setGridDistanceValuesForShape(CellGrid, org.vadere.util.geometry.shapes.VShape, PathFindingTag)}.
* Tests that in case of using a shape larger than the grid, the algorithms still works as if
* the shape were equally large.
*/
@Test
public void testSetGridDistanceValuesForShape_halfGridHeightPlusEps() {
shapeToDiscretize = new VRectangle(0, 0, 2.1, 1);
FloorDiscretizer.setGridDistanceValuesForShape(cellGrid, shapeToDiscretize, PathFindingTag.Target,
PathFindingTag.Reachable);
// test if all the values are set to zero even only half the grid was filled (i.e. the
// initial values are zero)
double counter = 0;
for (int y = 0; y < cellGrid.numPointsY; y++) {
for (int x = 0; x < cellGrid.numPointsX; x++) {
counter += cellGrid.getValue(x, y).potential;
}
}
assertEquals(0, counter, 1e-8);
}
/**
* Test method for
* {@link org.vadere.util.potential.FloorDiscretizer#setGridDistanceValuesForShape(CellGrid, org.vadere.util.geometry.shapes.VShape, PathFindingTag)}.
* Tests that in case of using a shape slightly overlapping certain cells, the distances are
* computed correctly.
*/
@Test
public void testSetGridDistanceValuesForShape_halfGridWidthPlusEps() {
shapeToDiscretize = new VRectangle(0, 0, 2, 1.1);
FloorDiscretizer.setGridDistanceValuesForShape(cellGrid, shapeToDiscretize, PathFindingTag.Target,
PathFindingTag.Reachable);
// test if not all the values are set to zero even only half the grid was filled (i.e. the
// initial values are zero)
double counter = 0;
int reachableCounter = 0;
int targetCounter = 0;
for (int y = 0; y < cellGrid.numPointsY; y++) {
for (int x = 0; x < cellGrid.numPointsX; x++) {
counter += cellGrid.getValue(x, y).potential;
if (cellGrid.getValue(x, y).tag == PathFindingTag.Reachable) {
reachableCounter++;
}
if (cellGrid.getValue(x, y).tag == PathFindingTag.Target) {
targetCounter++;
}
}
}
// compute the distances that should be computed by setGridDistanceValuesForShape:
double distanceSum =
cellGrid.getNumPointsY() * (Math.ceil(shapeToDiscretize.height) - shapeToDiscretize.height);
assertEquals(distanceSum, counter, 1e-8);
assertEquals(cellGrid.getNumPointsY(), reachableCounter);
assertEquals(cellGrid.getNumPointsY() * (cellGrid.getNumPointsX() + 1) / 2, targetCounter);
}
}
Markdown is supported
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