In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit 9f2c7595 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier

Coding Dojo: Added "org/vadere/simulator/util/TopographyChecker.java" and basic unit tests.

VadereUtils/src/org/vadere/util/geometry/shapes/VShape.java:
- Added default method "intersects()".
parent 868d53bd
Pipeline #68647 passed with stages
in 66 minutes and 21 seconds
package org.vadere.simulator.util;
import org.apache.commons.math3.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.vadere.state.scenario.Obstacle;
import org.vadere.state.scenario.Source;
import org.vadere.state.scenario.Topography;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TopographyChecker {
private final Topography topography;
public TopographyChecker(@NotNull final Topography topography) {
this.topography = topography;
}
public List<Pair<Obstacle, Obstacle>> checkObstacleOverlap() {
List<Pair<Obstacle, Obstacle>> intersectList = new ArrayList<>();
for (int i = 0; i < topography.getObstacles().size(); i++) {
for (int j = i + 1; j < topography.getObstacles().size(); j++) {
Obstacle obs1 = topography.getObstacles().get(i);
Obstacle obs2 = topography.getObstacles().get(j);
if (obs1.getShape().intersects(obs2.getShape())) {
intersectList.add(Pair.create(obs1, obs2));
}
}
}
return intersectList;
}
public boolean hasObstacleOverlaps() {
return checkObstacleOverlap().size() > 0;
}
public List<Source> getSourceWithoutTarget() {
return topography.getSources().stream()
.filter(s -> s.getAttributes().getTargetIds().size() == 0)
.collect(Collectors.toList());
}
}
package org.vadere.simulator.util;
import org.apache.commons.math3.util.Pair;
import org.junit.Test;
import org.vadere.state.attributes.scenario.AttributesObstacle;
import org.vadere.state.scenario.Obstacle;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class TopographyCheckerTest {
@Test
public void testCheckObstacleOverlapHasOverlap(){
Topography topography = new Topography();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(0,0,1,1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
TopographyChecker topcheck = new TopographyChecker(topography);
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
assertEquals(1, actualList.size());
}
@Test
public void testCheckObstacleOverlapHasNoOverlap(){
Topography topography = new Topography();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1.1,0,1,1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
TopographyChecker topcheck = new TopographyChecker(topography);
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
assertEquals(0, actualList.size());
}
@Test
public void testCheckObstacleOverlapReturnsNoOverlapsIfTwoSegmentsTouch(){
Topography topography = new Topography();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1,0,1,1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
TopographyChecker topcheck = new TopographyChecker(topography);
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
assertEquals(0, actualList.size());
}
}
\ No newline at end of file
package org.vadere.state.scenario;
import org.jetbrains.annotations.NotNull;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.attributes.scenario.AttributesObstacle;
......@@ -10,10 +11,7 @@ public class Obstacle extends ScenarioElement {
private AttributesObstacle attributes;
public Obstacle(AttributesObstacle attributes) {
if (attributes == null)
throw new IllegalArgumentException("Attributes must not be null.");
public Obstacle(@NotNull AttributesObstacle attributes) {
this.attributes = attributes;
}
......
package org.vadere.util.geometry.shapes;
import java.awt.Shape;
import java.awt.geom.Area;
import org.vadere.util.geometry.ShapeType;
......@@ -26,4 +27,12 @@ public interface VShape extends Shape, Cloneable {
VPoint getCentroid();
ShapeType getType();
default boolean intersects(VShape shape){
Area thisShape = new Area(this);
Area otherShape = new Area(shape);
Area thisShapeCpy = new Area(this);
thisShape.subtract(otherShape);
return !thisShape.equals(thisShapeCpy);
}
}
package org.vadere.util.geometry.shapes;
import org.junit.Test;
import static org.junit.Assert.*;
public class VShapeTest {
@Test
public void testIntersectShapesReturnsTrueWithOverlappingShapes(){
VShape a = new VRectangle(0,0,1,1);
VShape b = new VRectangle(0,0,1,1);
assertTrue(a.intersects(b));
}
@Test
public void testIntersectShapesReturnsFalseWithNonOverlappingShapes(){
VShape a = new VRectangle(0,0,1,1);
VShape b = new VRectangle(1.1,0,1,1);
assertFalse(a.intersects(b));
}
@Test
public void testIntersectShapesReturnsTrueWithOverlappingCircles(){
VShape a = new VRectangle(0,0,1,1);
VShape b = new VCircle(new VPoint(-1, -1), 5.0);
assertTrue(a.intersects(b));
}
}
\ No newline at end of file
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