use of aima.core.util.math.geom.shapes.Ellipse2D in project aima-java by aimacode.
the class SVGGroupParser method parseEllipse.
/**
* Parses the current element as an ellipse. This ellipse is added to the {@code shapes} if it is rendered.
*/
private void parseEllipse() {
String value = reader.getAttributeValue(null, CX_ATTRIBUTE);
final double cx = parseNumber(value);
value = reader.getAttributeValue(null, CY_ATTRIBUTE);
final double cy = parseNumber(value);
value = reader.getAttributeValue(null, RX_ATTRIBUTE);
final double rx = parseNumber(value);
value = reader.getAttributeValue(null, RY_ATTRIBUTE);
final double ry = parseNumber(value);
if (rx != 0.0d && ry != 0.0d) {
//SVG standard specifies that the radius is forced to have a value. Otherwise the rendering for this element is disabled.
IGeometric2D elipse = new Ellipse2D(new Point2D(cx, cy), rx, ry).transform(currentMatrix);
shapes.add(elipse);
}
}
use of aima.core.util.math.geom.shapes.Ellipse2D 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();
}
use of aima.core.util.math.geom.shapes.Ellipse2D 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();
}
}
use of aima.core.util.math.geom.shapes.Ellipse2D in project aima-java by aimacode.
the class Ellipse2DTest method setUp.
@Before
public void setUp() {
center = new Point2D(12.0d, 14.0d);
testEllipse = new Ellipse2D(center, 10.0d, 5.0d);
zeroPoint = new Point2D(0.0d, 0.0d);
}
use of aima.core.util.math.geom.shapes.Ellipse2D in project aima-java by aimacode.
the class Ellipse2DTest method testRayCast.
@Test
public final void testRayCast() {
// static tests
assertEquals("Ray doesn't intersect.", Double.POSITIVE_INFINITY, testEllipse.rayCast(new Ray2D(0.0d, 0.0d, 0.0d, 2.0d)), 0.000005d);
assertEquals("Ray intersects.", 2.0d, testEllipse.rayCast(new Ray2D(0.0d, 14.0d, 12.0d, 14.0d)), 0.000005d);
// serial tests
Ellipse2D randomEllipse;
Point2D randomPointOnEllipse;
Point2D randomPoint;
double currentXRadius;
double currentYRadius;
double xvalue;
double yvalue;
double randomAngle;
int sector;
int counter = 1000;
do {
randomEllipse = new Ellipse2D(new Point2D(Util.generateRandomDoubleBetween(-500.0d, 500.0d), Util.generateRandomDoubleBetween(-500.0d, 500.0d)), Util.generateRandomDoubleBetween(0.0d, 200.0d), Util.generateRandomDoubleBetween(0.0d, 200.0d));
currentXRadius = randomEllipse.getHorizontalLength();
currentYRadius = randomEllipse.getVerticalLength();
xvalue = Util.generateRandomDoubleBetween(0.0d, currentXRadius);
yvalue = (currentYRadius * Math.sqrt(currentXRadius * currentXRadius - xvalue * xvalue)) / currentXRadius;
sector = Util.randomNumberBetween(1, 4);
switch(sector) {
case 2:
{
yvalue = -yvalue;
randomPoint = new Point2D(Util.generateRandomDoubleBetween(randomEllipse.getCenter().getX() + xvalue, 1000.0d), Util.generateRandomDoubleBetween(-1000.0d, randomEllipse.getCenter().getY() + yvalue));
break;
}
case 3:
{
xvalue = -xvalue;
yvalue = -yvalue;
randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, randomEllipse.getCenter().getX() + xvalue), Util.generateRandomDoubleBetween(-1000.0d, randomEllipse.getCenter().getY() + yvalue));
break;
}
case 4:
{
xvalue = -xvalue;
randomPoint = new Point2D(Util.generateRandomDoubleBetween(-1000.0d, randomEllipse.getCenter().getX() + xvalue), Util.generateRandomDoubleBetween(randomEllipse.getCenter().getY() + yvalue, 1000.0d));
break;
}
default:
{
randomPoint = new Point2D(Util.generateRandomDoubleBetween(randomEllipse.getCenter().getX() + xvalue, 1000.0d), Util.generateRandomDoubleBetween(randomEllipse.getCenter().getY() + yvalue, 1000.0d));
break;
}
}
randomPointOnEllipse = new Point2D(randomEllipse.getCenter().getX() + xvalue, randomEllipse.getCenter().getY() + yvalue);
randomAngle = Util.generateRandomDoubleBetween(-Math.PI / 2, Math.PI / 2);
randomEllipse = (Ellipse2D) (randomEllipse.transform(TransformMatrix2D.rotate(randomAngle)));
randomPoint = TransformMatrix2D.rotate(randomAngle).multiply(randomPoint);
randomPointOnEllipse = TransformMatrix2D.rotate(randomAngle).multiply(randomPointOnEllipse);
// System.out.printf("RayCast No. %d: Ellipse at (%.2f,%.2f), radii: (%.2f,%.2f). Rotation angle: %.2f, original angle: %.2f, point on ellipse: (%.2f,%.2f), outside point: (%.2f,%.2f), distance: %.2f.\n", 1000-counter, randomEllipse.getCenter().getX(), randomEllipse.getCenter().getY(), randomEllipse.getHorizontalLength(), randomEllipse.getVerticalLength(), randomEllipse.getAngle(), randomAngle, randomPointOnEllipse.getX(), randomPointOnEllipse.getY(), randomPoint.getX(), randomPoint.getY(), randomPoint.distance(randomPointOnEllipse));
assertEquals("Serial rayCast test for Circle2D.", randomPoint.distance(randomPointOnEllipse), randomEllipse.rayCast(new Ray2D(randomPoint, randomPoint.vec(randomPointOnEllipse))), 0.000005d);
counter -= 1;
} while (counter > 0);
}
Aggregations