Search in sources :

Example 6 with GMatrix

use of javax.vecmath.GMatrix in project imageio-ext by geosolutions-it.

the class InterpolateVNetCDF method Resampler.

private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue, final int tv) {
    final Index latIndex = latData.getIndex();
    final Index lonIndex = lonData.getIndex();
    final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
    final int XOFFSET = 0;
    final int YOFFSET = 1;
    final int stepX = 2;
    final int stepY = 2;
    int numNeededPoints = 0;
    for (int xi = 0; xi < imageWidth; xi += stepX) {
        for (int yi = 0; yi < imageHeight; yi += stepY) {
            numNeededPoints++;
        }
    }
    computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex, tv);
    float[] destCoords = new float[2 * numNeededPoints];
    float[] srcCoords = new float[2 * numNeededPoints];
    /*
         * Copy source and destination coordinates into float arrays. The
         * destination coordinates are scaled in order to gets values similar to
         * source coordinates (values will be identical if all "real world"
         * coordinates are grid indices multiplied by a constant).
         */
    final float xmin = xminTV[tv];
    final float xperiod = xperiodTV[tv];
    final float ymax = ymaxTV[tv];
    final float yperiod = yperiodTV[tv];
    int offset = 0;
    for (int yi = 0; yi < imageHeight; yi += stepY) {
        for (int xi = 0; xi < imageWidth; xi += stepX) {
            srcCoords[offset] = xi;
            srcCoords[offset + 1] = yi;
            destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - xmin) / xperiod);
            destCoords[offset + 1] = (float) ((ymax - latData.getFloat(latIndex.set(yi))) / yperiod);
            // destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi))
            // - this.ymin) / this.periodY);
            offset += 2;
        }
    }
    GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
    for (int coord = 0; coord < numNeededPoints; coord++) {
        int var = 0;
        for (int i = 0; i <= polyDegree; i++) {
            for (int j = 0; j <= i; j++) {
                double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
                A.setElement(coord, var++, value);
            }
        }
    }
    GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
    GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
    AtAi.mulTransposeLeft(A, A);
    AtAi.invert();
    Ap.mulTransposeRight(AtAi, A);
    GMatrix xVector = new GMatrix(numNeededPoints, 1);
    GMatrix yVector = new GMatrix(numNeededPoints, 1);
    for (int idx = 0; idx < numNeededPoints; idx++) {
        xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
        yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
    }
    GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
    GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
    xCoeffsG.mul(Ap, xVector);
    yCoeffsG.mul(Ap, yVector);
    float[] xCoeffs = new float[numCoeffs];
    float[] yCoeffs = new float[numCoeffs];
    for (int ii = 0; ii < numCoeffs; ii++) {
        xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
        yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
    }
    WritableRaster outDataCube;
    WritableRandomIter iteratorDataCube;
    SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
    DataBuffer.TYPE_FLOAT, // width
    imageWidth, // height
    imageHeight, // num bands
    1);
    outDataCube = Raster.createWritableRaster(outSampleModel, null);
    iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
    // Transfering data in the WritableRaster structure
    Index indexInputVar = data.getIndex();
    for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
        for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
            for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
                iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
            }
        }
    }
    WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
    for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
        for (int yi = 0; yi < imageHeight; yi++) {
            for (int xi = 0; xi < imageWidth; xi++) {
                float[] dstCoords = new float[2];
                GMatrix regressionVec = new GMatrix(numCoeffs, 1);
                int var = 0;
                for (int i = 0; i <= polyDegree; i++) {
                    for (int j = 0; j <= i; j++) {
                        double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
                        regressionVec.setElement(var++, 0, value);
                    }
                }
                GMatrix xG = new GMatrix(1, 1);
                GMatrix yG = new GMatrix(1, 1);
                xG.mulTransposeLeft(regressionVec, xCoeffsG);
                yG.mulTransposeLeft(regressionVec, yCoeffsG);
                int X = (int) Math.round(xG.getElement(0, 0));
                int Y = (int) Math.round(yG.getElement(0, 0));
                if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
                    target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
                } else {
                    // TODO: Change with fillvalue
                    // target.setSample(xi, yi, bi, Float.NaN);
                    target.setSample(xi, yi, bi, fillValue);
                }
            }
        }
    }
    return target;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) SampleModel(java.awt.image.SampleModel) GMatrix(javax.vecmath.GMatrix) WritableRaster(java.awt.image.WritableRaster) Index(ucar.ma2.Index)

Example 7 with GMatrix

use of javax.vecmath.GMatrix in project imageio-ext by geosolutions-it.

