package ip.gui;

import math.Mat;

/**
 * User: lyon
 * Date: Dec 22, 2002
 * Time: 1:53:40 AM
 */
public class Gauss {

    public static final double gauss(
            double x, double y,
            double xc, double yc, double sigma) {
        double dx = x - xc;
        double dy = y - yc;
        double dx2 = dx * dx;
        double dy2 = dy * dy;
        double sigma2 = sigma * sigma;
        double oneOnSigma2 = 1 / sigma2;
        return
                Math.exp(-(dx2 + dy2) *
                oneOnSigma2 / 2) / Math.PI * oneOnSigma2 / 2;
    }

    public static final double gauss(
            double x,
            double xc, double sigma) {
        double oneOnSigmaSquaredOn2 = 1 / (sigma * sigma) / 2;
        return
                Math.exp(-((x - xc) * (x - xc)) *
                oneOnSigmaSquaredOn2) / Math.PI * oneOnSigmaSquaredOn2;
    }

    public static void printGaussKernel(
            int M, int N,
            double sigma, double centerMax) {
        short k[][] = new short[M][N];
        int xc = M / 2;
        int yc = N / 2;
        double scale = centerMax * 2 * Math.PI * sigma * sigma;
        for (int x = 0; x < k.length; x++)
            for (int y = 0; y < k[0].length; y++)
                k[x][y] = (short)
                        (scale * gauss(x, y, xc, yc, sigma));
        //Mat.printKernel(k,"gauss"+k.length);
    }

    public static void printGaussKernel(
            int M, int N,
            double sigma) {
        float k[][] = getGaussKernel(M, N, sigma);
        Mat.printKernel(k, "gauss" + k.length);
    }

    public static float[][] getGaussKernel(
            int M, int N,
            double sigma) {
        float k[][] = new float[M][N];
        int xc = M / 2;
        int yc = N / 2;
        for (int x = 0; x < k.length; x++)
            for (int y = 0; y < k[0].length; y++)
                k[x][y] = (float) gauss(x, y, xc, yc, sigma);
        Mat.normalize(k);
        return k;
    }
}