package es.iiia.shapegrammar.shape;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import es.iiia.shapegrammar.model.NodeModel;
import es.iiia.shapegrammar.model.PointComparator;
import es.iiia.shapegrammar.shape.guides.LineDefinition;
import es.iiia.shapegrammar.utils.MathUtils;
public class CarrierModel extends NodeModel {
protected LineDefinition definition;
protected ArrayList<LineModel> lines;
protected ArrayList<Point2D> points;
// Ctor
public CarrierModel() {
}
public CarrierModel(LineModel line) {
this.definition = line.getDefinition();
this.addLine(line);
}
public class LineComparator implements Comparator<LineModel> {
Point2D p11, p21;
public int compare(LineModel o1, LineModel o2) {
// sort line points
o1.sort();
o2.sort();
p11 = o1.getP1();
p21 = o2.getP1();
if (p11.getX() < p21.getX() || (p11.getX() == p21.getX() && p11.getY() < p21.getY()))
return -1;
else if (p11.getX() > p21.getX() || (p11.getX() == p21.getX() && p11.getY() > p21.getY())) {
return 1;
}
return 0;
}
}
// methods
public int hashCode() {
return this.getDefinition().hashCode();
}
// adds line and creates poins
public void addLine(LineModel line) {
this.getLines().add(line);
// now add points
this.addPoint(line.getP1());
this.addPoint(line.getP2());
}
public void addPoint(Point2D point) {
// check if point exists
for (Point2D pt : this.getPoints()) {
if (MathUtils.compare(pt, point) == 0) {
return;
}
}
// add point to collection
this.points.add(point);
// sort points from left to right for easier navigation in later phases
Collections.sort(this.points, new PointComparator());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CarrierModel) {
return this.definition.compareTo(((CarrierModel) obj).getDefinition()) == 0;
}
return false;
}
// Gs/Ss
public LineDefinition getDefinition() {
return definition;
}
public ArrayList<LineModel> getLines() {
if (lines == null) {
lines = new ArrayList<LineModel>();
}
return lines;
}
public ArrayList<LineModel> getSortedLines() {
this.sortLines();
return lines;
}
public ArrayList<Point2D> getPoints() {
if (points == null) {
points = new ArrayList<Point2D>();
}
return points;
}
private void sortLines() {
Collections.sort(this.lines, new LineComparator());
}
// private AffineTransform getSymmetryTransform(PointModel p1, PointModel p2) {
// double v1 = p2.getX() - p1.getX();
// double v2 = p2.getY() - p1.getY();
// double a = p1.getX();
// double b = p1.getY();
//
// double a00 = v1 * v1 - v2 * v2;
// double a01 = 2 * v1 * v2;
// double a02 = -v1 * v2 * b - v1 * v1 * b - v1 * v2 * b + v2 * v2 * a + a;
// double a10 = v1 * v2 - v2 * v2;
// double a11 = v1 * v2 - v1 * v1;
// double a12 = -v1 * v2 * b + v2 * v2 * a + v1 * v1 * b - v1 * v2 * a + b;
//
// return new AffineTransform(a00, a10, a01, a11, a02, a12);
// }
}