Example #1
0
 /**
  * With segmented selections, ignore first mouse up and finalize when user double-clicks,
  * control-clicks or clicks in start box.
  */
 protected void handleMouseUp(int sx, int sy) {
   if (state == MOVING) {
     state = NORMAL;
     return;
   }
   if (state == MOVING_HANDLE) {
     cachedMask = null; // mask is no longer valid
     state = NORMAL;
     updateClipRect();
     oldX = x;
     oldY = y;
     oldWidth = width;
     oldHeight = height;
     return;
   }
   if (state != CONSTRUCTING) return;
   if (IJ.spaceBarDown()) // is user scrolling image?
   return;
   boolean samePoint = false;
   if (xpf != null)
     samePoint = (xpf[nPoints - 2] == xpf[nPoints - 1] && ypf[nPoints - 2] == ypf[nPoints - 1]);
   else samePoint = (xp[nPoints - 2] == xp[nPoints - 1] && yp[nPoints - 2] == yp[nPoints - 1]);
   Rectangle biggerStartBox =
       new Rectangle(ic.screenXD(startXD) - 5, ic.screenYD(startYD) - 5, 10, 10);
   if (nPoints > 2
       && (biggerStartBox.contains(sx, sy)
           || (ic.offScreenXD(sx) == startXD && ic.offScreenYD(sy) == startYD)
           || (samePoint && (System.currentTimeMillis() - mouseUpTime) <= 500))) {
     nPoints--;
     addOffset();
     finishPolygon();
     return;
   } else if (!samePoint) {
     mouseUpTime = System.currentTimeMillis();
     if (type == ANGLE && nPoints == 3) {
       addOffset();
       finishPolygon();
       return;
     }
     // add point to polygon
     if (xpf != null) {
       xpf[nPoints] = xpf[nPoints - 1];
       ypf[nPoints] = ypf[nPoints - 1];
       nPoints++;
       if (nPoints == xpf.length) enlargeArrays();
     } else {
       xp[nPoints] = xp[nPoints - 1];
       yp[nPoints] = yp[nPoints - 1];
       nPoints++;
       if (nPoints == xp.length) enlargeArrays();
     }
     // if (lineWidth>1) fitSpline();
   }
 }
Example #2
0
  public void fitSpline(int evaluationPoints) {
    if (xpf == null) {
      xpf = toFloat(xp);
      ypf = toFloat(yp);
      subPixel = true;
    }
    if (xSpline == null || splinePoints != evaluationPoints) {
      splinePoints = evaluationPoints;
      xSpline = new float[splinePoints];
      ySpline = new float[splinePoints];
    }
    int nNodes = nPoints;
    if (type == POLYGON) {
      nNodes++;
      if (nNodes >= xpf.length) enlargeArrays();
      xpf[nNodes - 1] = xpf[0];
      ypf[nNodes - 1] = ypf[0];
    }
    float[] xindex = new float[nNodes];
    for (int i = 0; i < nNodes; i++) xindex[i] = i;
    SplineFitter sfx = new SplineFitter(xindex, xpf, nNodes);
    SplineFitter sfy = new SplineFitter(xindex, ypf, nNodes);

    // Evaluate the splines at all points
    double scale = (double) (nNodes - 1) / (splinePoints - 1);
    float xs = 0f, ys = 0f;
    float xmin = Float.MAX_VALUE, xmax = -xmin, ymin = xmin, ymax = xmax;
    for (int i = 0; i < splinePoints; i++) {
      double xvalue = i * scale;
      xs = (float) sfx.evalSpline(xindex, xpf, nNodes, xvalue);
      if (xs < xmin) xmin = xs;
      if (xs > xmax) xmax = xs;
      xSpline[i] = xs;
      ys = (float) sfy.evalSpline(xindex, ypf, nNodes, xvalue);
      if (ys < ymin) ymin = ys;
      if (ys > ymax) ymax = ys;
      ySpline[i] = ys;
    }
    int ixmin = (int) Math.floor(xmin + 0.5f);
    int ixmax = (int) Math.floor(xmax + 0.5f);
    int iymin = (int) Math.floor(ymin + 0.5f);
    int iymax = (int) Math.floor(ymax + 0.5f);
    if (ixmin != 0) {
      for (int i = 0; i < nPoints; i++) xpf[i] -= ixmin;
      for (int i = 0; i < splinePoints; i++) xSpline[i] -= ixmin;
    }
    if (iymin != 0) {
      for (int i = 0; i < nPoints; i++) ypf[i] -= iymin;
      for (int i = 0; i < splinePoints; i++) ySpline[i] -= iymin;
    }
    x += ixmin;
    y += iymin;
    width = ixmax - ixmin;
    height = iymax - iymin;
    bounds = null;
    cachedMask = null;
    // update protected xp and yp arrays for backward compatibility
    xp = toInt(xpf, xp, nPoints);
    yp = toInt(ypf, yp, nPoints);
  }