Search in sources :

Example 1 with Line2D

use of aima.core.util.math.geom.shapes.Line2D in project aima-java by aimacode.

the class SVGGroupParserTest method setUp.

@Before
public void setUp() {
    testGroup = new ArrayList<IGeometric2D>();
    testGroup.ensureCapacity(10);
    testGroup.add(new Polyline2D(testVertices1, true));
    testGroup.add(new Line2D(15.0d, 15.0d, 200.0d, 100.0d));
    testGroup.add(new Ellipse2D(new Point2D(300.0d, 80.0d), 60.0d, 40.0d));
    testGroup.add(new Circle2D(new Point2D(180.0d, 160.0d), 20.0d));
    testGroup.add(new Rect2D(161.0d, 200.0d, 261.0d, 250.0d));
    testGroup.add(new Polyline2D(testVertices2, true));
    testGroup.add(new Line2D(0.0d, 0.0d, 700.0d, 0.0d));
    testGroup.add(new Line2D(700.0d, 0.0d, 700.0d, 500.0d));
    testGroup.add(new Line2D(700.0d, 500.0d, 0.0d, 500.0d));
    testGroup.add(new Line2D(0.0d, 500.0d, 0.0d, 0.0d));
    testParser = new SVGGroupParser();
}
Also used : IGeometric2D(aima.core.util.math.geom.shapes.IGeometric2D) Polyline2D(aima.core.util.math.geom.shapes.Polyline2D) Point2D(aima.core.util.math.geom.shapes.Point2D) Circle2D(aima.core.util.math.geom.shapes.Circle2D) SVGGroupParser(aima.core.util.math.geom.SVGGroupParser) Line2D(aima.core.util.math.geom.shapes.Line2D) Rect2D(aima.core.util.math.geom.shapes.Rect2D) Ellipse2D(aima.core.util.math.geom.shapes.Ellipse2D) Before(org.junit.Before)

Example 2 with Line2D

use of aima.core.util.math.geom.shapes.Line2D in project aima-java by aimacode.

the class SVGGroupParserTest method testParse.

