Beispiel #1
0
 /** 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);
 }
Beispiel #2
0
 /**
  * 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;
 }