Exemplo n.º 1
0
  // ---------------------------------------------------------
  private synchronized AffineTransform bufferTransform() {
    AffineTransform r = new AffineTransform();
    for (int i = 0; i < a1.size(); i++) {
      int s1 = a1.get(i);
      Object s2 = a2.get(i);
      Object s3[] = null;
      if (s2 instanceof Object[]) s3 = (Object[]) s2;

      if (s1 == setTransform) {
        r = makeTransform(s2);
      }
      if (s1 == shear) r.shear((Double) s3[0], (Double) s3[1]);
      if (s1 == rotate1) r.rotate((Double) s2);
      if (s1 == rotate2) r.rotate((Double) s3[0], (Double) s3[1], (Double) s3[2]);
      if (s1 == scale1) r.scale((Double) s3[0], (Double) s3[1]);
      if (s1 == translate1) r.translate((Double) s3[0], (Double) s3[1]);
      if (s1 == translate2) r.translate((Integer) s3[0], (Integer) s3[1]);
    }
    return r;
  }
Exemplo n.º 2
0
  public AffineTransform getAffineTransform(
      int width, int height, double[] _xyzSW, double[] _xyzSE, double[] _xyzNW) {
    int[] cornerNW = projection.screenProjection(_xyzNW);
    int[] cornerSE = projection.screenProjection(_xyzSE);
    int[] cornerSW = projection.screenProjection(_xyzSW);

    double[] vectWE = {
      (double) cornerSE[0] - (double) cornerSW[0], (double) cornerSE[1] - (double) cornerSW[1]
    };
    double normvectWE = sqrt(sqr(vectWE[0]) + sqr(vectWE[1]));
    double[] vectSN = {
      (double) cornerNW[0] - (double) cornerSW[0], (double) cornerNW[1] - (double) cornerSW[1]
    };
    double normvectSN = sqrt(sqr(vectSN[0]) + sqr(vectSN[1]));
    double angleSW =
        acos((vectWE[0] * vectSN[0] + vectWE[1] * vectSN[1]) / (normvectWE * normvectSN));

    if (angleSW == 0.0) {
      return null;
    }

    AffineTransform t = new AffineTransform();

    t.translate(cornerNW[0], cornerNW[1]);
    t.scale(sign(vectWE[0]), -sign(vectSN[1]));
    t.rotate(-atan(vectSN[0] / vectSN[1]));
    t.shear(0, 1 / tan(PI - angleSW));
    t.scale(normvectWE * cos(angleSW - PI / 2) / (double) width, normvectSN / (double) height);

    double[] _cornerSE_tr = new double[2];
    double[] _cornerSE = {width, height};
    t.transform(_cornerSE, 0, _cornerSE_tr, 0, 1);

    if (isDiff(_cornerSE_tr, cornerSE)) {
      double[] vectSE_NW_1 = {
        (double) cornerNW[0] - (double) cornerSE[0], (double) cornerNW[1] - (double) cornerSE[1]
      };
      double[] vectSE_NW_2 = {
        (double) cornerNW[0] - (double) _cornerSE_tr[0],
        (double) cornerNW[1] - (double) _cornerSE_tr[1]
      };

      double normvect_1 = sqrt(sqr(vectSE_NW_1[0]) + sqr(vectSE_NW_1[1]));
      double normvect_2 = sqrt(sqr(vectSE_NW_1[0]) + sqr(vectSE_NW_1[1]));

      double cos_angle =
          (((vectSE_NW_1[0] * vectSE_NW_2[0] + vectSE_NW_1[1] * vectSE_NW_2[1])
              / (normvect_1 * normvect_2)));
      double vect = (vectSE_NW_1[0] * vectSE_NW_2[1] - vectSE_NW_1[1] * vectSE_NW_2[0]);

      AffineTransform t2 = new AffineTransform();
      if (vect < 0) {
        t2.rotate(acos(cos_angle), cornerNW[0], cornerNW[1]);
      } else {
        t2.rotate(-acos(cos_angle), cornerNW[0], cornerNW[1]);
      }
      t.preConcatenate(t2);
    }

    // TODO patch for many cases...

    /*double[] _cornerSW_tr = new double[2];
    double[] _cornerSW = { 0, img.getHeight(canvas) };
    t.transform(_cornerSW, 0, _cornerSW_tr, 0, 1);

    if (isDiff(_cornerSW_tr, cornerSW)) {
    double[] vectSW_NW_1 = { (double) cornerNW[0] - (double) cornerSW[0], (double) cornerNW[1] - (double) cornerSW[1] };
    double[] vectSW_NW_2 = { (double) cornerNW[0] - (double) _cornerSW_tr[0], (double) cornerNW[1] - (double) _cornerSW_tr[1] };

    double normvect_1 = sqrt(sqr(vectSW_NW_1[0]) + sqr(vectSW_NW_1[1]));
    double normvect_2 = sqrt(sqr(vectSW_NW_1[0]) + sqr(vectSW_NW_1[1]));

    double cos_angle = (((vectSW_NW_1[0] * vectSW_NW_2[0] + vectSW_NW_1[1] * vectSW_NW_2[1]) / (normvect_1 * normvect_2)));
    double vect = (vectSW_NW_1[0] * vectSW_NW_2[1] - vectSW_NW_1[1] * vectSW_NW_2[0]);

    System.out.println(cos_angle + " " + vect + " -> " + toDegrees(acos(cos_angle)));

    //System.out.println(" "+vectSE_NW_1[0]+","+vectSE_NW_1[1]+"  "+vectSE_NW_2[0]+","+vectSE_NW_2[1]);
    AffineTransform t2 = new AffineTransform();
    if (vect > 0)
    t2.rotate(acos(cos_angle), cornerNW[0], cornerNW[1]);
    else
    t2.rotate(-acos(cos_angle), cornerNW[0], cornerNW[1]);
    t.preConcatenate(t2);

    }*/
    return t;
  }