@Test
public void testParse() {
    IGeometric2D x;
    IGeometric2D y;
    Iterator<IGeometric2D> j;
    try {
        j = testParser.parse(this.getClass().getResourceAsStream(file), groupID).iterator();
        Iterator<IGeometric2D> i = testGroup.iterator();
        while (i.hasNext()) {
            x = i.next();
            y = j.next();
            if (x instanceof Circle2D && y instanceof Circle2D) {
                assertEquals("Circle: Center-X.", ((Circle2D) (y)).getCenter().getX(), ((Circle2D) (x)).getCenter().getX(), 0.000005d);
                assertEquals("Circle: Center-Y.", ((Circle2D) (y)).getCenter().getY(), ((Circle2D) (x)).getCenter().getY(), 0.000005d);
                assertEquals("Circle: Radius.", ((Circle2D) (y)).getRadius(), ((Circle2D) (x)).getRadius(), 0.000005d);
            } else if (x instanceof Ellipse2D && y instanceof Ellipse2D) {
                assertEquals("Ellipse: Center-X.", ((Ellipse2D) (y)).getCenter().getX(), ((Ellipse2D) (x)).getCenter().getX(), 0.000005d);
                assertEquals("Ellipse: Center-Y.", ((Ellipse2D) (y)).getCenter().getY(), ((Ellipse2D) (x)).getCenter().getY(), 0.000005d);
                assertEquals("Ellipse: Horizontal length.", ((Ellipse2D) (y)).getHorizontalLength(), ((Ellipse2D) (x)).getHorizontalLength(), 0.000005d);
                assertEquals("Ellipse: Vertical length.", ((Ellipse2D) (y)).getVerticalLength(), ((Ellipse2D) (x)).getVerticalLength(), 0.000005d);
                assertEquals("Ellipse: Rotation angle.", ((Ellipse2D) (y)).getAngle(), ((Ellipse2D) (x)).getAngle(), 0.000005d);
            } else if (x instanceof Line2D && y instanceof Line2D) {
                assertEquals("Line: Start-X.", ((Line2D) (y)).getStart().getX(), ((Line2D) (x)).getStart().getX(), 0.000005d);
                assertEquals("Line: Start-Y.", ((Line2D) (y)).getStart().getY(), ((Line2D) (x)).getStart().getY(), 0.000005d);
                assertEquals("Line: End-X.", ((Line2D) (y)).getEnd().getX(), ((Line2D) (x)).getEnd().getX(), 0.000005d);
                assertEquals("Line: End-Y.", ((Line2D) (y)).getEnd().getY(), ((Line2D) (x)).getEnd().getY(), 0.000005d);
            } else if (x instanceof Polyline2D && y instanceof Polyline2D) {
                if (((Polyline2D) x).getVertexes().length != ((Polyline2D) x).getVertexes().length)
                    fail();
                else {
                    for (int k = 0; k < ((Polyline2D) x).getVertexes().length; k++) {
                        assertEquals("Polygon: Vertex-X", ((Polyline2D) x).getVertexes()[k].getX(), ((Polyline2D) y).getVertexes()[k].getX(), 0.000005d);
                        assertEquals("Polygon: Vertex-Y", ((Polyline2D) x).getVertexes()[k].getY(), ((Polyline2D) y).getVertexes()[k].getY(), 0.000005d);
                    }
                }
            } else if (x instanceof Rect2D && y instanceof Rect2D) {
                assertEquals("Line: UpperLeft-X.", ((Rect2D) (y)).getUpperLeft().getX(), ((Rect2D) (x)).getUpperLeft().getX(), 0.000005d);
                assertEquals("Line: UpperLeft-Y.", ((Rect2D) (y)).getUpperLeft().getY(), ((Rect2D) (x)).getUpperLeft().getY(), 0.000005d);
                assertEquals("Line: LowerRight-X.", ((Rect2D) (y)).getLowerRight().getX(), ((Rect2D) (x)).getLowerRight().getX(), 0.000005d);
                assertEquals("Line: LowerRight-Y.", ((Rect2D) (y)).getLowerRight().getY(), ((Rect2D) (x)).getLowerRight().getY(), 0.000005d);
            }
        }
        assertFalse("Both groups are the same length and contain only legal shapes.", i.hasNext() || j.hasNext());
    } catch (XMLStreamException e) {
        e.printStackTrace();
        fail();
    } catch (NullPointerException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : IGeometric2D(aima.core.util.math.geom.shapes.IGeometric2D) Polyline2D(aima.core.util.math.geom.shapes.Polyline2D) XMLStreamException(javax.xml.stream.XMLStreamException) Circle2D(aima.core.util.math.geom.shapes.Circle2D) Line2D(aima.core.util.math.geom.shapes.Line2D) Rect2D(aima.core.util.math.geom.shapes.Rect2D) Ellipse2D(aima.core.util.math.geom.shapes.Ellipse2D) Test(org.junit.Test)

Example 3 with Line2D

use of aima.core.util.math.geom.shapes.Line2D in project aima-java by aimacode.

the class Line2DTest method testRayCast.

@Test
public final void testRayCast() {
    // Static rayCast tests
    assertEquals("Ray doesn't intersect.", Double.POSITIVE_INFINITY, testLine.rayCast(new Ray2D(1.0d, 1.0d, -7.0d, -8.0d)), 0.000005d);
    assertEquals("Ray intersects.", Math.sqrt(2), testLine.rayCast(new Ray2D(2.0d, 5.0d, 4.0d, 3.0d)), 0.000005d);
    assertEquals("Ray intersects.", 6, testLine2.rayCast(new Ray2D(zeroPoint, Vector2D.X_VECTOR)), 0.000005d);
    assertEquals("Ray intersects.", 3.0, testLine2.rayCast(new Ray2D(new Point2D(3.0d, 3.0d), Vector2D.X_VECTOR)), 0.000005d);
    assertEquals("Ray intersects.", Double.POSITIVE_INFINITY, testLine2.rayCast(new Ray2D(new Point2D(6.0d, 2.0d), Vector2D.X_VECTOR)), 0.000005d);
    assertEquals("Ray intersects.", 3.6, testLine3.rayCast(new Ray2D(zeroPoint, Vector2D.X_VECTOR)), 0.000005d);
    // Serial RayCast tests
    Point2D randomPoint;
    Line2D randomLine;
    Point2D randomPointOnLine;
    int counter = 5000;
    // generate a random point and another random point on a random Line and compare the distance between the two with a rayCast from the former towards the latter.
    do {
        randomLine = new Line2D(Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d));
        randomPointOnLine = randomLine.randomPoint();
        randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, 1000.0d));
        // System.out.printf("Line2D rayCast test no. %d: Line (%.2f,%.2f,%.2f,%.2f), point (%.2f,%.2f), point on line (%.2f,%.2f), distance: %.2f.\n", counter, randomLine.getStart().getX(), randomLine.getStart().getY(), randomLine.getEnd().getX(), randomLine.getEnd().getY(), randomPoint.getX(), randomPoint.getY(), randomPointOnLine.getX(), randomPointOnLine.getY(), randomPoint.distance(randomPointOnLine));
        assertEquals("Serial rayCast test for Line2D.", randomPoint.distance(randomPointOnLine), randomLine.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnLine))), 0.000005d);
        counter -= 1;
    } while (counter > 0);
}
Also used : Point2D(aima.core.util.math.geom.shapes.Point2D) Ray2D(aima.core.util.math.geom.shapes.Ray2D) Line2D(aima.core.util.math.geom.shapes.Line2D) Test(org.junit.Test)

