3 Replies Latest reply: Jul 30, 2010 3:18 PM by 843802 RSS

    JAI Triangle to Triangle mapping

      Hi Friends,
      I am currently mapping one arbitrary triangle to another arbitrary triangle.
      I tried to use javax.media.jai.PerspectiveTransform, there is a method called getTriToTri
      However, it looks like the method is not there.
      If somebody will help me with using this method or advise some other way to implement the process, I would appreciate a lot!
        • 1. Re: JAI Triangle to Triangle mapping
          Please help me....
          • 2. Re: JAI Triangle to Triangle mapping
            A triangle to triangle mapping is an affine transformation, which would explain the difficulty in using a PerspectiveTransform to represent such a mapping (there would be many possible perspective transformations that achieve the same 3 point to 3 point result). As an affine transformation that preserves the parallelness of lines, what you want is
            public static PerspectiveTransform getTriToTri(
                    double x0, double y0,
                    double x1, double y1,
                    double x2, double y2,
                    double x0p, double y0p,
                    double x1p, double y1p,
                    double x2p, double y2p) throws NoninvertibleTransformException{
                double[][] inv;
                     PerspectiveTransform coeff =  new PerspectiveTransform(
                            new double[][]{{x0,y0,1},
                     double det = coeff.getDeterminant();
                     inv = coeff.createAdjoint().getMatrix(new double[3][3]);
                     for(int row = 0; row < 3; row++) {
                         for(int col = 0; col < 3; col++) {
                             inv[row][col] /= det;
                }catch(CloneNotSupportedException e) {
                    throw new Error(e);
                double[][] m = new double[][] {
                    inv[0][0] * x0p + inv[0][1] * x1p + inv[0][2] * x2p, //m00
                    inv[1][0] * x0p + inv[1][1] * x1p + inv[1][2] * x2p, //m01
                    inv[2][0] * x0p + inv[2][1] * x1p + inv[2][2] * x2p, //m02
                    inv[0][0] * y0p + inv[0][1] * y1p + inv[0][2] * y2p, //m10
                    inv[1][0] * y0p + inv[1][1] * y1p + inv[1][2] * y2p, //m11
                    inv[2][0] * y0p + inv[2][1] * y1p + inv[2][2] * y2p, //m12
                    0, 0, 1 //m20, m21, m22
                return new PerspectiveTransform(m);
            • 3. Re: JAI Triangle to Triangle mapping
              Thanks very very much for your code.
              The other suggestion is to get every pixels in one triangle.
              Use barycentric coordinates system to calculate the position in source triangle.
              Then calculate the barycentric coordinates in target triangle.
              The colour will be decided by implementing bilinear Interpolation on each pixel.

              I could not figure out this process, so I am try to use JAI to transform the triangles.

              Again, thanks very much for your help.
              I will try your code first.