Commit da89eafa authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Merge branch 'master' into psychology

parents 4a2ef232 47dae5ed
Pipeline #373179 passed with stages
in 128 minutes and 56 seconds
......@@ -43,6 +43,18 @@ public class VRectangle extends Rectangle2D.Double implements VShape {
this(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
@Override
public boolean contains(VCircle otherShape) {
double centerX = otherShape.getCentroid().x;
double centerY = otherShape.getCentroid().y;
double radius = otherShape.getRadius();
boolean circleFitsIntoHorizontally = (centerX > (this.x + radius)) && ((this.x + this.width) > (centerX + radius));
boolean circleFitsIntoVertically = (centerY > (this.y + radius)) && ((this.y + this.height) > (centerY + radius));
return circleFitsIntoHorizontally && circleFitsIntoVertically;
}
@Override
public double distance(IPoint point) {
VPoint closestPoint = closestPoint(point);
......
......@@ -76,7 +76,12 @@ public interface VShape extends Shape, Cloneable, Geometry {
return thisShape.isEmpty();
}
// numerical not stable for comparision with VCircle.
// use contains(VCircle otherShape)
default boolean containsShape(VShape otherShape) {
if (otherShape instanceof VCircle){
return this.contains((VCircle)otherShape);
}
Area thisArea = new Area(this);
Area otherArea = new Area(otherShape);
thisArea.intersect(otherArea);
......@@ -84,6 +89,15 @@ public interface VShape extends Shape, Cloneable, Geometry {
}
// todo: remove default implementation and implement specific performance optimized and numerical stable versions.
default boolean contains(VCircle otherShape){
// override in specific shapes for more performance optimized implementations
Area thisArea = new Area(this);
Area otherArea = new Area(otherShape);
thisArea.intersect(otherArea);
return thisArea.equals(otherArea);
}
/**
* Returns a list of points (p1, p2, ..., pn) such that the line (p1,p2) is part of the boundary
* of the approximation of this shape. p1 != pn i.e. it is not a closed path.
......
package org.vadere.util.geometry.shapes;
import org.junit.Ignore;
import org.junit.Test;
import java.awt.geom.Path2D;
import static org.junit.Assert.*;
public class VShapeTest {
......@@ -16,6 +15,66 @@ public class VShapeTest {
assertTrue(a.intersects(b));
}
@Test(timeout = 1000) //ms
public void loopAtEdgeCase1(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(0.005096532915902063, 118.69364126200188), 0.2);
boolean contains = rec.containsShape(circle);
assertFalse(contains);
}
@Test(timeout = 1000) //ms
public void loopAtEdgeCase2(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(0.008096532915902063, 118.69364126200188), 0.2);
boolean contains = rec.containsShape(circle);
assertFalse(contains);
}
@Ignore
@Test(timeout = 1000) //ms
/**
* Error at magic number. With given Shapes in this test the AWT Area equals comparision
* will start loop forever. Small changes (0.005 --> 0.008) will fix this.
* Problem occurs if the contains check involves circles because they get transformed
* to awt Paths for the comparision (bezier curve) which is probably the reason.
* Current fix introduces a new contains(VCircle otherShape) overload in which
* specialized check should take place. Currently only VRectangle implement this.
* Other shapes will still use the numerical unstable version.
*/
public void loopAtEdgeCase_asVPolygon1(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(0.005096532915902063, 118.69364126200188), 0.2);
VPolygon poly = new VPolygon(rec);
boolean contains = poly.contains(circle);
assertFalse(contains);
}
@Test(timeout = 1000)
public void loopAtEdgeCase_asVPolygon2(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(0.008096532915902063, 118.69364126200188), 0.2);
VPolygon poly = new VPolygon(rec);
boolean contains = poly.contains(circle);
assertFalse(contains);
}
@Test(timeout = 1000) //ms
public void loopAtEdgeCase_asVRectangle(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(0.005096532915902063, 118.69364126200188), 0.2);
boolean contains = rec.contains(circle);
assertFalse(contains);
}
@Test
public void containsShapeFalse(){
VRectangle rec = new VRectangle(0.0, 114.0, 44.0, 5.0);
VCircle circle = new VCircle(new VPoint(300.0, 300.0), 0.2);
boolean contains = rec.containsShape(circle);
assertFalse(contains);
}
@Test
public void testIntersectShapesReturnsFalseWithNonOverlappingShapes() {
VShape a = new VRectangle(0, 0, 1, 1);
......
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