the class MercatorOceanConverter method Resampler.

private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue) {
    final Index latIndex = latData.getIndex();
    final Index lonIndex = lonData.getIndex();
    final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
    final int XOFFSET = 0;
    final int YOFFSET = 1;
    final int stepX = 2;
    final int stepY = 2;
    int numNeededPoints = 0;
    for (int xi = 0; xi < imageWidth; xi += stepX) {
        for (int yi = 0; yi < imageHeight; yi += stepY) {
            numNeededPoints++;
        }
    }
    computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex);
    float[] destCoords = new float[2 * numNeededPoints];
    float[] srcCoords = new float[2 * numNeededPoints];
    /*
         * Copy source and destination coordinates into float arrays. The
         * destination coordinates are scaled in order to gets values similar to
         * source coordinates (values will be identical if all "real world"
         * coordinates are grid indices multiplied by a constant).
         */
    int offset = 0;
    for (int yi = 0; yi < imageHeight; yi += stepY) {
        for (int xi = 0; xi < imageWidth; xi += stepX) {
            srcCoords[offset] = xi;
            srcCoords[offset + 1] = yi;
            destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - this.xmin) / this.periodX);
            destCoords[offset + 1] = (float) ((this.ymax - latData.getFloat(latIndex.set(yi))) / this.periodY);
            // destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi))
            // - this.ymin) / this.periodY);
            offset += 2;
        }
    }
    GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
    for (int coord = 0; coord < numNeededPoints; coord++) {
        int var = 0;
        for (int i = 0; i <= polyDegree; i++) {
            for (int j = 0; j <= i; j++) {
                double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
                A.setElement(coord, var++, value);
            }
        }
    }
    GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
    GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
    AtAi.mulTransposeLeft(A, A);
    AtAi.invert();
    Ap.mulTransposeRight(AtAi, A);
    GMatrix xVector = new GMatrix(numNeededPoints, 1);
    GMatrix yVector = new GMatrix(numNeededPoints, 1);
    for (int idx = 0; idx < numNeededPoints; idx++) {
        xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
        yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
    }
    GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
    GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
    xCoeffsG.mul(Ap, xVector);
    yCoeffsG.mul(Ap, yVector);
    float[] xCoeffs = new float[numCoeffs];
    float[] yCoeffs = new float[numCoeffs];
    for (int ii = 0; ii < numCoeffs; ii++) {
        xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
        yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
    }
    WritableRaster outDataCube;
    WritableRandomIter iteratorDataCube;
    SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
    DataBuffer.TYPE_FLOAT, // width
    imageWidth, // height
    imageHeight, // num bands
    1);
    outDataCube = Raster.createWritableRaster(outSampleModel, null);
    iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
    // Transfering data in the WritableRaster structure
    Index indexInputVar = data.getIndex();
    for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
        for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
            for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
                iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
            }
        }
    }
    WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
    for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
        for (int yi = 0; yi < imageHeight; yi++) {
            for (int xi = 0; xi < imageWidth; xi++) {
                GMatrix regressionVec = new GMatrix(numCoeffs, 1);
                int var = 0;
                for (int i = 0; i <= polyDegree; i++) {
                    for (int j = 0; j <= i; j++) {
                        double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
                        regressionVec.setElement(var++, 0, value);
                    }
                }
                GMatrix xG = new GMatrix(1, 1);
                GMatrix yG = new GMatrix(1, 1);
                xG.mulTransposeLeft(regressionVec, xCoeffsG);
                yG.mulTransposeLeft(regressionVec, yCoeffsG);
                int X = (int) Math.round(xG.getElement(0, 0));
                int Y = (int) Math.round(yG.getElement(0, 0));
                if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
                    target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
                } else {
                    // TODO: Change with fillvalue
                    // target.setSample(xi, yi, bi, Float.NaN);
                    target.setSample(xi, yi, bi, fillValue);
                }
            }
        }
    }
    return target;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) SampleModel(java.awt.image.SampleModel) GMatrix(javax.vecmath.GMatrix) WritableRaster(java.awt.image.WritableRaster) Index(ucar.ma2.Index)

Example 8 with GMatrix

use of javax.vecmath.GMatrix in project imageio-ext by geosolutions-it.

the class NetCDFCF_CLewis_Converter method Resampler.

