/** NOTE: the connection from java to matlab */ public RCvalue java2Matlab() throws EvalException { if (rc == null) { if (poly != null) { Value[] v = new Value[poly.degree()]; for (int i = 0; i < v.length; i++) { Point pt = poly.point(i); assert pt.type() == CohoDouble.type : "The result type is not CohoDouble, it is " + pt.type(); // if(pt.type()!=CohoDouble.type){ // throw new RuntimeException("The result type is not CohoDouble, it is "+pt.type() ); // } v[i] = RCvalue.factory() .create( new Value[] { DoubleValue.factory() .create(new Double(((CohoDouble) pt.x()).doubleValue()), null), DoubleValue.factory() .create(new Double(((CohoDouble) pt.y()).doubleValue()), null) }, false); } rc = (RCvalue) (RCvalue.factory().create(v, true)); } else { // empty polygon rc = (RCvalue) (RCvalue.factory().create(new Value[0], true)); } } return (rc); }
protected static Value grind(RCvalue args, Operation op) throws EvalException { for (int i = 0; i < args.size(); i++) if (!(args.value(i) instanceof PolygonValue)) throw new EvalException( op.name() + "(): all operands must polgons." + "Operand " + (i + 1) + "is not."); return (grind(args, op, 0, args.size())); }
public Value eval(RCvalue args) throws EvalException { if (args.size() != 1) throw new EvalException("usage: hull(polygon)"); if (!(args.value(0) instanceof PolygonValue)) throw new EvalException("hull: argument must be a polygon"); PolygonValue pv = (PolygonValue) (args.value(0)); Polygon poly = pv.polygon(); return new PolygonValue(poly.convexHull()); // NOTE: connection for convex hull }
public Value eval(RCvalue args) throws EvalException { Polygon[] polys = new Polygon[args.size() - 1]; for (int i = 0; i < args.size() - 1; i++) { PolygonValue pv = (PolygonValue) args.value(i); polys[i] = pv.polygon(); } PolygonValue pv = (PolygonValue) args.value(args.size() - 1); // the last polygon return new PolygonValue(pv.polygon().union(polys)); }
public Value eval(RCvalue args) throws EvalException { if ((args.size() < 0) || (3 < args.size())) throw new EvalException("usage: reduce(polygon, [errtol,[edgeReducible] ] ])"); if (!(args.value(0) instanceof PolygonValue)) throw new EvalException("reduce: first operand must be a polygon"); double errtol = defaultErrtol; if (args.size() >= 2) { // get error tolerance if (!(args.value(1) instanceof DoubleValue)) throw new EvalException("reduce: second operand must be an integer"); errtol = ((DoubleValue) (args.value(1))).value(); } boolean edgeReducible = true; if (args.size() >= 3) { double temp = ((DoubleValue) (args.value(2))).value(); edgeReducible = (temp > 0); // temp = 1, edge reducible, otherwise, not } Polygon.EndCondition ec = new Polygon.CostEndCondition(errtol); if (errtol >= 3) { int maxV = (int) Math.round(errtol); ec = new Polygon.DegreeEndCondition(maxV); } PolygonValue p = (PolygonValue) (args.value(0)); Polygon poly = p.polygon(); if (poly instanceof ConvexPolygon) { // NOTE: connection for reduce if (edgeReducible) return new PolygonValue(((ConvexPolygon) poly).reduce(ec)); else return p; // can't reduce because convex polygon can only reduce edge } else { return new PolygonValue(poly.reduce(ec, true, edgeReducible)); } }
public Value eval(RCvalue args) throws EvalException { if (args.size() != 2) throw new EvalException("usage: reduce(polygon, errtol])"); PolygonValue pv1 = (PolygonValue) args.value(0); PolygonValue pv2 = (PolygonValue) args.value(1); boolean isContain = pv1.polygon().contains(pv2.polygon()); double contain = 0; if (isContain) { contain = 1; // we don't have a boolean factory now. } return ValueFactory.create(contain); }
// value(lo) to value(hi-1) protected static Value grind(RCvalue args, Operation op, int lo, int hi) throws EvalException { switch (hi - lo) { case 0: return new PolygonValue( (RCvalue) (RCvalue.factory().create(new Value[] {}, true))); // empty case 1: return args.value(lo); // one case 2: Value result = op.op((PolygonValue) (args.value(lo)), (PolygonValue) (args.value(hi - 1))); return result; // two default: int mid = (lo + hi + 1) / 2; // split Value v0 = grind(args, op, lo, mid); Value v1 = grind(args, op, mid, hi); result = op.op((PolygonValue) (v0), (PolygonValue) (v1)); return result; } }
public Value eval(RCvalue args) throws EvalException { if (args.size() != 2) throw new EvalException("usage: intersect(poly1,poly2)"); Polygon p1 = ((PolygonValue) args.value(0)).polygon(); Polygon p2 = ((PolygonValue) args.value(1)).polygon(); return new PolygonValue(p1.intersect(p2)); }
/** * Create a polygon from a RCvalue(matrix) NOTE: the connection from matlab to java TODO: what if * there are less than 3 points */ protected PolygonValue(RCvalue u) throws EvalException { // NOTE: matlab to java Point[] points; if (u.isRow()) { // each column should have two double values points = new Point[u.size()]; for (int i = 0; i < u.size(); i++) { Value v = u.value(i); if (!(v instanceof RCvalue)) rc_bad(); RCvalue c = (RCvalue) v; if (c.size() != 2) rc_bad(); Value x = c.value(0), y = c.value(1); if (!(x instanceof DoubleValue) || !(y instanceof DoubleValue)) rc_bad(); points[i] = Point.create(((DoubleValue) x).value(), ((DoubleValue) y).value()); } } else { if (!(u.value(0) instanceof RCvalue) || !(u.value(1) instanceof RCvalue)) rc_bad(); RCvalue xMatrix = (RCvalue) (u.value(0)), yMatrix = (RCvalue) u.value(1); if (xMatrix.size() != yMatrix.size()) rc_bad(); points = new Point[xMatrix.size()]; for (int i = 0; i < xMatrix.size(); i++) { Value x = xMatrix.value(i), y = yMatrix.value(i); if (!(x instanceof DoubleValue) || !(y instanceof DoubleValue)) rc_bad(); points[i] = Point.create(((DoubleValue) x).value(), ((DoubleValue) y).value()); } } poly = new SimplePolygon(points); if (ConvexPolygon.isConvex( (SimplePolygon) poly)) // create convexPolygon if it is, simplify the computation later // BUGS, SimplePolygon removes duplicated points. Therefore, it is incorrect to construct a // convex polygon // using points and do not check. We should use the points from poly. // poly = new ConvexPolygon(points,false); poly = new ConvexPolygon(poly.points(), false); if (poly.degree() == u.size()) // SimplePolygon may remove redundant points rc = u; else rc = null; }
public Value eval(RCvalue args) throws EvalException { if (args.size() != 1) throw new EvalException("usage: polygon(row/col)"); if (!(args.value(0) instanceof RCvalue)) throw new EvalException("polygon: argument must be a row/col"); return new PolygonValue((RCvalue) (args.value(0))); }