/** 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); }
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() != 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 }
/** * 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() != 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)); }