private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue) {
    final Index latIndex = latData.getIndex();
    final Index lonIndex = lonData.getIndex();
    final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
    final int XOFFSET = 0;
    final int YOFFSET = 1;
    final int stepX = 2;
    final int stepY = 2;
    int numNeededPoints = 0;
    for (int xi = 0; xi < imageWidth; xi += stepX) {
        for (int yi = 0; yi < imageHeight; yi += stepY) {
            numNeededPoints++;
        }
    }
    computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex);
    float[] destCoords = new float[2 * numNeededPoints];
    float[] srcCoords = new float[2 * numNeededPoints];
    /*
         * Copy source and destination coordinates into float arrays. The
         * destination coordinates are scaled in order to gets values similar to
         * source coordinates (values will be identical if all "real world"
         * coordinates are grid indices multiplied by a constant).
         */
    int offset = 0;
    for (int yi = 0; yi < imageHeight; yi += stepY) {
        for (int xi = 0; xi < imageWidth; xi += stepX) {
            srcCoords[offset] = xi;
            srcCoords[offset + 1] = yi;
            destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - this.xmin) / this.periodX);
            destCoords[offset + 1] = (float) ((this.ymax - latData.getFloat(latIndex.set(yi))) / this.periodY);
            // destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi)) - this.ymin) / this.periodY);
            offset += 2;
        }
    }
    GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
    for (int coord = 0; coord < numNeededPoints; coord++) {
        int var = 0;
        for (int i = 0; i <= polyDegree; i++) {
            for (int j = 0; j <= i; j++) {
                double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
                A.setElement(coord, var++, value);
            }
        }
    }
    GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
    GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
    AtAi.mulTransposeLeft(A, A);
    AtAi.invert();
    Ap.mulTransposeRight(AtAi, A);
    GMatrix xVector = new GMatrix(numNeededPoints, 1);
    GMatrix yVector = new GMatrix(numNeededPoints, 1);
    for (int idx = 0; idx < numNeededPoints; idx++) {
        xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
        yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
    }
    GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
    GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
    xCoeffsG.mul(Ap, xVector);
    yCoeffsG.mul(Ap, yVector);
    float[] xCoeffs = new float[numCoeffs];
    float[] yCoeffs = new float[numCoeffs];
    for (int ii = 0; ii < numCoeffs; ii++) {
        xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
        yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
    }
    WritableRaster outDataCube;
    WritableRandomIter iteratorDataCube;
    SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
    DataBuffer.TYPE_FLOAT, // width
    imageWidth, // height
    imageHeight, // num bands
    1);
    outDataCube = Raster.createWritableRaster(outSampleModel, null);
    iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
    // Transfering data in the WritableRaster structure
    Index indexInputVar = data.getIndex();
    for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
        for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
            for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
                iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
            }
        }
    }
    WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
    for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
        for (int yi = 0; yi < imageHeight; yi++) {
            for (int xi = 0; xi < imageWidth; xi++) {
                float[] dstCoords = new float[2];
                GMatrix regressionVec = new GMatrix(numCoeffs, 1);
                int var = 0;
                for (int i = 0; i <= polyDegree; i++) {
                    for (int j = 0; j <= i; j++) {
                        double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
                        regressionVec.setElement(var++, 0, value);
                    }
                }
                GMatrix xG = new GMatrix(1, 1);
                GMatrix yG = new GMatrix(1, 1);
                xG.mulTransposeLeft(regressionVec, xCoeffsG);
                yG.mulTransposeLeft(regressionVec, yCoeffsG);
                int X = (int) Math.round(xG.getElement(0, 0));
                int Y = (int) Math.round(yG.getElement(0, 0));
                if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
                    target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
                } else {
                    // TODO: Change with fillvalue
                    // target.setSample(xi, yi, bi, Float.NaN);
                    target.setSample(xi, yi, bi, fillValue);
                }
            }
        }
    }
    return target;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) SampleModel(java.awt.image.SampleModel) GMatrix(javax.vecmath.GMatrix) WritableRaster(java.awt.image.WritableRaster) Index(ucar.ma2.Index)

Example 9 with GMatrix

use of javax.vecmath.GMatrix in project jwt by emweb.

the class WebGLUtils method multiply.

public static GVector multiply(Matrix4f M, GVector v) {
    assert (v.getSize() == 4);
    GVector result = new GVector(4);
    GMatrix gM = new GMatrix(4, 4);
    gM.set(M);
    result.mul(gM, v);
    return result;
}
Also used : GVector(javax.vecmath.GVector) GMatrix(javax.vecmath.GMatrix)

Aggregations

GMatrix (javax.vecmath.GMatrix)9 SampleModel (java.awt.image.SampleModel)5 WritableRaster (java.awt.image.WritableRaster)5 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)5 Index (ucar.ma2.Index)5 GVector (javax.vecmath.GVector)4 Test (org.junit.Test)2