Example 4 with Line2D

use of aima.core.util.math.geom.shapes.Line2D in project aima-java by aimacode.

the class Line2DTest method setUp.

@Before
public void setUp() throws Exception {
    testLine = new Line2D(2.0d, 3.0d, 4.0d, 5.0d);
    testLine2 = new Line2D(6.0d, 4.0d, 6.0d, -3.0d);
    testLine3 = new Line2D(6.0d, -3.0d, 2.0d, 2.0d);
    testLine4 = new Line2D(3.0d, 4.0d, 6.0d, 4.0d);
    zeroPoint = new Point2D(0.0d, 0.0d);
}
Also used : Point2D(aima.core.util.math.geom.shapes.Point2D) Line2D(aima.core.util.math.geom.shapes.Line2D) Before(org.junit.Before)

Example 5 with Line2D

use of aima.core.util.math.geom.shapes.Line2D in project aima-java by aimacode.

the class SVGGroupParser method parseLine.

/**
	 * Parses the current element as a line.
	 */
private void parseLine() {
    String value = reader.getAttributeValue(null, X1_ATTRIBUTE);
    final double x1 = parseNumber(value);
    value = reader.getAttributeValue(null, Y1_ATTRIBUTE);
    final double y1 = parseNumber(value);
    value = reader.getAttributeValue(null, X2_ATTRIBUTE);
    final double x2 = parseNumber(value);
    value = reader.getAttributeValue(null, Y2_ATTRIBUTE);
    final double y2 = parseNumber(value);
    IGeometric2D line = new Line2D(x1, y1, x2, y2).transform(currentMatrix);
    shapes.add(line);
}
Also used : IGeometric2D(aima.core.util.math.geom.shapes.IGeometric2D) Line2D(aima.core.util.math.geom.shapes.Line2D)

Aggregations

Line2D (aima.core.util.math.geom.shapes.Line2D)5 IGeometric2D (aima.core.util.math.geom.shapes.IGeometric2D)3 Point2D (aima.core.util.math.geom.shapes.Point2D)3 Circle2D (aima.core.util.math.geom.shapes.Circle2D)2 Ellipse2D (aima.core.util.math.geom.shapes.Ellipse2D)2 Polyline2D (aima.core.util.math.geom.shapes.Polyline2D)2 Rect2D (aima.core.util.math.geom.shapes.Rect2D)2 Before (org.junit.Before)2 Test (org.junit.Test)2 SVGGroupParser (aima.core.util.math.geom.SVGGroupParser)1 Ray2D (aima.core.util.math.geom.shapes.Ray2D)1 XMLStreamException (javax.xml.stream.XMLStreamException)1