@Test
 public void testCircularROIBeanConversionToCircularROI() throws Exception {
   // CircularROI
   double radius = 100;
   ROIBean rbean = new CircularROIBean();
   ((CircularROIBean) rbean).setName("circle");
   ((CircularROIBean) rbean).setStartPoint(startPoint);
   ((CircularROIBean) rbean).setType("CircularROI");
   ((CircularROIBean) rbean).setRadius(radius);
   CircularROI croi = (CircularROI) ROIBeanFactory.decapsulate(rbean);
   assertArrayEquals(startPoint, croi.getPoint(), 0);
   assertEquals(radius, croi.getRadius(), 0);
 }
  /**
   * @param spt starting point
   * @param pt
   * @return resized ROI
   */
  @SuppressWarnings("unchecked")
  public T resize(double[] spt, double[] pt) {
    T rroi = null;

    if (!closed && handle == 4) return rroi;

    rroi = (T) roi.copy();
    pt[0] -= spt[0];
    pt[1] -= spt[1];

    switch (handle) {
      case -1: // new definition
        rroi.setPoint(spt);
        break;
      case 0: // focus/centre
        rroi.addPoint(pt);
        break;
      case 1: // periapsis
        if (rroi instanceof CircularROI) {
          if (pt[0] == 0) break;
          CircularROI lroi = (CircularROI) rroi;
          // work out perpendicular displacement
          double r = lroi.getRadius();
          r -= pt[0];
          if (r < 0) r = 0;
          lroi.setRadius(r);
        } else if (rroi instanceof OrientableROIBase) {
          // work out perpendicular displacement
          double a = rroi.getAngle();
          double d = pt[0] * Math.cos(a) + pt[1] * Math.sin(a);
          if (d == 0) break;

          if (rroi instanceof EllipticalROI) {
            EllipticalROI lroi = (EllipticalROI) rroi;
            double r = lroi.getSemiAxis(0) - d;
            if (r < 0) r = 0;
            lroi.setSemiAxis(0, r);
          } else if (rroi instanceof ParabolicROI) {
            ParabolicROI lroi = (ParabolicROI) rroi;
            double p = lroi.getFocalParameter() - d;
            if (p < 0) p = 0;
            lroi.setFocalParameter(p);
          } else if (rroi instanceof HyperbolicROI) {
            HyperbolicROI lroi = (HyperbolicROI) rroi;
            double l = lroi.getSemilatusRectum();
            double e = lroi.getEccentricity();
            double x = l / (1 + e) - d;
            e = l / x - 1;
            if (e < 1) e = 1;
            lroi.setEccentricity(e);
          }
        }
        break;
      case 2:
        if (rroi instanceof CircularROI) {
          if (pt[1] == 0) break;
          CircularROI lroi = (CircularROI) rroi;
          double r = lroi.getRadius();
          r += pt[1];
          if (r < 0) r = 0;
          lroi.setRadius(r);
        } else if (rroi instanceof OrientableROIBase) {
          // work out perpendicular displacement
          double a = rroi.getAngle();
          double d = -pt[0] * Math.sin(a) + pt[1] * Math.cos(a);
          if (d == 0) break;

          if (rroi instanceof EllipticalROI) {
            EllipticalROI lroi = (EllipticalROI) rroi;
            // work out perpendicular displacement
            double r = lroi.getSemiAxis(1) + d;
            if (r < 0) r = 0;
            lroi.setSemiAxis(1, r);
          } else if (rroi instanceof ParabolicROI) {
            ParabolicROI lroi = (ParabolicROI) rroi;
            double p = lroi.getFocalParameter() + 0.5 * d;
            if (p < 0) p = 0;
            lroi.setFocalParameter(p);
          } else if (rroi instanceof HyperbolicROI) {
            HyperbolicROI lroi = (HyperbolicROI) rroi;
            double l = lroi.getSemilatusRectum() + d;
            if (l < 0) l = 0;
            lroi.setSemilatusRectum(l);
          }
        }
        break;
      case 3:
        if (rroi instanceof CircularROI) {
          if (pt[1] == 0) break;
          CircularROI lroi = (CircularROI) rroi;
          double r = lroi.getRadius();
          r -= pt[1];
          if (r < 0) r = 0;
          lroi.setRadius(r);
        } else if (rroi instanceof OrientableROIBase) {
          // work out perpendicular displacement
          double a = rroi.getAngle();
          double d = -pt[0] * Math.sin(a) + pt[1] * Math.cos(a);
          if (d == 0) break;

          if (rroi instanceof EllipticalROI) {
            EllipticalROI lroi = (EllipticalROI) rroi;
            // work out perpendicular displacement
            double r = lroi.getSemiAxis(1) - d;
            if (r < 0) r = 0;
            lroi.setSemiAxis(1, r);
          } else if (rroi instanceof ParabolicROI) {
            ParabolicROI lroi = (ParabolicROI) rroi;
            double p = lroi.getFocalParameter() - 0.5 * d;
            if (p < 0) p = 0;
            lroi.setFocalParameter(p);
          } else if (rroi instanceof HyperbolicROI) {
            HyperbolicROI lroi = (HyperbolicROI) rroi;
            double l = lroi.getSemilatusRectum() - d;
            if (l < 0) l = 0;
            lroi.setSemilatusRectum(l);
          }
        }
        break;
      case 4:
        if (closed) {
          if (rroi instanceof CircularROI) {
            if (pt[0] == 0) break;
            CircularROI lroi = (CircularROI) rroi;
            double r = lroi.getRadius();
            r += pt[0];
            if (r < 0) r = 0;
            lroi.setRadius(r);
          } else if (rroi instanceof EllipticalROI) {
            EllipticalROI lroi = (EllipticalROI) rroi;
            // work out perpendicular displacement
            double a = lroi.getAngle();
            double d = pt[0] * Math.cos(a) + pt[1] * Math.sin(a);
            if (d == 0) break;
            double r = lroi.getSemiAxis(0) + d;
            if (r < 0) r = 0;
            lroi.setSemiAxis(0, r);
          }
        }
        break;
      default:
        break;
    }

    return rroi;
  }